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