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