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