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