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