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