]> git.wh0rd.org - tt-rss.git/blame - functions.php
UNDO: add lang dropbox on login form
[tt-rss.git] / functions.php
CommitLineData
1d3a17c7 1<?php
f1a80dae 2
894ebcf5 3/* if ($_GET["debug"]) {
cce28758
AD
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
5 } else {
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
894ebcf5 7 } */
cce28758 8
40d13c28 9 require_once 'config.php';
cc17c205 10
8d039718 11 if (ENABLE_TRANSLATIONS == true) {
865220a4
AD
12 require_once "accept-to-gettext.php";
13 require_once "gettext/gettext.inc";
aba609e0 14
8d039718
AD
15 function startup_gettext() {
16
17 # Get locale from Accept-Language header
08e81c0b 18 $lang = al2gt(array("en_US", "ru_RU", "zh_CN", "fr_FR"), "text/html");
89cb787e
AD
19
20 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
21 $lang = _TRANSLATION_OVERRIDE_DEFAULT;
22 }
23
8d039718
AD
24 if ($lang) {
25 _setlocale(LC_MESSAGES, $lang);
26 _bindtextdomain("messages", "locale");
27 _textdomain("messages");
28 _bind_textdomain_codeset("messages", "UTF-8");
29 }
aba609e0 30 }
aba609e0 31
cc17c205 32 startup_gettext();
865220a4
AD
33
34 } else {
35 function __($msg) {
36 return $msg;
37 }
38 function startup_gettext() {
39 // no-op
40 return true;
41 }
cc17c205
AD
42 }
43
b619ff15 44 require_once 'db-prefs.php';
5bc0bd27 45 require_once 'compat.php';
af106b0e 46 require_once 'errors.php';
8911ac8b 47 require_once 'version.php';
40d13c28 48
49f9c923 49 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
a3ee2a38
AD
50 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
51
49f9c923
AD
52 require_once "magpierss/rss_fetch.inc";
53 require_once 'magpierss/rss_utils.inc';
54
f738aef1
AD
55 include_once "tw/tw-config.php";
56 include_once "tw/tw.php";
57 include_once TW_SETUP . "paranoya.php";
58
59 $tw_parser = new twParser();
60
6f9e33e4
AD
61 function _debug($msg) {
62 $ts = strftime("%H:%M:%S", time());
63 print "[$ts] $msg\n";
64 }
65
ad507f85
AD
66 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
67
68 $rows = -1;
4c193675 69
fefa6ca3 70 if (DB_TYPE == "pgsql") {
44e241cb 71/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 72 marked = false AND feed_id = '$feed_id' AND
35d8cf43 73 (SELECT date_entered FROM ttrss_entries WHERE
44e241cb
AD
74 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
75
6e7f8d26
AD
76 $pg_version = get_pgsql_version($link);
77
78 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35
AD
79
80 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
81 ttrss_entries.id = ref_id AND
82 marked = false AND
83 feed_id = '$feed_id' AND
84 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
85
86 } else {
87
88 $result = db_query($link, "DELETE FROM ttrss_user_entries
89 USING ttrss_entries
90 WHERE ttrss_entries.id = ref_id AND
91 marked = false AND
92 feed_id = '$feed_id' AND
fc774155 93 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 94 }
ad507f85
AD
95
96 $rows = pg_affected_rows($result);
97
fefa6ca3 98 } else {
1e59ae35 99
30f1746f 100/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 101 marked = false AND feed_id = '$feed_id' AND
35d8cf43 102 (SELECT date_entered FROM ttrss_entries WHERE
30f1746f
AD
103 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
104
105 $result = db_query($link, "DELETE FROM ttrss_user_entries
106 USING ttrss_user_entries, ttrss_entries
107 WHERE ttrss_entries.id = ref_id AND
108 marked = false AND
109 feed_id = '$feed_id' AND
110 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
111
ad507f85
AD
112 $rows = mysql_affected_rows($link);
113
114 }
115
116 if ($debug) {
6f9e33e4 117 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
fefa6ca3
AD
118 }
119 }
120
44e241cb
AD
121 function global_purge_old_posts($link, $do_output = false, $limit = false) {
122
894ebcf5 123 $random_qpart = sql_random_function();
fefa6ca3 124
44e241cb
AD
125 if ($limit) {
126 $limit_qpart = "LIMIT $limit";
127 } else {
128 $limit_qpart = "";
129 }
130
fefa6ca3 131 $result = db_query($link,
44e241cb
AD
132 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
133 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
134
135 while ($line = db_fetch_assoc($result)) {
136
137 $feed_id = $line["id"];
138 $purge_interval = $line["purge_interval"];
139 $owner_uid = $line["owner_uid"];
140
141 if ($purge_interval == 0) {
142
143 $tmp_result = db_query($link,
144 "SELECT value FROM ttrss_user_prefs WHERE
145 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
146
147 if (db_num_rows($tmp_result) != 0) {
148 $purge_interval = db_fetch_result($tmp_result, 0, "value");
149 }
150 }
151
152 if ($do_output) {
ad507f85 153// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
154 }
155
156 if ($purge_interval > 0) {
ad507f85 157 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3
AD
158 }
159 }
160
71604ca4
AD
161 // purge orphaned posts in main content table
162 db_query($link, "DELETE FROM ttrss_entries WHERE
163 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
164
fefa6ca3
AD
165 }
166
b6eefba5 167 function purge_old_posts($link) {
5d73494a 168
f1a80dae
AD
169 $user_id = $_SESSION["uid"];
170
171 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
172 WHERE owner_uid = '$user_id'");
5d73494a
AD
173
174 while ($line = db_fetch_assoc($result)) {
175
176 $feed_id = $line["id"];
177 $purge_interval = $line["purge_interval"];
178
b619ff15 179 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 180
140aae81 181 if ($purge_interval > 0) {
fefa6ca3 182 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
183 }
184 }
71604ca4
AD
185
186 // purge orphaned posts in main content table
187 db_query($link, "DELETE FROM ttrss_entries WHERE
188 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
189 }
190
1f2b01ed 191 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 192
4769ddaf 193 if (WEB_DEMO_MODE) return;
b0b4abcf 194
a2770077
AD
195 if (!$user_id) {
196 $user_id = $_SESSION["uid"];
197 purge_old_posts($link);
198 }
199
25af8dad 200// db_query($link, "BEGIN");
b82af8c3 201
cbd8650d
AD
202 if (MAX_UPDATE_TIME > 0) {
203 if (DB_TYPE == "mysql") {
204 $q_order = "RAND()";
205 } else {
206 $q_order = "RANDOM()";
207 }
208 } else {
209 $q_order = "last_updated DESC";
210 }
211
d148926e 212 $result = db_query($link, "SELECT feed_url,id,
798f722b 213 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd 214 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
cbd8650d
AD
215 ORDER BY $q_order");
216
217 $upd_start = time();
40d13c28 218
b6eefba5 219 while ($line = db_fetch_assoc($result)) {
d148926e
AD
220 $upd_intl = $line["update_interval"];
221
b619ff15 222 if (!$upd_intl || $upd_intl == 0) {
e289ca71 223 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
b619ff15 224 }
d148926e 225
c1e202b7
AD
226 if ($upd_intl < 0) {
227 // Updates for this feed are disabled
228 continue;
229 }
230
93d40f50
AD
231 if ($fetch || (!$line["last_updated"] ||
232 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 233
cbd8650d
AD
234// print "<!-- feed: ".$line["feed_url"]." -->";
235
1f2b01ed 236 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
cbd8650d
AD
237
238 $upd_elapsed = time() - $upd_start;
239
240 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
241 return;
242 }
d148926e 243 }
40d13c28
AD
244 }
245
25af8dad 246// db_query($link, "COMMIT");
b82af8c3 247
40d13c28
AD
248 }
249
4065b60b
AD
250 function fetch_file_contents($url) {
251 if (USE_CURL_FOR_ICONS) {
252 $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
253
254 $ch = curl_init($url);
255 $fp = fopen($tmpfile, "w");
256
257 if ($fp) {
258 curl_setopt($ch, CURLOPT_FILE, $fp);
dd966fed
AD
259 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
260 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
4065b60b
AD
261 curl_exec($ch);
262 curl_close($ch);
263 fclose($fp);
264 }
265
266 $contents = file_get_contents($tmpfile);
267 unlink($tmpfile);
268
269 return $contents;
270
271 } else {
272 return file_get_contents($url);
273 }
274
275 }
78800912 276
4065b60b
AD
277 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
278 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
78800912 279
4065b60b 280 function get_favicon_url($url) {
99331724 281
4065b60b 282 if ($html = @fetch_file_contents($url)) {
78800912 283
4065b60b
AD
284 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
285 // Attempt to grab a favicon link from their webpage url
286 $linkUrl = html_entity_decode($matches[1]);
c798704b 287
4065b60b
AD
288 if (substr($linkUrl, 0, 1) == '/') {
289 $urlParts = parse_url($url);
290 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
291 } else if (substr($linkUrl, 0, 7) == 'http://') {
292 $faviconURL = $linkUrl;
293 } else if (substr($url, -1, 1) == '/') {
294 $faviconURL = $url.$linkUrl;
295 } else {
296 $faviconURL = $url.'/'.$linkUrl;
e695fdc8 297 }
717f5e64 298
c798704b 299 } else {
4065b60b
AD
300 // If unsuccessful, attempt to "guess" the favicon location
301 $urlParts = parse_url($url);
302 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
303 }
304 }
c798704b 305
4065b60b
AD
306 // Run a test to see if what we have attempted to get actually exists.
307 if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) {
308 return $faviconURL;
309 } else {
310 return false;
311 }
312 }
313
314 function url_validate($link) {
315
316 $url_parts = @parse_url($link);
317
318 if ( empty( $url_parts["host"] ) )
319 return false;
320
321 if ( !empty( $url_parts["path"] ) ) {
322 $documentpath = $url_parts["path"];
323 } else {
324 $documentpath = "/";
325 }
326
327 if ( !empty( $url_parts["query"] ) )
328 $documentpath .= "?" . $url_parts["query"];
329
330 $host = $url_parts["host"];
331 $port = $url_parts["port"];
332
333 if ( empty($port) )
334 $port = "80";
335
336 $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
337
338 if ( !$socket )
339 return false;
c798704b 340
4065b60b
AD
341 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
342
343 $http_response = fgets( $socket, 22 );
344
345 $responses = "/(200 OK)|(30[0-9] Moved)/";
346 if ( preg_match($responses, $http_response) ) {
347 fclose($socket);
348 return true;
349 } else {
350 return false;
351 }
352
353 }
354
355 function check_feed_favicon($site_url, $feed, $link) {
356 $favicon_url = get_favicon_url($site_url);
357
358# print "FAVICON [$site_url]: $favicon_url\n";
359
360 error_reporting(0);
361
362 $icon_file = ICONS_DIR . "/$feed.ico";
363
364 if ($favicon_url && !file_exists($icon_file)) {
365 $contents = fetch_file_contents($favicon_url);
366
367 $fp = fopen($icon_file, "w");
78800912 368
4065b60b
AD
369 if ($fp) {
370 fwrite($fp, $contents);
371 fclose($fp);
372 chmod($icon_file, 0644);
373 }
78800912 374 }
4065b60b
AD
375
376 error_reporting(DEFAULT_ERROR_LEVEL);
377
78800912
AD
378 }
379
ddb68b81 380 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 381
ddb68b81 382 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
383 return;
384 }
385
4bc64807 386 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 387 _debug("update_rss_feed: start");
219bd8fc
AD
388 }
389
47c6c988 390 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
a88c1f36
AD
391 FROM ttrss_feeds WHERE id = '$feed'");
392
47439031
AD
393 $auth_login = db_fetch_result($result, 0, "auth_login");
394 $auth_pass = db_fetch_result($result, 0, "auth_pass");
47c6c988 395
a88c1f36
AD
396 $update_interval = db_fetch_result($result, 0, "update_interval");
397
398 if ($update_interval < 0) { return; }
399
ab3d0b99
AD
400 $feed = db_escape_string($feed);
401
47c6c988
AD
402 $fetch_url = $feed_url;
403
404 if ($auth_login && $auth_pass) {
405 $url_parts = array();
406 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
407
408 if ($url_parts[1] && $url_parts[2]) {
409 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
410 }
411
412 }
ab3d0b99 413
4bc64807 414 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 415 _debug("update_rss_feed: fetching...");
219bd8fc
AD
416 }
417
418 if (!defined('DAEMON_EXTENDED_DEBUG')) {
419 error_reporting(0);
420 }
421
49f9c923 422 $rss = fetch_rss($fetch_url);
219bd8fc 423
4bc64807 424 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 425 _debug("update_rss_feed: fetch done, parsing...");
219bd8fc
AD
426 } else {
427 error_reporting (DEFAULT_ERROR_LEVEL);
428 }
429
b6eefba5 430 $feed = db_escape_string($feed);
dcee8f61 431
49f9c923 432 if ($rss) {
50b62214 433
4bc64807 434 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
435 _debug("update_rss_feed: processing feed data...");
436 }
437
44e241cb 438// db_query($link, "BEGIN");
dd8c76a9 439
a88c1f36 440 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 441 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 442
b6eefba5
AD
443 $registered_title = db_fetch_result($result, 0, "title");
444 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 445 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 446
7fed1940
AD
447 $owner_uid = db_fetch_result($result, 0, "owner_uid");
448
8d0ec6fd 449 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
4bc64807 450 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
451 _debug("update_rss_feed: checking favicon...");
452 }
4065b60b 453 check_feed_favicon($rss->channel["link"], $feed, $link);
a2770077
AD
454 }
455
746b249f 456 if (!$registered_title || $registered_title == "[Unknown]") {
4bc64807 457
49f9c923 458 $feed_title = db_escape_string($rss->channel["title"]);
4bc64807
AD
459
460 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
461 _debug("update_rss_feed: registering title: $feed_title");
462 }
7c5a308d 463
f324892e
AD
464 db_query($link, "UPDATE ttrss_feeds SET
465 title = '$feed_title' WHERE id = '$feed'");
466 }
467
49f9c923
AD
468 $site_url = $rss->channel["link"];
469 // weird, weird Magpie
470 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
147f7691
AD
471
472 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
473 db_query($link, "UPDATE ttrss_feeds SET
474 site_url = '$site_url' WHERE id = '$feed'");
331900c6 475 }
40d13c28 476
b7f4bda2
AD
477// print "I: " . $rss->channel["image"]["url"];
478
49f9c923 479 $icon_url = $rss->image["url"];
b7f4bda2 480
147f7691 481 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
482 $icon_url = db_escape_string($icon_url);
483 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
484 }
485
4bc64807 486 if (defined('DAEMON_EXTENDED_DEBUG' || $_GET['xdebug'])) {
50b62214
AD
487 _debug("update_rss_feed: loading filters...");
488 }
e6155a06
AD
489
490 $filters = array();
491
4b3dff6e 492 $result = db_query($link, "SELECT reg_exp,
db42b934 493 ttrss_filter_types.name AS name,
073ca0e6 494 ttrss_filter_actions.name AS action,
c2d9322b 495 inverse,
073ca0e6 496 action_param
db42b934 497 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
e8b79d16 498 enabled = true AND
db42b934
AD
499 owner_uid = $owner_uid AND
500 ttrss_filter_types.id = filter_type AND
501 ttrss_filter_actions.id = action_id AND
f8382011 502 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
e6155a06 503
b6eefba5 504 while ($line = db_fetch_assoc($result)) {
e6155a06 505 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
506
507 $filter["reg_exp"] = $line["reg_exp"];
508 $filter["action"] = $line["action"];
073ca0e6 509 $filter["action_param"] = $line["action_param"];
c2d9322b 510 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
073ca0e6 511
19c9cb11 512 array_push($filters[$line["name"]], $filter);
e6155a06
AD
513 }
514
49f9c923 515 $iterator = $rss->items;
7c5a308d 516
49f9c923
AD
517 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
518 if (!$iterator || !is_array($iterator)) $iterator = $rss;
c22789da
AD
519
520 if (!is_array($iterator)) {
39541e74 521 /* db_query($link, "UPDATE ttrss_feeds
75bd0669 522 SET last_error = 'Parse error: can\'t find any articles.'
77f0a2a7
AD
523 WHERE id = '$feed'"); */
524
525 // clear any errors and mark feed as updated if fetched okay
526 // even if it's blank
527
844012bc
AD
528 if (defined('DAEMON_EXTENDED_DEBUG')) {
529 _debug("update_rss_feed: entry iterator is not an array, no articles?");
530 }
531
77f0a2a7
AD
532 db_query($link, "UPDATE ttrss_feeds
533 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
534
535 return; // no articles
c22789da 536 }
ddb68b81 537
50b62214
AD
538 if (defined('DAEMON_EXTENDED_DEBUG')) {
539 _debug("update_rss_feed: processing articles...");
540 }
541
ddb68b81 542 foreach ($iterator as $item) {
7c5a308d 543
be832a1a 544 $entry_guid = $item["id"];
34e420fb 545
be832a1a
AD
546 if (!$entry_guid) $entry_guid = $item["guid"];
547 if (!$entry_guid) $entry_guid = $item["link"];
548 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
549
34e420fb
AD
550 if (defined('DAEMON_EXTENDED_DEBUG')) {
551 _debug("update_rss_feed: guid $entry_guid");
552 }
553
be832a1a
AD
554 if (!$entry_guid) continue;
555
556 $entry_timestamp = "";
557
558 $rss_2_date = $item['pubdate'];
559 $rss_1_date = $item['dc']['date'];
560 $atom_date = $item['issued'];
561 if (!$atom_date) $atom_date = $item['updated'];
562
563 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
564 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
565 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3 566
2e930846 567 if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
be832a1a
AD
568 $entry_timestamp = time();
569 $no_orig_date = 'true';
570 } else {
571 $no_orig_date = 'false';
572 }
573
574 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
575
576 $entry_title = trim(strip_tags($item["title"]));
577
578 // strange Magpie workaround
579 $entry_link = $item["link_"];
580 if (!$entry_link) $entry_link = $item["link"];
581
582 if (!$entry_title) continue;
1f64b1be 583# if (!$entry_link) continue;
7d3ab0dd 584
be832a1a 585 $entry_link = strip_tags($entry_link);
7d3ab0dd 586
be832a1a 587 $entry_content = $item["content:escaped"];
bbb416e5 588
be832a1a
AD
589 if (!$entry_content) $entry_content = $item["content:encoded"];
590 if (!$entry_content) $entry_content = $item["content"];
591 if (!$entry_content) $entry_content = $item["atom_content"];
592 if (!$entry_content) $entry_content = $item["summary"];
593 if (!$entry_content) $entry_content = $item["description"];
bbb416e5 594
be832a1a 595// if (!$entry_content) continue;
bbb416e5 596
be832a1a
AD
597 // WTF
598 if (is_array($entry_content)) {
599 $entry_content = $entry_content["encoded"];
600 if (!$entry_content) $entry_content = $entry_content["escaped"];
601 }
602
603// print_r($item);
604// print_r(htmlspecialchars($entry_content));
605// print "<br>";
606
607 $entry_content_unescaped = $entry_content;
be832a1a
AD
608
609 $entry_comments = strip_tags($item["comments"]);
610
611 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
612
83f114c8 613 if ($item['author']) {
83f114c8 614
4d6e9157
AD
615 if (is_array($item['author'])) {
616
617 if (!$entry_author) {
618 $entry_author = db_escape_string(strip_tags($item['author']['name']));
619 }
620
621 if (!$entry_author) {
622 $entry_author = db_escape_string(strip_tags($item['author']['email']));
623 }
83f114c8
AD
624 }
625
626 if (!$entry_author) {
627 $entry_author = db_escape_string(strip_tags($item['author']));
628 }
be832a1a
AD
629 }
630
83f114c8
AD
631 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
632
be832a1a
AD
633 $entry_guid = db_escape_string(strip_tags($entry_guid));
634
635 $result = db_query($link, "SELECT id FROM ttrss_entries
636 WHERE guid = '$entry_guid'");
637
638 $entry_content = db_escape_string($entry_content);
7e43ad58
AD
639
640 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
641
be832a1a
AD
642 $entry_title = db_escape_string($entry_title);
643 $entry_link = db_escape_string($entry_link);
2544f36b
AD
644 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
645 $entry_author = mb_substr($entry_author, 0, 250);
be832a1a
AD
646
647 $num_comments = db_escape_string($item["slash"]["comments"]);
648
649 if (!$num_comments) $num_comments = 0;
650
fefef828 651 // parse <category> entries into tags
be832a1a 652
fefef828 653 $t_ctr = $item['category#'];
be832a1a 654
fefef828
AD
655 $additional_tags = false;
656
657 if ($t_ctr == 0) {
658 $additional_tags = false;
659 } else if ($t_ctr == 1) {
660 $additional_tags = array($item['category']);
661 } else {
662 $additional_tags = array();
663 for ($i = 0; $i <= $t_ctr; $i++ ) {
664 if ($item["category#$i"]) {
665 array_push($additional_tags, $item["category#$i"]);
666 }
667 }
668 }
669
670 // parse <dc:subject> elements
671
672 $t_ctr = $item['dc']['subject#'];
673
674 if ($t_ctr == 1) {
675 $additional_tags = array($item['dc']['subject']);
676 } else if ($t_ctr > 1) {
677 $additional_tags = array();
678 for ($i = 0; $i <= $t_ctr; $i++ ) {
679 if ($item['dc']["subject#$i"]) {
680 array_push($additional_tags, $item['dc']["subject#$i"]);
681 }
682 }
683 }
8add756a 684
d48d160c 685 # sanitize content
183ad07b 686
007a38d4 687// $entry_content = sanitize_rss($entry_content);
d48d160c 688
34e420fb
AD
689 if (defined('DAEMON_EXTENDED_DEBUG')) {
690 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
691 }
692
44e241cb
AD
693 db_query($link, "BEGIN");
694
4c193675
AD
695 if (db_num_rows($result) == 0) {
696
34e420fb
AD
697 if (defined('DAEMON_EXTENDED_DEBUG')) {
698 _debug("update_rss_feed: base guid not found");
699 }
700
4c193675
AD
701 // base post entry does not exist, create it
702
4c193675
AD
703 $result = db_query($link,
704 "INSERT INTO ttrss_entries
705 (title,
706 guid,
707 link,
708 updated,
709 content,
710 content_hash,
711 no_orig_date,
712 date_entered,
11b0dce2 713 comments,
b6104dee
AD
714 num_comments,
715 author)
4c193675
AD
716 VALUES
717 ('$entry_title',
718 '$entry_guid',
719 '$entry_link',
720 '$entry_timestamp_fmt',
721 '$entry_content',
722 '$content_hash',
723 $no_orig_date,
724 NOW(),
11b0dce2 725 '$entry_comments',
b6104dee
AD
726 '$num_comments',
727 '$entry_author')");
8926aab8
AD
728 } else {
729 // we keep encountering the entry in feeds, so we need to
730 // update date_entered column so that we don't get horrible
731 // dupes when the entry gets purged and reinserted again e.g.
732 // in the case of SLOW SLOW OMG SLOW updating feeds
733
734 $base_entry_id = db_fetch_result($result, 0, "id");
735
736 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
737 WHERE id = '$base_entry_id'");
4c193675
AD
738 }
739
740 // now it should exist, if not - bad luck then
741
6385315d
AD
742 $result = db_query($link, "SELECT
743 id,content_hash,no_orig_date,title,
8926aab8 744 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
745 substring(updated,1,19) as updated,
746 num_comments
6385315d
AD
747 FROM
748 ttrss_entries
749 WHERE guid = '$entry_guid'");
4c193675
AD
750
751 if (db_num_rows($result) == 1) {
752
34e420fb 753 if (defined('DAEMON_EXTENDED_DEBUG')) {
7ca91eb3 754 _debug("update_rss_feed: base guid found, checking for user record");
34e420fb
AD
755 }
756
11b0dce2
AD
757 // this will be used below in update handler
758 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
759 $orig_title = db_fetch_result($result, 0, "title");
760 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
761 $orig_date_entered = strtotime(db_fetch_result($result,
762 0, "date_entered"));
6385315d 763
11b0dce2 764 $ref_id = db_fetch_result($result, 0, "id");
4c193675 765
11b0dce2 766 // check for user post link to main table
4c193675 767
11b0dce2 768 // do we allow duplicate posts with same GUID in different feeds?
8d0ec6fd 769 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
11b0dce2
AD
770 $dupcheck_qpart = "AND feed_id = '$feed'";
771 } else {
772 $dupcheck_qpart = "";
773 }
71604ca4 774
11b0dce2 775// error_reporting(0);
19c9cb11 776
f8382011
AD
777 $article_filters = get_article_filters($filters, $entry_title,
778 $entry_content, $entry_link);
19c9cb11 779
7ca91eb3
AD
780 if (defined('DAEMON_EXTENDED_DEBUG')) {
781 _debug("update_rss_feed: article filters: ");
782 if (count($article_filters) != 0) {
783 print_r($article_filters);
784 }
785 }
786
f8382011 787 if (find_article_filter($article_filters, "filter")) {
11b0dce2
AD
788 continue;
789 }
19c9cb11 790
11b0dce2 791// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 792
11b0dce2
AD
793 $result = db_query($link,
794 "SELECT ref_id FROM ttrss_user_entries WHERE
795 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
796 $dupcheck_qpart");
7ca91eb3 797
11b0dce2
AD
798 // okay it doesn't exist - create user entry
799 if (db_num_rows($result) == 0) {
800
7ca91eb3
AD
801 if (defined('DAEMON_EXTENDED_DEBUG')) {
802 _debug("update_rss_feed: user record not found, creating...");
803 }
804
f8382011 805 if (!find_article_filter($article_filters, 'catchup')) {
11b0dce2
AD
806 $unread = 'true';
807 $last_read_qpart = 'NULL';
808 } else {
809 $unread = 'false';
810 $last_read_qpart = 'NOW()';
811 }
dd7d3187 812
f8382011 813 if (find_article_filter($article_filters, 'mark')) {
dd7d3187
AD
814 $marked = 'true';
815 } else {
816 $marked = 'false';
817 }
a36c0dfe
AD
818
819 if (find_article_filter($article_filters, 'publish')) {
820 $published = 'true';
821 } else {
822 $published = 'false';
823 }
824
11b0dce2
AD
825 $result = db_query($link,
826 "INSERT INTO ttrss_user_entries
a36c0dfe 827 (ref_id, owner_uid, feed_id, unread, last_read, marked, published)
11b0dce2 828 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
a36c0dfe 829 $last_read_qpart, $marked, $published)");
11b0dce2
AD
830 }
831
6385315d
AD
832 $post_needs_update = false;
833
8d0ec6fd 834 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
6385315d 835 ($content_hash != $orig_content_hash)) {
7e43ad58 836// print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
6385315d
AD
837 $post_needs_update = true;
838 }
839
7e43ad58 840 if (db_escape_string($orig_title) != $entry_title) {
6385315d
AD
841 $post_needs_update = true;
842 }
843
11b0dce2
AD
844 if ($orig_num_comments != $num_comments) {
845 $post_needs_update = true;
846 }
847
6385315d
AD
848// this doesn't seem to be very reliable
849//
850// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
851// $post_needs_update = true;
852// }
853
854 // if post needs update, update it and mark all user entries
1c73bc0c 855 // linking to this post as updated
6385315d
AD
856 if ($post_needs_update) {
857
7ca91eb3
AD
858 if (defined('DAEMON_EXTENDED_DEBUG')) {
859 _debug("update_rss_feed: post $entry_guid needs update...");
860 }
861
6385315d
AD
862// print "<!-- post $orig_title needs update : $post_needs_update -->";
863
6385315d 864 db_query($link, "UPDATE ttrss_entries
11b0dce2 865 SET title = '$entry_title', content = '$entry_content',
7e43ad58 866 content_hash = '$content_hash',
11b0dce2 867 num_comments = '$num_comments'
6385315d
AD
868 WHERE id = '$ref_id'");
869
8d0ec6fd 870 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
4919fb42
AD
871 db_query($link, "UPDATE ttrss_user_entries
872 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
873 } else {
874 db_query($link, "UPDATE ttrss_user_entries
875 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
876 }
6385315d
AD
877
878 }
4c193675
AD
879 }
880
44e241cb
AD
881 db_query($link, "COMMIT");
882
34e420fb
AD
883 if (defined('DAEMON_EXTENDED_DEBUG')) {
884 _debug("update_rss_feed: looking for tags...");
885 }
886
eb36b4eb 887 /* taaaags */
40e1a95b 888 // <a href="..." rel="tag">Xorg</a>, //
eb36b4eb 889
05732aa0 890 $entry_tags = null;
eb36b4eb 891
40e1a95b 892 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
ee2c3050
AD
893 $entry_content_unescaped, $entry_tags);
894
fefef828
AD
895/* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
896 print_r($entry_tags);
897 print "<br/></p>"; */
eb36b4eb
AD
898
899 $entry_tags = $entry_tags[1];
900
073ca0e6
AD
901 # check for manual tags
902
f8382011
AD
903 $tag_filter = find_article_filter($article_filters, "tag");
904
905 if ($tag_filter) {
073ca0e6 906
f8382011 907 $manual_tags = trim_array(split(",", $tag_filter[1]));
073ca0e6 908
f8382011 909 foreach ($manual_tags as $tag) {
be832a1a
AD
910 if (tag_is_valid($tag)) {
911 array_push($entry_tags, $tag);
912 }
913 }
914 }
915
11c9ea1f
AD
916 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
917 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
8fc70781 918
fefef828
AD
919 if ($additional_tags && is_array($additional_tags)) {
920 foreach ($additional_tags as $tag) {
156a785d
AD
921 if (tag_is_valid($tag) &&
922 array_search($tag, $boring_tags) === FALSE) {
073ca0e6
AD
923 array_push($entry_tags, $tag);
924 }
925 }
fefef828
AD
926 }
927
fcc95e24 928// print "<p>TAGS: "; print_r($entry_tags); print "</p>";
073ca0e6 929
eb36b4eb
AD
930 if (count($entry_tags) > 0) {
931
44e241cb
AD
932 db_query($link, "BEGIN");
933
05732aa0
AD
934 $result = db_query($link, "SELECT id,int_id
935 FROM ttrss_entries,ttrss_user_entries
25da6909 936 WHERE guid = '$entry_guid'
05732aa0 937 AND feed_id = '$feed' AND ref_id = id
7fed1940 938 AND owner_uid = '$owner_uid'");
eb36b4eb 939
fe99ab12 940 if (db_num_rows($result) == 1) {
eb36b4eb 941
fe99ab12
AD
942 $entry_id = db_fetch_result($result, 0, "id");
943 $entry_int_id = db_fetch_result($result, 0, "int_id");
944
945 foreach ($entry_tags as $tag) {
fefef828 946
14b6c54b 947 $tag = sanitize_tag($tag);
fefef828 948 $tag = db_escape_string($tag);
31483fc1 949
ef063748
AD
950 if (!tag_is_valid($tag)) continue;
951
fe99ab12
AD
952 $result = db_query($link, "SELECT id FROM ttrss_tags
953 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
954 owner_uid = '$owner_uid' LIMIT 1");
955
956 // print db_fetch_result($result, 0, "id");
957
958 if ($result && db_num_rows($result) == 0) {
959
fe99ab12
AD
960 db_query($link, "INSERT INTO ttrss_tags
961 (owner_uid,tag_name,post_int_id)
962 VALUES ('$owner_uid','$tag', '$entry_int_id')");
963 }
964 }
eb36b4eb 965 }
44e241cb 966 db_query($link, "COMMIT");
05732aa0 967 }
4c193675 968 }
40d13c28 969
ab3d0b99
AD
970 db_query($link, "UPDATE ttrss_feeds
971 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 972
44e241cb 973// db_query($link, "COMMIT");
dd8c76a9 974
ab3d0b99
AD
975 } else {
976 $error_msg = db_escape_string(magpie_error());
977 db_query($link,
aa5f9f5f
AD
978 "UPDATE ttrss_feeds SET last_error = '$error_msg',
979 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
980 }
981
219bd8fc 982 if (defined('DAEMON_EXTENDED_DEBUG')) {
34e420fb 983 _debug("update_rss_feed: done");
219bd8fc
AD
984 }
985
40d13c28
AD
986 }
987
f175937c 988 function print_select($id, $default, $values, $attributes = "") {
79f3553b 989 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
990 foreach ($values as $v) {
991 if ($v == $default)
992 $sel = " selected";
993 else
994 $sel = "";
995
996 print "<option$sel>$v</option>";
997 }
998 print "</select>";
999 }
40d13c28 1000
79f3553b
AD
1001 function print_select_hash($id, $default, $values, $attributes = "") {
1002 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
1003 foreach (array_keys($values) as $v) {
1004 if ($v == $default)
1005 $sel = "selected";
1006 else
1007 $sel = "";
1008
1009 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1010 }
1011
1012 print "</select>";
1013 }
1014
f8382011 1015 function get_article_filters($filters, $title, $content, $link) {
240054f1 1016 $matches = array();
c2d9322b 1017
240054f1
AD
1018 if ($filters["title"]) {
1019 foreach ($filters["title"] as $filter) {
c2d9322b
AD
1020 $reg_exp = $filter["reg_exp"];
1021 $inverse = $filter["inverse"];
1022 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1023 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1024
240054f1
AD
1025 array_push($matches, array($filter["action"], $filter["action_param"]));
1026 }
1027 }
1028 }
1029
1030 if ($filters["content"]) {
1031 foreach ($filters["content"] as $filter) {
c2d9322b
AD
1032 $reg_exp = $filter["reg_exp"];
1033 $inverse = $filter["inverse"];
1034
1035 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1036 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1037
240054f1
AD
1038 array_push($matches, array($filter["action"], $filter["action_param"]));
1039 }
1040 }
1041 }
1042
1043 if ($filters["both"]) {
1044 foreach ($filters["both"] as $filter) {
1045 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1046 $inverse = $filter["inverse"];
1047
1048 if ($inverse) {
1049 if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) {
1050 array_push($matches, array($filter["action"], $filter["action_param"]));
1051 }
1052 } else {
1053 if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
1054 array_push($matches, array($filter["action"], $filter["action_param"]));
1055 }
240054f1
AD
1056 }
1057 }
1058 }
1059
1060 if ($filters["link"]) {
1061 $reg_exp = $filter["reg_exp"];
1062 foreach ($filters["link"] as $filter) {
1063 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1064 $inverse = $filter["inverse"];
1065
1066 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1067 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1068
240054f1
AD
1069 array_push($matches, array($filter["action"], $filter["action_param"]));
1070 }
1071 }
1072 }
1073
1074 return $matches;
1075 }
1076
f8382011
AD
1077 function find_article_filter($filters, $filter_name) {
1078 foreach ($filters as $f) {
1079 if ($f[0] == $filter_name) {
1080 return $f;
1081 };
1082 }
1083 return false;
1084 }
1085
9323147e 1086 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 1087 $rtl_content = false, $last_updated = false, $last_error = false) {
254e0e4b
AD
1088
1089 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 1090 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 1091 } else {
023fe037 1092 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
1093 }
1094
9323147e
AD
1095 if ($rtl_content) {
1096 $rtl_tag = "dir=\"rtl\"";
1097 } else {
1098 $rtl_tag = "dir=\"ltr\"";
1099 }
1100
78d5212c
AD
1101 $error_notify_msg = "";
1102
fb1fb4ab
AD
1103 if ($last_error) {
1104 $link_title = "Error: $last_error ($last_updated)";
78d5212c 1105 $error_notify_msg = "(Error)";
ad780e9c 1106 } else if ($last_updated) {
fb1fb4ab
AD
1107 $link_title = "Updated: $last_updated";
1108 }
1109
7210613a 1110 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
c50e2b30 1111 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
254e0e4b
AD
1112
1113 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 1114 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
1115 print "$feed_icon";
1116 }
1117
9323147e 1118 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
1119
1120 if ($unread != 0) {
1121 $fctr_class = "";
1122 } else {
1123 $fctr_class = "class=\"invisible\"";
1124 }
1125
9323147e 1126 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b 1127 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
78d5212c
AD
1128
1129 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1130 print "<div class=\"feedExtInfo\">
1131 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1132 }
1133
254e0e4b
AD
1134 print "</li>";
1135
1136 }
1137
406d9489
AD
1138 function getmicrotime() {
1139 list($usec, $sec) = explode(" ",microtime());
1140 return ((float)$usec + (float)$sec);
1141 }
1142
77e96719
AD
1143 function print_radio($id, $default, $values, $attributes = "") {
1144 foreach ($values as $v) {
1145
1146 if ($v == $default)
5da169d9 1147 $sel = "checked";
77e96719 1148 else
5da169d9
AD
1149 $sel = "";
1150
1151 if ($v == "Yes") {
1152 $sel .= " value=\"1\"";
1153 } else {
1154 $sel .= " value=\"0\"";
1155 }
77e96719 1156
69654950
AD
1157 print "<input class=\"noborder\"
1158 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
1159
1160 }
1161 }
1162
ff485f1d
AD
1163 function initialize_user_prefs($link, $uid) {
1164
1165 $uid = db_escape_string($uid);
1166
1167 db_query($link, "BEGIN");
1168
1169 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1170
1171 $u_result = db_query($link, "SELECT pref_name
1172 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1173
1174 $active_prefs = array();
1175
1176 while ($line = db_fetch_assoc($u_result)) {
1177 array_push($active_prefs, $line["pref_name"]);
1178 }
1179
1180 while ($line = db_fetch_assoc($result)) {
1181 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1182// print "adding " . $line["pref_name"] . "<br>";
1183
1184 db_query($link, "INSERT INTO ttrss_user_prefs
1185 (owner_uid,pref_name,value) VALUES
1186 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1187
1188 }
1189 }
1190
1191 db_query($link, "COMMIT");
1192
1193 }
956c7629
AD
1194
1195 function lookup_user_id($link, $user) {
1196
1197 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1198 login = '$login'");
1199
1200 if (db_num_rows($result) == 1) {
1201 return db_fetch_result($result, 0, "id");
1202 } else {
1203 return false;
1204 }
1205 }
1206
18664970
AD
1207 function http_authenticate_user($link) {
1208
4bc64807
AD
1209 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1210
18664970
AD
1211 if (!$_SERVER["PHP_AUTH_USER"]) {
1212
1213 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1214 header('HTTP/1.0 401 Unauthorized');
1215 exit;
1216
1217 } else {
1218 $auth_result = authenticate_user($link,
1219 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1220
1221 if (!$auth_result) {
1222 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1223 header('HTTP/1.0 401 Unauthorized');
1224 exit;
1225 }
1226 }
1227
1228 return true;
1229 }
1230
461766f3 1231 function authenticate_user($link, $login, $password, $force_auth = false) {
c8437f35 1232
131b01b3 1233 if (!SINGLE_USER_MODE) {
c8437f35 1234
131b01b3 1235 $pwd_hash = 'SHA1:' . sha1($password);
461766f3
AD
1236
1237 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1238 $query = "SELECT id,login,access_level
1239 FROM ttrss_users WHERE
1240 login = '$login'";
1241 } else {
1242 $query = "SELECT id,login,access_level
1243 FROM ttrss_users WHERE
1244 login = '$login' AND pwd_hash = '$pwd_hash'";
1245 }
1246
1247 $result = db_query($link, $query);
131b01b3
AD
1248
1249 if (db_num_rows($result) == 1) {
1250 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1251 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1252 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1253
1254 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1255 $_SESSION["uid"]);
1256
1257 $user_theme = get_user_theme_path($link);
1258
1259 $_SESSION["theme"] = $user_theme;
1260 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1261
1262 initialize_user_prefs($link, $_SESSION["uid"]);
1263
1264 return true;
1265 }
1266
1267 return false;
503eb349 1268
131b01b3 1269 } else {
503eb349 1270
131b01b3
AD
1271 $_SESSION["uid"] = 1;
1272 $_SESSION["name"] = "admin";
f557cd78 1273
0bbba72d
AD
1274 $user_theme = get_user_theme_path($link);
1275
1276 $_SESSION["theme"] = $user_theme;
1277 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1278
1279 initialize_user_prefs($link, $_SESSION["uid"]);
1280
c8437f35
AD
1281 return true;
1282 }
c8437f35
AD
1283 }
1284
e6cb77a0
AD
1285 function make_password($length = 8) {
1286
1287 $password = "";
798f722b
AD
1288 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1289
1290 $i = 0;
e6cb77a0
AD
1291
1292 while ($i < $length) {
1293 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1294
1295 if (!strstr($password, $char)) {
1296 $password .= $char;
1297 $i++;
1298 }
1299 }
1300 return $password;
1301 }
1302
1303 // this is called after user is created to initialize default feeds, labels
1304 // or whatever else
1305
1306 // user preferences are checked on every login, not here
1307
1308 function initialize_user($link, $uid) {
1309
1310 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1311 values ('$uid','unread = true', 'Unread articles')");
1312
1313 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1314 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1315
1316 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1317 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 1318 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b 1319
cd2cd415
AD
1320 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1321 values ('$uid', 'Tiny Tiny RSS: Forum',
1322 'http://tt-rss.spb.ru/forum/rss.php')");
3b0feb9b 1323 }
e6cb77a0 1324
b8aa49bc 1325 function logout_user() {
5ccc1cf5
AD
1326 session_destroy();
1327 if (isset($_COOKIE[session_name()])) {
1328 setcookie(session_name(), '', time()-42000, '/');
1329 }
b8aa49bc
AD
1330 }
1331
75836f33 1332 function get_script_urlpath() {
87a79fa4 1333 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1334 }
1335
916f788a 1336 function validate_session($link) {
a2e9b457 1337 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
1338 if ($_SESSION["ip_address"]) {
1339 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
7f0acba7 1340 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
916f788a
AD
1341 return false;
1342 }
1343 }
1344 }
d620cfe7 1345
a885f0ec 1346/* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
d620cfe7 1347
8e849206 1348 //print_r($_SESSION);
d620cfe7
AD
1349
1350 if (time() > $_SESSION["cookie_lifetime"]) {
1351 return false;
1352 }
a885f0ec
AD
1353 } */
1354
916f788a
AD
1355 return true;
1356 }
1357
793185a9 1358 function login_sequence($link, $mobile = false) {
b8aa49bc 1359 if (!SINGLE_USER_MODE) {
75836f33 1360
461766f3
AD
1361 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1362 $swu = db_escape_string($_REQUEST["swu"]);
1363 if ($swu) {
1364 $_SESSION["prefs_cache"] = false;
1365 return authenticate_user($link, $swu, null, true);
1366 }
1367 }
1368
7f0acba7 1369 $login_action = $_POST["login_action"];
a885f0ec 1370
01a87dff 1371 # try to authenticate user if called from login form
7f0acba7 1372 if ($login_action == "do_login") {
01a87dff
AD
1373 $login = $_POST["login"];
1374 $password = $_POST["password"];
d620cfe7 1375 $remember_me = $_POST["remember_me"];
f557cd78 1376
01a87dff
AD
1377 if (authenticate_user($link, $login, $password)) {
1378 $_POST["password"] = "";
d620cfe7 1379
d620cfe7
AD
1380 header("Location: " . $_SERVER["REQUEST_URI"]);
1381 exit;
1382
01a87dff 1383 return;
7f0acba7
AD
1384 } else {
1385 $_SESSION["login_error_msg"] = "Incorrect username or password";
01a87dff
AD
1386 }
1387 }
1388
1df0f48b
AD
1389// print session_id();
1390// print_r($_SESSION);
7f0acba7
AD
1391
1392 if (!$_SESSION["uid"] || !validate_session($link)) {
793185a9 1393 render_login_form($link, $mobile);
01a87dff 1394 exit;
d3687e7a
AD
1395 } else {
1396 /* bump login timestamp */
1397 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1398 $_SESSION["uid"]);
b8aa49bc 1399 }
d620cfe7 1400
b8aa49bc 1401 } else {
0bbba72d 1402 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1403 }
1404 }
3547842a
AD
1405
1406 function truncate_string($str, $max_len) {
12db369c
AD
1407 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1408 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
1409 } else {
1410 return $str;
1411 }
1412 }
54a60e1a
AD
1413
1414 function get_user_theme_path($link) {
798f722b
AD
1415 $result = db_query($link, "SELECT theme_path
1416 FROM
1417 ttrss_themes,ttrss_users
1418 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
1419 if (db_num_rows($result) != 0) {
1420 return db_fetch_result($result, 0, "theme_path");
1421 } else {
1422 return null;
1423 }
1424 }
be773442
AD
1425
1426 function smart_date_time($timestamp) {
1427 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1428 return date("G:i", $timestamp);
f26450f1 1429 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1430 return date("M d, G:i", $timestamp);
1431 } else {
7d7e0509 1432 return date("Y/m/d, G:i", $timestamp);
be773442
AD
1433 }
1434 }
1435
1436 function smart_date($timestamp) {
1437 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1438 return "Today";
f26450f1 1439 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1440 return date("D m", $timestamp);
1441 } else {
b02111c2 1442 return date("Y/m/d", $timestamp);
be773442
AD
1443 }
1444 }
a654a595
AD
1445
1446 function sql_bool_to_string($s) {
1447 if ($s == "t" || $s == "1") {
1448 return "true";
1449 } else {
1450 return "false";
1451 }
1452 }
e3c99f3b
AD
1453
1454 function sql_bool_to_bool($s) {
1455 if ($s == "t" || $s == "1") {
1456 return true;
1457 } else {
1458 return false;
1459 }
1460 }
0ea4fb50 1461
e3c99f3b 1462
0ea4fb50
AD
1463 function toggleEvenOdd($a) {
1464 if ($a == "even")
1465 return "odd";
1466 else
1467 return "even";
1468 }
6043fb7e
AD
1469
1470 function sanity_check($link) {
9cbca41f 1471
aec3ce39
AD
1472 error_reporting(0);
1473
6043fb7e
AD
1474 $error_code = 0;
1475 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1476 $schema_version = db_fetch_result($result, 0, "schema_version");
1477
1478 if ($schema_version != SCHEMA_VERSION) {
1479 $error_code = 5;
1480 }
1481
aec3ce39
AD
1482 if (DB_TYPE == "mysql") {
1483 $result = db_query($link, "SELECT true", false);
1484 if (db_num_rows($result) != 1) {
1485 $error_code = 10;
1486 }
1487 }
1488
1489 error_reporting (DEFAULT_ERROR_LEVEL);
1490
6043fb7e 1491 if ($error_code != 0) {
aec3ce39 1492 print_error_xml($error_code);
6043fb7e
AD
1493 return false;
1494 } else {
1495 return true;
4220d6b0 1496 }
6043fb7e
AD
1497 }
1498
27981ca3
AD
1499 function file_is_locked($filename) {
1500 error_reporting(0);
1501 $fp = fopen($filename, "r");
1502 error_reporting(DEFAULT_ERROR_LEVEL);
1503 if ($fp) {
1504 if (flock($fp, LOCK_EX | LOCK_NB)) {
1505 flock($fp, LOCK_UN);
1506 fclose($fp);
1507 return false;
1508 }
1509 fclose($fp);
1510 return true;
1511 }
1512 return false;
1513 }
1514
fcb4c0c9
AD
1515 function make_lockfile($filename) {
1516 $fp = fopen($filename, "w");
1517
1518 if (flock($fp, LOCK_EX | LOCK_NB)) {
1519 return $fp;
1520 } else {
1521 return false;
1522 }
1523 }
1524
bf7fcde8
AD
1525 function make_stampfile($filename) {
1526 $fp = fopen($filename, "w");
1527
8e00ae9b 1528 if (flock($fp, LOCK_EX | LOCK_NB)) {
bf7fcde8 1529 fwrite($fp, time() . "\n");
8e00ae9b 1530 flock($fp, LOCK_UN);
bf7fcde8
AD
1531 fclose($fp);
1532 return true;
1533 } else {
1534 return false;
1535 }
1536 }
1537
8e00ae9b
AD
1538 function read_stampfile($filename) {
1539
1540 error_reporting(0);
1541 $fp = fopen($filename, "r");
1542 error_reporting (DEFAULT_ERROR_LEVEL);
1543
1544 if (flock($fp, LOCK_EX)) {
1545 $stamp = fgets($fp);
1546 flock($fp, LOCK_UN);
1547 fclose($fp);
1548 return $stamp;
1549 } else {
1550 return false;
1551 }
1552 }
bf7fcde8 1553
894ebcf5
AD
1554 function sql_random_function() {
1555 if (DB_TYPE == "mysql") {
1556 return "RAND()";
1557 } else {
1558 return "RANDOM()";
1559 }
1560 }
1561
23aa0d16 1562 function catchup_feed($link, $feed, $cat_view) {
88040f57
AD
1563
1564 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
23aa0d16
AD
1565
1566 if ($cat_view) {
1567
1568 if ($feed > 0) {
1569 $cat_qpart = "cat_id = '$feed'";
1570 } else {
1571 $cat_qpart = "cat_id IS NULL";
1572 }
1573
1574 $tmp_result = db_query($link, "SELECT id
1575 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1576 $_SESSION["uid"]);
1577
1578 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1579
1580 $tmp_feed = $tmp_line["id"];
1581
1582 db_query($link, "UPDATE ttrss_user_entries
1583 SET unread = false,last_read = NOW()
1584 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1585 }
1586
1587 } else if ($feed > 0) {
1588
1589 $tmp_result = db_query($link, "SELECT id
1590 FROM ttrss_feeds WHERE parent_feed = '$feed'
1591 ORDER BY cat_id,title");
1592
1593 $parent_ids = array();
1594
1595 if (db_num_rows($tmp_result) > 0) {
1596 while ($p = db_fetch_assoc($tmp_result)) {
1597 array_push($parent_ids, "feed_id = " . $p["id"]);
1598 }
1599
1600 $children_qpart = implode(" OR ", $parent_ids);
1601
1602 db_query($link, "UPDATE ttrss_user_entries
1603 SET unread = false,last_read = NOW()
1604 WHERE (feed_id = '$feed' OR $children_qpart)
1605 AND owner_uid = " . $_SESSION["uid"]);
1606
1607 } else {
1608 db_query($link, "UPDATE ttrss_user_entries
1609 SET unread = false,last_read = NOW()
1610 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1611 }
1612
1613 } else if ($feed < 0 && $feed > -10) { // special, like starred
1614
1615 if ($feed == -1) {
1616 db_query($link, "UPDATE ttrss_user_entries
1617 SET unread = false,last_read = NOW()
1618 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1619 }
e4f4b46f
AD
1620
1621 if ($feed == -2) {
1622 db_query($link, "UPDATE ttrss_user_entries
1623 SET unread = false,last_read = NOW()
1624 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
1625 }
1626
23aa0d16
AD
1627 } else if ($feed < -10) { // label
1628
1629 // TODO make this more efficient
1630
1631 $label_id = -$feed - 11;
1632
1633 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1634 WHERE id = '$label_id'");
1635
1636 if ($tmp_result) {
1637 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1638
1639 db_query($link, "BEGIN");
1640
1641 $tmp2_result = db_query($link,
1642 "SELECT
1643 int_id
1644 FROM
88040f57 1645 ttrss_user_entries,ttrss_entries,ttrss_feeds
23aa0d16 1646 WHERE
88040f57
AD
1647 ref_id = ttrss_entries.id AND
1648 ttrss_user_entries.feed_id = ttrss_feeds.id AND
23aa0d16 1649 $sql_exp AND
88040f57 1650 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
23aa0d16
AD
1651
1652 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1653 db_query($link, "UPDATE
1654 ttrss_user_entries
1655 SET
1656 unread = false, last_read = NOW()
1657 WHERE
1658 int_id = " . $tmp_line["int_id"]);
1659 }
1660
1661 db_query($link, "COMMIT");
1662
1663/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1664 SET unread = false,last_read = NOW()
1665 WHERE $sql_exp
1666 AND ref_id = id
1667 AND owner_uid = ".$_SESSION["uid"]); */
1668 }
1669 }
1670 } else { // tag
1671 db_query($link, "BEGIN");
1672
1673 $tag_name = db_escape_string($feed);
1674
1675 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1676 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1677
1678 while ($line = db_fetch_assoc($result)) {
1679 db_query($link, "UPDATE ttrss_user_entries SET
1680 unread = false, last_read = NOW()
1681 WHERE int_id = " . $line["post_int_id"]);
1682 }
1683 db_query($link, "COMMIT");
1684 }
1685 }
1686
1687 function update_generic_feed($link, $feed, $cat_view) {
1688 if ($cat_view) {
1689
1690 if ($feed > 0) {
1691 $cat_qpart = "cat_id = '$feed'";
1692 } else {
1693 $cat_qpart = "cat_id IS NULL";
1694 }
1695
1696 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1697 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1698
1699 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1700 $feed_url = $tmp_line["feed_url"];
1701 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1702 }
1703
1704 } else {
1705 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1706 WHERE id = '$feed'");
1707 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1708 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1709 }
1710 }
a9cb1f83 1711
2bc2147f 1712 function getAllCounters($link, $omode = "flc") {
cf4d339c 1713/* getLabelCounters($link);
a9cb1f83
AD
1714 getFeedCounters($link);
1715 getTagCounters($link);
1716 getGlobalCounters($link);
1717 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1718 getCategoryCounters($link);
cf4d339c
AD
1719 } */
1720
2bc2147f 1721 if (!$omode) $omode = "flc";
cf4d339c
AD
1722
1723 getGlobalCounters($link);
1724
1725 if (strchr($omode, "l")) getLabelCounters($link);
1726 if (strchr($omode, "f")) getFeedCounters($link);
1727 if (strchr($omode, "t")) getTagCounters($link);
1728 if (strchr($omode, "c")) {
1729 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1730 getCategoryCounters($link);
1731 }
a9cb1f83
AD
1732 }
1733 }
1734
1735 function getCategoryCounters($link) {
1736 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1737 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1738 AND unread = true)) AS unread FROM ttrss_feeds
1739 WHERE
cfb02131 1740 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
a9cb1f83
AD
1741
1742 while ($line = db_fetch_assoc($result)) {
1743 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1744 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1745 $line["unread"]."\"/>";
1746 }
1747 }
1748
f295c368
AD
1749 function getCategoryUnread($link, $cat) {
1750
18664970
AD
1751 if ($cat != 0) {
1752 $cat_query = "cat_id = '$cat'";
1753 } else {
1754 $cat_query = "cat_id IS NULL";
1755 }
1756
1757 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
cfb02131 1758 AND hidden = false
f295c368
AD
1759 AND owner_uid = " . $_SESSION["uid"]);
1760
1761 $cat_feeds = array();
1762 while ($line = db_fetch_assoc($result)) {
1763 array_push($cat_feeds, "feed_id = " . $line["id"]);
1764 }
1765
18664970
AD
1766 if (count($cat_feeds) == 0) return 0;
1767
f295c368
AD
1768 $match_part = implode(" OR ", $cat_feeds);
1769
1770 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1771 FROM ttrss_user_entries
4919fb42 1772 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
f295c368
AD
1773
1774 $unread = 0;
1775
1776 # this needs to be rewritten
1777 while ($line = db_fetch_assoc($result)) {
1778 $unread += $line["unread"];
1779 }
1780
1781 return $unread;
1782
1783 }
1784
1785 function getFeedUnread($link, $feed, $is_cat = false) {
a9cb1f83 1786 $n_feed = sprintf("%d", $feed);
f295c368
AD
1787
1788 if ($is_cat) {
831ff047 1789 return getCategoryUnread($link, $n_feed);
f295c368 1790 } else if ($n_feed == -1) {
a9cb1f83 1791 $match_part = "marked = true";
e4f4b46f
AD
1792 } else if ($n_feed == -2) {
1793 $match_part = "published = true";
4919fb42 1794 } else if ($n_feed > 0) {
831ff047 1795
e8b8485f
AD
1796 $result = db_query($link, "SELECT id FROM ttrss_feeds
1797 WHERE parent_feed = '$n_feed'
318260cc 1798 AND hidden = false
4919fb42 1799 AND owner_uid = " . $_SESSION["uid"]);
831ff047
AD
1800
1801 if (db_num_rows($result) > 0) {
4919fb42 1802
831ff047
AD
1803 $linked_feeds = array();
1804 while ($line = db_fetch_assoc($result)) {
1805 array_push($linked_feeds, "feed_id = " . $line["id"]);
1806 }
e8b8485f
AD
1807
1808 array_push($linked_feeds, "feed_id = $n_feed");
831ff047
AD
1809
1810 $match_part = implode(" OR ", $linked_feeds);
1811
4919fb42 1812 $result = db_query($link, "SELECT COUNT(int_id) AS unread
318260cc 1813 FROM ttrss_user_entries
e8b8485f
AD
1814 WHERE unread = true AND ($match_part)
1815 AND owner_uid = " . $_SESSION["uid"]);
4919fb42
AD
1816
1817 $unread = 0;
1818
1819 # this needs to be rewritten
1820 while ($line = db_fetch_assoc($result)) {
1821 $unread += $line["unread"];
1822 }
1823
1824 return $unread;
1825
831ff047
AD
1826 } else {
1827 $match_part = "feed_id = '$n_feed'";
1828 }
a9cb1f83 1829 } else if ($feed < -10) {
318260cc 1830
a9cb1f83
AD
1831 $label_id = -$feed - 11;
1832
1833 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1834 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1835
1836 $match_part = db_fetch_result($result, 0, "sql_exp");
1837 }
1838
1839 if ($match_part) {
1840
1841 $result = db_query($link, "SELECT count(int_id) AS unread
88040f57
AD
1842 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1843 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1844 ttrss_user_entries.ref_id = ttrss_entries.id AND
cfb02131 1845 ttrss_feeds.hidden = false AND
88040f57 1846 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
1847
1848 } else {
1849
1850 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1851 FROM ttrss_tags,ttrss_user_entries
1852 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1853 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1854 }
1855
1856 $unread = db_fetch_result($result, 0, "unread");
cfb02131 1857
a9cb1f83
AD
1858 return $unread;
1859 }
1860
1861 /* FIXME this needs reworking */
1862
f3acc32e
AD
1863 function getGlobalUnread($link, $user_id = false) {
1864
1865 if (!$user_id) {
1866 $user_id = $_SESSION["uid"];
1867 }
1868
3831db41 1869 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 1870 WHERE unread = true AND
3831db41 1871 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 1872 ttrss_user_entries.ref_id = ttrss_entries.id AND
3831db41 1873 hidden = false AND
f3acc32e 1874 ttrss_user_entries.owner_uid = '$user_id'");
a9cb1f83
AD
1875 $c_id = db_fetch_result($result, 0, "c_id");
1876 return $c_id;
1877 }
1878
1879 function getGlobalCounters($link, $global_unread = -1) {
1880 if ($global_unread == -1) {
1881 $global_unread = getGlobalUnread($link);
1882 }
7bf7e4d3
AD
1883 print "<counter type=\"global\" id='global-unread'
1884 counter='$global_unread'/>";
1885
1886 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1887 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1888
1889 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1890
1891 print "<counter type=\"global\" id='subscribed-feeds'
1892 counter='$subscribed_feeds'/>";
1893
a9cb1f83
AD
1894 }
1895
1896 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1897
1898 if ($smart_mode) {
1899 if (!$_SESSION["tctr_last_value"]) {
1900 $_SESSION["tctr_last_value"] = array();
1901 }
1902 }
1903
1904 $old_counters = $_SESSION["tctr_last_value"];
1905
1906 $tctrs_modified = false;
1907
1908/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1909 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1910 ttrss_user_entries.ref_id = ttrss_entries.id AND
1911 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1912 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1913 UNION
1914 select tag_name,0 as count FROM ttrss_tags
1915 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1916
1917 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1918 FROM ttrss_user_entries WHERE int_id = post_int_id
1919 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
1920 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
1921 ORDER BY count DESC LIMIT 55");
a9cb1f83
AD
1922
1923 $tags = array();
1924
1925 while ($line = db_fetch_assoc($result)) {
1926 $tags[$line["tag_name"]] += $line["count"];
1927 }
1928
1929 foreach (array_keys($tags) as $tag) {
1930 $unread = $tags[$tag];
1931
1932 $tag = htmlspecialchars($tag);
1933
1934 if (!$smart_mode || $old_counters[$tag] != $unread) {
1935 $old_counters[$tag] = $unread;
1936 $tctrs_modified = true;
1937 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1938 }
1939
1940 }
1941
1942 if ($smart_mode && $tctrs_modified) {
1943 $_SESSION["tctr_last_value"] = $old_counters;
1944 }
1945
1946 }
1947
ef393de7 1948 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
a9cb1f83
AD
1949
1950 if ($smart_mode) {
1951 if (!$_SESSION["lctr_last_value"]) {
1952 $_SESSION["lctr_last_value"] = array();
1953 }
1954 }
1955
ef393de7
AD
1956 $ret_arr = array();
1957
a9cb1f83
AD
1958 $old_counters = $_SESSION["lctr_last_value"];
1959 $lctrs_modified = false;
1960
88040f57 1961 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 1962 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 1963 ttrss_user_entries.feed_id = ttrss_feeds.id AND
e4f4b46f 1964 hidden = false AND unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1965
1966 $count = db_fetch_result($result, 0, "count");
1967
ef393de7
AD
1968 if (!$ret_mode) {
1969 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1970 } else {
1971 $ret_arr["-1"]["counter"] = $count;
1972 $ret_arr["-1"]["description"] = "Starred";
1973 }
a9cb1f83 1974
e4f4b46f
AD
1975 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1976 WHERE published = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1977 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1978 hidden = false AND unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1979
1980 $count = db_fetch_result($result, 0, "count");
1981
1982 if (!$ret_mode) {
1983 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
1984 } else {
1985 $ret_arr["-2"]["counter"] = $count;
1986 $ret_arr["-2"]["description"] = "Published";
1987 }
1988
1989
a9cb1f83
AD
1990 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1991 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1992
1993 while ($line = db_fetch_assoc($result)) {
1994
1995 $id = -$line["id"] - 11;
1996
ef393de7
AD
1997 $label_name = $line["description"];
1998
a9cb1f83
AD
1999 error_reporting (0);
2000
88040f57 2001 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
a9cb1f83 2002 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
cfb02131 2003 ttrss_feeds.hidden = false AND
88040f57 2004 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 2005 ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 2006 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
2007
2008 $count = db_fetch_result($tmp_result, 0, "count");
2009
2010 if (!$smart_mode || $old_counters[$id] != $count) {
2011 $old_counters[$id] = $count;
2012 $lctrs_modified = true;
ef393de7
AD
2013 if (!$ret_mode) {
2014 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2015 } else {
2016 $ret_arr[$id]["counter"] = $count;
2017 $ret_arr[$id]["description"] = $label_name;
2018 }
a9cb1f83
AD
2019 }
2020
2021 error_reporting (DEFAULT_ERROR_LEVEL);
2022 }
2023
2024 if ($smart_mode && $lctrs_modified) {
2025 $_SESSION["lctr_last_value"] = $old_counters;
2026 }
ef393de7
AD
2027
2028 return $ret_arr;
a9cb1f83
AD
2029 }
2030
2031/* function getFeedCounter($link, $id) {
2032
2033 $result = db_query($link, "SELECT
2034 count(id) as count,last_error
2035 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2036 WHERE feed_id = '$id' AND unread = true
2037 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2038 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2039
2040 $count = db_fetch_result($result, 0, "count");
2041 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2042
2043 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2044 } */
2045
2046 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
2047
2048 if ($smart_mode) {
2049 if (!$_SESSION["fctr_last_value"]) {
2050 $_SESSION["fctr_last_value"] = array();
2051 }
2052 }
2053
2054 $old_counters = $_SESSION["fctr_last_value"];
2055
1b1b8a7b 2056/* $result = db_query($link, "SELECT id,last_error,parent_feed,
fb1fb4ab 2057 SUBSTRING(last_updated,1,19) AS last_updated,
a9cb1f83
AD
2058 (SELECT count(id)
2059 FROM ttrss_entries,ttrss_user_entries
2060 WHERE feed_id = ttrss_feeds.id AND
2061 ttrss_user_entries.ref_id = ttrss_entries.id
2062 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2063 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1b1b8a7b
AD
2064 AND parent_feed IS NULL"); */
2065
2066 $result = db_query($link, "SELECT ttrss_feeds.id,
2067 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2068 last_error,
bc60fda3 2069 COUNT(ttrss_entries.id) AS count
1b1b8a7b
AD
2070 FROM ttrss_feeds
2071 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2072 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2073 AND ttrss_user_entries.unread = true)
2074 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2075 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2076 AND parent_feed IS NULL
2077 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error");
a9cb1f83
AD
2078
2079 $fctrs_modified = false;
2080
fb1fb4ab
AD
2081 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2082
a9cb1f83
AD
2083 while ($line = db_fetch_assoc($result)) {
2084
2085 $id = $line["id"];
2086 $count = $line["count"];
2087 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
2088
2089 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2090 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2091 } else {
2092 $last_updated = date($short_date, strtotime($line["last_updated"]));
2093 }
2094
a9cb1f83
AD
2095 $has_img = is_file(ICONS_DIR . "/$id.ico");
2096
2097 $tmp_result = db_query($link,
2098 "SELECT id,COUNT(unread) AS unread
2099 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2100 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2101 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
2102
2103 if (db_num_rows($tmp_result) > 0) {
2104 while ($l = db_fetch_assoc($tmp_result)) {
2105 $count += $l["unread"];
2106 }
2107 }
2108
2109 if (!$smart_mode || $old_counters[$id] != $count) {
2110 $old_counters[$id] = $count;
2111 $fctrs_modified = true;
2112
2113 if ($last_error) {
2114 $error_part = "error=\"$last_error\"";
2115 } else {
2116 $error_part = "";
2117 }
2118
2119 if ($has_img) {
2120 $has_img_part = "hi=\"$has_img\"";
2121 } else {
2122 $has_img_part = "";
2123 }
2124
fb1fb4ab 2125 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
a9cb1f83
AD
2126 }
2127 }
2128
2129 if ($smart_mode && $fctrs_modified) {
2130 $_SESSION["fctr_last_value"] = $old_counters;
2131 }
2132 }
2133
1b758780 2134 function get_script_dt_add() {
34e420fb 2135 if (strpos(VERSION, ".99") === false) {
1b758780
AD
2136 return VERSION;
2137 } else {
2138 return time();
2139 }
2140 }
2141
6e7f8d26
AD
2142 function get_pgsql_version($link) {
2143 $result = db_query($link, "SELECT version() AS version");
2144 $version = split(" ", db_fetch_result($result, 0, "version"));
2145 return $version[1];
2146 }
2147
af106b0e
AD
2148 function print_error_xml($code, $add_msg = "") {
2149 global $ERRORS;
2150
2151 $error_msg = $ERRORS[$code];
2152
2153 if ($add_msg) {
2154 $error_msg = "$error_msg; $add_msg";
2155 }
2156
4c2abbc1 2157 print "<rpc-reply>";
af106b0e 2158 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 2159 print "</rpc-reply>";
af106b0e 2160 }
956c7629 2161
f27de515
AD
2162 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2163 $auth_login = '', $auth_pass = '') {
bb0f29a4 2164
b3dfe8ba 2165 # check for feed:http://url
c91c2249
AD
2166 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2167
b3dfe8ba 2168 # check for feed://URL
e2d84cdb 2169 if (strpos($feed_link, "//") === 0) {
b3dfe8ba
AD
2170 $feed_link = "http:$feed_link";
2171 }
2172
c91c2249 2173 if ($feed_link == "") return;
bb0f29a4 2174
956c7629
AD
2175 if ($cat_id == "0" || !$cat_id) {
2176 $cat_qpart = "NULL";
2177 } else {
2178 $cat_qpart = "'$cat_id'";
2179 }
2180
2181 $result = db_query($link,
2182 "SELECT id FROM ttrss_feeds
2183 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2184
2185 if (db_num_rows($result) == 0) {
2186
2187 $result = db_query($link,
f27de515
AD
2188 "INSERT INTO ttrss_feeds
2189 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
956c7629 2190 VALUES ('".$_SESSION["uid"]."', '$feed_link',
f27de515 2191 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
956c7629
AD
2192
2193 $result = db_query($link,
2194 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
f27de515 2195 AND owner_uid = " . $_SESSION["uid"]);
956c7629
AD
2196
2197 $feed_id = db_fetch_result($result, 0, "id");
2198
2199 if ($feed_id) {
2200 update_rss_feed($link, $feed_link, $feed_id, true);
2201 }
2202
2203 return true;
2204 } else {
2205 return false;
2206 }
2207 }
2208
673d54ca
AD
2209 function print_feed_select($link, $id, $default_id = "",
2210 $attributes = "", $include_all_feeds = true) {
2211
79f3553b 2212 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 2213 if ($include_all_feeds) {
89cb787e 2214 print "<option value=\"0\">".__('All feeds')."</option>";
673d54ca
AD
2215 }
2216
2217 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2218 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2219
2220 if (db_num_rows($result) > 0 && $include_all_feeds) {
2221 print "<option disabled>--------</option>";
2222 }
2223
2224 while ($line = db_fetch_assoc($result)) {
2225 if ($line["id"] == $default_id) {
2226 $is_selected = "selected";
2227 } else {
2228 $is_selected = "";
2229 }
79f3553b 2230 printf("<option $is_selected value='%d'>%s</option>",
47439031 2231 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
2232 }
2233
2234 print "</select>";
2235 }
2236
2237 function print_feed_cat_select($link, $id, $default_id = "",
2238 $attributes = "", $include_all_cats = true) {
2239
79f3553b 2240 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
2241
2242 if ($include_all_cats) {
d1db26aa 2243 print "<option value=\"0\">".__('Uncategorized')."</option>";
673d54ca
AD
2244 }
2245
2246 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2247 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2248
2249 if (db_num_rows($result) > 0 && $include_all_cats) {
2250 print "<option disabled>--------</option>";
2251 }
2252
2253 while ($line = db_fetch_assoc($result)) {
2254 if ($line["id"] == $default_id) {
2255 $is_selected = "selected";
2256 } else {
2257 $is_selected = "";
2258 }
14f69488 2259 printf("<option $is_selected value='%d'>%s</option>",
47439031 2260 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
2261 }
2262
2263 print "</select>";
2264 }
2265
14f69488
AD
2266 function checkbox_to_sql_bool($val) {
2267 return ($val == "on") ? "true" : "false";
2268 }
86b682ce
AD
2269
2270 function getFeedCatTitle($link, $id) {
2271 if ($id == -1) {
d1db26aa 2272 return __("Special");
86b682ce 2273 } else if ($id < -10) {
d1db26aa 2274 return __("Labels");
86b682ce
AD
2275 } else if ($id > 0) {
2276 $result = db_query($link, "SELECT ttrss_feed_categories.title
2277 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2278 cat_id = ttrss_feed_categories.id");
2279 if (db_num_rows($result) == 1) {
2280 return db_fetch_result($result, 0, "title");
2281 } else {
d1db26aa 2282 return __("Uncategorized");
86b682ce
AD
2283 }
2284 } else {
2285 return "getFeedCatTitle($id) failed";
2286 }
2287
2288 }
2289
2290 function getFeedTitle($link, $id) {
2291 if ($id == -1) {
d1db26aa 2292 return __("Starred articles");
945c243e
AD
2293 } else if ($id == -2) {
2294 return __("Published articles");
86b682ce
AD
2295 } else if ($id < -10) {
2296 $label_id = -10 - $id;
2297 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2298 if (db_num_rows($result) == 1) {
2299 return db_fetch_result($result, 0, "description");
2300 } else {
2301 return "Unknown label ($label_id)";
2302 }
2303
2304 } else if ($id > 0) {
2305 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2306 if (db_num_rows($result) == 1) {
2307 return db_fetch_result($result, 0, "title");
2308 } else {
2309 return "Unknown feed ($id)";
2310 }
2311 } else {
2312 return "getFeedTitle($id) failed";
2313 }
2314
2315 }
3dd46f19
AD
2316
2317 function get_session_cookie_name() {
2318 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
2319 }
3ac2b520
AD
2320
2321 function print_init_params($link) {
2322 print "<init-params>";
2323 if ($_SESSION["stored-params"]) {
2324 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
2325 if ($key) {
2326 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2327 print "<param key=\"$key\" value=\"$value\"/>";
2328 }
3ac2b520
AD
2329 }
2330 }
2331
2332 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2333 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
0d51e25d 2334 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
3ac2b520
AD
2335
2336 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2337 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2338
e8bd0da9 2339 print "<param key=\"hide_read_feeds\" value=\"" .
465ff90b 2340 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
e8bd0da9 2341
c9268ed5 2342 print "<param key=\"feeds_sort_by_unread\" value=\"" .
465ff90b 2343 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
c9268ed5 2344
f6d6e22f 2345 print "<param key=\"confirm_feed_catchup\" value=\"" .
465ff90b 2346 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
f6d6e22f 2347
ac7bcd71 2348 print "<param key=\"cdm_auto_catchup\" value=\"" .
465ff90b 2349 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
ac7bcd71 2350
8e9c121b
AD
2351 print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>";
2352
be0801a1
AD
2353 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>";
2354
40496720
AD
2355 print "<param key=\"default_view_mode\" value=\"" .
2356 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2357
2358 print "<param key=\"default_view_limit\" value=\"" .
465ff90b 2359 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
40496720 2360
fe8d2059
AD
2361 print "<param key=\"prefs_active_tab\" value=\"" .
2362 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2363
465ff90b
AD
2364 print "<param key=\"infobox_disable_overlay\" value=\"" .
2365 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2366
3ac2b520
AD
2367 print "</init-params>";
2368 }
f54f515f
AD
2369
2370 function print_runtime_info($link) {
2371 print "<runtime-info>";
71ad883b
AD
2372 if (ENABLE_UPDATE_DAEMON) {
2373 print "<param key=\"daemon_is_running\" value=\"".
2374 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
8e00ae9b 2375
784b47a3 2376 if ($_SESSION["daemon_stamp_check"] + 600 < time()) {
8e00ae9b
AD
2377
2378 $stamp = (int)read_stampfile("update_daemon.stamp");
2379
2380 if ($stamp) {
2381 if ($stamp + 86400*3 < time()) {
2382 print "<param key=\"daemon_stamp_ok\" value=\"0\"/>";
2383 } else {
2384 print "<param key=\"daemon_stamp_ok\" value=\"1\"/>";
2385 }
2386
2387 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2388
2389 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2390 }
2391
2392 $_SESSION["daemon_stamp_check"] = time();
2393 }
71ad883b 2394 }
8e00ae9b 2395
d9fa39f1
AD
2396 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
2397
2398 if ($_SESSION["last_version_check"] + 600 < time()) {
2399 $new_version_details = check_for_update($link);
2400
2401 print "<param key=\"new_version_available\" value=\"".
2402 sprintf("%d", $new_version_details != ""). "\"/>";
2403
2404 $_SESSION["last_version_check"] = time();
2405 }
2406 }
2407
f54f515f
AD
2408 print "</runtime-info>";
2409 }
ef393de7 2410
88040f57 2411 function getSearchSql($search, $match_on) {
ef393de7 2412
88040f57 2413 $search_query_part = "";
e20c9d88 2414
88040f57
AD
2415 $keywords = split(" ", $search);
2416 $query_keywords = array();
e20c9d88 2417
88040f57 2418 if ($match_on == "both") {
e20c9d88 2419
88040f57
AD
2420 foreach ($keywords as $k) {
2421 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2422 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2423 }
e20c9d88 2424
88040f57 2425 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2426
88040f57 2427 } else if ($match_on == "title") {
e20c9d88 2428
88040f57
AD
2429 foreach ($keywords as $k) {
2430 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2431 }
e20c9d88 2432
88040f57 2433 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2434
88040f57
AD
2435 } else if ($match_on == "content") {
2436
2437 foreach ($keywords as $k) {
2438 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2439 }
2440 }
2441
2442 $search_query_part = implode("AND", $query_keywords);
2443
2444 return $search_query_part;
2445 }
2446
c36bf4d5
AD
2447 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
2448
2449 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
c1a0b534 2450
88040f57
AD
2451 if ($search) {
2452
2453 $search_query_part = getSearchSql($search, $match_on);
2454 $search_query_part .= " AND ";
e20c9d88 2455
ef393de7
AD
2456 } else {
2457 $search_query_part = "";
2458 }
2459
2460 $view_query_part = "";
2461
2462 if ($view_mode == "adaptive") {
2463 if ($search) {
2464 $view_query_part = " ";
2465 } else if ($feed != -1) {
f295c368 2466 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
2467 if ($unread > 0) {
2468 $view_query_part = " unread = true AND ";
2469 }
2470 }
2471 }
2472
2473 if ($view_mode == "marked") {
2474 $view_query_part = " marked = true AND ";
2475 }
2476
2477 if ($view_mode == "unread") {
2478 $view_query_part = " unread = true AND ";
2479 }
2480
2481 if ($limit > 0) {
2482 $limit_query_part = "LIMIT " . $limit;
2483 }
2484
2485 $vfeed_query_part = "";
2486
2487 // override query strategy and enable feed display when searching globally
2488 if ($search && $search_mode == "all_feeds") {
2489 $query_strategy_part = "ttrss_entries.id > 0";
2490 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2491 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2492 $query_strategy_part = "ttrss_entries.id > 0";
2493 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2494 id = feed_id) as feed_title,";
2495 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2496
2497 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2498
2499 $tmp_result = false;
2500
2501 if ($cat_view) {
2502 $tmp_result = db_query($link, "SELECT id
2503 FROM ttrss_feeds WHERE cat_id = '$feed'");
2504 } else {
2505 $tmp_result = db_query($link, "SELECT id
2506 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2507 WHERE id = '$feed') AND id != '$feed'");
2508 }
ef393de7
AD
2509
2510 $cat_siblings = array();
2511
2512 if (db_num_rows($tmp_result) > 0) {
2513 while ($p = db_fetch_assoc($tmp_result)) {
2514 array_push($cat_siblings, "feed_id = " . $p["id"]);
2515 }
2516
2517 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2518 $feed, implode(" OR ", $cat_siblings));
2519
2520 } else {
2521 $query_strategy_part = "ttrss_entries.id > 0";
2522 }
2523
2524 } else if ($feed >= 0) {
2525
2526 if ($cat_view) {
5c365f60 2527
ef393de7
AD
2528 if ($feed > 0) {
2529 $query_strategy_part = "cat_id = '$feed'";
2530 } else {
2531 $query_strategy_part = "cat_id IS NULL";
2532 }
2533
2534 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 2535
ef393de7
AD
2536 } else {
2537 $tmp_result = db_query($link, "SELECT id
2538 FROM ttrss_feeds WHERE parent_feed = '$feed'
2539 ORDER BY cat_id,title");
2540
2541 $parent_ids = array();
2542
2543 if (db_num_rows($tmp_result) > 0) {
2544 while ($p = db_fetch_assoc($tmp_result)) {
2545 array_push($parent_ids, "feed_id = " . $p["id"]);
2546 }
2547
2548 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2549 $feed, implode(" OR ", $parent_ids));
2550
2551 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2552 } else {
2553 $query_strategy_part = "feed_id = '$feed'";
2554 }
2555 }
2556 } else if ($feed == -1) { // starred virtual feed
2557 $query_strategy_part = "marked = true";
2558 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
e4f4b46f
AD
2559 } else if ($feed == -2) { // published virtual feed
2560 $query_strategy_part = "published = true";
2561 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
2562 } else if ($feed <= -10) { // labels
2563 $label_id = -$feed - 11;
2564
2565 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2566 WHERE id = '$label_id'");
2567
2568 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
3de0261a
AD
2569
2570 if (!$query_strategy_part) {
2571 return false;
2572 }
2573
ef393de7
AD
2574 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2575 } else {
2576 $query_strategy_part = "id > 0"; // dumb
2577 }
d6e5706d
AD
2578
2579 if (get_pref($link, 'REVERSE_HEADLINES')) {
2580 $order_by = "updated";
2581 } else {
2582 $order_by = "updated DESC";
2583 }
e939722a
AD
2584
2585 if ($override_order) {
2586 $order_by = $override_order;
2587 }
ef393de7
AD
2588
2589 $feed_title = "";
2590
2591 if ($search && $search_mode == "all_feeds") {
b36e002f 2592 $feed_title = __("Search results")." ($search)";
ef393de7 2593 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
b36e002f 2594 $feed_title = __("Search results")." ($search, $feed)";
ef393de7
AD
2595 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2596 $feed_title = $feed;
2597 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2598
2599 if ($cat_view) {
5c365f60 2600
ef393de7
AD
2601 if ($feed != 0) {
2602 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
c36bf4d5 2603 WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7
AD
2604 $feed_title = db_fetch_result($result, 0, "title");
2605 } else {
d1db26aa 2606 $feed_title = __("Uncategorized");
ef393de7 2607 }
e1eb2147
AD
2608
2609 if ($search) {
b36e002f 2610 $feed_title = __("Searched for")." $search ($feed_title)";
e1eb2147
AD
2611 }
2612
ef393de7
AD
2613 } else {
2614
2615 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
c36bf4d5 2616 WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7
AD
2617
2618 $feed_title = db_fetch_result($result, 0, "title");
2619 $feed_site_url = db_fetch_result($result, 0, "site_url");
2620 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
2621
2622 if ($search) {
b36e002f 2623 $feed_title = __("Searched for") . " $search ($feed_title)";
e1eb2147 2624 }
ef393de7
AD
2625 }
2626
2627 } else if ($feed == -1) {
d1db26aa 2628 $feed_title = __("Starred articles");
e4f4b46f
AD
2629 } else if ($feed == -2) {
2630 $feed_title = __("Published articles");
ef393de7
AD
2631 } else if ($feed < -10) {
2632 $label_id = -$feed - 11;
2633 $result = db_query($link, "SELECT description FROM ttrss_labels
2634 WHERE id = '$label_id'");
2635 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
2636
2637 if ($search) {
b36e002f 2638 $feed_title = __("Searched for") . " $search ($feed_title)";
88040f57 2639 }
ef393de7
AD
2640 } else {
2641 $feed_title = "?";
2642 }
2643
ef393de7
AD
2644 if ($feed < -10) error_reporting (0);
2645
2646 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2647
2648 if ($feed >= 0) {
2649 $feed_kind = "Feeds";
2650 } else {
2651 $feed_kind = "Labels";
2652 }
2653
2654 $content_query_part = "content as content_preview,";
203de776 2655
95a82c08
AD
2656 if ($limit_query_part) {
2657 $offset_query_part = "OFFSET $offset";
2658 }
2659
ef393de7 2660 $query = "SELECT
1f64b1be 2661 guid,
ef393de7 2662 ttrss_entries.id,ttrss_entries.title,
46921916 2663 updated,
e4f4b46f 2664 unread,feed_id,marked,published,link,last_read,
ef393de7
AD
2665 SUBSTRING(last_read,1,19) as last_read_noms,
2666 $vfeed_query_part
2667 $content_query_part
d4b4b9de
AD
2668 SUBSTRING(updated,1,19) as updated_noms,
2669 author
ef393de7
AD
2670 FROM
2671 ttrss_entries,ttrss_user_entries,ttrss_feeds
2672 WHERE
cfb02131 2673 ttrss_feeds.hidden = false AND
ef393de7
AD
2674 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2675 ttrss_user_entries.ref_id = ttrss_entries.id AND
c36bf4d5 2676 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
2677 $search_query_part
2678 $view_query_part
2679 $query_strategy_part ORDER BY $order_by
95a82c08 2680 $limit_query_part $offset_query_part";
ef393de7
AD
2681
2682 $result = db_query($link, $query);
2683
2684 if ($_GET["debug"]) print $query;
2685
2686 } else {
2687 // browsing by tag
2688
2689 $feed_kind = "Tags";
2690
2691 $result = db_query($link, "SELECT
1f64b1be 2692 guid,
ef393de7 2693 ttrss_entries.id as id,title,
46921916 2694 updated,
ef393de7
AD
2695 unread,feed_id,
2696 marked,link,last_read,
2697 SUBSTRING(last_read,1,19) as last_read_noms,
2698 $vfeed_query_part
2699 $content_query_part
2700 SUBSTRING(updated,1,19) as updated_noms
2701 FROM
2702 ttrss_entries,ttrss_user_entries,ttrss_tags
2703 WHERE
2704 ref_id = ttrss_entries.id AND
c36bf4d5 2705 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
2706 post_int_id = int_id AND tag_name = '$feed' AND
2707 $view_query_part
2708 $search_query_part
2709 $query_strategy_part ORDER BY $order_by
2710 $limit_query_part");
2711 }
2712
c7188969 2713 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
2714
2715 }
2716
c36bf4d5 2717 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
3baeeeca 2718 $search, $search_mode, $match_on) {
18664970
AD
2719
2720 $qfh_ret = queryFeedHeadlines($link, $feed,
c36bf4d5
AD
2721 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
2722 $owner_uid);
18664970
AD
2723
2724 $result = $qfh_ret[0];
59e2aab4 2725 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
2726 $feed_site_url = $qfh_ret[2];
2727 $last_error = $qfh_ret[3];
2728
a5472764 2729// if (!$feed_site_url) $feed_site_url = "http://localhost/";
4bc64807 2730
a5472764
AD
2731 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
2732 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
2733 <rss version=\"2.0\">
3baeeeca
AD
2734 <channel>
2735 <title>$feed_title</title>
4bc64807
AD
2736 <link>$feed_site_url</link>
2737 <description>Feed generated by Tiny Tiny RSS</description>";
3baeeeca
AD
2738
2739 while ($line = db_fetch_assoc($result)) {
2740 print "<item>";
4bc64807 2741 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3baeeeca 2742 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
0c3d1c68 2743
bc976a8c 2744 $tags = get_article_tags($link, $line["id"], $owner_uid);
0c3d1c68
AD
2745
2746 foreach ($tags as $tag) {
2747 print "<category>" . htmlspecialchars($tag) . "</category>";
2748 }
2749
3baeeeca
AD
2750 $rfc822_date = date('r', strtotime($line["updated"]));
2751
2752 print "<pubDate>$rfc822_date</pubDate>";
2753
2754 print "<title>" .
2755 htmlspecialchars($line["title"]) . "</title>";
2756
0c3d1c68
AD
2757 print "<description><![CDATA[" .
2758 $line["content_preview"] . "]]></description>";
3baeeeca
AD
2759
2760 print "</item>";
2761 }
2762
2763 print "</channel></rss>";
18664970
AD
2764
2765 }
2766
0a6c4846
AD
2767 function getCategoryTitle($link, $cat_id) {
2768
2769 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2770 id = '$cat_id'");
2771
2772 if (db_num_rows($result) == 1) {
2773 return db_fetch_result($result, 0, "title");
2774 } else {
2775 return "Uncategorized";
2776 }
2777 }
2778
f738aef1
AD
2779 // http://ru2.php.net/strip-tags
2780
2781 function strip_tags_long($textstring, $allowed){
2782 while($textstring != strip_tags($textstring, $allowed))
2783 {
2784 while (strlen($textstring) != 0)
2785 {
2786 if (strlen($textstring) > 1024) {
2787 $otherlen = 1024;
2788 } else {
2789 $otherlen = strlen($textstring);
2790 }
2791 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
2792 $safetext .= $temptext;
2793 $textstring = substr_replace($textstring,'',0,$otherlen);
2794 }
2795 $textstring = $safetext;
2796 }
2797 return $textstring;
2798 }
2799
2800
1ac0baf4 2801 function sanitize_rss($link, $str, $force_strip_tags = false) {
60452879 2802 $res = $str;
183ad07b 2803
1ac0baf4 2804 if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) {
f738aef1
AD
2805 global $tw_parser;
2806 global $tw_paranoya_setup;
2807
2808 $res = $tw_parser->strip_tags($res, $tw_paranoya_setup);
2809
2810// $res = preg_replace("/\r\n|\n|\r/", "", $res);
2811// $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
f826eee1
AD
2812 }
2813
183ad07b
AD
2814 return $res;
2815 }
b72c3ef8 2816
9cd7c995
AD
2817 function send_headlines_digests($link, $limit = 100) {
2818
1ddba275
AD
2819 if (!DIGEST_ENABLE) return false;
2820
9cd7c995 2821 $user_limit = DIGEST_EMAIL_LIMIT;
5430c959 2822 $days = 1;
9cd7c995
AD
2823
2824 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2825
2826 if (DB_TYPE == "pgsql") {
2827 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2828 } else if (DB_TYPE == "mysql") {
2829 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2830 }
2831
2832 $result = db_query($link, "SELECT id,email FROM ttrss_users
2833 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2834
2835 while ($line = db_fetch_assoc($result)) {
2836 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2837 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2838
2839 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2840 $digest = $tuple[0];
2841 $headlines_count = $tuple[1];
2842
2843 if ($headlines_count > 0) {
2844 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2845 "[tt-rss] New headlines for last 24 hours", $digest,
3ab3c1f0
AD
2846 "From: " . MAIL_FROM . "\n".
2847 "Content-Type: text/plain; charset=\"utf-8\"\n".
2848 "Content-Transfer-Encoding: 8bit\n");
9cd7c995
AD
2849 print "RC=$rc\n";
2850 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2851 WHERE id = " . $line["id"]);
2852 } else {
2853 print "No headlines\n";
2854 }
2855 }
2856 }
2857
2858// $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2859
2860 }
2861
7e3634d9 2862 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
d1db26aa 2863 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
7e3634d9
AD
2864 $tmp .= "=======================================================\n\n";
2865
2866 if (DB_TYPE == "pgsql") {
9cd7c995 2867 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
7e3634d9
AD
2868 } else if (DB_TYPE == "mysql") {
2869 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2870 }
2871
2872 $result = db_query($link, "SELECT ttrss_entries.title,
2873 ttrss_feeds.title AS feed_title,
2874 date_entered,
2875 link,
2876 SUBSTRING(last_updated,1,19) AS last_updated
2877 FROM
2878 ttrss_user_entries,ttrss_entries,ttrss_feeds
2879 WHERE
2880 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 2881 AND include_in_digest = true
7e3634d9 2882 AND $interval_query
448b0abd 2883 AND ttrss_user_entries.owner_uid = $user_id
7e3634d9
AD
2884 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2885 LIMIT $limit");
2886
2887 $cur_feed_title = "";
2888
9cd7c995
AD
2889 $headlines_count = db_num_rows($result);
2890
7e3634d9
AD
2891 while ($line = db_fetch_assoc($result)) {
2892 $updated = smart_date_time(strtotime($line["last_updated"]));
2893 $feed_title = $line["feed_title"];
2894
2895 if ($cur_feed_title != $feed_title) {
2896 $cur_feed_title = $feed_title;
2897
2898 $tmp .= "$feed_title\n\n";
2899 }
2900
2901 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2902 $tmp .= " " . trim($line["link"]) . "\n";
2903 $tmp .= "\n";
2904 }
2905
2906 $tmp .= "--- \n";
d1db26aa 2907 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
dfe6f833 2908 DIGEST_HOSTNAME . "\n".
d1db26aa 2909 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
7e3634d9
AD
2910
2911
9cd7c995 2912 return array($tmp, $headlines_count);
7e3634d9
AD
2913 }
2914
d9fa39f1 2915 function check_for_update($link, $brief_fmt = true) {
b72c3ef8
AD
2916 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2917
2918 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
2919 return;
2920 }
2921
2922 error_reporting(0);
2923 $rss = fetch_rss($releases_feed);
2924 error_reporting (DEFAULT_ERROR_LEVEL);
2925
2926 if ($rss) {
2927
2928 $items = $rss->items;
2929
2930 if (!$items || !is_array($items)) $items = $rss->entries;
2931 if (!$items || !is_array($items)) $items = $rss;
2932
da412ad3 2933 if (!is_array($items) || count($items) == 0) {
b72c3ef8 2934 return;
da412ad3 2935 }
b72c3ef8 2936
a41d2c65 2937 $latest_item = $items[0];
b72c3ef8 2938
a41d2c65 2939 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
b72c3ef8 2940
007a38d4
AD
2941 $release_url = sanitize_rss($link, $latest_item["link"]);
2942 $content = sanitize_rss($link, $latest_item["description"]);
48e1a342 2943
a41d2c65 2944 if (version_compare(VERSION, $latest_version) == -1) {
d9fa39f1 2945 if ($brief_fmt) {
0d32b41e 2946 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
d9fa39f1 2947 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
0d32b41e 2948 <div id=\"milestoneDetails\">$content</div>");
d9fa39f1 2949 } else {
92625568
AD
2950 return "New version of Tiny-Tiny RSS ($latest_version) is available:
2951 <div class='milestoneDetails'>$content</div>
2952 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
2953 download and update information.";
d9fa39f1
AD
2954 }
2955
da412ad3 2956 }
b72c3ef8
AD
2957 }
2958 }
472782e8 2959
18eddb2c
AD
2960 function markArticlesById($link, $ids, $cmode) {
2961
2962 $tmp_ids = array();
2963
2964 foreach ($ids as $id) {
2965 array_push($tmp_ids, "ref_id = '$id'");
2966 }
2967
2968 $ids_qpart = join(" OR ", $tmp_ids);
2969
2970 if ($cmode == 0) {
2971 db_query($link, "UPDATE ttrss_user_entries SET
2972 marked = false,last_read = NOW()
2973 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2974 } else if ($cmode == 1) {
2975 db_query($link, "UPDATE ttrss_user_entries SET
2976 marked = true
2977 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2978 } else {
2979 db_query($link, "UPDATE ttrss_user_entries SET
2980 marked = NOT marked,last_read = NOW()
2981 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2982 }
2983 }
2984
e4f4b46f
AD
2985 function publishArticlesById($link, $ids, $cmode) {
2986
2987 $tmp_ids = array();
2988
2989 foreach ($ids as $id) {
2990 array_push($tmp_ids, "ref_id = '$id'");
2991 }
2992
2993 $ids_qpart = join(" OR ", $tmp_ids);
2994
2995 if ($cmode == 0) {
2996 db_query($link, "UPDATE ttrss_user_entries SET
2997 published = false,last_read = NOW()
2998 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2999 } else if ($cmode == 1) {
3000 db_query($link, "UPDATE ttrss_user_entries SET
3001 published = true
3002 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3003 } else {
3004 db_query($link, "UPDATE ttrss_user_entries SET
3005 published = NOT published,last_read = NOW()
3006 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3007 }
3008 }
3009
472782e8
AD
3010 function catchupArticlesById($link, $ids, $cmode) {
3011
3012 $tmp_ids = array();
3013
3014 foreach ($ids as $id) {
3015 array_push($tmp_ids, "ref_id = '$id'");
3016 }
3017
3018 $ids_qpart = join(" OR ", $tmp_ids);
3019
3020 if ($cmode == 0) {
3021 db_query($link, "UPDATE ttrss_user_entries SET
3022 unread = false,last_read = NOW()
3023 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3024 } else if ($cmode == 1) {
3025 db_query($link, "UPDATE ttrss_user_entries SET
3026 unread = true
3027 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3028 } else {
3029 db_query($link, "UPDATE ttrss_user_entries SET
3030 unread = NOT unread,last_read = NOW()
3031 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3032 }
3033 }
3034
e097e8be
AD
3035 function catchupArticleById($link, $id, $cmode) {
3036
3037 if ($cmode == 0) {
3038 db_query($link, "UPDATE ttrss_user_entries SET
3039 unread = false,last_read = NOW()
3040 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3041 } else if ($cmode == 1) {
3042 db_query($link, "UPDATE ttrss_user_entries SET
3043 unread = true
3044 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3045 } else {
3046 db_query($link, "UPDATE ttrss_user_entries SET
3047 unread = NOT unread,last_read = NOW()
3048 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3049 }
3050 }
3051
1f64b1be
AD
3052 function make_guid_from_title($title) {
3053 return preg_replace("/[ \"\',.:;]/", "-",
fefef828 3054 mb_strtolower(strip_tags($title), 'utf-8'));
1f64b1be
AD
3055 }
3056
11befbb2
AD
3057 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3058 $bottom = false, $rtl_content = false, $feed_id = 0,
3059 $is_cat = false, $search = false, $match_on = false,
95a82c08 3060 $search_mode = false, $offset = 0, $limit = 0) {
11befbb2 3061
e6c115b2
AD
3062 $user_page_offset = $offset + 1;
3063
11befbb2
AD
3064 if (!$bottom) {
3065 $class = "headlinesSubToolbar";
3066 $tid = "headlineActionsTop";
3067 } else {
3068 $class = "headlinesSubToolbar";
3069 $tid = "headlineActionsBottom";
3070 }
3071
3072 print "<table class=\"$class\" id=\"$tid\"
3073 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3074
3075 if ($rtl_content) {
3076 $rtl_cpart = "RTL";
3077 } else {
3078 $rtl_cpart = "";
3079 }
3080
e6c115b2
AD
3081 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3082 $page_next_link = "javascript:viewFeedGoPage(1)";
3083 $page_first_link = "javascript:viewFeedGoPage(0)";
203de776 3084
eb28b131
AD
3085 $catchup_page_link = "javascript:catchupPage()";
3086 $catchup_feed_link = "javascript:catchupCurrentFeed()";
c6008b62 3087
11befbb2
AD
3088 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3089
c6008b62
AD
3090 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3091 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3092 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
11befbb2 3093
c6008b62
AD
3094 $tog_unread_link = "javascript:selectionToggleUnread()";
3095 $tog_marked_link = "javascript:selectionToggleMarked()";
e4f4b46f 3096 $tog_published_link = "javascript:selectionTogglePublished()";
11befbb2 3097
c6008b62 3098 } else {
11befbb2 3099
c6008b62
AD
3100 $sel_all_link = "javascript:cdmSelectArticles('all')";
3101 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3102 $sel_none_link = "javascript:cdmSelectArticles('none')";
3103
3104 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3105 $tog_marked_link = "javascript:selectionToggleMarked(true)";
e4f4b46f 3106 $tog_published_link = "javascript:selectionTogglePublished(true)";
c6008b62
AD
3107
3108 }
3109
d420f2ee 3110 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
c6008b62 3111
2dd2c13b
AD
3112 print "<td class=\"headlineActions$rtl_cpart\">
3113 <ul class=\"headlineDropdownMenu\">
3114 <li class=\"top2\">
3692e98f 3115 ".__('Select:')."
1025ad87
AD
3116 <a href=\"$sel_all_link\">".__('All')."</a>,
3117 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3118 <a href=\"$sel_none_link\">".__('None')."</a></li>
2dd2c13b 3119 <li class=\"vsep\">&nbsp;</li>
89cb787e 3120 <li class=\"top\">".__('Toggle')."<ul>
9cc600d1 3121 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
e4f4b46f
AD
3122 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3123 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3124 </ul></li>
2dd2c13b 3125 <li class=\"vsep\">&nbsp;</li>
1025ad87
AD
3126 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
3127 <li onclick=\"$catchup_page_link\">".__('This page')."</li>
3128 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
237ec2ad 3129 ";
95a82c08 3130
237ec2ad
AD
3131 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3132
3133 if ($limit != 0 && !$search && $enable_pagination) {
95a82c08 3134 print "
237ec2ad 3135 <li class=\"vsep\">&nbsp;</li>
1025ad87
AD
3136 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3137 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3138 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
95a82c08 3139 </ul>";
d420f2ee 3140 }
95a82c08 3141
d420f2ee 3142 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
237ec2ad
AD
3143 print "
3144 <li class=\"vsep\">&nbsp;</li>
3145 <li class=\"top3\">
d420f2ee
AD
3146 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3147 '$match_on', '$feed_id', '$is_cat');\">
b36e002f 3148 ".__('Convert to label')."</a></td>";
d420f2ee 3149 }
95a82c08 3150 print "
2dd2c13b
AD
3151 </td>";
3152
3153 } else {
e6c115b2
AD
3154 // old style subtoolbar:
3155
2dd2c13b 3156 print "<td class=\"headlineActions$rtl_cpart\">".
d1db26aa 3157 __('Select:')."
1025ad87
AD
3158 <a href=\"$sel_all_link\">".__('All')."</a>,
3159 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3160 <a href=\"$sel_none_link\">".__('None')."</a>
2dd2c13b 3161 &nbsp;&nbsp;".
1025ad87
AD
3162 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3163 <a href=\"$tog_marked_link\">".__('Starred')."</a>
2dd2c13b 3164 &nbsp;&nbsp;".
d1db26aa 3165 __('Mark as read:')."
1025ad87
AD
3166 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3167 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
d420f2ee
AD
3168
3169 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3170
3171 print "&nbsp;&nbsp;
3172 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3173 '$match_on', '$feed_id', '$is_cat');\">
b36e002f 3174 ".__('Convert to label')."</a>";
d420f2ee
AD
3175 }
3176
2dd2c13b
AD
3177 print "</td>";
3178
3179 }
c6008b62 3180
d420f2ee 3181/* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
c6008b62
AD
3182 print "<td class=\"headlineActions$rtl_cpart\">
3183 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3184 '$match_on', '$feed_id', '$is_cat');\">
d1db26aa 3185 ".__('Convert to Label')."</a></td>";
d420f2ee 3186} */
11befbb2
AD
3187
3188 print "<td class=\"headlineTitle$rtl_cpart\">";
3189
3190 if ($feed_site_url) {
3191 if (!$bottom) {
3fc2eed5 3192 $target = "target=\"_new\"";
11befbb2
AD
3193 }
3194 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
3195 } else {
3196 print $feed_title;
3197 }
3198
3199 if ($search) {
3200 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
3201 }
3202
e6c115b2
AD
3203 if ($user_page_offset > 1) {
3204 print " [$user_page_offset] ";
3205 }
3206
11befbb2 3207 if (!$bottom) {
e6c115b2 3208 print "
11befbb2
AD
3209 <a target=\"_new\"
3210 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3211 <img class=\"noborder\"
1025ad87 3212 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
11befbb2
AD
3213 </a>";
3214 }
3215
3216 print "</td>";
3217 print "</tr></table>";
3218
3219 }
3220
f407c086
AD
3221 function outputFeedList($link, $tags = false) {
3222
3bd9a780 3223 print "<ul class=\"feedList\" id=\"feedList\">";
f407c086
AD
3224
3225 $owner_uid = $_SESSION["uid"];
3226
cf4d339c 3227 /* virtual feeds */
f407c086 3228
cf4d339c 3229 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780
AD
3230 print "<li class=\"feedCat\">".__('Special')."</li>";
3231 print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
cf4d339c 3232 }
f407c086 3233
cf4d339c 3234 $num_starred = getFeedUnread($link, -1);
e4f4b46f 3235 $num_published = getFeedUnread($link, -2);
f407c086 3236
cf4d339c 3237 $class = "virt";
f407c086 3238
cf4d339c 3239 if ($num_starred > 0) $class .= "Unread";
f407c086 3240
d1db26aa 3241 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
cf4d339c 3242 "images/mark_set.png", $link);
f407c086 3243
e4f4b46f
AD
3244 $class = "virt";
3245
3246 if ($num_published > 0) $class .= "Unread";
3247
3248 printFeedEntry(-2, $class, __("Published articles"), $num_published,
3249 "images/pub_set.png", $link);
3250
cf4d339c 3251 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 3252 print "</ul>";
cf4d339c 3253 }
f407c086 3254
cf4d339c 3255 if (!$tags) {
f407c086
AD
3256
3257 if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
3258
3259 $result = db_query($link, "SELECT id,sql_exp,description FROM
3260 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3261
3262 if (db_num_rows($result) > 0) {
3263 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780
AD
3264 print "<li class=\"feedCat\">".__('Labels')."</li>";
3265 print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
f407c086 3266 } else {
3bd9a780 3267 print "<li><hr></li>";
f407c086
AD
3268 }
3269 }
3270
3271 while ($line = db_fetch_assoc($result)) {
3272
3273 error_reporting (0);
3274
3275 $label_id = -$line['id'] - 11;
3276 $count = getFeedUnread($link, $label_id);
3277
3278 $class = "label";
3279
3280 if ($count > 0) {
3281 $class .= "Unread";
3282 }
3283
3284 error_reporting (DEFAULT_ERROR_LEVEL);
3285
3286 printFeedEntry($label_id,
47439031 3287 $class, $line["description"],
f407c086
AD
3288 $count, "images/label.png", $link);
3289
3290 }
3291
3292 if (db_num_rows($result) > 0) {
3293 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3294 print "</ul>";
3295 }
3296 }
3297
3298 }
3299
3300 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 3301 print "<li><hr></li>";
f407c086
AD
3302 }
3303
3304 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3305 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3306 $order_by_qpart = "category,unread DESC,title";
3307 } else {
3308 $order_by_qpart = "category,title";
3309 }
3310 } else {
3311 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3312 $order_by_qpart = "unread DESC,title";
3313 } else {
3314 $order_by_qpart = "title";
3315 }
3316 }
3317
3318 $result = db_query($link, "SELECT ttrss_feeds.*,
3319 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3320 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3321 WHERE feed_id = ttrss_feeds.id AND unread = true
3322 AND ttrss_user_entries.ref_id = ttrss_entries.id
3323 AND owner_uid = '$owner_uid') as unread,
3324 cat_id,last_error,
3325 ttrss_feed_categories.title AS category,
3326 ttrss_feed_categories.collapsed
3327 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3328 ON (ttrss_feed_categories.id = cat_id)
3329 WHERE
3330 ttrss_feeds.hidden = false AND
3331 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3332 ORDER BY $order_by_qpart");
3333
3334 $actid = $_GET["actid"];
3335
3336 /* real feeds */
3337
3338 $lnum = 0;
3339
3340 $total_unread = 0;
3341
3342 $category = "";
3343
3344 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3345
3346 while ($line = db_fetch_assoc($result)) {
3347
47439031 3348 $feed = trim($line["title"]);
0f39ae20
AD
3349
3350 if (!$feed) $feed = "[Untitled]";
3351
f407c086
AD
3352 $feed_id = $line["id"];
3353
3354 $subop = $_GET["subop"];
3355
3356 $unread = $line["unread"];
3357
3358 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3359 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3360 } else {
3361 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3362 }
3363
3364 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3365
3366 if ($rtl_content) {
3367 $rtl_tag = "dir=\"RTL\"";
3368 } else {
3369 $rtl_tag = "";
3370 }
3371
3372 $tmp_result = db_query($link,
3373 "SELECT id,COUNT(unread) AS unread
3374 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3375 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3376 WHERE parent_feed = '$feed_id' AND unread = true
3377 GROUP BY ttrss_feeds.id");
3378
3379 if (db_num_rows($tmp_result) > 0) {
3380 while ($l = db_fetch_assoc($tmp_result)) {
3381 $unread += $l["unread"];
3382 }
3383 }
3384
3385 $cat_id = $line["cat_id"];
3386
3387 $tmp_category = $line["category"];
3388
3389 if (!$tmp_category) {
d1db26aa 3390 $tmp_category = __("Uncategorized");
f407c086
AD
3391 }
3392
3393 // $class = ($lnum % 2) ? "even" : "odd";
3394
3395 if ($line["last_error"]) {
3396 $class = "error";
3397 } else {
3398 $class = "feed";
3399 }
3400
3401 if ($unread > 0) $class .= "Unread";
3402
3403 if ($actid == $feed_id) {
3404 $class .= "Selected";
3405 }
3406
3407 $total_unread += $unread;
3408
3409 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3410
3411 if ($category) {
3412 print "</ul></li>";
3413 }
3414
3415 $category = $tmp_category;
3416
3417 $collapsed = $line["collapsed"];
3418
3419 // workaround for NULL category
d1db26aa 3420 if ($category == __("Uncategorized")) {
f407c086
AD
3421 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3422 $collapsed = "t";
3423 }
3424 }
3425
3426 if ($collapsed == "t" || $collapsed == "1") {
225ec0d4
AD
3427 $holder_class = "feedCatHolder";
3428 $holder_style = "display:none;";
f407c086
AD
3429 $ellipsis = "...";
3430 } else {
be5b75da 3431 $holder_class = "feedCatHolder";
225ec0d4 3432 $holder_style = "";
f407c086
AD
3433 $ellipsis = "";
3434 }
3435
3436 $cat_id = sprintf("%d", $cat_id);
3437
3438 $cat_unread = getCategoryUnread($link, $cat_id);
67dabe1a
AD
3439
3440 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3441
3bd9a780 3442 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
439bbe63 3443 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
7210613a 3444 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
67dabe1a
AD
3445 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3446 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3bd9a780 3447 </a></li>";
f407c086
AD
3448
3449 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
3450 // -> keyboard navigation, etc.
225ec0d4 3451 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
f407c086
AD
3452 }
3453
3454 printFeedEntry($feed_id, $class, $feed, $unread,
99e37e09 3455 ICONS_DIR."/$feed_id.ico", $link, $rtl_content,
f407c086
AD
3456 $last_updated, $line["last_error"]);
3457
3458 ++$lnum;
3459 }
3460
3461 if (db_num_rows($result) == 0) {
3bd9a780 3462 print "<li>".__('No feeds to display.')."</li>";
f407c086
AD
3463 }
3464
3465 } else {
3466
3467 // tags
3468
3469/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3470 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3471 post_int_id = ttrss_user_entries.int_id AND
3472 unread = true AND ref_id = ttrss_entries.id
3473 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3474 UNION
3475 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3476 ORDER BY tag_name"); */
3477
3478 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 3479 print "<li class=\"feedCat\">".__('Tags')."</li>";
f407c086
AD
3480 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3481 }
3482
3483 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3484 FROM ttrss_user_entries WHERE int_id = post_int_id
3485 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
3486 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
3487 ORDER BY count DESC LIMIT 50");
f407c086
AD
3488
3489 $tags = array();
3490
3491 while ($line = db_fetch_assoc($result)) {
3492 $tags[$line["tag_name"]] += $line["count"];
3493 }
3494
3495 foreach (array_keys($tags) as $tag) {
3496
3497 $unread = $tags[$tag];
3498
3499 $class = "tag";
3500
3501 if ($unread > 0) {
3502 $class .= "Unread";
3503 }
3504
3505 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3506
3507 }
3508
3509 if (db_num_rows($result) == 0) {
3510 print "<li>No tags to display.</li>";
3511 }
3512
3513 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 3514 print "</ul>";
f407c086
AD
3515 }
3516
3517 }
3518
3519 print "</ul>";
3520
3521 }
3522
bc976a8c 3523 function get_article_tags($link, $id, $owner_uid = 0) {
0b126ac2
AD
3524
3525 $a_id = db_escape_string($id);
3526
bc976a8c
AD
3527 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3528
0c3d1c68
AD
3529 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
3530 owner_uid as owner FROM
0b126ac2 3531 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
bc976a8c 3532 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
0b126ac2
AD
3533
3534 $tags = array();
3535
3536 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3537 array_push($tags, $tmp_line["tag_name"]);
3538 }
3539
3540 return $tags;
3541 }
3542
d62a3b63
AD
3543 function trim_value(&$value) {
3544 $value = trim($value);
3545 }
3546
3547 function trim_array($array) {
3548 $tmp = $array;
3549 array_walk($tmp, 'trim_value');
3550 return $tmp;
3551 }
3552
be832a1a 3553 function tag_is_valid($tag) {
ef063748
AD
3554 if ($tag == '') return false;
3555 if (preg_match("/^[0-9]*$/", $tag)) return false;
3556
3557 $tag = iconv("utf-8", "utf-8", $tag);
3558 if (!$tag) return false;
3559
3560 return true;
be832a1a
AD
3561 }
3562
793185a9
AD
3563 function render_login_form($link, $mobile = false) {
3564 if (!$mobile) {
3565 require_once "login_form.php";
3566 } else {
3567 require_once "mobile/login_form.php";
3568 }
01a87dff
AD
3569 }
3570
dc56b3b7
AD
3571 // from http://developer.apple.com/internet/safari/faq.html
3572 function no_cache_incantation() {
3573 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3574 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3575 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3576 header("Cache-Control: post-check=0, pre-check=0", false);
3577 header("Pragma: no-cache"); // HTTP/1.0
3578 }
3579
42395d28
AD
3580 function format_warning($msg, $id = "") {
3581 return "<div class=\"warning\" id=\"$id\">
0d32b41e
AD
3582 <img src=\"images/sign_excl.png\">$msg</div>";
3583 }
3584
3585 function format_notice($msg) {
3586 return "<div class=\"notice\">
3587 <img src=\"images/sign_info.png\">$msg</div>";
3588 }
3589
68d2f95e
AD
3590 function format_error($msg) {
3591 return "<div class=\"error\">
3592 <img src=\"images/sign_excl.png\">$msg</div>";
3593 }
3594
4dccf1ed
AD
3595 function print_notice($msg) {
3596 return print format_notice($msg);
3597 }
3598
3599 function print_warning($msg) {
3600 return print format_warning($msg);
3601 }
3602
68d2f95e
AD
3603 function print_error($msg) {
3604 return print format_error($msg);
3605 }
3606
3607
4dccf1ed
AD
3608 function T_sprintf() {
3609 $args = func_get_args();
3610 return vsprintf(__(array_shift($args)), $args);
3611 }
3612
3de0261a
AD
3613 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
3614
10eb9da8
AD
3615 /* we can figure out feed_id from article id anyway, why do we
3616 * pass feed_id here? */
3617
3618 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
3619 WHERE ref_id = '$id'");
3620
3621 $feed_id = db_fetch_result($result, 0, "feed_id");
3622
3de0261a
AD
3623 print "<article id='$id'><![CDATA[";
3624
3625 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3626 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
3627
3628 if (db_num_rows($result) == 1) {
3629 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3630 } else {
3631 $rtl_content = false;
3632 }
3633
3634 if ($rtl_content) {
3635 $rtl_tag = "dir=\"RTL\"";
3636 $rtl_class = "RTL";
3637 } else {
3638 $rtl_tag = "";
3639 $rtl_class = "";
3640 }
3641
3642 if ($mark_as_read) {
3643 $result = db_query($link, "UPDATE ttrss_user_entries
3644 SET unread = false,last_read = NOW()
3645 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3646 }
3647
3648 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
3649 SUBSTRING(updated,1,16) as updated,
3650 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
3651 num_comments,
3652 author
3653 FROM ttrss_entries,ttrss_user_entries
3654 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
3655
3656 if ($result) {
3657
3658 $link_target = "";
3659
3660 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3661 $link_target = "target=\"_new\"";
3662 }
3663
3664 $line = db_fetch_assoc($result);
3665
3666 if ($line["icon_url"]) {
3667 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
3668 } else {
3669 $feed_icon = "&nbsp;";
3670 }
3671
3672/* if ($line["comments"] && $line["link"] != $line["comments"]) {
3673 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
3674 } else {
3675 $entry_comments = "";
3676 } */
3677
3678 $num_comments = $line["num_comments"];
3679 $entry_comments = "";
3680
3681 if ($num_comments > 0) {
3682 if ($line["comments"]) {
3683 $comments_url = $line["comments"];
3684 } else {
3685 $comments_url = $line["link"];
3686 }
3687 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
3688 } else {
3689 if ($line["comments"] && $line["link"] != $line["comments"]) {
3690 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
3691 }
3692 }
3693
3694 print "<div class=\"postReply\">";
3695
3696 print "<div class=\"postHeader\">";
3697
3698 $entry_author = $line["author"];
3699
3700 if ($entry_author) {
3701 $entry_author = __(" - by ") . $entry_author;
3702 }
3703
3704 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
3705 strtotime($line["updated"]));
3706
3707 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
3708
3709 if ($line["link"]) {
3710 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
3711 $line["title"] . "</a>$entry_author</div>";
3712 } else {
3713 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
3714 }
3715
9cfd409d 3716/* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3de0261a 3717 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
9cfd409d
AD
3718 ORDER BY tag_name"); */
3719
3720 $tags = get_article_tags($link, $id);
3de0261a
AD
3721
3722 $tags_str = "";
3723 $f_tags_str = "";
3724
3725 $num_tags = 0;
3726
9cfd409d 3727 foreach ($tags as $tag) {
3de0261a 3728 $num_tags++;
14b6c54b
AD
3729 $tag_escaped = str_replace("'", "\\'", $tag);
3730
3731 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
3de0261a
AD
3732
3733 if ($num_tags == 6) {
e7544143
AD
3734 $tags_str .= "...";
3735
3de0261a
AD
3736 } else if ($num_tags < 6) {
3737 $tags_str .= $tag_str;
3738 }
3739 $f_tags_str .= $tag_str;
3740 }
3741
3742 $tags_str = preg_replace("/, $/", "", $tags_str);
3743 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
3744
e7544143
AD
3745 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
3746 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
3747
3de0261a
AD
3748 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
3749
3750 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
3751
3752 print "<div style='float : right'>$tags_str
3753 <a title=\"Edit tags for this article\"
3754 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
3755 <div clear='both'>$entry_comments</div>";
3756
3757 print "</div>";
3758
3759 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
3760 print "<div class=\"postContent\">";
3761
e7544143 3762 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
3de0261a 3763
c41890b0
AD
3764 $line["content"] = sanitize_rss($link, $line["content"]);
3765
3de0261a
AD
3766 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
3767 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
3768 }
3769
3de0261a
AD
3770 print $line["content"] . "</div>";
3771
3772 print "</div>";
3773
3774 }
3775
3776 print "]]></article>";
3777
3778 }
3779
3780 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
3781 $next_unread_feed, $offset) {
3782
46921916
AD
3783 $timing_info = getmicrotime();
3784
961f4c73
AD
3785 $topmost_article_ids = array();
3786
ac541432
AD
3787 if (!$offset) {
3788 $offset = 0;
3789 }
3de0261a
AD
3790
3791 if ($subop == "undefined") $subop = "";
3792
3793 if ($subop == "CatchupSelected") {
3794 $ids = split(",", db_escape_string($_GET["ids"]));
3795 $cmode = sprintf("%d", $_GET["cmode"]);
3796
3797 catchupArticlesById($link, $ids, $cmode);
3798 }
3799
3800 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
3801 update_generic_feed($link, $feed, $cat_view);
3802 }
3803
3804 if ($subop == "MarkAllRead") {
3805 catchup_feed($link, $feed, $cat_view);
3806
3807 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
3808 if ($next_unread_feed) {
3809 $feed = $next_unread_feed;
3810 }
3811 }
3812 }
3813
3814 if ($feed_id > 0) {
3815 $result = db_query($link,
3816 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
3817
3818 if (db_num_rows($result) == 0) {
3819 print "<div align='center'>".__('Feed not found.')."</div>";
3820 return;
3821 }
3822 }
3823
3824 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
10eb9da8 3825
3de0261a
AD
3826 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
3827 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
3828
3829 if (db_num_rows($result) == 1) {
3830 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
3831 } else {
3832 $rtl_content = false;
3833 }
3834
3835 if ($rtl_content) {
3836 $rtl_tag = "dir=\"RTL\"";
3837 } else {
3838 $rtl_tag = "";
3839 }
3840 } else {
3841 $rtl_tag = "";
3842 $rtl_content = false;
3843 }
3844
3845 $script_dt_add = get_script_dt_add();
3846
3847 /// START /////////////////////////////////////////////////////////////////////////////////
3848
3849 $search = db_escape_string($_GET["query"]);
3850 $search_mode = db_escape_string($_GET["search_mode"]);
3851 $match_on = db_escape_string($_GET["match_on"]);
3852
3853 if (!$match_on) {
3854 $match_on = "both";
3855 }
3856
3857 $real_offset = $offset * $limit;
3858
46921916
AD
3859 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
3860
3de0261a
AD
3861 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
3862 $search, $search_mode, $match_on, false, $real_offset);
3863
46921916
AD
3864 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
3865
3de0261a
AD
3866 $result = $qfh_ret[0];
3867 $feed_title = $qfh_ret[1];
3868 $feed_site_url = $qfh_ret[2];
3869 $last_error = $qfh_ret[3];
f56e3080
AD
3870
3871 if ($feed == -2) {
3872 $feed_site_url = article_publish_url($link);
3873 }
3874
3de0261a
AD
3875 /// STOP //////////////////////////////////////////////////////////////////////////////////
3876
ac541432
AD
3877 if (!$offset) {
3878 print "<div id=\"headlinesContainer\" $rtl_tag>";
3de0261a 3879
ac541432
AD
3880 if (!$result) {
3881 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
3882 return;
3883 }
3de0261a 3884
ac541432
AD
3885 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
3886 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
3887 $offset, $limit);
3de0261a 3888
ac541432
AD
3889 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
3890 }
3de0261a
AD
3891
3892 if (db_num_rows($result) > 0) {
3893
3894# print "\{$offset}";
3895
ac541432 3896 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
3897 print "<table class=\"headlinesList\" id=\"headlinesList\"
3898 cellspacing=\"0\">";
3899 }
3900
3901 $lnum = 0;
3902
3903 error_reporting (DEFAULT_ERROR_LEVEL);
3904
3905 $num_unread = 0;
3906
3907 while ($line = db_fetch_assoc($result)) {
3908
3909 $class = ($lnum % 2) ? "even" : "odd";
3910
3911 $id = $line["id"];
3912 $feed_id = $line["feed_id"];
961f4c73
AD
3913
3914 if (count($topmost_article_ids) < 5) {
3915 array_push($topmost_article_ids, $id);
3916 }
3917
3de0261a
AD
3918 if ($line["last_read"] == "" &&
3919 ($line["unread"] != "t" && $line["unread"] != "1")) {
3920
3921 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
3922 alt=\"Updated\">";
3923 } else {
3924 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
3925 alt=\"Updated\">";
3926 }
3927
3928 if ($line["unread"] == "t" || $line["unread"] == "1") {
3929 $class .= "Unread";
3930 ++$num_unread;
3931 $is_unread = true;
3932 } else {
3933 $is_unread = false;
3934 }
3935
3936 if ($line["marked"] == "t" || $line["marked"] == "1") {
67343d9f 3937 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.png\"
3de0261a 3938 class=\"markedPic\"
67343d9f 3939 alt=\"Reset mark\" onclick='javascript:tMark($id)'>";
3de0261a 3940 } else {
67343d9f 3941 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.png\"
3de0261a 3942 class=\"markedPic\"
67343d9f 3943 alt=\"Set mark\" onclick='javascript:tMark($id)'>";
3de0261a
AD
3944 }
3945
e4f4b46f
AD
3946 if ($line["published"] == "t" || $line["published"] == "1") {
3947 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.png\"
3948 class=\"markedPic\"
3949 alt=\"Unpublish\" onclick='javascript:tPub($id)'>";
3950 } else {
3951 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.png\"
3952 class=\"markedPic\"
3953 alt=\"Publish\" onclick='javascript:tPub($id)'>";
3954 }
3955
3de0261a
AD
3956# $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
3957# $line["title"] . "</a>";
3958
3959 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
3960 $line["title"] . "</a>";
3961
3962# $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
3963# $line["title"] . "</a>";
3964
3965 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
46921916 3966 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
3de0261a
AD
3967 } else {
3968 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
46921916 3969 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
3de0261a
AD
3970 }
3971
3972 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
3973 $content_preview = truncate_string(strip_tags($line["content_preview"]),
3974 100);
3975 }
3976
3977 $entry_author = $line["author"];
3978
3979 if ($entry_author) {
3980 $entry_author = " - by $entry_author";
3981 }
3982
3983 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3984
3985 print "<tr class='$class' id='RROW-$id'>";
3986
67343d9f 3987 print "<td class='hlUpdPic'>$update_pic</td>";
3de0261a
AD
3988
3989 print "<td class='hlSelectRow'>
67343d9f
AD
3990 <input type=\"checkbox\" onclick=\"tSR(this)\"
3991 id=\"RCHK-$id\">
3de0261a
AD
3992 </td>";
3993
3994 print "<td class='hlMarkedPic'>$marked_pic</td>";
e4f4b46f 3995 print "<td class='hlMarkedPic'>$published_pic</td>";
3de0261a
AD
3996
3997 if ($line["feed_title"]) {
3998 print "<td class='hlContent'>$content_link</td>";
3999 print "<td class='hlFeed'>
4000 <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4001 $line["feed_title"]."</a>&nbsp;</td>";
4002 } else {
4003 print "<td class='hlContent' valign='middle'>";
4004
4005 print "<a href=\"javascript:view($id,$feed_id);\">" .
4006 $line["title"];
4007
4008 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4009 if ($content_preview) {
4010 print "<span class=\"contentPreview\"> - $content_preview</span>";
4011 }
4012 }
4013
4014 print "</a>";
4015 print "</td>";
4016 }
4017
4018 print "<td class=\"hlUpdated\"><nobr>$updated_fmt&nbsp;</nobr></td>";
4019
4020 print "</tr>";
4021
4022 } else {
4023
4024 if ($is_unread) {
4025 $add_class = "Unread";
4026 } else {
4027 $add_class = "";
4028 }
4029
e4914b62
AD
4030 print "<div class=\"cdmArticle$add_class\"
4031 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4032 onmouseout='cdmMouseOut(this)'>";
3de0261a
AD
4033
4034 print "<div class=\"cdmHeader\">";
4035
4036 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4037
4038 print "<a class=\"title\"
4039 onclick=\"javascript:toggleUnread($id, 0)\"
3fc2eed5 4040 target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
3de0261a
AD
4041
4042 print $entry_author;
4043
4044 if ($line["feed_title"]) {
4045 print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4046 }
4047
4048 print "</div>";
4049
3fc2eed5
AD
4050 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4051 $line["content_preview"] = preg_replace("/href=/i",
4052 "target=\"_new\" href=", $line["content_preview"]);
4053 }
4054
3de0261a
AD
4055 print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">";
4056
e2ccbfab 4057 print "<div class=\"cdmFooter\"><span class='s0'>";
3de0261a 4058
e2ccbfab 4059 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
3de0261a 4060
e2ccbfab
AD
4061 print __("Select:").
4062 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
3de0261a
AD
4063 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4064
e2ccbfab 4065 print "</span><span class='s1'>$marked_pic</span> ";
e4f4b46f 4066 print "<span class='s1'>$published_pic</span> ";
e2ccbfab 4067
3de0261a
AD
4068 $tags = get_article_tags($link, $id);
4069
4070 $tags_str = "";
22d1f3db 4071 $full_tags_str = "";
d735ebd2 4072 $num_tags = 0;
3de0261a
AD
4073
4074 foreach ($tags as $tag) {
4075 $num_tags++;
22d1f3db
AD
4076 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4077 if ($num_tags < 5) {
4078 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4079 } else if ($num_tags == 5) {
4080 $tags_str .= "...";
4081 }
3de0261a
AD
4082 }
4083
4084 $tags_str = preg_replace("/, $/", "", $tags_str);
22d1f3db
AD
4085 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
4086
4087 $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
4088
4089 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4090
3de0261a
AD
4091
4092 if ($tags_str == "") $tags_str = "no tags";
e2ccbfab
AD
4093
4094// print "<img src='images/tag.png' class='markedPic'>";
4095
4096 print "<span class='s1'>Tags: $tags_str <a title=\"Edit tags for this article\"
3de0261a
AD
4097 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4098
e2ccbfab 4099 print "</span>";
3de0261a 4100
e2ccbfab
AD
4101 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4102 href=\"javascript:toggleUnread($id)\">
4103 Unread</a></span>";
3de0261a 4104
e2ccbfab 4105 print "</div>";
3de0261a
AD
4106 print "</div>";
4107
4108 }
4109
4110 ++$lnum;
4111 }
4112
ac541432 4113 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
4114 print "</table>";
4115 }
4116
4117// print_headline_subtoolbar($link,
4118// "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
4119
4120
4121 } else {
ac541432 4122 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
3de0261a
AD
4123 }
4124
ac541432
AD
4125 if (!$offset) {
4126 print "</div>";
4127 print "</div>";
4128 }
3de0261a 4129
961f4c73 4130 return $topmost_article_ids;
3de0261a 4131 }
0979b696
AD
4132
4133// from here: http://www.roscripts.com/Create_tag_cloud-71.html
4134
4135 function printTagCloud($link) {
35a03bdd
AD
4136
4137 /* get first ref_id to count from */
4138
dcac082b
AD
4139 /*
4140
35a03bdd
AD
4141 $query = "";
4142
4143 if (DB_TYPE == "pgsql") {
4144 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4145 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4146 AND date_entered > NOW() - INTERVAL '30 days'";
4147 } else {
4148 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4149 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4150 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
4151 }
4152
4153 $result = db_query($link, $query);
dcac082b 4154 $first_id = db_fetch_result($result, 0, "id"); */
35a03bdd 4155
dcac082b 4156 //AND post_int_id >= '$first_id'
0979b696
AD
4157 $query = "SELECT tag_name, COUNT(post_int_id) AS count
4158 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
b31af972 4159 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
0979b696
AD
4160
4161 $result = db_query($link, $query);
4162
4163 $tags = array();
4164
4165 while ($line = db_fetch_assoc($result)) {
4166 $tags[$line["tag_name"]] = $line["count"];
4167 }
4168
4169 ksort($tags);
4170
4171 $max_size = 32; // max font size in pixels
4548a580 4172 $min_size = 11; // min font size in pixels
0979b696
AD
4173
4174 // largest and smallest array values
4175 $max_qty = max(array_values($tags));
4176 $min_qty = min(array_values($tags));
4177
4178 // find the range of values
4179 $spread = $max_qty - $min_qty;
4180 if ($spread == 0) { // we don't want to divide by zero
4181 $spread = 1;
4182 }
4183
4184 // set the font-size increment
4185 $step = ($max_size - $min_size) / ($spread);
4186
4187 // loop through the tag array
4188 foreach ($tags as $key => $value) {
4189 // calculate font-size
4190 // find the $value in excess of $min_qty
4191 // multiply by the font-size increment ($size)
4192 // and add the $min_size set above
4193 $size = round($min_size + (($value - $min_qty) * $step));
546ffab4
AD
4194
4195 $key_escaped = str_replace("'", "\\'", $key);
4196
4197 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
0979b696
AD
4198 $size . "px\" title=\"$value articles tagged with " .
4199 $key . '">' . $key . '</a> ';
4200 }
4201 }
46921916
AD
4202
4203 function print_checkpoint($n, $s) {
4204 $ts = getmicrotime();
4205 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
4206 return $ts;
4207 }
14b6c54b
AD
4208
4209 function sanitize_tag($tag) {
4210 $tag = trim($tag);
4211
4212 $tag = mb_strtolower($tag, 'utf-8');
4213
0c3d1c68
AD
4214 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
4215
4216// $tag = str_replace('"', "", $tag);
4217// $tag = str_replace("+", " ", $tag);
14b6c54b
AD
4218 $tag = str_replace("technorati tag: ", "", $tag);
4219
4220 return $tag;
4221 }
e4f4b46f
AD
4222
4223 function generate_publish_key() {
4224 return sha1(uniqid(rand(), true));
4225 }
4226
f56e3080
AD
4227 function article_publish_url($link) {
4228
17a756d1 4229 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
f56e3080
AD
4230
4231 $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
4232
4233 return $url_path;
4234 }
4235
40d13c28 4236?>