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