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