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