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