]> git.wh0rd.org - tt-rss.git/blob - functions.php
purge_feed: stop when purging is disabled for the feed
[tt-rss.git] / functions.php
1 <?php
2
3 /* if ($_GET["debug"]) {
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
5 } else {
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
7 } */
8
9 require_once 'config.php';
10
11 if (DB_TYPE == "pgsql") {
12 define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
13 } else {
14 define('SUBSTRING_FOR_DATE', 'SUBSTRING');
15 }
16
17 /**
18 * Return available translations names.
19 *
20 * @access public
21 * @return array A array of available translations.
22 */
23 function get_translations() {
24 $tr = array(
25 "auto" => "Detect automatically",
26 "en_US" => "English",
27 "fr_FR" => "Français",
28 "hu_HU" => "Magyar (Hungarian)",
29 "it_IT" => "Italiano",
30 "ja_JP" => "日本語 (Japanese)",
31 "nb_NO" => "Norwegian bokmål",
32 "ru_RU" => "Русский",
33 "pt_BR" => "Portuguese/Brazil",
34 "zh_CN" => "Simplified Chinese");
35
36 return $tr;
37 }
38
39 if (ENABLE_TRANSLATIONS == true) { // If translations are enabled.
40 require_once "accept-to-gettext.php";
41 require_once "gettext/gettext.inc";
42
43 function startup_gettext() {
44
45 # Get locale from Accept-Language header
46 $lang = al2gt(array_keys(get_translations()), "text/html");
47
48 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
49 $lang = _TRANSLATION_OVERRIDE_DEFAULT;
50 }
51
52 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
53 $lang = $_COOKIE["ttrss_lang"];
54 }
55
56 /* In login action of mobile version */
57 if ($_POST["language"] && defined('MOBILE_VERSION')) {
58 $lang = $_POST["language"];
59 $_COOKIE["ttrss_lang"] = $lang;
60 }
61
62 if ($lang) {
63 if (defined('LC_MESSAGES')) {
64 _setlocale(LC_MESSAGES, $lang);
65 } else if (defined('LC_ALL')) {
66 _setlocale(LC_ALL, $lang);
67 } else {
68 die("can't setlocale(): please set ENABLE_TRANSLATIONS to false in config.php");
69 }
70
71 if (defined('MOBILE_VERSION')) {
72 _bindtextdomain("messages", "../locale");
73 } else {
74 _bindtextdomain("messages", "locale");
75 }
76
77 _textdomain("messages");
78 _bind_textdomain_codeset("messages", "UTF-8");
79 }
80 }
81
82 startup_gettext();
83
84 } else { // If translations are enabled.
85 function __($msg) {
86 return $msg;
87 }
88 function startup_gettext() {
89 // no-op
90 return true;
91 }
92 } // If translations are enabled.
93
94 require_once 'db-prefs.php';
95 require_once 'compat.php';
96 require_once 'errors.php';
97 require_once 'version.php';
98
99 require_once 'phpmailer/class.phpmailer.php';
100
101 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
102 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
103 define('MAGPIE_CACHE_AGE', 60*15); // 15 minutes
104
105 require_once "simplepie/simplepie.inc";
106 require_once "magpierss/rss_fetch.inc";
107 require_once 'magpierss/rss_utils.inc';
108
109 /**
110 * Print a timestamped debug message.
111 *
112 * @param string $msg The debug message.
113 * @return void
114 */
115 function _debug($msg) {
116 $ts = strftime("%H:%M:%S", time());
117 if (function_exists('posix_getpid')) {
118 $ts = "$ts/" . posix_getpid();
119 }
120 print "[$ts] $msg\n";
121 } // function _debug
122
123 /**
124 * Purge a feed old posts.
125 *
126 * @param mixed $link A database connection.
127 * @param mixed $feed_id The id of the purged feed.
128 * @param mixed $purge_interval Olderness of purged posts.
129 * @param boolean $debug Set to True to enable the debug. False by default.
130 * @access public
131 * @return void
132 */
133 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
134
135 if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
136 if ($purge_interval == -1 || !$purge_interval) return;
137
138 $rows = -1;
139
140 $result = db_query($link,
141 "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
142
143 $owner_uid = false;
144
145 if (db_num_rows($result) == 1) {
146 $owner_uid = db_fetch_result($result, 0, "owner_uid");
147 }
148
149 if (!$owner_uid) return;
150
151 if (FORCE_ARTICLE_PURGE == 0) {
152 $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
153 $owner_uid, false);
154 } else {
155 $purge_unread = true;
156 $purge_interval = FORCE_ARTICLE_PURGE;
157 }
158
159 if (!$purge_unread) $query_limit = " unread = false AND ";
160
161 if (DB_TYPE == "pgsql") {
162 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
163 marked = false AND feed_id = '$feed_id' AND
164 (SELECT date_entered FROM ttrss_entries WHERE
165 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
166
167 $pg_version = get_pgsql_version($link);
168
169 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
170
171 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
172 ttrss_entries.id = ref_id AND
173 marked = false AND
174 feed_id = '$feed_id' AND
175 $query_limit
176 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
177
178 } else {
179
180 $result = db_query($link, "DELETE FROM ttrss_user_entries
181 USING ttrss_entries
182 WHERE ttrss_entries.id = ref_id AND
183 marked = false AND
184 feed_id = '$feed_id' AND
185 $query_limit
186 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
187 }
188
189 $rows = pg_affected_rows($result);
190
191 } else {
192
193 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
194 marked = false AND feed_id = '$feed_id' AND
195 (SELECT date_entered FROM ttrss_entries WHERE
196 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
197
198 $result = db_query($link, "DELETE FROM ttrss_user_entries
199 USING ttrss_user_entries, ttrss_entries
200 WHERE ttrss_entries.id = ref_id AND
201 marked = false AND
202 feed_id = '$feed_id' AND
203 $query_limit
204 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
205
206 $rows = mysql_affected_rows($link);
207
208 }
209
210 ccache_update($link, $feed_id, $owner_uid);
211
212 if ($debug) {
213 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
214 }
215 } // function purge_feed
216
217 /**
218 * Purge old posts from old feeds.
219 *
220 * @param mixed $link A database connection
221 * @param boolean $do_output Set to true to enable printed output, false by default.
222 * @param integer $limit The maximal number of removed posts.
223 * @access public
224 * @return void
225 */
226 function global_purge_old_posts($link, $do_output = false, $limit = false) {
227
228 $random_qpart = sql_random_function();
229
230 if ($limit) {
231 $limit_qpart = "LIMIT $limit";
232 } else {
233 $limit_qpart = "";
234 }
235
236 $result = db_query($link,
237 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
238 ORDER BY $random_qpart $limit_qpart");
239
240 while ($line = db_fetch_assoc($result)) {
241
242 $feed_id = $line["id"];
243 $purge_interval = $line["purge_interval"];
244 $owner_uid = $line["owner_uid"];
245
246 if ($purge_interval == 0) {
247
248 $tmp_result = db_query($link,
249 "SELECT value FROM ttrss_user_prefs WHERE
250 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
251
252 if (db_num_rows($tmp_result) != 0) {
253 $purge_interval = db_fetch_result($tmp_result, 0, "value");
254 }
255 }
256
257 if ($do_output) {
258 // print "Feed $feed_id: purge interval = $purge_interval\n";
259 }
260
261 if ($purge_interval > 0 || FORCE_ARTICLE_PURGE) {
262 purge_feed($link, $feed_id, $purge_interval, $do_output);
263 }
264 }
265
266 // purge orphaned posts in main content table
267 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
268 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
269
270 if ($do_output) {
271 $rows = db_affected_rows($link, $result);
272 _debug("Purged $rows orphaned posts.");
273 }
274
275 } // function global_purge_old_posts
276
277 function feed_purge_interval($link, $feed_id) {
278
279 $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds
280 WHERE id = '$feed_id'");
281
282 if (db_num_rows($result) == 1) {
283 $purge_interval = db_fetch_result($result, 0, "purge_interval");
284 $owner_uid = db_fetch_result($result, 0, "owner_uid");
285
286 if ($purge_interval == 0) $purge_interval = get_pref($link,
287 'PURGE_OLD_DAYS', $owner_uid);
288
289 return $purge_interval;
290
291 } else {
292 return -1;
293 }
294 }
295
296 function purge_old_posts($link) {
297
298 $user_id = $_SESSION["uid"];
299
300 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
301 WHERE owner_uid = '$user_id'");
302
303 while ($line = db_fetch_assoc($result)) {
304
305 $feed_id = $line["id"];
306 $purge_interval = $line["purge_interval"];
307
308 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
309
310 if ($purge_interval > 0) {
311 purge_feed($link, $feed_id, $purge_interval);
312 }
313 }
314
315 purge_orphans($link);
316 }
317
318 function purge_orphans($link) {
319 // purge orphaned posts in main content table
320 db_query($link, "DELETE FROM ttrss_entries WHERE
321 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
322 }
323
324 function get_feed_update_interval($link, $feed_id) {
325 $result = db_query($link, "SELECT owner_uid, update_interval FROM
326 ttrss_feeds WHERE id = '$feed_id'");
327
328 if (db_num_rows($result) == 1) {
329 $update_interval = db_fetch_result($result, 0, "update_interval");
330 $owner_uid = db_fetch_result($result, 0, "owner_uid");
331
332 if ($update_interval != 0) {
333 return $update_interval;
334 } else {
335 return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
336 }
337
338 } else {
339 return -1;
340 }
341 }
342
343 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
344
345 if (WEB_DEMO_MODE) return;
346
347 if (!$user_id) {
348 $user_id = $_SESSION["uid"];
349 purge_old_posts($link);
350 }
351
352 // db_query($link, "BEGIN");
353
354 if (MAX_UPDATE_TIME > 0) {
355 if (DB_TYPE == "mysql") {
356 $q_order = "RAND()";
357 } else {
358 $q_order = "RANDOM()";
359 }
360 } else {
361 $q_order = "last_updated DESC";
362 }
363
364 $result = db_query($link, "SELECT feed_url,id,
365 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated,
366 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
367 ORDER BY $q_order");
368
369 $upd_start = time();
370
371 while ($line = db_fetch_assoc($result)) {
372 $upd_intl = $line["update_interval"];
373
374 if (!$upd_intl || $upd_intl == 0) {
375 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
376 }
377
378 if ($upd_intl < 0) {
379 // Updates for this feed are disabled
380 continue;
381 }
382
383 if ($fetch || (!$line["last_updated"] ||
384 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
385
386 // print "<!-- feed: ".$line["feed_url"]." -->";
387
388 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
389
390 $upd_elapsed = time() - $upd_start;
391
392 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
393 return;
394 }
395 }
396 }
397
398 // db_query($link, "COMMIT");
399
400 }
401
402 function fetch_file_contents($url) {
403 if (USE_CURL_FOR_ICONS) {
404 $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
405
406 $ch = curl_init($url);
407 $fp = fopen($tmpfile, "w");
408
409 if ($fp) {
410 curl_setopt($ch, CURLOPT_FILE, $fp);
411 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
412 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
413 curl_exec($ch);
414 curl_close($ch);
415 fclose($fp);
416 }
417
418 $contents = file_get_contents($tmpfile);
419 unlink($tmpfile);
420
421 return $contents;
422
423 } else {
424 return file_get_contents($url);
425 }
426
427 }
428
429 /**
430 * Try to determine the favicon URL for a feed.
431 * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
432 * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
433 *
434 * @param string $url A feed or page URL
435 * @access public
436 * @return mixed The favicon URL, or false if none was found.
437 */
438 function get_favicon_url($url) {
439
440 if ($html = @fetch_file_contents($url)) {
441
442 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
443 // Attempt to grab a favicon link from their webpage url
444 $linkUrl = html_entity_decode($matches[1]);
445
446 if (substr($linkUrl, 0, 1) == '/') {
447 $urlParts = parse_url($url);
448 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
449 } else if (substr($linkUrl, 0, 7) == 'http://') {
450 $faviconURL = $linkUrl;
451 } else if (substr($url, -1, 1) == '/') {
452 $faviconURL = $url.$linkUrl;
453 } else {
454 $faviconURL = $url.'/'.$linkUrl;
455 }
456
457 } else {
458 // If unsuccessful, attempt to "guess" the favicon location
459 $urlParts = parse_url($url);
460 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
461 }
462 }
463
464 // Run a test to see if what we have attempted to get actually exists.
465 if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) {
466 return $faviconURL;
467 } else {
468 return false;
469 }
470 } // function get_favicon_url
471
472 /**
473 * Check if a link is a valid and working URL.
474 *
475 * @param mixed $link A URL to check
476 * @access public
477 * @return boolean True if the URL is valid, false otherwise.
478 */
479 function url_validate($link) {
480
481 $url_parts = @parse_url($link);
482
483 if ( empty( $url_parts["host"] ) )
484 return false;
485
486 if ( !empty( $url_parts["path"] ) ) {
487 $documentpath = $url_parts["path"];
488 } else {
489 $documentpath = "/";
490 }
491
492 if ( !empty( $url_parts["query"] ) )
493 $documentpath .= "?" . $url_parts["query"];
494
495 $host = $url_parts["host"];
496 $port = $url_parts["port"];
497
498 if ( empty($port) )
499 $port = "80";
500
501 $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
502
503 if ( !$socket )
504 return false;
505
506 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
507
508 $http_response = fgets( $socket, 22 );
509
510 $responses = "/(200 OK)|(30[0-9] Moved)/";
511 if ( preg_match($responses, $http_response) ) {
512 fclose($socket);
513 return true;
514 } else {
515 return false;
516 }
517
518 } // function url_validate
519
520 function check_feed_favicon($site_url, $feed, $link) {
521 $favicon_url = get_favicon_url($site_url);
522
523 # print "FAVICON [$site_url]: $favicon_url\n";
524
525 error_reporting(0);
526
527 $icon_file = ICONS_DIR . "/$feed.ico";
528
529 if ($favicon_url && !file_exists($icon_file)) {
530 $contents = fetch_file_contents($favicon_url);
531
532 $fp = fopen($icon_file, "w");
533
534 if ($fp) {
535 fwrite($fp, $contents);
536 fclose($fp);
537 chmod($icon_file, 0644);
538 }
539 }
540
541 error_reporting(DEFAULT_ERROR_LEVEL);
542
543 }
544
545 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
546
547 if (!$_GET["daemon"] && !$ignore_daemon) {
548 return false;
549 }
550
551 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
552 _debug("update_rss_feed: start");
553 }
554
555 if (!$ignore_daemon) {
556
557 if (DB_TYPE == "pgsql") {
558 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
559 } else {
560 $updstart_thresh_qpart = "(ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
561 }
562
563 $result = db_query($link, "SELECT id,update_interval,auth_login,
564 auth_pass,cache_images,update_method
565 FROM ttrss_feeds WHERE id = '$feed' AND $updstart_thresh_qpart");
566
567 } else {
568
569 $result = db_query($link, "SELECT id,update_interval,auth_login,
570 auth_pass,cache_images,update_method,hidden
571 FROM ttrss_feeds WHERE id = '$feed'");
572
573 }
574
575 if (db_num_rows($result) == 0) {
576 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
577 _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND/SKIPPED");
578 }
579 return false;
580 }
581
582 $hidden = sql_bool_to_bool(db_fetch_result($result, 0, "hidden"));
583 $update_method = db_fetch_result($result, 0, "update_method");
584
585 db_query($link, "UPDATE ttrss_feeds SET last_update_started = NOW()
586 WHERE id = '$feed'");
587
588 $auth_login = db_fetch_result($result, 0, "auth_login");
589 $auth_pass = db_fetch_result($result, 0, "auth_pass");
590
591 if (ALLOW_SELECT_UPDATE_METHOD) {
592 if (ENABLE_SIMPLEPIE) {
593 $use_simplepie = $update_method != 1;
594 } else {
595 $use_simplepie = $update_method == 2;
596 }
597 } else {
598 $use_simplepie = ENABLE_SIMPLEPIE;
599 }
600
601 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
602 _debug("use simplepie: $use_simplepie (feed setting: $update_method)\n");
603 }
604
605 if (!$use_simplepie) {
606 $auth_login = urlencode($auth_login);
607 $auth_pass = urlencode($auth_pass);
608 }
609
610 $update_interval = db_fetch_result($result, 0, "update_interval");
611 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
612
613 if ($update_interval < 0) { return; }
614
615 $feed = db_escape_string($feed);
616
617 $fetch_url = $feed_url;
618
619 if ($auth_login && $auth_pass) {
620 $url_parts = array();
621 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
622
623 if ($url_parts[1] && $url_parts[2]) {
624 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
625 }
626
627 }
628
629 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
630 _debug("update_rss_feed: fetching [$fetch_url]...");
631 }
632
633 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
634 error_reporting(0);
635 }
636
637 if (!$use_simplepie) {
638 $rss = fetch_rss($fetch_url);
639 } else {
640 if (!is_dir(SIMPLEPIE_CACHE_DIR)) {
641 mkdir(SIMPLEPIE_CACHE_DIR);
642 }
643
644 $rss = new SimplePie();
645 $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
646 # $rss->set_timeout(10);
647 $rss->set_feed_url($fetch_url);
648 $rss->set_output_encoding('UTF-8');
649
650 if (SIMPLEPIE_CACHE_IMAGES && $cache_images) {
651 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
652 _debug("enabling image cache");
653 }
654
655 $rss->set_image_handler('./image.php', 'i');
656 }
657
658 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
659 _debug("feed update interval (sec): " .
660 get_feed_update_interval($link, $feed)*60);
661 }
662
663 if (is_dir(SIMPLEPIE_CACHE_DIR)) {
664 $rss->set_cache_location(SIMPLEPIE_CACHE_DIR);
665 $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
666 }
667
668 $rss->init();
669 }
670
671 // print_r($rss);
672
673 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
674 _debug("update_rss_feed: fetch done, parsing...");
675 } else {
676 error_reporting (DEFAULT_ERROR_LEVEL);
677 }
678
679 $feed = db_escape_string($feed);
680
681 if ($use_simplepie) {
682 $fetch_ok = !$rss->error();
683 } else {
684 $fetch_ok = !!$rss;
685 }
686
687 if ($fetch_ok) {
688
689 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
690 _debug("update_rss_feed: processing feed data...");
691 }
692
693 // db_query($link, "BEGIN");
694
695 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
696 FROM ttrss_feeds WHERE id = '$feed'");
697
698 $registered_title = db_fetch_result($result, 0, "title");
699 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
700 $orig_site_url = db_fetch_result($result, 0, "site_url");
701
702 $owner_uid = db_fetch_result($result, 0, "owner_uid");
703
704 if ($use_simplepie) {
705 $site_url = $rss->get_link();
706 } else {
707 $site_url = $rss->channel["link"];
708 }
709
710 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
711 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
712 _debug("update_rss_feed: checking favicon...");
713 }
714
715 check_feed_favicon($site_url, $feed, $link);
716 }
717
718 if (!$registered_title || $registered_title == "[Unknown]") {
719
720 if ($use_simplepie) {
721 $feed_title = db_escape_string($rss->get_title());
722 } else {
723 $feed_title = db_escape_string($rss->channel["title"]);
724 }
725
726 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
727 _debug("update_rss_feed: registering title: $feed_title");
728 }
729
730 db_query($link, "UPDATE ttrss_feeds SET
731 title = '$feed_title' WHERE id = '$feed'");
732 }
733
734 // weird, weird Magpie
735 if (!$use_simplepie) {
736 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
737 }
738
739 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
740 db_query($link, "UPDATE ttrss_feeds SET
741 site_url = '$site_url' WHERE id = '$feed'");
742 }
743
744 // print "I: " . $rss->channel["image"]["url"];
745
746 if (!$use_simplepie) {
747 $icon_url = $rss->image["url"];
748 } else {
749 $icon_url = $rss->get_image_url();
750 }
751
752 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
753 $icon_url = db_escape_string($icon_url);
754 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
755 }
756
757 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
758 _debug("update_rss_feed: loading filters...");
759 }
760
761 $filters = load_filters($link, $feed, $owner_uid);
762
763 if ($use_simplepie) {
764 $iterator = $rss->get_items();
765 } else {
766 $iterator = $rss->items;
767 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
768 if (!$iterator || !is_array($iterator)) $iterator = $rss;
769 }
770
771 if (!is_array($iterator)) {
772 /* db_query($link, "UPDATE ttrss_feeds
773 SET last_error = 'Parse error: can\'t find any articles.'
774 WHERE id = '$feed'"); */
775
776 // clear any errors and mark feed as updated if fetched okay
777 // even if it's blank
778
779 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
780 _debug("update_rss_feed: entry iterator is not an array, no articles?");
781 }
782
783 db_query($link, "UPDATE ttrss_feeds
784 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
785
786 return; // no articles
787 }
788
789 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
790 _debug("update_rss_feed: processing articles...");
791 }
792
793 foreach ($iterator as $item) {
794
795 if ($_GET['xdebug']) {
796 print_r($item);
797
798 }
799
800 if ($use_simplepie) {
801 $entry_guid = $item->get_id();
802 if (!$entry_guid) $entry_guid = $item->get_link();
803 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
804
805 } else {
806
807 $entry_guid = $item["id"];
808
809 if (!$entry_guid) $entry_guid = $item["guid"];
810 if (!$entry_guid) $entry_guid = $item["link"];
811 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
812 }
813
814 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
815 _debug("update_rss_feed: guid $entry_guid");
816 }
817
818 if (!$entry_guid) continue;
819
820 $entry_timestamp = "";
821
822 if ($use_simplepie) {
823 $entry_timestamp = strtotime($item->get_date());
824 } else {
825 $rss_2_date = $item['pubdate'];
826 $rss_1_date = $item['dc']['date'];
827 $atom_date = $item['issued'];
828 if (!$atom_date) $atom_date = $item['updated'];
829
830 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
831 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
832 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
833 }
834
835 if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
836 $entry_timestamp = time();
837 $no_orig_date = 'true';
838 } else {
839 $no_orig_date = 'false';
840 }
841
842 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
843
844 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
845 _debug("update_rss_feed: date $entry_timestamp [$entry_timestamp_fmt]");
846 }
847
848 if ($use_simplepie) {
849 $entry_title = $item->get_title();
850 } else {
851 $entry_title = trim(strip_tags($item["title"]));
852 }
853
854 if ($use_simplepie) {
855 $entry_link = $item->get_link();
856 } else {
857 // strange Magpie workaround
858 $entry_link = $item["link_"];
859 if (!$entry_link) $entry_link = $item["link"];
860 }
861
862 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
863 _debug("update_rss_feed: title $entry_title");
864 }
865
866 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
867
868 $entry_link = strip_tags($entry_link);
869
870 if ($use_simplepie) {
871 $entry_content = $item->get_content();
872 if (!$entry_content) $entry_content = $item->get_description();
873 } else {
874 $entry_content = $item["content:escaped"];
875
876 if (!$entry_content) $entry_content = $item["content:encoded"];
877 if (!$entry_content) $entry_content = $item["content"]["encoded"];
878 if (!$entry_content) $entry_content = $item["content"];
879
880 // Magpie bugs are getting ridiculous
881 if (trim($entry_content) == "Array") $entry_content = false;
882
883 if (!$entry_content) $entry_content = $item["atom_content"];
884 if (!$entry_content) $entry_content = $item["summary"];
885
886 if (!$entry_content ||
887 strlen($entry_content) < strlen($item["description"])) {
888 $entry_content = $item["description"];
889 };
890
891 // WTF
892 if (is_array($entry_content)) {
893 $entry_content = $entry_content["encoded"];
894 if (!$entry_content) $entry_content = $entry_content["escaped"];
895 }
896 }
897
898 if ($_GET["xdebug"]) {
899 print "update_rss_feed: content: ";
900 print_r(htmlspecialchars($entry_content));
901 }
902
903 $entry_content_unescaped = $entry_content;
904
905 if ($use_simplepie) {
906 $entry_comments = strip_tags($item->data["comments"]);
907 if ($item->get_author()) {
908 $entry_author_item = $item->get_author();
909 $entry_author = $entry_author_item->get_name();
910 if (!$entry_author) $entry_author = $entry_author_item->get_email();
911
912 $entry_author = db_escape_string($entry_author);
913 }
914 } else {
915 $entry_comments = strip_tags($item["comments"]);
916
917 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
918
919 if ($item['author']) {
920
921 if (is_array($item['author'])) {
922
923 if (!$entry_author) {
924 $entry_author = db_escape_string(strip_tags($item['author']['name']));
925 }
926
927 if (!$entry_author) {
928 $entry_author = db_escape_string(strip_tags($item['author']['email']));
929 }
930 }
931
932 if (!$entry_author) {
933 $entry_author = db_escape_string(strip_tags($item['author']));
934 }
935 }
936 }
937
938 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
939
940 $entry_guid = db_escape_string(strip_tags($entry_guid));
941 $entry_guid = mb_substr($entry_guid, 0, 250);
942
943 $result = db_query($link, "SELECT id FROM ttrss_entries
944 WHERE guid = '$entry_guid'");
945
946 $entry_content = db_escape_string($entry_content);
947
948 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
949
950 $entry_title = db_escape_string($entry_title);
951 $entry_link = db_escape_string($entry_link);
952 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
953 $entry_author = mb_substr($entry_author, 0, 250);
954
955 if ($use_simplepie) {
956 $num_comments = 0; #FIXME#
957 } else {
958 $num_comments = db_escape_string($item["slash"]["comments"]);
959 }
960
961 if (!$num_comments) $num_comments = 0;
962
963 // parse <category> entries into tags
964
965 if ($use_simplepie) {
966
967 $additional_tags = array();
968 $additional_tags_src = $item->get_categories();
969
970 if (is_array($additional_tags_src)) {
971 foreach ($additional_tags_src as $tobj) {
972 array_push($additional_tags, $tobj->get_term());
973 }
974 }
975
976 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
977 _debug("update_rss_feed: category tags:");
978 print_r($additional_tags);
979 }
980
981 } else {
982
983 $t_ctr = $item['category#'];
984
985 $additional_tags = false;
986
987 if ($t_ctr == 0) {
988 $additional_tags = false;
989 } else if ($t_ctr > 0) {
990 $additional_tags = array($item['category']);
991
992 if ($item['category@term']) {
993 array_push($additional_tags, $item['category@term']);
994 }
995
996 for ($i = 0; $i <= $t_ctr; $i++ ) {
997 if ($item["category#$i"]) {
998 array_push($additional_tags, $item["category#$i"]);
999 }
1000
1001 if ($item["category#$i@term"]) {
1002 array_push($additional_tags, $item["category#$i@term"]);
1003 }
1004 }
1005 }
1006
1007 // parse <dc:subject> elements
1008
1009 $t_ctr = $item['dc']['subject#'];
1010
1011 if ($t_ctr > 0) {
1012 $additional_tags = array($item['dc']['subject']);
1013
1014 for ($i = 0; $i <= $t_ctr; $i++ ) {
1015 if ($item['dc']["subject#$i"]) {
1016 array_push($additional_tags, $item['dc']["subject#$i"]);
1017 }
1018 }
1019 }
1020 }
1021
1022 // enclosures
1023
1024 $enclosures = array();
1025
1026 if ($use_simplepie) {
1027 $encs = $item->get_enclosures();
1028
1029 if (is_array($encs)) {
1030 foreach ($encs as $e) {
1031 $e_item = array(
1032 $e->link, $e->type, $e->length);
1033
1034 array_push($enclosures, $e_item);
1035 }
1036 }
1037
1038 } else {
1039 // <enclosure>
1040
1041 $e_ctr = $item['enclosure#'];
1042
1043 if ($e_ctr > 0) {
1044 $e_item = array($item['enclosure@url'],
1045 $item['enclosure@type'],
1046 $item['enclosure@length']);
1047
1048 array_push($enclosures, $e_item);
1049
1050 for ($i = 0; $i <= $e_ctr; $i++ ) {
1051
1052 if ($item["enclosure#$i@url"]) {
1053 $e_item = array($item["enclosure#$i@url"],
1054 $item["enclosure#$i@type"],
1055 $item["enclosure#$i@length"]);
1056 array_push($enclosures, $e_item);
1057 }
1058 }
1059 }
1060
1061 // <media:content>
1062 // can there be many of those? yes -fox
1063
1064 $m_ctr = $item['media']['content#'];
1065
1066 if ($m_ctr > 0) {
1067 $e_item = array($item['media']['content@url'],
1068 $item['media']['content@medium'],
1069 $item['media']['content@length']);
1070
1071 array_push($enclosures, $e_item);
1072
1073 for ($i = 0; $i <= $m_ctr; $i++ ) {
1074
1075 if ($item["media"]["content#$i@url"]) {
1076 $e_item = array($item["media"]["content#$i@url"],
1077 $item["media"]["content#$i@medium"],
1078 $item["media"]["content#$i@length"]);
1079 array_push($enclosures, $e_item);
1080 }
1081 }
1082
1083 }
1084 }
1085
1086 # sanitize content
1087
1088 $entry_content = sanitize_article_content($entry_content);
1089 $entry_title = sanitize_article_content($entry_title);
1090
1091 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1092 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
1093 }
1094
1095 db_query($link, "BEGIN");
1096
1097 if (db_num_rows($result) == 0) {
1098
1099 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1100 _debug("update_rss_feed: base guid not found");
1101 }
1102
1103 // base post entry does not exist, create it
1104
1105 $result = db_query($link,
1106 "INSERT INTO ttrss_entries
1107 (title,
1108 guid,
1109 link,
1110 updated,
1111 content,
1112 content_hash,
1113 no_orig_date,
1114 date_entered,
1115 comments,
1116 num_comments,
1117 author)
1118 VALUES
1119 ('$entry_title',
1120 '$entry_guid',
1121 '$entry_link',
1122 '$entry_timestamp_fmt',
1123 '$entry_content',
1124 '$content_hash',
1125 $no_orig_date,
1126 NOW(),
1127 '$entry_comments',
1128 '$num_comments',
1129 '$entry_author')");
1130 } else {
1131 // we keep encountering the entry in feeds, so we need to
1132 // update date_entered column so that we don't get horrible
1133 // dupes when the entry gets purged and reinserted again e.g.
1134 // in the case of SLOW SLOW OMG SLOW updating feeds
1135
1136 $base_entry_id = db_fetch_result($result, 0, "id");
1137
1138 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
1139 WHERE id = '$base_entry_id'");
1140 }
1141
1142 // now it should exist, if not - bad luck then
1143
1144 $result = db_query($link, "SELECT
1145 id,content_hash,no_orig_date,title,
1146 ".SUBSTRING_FOR_DATE."(date_entered,1,19) as date_entered,
1147 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated,
1148 num_comments
1149 FROM
1150 ttrss_entries
1151 WHERE guid = '$entry_guid'");
1152
1153 $entry_ref_id = 0;
1154 $entry_int_id = 0;
1155
1156 if (db_num_rows($result) == 1) {
1157
1158 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1159 _debug("update_rss_feed: base guid found, checking for user record");
1160 }
1161
1162 // this will be used below in update handler
1163 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
1164 $orig_title = db_fetch_result($result, 0, "title");
1165 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
1166 $orig_date_entered = strtotime(db_fetch_result($result,
1167 0, "date_entered"));
1168
1169 $ref_id = db_fetch_result($result, 0, "id");
1170 $entry_ref_id = $ref_id;
1171
1172 // check for user post link to main table
1173
1174 // do we allow duplicate posts with same GUID in different feeds?
1175 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
1176 $dupcheck_qpart = "AND feed_id = '$feed'";
1177 } else {
1178 $dupcheck_qpart = "";
1179 }
1180
1181 // error_reporting(0);
1182
1183 $article_filters = get_article_filters($filters, $entry_title,
1184 $entry_content, $entry_link, $entry_timestamp);
1185
1186 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1187 _debug("update_rss_feed: article filters: ");
1188 if (count($article_filters) != 0) {
1189 print_r($article_filters);
1190 }
1191 }
1192
1193 if (find_article_filter($article_filters, "filter")) {
1194 db_query($link, "COMMIT"); // close transaction in progress
1195 continue;
1196 }
1197
1198 // error_reporting (DEFAULT_ERROR_LEVEL);
1199
1200 $score = calculate_article_score($article_filters);
1201
1202 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1203 _debug("update_rss_feed: initial score: $score");
1204 }
1205
1206 $result = db_query($link,
1207 "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
1208 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
1209 $dupcheck_qpart");
1210
1211 // okay it doesn't exist - create user entry
1212 if (db_num_rows($result) == 0) {
1213
1214 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1215 _debug("update_rss_feed: user record not found, creating...");
1216 }
1217
1218 if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) {
1219 $unread = 'true';
1220 $last_read_qpart = 'NULL';
1221 } else {
1222 $unread = 'false';
1223 $last_read_qpart = 'NOW()';
1224 }
1225
1226 if (find_article_filter($article_filters, 'mark') || $score > 1000) {
1227 $marked = 'true';
1228 } else {
1229 $marked = 'false';
1230 }
1231
1232 if (find_article_filter($article_filters, 'publish')) {
1233 $published = 'true';
1234 } else {
1235 $published = 'false';
1236 }
1237
1238 $result = db_query($link,
1239 "INSERT INTO ttrss_user_entries
1240 (ref_id, owner_uid, feed_id, unread, last_read, marked,
1241 published, score)
1242 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
1243 $last_read_qpart, $marked, $published, '$score')");
1244
1245 $result = db_query($link,
1246 "SELECT int_id FROM ttrss_user_entries WHERE
1247 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
1248 feed_id = '$feed' LIMIT 1");
1249
1250 if (db_num_rows($result) == 1) {
1251 $entry_int_id = db_fetch_result($result, 0, "int_id");
1252 }
1253 } else {
1254 $entry_ref_id = db_fetch_result($result, 0, "ref_id");
1255 $entry_int_id = db_fetch_result($result, 0, "int_id");
1256 }
1257
1258 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1259 _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
1260 }
1261
1262 $post_needs_update = false;
1263
1264 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
1265 ($content_hash != $orig_content_hash)) {
1266 // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
1267 $post_needs_update = true;
1268 }
1269
1270 if (db_escape_string($orig_title) != $entry_title) {
1271 $post_needs_update = true;
1272 }
1273
1274 if ($orig_num_comments != $num_comments) {
1275 $post_needs_update = true;
1276 }
1277
1278 // this doesn't seem to be very reliable
1279 //
1280 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
1281 // $post_needs_update = true;
1282 // }
1283
1284 // if post needs update, update it and mark all user entries
1285 // linking to this post as updated
1286 if ($post_needs_update) {
1287
1288 if (defined('DAEMON_EXTENDED_DEBUG')) {
1289 _debug("update_rss_feed: post $entry_guid needs update...");
1290 }
1291
1292 // print "<!-- post $orig_title needs update : $post_needs_update -->";
1293
1294 db_query($link, "UPDATE ttrss_entries
1295 SET title = '$entry_title', content = '$entry_content',
1296 content_hash = '$content_hash',
1297 num_comments = '$num_comments'
1298 WHERE id = '$ref_id'");
1299
1300 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
1301 db_query($link, "UPDATE ttrss_user_entries
1302 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
1303 } else {
1304 db_query($link, "UPDATE ttrss_user_entries
1305 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
1306 }
1307
1308 }
1309 }
1310
1311 db_query($link, "COMMIT");
1312
1313 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1314 _debug("update_rss_feed: assigning labels...");
1315 }
1316
1317 assign_article_to_labels($link, $entry_ref_id, $article_filters,
1318 $owner_uid);
1319
1320 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1321 _debug("update_rss_feed: looking for enclosures...");
1322 }
1323
1324 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1325 print_r($enclosures);
1326 }
1327
1328 db_query($link, "BEGIN");
1329
1330 foreach ($enclosures as $enc) {
1331 $enc_url = db_escape_string($enc[0]);
1332 $enc_type = db_escape_string($enc[1]);
1333 $enc_dur = db_escape_string($enc[2]);
1334
1335 $result = db_query($link, "SELECT id FROM ttrss_enclosures
1336 WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
1337
1338 if (db_num_rows($result) == 0) {
1339 db_query($link, "INSERT INTO ttrss_enclosures
1340 (content_url, content_type, title, duration, post_id) VALUES
1341 ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')");
1342 }
1343 }
1344
1345 db_query($link, "COMMIT");
1346
1347 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1348 _debug("update_rss_feed: looking for tags...");
1349 }
1350
1351 /* taaaags */
1352 // <a href="..." rel="tag">Xorg</a>, //
1353
1354 $entry_tags = null;
1355
1356 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
1357 $entry_content_unescaped, $entry_tags);
1358
1359 /* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
1360 print_r($entry_tags);
1361 print "<br/></p>"; */
1362
1363 $entry_tags = $entry_tags[1];
1364
1365 # check for manual tags
1366
1367 $tag_filter = find_article_filter($article_filters, "tag");
1368
1369 if ($tag_filter) {
1370
1371 $manual_tags = trim_array(split(",", $tag_filter[1]));
1372
1373 foreach ($manual_tags as $tag) {
1374 if (tag_is_valid($tag)) {
1375 array_push($entry_tags, $tag);
1376 }
1377 }
1378 }
1379
1380 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1381 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
1382
1383 if ($additional_tags && is_array($additional_tags)) {
1384 foreach ($additional_tags as $tag) {
1385 if (tag_is_valid($tag) &&
1386 array_search($tag, $boring_tags) === FALSE) {
1387 array_push($entry_tags, $tag);
1388 }
1389 }
1390 }
1391
1392 // print "<p>TAGS: "; print_r($entry_tags); print "</p>";
1393
1394 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1395 print_r($entry_tags);
1396 }
1397
1398 if (count($entry_tags) > 0) {
1399
1400 db_query($link, "BEGIN");
1401
1402 foreach ($entry_tags as $tag) {
1403
1404 $tag = sanitize_tag($tag);
1405 $tag = db_escape_string($tag);
1406
1407 if (!tag_is_valid($tag)) continue;
1408
1409 $result = db_query($link, "SELECT id FROM ttrss_tags
1410 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1411 owner_uid = '$owner_uid' LIMIT 1");
1412
1413 // print db_fetch_result($result, 0, "id");
1414
1415 if ($result && db_num_rows($result) == 0) {
1416
1417 db_query($link, "INSERT INTO ttrss_tags
1418 (owner_uid,tag_name,post_int_id)
1419 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1420 }
1421 }
1422
1423 db_query($link, "COMMIT");
1424 }
1425
1426 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1427 _debug("update_rss_feed: article processed");
1428 }
1429 }
1430
1431 if (!$hidden) {
1432 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1433 _debug("update_rss_feed: updating counters cache...");
1434 }
1435
1436 // disabled, purge_feed() does that...
1437 //ccache_update($link, $feed, $owner_uid);
1438 }
1439
1440 purge_feed($link, $feed, 0);
1441
1442 db_query($link, "UPDATE ttrss_feeds
1443 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
1444
1445 // db_query($link, "COMMIT");
1446
1447 } else {
1448
1449 if ($use_simplepie) {
1450 $error_msg = mb_substr($rss->error(), 0, 250);
1451 } else {
1452 $error_msg = mb_substr(magpie_error(), 0, 250);
1453 }
1454
1455 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1456 _debug("update_rss_feed: error fetching feed: $error_msg");
1457 }
1458
1459 $error_msg = db_escape_string($error_msg);
1460
1461 db_query($link,
1462 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1463 last_updated = NOW() WHERE id = '$feed'");
1464 }
1465
1466 if ($use_simplepie) {
1467 unset($rss);
1468 }
1469
1470 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1471 _debug("update_rss_feed: done");
1472 }
1473
1474 }
1475
1476 function print_select($id, $default, $values, $attributes = "") {
1477 print "<select name=\"$id\" id=\"$id\" $attributes>";
1478 foreach ($values as $v) {
1479 if ($v == $default)
1480 $sel = " selected";
1481 else
1482 $sel = "";
1483
1484 print "<option$sel>$v</option>";
1485 }
1486 print "</select>";
1487 }
1488
1489 function print_select_hash($id, $default, $values, $attributes = "") {
1490 print "<select name=\"$id\" id='$id' $attributes>";
1491 foreach (array_keys($values) as $v) {
1492 if ($v == $default)
1493 $sel = 'selected="selected"';
1494 else
1495 $sel = "";
1496
1497 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1498 }
1499
1500 print "</select>";
1501 }
1502
1503 function get_article_filters($filters, $title, $content, $link, $timestamp) {
1504 $matches = array();
1505
1506 if ($filters["title"]) {
1507 foreach ($filters["title"] as $filter) {
1508 $reg_exp = $filter["reg_exp"];
1509 $inverse = $filter["inverse"];
1510 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1511 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1512
1513 array_push($matches, array($filter["action"], $filter["action_param"]));
1514 }
1515 }
1516 }
1517
1518 if ($filters["content"]) {
1519 foreach ($filters["content"] as $filter) {
1520 $reg_exp = $filter["reg_exp"];
1521 $inverse = $filter["inverse"];
1522
1523 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1524 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1525
1526 array_push($matches, array($filter["action"], $filter["action_param"]));
1527 }
1528 }
1529 }
1530
1531 if ($filters["both"]) {
1532 foreach ($filters["both"] as $filter) {
1533 $reg_exp = $filter["reg_exp"];
1534 $inverse = $filter["inverse"];
1535
1536 if ($inverse) {
1537 if (!preg_match("/$reg_exp/i", $title) && !preg_match("/$reg_exp/i", $content)) {
1538 array_push($matches, array($filter["action"], $filter["action_param"]));
1539 }
1540 } else {
1541 if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
1542 array_push($matches, array($filter["action"], $filter["action_param"]));
1543 }
1544 }
1545 }
1546 }
1547
1548 if ($filters["link"]) {
1549 $reg_exp = $filter["reg_exp"];
1550 foreach ($filters["link"] as $filter) {
1551 $reg_exp = $filter["reg_exp"];
1552 $inverse = $filter["inverse"];
1553
1554 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1555 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1556
1557 array_push($matches, array($filter["action"], $filter["action_param"]));
1558 }
1559 }
1560 }
1561
1562 if ($filters["date"]) {
1563 $reg_exp = $filter["reg_exp"];
1564 foreach ($filters["date"] as $filter) {
1565 $date_modifier = $filter["filter_param"];
1566 $inverse = $filter["inverse"];
1567 $check_timestamp = strtotime($filter["reg_exp"]);
1568
1569 # no-op when timestamp doesn't parse to prevent misfires
1570
1571 if ($check_timestamp) {
1572 $match_ok = false;
1573
1574 if ($date_modifier == "before" && $timestamp < $check_timestamp ||
1575 $date_modifier == "after" && $timestamp > $check_timestamp) {
1576 $match_ok = true;
1577 }
1578
1579 if ($inverse) $match_ok = !$match_ok;
1580
1581 if ($match_ok) {
1582 array_push($matches, array($filter["action"], $filter["action_param"]));
1583 }
1584 }
1585 }
1586 }
1587
1588 return $matches;
1589 }
1590
1591 function find_article_filter($filters, $filter_name) {
1592 foreach ($filters as $f) {
1593 if ($f[0] == $filter_name) {
1594 return $f;
1595 };
1596 }
1597 return false;
1598 }
1599
1600 function calculate_article_score($filters) {
1601 $score = 0;
1602
1603 foreach ($filters as $f) {
1604 if ($f[0] == "score") {
1605 $score += $f[1];
1606 };
1607 }
1608 return $score;
1609 }
1610
1611 function assign_article_to_labels($link, $id, $filters, $owner_uid) {
1612 foreach ($filters as $f) {
1613 if ($f[0] == "label") {
1614 label_add_article($link, $id, $f[1], $owner_uid);
1615 };
1616 }
1617 }
1618
1619 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1620 $rtl_content = false, $last_updated = false, $last_error = false) {
1621
1622 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1623 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1624 } else {
1625 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1626 }
1627
1628 if ($rtl_content) {
1629 $rtl_tag = "dir=\"rtl\"";
1630 } else {
1631 $rtl_tag = "dir=\"ltr\"";
1632 }
1633
1634 $error_notify_msg = "";
1635
1636 if ($last_error) {
1637 $link_title = "Error: $last_error ($last_updated)";
1638 $error_notify_msg = "(Error)";
1639 } else if ($last_updated) {
1640 $link_title = "Updated: $last_updated";
1641 }
1642
1643 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1644 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1645
1646 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1647 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1648 print "$feed_icon";
1649 }
1650
1651 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1652
1653 if ($unread != 0) {
1654 $fctr_class = "class=\"feedCtrHasUnread\"";
1655 } else {
1656 $fctr_class = "class=\"feedCtrNoUnread\"";
1657 }
1658
1659 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1660 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1661
1662 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1663 $total = getFeedArticles($link, $feed_id);
1664 print "<div class=\"feedExtInfo\">
1665 <span id=\"FLUPD-$feed_id\">$last_updated ($total total) $error_notify_msg</span></div>";
1666 }
1667
1668 print "</li>";
1669
1670 }
1671
1672 function getmicrotime() {
1673 list($usec, $sec) = explode(" ",microtime());
1674 return ((float)$usec + (float)$sec);
1675 }
1676
1677 function print_radio($id, $default, $true_is, $values, $attributes = "") {
1678 foreach ($values as $v) {
1679
1680 if ($v == $default)
1681 $sel = "checked";
1682 else
1683 $sel = "";
1684
1685 if ($v == $true_is) {
1686 $sel .= " value=\"1\"";
1687 } else {
1688 $sel .= " value=\"0\"";
1689 }
1690
1691 print "<input class=\"noborder\"
1692 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
1693
1694 }
1695 }
1696
1697 function initialize_user_prefs($link, $uid) {
1698
1699 $uid = db_escape_string($uid);
1700
1701 db_query($link, "BEGIN");
1702
1703 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1704
1705 $u_result = db_query($link, "SELECT pref_name
1706 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1707
1708 $active_prefs = array();
1709
1710 while ($line = db_fetch_assoc($u_result)) {
1711 array_push($active_prefs, $line["pref_name"]);
1712 }
1713
1714 while ($line = db_fetch_assoc($result)) {
1715 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1716 // print "adding " . $line["pref_name"] . "<br>";
1717
1718 db_query($link, "INSERT INTO ttrss_user_prefs
1719 (owner_uid,pref_name,value) VALUES
1720 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1721
1722 }
1723 }
1724
1725 db_query($link, "COMMIT");
1726
1727 }
1728
1729 function lookup_user_id($link, $user) {
1730
1731 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1732 login = '$login'");
1733
1734 if (db_num_rows($result) == 1) {
1735 return db_fetch_result($result, 0, "id");
1736 } else {
1737 return false;
1738 }
1739 }
1740
1741 function http_authenticate_user($link) {
1742
1743 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1744
1745 if (!$_SERVER["PHP_AUTH_USER"]) {
1746
1747 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1748 header('HTTP/1.0 401 Unauthorized');
1749 exit;
1750
1751 } else {
1752 $auth_result = authenticate_user($link,
1753 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1754
1755 if (!$auth_result) {
1756 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1757 header('HTTP/1.0 401 Unauthorized');
1758 exit;
1759 }
1760 }
1761
1762 return true;
1763 }
1764
1765 function authenticate_user($link, $login, $password, $force_auth = false) {
1766
1767 if (!SINGLE_USER_MODE) {
1768
1769 $pwd_hash1 = encrypt_password($password);
1770 $pwd_hash2 = encrypt_password($password, $login);
1771
1772 if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
1773 && $_SERVER["REMOTE_USER"] && $login != "admin") {
1774
1775 $login = db_escape_string($_SERVER["REMOTE_USER"]);
1776
1777 $query = "SELECT id,login,access_level,pwd_hash
1778 FROM ttrss_users WHERE
1779 login = '$login'";
1780
1781 } else {
1782 $query = "SELECT id,login,access_level,pwd_hash
1783 FROM ttrss_users WHERE
1784 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
1785 pwd_hash = '$pwd_hash2')";
1786 }
1787
1788 $result = db_query($link, $query);
1789
1790 if (db_num_rows($result) == 1) {
1791 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1792 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1793 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1794
1795 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1796 $_SESSION["uid"]);
1797
1798 $user_theme = get_user_theme_path($link);
1799
1800 $_SESSION["theme"] = $user_theme;
1801 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1802 $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
1803
1804 initialize_user_prefs($link, $_SESSION["uid"]);
1805
1806 return true;
1807 }
1808
1809 return false;
1810
1811 } else {
1812
1813 $_SESSION["uid"] = 1;
1814 $_SESSION["name"] = "admin";
1815
1816 $user_theme = get_user_theme_path($link);
1817
1818 $_SESSION["theme"] = $user_theme;
1819 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1820
1821 initialize_user_prefs($link, $_SESSION["uid"]);
1822
1823 return true;
1824 }
1825 }
1826
1827 function make_password($length = 8) {
1828
1829 $password = "";
1830 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1831
1832 $i = 0;
1833
1834 while ($i < $length) {
1835 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1836
1837 if (!strstr($password, $char)) {
1838 $password .= $char;
1839 $i++;
1840 }
1841 }
1842 return $password;
1843 }
1844
1845 // this is called after user is created to initialize default feeds, labels
1846 // or whatever else
1847
1848 // user preferences are checked on every login, not here
1849
1850 function initialize_user($link, $uid) {
1851
1852 /* db_query($link, "INSERT INTO ttrss_labels2 (owner_uid, caption)
1853 VALUES ('$uid', 'All Articles')");
1854
1855 db_query($link, "INSERT INTO ttrss_filters
1856 (owner_uid, feed_id, filter_type, reg_exp, enabled,
1857 action_id, action_param, filter_param)
1858 VALUES ('$uid', NULL, 1, '.', true, 7, 'All Articles', 'before')"); */
1859
1860 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1861 values ('$uid', 'Tiny Tiny RSS: New Releases',
1862 'http://tt-rss.org/releases.rss')");
1863
1864 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1865 values ('$uid', 'Tiny Tiny RSS: Forum',
1866 'http://tt-rss.org/forum/rss.php')");
1867 }
1868
1869 function logout_user() {
1870 session_destroy();
1871 if (isset($_COOKIE[session_name()])) {
1872 setcookie(session_name(), '', time()-42000, '/');
1873 }
1874 }
1875
1876 function get_script_urlpath() {
1877 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1878 }
1879
1880 function validate_session($link) {
1881 if (SINGLE_USER_MODE) {
1882 return true;
1883 }
1884
1885 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
1886 if ($_SESSION["ip_address"]) {
1887 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1888 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1889 return false;
1890 }
1891 }
1892 }
1893
1894 if ($_SESSION["uid"]) {
1895
1896 $result = db_query($link,
1897 "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
1898
1899 $pwd_hash = db_fetch_result($result, 0, "pwd_hash");
1900
1901 if ($pwd_hash != $_SESSION["pwd_hash"]) {
1902 return false;
1903 }
1904 }
1905
1906 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1907
1908 //print_r($_SESSION);
1909
1910 if (time() > $_SESSION["cookie_lifetime"]) {
1911 return false;
1912 }
1913 } */
1914
1915 return true;
1916 }
1917
1918 function login_sequence($link, $mobile = false) {
1919 if (!SINGLE_USER_MODE) {
1920
1921 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1922 $swu = db_escape_string($_REQUEST["swu"]);
1923 if ($swu) {
1924 $_SESSION["prefs_cache"] = false;
1925 return authenticate_user($link, $swu, null, true);
1926 }
1927 }
1928
1929 $login_action = $_POST["login_action"];
1930
1931 # try to authenticate user if called from login form
1932 if ($login_action == "do_login") {
1933 $login = $_POST["login"];
1934 $password = $_POST["password"];
1935 $remember_me = $_POST["remember_me"];
1936
1937 if (authenticate_user($link, $login, $password)) {
1938 $_POST["password"] = "";
1939
1940 $_SESSION["language"] = $_POST["language"];
1941 $_SESSION["bw_limit"] = !!$_POST["bw_limit"];
1942
1943 header("Location: " . $_SERVER["REQUEST_URI"]);
1944 exit;
1945
1946 return;
1947 } else {
1948 $_SESSION["login_error_msg"] = "Incorrect username or password";
1949 }
1950 }
1951
1952 if (!$_SESSION["uid"] || !validate_session($link)) {
1953 render_login_form($link, $mobile);
1954 exit;
1955 } else {
1956 /* bump login timestamp */
1957 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1958 $_SESSION["uid"]);
1959
1960 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
1961 setcookie("ttrss_lang", $_SESSION["language"],
1962 time() + SESSION_COOKIE_LIFETIME);
1963 }
1964
1965 /* bump counters stamp since we're getting reloaded anyway */
1966
1967 $_SESSION["get_all_counters_stamp"] = time();
1968 }
1969
1970 } else {
1971 return authenticate_user($link, "admin", null);
1972 }
1973 }
1974
1975 function truncate_string($str, $max_len) {
1976 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1977 return mb_substr($str, 0, $max_len, "utf-8") . "&hellip;";
1978 } else {
1979 return $str;
1980 }
1981 }
1982
1983 function get_user_theme_path($link) {
1984 $result = db_query($link, "SELECT theme_path
1985 FROM
1986 ttrss_themes,ttrss_users
1987 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1988 if (db_num_rows($result) != 0) {
1989 return db_fetch_result($result, 0, "theme_path");
1990 } else {
1991 return null;
1992 }
1993 }
1994
1995 function smart_date_time($timestamp) {
1996 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1997 return date("G:i", $timestamp);
1998 } else if (date("Y", $timestamp) == date("Y")) {
1999 return date("M d, G:i", $timestamp);
2000 } else {
2001 return date("Y/m/d, G:i", $timestamp);
2002 }
2003 }
2004
2005 function smart_date($timestamp) {
2006 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
2007 return "Today";
2008 } else if (date("Y", $timestamp) == date("Y")) {
2009 return date("D m", $timestamp);
2010 } else {
2011 return date("Y/m/d", $timestamp);
2012 }
2013 }
2014
2015 function sql_bool_to_string($s) {
2016 if ($s == "t" || $s == "1") {
2017 return "true";
2018 } else {
2019 return "false";
2020 }
2021 }
2022
2023 function sql_bool_to_bool($s) {
2024 if ($s == "t" || $s == "1") {
2025 return true;
2026 } else {
2027 return false;
2028 }
2029 }
2030
2031
2032 function toggleEvenOdd($a) {
2033 if ($a == "even")
2034 return "odd";
2035 else
2036 return "even";
2037 }
2038
2039 function sanity_check($link) {
2040
2041 error_reporting(0);
2042
2043 $error_code = 0;
2044 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
2045 $schema_version = db_fetch_result($result, 0, "schema_version");
2046
2047 if ($schema_version != SCHEMA_VERSION) {
2048 $error_code = 5;
2049 }
2050
2051 if (DB_TYPE == "mysql") {
2052 $result = db_query($link, "SELECT true", false);
2053 if (db_num_rows($result) != 1) {
2054 $error_code = 10;
2055 }
2056 }
2057
2058 if (db_escape_string("testTEST") != "testTEST") {
2059 $error_code = 12;
2060 }
2061
2062 error_reporting (DEFAULT_ERROR_LEVEL);
2063
2064 if ($error_code != 0) {
2065 print_error_xml($error_code);
2066 return false;
2067 } else {
2068 return true;
2069 }
2070 }
2071
2072 function file_is_locked($filename) {
2073 if (function_exists('flock')) {
2074 error_reporting(0);
2075 $fp = fopen(LOCK_DIRECTORY . "/$filename", "r");
2076 error_reporting(DEFAULT_ERROR_LEVEL);
2077 if ($fp) {
2078 if (flock($fp, LOCK_EX | LOCK_NB)) {
2079 flock($fp, LOCK_UN);
2080 fclose($fp);
2081 return false;
2082 }
2083 fclose($fp);
2084 return true;
2085 } else {
2086 return false;
2087 }
2088 }
2089 return true; // consider the file always locked and skip the test
2090 }
2091
2092 function make_lockfile($filename) {
2093 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
2094
2095 if (flock($fp, LOCK_EX | LOCK_NB)) {
2096 return $fp;
2097 } else {
2098 return false;
2099 }
2100 }
2101
2102 function make_stampfile($filename) {
2103 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
2104
2105 if (flock($fp, LOCK_EX | LOCK_NB)) {
2106 fwrite($fp, time() . "\n");
2107 flock($fp, LOCK_UN);
2108 fclose($fp);
2109 return true;
2110 } else {
2111 return false;
2112 }
2113 }
2114
2115 function read_stampfile($filename) {
2116
2117 error_reporting(0);
2118 $fp = fopen(LOCK_DIRECTORY . "/$filename", "r");
2119 error_reporting (DEFAULT_ERROR_LEVEL);
2120
2121 if ($fp) {
2122 if (flock($fp, LOCK_EX)) {
2123 $stamp = fgets($fp);
2124 flock($fp, LOCK_UN);
2125 fclose($fp);
2126 return $stamp;
2127 } else {
2128 return false;
2129 }
2130 } else {
2131 return false;
2132 }
2133 }
2134
2135 function sql_random_function() {
2136 if (DB_TYPE == "mysql") {
2137 return "RAND()";
2138 } else {
2139 return "RANDOM()";
2140 }
2141 }
2142
2143 function catchup_feed($link, $feed, $cat_view) {
2144
2145 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2146
2147 if ($cat_view) {
2148
2149 if ($feed > 0) {
2150 $cat_qpart = "cat_id = '$feed'";
2151 } else {
2152 $cat_qpart = "cat_id IS NULL";
2153 }
2154
2155 $tmp_result = db_query($link, "SELECT id
2156 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
2157 $_SESSION["uid"]);
2158
2159 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2160
2161 $tmp_feed = $tmp_line["id"];
2162
2163 db_query($link, "UPDATE ttrss_user_entries
2164 SET unread = false,last_read = NOW()
2165 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
2166 }
2167
2168 } else if ($feed > 0) {
2169
2170 $tmp_result = db_query($link, "SELECT id
2171 FROM ttrss_feeds WHERE parent_feed = '$feed'
2172 ORDER BY cat_id,title");
2173
2174 $parent_ids = array();
2175
2176 if (db_num_rows($tmp_result) > 0) {
2177 while ($p = db_fetch_assoc($tmp_result)) {
2178 array_push($parent_ids, "feed_id = " . $p["id"]);
2179 }
2180
2181 $children_qpart = implode(" OR ", $parent_ids);
2182
2183 db_query($link, "UPDATE ttrss_user_entries
2184 SET unread = false,last_read = NOW()
2185 WHERE (feed_id = '$feed' OR $children_qpart)
2186 AND owner_uid = " . $_SESSION["uid"]);
2187
2188 } else {
2189 db_query($link, "UPDATE ttrss_user_entries
2190 SET unread = false,last_read = NOW()
2191 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2192 }
2193
2194 } else if ($feed < 0 && $feed > -10) { // special, like starred
2195
2196 if ($feed == -1) {
2197 db_query($link, "UPDATE ttrss_user_entries
2198 SET unread = false,last_read = NOW()
2199 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
2200 }
2201
2202 if ($feed == -2) {
2203 db_query($link, "UPDATE ttrss_user_entries
2204 SET unread = false,last_read = NOW()
2205 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
2206 }
2207
2208 if ($feed == -3) {
2209
2210 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2211
2212 if (DB_TYPE == "pgsql") {
2213 $match_part = "updated > NOW() - INTERVAL '$intl hour' ";
2214 } else {
2215 $match_part = "updated > DATE_SUB(NOW(),
2216 INTERVAL $intl HOUR) ";
2217 }
2218
2219 $result = db_query($link, "SELECT id FROM ttrss_entries,
2220 ttrss_user_entries WHERE $match_part AND
2221 unread = true AND
2222 ttrss_user_entries.ref_id = ttrss_entries.id AND
2223 owner_uid = ".$_SESSION["uid"]);
2224
2225 $affected_ids = array();
2226
2227 while ($line = db_fetch_assoc($result)) {
2228 array_push($affected_ids, $line["id"]);
2229 }
2230
2231 catchupArticlesById($link, $affected_ids, 0);
2232 }
2233
2234 if ($feed == -4) {
2235 db_query($link, "UPDATE ttrss_user_entries
2236 SET unread = false,last_read = NOW()
2237 WHERE owner_uid = ".$_SESSION["uid"]);
2238 }
2239
2240 } else if ($feed < -10) { // label
2241
2242 $label_id = -$feed - 11;
2243
2244 db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2
2245 SET unread = false WHERE label_id = '$label_id' AND unread = true
2246 AND owner_uid = '".$_SESSION["uid"]."' AND ref_id = article_id");
2247
2248 }
2249
2250 ccache_update($link, $feed, $_SESSION["uid"], $cat_view);
2251
2252 } else { // tag
2253 db_query($link, "BEGIN");
2254
2255 $tag_name = db_escape_string($feed);
2256
2257 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
2258 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
2259
2260 while ($line = db_fetch_assoc($result)) {
2261 db_query($link, "UPDATE ttrss_user_entries SET
2262 unread = false, last_read = NOW()
2263 WHERE int_id = " . $line["post_int_id"]);
2264 }
2265 db_query($link, "COMMIT");
2266 }
2267 }
2268
2269 function update_generic_feed($link, $feed, $cat_view, $force_update = false) {
2270 if ($cat_view) {
2271
2272 if ($feed > 0) {
2273 $cat_qpart = "cat_id = '$feed'";
2274 } else {
2275 $cat_qpart = "cat_id IS NULL";
2276 }
2277
2278 $tmp_result = db_query($link, "SELECT id,feed_url FROM ttrss_feeds
2279 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
2280
2281 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2282 $feed_url = $tmp_line["feed_url"];
2283 $feed_id = $tmp_line["id"];
2284 update_rss_feed($link, $feed_url, $feed_id, $force_update);
2285 }
2286
2287 } else {
2288 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
2289 WHERE id = '$feed'");
2290 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
2291 update_rss_feed($link, $feed_url, $feed, $force_update);
2292 }
2293 }
2294
2295 function getAllCounters($link, $omode = "flc", $active_feed = false) {
2296
2297 if (!$omode) $omode = "flc";
2298
2299 getGlobalCounters($link);
2300
2301 if (strchr($omode, "l")) getLabelCounters($link);
2302 if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed);
2303 if (strchr($omode, "t")) getTagCounters($link);
2304 if (strchr($omode, "c")) {
2305 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2306 getCategoryCounters($link);
2307 }
2308 }
2309 }
2310
2311 function getCategoryCounters($link) {
2312 # two special categories are -1 and -2 (all virtuals; all labels)
2313
2314 /* $ctr = getCategoryUnread($link, -1);
2315
2316 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>"; */
2317
2318 $ctr = getCategoryUnread($link, -2);
2319
2320 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
2321
2322 $age_qpart = getMaxAgeSubquery();
2323
2324 $result = db_query($link, "SELECT id AS cat_id, value AS unread
2325 FROM ttrss_feed_categories, ttrss_cat_counters_cache
2326 WHERE ttrss_cat_counters_cache.feed_id = id AND
2327 ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
2328
2329 while ($line = db_fetch_assoc($result)) {
2330 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
2331
2332 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
2333 $line["unread"]."\"/>";
2334 }
2335
2336 /* Special case: NULL category doesn't actually exist in the DB */
2337
2338 print "<counter type=\"category\" id=\"0\" counter=\"".
2339 ccache_find($link, 0, $_SESSION["uid"], true)."\"/>";
2340
2341 }
2342
2343 function getCategoryUnread($link, $cat, $owner_uid = false) {
2344
2345 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2346
2347 if ($cat >= 0) {
2348
2349 if ($cat != 0) {
2350 $cat_query = "cat_id = '$cat'";
2351 } else {
2352 $cat_query = "cat_id IS NULL";
2353 }
2354
2355 $age_qpart = getMaxAgeSubquery();
2356
2357 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
2358 AND hidden = false
2359 AND owner_uid = " . $owner_uid);
2360
2361 $cat_feeds = array();
2362 while ($line = db_fetch_assoc($result)) {
2363 array_push($cat_feeds, "feed_id = " . $line["id"]);
2364 }
2365
2366 if (count($cat_feeds) == 0) return 0;
2367
2368 $match_part = implode(" OR ", $cat_feeds);
2369
2370 $result = db_query($link, "SELECT COUNT(int_id) AS unread
2371 FROM ttrss_user_entries,ttrss_entries
2372 WHERE unread = true AND ($match_part) AND id = ref_id
2373 AND $age_qpart AND owner_uid = " . $owner_uid);
2374
2375 $unread = 0;
2376
2377 # this needs to be rewritten
2378 while ($line = db_fetch_assoc($result)) {
2379 $unread += $line["unread"];
2380 }
2381
2382 return $unread;
2383 } else if ($cat == -1) {
2384 return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3);
2385 } else if ($cat == -2) {
2386
2387 $result = db_query($link, "
2388 SELECT COUNT(unread) AS unread FROM
2389 ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
2390 WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
2391 ttrss_labels2.owner_uid = '$owner_uid'
2392 AND unread = true AND hidden = false AND feed_id = ttrss_feeds.id
2393 AND ttrss_user_entries.owner_uid = '$owner_uid'");
2394
2395 $unread = db_fetch_result($result, 0, "unread");
2396
2397 return $unread;
2398
2399 }
2400 }
2401
2402 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
2403 if (DB_TYPE == "pgsql") {
2404 return "ttrss_entries.date_entered >
2405 NOW() - INTERVAL '$days days'";
2406 } else {
2407 return "ttrss_entries.date_entered >
2408 DATE_SUB(NOW(), INTERVAL $days DAY)";
2409 }
2410 }
2411
2412 function getFeedUnread($link, $feed, $is_cat = false) {
2413 return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]);
2414 }
2415
2416 function getLabelUnread($link, $label_id, $owner_uid = false) {
2417 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2418
2419 $result = db_query($link, "
2420 SELECT COUNT(unread) AS unread FROM
2421 ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
2422 WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
2423 ttrss_labels2.owner_uid = '$owner_uid' AND ttrss_labels2.id = '$label_id'
2424 AND unread = true AND hidden = false AND feed_id = ttrss_feeds.id
2425 AND ttrss_user_entries.owner_uid = '$owner_uid'");
2426
2427 if (db_num_rows($result) != 0) {
2428 return db_fetch_result($result, 0, "unread");
2429 } else {
2430 return 0;
2431 }
2432 }
2433
2434 function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false,
2435 $owner_uid = false) {
2436
2437 $n_feed = sprintf("%d", $feed);
2438
2439 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2440
2441 if ($unread_only) {
2442 $unread_qpart = "unread = true";
2443 } else {
2444 $unread_qpart = "true";
2445 }
2446
2447 $age_qpart = getMaxAgeSubquery();
2448
2449 if ($is_cat) {
2450 return getCategoryUnread($link, $n_feed, $owner_uid);
2451 } else if ($n_feed == -1) {
2452 $match_part = "marked = true";
2453 } else if ($n_feed == -2) {
2454 $match_part = "published = true";
2455 } else if ($n_feed == -3) {
2456 $match_part = "unread = true";
2457
2458 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
2459
2460 if (DB_TYPE == "pgsql") {
2461 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2462 } else {
2463 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2464 }
2465 } else if ($n_feed == -4) {
2466 $match_part = "true";
2467 } else if ($n_feed > 0) {
2468
2469 $result = db_query($link, "SELECT id FROM ttrss_feeds
2470 WHERE parent_feed = '$n_feed'
2471 AND hidden = false
2472 AND owner_uid = " . $owner_uid);
2473
2474 if (db_num_rows($result) > 0) {
2475
2476 $linked_feeds = array();
2477 while ($line = db_fetch_assoc($result)) {
2478 array_push($linked_feeds, "feed_id = " . $line["id"]);
2479 }
2480
2481 array_push($linked_feeds, "feed_id = $n_feed");
2482
2483 $match_part = implode(" OR ", $linked_feeds);
2484
2485 $result = db_query($link, "SELECT COUNT(int_id) AS unread
2486 FROM ttrss_user_entries,ttrss_entries
2487 WHERE $unread_qpart AND
2488 ttrss_user_entries.ref_id = ttrss_entries.id AND
2489 $age_qpart AND
2490 ($match_part) AND
2491 owner_uid = " . $owner_uid);
2492
2493 $unread = 0;
2494
2495 # this needs to be rewritten
2496 while ($line = db_fetch_assoc($result)) {
2497 $unread += $line["unread"];
2498 }
2499
2500 return $unread;
2501
2502 } else {
2503 $match_part = "feed_id = '$n_feed'";
2504 }
2505 } else if ($feed < -10) {
2506
2507 $label_id = -$feed - 11;
2508
2509 return getLabelUnread($link, $label_id, $owner_uid);
2510
2511 }
2512
2513 if ($match_part) {
2514
2515 $result = db_query($link, "SELECT count(int_id) AS unread
2516 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2517 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2518 ttrss_user_entries.ref_id = ttrss_entries.id AND
2519 ttrss_feeds.hidden = false AND
2520 $age_qpart AND
2521 $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = " . $owner_uid);
2522
2523 } else {
2524
2525 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
2526 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
2527 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
2528 AND $unread_qpart AND $age_qpart AND
2529 ttrss_tags.owner_uid = " . $owner_uid);
2530 }
2531
2532 $unread = db_fetch_result($result, 0, "unread");
2533
2534 return $unread;
2535 }
2536
2537 function getGlobalUnread($link, $user_id = false) {
2538
2539 if (!$user_id) {
2540 $user_id = $_SESSION["uid"];
2541 }
2542
2543 $result = db_query($link, "SELECT SUM(value) AS c_id FROM ttrss_counters_cache
2544 WHERE owner_uid = '$user_id' AND feed_id > 0");
2545
2546 $c_id = db_fetch_result($result, 0, "c_id");
2547
2548 return $c_id;
2549 }
2550
2551 function getGlobalCounters($link, $global_unread = -1) {
2552 if ($global_unread == -1) {
2553 $global_unread = getGlobalUnread($link);
2554 }
2555 print "<counter type=\"global\" id='global-unread'
2556 counter='$global_unread'/>";
2557
2558 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2559 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2560
2561 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2562
2563 print "<counter type=\"global\" id='subscribed-feeds'
2564 counter='$subscribed_feeds'/>";
2565
2566 }
2567
2568 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
2569
2570 if ($smart_mode) {
2571 if (!$_SESSION["tctr_last_value"]) {
2572 $_SESSION["tctr_last_value"] = array();
2573 }
2574 }
2575
2576 $old_counters = $_SESSION["tctr_last_value"];
2577
2578 $tctrs_modified = false;
2579
2580 $age_qpart = getMaxAgeSubquery();
2581
2582 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2583 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
2584 AND ref_id = id AND $age_qpart
2585 AND unread = true)) AS count FROM ttrss_tags
2586 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2587 ORDER BY count DESC LIMIT 55");
2588
2589 $tags = array();
2590
2591 while ($line = db_fetch_assoc($result)) {
2592 $tags[$line["tag_name"]] += $line["count"];
2593 }
2594
2595 foreach (array_keys($tags) as $tag) {
2596 $unread = $tags[$tag];
2597
2598 $tag = htmlspecialchars($tag);
2599
2600 if (!$smart_mode || $old_counters[$tag] != $unread) {
2601 $old_counters[$tag] = $unread;
2602 $tctrs_modified = true;
2603 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2604 }
2605
2606 }
2607
2608 if ($smart_mode && $tctrs_modified) {
2609 $_SESSION["tctr_last_value"] = $old_counters;
2610 }
2611
2612 }
2613
2614 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
2615
2616 $age_qpart = getMaxAgeSubquery();
2617
2618 if ($smart_mode) {
2619 if (!$_SESSION["lctr_last_value"]) {
2620 $_SESSION["lctr_last_value"] = array();
2621 }
2622 }
2623
2624 $ret_arr = array();
2625
2626 for ($i = -1; $i >= -4; $i--) {
2627
2628 $count = getFeedUnread($link, $i);
2629
2630 if (!$ret_mode) {
2631
2632 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2633 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $i) . " total)\"";
2634 } else {
2635 $xmsg_part = "";
2636 }
2637
2638 print "<counter type=\"label\" id=\"$i\" counter=\"$count\" $xmsg_part/>";
2639 } else {
2640 $ret_arr[$i]["counter"] = $count;
2641 $ret_arr[$i]["description"] = getFeedTitle($link, $i);
2642 }
2643
2644 }
2645
2646 $old_counters = $_SESSION["lctr_last_value"];
2647 $lctrs_modified = false;
2648
2649
2650 $owner_uid = $_SESSION["uid"];
2651
2652 $result = db_query($link, "SELECT id, caption FROM ttrss_labels2
2653 WHERE owner_uid = '$owner_uid'");
2654
2655 while ($line = db_fetch_assoc($result)) {
2656
2657 $id = -$line["id"] - 11;
2658
2659 $label_name = $line["caption"];
2660 $count = getFeedUnread($link, $id);
2661
2662 if (!$smart_mode || $old_counters[$id] != $count) {
2663 $old_counters[$id] = $count;
2664 $lctrs_modified = true;
2665 if (!$ret_mode) {
2666
2667 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2668 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
2669 } else {
2670 $xmsg_part = "";
2671 }
2672
2673 print "<counter type=\"label\" id=\"$id\" counter=\"$count\" $xmsg_part/>";
2674 } else {
2675 $ret_arr[$id]["counter"] = $count;
2676 $ret_arr[$id]["description"] = $label_name;
2677 }
2678 }
2679
2680 error_reporting (DEFAULT_ERROR_LEVEL);
2681 }
2682
2683 if ($smart_mode && $lctrs_modified) {
2684 $_SESSION["lctr_last_value"] = $old_counters;
2685 }
2686
2687 return $ret_arr;
2688 }
2689
2690 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS, $active_feed = false) {
2691
2692 $age_qpart = getMaxAgeSubquery();
2693
2694 if ($smart_mode) {
2695 if (!$_SESSION["fctr_last_value"]) {
2696 $_SESSION["fctr_last_value"] = array();
2697 }
2698 }
2699
2700 $old_counters = $_SESSION["fctr_last_value"];
2701
2702 $query = "SELECT ttrss_feeds.id,
2703 ttrss_feeds.title,
2704 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
2705 last_error, value AS count
2706 FROM ttrss_feeds, ttrss_counters_cache
2707 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2708 AND parent_feed IS NULL
2709 AND ttrss_counters_cache.feed_id = id";
2710
2711 $result = db_query($link, $query);
2712 $fctrs_modified = false;
2713
2714 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2715
2716 while ($line = db_fetch_assoc($result)) {
2717
2718 $id = $line["id"];
2719 $count = $line["count"];
2720 $last_error = htmlspecialchars($line["last_error"]);
2721
2722 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2723 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2724 } else {
2725 $last_updated = date($short_date, strtotime($line["last_updated"]));
2726 }
2727
2728 $last_updated = htmlspecialchars($last_updated);
2729
2730 $has_img = feed_has_icon($id);
2731
2732 $tmp_result = db_query($link,
2733 "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache
2734 WHERE parent_feed = '$id' AND feed_id = id");
2735
2736 $count += db_fetch_result($tmp_result, 0, "unread");
2737
2738 if (!$smart_mode || $old_counters[$id] != $count) {
2739 $old_counters[$id] = $count;
2740 $fctrs_modified = true;
2741
2742 if ($last_error) {
2743 $error_part = "error=\"$last_error\"";
2744 } else {
2745 $error_part = "";
2746 }
2747
2748 if ($has_img) {
2749 $has_img_part = "hi=\"$has_img\"";
2750 } else {
2751 $has_img_part = "";
2752 }
2753
2754 if ($active_feed && $id == $active_feed) {
2755 $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\"";
2756 } else {
2757 $has_title_part = "";
2758 }
2759
2760 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2761 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
2762 }
2763
2764 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $xmsg_part $has_title_part/>";
2765 }
2766 }
2767
2768 if ($smart_mode && $fctrs_modified) {
2769 $_SESSION["fctr_last_value"] = $old_counters;
2770 }
2771 }
2772
2773 function get_script_dt_add() {
2774 if (strpos(VERSION, ".99") === false) {
2775 return VERSION;
2776 } else {
2777 return time();
2778 }
2779 }
2780
2781 function get_pgsql_version($link) {
2782 $result = db_query($link, "SELECT version() AS version");
2783 $version = split(" ", db_fetch_result($result, 0, "version"));
2784 return $version[1];
2785 }
2786
2787 function print_error_xml($code, $add_msg = "") {
2788 global $ERRORS;
2789
2790 $error_msg = $ERRORS[$code];
2791
2792 if ($add_msg) {
2793 $error_msg = "$error_msg; $add_msg";
2794 }
2795
2796 print "<rpc-reply>";
2797 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2798 print "</rpc-reply>";
2799 }
2800
2801 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2802 $auth_login = '', $auth_pass = '') {
2803
2804 # check for feed:http://url
2805 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2806
2807 # check for feed://URL
2808 if (strpos($feed_link, "//") === 0) {
2809 $feed_link = "http:$feed_link";
2810 }
2811
2812 if ($feed_link == "") return;
2813
2814 if ($cat_id == "0" || !$cat_id) {
2815 $cat_qpart = "NULL";
2816 } else {
2817 $cat_qpart = "'$cat_id'";
2818 }
2819
2820 $result = db_query($link,
2821 "SELECT id FROM ttrss_feeds
2822 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2823
2824 if (db_num_rows($result) == 0) {
2825
2826 $result = db_query($link,
2827 "INSERT INTO ttrss_feeds
2828 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2829 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2830 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2831
2832 $result = db_query($link,
2833 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2834 AND owner_uid = " . $_SESSION["uid"]);
2835
2836 $feed_id = db_fetch_result($result, 0, "id");
2837
2838 if ($feed_id) {
2839 update_rss_feed($link, $feed_link, $feed_id, true);
2840 }
2841
2842 return true;
2843 } else {
2844 return false;
2845 }
2846 }
2847
2848 function print_feed_select($link, $id, $default_id = "",
2849 $attributes = "", $include_all_feeds = true) {
2850
2851 print "<select id=\"$id\" name=\"$id\" $attributes>";
2852 if ($include_all_feeds) {
2853 print "<option value=\"0\">".__('All feeds')."</option>";
2854 }
2855
2856 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2857 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2858
2859 if (db_num_rows($result) > 0 && $include_all_feeds) {
2860 print "<option disabled>--------</option>";
2861 }
2862
2863 while ($line = db_fetch_assoc($result)) {
2864 if ($line["id"] == $default_id) {
2865 $is_selected = "selected";
2866 } else {
2867 $is_selected = "";
2868 }
2869 printf("<option $is_selected value='%d'>%s</option>",
2870 $line["id"], htmlspecialchars($line["title"]));
2871 }
2872
2873 print "</select>";
2874 }
2875
2876 function print_feed_cat_select($link, $id, $default_id = "",
2877 $attributes = "", $include_all_cats = true) {
2878
2879 print "<select id=\"$id\" name=\"$id\" $attributes>";
2880
2881 if ($include_all_cats) {
2882 print "<option value=\"0\">".__('Uncategorized')."</option>";
2883 }
2884
2885 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2886 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2887
2888 if (db_num_rows($result) > 0 && $include_all_cats) {
2889 print "<option disabled>--------</option>";
2890 }
2891
2892 while ($line = db_fetch_assoc($result)) {
2893 if ($line["id"] == $default_id) {
2894 $is_selected = "selected";
2895 } else {
2896 $is_selected = "";
2897 }
2898 printf("<option $is_selected value='%d'>%s</option>",
2899 $line["id"], htmlspecialchars($line["title"]));
2900 }
2901
2902 print "</select>";
2903 }
2904
2905 function checkbox_to_sql_bool($val) {
2906 return ($val == "on") ? "true" : "false";
2907 }
2908
2909 function getFeedCatTitle($link, $id) {
2910 if ($id == -1) {
2911 return __("Special");
2912 } else if ($id < -10) {
2913 return __("Labels");
2914 } else if ($id > 0) {
2915 $result = db_query($link, "SELECT ttrss_feed_categories.title
2916 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2917 cat_id = ttrss_feed_categories.id");
2918 if (db_num_rows($result) == 1) {
2919 return db_fetch_result($result, 0, "title");
2920 } else {
2921 return __("Uncategorized");
2922 }
2923 } else {
2924 return "getFeedCatTitle($id) failed";
2925 }
2926
2927 }
2928
2929 function getFeedTitle($link, $id) {
2930 if ($id == -1) {
2931 return __("Starred articles");
2932 } else if ($id == -2) {
2933 return __("Published articles");
2934 } else if ($id == -3) {
2935 return __("Fresh articles");
2936 } else if ($id == -4) {
2937 return __("All articles");
2938 } else if ($id < -10) {
2939 $label_id = -$id - 11;
2940 $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
2941 if (db_num_rows($result) == 1) {
2942 return db_fetch_result($result, 0, "caption");
2943 } else {
2944 return "Unknown label ($label_id)";
2945 }
2946
2947 } else if ($id > 0) {
2948 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2949 if (db_num_rows($result) == 1) {
2950 return db_fetch_result($result, 0, "title");
2951 } else {
2952 return "Unknown feed ($id)";
2953 }
2954 } else {
2955 return "getFeedTitle($id) failed";
2956 }
2957
2958 }
2959
2960 function get_session_cookie_name() {
2961 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
2962 }
2963
2964 function print_init_params($link) {
2965 print "<init-params>";
2966 if ($_SESSION["stored-params"]) {
2967 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2968 if ($key) {
2969 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2970 print "<param key=\"$key\" value=\"$value\"/>";
2971 }
2972 }
2973 }
2974
2975 print "<param key=\"theme\" value=\"".$_SESSION["theme"]."\"/>";
2976 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2977 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
2978 print "<param key=\"daemon_refresh_only\" value=\"true\"/>";
2979
2980 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2981 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2982
2983 print "<param key=\"hide_read_feeds\" value=\"" .
2984 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2985
2986 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2987 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2988
2989 print "<param key=\"confirm_feed_catchup\" value=\"" .
2990 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2991
2992 print "<param key=\"cdm_auto_catchup\" value=\"" .
2993 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2994
2995 print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>";
2996
2997 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>";
2998
2999 print "<param key=\"default_view_mode\" value=\"" .
3000 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
3001
3002 print "<param key=\"default_view_limit\" value=\"" .
3003 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
3004
3005 print "<param key=\"default_view_order_by\" value=\"" .
3006 get_pref($link, "_DEFAULT_VIEW_ORDER_BY") . "\"/>";
3007
3008 print "<param key=\"prefs_active_tab\" value=\"" .
3009 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
3010
3011 print "<param key=\"infobox_disable_overlay\" value=\"" .
3012 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
3013
3014 print "<param key=\"icons_location\" value=\"" .
3015 ICONS_URL . "\"/>";
3016
3017 print "<param key=\"hide_read_shows_special\" value=\"" .
3018 (int) get_pref($link, "HIDE_READ_SHOWS_SPECIAL") . "\"/>";
3019
3020 print "<param key=\"hide_feedlist\" value=\"" .
3021 (int) get_pref($link, "HIDE_FEEDLIST") . "\"/>";
3022
3023 print "<param key=\"bw_limit\" value=\"".
3024 (int) $_SESSION["bw_limit"]."\"/>";
3025
3026 // print "<param key=\"sync_counters\" value=\"" .
3027 // (int) get_pref($link, "SYNC_COUNTERS") . "\"/>";
3028
3029 print "<param key=\"sync_counters\" value=\"1\"/>";
3030
3031 print "</init-params>";
3032 }
3033
3034 function print_runtime_info($link) {
3035 print "<runtime-info>";
3036
3037 if (ENABLE_UPDATE_DAEMON) {
3038 print "<param key=\"daemon_is_running\" value=\"".
3039 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
3040
3041 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
3042
3043 $stamp = (int)read_stampfile("update_daemon.stamp");
3044
3045 // print "<param key=\"daemon_stamp_delta\" value=\"$stamp_delta\"/>";
3046
3047 if ($stamp) {
3048 $stamp_delta = time() - $stamp;
3049
3050 if ($stamp_delta > 1800) {
3051 $stamp_check = 0;
3052 } else {
3053 $stamp_check = 1;
3054 $_SESSION["daemon_stamp_check"] = time();
3055 }
3056
3057 print "<param key=\"daemon_stamp_ok\" value=\"$stamp_check\"/>";
3058
3059 $stamp_fmt = date("Y.m.d, G:i", $stamp);
3060
3061 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
3062 }
3063 }
3064 }
3065
3066 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
3067
3068 if ($_SESSION["last_version_check"] + 86400 < time()) {
3069 $new_version_details = check_for_update($link);
3070
3071 print "<param key=\"new_version_available\" value=\"".
3072 sprintf("%d", $new_version_details != ""). "\"/>";
3073
3074 $_SESSION["last_version_check"] = time();
3075 }
3076 }
3077
3078 // print "<param key=\"new_version_available\" value=\"1\"/>";
3079
3080 print "</runtime-info>";
3081 }
3082
3083 function getSearchSql($search, $match_on) {
3084
3085 $search_query_part = "";
3086
3087 $keywords = split(" ", $search);
3088 $query_keywords = array();
3089
3090 if ($match_on == "both") {
3091
3092 foreach ($keywords as $k) {
3093 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
3094 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
3095 }
3096
3097 $search_query_part = implode("AND", $query_keywords) . " AND ";
3098
3099 } else if ($match_on == "title") {
3100
3101 foreach ($keywords as $k) {
3102 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
3103 }
3104
3105 $search_query_part = implode("AND", $query_keywords) . " AND ";
3106
3107 } else if ($match_on == "content") {
3108
3109 foreach ($keywords as $k) {
3110 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
3111 }
3112 }
3113
3114 $search_query_part = implode("AND", $query_keywords);
3115
3116 return $search_query_part;
3117 }
3118
3119 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
3120
3121 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3122
3123 if ($search) {
3124
3125 $search_query_part = getSearchSql($search, $match_on);
3126 $search_query_part .= " AND ";
3127
3128 } else {
3129 $search_query_part = "";
3130 }
3131
3132 $view_query_part = "";
3133
3134 if ($view_mode == "adaptive" || $view_query_part == "noscores") {
3135 if ($search) {
3136 $view_query_part = " ";
3137 } else if ($feed != -1) {
3138 $unread = getFeedUnread($link, $feed, $cat_view);
3139 if ($unread > 0) {
3140 $view_query_part = " unread = true AND ";
3141 }
3142 }
3143 }
3144
3145 if ($view_mode == "marked") {
3146 $view_query_part = " marked = true AND ";
3147 }
3148
3149 if ($view_mode == "unread") {
3150 $view_query_part = " unread = true AND ";
3151 }
3152
3153 if ($view_mode == "updated") {
3154 $view_query_part = " (last_read is null and unread = false) AND ";
3155 }
3156
3157 if ($limit > 0) {
3158 $limit_query_part = "LIMIT " . $limit;
3159 }
3160
3161 $vfeed_query_part = "";
3162
3163 // override query strategy and enable feed display when searching globally
3164 if ($search && $search_mode == "all_feeds") {
3165 $query_strategy_part = "ttrss_entries.id > 0";
3166 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3167 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
3168 $query_strategy_part = "ttrss_entries.id > 0";
3169 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
3170 id = feed_id) as feed_title,";
3171 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
3172
3173 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3174
3175 $tmp_result = false;
3176
3177 if ($cat_view) {
3178 $tmp_result = db_query($link, "SELECT id
3179 FROM ttrss_feeds WHERE cat_id = '$feed'");
3180 } else {
3181 $tmp_result = db_query($link, "SELECT id
3182 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
3183 WHERE id = '$feed') AND id != '$feed'");
3184 }
3185
3186 $cat_siblings = array();
3187
3188 if (db_num_rows($tmp_result) > 0) {
3189 while ($p = db_fetch_assoc($tmp_result)) {
3190 array_push($cat_siblings, "feed_id = " . $p["id"]);
3191 }
3192
3193 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3194 $feed, implode(" OR ", $cat_siblings));
3195
3196 } else {
3197 $query_strategy_part = "ttrss_entries.id > 0";
3198 }
3199
3200 } else if ($feed >= 0) {
3201
3202 if ($cat_view) {
3203
3204 if ($feed > 0) {
3205 $query_strategy_part = "cat_id = '$feed'";
3206 } else {
3207 $query_strategy_part = "cat_id IS NULL";
3208 }
3209
3210 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3211
3212 } else {
3213 $tmp_result = db_query($link, "SELECT id
3214 FROM ttrss_feeds WHERE parent_feed = '$feed'
3215 ORDER BY cat_id,title");
3216
3217 $parent_ids = array();
3218
3219 if (db_num_rows($tmp_result) > 0) {
3220 while ($p = db_fetch_assoc($tmp_result)) {
3221 array_push($parent_ids, "feed_id = " . $p["id"]);
3222 }
3223
3224 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3225 $feed, implode(" OR ", $parent_ids));
3226
3227 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3228 } else {
3229 $query_strategy_part = "feed_id = '$feed'";
3230 }
3231 }
3232 } else if ($feed == -1) { // starred virtual feed
3233 $query_strategy_part = "marked = true";
3234 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3235 } else if ($feed == -2) { // published virtual feed OR labels category
3236
3237 if (!$cat_view) {
3238 $query_strategy_part = "published = true";
3239 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3240 } else {
3241 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3242
3243 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
3244
3245 $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
3246 ttrss_user_labels2.article_id = ref_id";
3247
3248 }
3249
3250 } else if ($feed == -3) { // fresh virtual feed
3251 $query_strategy_part = "unread = true";
3252
3253 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
3254
3255 if (DB_TYPE == "pgsql") {
3256 $query_strategy_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
3257 } else {
3258 $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
3259 }
3260
3261 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3262 } else if ($feed == -4) { // all articles virtual feed
3263 $query_strategy_part = "true";
3264 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3265 } else if ($feed <= -10) { // labels
3266 $label_id = -$feed - 11;
3267
3268 $query_strategy_part = "label_id = '$label_id' AND
3269 ttrss_labels2.id = ttrss_user_labels2.label_id AND
3270 ttrss_user_labels2.article_id = ref_id";
3271
3272 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3273 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
3274
3275 } else {
3276 $query_strategy_part = "id > 0"; // dumb
3277 }
3278
3279 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
3280 $order_by = "updated";
3281 } else {
3282 $order_by = "updated DESC";
3283 }
3284
3285 if ($view_mode != "noscores") {
3286 $order_by = "score DESC, $order_by";
3287 }
3288
3289 if ($override_order) {
3290 $order_by = $override_order;
3291 }
3292
3293 $feed_title = "";
3294
3295 if ($search && $search_mode == "all_feeds") {
3296 $feed_title = __("Search results")." ($search)";
3297 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
3298 $feed_title = __("Search results")." ($search, $feed)";
3299 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
3300 $feed_title = $feed;
3301 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
3302
3303 if ($cat_view) {
3304
3305 if ($feed != 0) {
3306 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
3307 WHERE id = '$feed' AND owner_uid = $owner_uid");
3308 $feed_title = db_fetch_result($result, 0, "title");
3309 } else {
3310 $feed_title = __("Uncategorized");
3311 }
3312
3313 if ($search) {
3314 $feed_title = __("Searched for")." $search ($feed_title)";
3315 }
3316
3317 } else {
3318
3319 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
3320 WHERE id = '$feed' AND owner_uid = $owner_uid");
3321
3322 $feed_title = db_fetch_result($result, 0, "title");
3323 $feed_site_url = db_fetch_result($result, 0, "site_url");
3324 $last_error = db_fetch_result($result, 0, "last_error");
3325
3326 if ($search) {
3327 $feed_title = __("Searched for") . " $search ($feed_title)";
3328 }
3329 }
3330
3331 } else if ($feed == -1) {
3332 $feed_title = __("Starred articles");
3333 if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; }
3334 } else if ($feed == -2) {
3335 if (!$cat_view) {
3336 $feed_title = __("Published articles");
3337 if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; }
3338 } else {
3339 $feed_title = __("Labels");
3340 }
3341 } else if ($feed == -3) {
3342 $feed_title = __("Fresh articles");
3343 if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; }
3344 } else if ($feed == -4) {
3345 $feed_title = __("All articles");
3346 if ($search) { $feed_title = __("Searched for") . " $search ($feed_title)"; }
3347 } else if ($feed < -10) {
3348 $label_id = -$feed - 11;
3349 $result = db_query($link, "SELECT caption FROM ttrss_labels2
3350 WHERE id = '$label_id'");
3351 $feed_title = db_fetch_result($result, 0, "caption");
3352
3353 if ($search) {
3354 $feed_title = __("Searched for") . " $search ($feed_title)";
3355 }
3356 } else {
3357 $feed_title = "?";
3358 }
3359
3360 if ($feed < -10) error_reporting (0);
3361
3362 $content_query_part = "content as content_preview,";
3363
3364 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
3365
3366 if ($feed >= 0) {
3367 $feed_kind = "Feeds";
3368 } else {
3369 $feed_kind = "Labels";
3370 }
3371
3372 if ($limit_query_part) {
3373 $offset_query_part = "OFFSET $offset";
3374 }
3375
3376 if ($vfeed_query_part && get_pref($link, 'VFEED_GROUP_BY_FEED', $owner_uid)) {
3377 if (!$override_order) {
3378 $order_by = "ttrss_feeds.title, $order_by";
3379 }
3380
3381 // Special output for Fresh feed
3382
3383 /* if ($feed == -3) {
3384 $group_limit_part = "(select count(*) from
3385 ttrss_user_entries AS t1, ttrss_entries AS t2 where
3386 t1.ref_id = t2.id and t1.owner_uid = 2 and
3387 t1.feed_id = ttrss_user_entries.feed_id and
3388 t2.updated > ttrss_entries.updated) <= 5 AND";
3389 } */
3390 }
3391
3392 $query = "SELECT DISTINCT
3393 guid,
3394 ttrss_entries.id,ttrss_entries.title,
3395 updated,
3396 unread,feed_id,marked,published,link,last_read,
3397 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
3398 $vfeed_query_part
3399 $content_query_part
3400 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
3401 author,score
3402 FROM
3403 ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part
3404 WHERE
3405 $group_limit_part
3406 ttrss_feeds.hidden = false AND
3407 ttrss_user_entries.feed_id = ttrss_feeds.id AND
3408 ttrss_user_entries.ref_id = ttrss_entries.id AND
3409 ttrss_user_entries.owner_uid = '$owner_uid' AND
3410 $search_query_part
3411 $view_query_part
3412 $query_strategy_part ORDER BY $order_by
3413 $limit_query_part $offset_query_part";
3414
3415 if ($_GET["debug"]) print $query;
3416
3417 $result = db_query($link, $query);
3418
3419 } else {
3420 // browsing by tag
3421
3422 $feed_kind = "Tags";
3423
3424 $result = db_query($link, "SELECT
3425 guid,
3426 ttrss_entries.id as id,title,
3427 updated,
3428 unread,feed_id,
3429 marked,link,last_read,
3430 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
3431 $vfeed_query_part
3432 $content_query_part
3433 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
3434 score
3435 FROM
3436 ttrss_entries,ttrss_user_entries,ttrss_tags
3437 WHERE
3438 ref_id = ttrss_entries.id AND
3439 ttrss_user_entries.owner_uid = '$owner_uid' AND
3440 post_int_id = int_id AND tag_name = '$feed' AND
3441 $view_query_part
3442 $search_query_part
3443 $query_strategy_part ORDER BY $order_by
3444 $limit_query_part");
3445 }
3446
3447 return array($result, $feed_title, $feed_site_url, $last_error);
3448
3449 }
3450
3451 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
3452 $limit, $search, $search_mode, $match_on) {
3453
3454 if (!$limit) $limit = 30;
3455
3456 $qfh_ret = queryFeedHeadlines($link, $feed,
3457 $limit, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
3458 $owner_uid);
3459
3460 $result = $qfh_ret[0];
3461 $feed_title = htmlspecialchars($qfh_ret[1]);
3462 $feed_site_url = $qfh_ret[2];
3463 $last_error = $qfh_ret[3];
3464
3465 // if (!$feed_site_url) $feed_site_url = "http://localhost/";
3466
3467 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
3468 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
3469 <rss version=\"2.0\">
3470 <channel>
3471 <title>$feed_title</title>
3472 <link>$feed_site_url</link>
3473 <description>Feed generated by Tiny Tiny RSS</description>";
3474
3475 while ($line = db_fetch_assoc($result)) {
3476 print "<item>";
3477 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3478 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
3479
3480 $tags = get_article_tags($link, $line["id"], $owner_uid);
3481
3482 foreach ($tags as $tag) {
3483 print "<category>" . htmlspecialchars($tag) . "</category>";
3484 }
3485
3486 $rfc822_date = date('r', strtotime($line["updated"]));
3487
3488 print "<pubDate>$rfc822_date</pubDate>";
3489
3490 print "<title>" .
3491 htmlspecialchars($line["title"]) . "</title>";
3492
3493 print "<description><![CDATA[" .
3494 $line["content_preview"] . "]]></description>";
3495
3496 print "</item>";
3497 }
3498
3499 print "</channel></rss>";
3500
3501 }
3502
3503 function getCategoryTitle($link, $cat_id) {
3504
3505 if ($cat_id == -1) {
3506 return __("Special");
3507 } else if ($cat_id == -2) {
3508 return __("Labels");
3509 } else {
3510
3511 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
3512 id = '$cat_id'");
3513
3514 if (db_num_rows($result) == 1) {
3515 return db_fetch_result($result, 0, "title");
3516 } else {
3517 return "Uncategorized";
3518 }
3519 }
3520 }
3521
3522 // http://ru2.php.net/strip-tags
3523
3524 function strip_tags_long($textstring, $allowed){
3525 while($textstring != strip_tags($textstring, $allowed))
3526 {
3527 while (strlen($textstring) != 0)
3528 {
3529 if (strlen($textstring) > 1024) {
3530 $otherlen = 1024;
3531 } else {
3532 $otherlen = strlen($textstring);
3533 }
3534 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
3535 $safetext .= $temptext;
3536 $textstring = substr_replace($textstring,'',0,$otherlen);
3537 }
3538 $textstring = $safetext;
3539 }
3540 return $textstring;
3541 }
3542
3543
3544 function sanitize_rss($link, $str, $force_strip_tags = false) {
3545 $res = $str;
3546
3547 if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) {
3548
3549 $res = strip_tags_long($res,
3550 "<p><a><i><em><b><strong><code><pre><blockquote><br><img><ul><ol><li>");
3551
3552 // $res = preg_replace("/\r\n|\n|\r/", "", $res);
3553 // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
3554 }
3555
3556 if (get_pref($link, "STRIP_IMAGES")) {
3557
3558 $res = preg_replace('/<img[^>]+>/is', '', $res);
3559
3560 }
3561
3562 return $res;
3563 }
3564
3565 /**
3566 * Send by mail a digest of last articles.
3567 *
3568 * @param mixed $link The database connection.
3569 * @param integer $limit The maximum number of articles by digest.
3570 * @return boolean Return false if digests are not enabled.
3571 */
3572 function send_headlines_digests($link, $limit = 100) {
3573
3574 if (!DIGEST_ENABLE) return false;
3575
3576 $user_limit = DIGEST_EMAIL_LIMIT;
3577 $days = 1;
3578
3579 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3580
3581 if (DB_TYPE == "pgsql") {
3582 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3583 } else if (DB_TYPE == "mysql") {
3584 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3585 }
3586
3587 $result = db_query($link, "SELECT id,email FROM ttrss_users
3588 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3589
3590 while ($line = db_fetch_assoc($result)) {
3591
3592 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3593 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3594
3595 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
3596
3597 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3598 $digest = $tuple[0];
3599 $headlines_count = $tuple[1];
3600 $affected_ids = $tuple[2];
3601 $digest_text = $tuple[3];
3602
3603 if ($headlines_count > 0) {
3604
3605 $mail = new PHPMailer();
3606
3607 $mail->PluginDir = "phpmailer/";
3608 $mail->SetLanguage("en", "phpmailer/language/");
3609
3610 $mail->CharSet = "UTF-8";
3611
3612 $mail->From = DIGEST_FROM_ADDRESS;
3613 $mail->FromName = DIGEST_FROM_NAME;
3614 $mail->AddAddress($line["email"], $line["login"]);
3615
3616 if (DIGEST_SMTP_HOST) {
3617 $mail->Host = DIGEST_SMTP_HOST;
3618 $mail->Mailer = "smtp";
3619 $mail->SMTPAuth = DIGEST_SMTP_LOGIN != '';
3620 $mail->Username = DIGEST_SMTP_LOGIN;
3621 $mail->Password = DIGEST_SMTP_PASSWORD;
3622 }
3623
3624 $mail->IsHTML(true);
3625 $mail->Subject = DIGEST_SUBJECT;
3626 $mail->Body = $digest;
3627 $mail->AltBody = $digest_text;
3628
3629 $rc = $mail->Send();
3630
3631 if (!$rc) print "ERROR: " . $mail->ErrorInfo;
3632
3633 print "RC=$rc\n";
3634
3635 if ($rc && $do_catchup) {
3636 print "Marking affected articles as read...\n";
3637 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
3638 }
3639
3640 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3641 WHERE id = " . $line["id"]);
3642 } else {
3643 print "No headlines\n";
3644 }
3645 }
3646 }
3647
3648 print "All done.\n";
3649
3650 }
3651
3652 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
3653
3654 require_once "MiniTemplator.class.php";
3655
3656 $tpl = new MiniTemplator;
3657 $tpl_t = new MiniTemplator;
3658
3659 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
3660 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
3661
3662 $tpl->setVariable('CUR_DATE', date('Y/m/d'));
3663 $tpl->setVariable('CUR_TIME', date('G:i'));
3664
3665 $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
3666 $tpl_t->setVariable('CUR_TIME', date('G:i'));
3667
3668 $affected_ids = array();
3669
3670 if (DB_TYPE == "pgsql") {
3671 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
3672 } else if (DB_TYPE == "mysql") {
3673 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3674 }
3675
3676 $result = db_query($link, "SELECT ttrss_entries.title,
3677 ttrss_feeds.title AS feed_title,
3678 date_entered,
3679 ttrss_user_entries.ref_id,
3680 link,
3681 SUBSTRING(content, 1, 120) AS excerpt,
3682 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
3683 FROM
3684 ttrss_user_entries,ttrss_entries,ttrss_feeds
3685 WHERE
3686 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3687 AND include_in_digest = true
3688 AND $interval_query
3689 AND hidden = false
3690 AND ttrss_user_entries.owner_uid = $user_id
3691 AND unread = true
3692 ORDER BY ttrss_feeds.title, date_entered DESC
3693 LIMIT $limit");
3694
3695 $cur_feed_title = "";
3696
3697 $headlines_count = db_num_rows($result);
3698
3699 $headlines = array();
3700
3701 while ($line = db_fetch_assoc($result)) {
3702 array_push($headlines, $line);
3703 }
3704
3705 for ($i = 0; $i < sizeof($headlines); $i++) {
3706
3707 $line = $headlines[$i];
3708
3709 array_push($affected_ids, $line["ref_id"]);
3710
3711 $updated = smart_date_time(strtotime($line["last_updated"]));
3712
3713 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
3714 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
3715 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
3716 $tpl->setVariable('ARTICLE_UPDATED', $updated);
3717 $tpl->setVariable('ARTICLE_EXCERPT',
3718 truncate_string(strip_tags($line["excerpt"]), 100));
3719
3720 $tpl->addBlock('article');
3721
3722 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
3723 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
3724 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
3725 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
3726 // $tpl_t->setVariable('ARTICLE_EXCERPT',
3727 // truncate_string(strip_tags($line["excerpt"]), 100));
3728
3729 $tpl_t->addBlock('article');
3730
3731 if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
3732 $tpl->addBlock('feed');
3733 $tpl_t->addBlock('feed');
3734 }
3735
3736 }
3737
3738 $tpl->addBlock('digest');
3739 $tpl->generateOutputToString($tmp);
3740
3741 $tpl_t->addBlock('digest');
3742 $tpl_t->generateOutputToString($tmp_t);
3743
3744 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
3745 }
3746
3747 function check_for_update($link, $brief_fmt = true) {
3748 $releases_feed = "http://tt-rss.org/releases.rss";
3749
3750 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
3751 return;
3752 }
3753
3754 error_reporting(0);
3755 if (ENABLE_SIMPLEPIE) {
3756 $rss = new SimplePie();
3757 $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
3758 // $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
3759 $rss->set_feed_url($fetch_url);
3760 $rss->set_output_encoding('UTF-8');
3761 $rss->init();
3762 } else {
3763 $rss = fetch_rss($releases_feed);
3764 }
3765 error_reporting (DEFAULT_ERROR_LEVEL);
3766
3767 if ($rss) {
3768
3769 if (ENABLE_SIMPLEPIE) {
3770 $items = $rss->get_items();
3771 } else {
3772 $items = $rss->items;
3773
3774 if (!$items || !is_array($items)) $items = $rss->entries;
3775 if (!$items || !is_array($items)) $items = $rss;
3776 }
3777
3778 if (!is_array($items) || count($items) == 0) {
3779 return;
3780 }
3781
3782 $latest_item = $items[0];
3783
3784 if (ENABLE_SIMPLEPIE) {
3785 $last_title = $latest_item->get_title();
3786 } else {
3787 $last_title = $latest_item["title"];
3788 }
3789
3790 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
3791
3792 if (ENABLE_SIMPLEPIE) {
3793 $release_url = sanitize_rss($link, $latest_item->get_link());
3794 $content = sanitize_rss($link, $latest_item->get_description());
3795 } else {
3796 $release_url = sanitize_rss($link, $latest_item["link"]);
3797 $content = sanitize_rss($link, $latest_item["description"]);
3798 }
3799
3800 if (version_compare(VERSION, $latest_version) == -1) {
3801 if ($brief_fmt) {
3802 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
3803 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
3804 <div id=\"milestoneDetails\">$content</div>");
3805 } else {
3806 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3807 <div class='milestoneDetails'>$content</div>
3808 Visit <a target=\"_blank\" href=\"http://tt-rss.org/\">official site</a> for
3809 download and update information.";
3810 }
3811
3812 }
3813 }
3814 }
3815
3816 function markArticlesById($link, $ids, $cmode) {
3817
3818 $tmp_ids = array();
3819
3820 foreach ($ids as $id) {
3821 array_push($tmp_ids, "ref_id = '$id'");
3822 }
3823
3824 $ids_qpart = join(" OR ", $tmp_ids);
3825
3826 if ($cmode == 0) {
3827 db_query($link, "UPDATE ttrss_user_entries SET
3828 marked = false,last_read = NOW()
3829 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3830 } else if ($cmode == 1) {
3831 db_query($link, "UPDATE ttrss_user_entries SET
3832 marked = true
3833 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3834 } else {
3835 db_query($link, "UPDATE ttrss_user_entries SET
3836 marked = NOT marked,last_read = NOW()
3837 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3838 }
3839 }
3840
3841 function publishArticlesById($link, $ids, $cmode) {
3842
3843 $tmp_ids = array();
3844
3845 foreach ($ids as $id) {
3846 array_push($tmp_ids, "ref_id = '$id'");
3847 }
3848
3849 $ids_qpart = join(" OR ", $tmp_ids);
3850
3851 if ($cmode == 0) {
3852 db_query($link, "UPDATE ttrss_user_entries SET
3853 published = false,last_read = NOW()
3854 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3855 } else if ($cmode == 1) {
3856 db_query($link, "UPDATE ttrss_user_entries SET
3857 published = true
3858 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3859 } else {
3860 db_query($link, "UPDATE ttrss_user_entries SET
3861 published = NOT published,last_read = NOW()
3862 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3863 }
3864 }
3865
3866 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
3867
3868 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
3869
3870 $tmp_ids = array();
3871
3872 foreach ($ids as $id) {
3873 array_push($tmp_ids, "ref_id = '$id'");
3874 }
3875
3876 $ids_qpart = join(" OR ", $tmp_ids);
3877
3878 if ($cmode == 0) {
3879 db_query($link, "UPDATE ttrss_user_entries SET
3880 unread = false,last_read = NOW()
3881 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3882 } else if ($cmode == 1) {
3883 db_query($link, "UPDATE ttrss_user_entries SET
3884 unread = true
3885 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3886 } else {
3887 db_query($link, "UPDATE ttrss_user_entries SET
3888 unread = NOT unread,last_read = NOW()
3889 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3890 }
3891
3892 /* update ccache */
3893
3894 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
3895 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
3896
3897 while ($line = db_fetch_assoc($result)) {
3898 ccache_update($link, $line["feed_id"], $owner_uid);
3899 }
3900 }
3901
3902 function catchupArticleById($link, $id, $cmode) {
3903
3904 if ($cmode == 0) {
3905 db_query($link, "UPDATE ttrss_user_entries SET
3906 unread = false,last_read = NOW()
3907 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3908 } else if ($cmode == 1) {
3909 db_query($link, "UPDATE ttrss_user_entries SET
3910 unread = true
3911 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3912 } else {
3913 db_query($link, "UPDATE ttrss_user_entries SET
3914 unread = NOT unread,last_read = NOW()
3915 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3916 }
3917 }
3918
3919 function make_guid_from_title($title) {
3920 return preg_replace("/[ \"\',.:;]/", "-",
3921 mb_strtolower(strip_tags($title), 'utf-8'));
3922 }
3923
3924 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3925 $bottom = false, $rtl_content = false, $feed_id = 0,
3926 $is_cat = false, $search = false, $match_on = false,
3927 $search_mode = false, $offset = 0, $limit = 0,
3928 $dashboard_menu = 0, $disable_feed = 0, $feed_small_icon = 0) {
3929
3930 $user_page_offset = $offset + 1;
3931
3932 if (!$bottom) {
3933 $class = "headlinesSubToolbar";
3934 $tid = "headlineActionsTop";
3935 } else {
3936 $class = "headlinesSubToolbar";
3937 $tid = "headlineActionsBottom";
3938 }
3939
3940 print "<nobr><table class=\"$class\" id=\"$tid\"
3941 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3942
3943 if ($rtl_content) {
3944 $rtl_cpart = "RTL";
3945 } else {
3946 $rtl_cpart = "";
3947 }
3948
3949 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3950 $page_next_link = "javascript:viewFeedGoPage(1)";
3951 $page_first_link = "javascript:viewFeedGoPage(0)";
3952
3953 $catchup_page_link = "javascript:catchupPage()";
3954 $catchup_feed_link = "javascript:catchupCurrentFeed()";
3955 $catchup_sel_link = "javascript:catchupSelection()";
3956
3957 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3958
3959 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3960 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3961 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
3962 $sel_inv_link = "javascript:invertHeadlineSelection()";
3963
3964 $tog_unread_link = "javascript:selectionToggleUnread()";
3965 $tog_marked_link = "javascript:selectionToggleMarked()";
3966 $tog_published_link = "javascript:selectionTogglePublished()";
3967
3968 } else {
3969
3970 $sel_all_link = "javascript:cdmSelectArticles('all')";
3971 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3972 $sel_none_link = "javascript:cdmSelectArticles('none')";
3973
3974 $sel_inv_link = "javascript:invertHeadlineSelection()";
3975
3976 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3977 $tog_marked_link = "javascript:selectionToggleMarked(true)";
3978 $tog_published_link = "javascript:selectionTogglePublished(true)";
3979
3980 }
3981
3982 print "<td class=\"headlineActions$rtl_cpart\">";
3983
3984
3985 print __('Select:')."
3986 <a href=\"$sel_all_link\">".__('All')."</a>,
3987 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3988 <a href=\"$sel_inv_link\">".__('Invert')."</a>,
3989 <a href=\"$sel_none_link\">".__('None')."</a></li>";
3990
3991 print "&nbsp;&nbsp;";
3992
3993 print "<span
3994 onmouseover=\"enable_selection(false)\"
3995 onmouseout=\"enable_selection(true)\"
3996 onclick=\"toggleHeadlineActions()\" id=\"headlineActionsDrop\">".
3997 __("Actions...") . "&nbsp;&nbsp;<img src=\"images/down_arrow.png\">
3998 </span>";
3999
4000 print "<ul id=\"headlineActionsBody\" style=\"display : none\">";
4001
4002 print "<li class=\"insensitive\">".__('Selection toggle:')."</li>
4003 <li onclick=\"$tog_unread_link\">&nbsp;&nbsp;".__('Unread')."</li>
4004 <li onclick=\"$tog_marked_link\">&nbsp;&nbsp;".__('Starred')."</li>
4005 <li onclick=\"$tog_published_link\">&nbsp;&nbsp;".__('Published')."</li>
4006 <!-- <li><span class=\"insensitive\">--------</span></li> -->
4007 <li class=\"insensitive\">".__('Mark as read:')."</li>
4008 <li onclick=\"$catchup_sel_link\">&nbsp;&nbsp;".__('Selection')."</li>";
4009
4010 print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed').
4011 "</li>";
4012
4013 //print "<li><span class=\"insensitive\">--------</span></li>";
4014 print "<li class=\"insensitive\">".__('Assign label:')."</li>";
4015
4016 print "<li onclick=\"javascript:addLabel()\">
4017 &nbsp;&nbsp;".__("Create label...")."</li>";
4018
4019 $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 WHERE
4020 owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
4021
4022 while ($line = db_fetch_assoc($result)) {
4023
4024 $label_id = $line["id"];
4025 $label_caption = $line["caption"];
4026
4027 if ($feed_id < -10 && $feed_id == -11-$label_id) {
4028 print "<li onclick=\"javascript:selectionRemoveLabel($label_id)\">
4029 &nbsp;&nbsp;$label_caption ".__('(remove)')."</li>";
4030 } else {
4031 print "<li onclick=\"javascript:selectionAssignLabel($label_id)\">
4032 &nbsp;&nbsp;$label_caption</li>";
4033 }
4034 }
4035
4036 print "</ul>";
4037 print "</td>";
4038
4039 print "<td class=\"headlineTitle$rtl_cpart\">";
4040
4041 print "<span id=\"subtoolbar_ftitle\">";
4042
4043 if ($feed_site_url) {
4044 if (!$bottom) {
4045 $target = "target=\"_blank\"";
4046 }
4047 print "<a $target href=\"$feed_site_url\">".
4048 truncate_string($feed_title,30)."</a>";
4049 } else {
4050 print $feed_title;
4051 }
4052
4053 if ($search) {
4054 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
4055 }
4056
4057 if ($user_page_offset > 1) {
4058 print " [$user_page_offset] ";
4059 }
4060
4061 if (!$bottom && !$disable_feed) {
4062 print "
4063 <a target=\"_blank\"
4064 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
4065 <img class=\"noborder\"
4066 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
4067 </a>";
4068 } else if ($feed_small_icon) {
4069 print "<img class=\"noborder\" alt=\"\" src=\"images/$feed_small_icon\">";
4070 }
4071
4072 print "</span>";
4073
4074 print "</td>";
4075 print "</tr></table></nobr>";
4076
4077 }
4078
4079 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
4080
4081 $tmp_category = getCategoryTitle($link, $cat_id);
4082
4083 if ($cat_id > 0) {
4084 $cat_unread = ccache_find($link, $cat_id, $_SESSION["uid"], true);
4085 } else if ($cat_id == 0 || $cat_id == -2) {
4086 $cat_unread = getCategoryUnread($link, $cat_id);
4087 }
4088
4089 if ($hidden) {
4090 $holder_style = "display:none;";
4091 $ellipsis = "…";
4092 } else {
4093 $holder_style = "";
4094 $ellipsis = "";
4095 }
4096
4097 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
4098
4099 if ($can_browse) {
4100 $browse_cat_link = "onclick=\"javascript:viewCategory($cat_id)\"";
4101 $inner_title_class = "catTitle";
4102 } else {
4103 $browse_cat_link = "";
4104 $inner_title_class = "catTitleNL";
4105 }
4106
4107 $cat_class = "feedCat";
4108
4109 print "<li class=\"$cat_class\" id=\"FCAT-$cat_id\">
4110 <img onclick=\"toggleCollapseCat($cat_id)\" class=\"catCollapse\"
4111 title=\"".__('Click to collapse category')."\"
4112 src=\"images/cat-collapse.png\"><span class=\"$inner_title_class\"
4113 id=\"FCATN-$cat_id\" $browse_cat_link
4114 \">$tmp_category</span>";
4115
4116 print "<span id=\"FCAP-$cat_id\">";
4117
4118 print " <span id=\"FCATCTR-$cat_id\"
4119 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
4120
4121 print "</span>";
4122
4123 //print "</li>";
4124
4125 print "<ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
4126
4127 }
4128
4129 function outputFeedList($link, $tags = false) {
4130
4131 print "<ul class=\"feedList\" id=\"feedList\">";
4132
4133 $owner_uid = $_SESSION["uid"];
4134
4135 /* virtual feeds */
4136
4137 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4138
4139 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
4140 $cat_hidden = true;
4141 } else {
4142 $cat_hidden = false;
4143 }
4144
4145 printCategoryHeader($link, -1, $cat_hidden, false);
4146 }
4147
4148 $num_starred = getFeedUnread($link, -1);
4149 $num_published = getFeedUnread($link, -2);
4150 $num_fresh = getFeedUnread($link, -3);
4151 $num_total = getFeedUnread($link, -4);
4152
4153 $class = "virt";
4154
4155 if ($num_total > 0) $class .= "Unread";
4156
4157 printFeedEntry(-4, $class, __("All articles"), $num_total,
4158 "images/tag.png", $link);
4159
4160 $class = "virt";
4161
4162 if ($num_fresh > 0) $class .= "Unread";
4163
4164 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
4165 "images/fresh.png", $link);
4166
4167 $class = "virt";
4168
4169 if ($num_starred > 0) $class .= "Unread";
4170
4171 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
4172
4173 if ($is_ie) {
4174 $mark_img_ext = "gif";
4175 } else {
4176 $mark_img_ext = "png";
4177 }
4178
4179 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
4180 "images/mark_set.$mark_img_ext", $link);
4181
4182 $class = "virt";
4183
4184 if ($num_published > 0) $class .= "Unread";
4185
4186 printFeedEntry(-2, $class, __("Published articles"), $num_published,
4187 "images/pub_set.gif", $link);
4188
4189 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4190 print "</ul></li>";
4191 }
4192
4193 if (!$tags) {
4194
4195
4196 $result = db_query($link, "SELECT id,caption FROM
4197 ttrss_labels2 WHERE owner_uid = '$owner_uid' ORDER by caption");
4198
4199 if (db_num_rows($result) > 0) {
4200 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4201
4202 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
4203 $cat_hidden = true;
4204 } else {
4205 $cat_hidden = false;
4206 }
4207
4208 printCategoryHeader($link, -2, $cat_hidden, true);
4209
4210 } else {
4211 print "<li><hr></li>";
4212 }
4213 }
4214
4215 while ($line = db_fetch_assoc($result)) {
4216
4217 $label_id = -$line['id'] - 11;
4218 $count = getFeedUnread($link, $label_id);
4219
4220 $class = "label";
4221
4222 if ($count > 0) {
4223 $class .= "Unread";
4224 }
4225
4226 printFeedEntry($label_id,
4227 $class, $line["caption"],
4228 $count, "images/label.png", $link);
4229
4230 }
4231
4232 if (db_num_rows($result) > 0) {
4233 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4234 print "</ul>";
4235 }
4236 }
4237
4238
4239 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
4240 print "<li><hr></li>";
4241 }
4242
4243 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4244 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4245 $order_by_qpart = "order_id,category,unread DESC,title";
4246 } else {
4247 $order_by_qpart = "order_id,category,title";
4248 }
4249 } else {
4250 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4251 $order_by_qpart = "unread DESC,title";
4252 } else {
4253 $order_by_qpart = "title";
4254 }
4255 }
4256
4257 $age_qpart = getMaxAgeSubquery();
4258
4259 $query = "SELECT ttrss_feeds.*,
4260 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated_noms,
4261 cat_id,last_error,
4262 ttrss_feed_categories.title AS category,
4263 ttrss_feed_categories.collapsed,
4264 value AS unread
4265 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
4266 ON (ttrss_feed_categories.id = cat_id)
4267 LEFT JOIN ttrss_counters_cache
4268 ON
4269 (ttrss_feeds.id = feed_id)
4270 WHERE
4271 ttrss_feeds.hidden = false AND
4272 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
4273 ORDER BY $order_by_qpart";
4274
4275 $result = db_query($link, $query);
4276
4277 $actid = $_GET["actid"];
4278
4279 /* real feeds */
4280
4281 $lnum = 0;
4282
4283 $total_unread = 0;
4284
4285 $category = "";
4286
4287 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4288
4289 while ($line = db_fetch_assoc($result)) {
4290
4291 $feed = trim($line["title"]);
4292
4293 if (!$feed) $feed = "[Untitled]";
4294
4295 $feed_id = $line["id"];
4296 $unread = $line["unread"];
4297
4298 $subop = $_GET["subop"];
4299
4300 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4301 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
4302 } else {
4303 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
4304 }
4305
4306 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
4307
4308 if ($rtl_content) {
4309 $rtl_tag = "dir=\"RTL\"";
4310 } else {
4311 $rtl_tag = "";
4312 }
4313
4314 $tmp_result = db_query($link,
4315 "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache
4316 WHERE parent_feed = '$feed_id' AND feed_id = id");
4317
4318 $unread += db_fetch_result($tmp_result, 0, "unread");
4319
4320 $cat_id = $line["cat_id"];
4321
4322 $tmp_category = $line["category"];
4323
4324 if (!$tmp_category) {
4325 $tmp_category = __("Uncategorized");
4326 }
4327
4328 // $class = ($lnum % 2) ? "even" : "odd";
4329
4330 if ($line["last_error"]) {
4331 $class = "error";
4332 } else {
4333 $class = "feed";
4334 }
4335
4336 if ($unread > 0) $class .= "Unread";
4337
4338 if ($actid == $feed_id) {
4339 $class .= "Selected";
4340 }
4341
4342 $total_unread += $unread;
4343
4344 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
4345
4346 if ($category) {
4347 print "</ul></li>";
4348 }
4349
4350 $category = $tmp_category;
4351
4352 $collapsed = sql_bool_to_bool($line["collapsed"]);
4353
4354 // workaround for NULL category
4355 if ($category == __("Uncategorized")) {
4356 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
4357 $collapsed = "t";
4358 }
4359 }
4360
4361 $cat_id = sprintf("%d", $cat_id);
4362
4363 printCategoryHeader($link, $cat_id, $collapsed, true);
4364
4365 }
4366
4367 printFeedEntry($feed_id, $class, $feed, $unread,
4368 ICONS_URL."/$feed_id.ico", $link, $rtl_content,
4369 $last_updated, $line["last_error"]);
4370
4371 ++$lnum;
4372 }
4373
4374 if (db_num_rows($result) == 0) {
4375 print "<li>".__('No feeds to display.')."</li>";
4376 }
4377
4378 } else {
4379
4380 // tags
4381
4382 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
4383 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
4384 post_int_id = ttrss_user_entries.int_id AND
4385 unread = true AND ref_id = ttrss_entries.id
4386 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
4387 UNION
4388 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
4389 ORDER BY tag_name"); */
4390
4391 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4392 print "<li class=\"feedCat\">".__('Tags')."</li>";
4393 print "<ul class=\"feedCatList\">";
4394 }
4395
4396 $age_qpart = getMaxAgeSubquery();
4397
4398 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
4399 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
4400 AND ref_id = id AND $age_qpart
4401 AND unread = true)) AS count FROM ttrss_tags
4402 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
4403 ORDER BY count DESC LIMIT 50");
4404
4405 $tags = array();
4406
4407 while ($line = db_fetch_assoc($result)) {
4408 $tags[$line["tag_name"]] += $line["count"];
4409 }
4410
4411 foreach (array_keys($tags) as $tag) {
4412
4413 $unread = $tags[$tag];
4414
4415 $class = "tag";
4416
4417 if ($unread > 0) {
4418 $class .= "Unread";
4419 }
4420
4421 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
4422
4423 }
4424
4425 if (db_num_rows($result) == 0) {
4426 print "<li>No tags to display.</li>";
4427 }
4428
4429 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4430 print "</ul>";
4431 }
4432
4433 }
4434
4435 print "</ul>";
4436
4437 }
4438
4439 function get_article_tags($link, $id, $owner_uid = 0) {
4440
4441 $a_id = db_escape_string($id);
4442
4443 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4444
4445 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
4446 owner_uid as owner FROM
4447 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
4448 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
4449
4450 $tags = array();
4451
4452 while ($tmp_line = db_fetch_assoc($tmp_result)) {
4453 array_push($tags, $tmp_line["tag_name"]);
4454 }
4455
4456 return $tags;
4457 }
4458
4459 function trim_value(&$value) {
4460 $value = trim($value);
4461 }
4462
4463 function trim_array($array) {
4464 $tmp = $array;
4465 array_walk($tmp, 'trim_value');
4466 return $tmp;
4467 }
4468
4469 function tag_is_valid($tag) {
4470 if ($tag == '') return false;
4471 if (preg_match("/^[0-9]*$/", $tag)) return false;
4472
4473 if (function_exists('iconv')) {
4474 $tag = iconv("utf-8", "utf-8", $tag);
4475 }
4476
4477 if (!$tag) return false;
4478
4479 return true;
4480 }
4481
4482 function render_login_form($link, $mobile = false) {
4483 if (!$mobile) {
4484 require_once "login_form.php";
4485 } else {
4486 require_once "mobile/login_form.php";
4487 }
4488 }
4489
4490 // from http://developer.apple.com/internet/safari/faq.html
4491 function no_cache_incantation() {
4492 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
4493 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
4494 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
4495 header("Cache-Control: post-check=0, pre-check=0", false);
4496 header("Pragma: no-cache"); // HTTP/1.0
4497 }
4498
4499 function format_warning($msg, $id = "") {
4500 return "<div class=\"warning\" id=\"$id\">
4501 <img src=\"images/sign_excl.gif\">$msg</div>";
4502 }
4503
4504 function format_notice($msg) {
4505 return "<div class=\"notice\">
4506 <img src=\"images/sign_info.gif\">$msg</div>";
4507 }
4508
4509 function format_error($msg) {
4510 return "<div class=\"error\">
4511 <img src=\"images/sign_excl.gif\">$msg</div>";
4512 }
4513
4514 function print_notice($msg) {
4515 return print format_notice($msg);
4516 }
4517
4518 function print_warning($msg) {
4519 return print format_warning($msg);
4520 }
4521
4522 function print_error($msg) {
4523 return print format_error($msg);
4524 }
4525
4526
4527 function T_sprintf() {
4528 $args = func_get_args();
4529 return vsprintf(__(array_shift($args)), $args);
4530 }
4531
4532 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true,
4533 $zoom_mode = false) {
4534
4535 /* we can figure out feed_id from article id anyway, why do we
4536 * pass feed_id here? */
4537
4538 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
4539 WHERE ref_id = '$id'");
4540
4541 $feed_id = db_fetch_result($result, 0, "feed_id");
4542
4543 if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
4544
4545 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4546 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
4547
4548 if (db_num_rows($result) == 1) {
4549 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4550 } else {
4551 $rtl_content = false;
4552 }
4553
4554 if ($rtl_content) {
4555 $rtl_tag = "dir=\"RTL\"";
4556 $rtl_class = "RTL";
4557 } else {
4558 $rtl_tag = "";
4559 $rtl_class = "";
4560 }
4561
4562 if ($mark_as_read) {
4563 $result = db_query($link, "UPDATE ttrss_user_entries
4564 SET unread = false,last_read = NOW()
4565 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4566
4567 ccache_update($link, $feed_id, $_SESSION["uid"]);
4568 }
4569
4570 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
4571 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
4572 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
4573 num_comments,
4574 author
4575 FROM ttrss_entries,ttrss_user_entries
4576 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
4577
4578 if ($result) {
4579
4580 $link_target = "";
4581
4582 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4583 $link_target = "target=\"_blank\"";
4584 }
4585
4586 $line = db_fetch_assoc($result);
4587
4588 if ($line["icon_url"]) {
4589 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
4590 } else {
4591 $feed_icon = "&nbsp;";
4592 }
4593
4594 $num_comments = $line["num_comments"];
4595 $entry_comments = "";
4596
4597 if ($num_comments > 0) {
4598 if ($line["comments"]) {
4599 $comments_url = $line["comments"];
4600 } else {
4601 $comments_url = $line["link"];
4602 }
4603 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
4604 } else {
4605 if ($line["comments"] && $line["link"] != $line["comments"]) {
4606 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
4607 }
4608 }
4609
4610 if ($zoom_mode) {
4611 header("Content-Type: text/html");
4612 print "<html><head>
4613 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
4614 <title>Tiny Tiny RSS - ".$line["title"]."</title>
4615 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\">
4616 </head><body>";
4617 }
4618
4619
4620 print "<div class=\"postReply\">";
4621
4622 print "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\"
4623 onmouseout=\"enable_resize(false)\">";
4624
4625 $entry_author = $line["author"];
4626
4627 if ($entry_author) {
4628 $entry_author = __(" - ") . $entry_author;
4629 }
4630
4631 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4632 strtotime($line["updated"]));
4633
4634 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
4635
4636 if ($line["link"]) {
4637 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
4638 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
4639 } else {
4640 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
4641 }
4642
4643 $tags = get_article_tags($link, $id);
4644
4645 $tags_str = "";
4646 $tags_nolinks_str = "";
4647 $f_tags_str = "";
4648
4649 $num_tags = 0;
4650
4651 if ($_SESSION["theme"] == "3pane") {
4652 $tag_limit = 3;
4653 } else {
4654 $tag_limit = 6;
4655 }
4656
4657 foreach ($tags as $tag) {
4658 $num_tags++;
4659 $tag_escaped = str_replace("'", "\\'", $tag);
4660
4661 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
4662
4663 if ($num_tags == $tag_limit) {
4664 $tags_str .= "&hellip;";
4665 $tags_nolinks_str .= "&hellip;";
4666
4667 } else if ($num_tags < $tag_limit) {
4668 $tags_str .= $tag_str;
4669 $tags_nolinks_str .= "$tag, ";
4670 }
4671 $f_tags_str .= $tag_str;
4672 }
4673
4674 $tags_str = preg_replace("/, $/", "", $tags_str);
4675 $tags_nolinks_str = preg_replace("/, $/", "", $tags_nolinks_str);
4676 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4677
4678 $all_tags_div = "<span class='cdmAllTagsCtr'>&hellip;<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
4679 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4680
4681 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
4682
4683 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
4684 if (!$tags_nolinks_str) $tags_nolinks_str = '<span class="tagList">'.__('no tags').'</span>';
4685
4686 print "<div style='float : right'>
4687 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>";
4688
4689 if (!$zoom_mode) {
4690 print "$tags_str
4691 <a title=\"".__('Edit tags for this article')."\"
4692 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a>";
4693
4694 if (defined('_ENABLE_INLINE_VIEW')) {
4695
4696 print "<img src=\"images/art-inline.png\" class='tagsPic'
4697 style=\"cursor : pointer\" style=\"cursor : pointer\"
4698 onclick=\"showOriginalArticleInline($id)\"
4699 alt='Inline' title='".__('Display original article content')."'>";
4700
4701 }
4702
4703 print "<img src=\"images/art-zoom.png\" class='tagsPic'
4704 style=\"cursor : pointer\" style=\"cursor : pointer\"
4705 onclick=\"zoomToArticle($id)\"
4706 alt='Zoom' title='".__('Show article summary in new window')."'>";
4707 } else {
4708 print "$tags_nolinks_str";
4709 }
4710 print "</div>";
4711 print "<div clear='both'>$entry_comments</div>";
4712
4713 print "</div>";
4714
4715 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4716
4717 print "<div class=\"postContent\">";
4718
4719 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
4720
4721 $article_content = sanitize_rss($link, $line["content"]);
4722
4723 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4724 $article_content = preg_replace("/href=/i", "target=\"_blank\" href=",
4725 $article_content);
4726 }
4727
4728 print $article_content;
4729
4730 $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4731 post_id = '$id' AND content_url != ''");
4732
4733 if (db_num_rows($result) > 0) {
4734
4735 $entries_html = array();
4736 $entries = array();
4737
4738 while ($line = db_fetch_assoc($result)) {
4739
4740 $url = $line["content_url"];
4741 $ctype = $line["content_type"];
4742
4743 if (!$ctype) $ctype = __("unknown type");
4744
4745 $filename = substr($url, strrpos($url, "/")+1);
4746
4747 $entry = "";
4748
4749 if (($ctype == __("audio/mpeg")) &&
4750 (get_pref($link, "ENABLE_FLASH_PLAYER")) ) {
4751
4752 $entry .= "<object type=\"application/x-shockwave-flash\" data=\"extras/button/musicplayer.swf?song_url=$url\" width=\"17\" height=\"17\"> <param name=\"movie\" value=\"extras/button/musicplayer.swf?song_url=$url\" /> </object> ";
4753
4754 }
4755
4756 $entry .= "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4757 $filename . " (" . $ctype . ")" . "</a>";
4758
4759 array_push($entries_html, $entry);
4760
4761 $entry = array();
4762
4763 $entry["type"] = $ctype;
4764 $entry["filename"] = $filename;
4765 $entry["url"] = $url;
4766
4767 array_push($entries, $entry);
4768 }
4769
4770 print "<div class=\"postEnclosures\">";
4771
4772 if (!preg_match("/img/i", $article_content)) {
4773 foreach ($entries as $entry) {
4774 if (preg_match("/image/", $entry["type"])) {
4775 print "<p><img
4776 alt=\"".htmlspecialchars($entry["filename"])."\"
4777 src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
4778 }
4779 }
4780 }
4781
4782 print "<div class=\"postEnclosures\">";
4783
4784 if (db_num_rows($result) == 1) {
4785 print __("Attachment:") . " ";
4786 } else {
4787 print __("Attachments:") . " ";
4788 }
4789
4790 print join(", ", $entries_html);
4791
4792 print "</div>";
4793 }
4794
4795 print "</div>";
4796
4797 print "</div>";
4798
4799 }
4800
4801 if (!$zoom_mode) {
4802 print "]]></article>";
4803 } else {
4804 print "
4805 <div style=\"text-align : center\">
4806 <input type=\"submit\" onclick=\"return window.close()\"
4807 value=\"".__("Close this window")."\"></div>";
4808 print "</body></html>";
4809
4810 }
4811
4812 }
4813
4814 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
4815 $next_unread_feed, $offset, $vgr_last_feed = false,
4816 $override_order = false) {
4817
4818 $disable_cache = false;
4819
4820 $timing_info = getmicrotime();
4821
4822 $topmost_article_ids = array();
4823
4824 if (!$offset) {
4825 $offset = 0;
4826 }
4827
4828 if ($subop == "undefined") $subop = "";
4829
4830 $subop_split = split(":", $subop);
4831
4832 if ($subop == "CatchupSelected") {
4833 $ids = split(",", db_escape_string($_GET["ids"]));
4834 $cmode = sprintf("%d", $_GET["cmode"]);
4835
4836 catchupArticlesById($link, $ids, $cmode);
4837 }
4838
4839 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
4840 update_generic_feed($link, $feed, $cat_view, true);
4841 }
4842
4843 if ($subop == "MarkAllRead") {
4844 catchup_feed($link, $feed, $cat_view);
4845
4846 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4847 if ($next_unread_feed) {
4848 $feed = $next_unread_feed;
4849 }
4850 }
4851 }
4852
4853 if ($subop_split[0] == "MarkAllReadGR") {
4854 catchup_feed($link, $subop_split[1], false);
4855 }
4856
4857
4858 if ($feed_id > 0) {
4859 $result = db_query($link,
4860 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4861
4862 if (db_num_rows($result) == 0) {
4863 print "<div align='center'>".__('Feed not found.')."</div>";
4864 return;
4865 }
4866 }
4867
4868 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
4869
4870 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4871 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4872
4873 if (db_num_rows($result) == 1) {
4874 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4875 } else {
4876 $rtl_content = false;
4877 }
4878
4879 if ($rtl_content) {
4880 $rtl_tag = "dir=\"RTL\"";
4881 } else {
4882 $rtl_tag = "";
4883 }
4884 } else {
4885 $rtl_tag = "";
4886 $rtl_content = false;
4887 }
4888
4889 $script_dt_add = get_script_dt_add();
4890
4891 /// START /////////////////////////////////////////////////////////////////////////////////
4892
4893 $search = db_escape_string($_GET["query"]);
4894
4895 if ($search) {
4896 $disable_cache = true;
4897 }
4898
4899 $search_mode = db_escape_string($_GET["search_mode"]);
4900 $match_on = db_escape_string($_GET["match_on"]);
4901
4902 if (!$match_on) {
4903 $match_on = "both";
4904 }
4905
4906 $real_offset = $offset * $limit;
4907
4908 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4909
4910 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
4911 $search, $search_mode, $match_on, $override_order, $real_offset);
4912
4913 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4914
4915 $result = $qfh_ret[0];
4916 $feed_title = $qfh_ret[1];
4917 $feed_site_url = $qfh_ret[2];
4918 $last_error = $qfh_ret[3];
4919
4920 $vgroup_last_feed = $vgr_last_feed;
4921
4922 if ($feed == -2) {
4923 $feed_site_url = article_publish_url($link);
4924 }
4925
4926 /// STOP //////////////////////////////////////////////////////////////////////////////////
4927
4928 if (!$offset) {
4929 print "<div id=\"headlinesContainer\" $rtl_tag>";
4930
4931 if (!$result) {
4932 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4933 return;
4934 }
4935
4936 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4937 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4938 $offset, $limit);
4939
4940 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4941 }
4942
4943 $headlines_count = db_num_rows($result);
4944
4945 if (db_num_rows($result) > 0) {
4946
4947 # print "\{$offset}";
4948
4949 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
4950 print "<table class=\"headlinesList\" id=\"headlinesList\"
4951 cellspacing=\"0\">";
4952 }
4953
4954 $lnum = $limit*$offset;
4955
4956 error_reporting (DEFAULT_ERROR_LEVEL);
4957
4958 $num_unread = 0;
4959 $cur_feed_title = '';
4960
4961 while ($line = db_fetch_assoc($result)) {
4962
4963 $class = ($lnum % 2) ? "even" : "odd";
4964
4965 $id = $line["id"];
4966 $feed_id = $line["feed_id"];
4967
4968 $labels = get_article_labels($link, $id);
4969 $labels_str = "<span id=\"HLLCTR-$id\">";
4970
4971 foreach ($labels as $l) {
4972 $labels_str .= "<span
4973 class='hlLabelRef'>".
4974 $l[1]."</span>";
4975 }
4976
4977 $labels_str .= "</span>";
4978
4979 if (count($topmost_article_ids) < 5) {
4980 array_push($topmost_article_ids, $id);
4981 }
4982
4983 if ($line["last_read"] == "" &&
4984 ($line["unread"] != "t" && $line["unread"] != "1")) {
4985
4986 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
4987 alt=\"Updated\">";
4988 } else {
4989 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
4990 alt=\"Updated\">";
4991 }
4992
4993 if ($line["unread"] == "t" || $line["unread"] == "1") {
4994 $class .= "Unread";
4995 ++$num_unread;
4996 $is_unread = true;
4997 } else {
4998 $is_unread = false;
4999 }
5000
5001 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
5002
5003 if ($is_ie) {
5004 $mark_img_ext = "gif";
5005 } else {
5006 $mark_img_ext = "png";
5007 }
5008
5009 if ($line["marked"] == "t" || $line["marked"] == "1") {
5010 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\"
5011 class=\"markedPic\"
5012 alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
5013 } else {
5014 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\"
5015 class=\"markedPic\"
5016 alt=\"Star article\" onclick='javascript:tMark($id)'>";
5017 }
5018
5019 if ($line["published"] == "t" || $line["published"] == "1") {
5020 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\"
5021 class=\"markedPic\"
5022 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
5023 } else {
5024 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\"
5025 class=\"markedPic\"
5026 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
5027 }
5028
5029 # $content_link = "<a target=\"_blank\" href=\"".$line["link"]."\">" .
5030 # $line["title"] . "</a>";
5031
5032 # $content_link = "<a
5033 # href=\"" . htmlspecialchars($line["link"]) . "\"
5034 # onclick=\"view($id,$feed_id);\">" .
5035 # $line["title"] . "</a>";
5036
5037 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
5038 # $line["title"] . "</a>";
5039
5040 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
5041 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
5042 } else {
5043 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
5044 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
5045 }
5046
5047 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
5048 $content_preview = truncate_string(strip_tags($line["content_preview"]),
5049 100);
5050 }
5051
5052 $score = $line["score"];
5053
5054 $score_pic = get_score_pic($score);
5055
5056 /* $score_title = __("(Click to change)");
5057 $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\"
5058 onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">"; */
5059
5060 $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\"
5061 title=\"$score\">";
5062
5063 if ($score > 500) {
5064 $hlc_suffix = "H";
5065 } else if ($score < -100) {
5066 $hlc_suffix = "L";
5067 } else {
5068 $hlc_suffix = "";
5069 }
5070
5071 $entry_author = $line["author"];
5072
5073 if ($entry_author) {
5074 $entry_author = " - $entry_author";
5075 }
5076
5077 $has_feed_icon = feed_has_icon($feed_id);
5078
5079 if ($has_feed_icon) {
5080 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
5081 } else {
5082 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
5083 $feed_icon_img = "";
5084 }
5085
5086 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
5087
5088 if (get_pref($link, 'VFEED_GROUP_BY_FEED')) {
5089 if ($feed_id != $vgroup_last_feed && $line["feed_title"]) {
5090
5091 $cur_feed_title = $line["feed_title"];
5092 $vgroup_last_feed = $feed_id;
5093
5094 $cur_feed_title = htmlspecialchars($cur_feed_title);
5095
5096 $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>mark as read</a>)";
5097
5098 print "<tr class='feedTitle'><td colspan='7'>".
5099 "<div style=\"float : right\">$feed_icon_img</div>".
5100 "<a href=\"javascript:viewfeed($feed_id, '', false)\">".
5101 $line["feed_title"]."</a> $vf_catchup_link</td></tr>";
5102 }
5103 }
5104
5105 $mouseover_attrs = "onmouseover='postMouseIn($id)'
5106 onmouseout='postMouseOut($id)'";
5107
5108 print "<tr class='$class' id='RROW-$id' $mouseover_attrs>";
5109
5110 print "<td class='hlUpdPic'>$update_pic</td>";
5111
5112 print "<td class='hlSelectRow'>
5113 <input type=\"checkbox\" onclick=\"tSR(this)\"
5114 id=\"RCHK-$id\">
5115 </td>";
5116
5117 print "<td class='hlMarkedPic'>$marked_pic</td>";
5118 print "<td class='hlMarkedPic'>$published_pic</td>";
5119
5120 # if ($line["feed_title"]) {
5121 # print "<td class='hlContent'>$content_link</td>";
5122 # print "<td class='hlFeed'>
5123 # <a href=\"javascript:viewfeed($feed_id, '', false)\">".
5124 # truncate_string($line["feed_title"],30)."</a>&nbsp;</td>";
5125 # } else {
5126
5127 print "<td onclick='view($id,$feed_id)' class='hlContent$hlc_suffix' valign='middle'>";
5128
5129 print "<a id=\"RTITLE-$id\"
5130 href=\"" . htmlspecialchars($line["link"]) . "\"
5131 onclick=\"return view($id,$feed_id);\">" .
5132 $line["title"];
5133
5134 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
5135 if ($content_preview) {
5136 print "<span class=\"contentPreview\"> - $content_preview</span>";
5137 }
5138 }
5139
5140 print "</a>";
5141
5142 print $labels_str;
5143
5144 # <a href=\"javascript:viewfeed($feed_id, '', false)\">".
5145 # $line["feed_title"]."</a>
5146
5147 if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
5148 if ($line["feed_title"]) {
5149 print "<span class=\"hlFeed\">
5150 (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
5151 $line["feed_title"]."</a>)
5152 </span>";
5153 }
5154 }
5155 print "</td>";
5156
5157 # }
5158
5159 print "<td class=\"hlUpdated\" onclick='view($id,$feed_id)'><nobr>$updated_fmt&nbsp;</nobr></td>";
5160
5161 print "<td class='hlMarkedPic'>$score_pic</td>";
5162
5163 if ($line["feed_title"] && !get_pref($link, 'VFEED_GROUP_BY_FEED')) {
5164 print "<td onclick=\"viewfeed($feed_id)\" class=\"hlFeedIcon\">$feed_icon_img</td>";
5165 }
5166
5167 print "</tr>";
5168
5169 } else {
5170
5171 if (get_pref($link, 'VFEED_GROUP_BY_FEED') && $line["feed_title"]) {
5172 if ($feed_id != $vgroup_last_feed) {
5173
5174 $cur_feed_title = $line["feed_title"];
5175 $vgroup_last_feed = $feed_id;
5176
5177 $cur_feed_title = htmlspecialchars($cur_feed_title);
5178
5179 $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>mark as read</a>)";
5180
5181 $has_feed_icon = feed_has_icon($feed_id);
5182
5183 if ($has_feed_icon) {
5184 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
5185 } else {
5186 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
5187 }
5188
5189 print "<div class='cdmFeedTitle'>".
5190 "<div style=\"float : right\">$feed_icon_img</div>".
5191 "<a href=\"javascript:viewfeed($feed_id, '', false)\">".
5192 $line["feed_title"]."</a> $vf_catchup_link</div>";
5193 }
5194 }
5195
5196 if ($is_unread) {
5197 $add_class = "Unread";
5198 } else {
5199 $add_class = "";
5200 }
5201
5202 $expand_cdm = get_pref($link, 'CDM_EXPANDED');
5203 $show_excerpt = false;
5204
5205 if ($expand_cdm && $score >= -100) {
5206 $cdm_cstyle = "";
5207 $show_excerpt = false;
5208 } else {
5209 $cdm_cstyle = "style=\"display : none\"";
5210 $show_excerpt = true;
5211 }
5212
5213 $mouseover_attrs = "onmouseover='postMouseIn($id)'
5214 onmouseout='postMouseOut($id)'";
5215
5216 print "<div class=\"cdmArticle$add_class\"
5217 id=\"RROW-$id\"
5218 $mouseover_attrs'>";
5219
5220 print "<div class=\"cdmHeader\">";
5221
5222 if (!get_pref($link, "VFEED_GROUP_BY_FEED") || !$line["feed_title"]) {
5223 $cdm_feed_icon = "<span style=\"cursor : pointer\" onclick=\"viewfeed($feed_id)\">$feed_icon_img</span>";
5224 }
5225
5226 print "<div class=\"articleUpdated\">$updated_fmt $score_pic $cdm_feed_icon
5227 </div>";
5228
5229 print "<span id=\"RTITLE-$id\" class=\"titleWrap$hlc_suffix\"><a class=\"title\"
5230 onclick=\"javascript:toggleUnread($id, 0)\"
5231 target=\"_blank\" href=\"".$line["link"]."\">".$line["title"]."</a>
5232 ";
5233
5234 print $entry_author;
5235
5236 /* if (!$expand_cdm || $score < -100) {
5237 print "&nbsp;<a id=\"CICH-$id\"
5238 href=\"javascript:cdmExpandArticle($id)\">
5239 (".__('Show article').")</a>";
5240 } */
5241
5242 print $labels_str;
5243
5244 if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
5245 if ($line["feed_title"]) {
5246 print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
5247 }
5248 }
5249
5250 print "</span></div>";
5251
5252 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
5253 $line["content_preview"] = preg_replace("/href=/i",
5254 "target=\"_blank\" href=", $line["content_preview"]);
5255 }
5256
5257 if ($show_excerpt) {
5258 print "<div class=\"cdmExcerpt\" id=\"CEXC-$id\"
5259 onclick=\"cdmExpandArticle($id)\"
5260 title=\"".__('Click to expand article')."\">";
5261 print truncate_string(strip_tags($line["content_preview"]), 100);
5262 print "</div>";
5263 }
5264
5265 print "<div class=\"cdmContent\"
5266 onclick=\"cdmClicked($id)\"
5267 id=\"CICD-$id\" $cdm_cstyle>";
5268
5269 // print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>";
5270
5271 print sanitize_rss($link, $line["content_preview"]);
5272 $article_content = $line["content_preview"];
5273
5274 $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
5275 post_id = '$id' AND content_url != ''");
5276
5277 if (db_num_rows($e_result) > 0) {
5278
5279 $entries_html = array();
5280 $entries = array();
5281
5282 while ($e_line = db_fetch_assoc($e_result)) {
5283
5284 $url = $e_line["content_url"];
5285 $ctype = $e_line["content_type"];
5286 if (!$ctype) $ctype = __("unknown type");
5287
5288 $filename = substr($url, strrpos($url, "/")+1);
5289
5290 $entry = "";
5291
5292 if (($ctype == __("audio/mpeg")) &&
5293 (get_pref($link, "ENABLE_FLASH_PLAYER")) ) {
5294
5295 $entry .= "<object type=\"application/x-shockwave-flash\" data=\"extras/button/musicplayer.swf?song_url=$url\" width=\"17\" height=\"17\"> <param name=\"movie\" value=\"extras/button/musicplayer.swf?song_url=$url\" /> </object> ";
5296
5297 }
5298
5299 $entry .= "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
5300 $filename . " (" . $ctype . ")" . "</a>";
5301
5302 array_push($entries_html, $entry);
5303
5304 $entry = array();
5305
5306 $entry["type"] = $ctype;
5307 $entry["filename"] = $filename;
5308 $entry["url"] = $url;
5309
5310 array_push($entries, $entry);
5311 }
5312
5313 if (!preg_match("/img/i", $article_content)) {
5314 foreach ($entries as $entry) {
5315 if (preg_match("/image/", $entry["type"])) {
5316 print "<p><img
5317 alt=\"".htmlspecialchars($entry["filename"])."\"
5318 src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
5319 }
5320 }
5321 }
5322
5323 print "<div class=\"cdmEnclosures\">";
5324
5325 if (db_num_rows($e_result) == 1) {
5326 print __("Attachment:") . " ";
5327 } else {
5328 print __("Attachments:") . " ";
5329 }
5330
5331 print join(", ", $entries_html);
5332
5333 print "</div>";
5334 }
5335
5336
5337 print "<br clear='both'>";
5338 // print "</div>";
5339
5340 /* if (!$expand_cdm) {
5341 print "<a id=\"CICH-$id\"
5342 href=\"javascript:cdmExpandArticle($id)\">
5343 Show article</a>";
5344 } */
5345
5346 print "</div>";
5347
5348 print "<div class=\"cdmFooter\"><span class='s0'>";
5349
5350 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
5351
5352 print __("Select:").
5353 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
5354 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
5355
5356 print "</span><span class='s1'>$marked_pic</span> ";
5357 print "<span class='s1'>$published_pic</span> ";
5358 print "<span class='s1'><img src=\"images/art-zoom.png\" class='tagsPic'
5359 onclick=\"zoomToArticle($id)\"
5360 style=\"cursor : pointer\"
5361 alt='Zoom'
5362 title='".__('Show article summary in new window')."'></span>";
5363
5364 $tags = get_article_tags($link, $id);
5365
5366 $tags_str = "";
5367 $full_tags_str = "";
5368 $num_tags = 0;
5369
5370 foreach ($tags as $tag) {
5371 $num_tags++;
5372 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
5373 if ($num_tags < 5) {
5374 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
5375 } else if ($num_tags == 5) {
5376 $tags_str .= "&hellip;";
5377 }
5378 }
5379
5380 $tags_str = preg_replace("/, $/", "", $tags_str);
5381 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
5382
5383 $all_tags_div = "<span class='cdmAllTagsCtr'>&hellip;<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
5384
5385 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
5386
5387
5388 if ($tags_str == "") $tags_str = "no tags";
5389
5390 // print "<img src='images/tag.png' class='markedPic'>";
5391
5392 print "<span class='s1'>
5393 <img class='tagsPic' src='images/tag.png' alt='Tags'
5394 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
5395 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
5396
5397 print "</span>";
5398
5399 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
5400 href=\"javascript:toggleUnread($id)\">
5401 Unread</a></span>";
5402
5403 print "</div>";
5404 print "</div>";
5405
5406 }
5407
5408 ++$lnum;
5409 }
5410
5411 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
5412 print "</table>";
5413 }
5414
5415 } else {
5416 $message = "";
5417
5418 switch ($view_mode) {
5419 case "unread":
5420 $message = __("No unread articles found to display.");
5421 break;
5422 case "updated":
5423 $message = __("No updated articles found to display.");
5424 break;
5425 case "marked":
5426 $message = __("No starred articles found to display.");
5427 break;
5428 default:
5429 if ($feed < -10) {
5430 $message = __("No articles found to display. You can assign articles to labels manually (see the Actions menu above) or use a filter.");
5431 } else {
5432 $message = __("No articles found to display.");
5433 }
5434 }
5435
5436 if (!$offset) print "<div class='whiteBox'>$message</div>";
5437 }
5438
5439 if (!$offset) {
5440 print "</div>";
5441 print "</div>";
5442 }
5443
5444 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache, $vgroup_last_feed);
5445 }
5446
5447 // from here: http://www.roscripts.com/Create_tag_cloud-71.html
5448
5449 function printTagCloud($link) {
5450
5451 $query = "SELECT tag_name, COUNT(post_int_id) AS count
5452 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
5453 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
5454
5455 $result = db_query($link, $query);
5456
5457 $tags = array();
5458
5459 while ($line = db_fetch_assoc($result)) {
5460 $tags[$line["tag_name"]] = $line["count"];
5461 }
5462
5463 ksort($tags);
5464
5465 $max_size = 32; // max font size in pixels
5466 $min_size = 11; // min font size in pixels
5467
5468 // largest and smallest array values
5469 $max_qty = max(array_values($tags));
5470 $min_qty = min(array_values($tags));
5471
5472 // find the range of values
5473 $spread = $max_qty - $min_qty;
5474 if ($spread == 0) { // we don't want to divide by zero
5475 $spread = 1;
5476 }
5477
5478 // set the font-size increment
5479 $step = ($max_size - $min_size) / ($spread);
5480
5481 // loop through the tag array
5482 foreach ($tags as $key => $value) {
5483 // calculate font-size
5484 // find the $value in excess of $min_qty
5485 // multiply by the font-size increment ($size)
5486 // and add the $min_size set above
5487 $size = round($min_size + (($value - $min_qty) * $step));
5488
5489 $key_escaped = str_replace("'", "\\'", $key);
5490
5491 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
5492 $size . "px\" title=\"$value articles tagged with " .
5493 $key . '">' . $key . '</a> ';
5494 }
5495 }
5496
5497 function print_checkpoint($n, $s) {
5498 $ts = getmicrotime();
5499 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
5500 return $ts;
5501 }
5502
5503 function sanitize_tag($tag) {
5504 $tag = trim($tag);
5505
5506 $tag = mb_strtolower($tag, 'utf-8');
5507
5508 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
5509
5510 // $tag = str_replace('"', "", $tag);
5511 // $tag = str_replace("+", " ", $tag);
5512 $tag = str_replace("technorati tag: ", "", $tag);
5513
5514 return $tag;
5515 }
5516
5517 function generate_publish_key() {
5518 return sha1(uniqid(rand(), true));
5519 }
5520
5521 function article_publish_url($link) {
5522
5523 $url_path = "";
5524
5525
5526 if ($_SERVER['HTTPS'] != "on") {
5527 $url_path = "http://";
5528 } else {
5529 $url_path = "https://";
5530 }
5531
5532 $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
5533 $url_path .= "/backend.php?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
5534
5535 return $url_path;
5536 }
5537
5538 /**
5539 * Purge a feed contents, marked articles excepted.
5540 *
5541 * @param mixed $link The database connection.
5542 * @param integer $id The id of the feed to purge.
5543 * @return void
5544 */
5545 function clear_feed_articles($link, $id) {
5546 $result = db_query($link, "DELETE FROM ttrss_user_entries
5547 WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
5548
5549 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
5550 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
5551
5552 ccache_update($link, $id, $_SESSION['uid']);
5553 } // function clear_feed_articles
5554
5555 /**
5556 * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI.
5557 *
5558 * @return string The Mozilla Firefox feed adding URL.
5559 */
5560 function add_feed_url() {
5561 $url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
5562 $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
5563 return $url_path;
5564 } // function add_feed_url
5565
5566 /**
5567 * Encrypt a password in SHA1.
5568 *
5569 * @param string $pass The password to encrypt.
5570 * @param string $login A optionnal login.
5571 * @return string The encrypted password.
5572 */
5573 function encrypt_password($pass, $login = '') {
5574 if ($login) {
5575 return "SHA1X:" . sha1("$login:$pass");
5576 } else {
5577 return "SHA1:" . sha1($pass);
5578 }
5579 } // function encrypt_password
5580
5581 /**
5582 * Update a feed batch.
5583 * Used by daemons to update n feeds by run.
5584 * Only update feed needing a update, and not being processed
5585 * by another process.
5586 *
5587 * @param mixed $link Database link
5588 * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT.
5589 * @param boolean $from_http Set to true if you call this function from http to disable cli specific code.
5590 * @param boolean $debug Set to false to disable debug output. Default to true.
5591 * @return void
5592 */
5593 function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) {
5594 // Process all other feeds using last_updated and interval parameters
5595
5596 // Test if the user has loggued in recently. If not, it does not update its feeds.
5597 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
5598 if (DB_TYPE == "pgsql") {
5599 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
5600 } else {
5601 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
5602 }
5603 } else {
5604 $login_thresh_qpart = "";
5605 }
5606
5607 // Test if the feed need a update (update interval exceded).
5608 if (DB_TYPE == "pgsql") {
5609 $update_limit_qpart = "AND ((
5610 ttrss_feeds.update_interval = 0
5611 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
5612 ) OR (
5613 ttrss_feeds.update_interval > 0
5614 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
5615 ) OR ttrss_feeds.last_updated IS NULL)";
5616 } else {
5617 $update_limit_qpart = "AND ((
5618 ttrss_feeds.update_interval = 0
5619 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
5620 ) OR (
5621 ttrss_feeds.update_interval > 0
5622 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
5623 ) OR ttrss_feeds.last_updated IS NULL)";
5624 }
5625
5626 // Test if feed is currently being updated by another process.
5627 if (DB_TYPE == "pgsql") {
5628 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
5629 } else {
5630 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
5631 }
5632
5633 // Test if there is a limit to number of updated feeds
5634 $query_limit = "";
5635 if($limit) $query_limit = sprintf("LIMIT %d", $limit);
5636
5637 $random_qpart = sql_random_function();
5638
5639 // We search for feed needing update.
5640 $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid,
5641 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
5642 ttrss_feeds.update_interval
5643 FROM
5644 ttrss_feeds, ttrss_users, ttrss_user_prefs
5645 WHERE
5646 ttrss_feeds.owner_uid = ttrss_users.id
5647 AND ttrss_users.id = ttrss_user_prefs.owner_uid
5648 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
5649 $login_thresh_qpart $update_limit_qpart
5650 $updstart_thresh_qpart
5651 ORDER BY $random_qpart $query_limit");
5652
5653 $user_prefs_cache = array();
5654
5655 if($debug) _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
5656
5657 // Here is a little cache magic in order to minimize risk of double feed updates.
5658 $feeds_to_update = array();
5659 while ($line = db_fetch_assoc($result)) {
5660 $feeds_to_update[$line['id']] = $line;
5661 }
5662
5663 // We update the feed last update started date before anything else.
5664 // There is no lag due to feed contents downloads
5665 // It prevent an other process to update the same feed.
5666 $feed_ids = array_keys($feeds_to_update);
5667 if($feed_ids) {
5668 db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
5669 WHERE id IN (%s)", implode(',', $feed_ids)));
5670 }
5671
5672 // For each feed, we call the feed update function.
5673 while ($line = array_pop($feeds_to_update)) {
5674
5675 if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
5676
5677 // We setup a alarm to alert if the feed take more than 300s to update.
5678 // => HANG alarm.
5679 if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(300);
5680 update_rss_feed($link, $line["feed_url"], $line["id"], true);
5681 // Cancel the alarm (the update went well)
5682 if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(0);
5683
5684 sleep(1); // prevent flood (FIXME make this an option?)
5685 }
5686
5687 // Send feed digests by email if needed.
5688 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
5689
5690 purge_orphans($link);
5691
5692 } // function update_daemon_common
5693
5694 function sanitize_article_content($text) {
5695 # we don't support CDATA sections in articles, they break our own escaping
5696 $text = preg_replace("/\[\[CDATA/", "", $text);
5697 $text = preg_replace("/\]\]\>/", "", $text);
5698 return $text;
5699 }
5700
5701 function load_filters($link, $feed, $owner_uid, $action_id = false) {
5702 $filters = array();
5703
5704 if ($action_id) $ftype_query_part = "action_id = '$action_id' AND";
5705
5706 $result = db_query($link, "SELECT reg_exp,
5707 ttrss_filter_types.name AS name,
5708 ttrss_filter_actions.name AS action,
5709 inverse,
5710 action_param,
5711 filter_param
5712 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
5713 enabled = true AND
5714 $ftype_query_part
5715 owner_uid = $owner_uid AND
5716 ttrss_filter_types.id = filter_type AND
5717 ttrss_filter_actions.id = action_id AND
5718 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
5719
5720 while ($line = db_fetch_assoc($result)) {
5721 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
5722 $filter["reg_exp"] = $line["reg_exp"];
5723 $filter["action"] = $line["action"];
5724 $filter["action_param"] = $line["action_param"];
5725 $filter["filter_param"] = $line["filter_param"];
5726 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
5727
5728 array_push($filters[$line["name"]], $filter);
5729 }
5730
5731 return $filters;
5732 }
5733
5734 function get_score_pic($score) {
5735 if ($score > 100) {
5736 return "score_high.png";
5737 } else if ($score > 0) {
5738 return "score_half_high.png";
5739 } else if ($score < -100) {
5740 return "score_low.png";
5741 } else if ($score < 0) {
5742 return "score_half_low.png";
5743 } else {
5744 return "score_neutral.png";
5745 }
5746 }
5747
5748 function rounded_table_start($classname, $header = "&nbsp;") {
5749 print "<table width='100%' class='$classname' cellspacing='0' cellpadding='0'>";
5750 print "<tr><td class='c1'>&nbsp;</td><td class='top'>$header</td><td class='c2'>&nbsp;</td></tr>";
5751 print "<tr><td class='left'>&nbsp;</td><td class='content'>";
5752 }
5753
5754 function rounded_table_end($footer = "&nbsp;") {
5755 print "</td><td class='right'>&nbsp;</td></tr>";
5756 print "<tr><td class='c4'>&nbsp;</td><td class='bottom'>$footer</td><td class='c3'>&nbsp;</td></tr>";
5757 print "</table>";
5758 }
5759
5760 function feed_has_icon($id) {
5761 return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
5762 }
5763
5764 function init_connection($link) {
5765 if (DB_TYPE == "pgsql") {
5766 pg_query($link, "set client_encoding = 'UTF-8'");
5767 pg_set_client_encoding("UNICODE");
5768 pg_query($link, "set datestyle = 'ISO, european'");
5769 } else {
5770 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
5771 db_query($link, "SET NAMES " . MYSQL_CHARSET);
5772 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
5773 }
5774 }
5775 }
5776
5777 function update_feedbrowser_cache($link) {
5778
5779 $result = db_query($link, "SELECT feed_url,title, COUNT(id) AS subscribers
5780 FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
5781 WHERE tf.feed_url = ttrss_feeds.feed_url
5782 AND (private IS true OR feed_url LIKE '%:%@%/%'))
5783 GROUP BY feed_url, title ORDER BY subscribers DESC LIMIT 1000");
5784
5785 db_query($link, "BEGIN");
5786
5787 db_query($link, "DELETE FROM ttrss_feedbrowser_cache");
5788
5789 $count = 0;
5790
5791 while ($line = db_fetch_assoc($result)) {
5792 $subscribers = db_escape_string($line["subscribers"]);
5793 $feed_url = db_escape_string($line["feed_url"]);
5794 $title = db_escape_string($line["title"]);
5795
5796 $tmp_result = db_query($link, "SELECT subscribers FROM
5797 ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'");
5798
5799 if (db_num_rows($tmp_result) == 0) {
5800
5801 db_query($link, "INSERT INTO ttrss_feedbrowser_cache
5802 (feed_url, title, subscribers) VALUES ('$feed_url',
5803 '$title', '$subscribers')");
5804
5805 ++$count;
5806
5807 }
5808
5809 }
5810
5811 db_query($link, "COMMIT");
5812
5813 return $count;
5814
5815 }
5816
5817 function ccache_zero($link, $feed_id, $owner_uid) {
5818 db_query($link, "UPDATE ttrss_counters_cache SET
5819 value = 0, updated = NOW() WHERE
5820 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
5821 }
5822
5823 function ccache_zero_all($link, $owner_uid) {
5824 db_query($link, "UPDATE ttrss_counters_cache SET
5825 value = 0 WHERE owner_uid = '$owner_uid'");
5826
5827 db_query($link, "UPDATE ttrss_cat_counters_cache SET
5828 value = 0 WHERE owner_uid = '$owner_uid'");
5829 }
5830
5831 function ccache_update_all($link, $owner_uid) {
5832
5833 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid)) {
5834
5835 $result = db_query($link, "SELECT feed_id FROM ttrss_cat_counters_cache
5836 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
5837
5838 while ($line = db_fetch_assoc($result)) {
5839 ccache_update($link, $line["feed_id"], $owner_uid, true);
5840 }
5841
5842 /* We have to manually include category 0 */
5843
5844 ccache_update($link, 0, $owner_uid, true);
5845
5846 } else {
5847 $result = db_query($link, "SELECT feed_id FROM ttrss_counters_cache
5848 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
5849
5850 while ($line = db_fetch_assoc($result)) {
5851 print ccache_update($link, $line["feed_id"], $owner_uid);
5852
5853 }
5854
5855 }
5856 }
5857
5858 function ccache_find($link, $feed_id, $owner_uid, $is_cat = false,
5859 $no_update = false) {
5860
5861 if (!$is_cat) {
5862 $table = "ttrss_counters_cache";
5863 } else {
5864 $table = "ttrss_cat_counters_cache";
5865 }
5866
5867 if (DB_TYPE == "pgsql") {
5868 $date_qpart = "updated > NOW() - INTERVAL '15 minutes'";
5869 } else if (DB_TYPE == "mysql") {
5870 $date_qpart = "updated > DATE_SUB(NOW(), INTERVAL 15 MINUTE)";
5871 }
5872
5873 $result = db_query($link, "SELECT value FROM $table
5874 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id'
5875 LIMIT 1");
5876
5877 if (db_num_rows($result) == 1) {
5878 return db_fetch_result($result, 0, "value");
5879 } else {
5880 if ($no_update) {
5881 return -1;
5882 } else {
5883 return ccache_update($link, $feed_id, $owner_uid, $is_cat);
5884 }
5885 }
5886
5887 }
5888
5889 function ccache_update($link, $feed_id, $owner_uid, $is_cat = false,
5890 $update_pcat = true) {
5891
5892 $prev_unread = ccache_find($link, $feed_id, $owner_uid, $is_cat, true);
5893
5894 /* When updating a label, all we need to do is recalculate feed counters
5895 * because labels are not cached */
5896
5897 if ($feed_id < 0) {
5898 ccache_update_all($link, $owner_uid);
5899 return;
5900 }
5901
5902 if (!$is_cat) {
5903 $table = "ttrss_counters_cache";
5904 } else {
5905 $table = "ttrss_cat_counters_cache";
5906 }
5907
5908 if ($is_cat && $feed_id >= 0) {
5909 if ($feed_id != 0) {
5910 $cat_qpart = "cat_id = '$feed_id'";
5911 } else {
5912 $cat_qpart = "cat_id IS NULL";
5913 }
5914
5915 /* Recalculate counters for child feeds */
5916
5917 $result = db_query($link, "SELECT id FROM ttrss_feeds
5918 WHERE owner_uid = '$owner_uid' AND $cat_qpart");
5919
5920 while ($line = db_fetch_assoc($result)) {
5921 ccache_update($link, $line["id"], $owner_uid, false, false);
5922 }
5923
5924 $result = db_query($link, "SELECT SUM(value) AS sv
5925 FROM ttrss_counters_cache, ttrss_feeds
5926 WHERE id = feed_id AND $cat_qpart AND
5927 ttrss_feeds.owner_uid = '$owner_uid'");
5928
5929 $unread = (int) db_fetch_result($result, 0, "sv");
5930
5931 } else {
5932 $unread = (int) getFeedArticles($link, $feed_id, $is_cat, true, $owner_uid);
5933 }
5934
5935 $result = db_query($link, "SELECT feed_id FROM $table
5936 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1");
5937
5938 if (db_num_rows($result) == 1) {
5939 db_query($link, "UPDATE $table SET
5940 value = '$unread', updated = NOW() WHERE
5941 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
5942
5943 } else {
5944 db_query($link, "INSERT INTO $table
5945 (feed_id, value, owner_uid, updated)
5946 VALUES
5947 ($feed_id, $unread, $owner_uid, NOW())");
5948 }
5949
5950 if ($feed_id > 0 && $prev_unread != $unread) {
5951
5952 if (!$is_cat) {
5953
5954 /* Update parent category */
5955
5956 if ($update_pcat) {
5957
5958 $result = db_query($link, "SELECT cat_id FROM ttrss_feeds
5959 WHERE owner_uid = '$owner_uid' AND id = '$feed_id'");
5960
5961 $cat_id = (int) db_fetch_result($result, 0, "cat_id");
5962
5963 ccache_update($link, $cat_id, $owner_uid, true);
5964
5965 }
5966 }
5967 } else if ($feed_id < 0) {
5968 ccache_update_all($link, $owner_uid);
5969 }
5970
5971 return $unread;
5972 }
5973
5974 function label_find_id($link, $label, $owner_uid) {
5975 $result = db_query($link,
5976 "SELECT id FROM ttrss_labels2 WHERE caption = '$label'
5977 AND owner_uid = '$owner_uid' LIMIT 1");
5978
5979 if (db_num_rows($result) == 1) {
5980 return db_fetch_result($result, 0, "id");
5981 } else {
5982 return 0;
5983 }
5984 }
5985
5986 function get_article_labels($link, $id) {
5987 $result = db_query($link,
5988 "SELECT DISTINCT label_id,caption
5989 FROM ttrss_labels2, ttrss_user_labels2
5990 WHERE id = label_id
5991 AND article_id = '$id'
5992 AND owner_uid = ".$_SESSION["uid"] . "
5993 ORDER BY caption");
5994
5995 $rv = array();
5996
5997 while ($line = db_fetch_assoc($result)) {
5998 $rk = array($line["label_id"], $line["caption"]);
5999 array_push($rv, $rk);
6000 }
6001
6002 return $rv;
6003 }
6004
6005
6006 function label_find_caption($link, $label, $owner_uid) {
6007 $result = db_query($link,
6008 "SELECT caption FROM ttrss_labels2 WHERE id = '$label'
6009 AND owner_uid = '$owner_uid' LIMIT 1");
6010
6011 if (db_num_rows($result) == 1) {
6012 return db_fetch_result($result, 0, "caption");
6013 } else {
6014 return "";
6015 }
6016 }
6017
6018 function label_remove_article($link, $id, $label, $owner_uid) {
6019
6020 $label_id = label_find_id($link, $label, $owner_uid);
6021
6022 if (!$label_id) return;
6023
6024 $result = db_query($link,
6025 "DELETE FROM ttrss_user_labels2
6026 WHERE
6027 label_id = '$label_id' AND
6028 article_id = '$id'");
6029 }
6030
6031 function label_add_article($link, $id, $label, $owner_uid) {
6032
6033 $label_id = label_find_id($link, $label, $owner_uid);
6034
6035 if (!$label_id) return;
6036
6037 $result = db_query($link,
6038 "SELECT
6039 article_id FROM ttrss_labels2, ttrss_user_labels2
6040 WHERE
6041 label_id = id AND
6042 label_id = '$label_id' AND
6043 article_id = '$id' AND owner_uid = '$owner_uid'
6044 LIMIT 1");
6045
6046 if (db_num_rows($result) == 0) {
6047 db_query($link, "INSERT INTO ttrss_user_labels2
6048 (label_id, article_id) VALUES ('$label_id', '$id')");
6049 }
6050 }
6051
6052 function label_remove($link, $id, $owner_uid) {
6053
6054 db_query($link, "BEGIN");
6055
6056 $result = db_query($link, "SELECT caption FROM ttrss_labels2
6057 WHERE id = '$id'");
6058
6059 $caption = db_fetch_result($result, 0, "caption");
6060
6061 $result = db_query($link, "DELETE FROM ttrss_labels2 WHERE id = '$id'
6062 AND owner_uid = " . $_SESSION["uid"]);
6063
6064 if (db_affected_rows($link, $result) != 0 && $caption) {
6065
6066 /* Disable filters that reference label being removed */
6067
6068 db_query($link, "UPDATE ttrss_filters SET
6069 enabled = false WHERE action_param = '$caption'
6070 AND action_id = 7
6071 AND owner_uid = " . $_SESSION["uid"]);
6072 }
6073
6074 db_query($link, "COMMIT");
6075 }
6076 ?>