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