3 /* if ($_GET["debug"]) {
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
9 require_once "accept-to-gettext.php";
10 require_once "gettext/gettext.inc";
12 require_once 'config.php';
14 function startup_gettext() {
16 # Get locale from Accept-Language header
17 $lang = al2gt(array("en_US", "ru_RU"), "text/html");
20 _setlocale(LC_MESSAGES, $lang);
21 _bindtextdomain("messages", "locale");
22 _textdomain("messages");
23 _bind_textdomain_codeset("messages", "UTF-8");
27 if (ENABLE_TRANSLATIONS == true) {
31 require_once 'db-prefs.php';
32 require_once 'compat.php';
33 require_once 'errors.php';
34 require_once 'version.php';
36 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
37 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
39 require_once "magpierss/rss_fetch.inc";
40 require_once 'magpierss/rss_utils.inc';
42 function _debug($msg) {
43 $ts = strftime("%H:%M:%S", time());
47 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
51 if (DB_TYPE == "pgsql") {
52 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
53 marked = false AND feed_id = '$feed_id' AND
54 (SELECT date_entered FROM ttrss_entries WHERE
55 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
57 $pg_version = get_pgsql_version($link);
59 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
61 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
62 ttrss_entries.id = ref_id AND
64 feed_id = '$feed_id' AND
65 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
69 $result = db_query($link, "DELETE FROM ttrss_user_entries
71 WHERE ttrss_entries.id = ref_id AND
73 feed_id = '$feed_id' AND
74 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
77 $rows = pg_affected_rows($result);
81 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
82 marked = false AND feed_id = '$feed_id' AND
83 (SELECT date_entered FROM ttrss_entries WHERE
84 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
86 $result = db_query($link, "DELETE FROM ttrss_user_entries
87 USING ttrss_user_entries, ttrss_entries
88 WHERE ttrss_entries.id = ref_id AND
90 feed_id = '$feed_id' AND
91 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
93 $rows = mysql_affected_rows($link);
98 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
102 function global_purge_old_posts($link, $do_output = false, $limit = false) {
104 $random_qpart = sql_random_function();
107 $limit_qpart = "LIMIT $limit";
112 $result = db_query($link,
113 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
114 ORDER BY $random_qpart $limit_qpart");
116 while ($line = db_fetch_assoc($result)) {
118 $feed_id = $line["id"];
119 $purge_interval = $line["purge_interval"];
120 $owner_uid = $line["owner_uid"];
122 if ($purge_interval == 0) {
124 $tmp_result = db_query($link,
125 "SELECT value FROM ttrss_user_prefs WHERE
126 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
128 if (db_num_rows($tmp_result) != 0) {
129 $purge_interval = db_fetch_result($tmp_result, 0, "value");
134 // print "Feed $feed_id: purge interval = $purge_interval\n";
137 if ($purge_interval > 0) {
138 purge_feed($link, $feed_id, $purge_interval, $do_output);
142 // purge orphaned posts in main content table
143 db_query($link, "DELETE FROM ttrss_entries WHERE
144 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
148 function purge_old_posts($link) {
150 $user_id = $_SESSION["uid"];
152 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
153 WHERE owner_uid = '$user_id'");
155 while ($line = db_fetch_assoc($result)) {
157 $feed_id = $line["id"];
158 $purge_interval = $line["purge_interval"];
160 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
162 if ($purge_interval > 0) {
163 purge_feed($link, $feed_id, $purge_interval);
167 // purge orphaned posts in main content table
168 db_query($link, "DELETE FROM ttrss_entries WHERE
169 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
172 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
174 if (WEB_DEMO_MODE) return;
177 $user_id = $_SESSION["uid"];
178 purge_old_posts($link);
181 // db_query($link, "BEGIN");
183 if (MAX_UPDATE_TIME > 0) {
184 if (DB_TYPE == "mysql") {
187 $q_order = "RANDOM()";
190 $q_order = "last_updated DESC";
193 $result = db_query($link, "SELECT feed_url,id,
194 SUBSTRING(last_updated,1,19) AS last_updated,
195 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
200 while ($line = db_fetch_assoc($result)) {
201 $upd_intl = $line["update_interval"];
203 if (!$upd_intl || $upd_intl == 0) {
204 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
208 // Updates for this feed are disabled
212 if ($fetch || (!$line["last_updated"] ||
213 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
215 // print "<!-- feed: ".$line["feed_url"]." -->";
217 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
219 $upd_elapsed = time() - $upd_start;
221 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
227 // db_query($link, "COMMIT");
231 function fetch_file_contents($url) {
232 if (USE_CURL_FOR_ICONS) {
233 $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
235 $ch = curl_init($url);
236 $fp = fopen($tmpfile, "w");
239 curl_setopt($ch, CURLOPT_FILE, $fp);
240 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
241 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
247 $contents = file_get_contents($tmpfile);
253 return file_get_contents($url);
258 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
259 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
261 function get_favicon_url($url) {
263 if ($html = @fetch_file_contents($url)) {
265 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
266 // Attempt to grab a favicon link from their webpage url
267 $linkUrl = html_entity_decode($matches[1]);
269 if (substr($linkUrl, 0, 1) == '/') {
270 $urlParts = parse_url($url);
271 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
272 } else if (substr($linkUrl, 0, 7) == 'http://') {
273 $faviconURL = $linkUrl;
274 } else if (substr($url, -1, 1) == '/') {
275 $faviconURL = $url.$linkUrl;
277 $faviconURL = $url.'/'.$linkUrl;
281 // If unsuccessful, attempt to "guess" the favicon location
282 $urlParts = parse_url($url);
283 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
287 // Run a test to see if what we have attempted to get actually exists.
288 if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) {
295 function url_validate($link) {
297 $url_parts = @parse_url($link);
299 if ( empty( $url_parts["host"] ) )
302 if ( !empty( $url_parts["path"] ) ) {
303 $documentpath = $url_parts["path"];
308 if ( !empty( $url_parts["query"] ) )
309 $documentpath .= "?" . $url_parts["query"];
311 $host = $url_parts["host"];
312 $port = $url_parts["port"];
317 $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
322 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
324 $http_response = fgets( $socket, 22 );
326 $responses = "/(200 OK)|(30[0-9] Moved)/";
327 if ( preg_match($responses, $http_response) ) {
336 function check_feed_favicon($site_url, $feed, $link) {
337 $favicon_url = get_favicon_url($site_url);
339 # print "FAVICON [$site_url]: $favicon_url\n";
343 $icon_file = ICONS_DIR . "/$feed.ico";
345 if ($favicon_url && !file_exists($icon_file)) {
346 $contents = fetch_file_contents($favicon_url);
348 $fp = fopen($icon_file, "w");
351 fwrite($fp, $contents);
353 chmod($icon_file, 0644);
357 error_reporting(DEFAULT_ERROR_LEVEL);
361 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
363 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
367 if (defined('DAEMON_EXTENDED_DEBUG')) {
368 _debug("update_rss_feed: start");
371 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
372 FROM ttrss_feeds WHERE id = '$feed'");
374 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
375 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
377 $update_interval = db_fetch_result($result, 0, "update_interval");
379 if ($update_interval < 0) { return; }
381 $feed = db_escape_string($feed);
383 $fetch_url = $feed_url;
385 if ($auth_login && $auth_pass) {
386 $url_parts = array();
387 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
389 if ($url_parts[1] && $url_parts[2]) {
390 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
395 if (defined('DAEMON_EXTENDED_DEBUG')) {
396 _debug("update_rss_feed: fetching...");
399 if (!defined('DAEMON_EXTENDED_DEBUG')) {
403 $rss = fetch_rss($fetch_url);
405 if (defined('DAEMON_EXTENDED_DEBUG')) {
406 _debug("update_rss_feed: fetch done, parsing...");
408 error_reporting (DEFAULT_ERROR_LEVEL);
411 $feed = db_escape_string($feed);
415 if (defined('DAEMON_EXTENDED_DEBUG')) {
416 _debug("update_rss_feed: processing feed data...");
419 // db_query($link, "BEGIN");
421 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
422 FROM ttrss_feeds WHERE id = '$feed'");
424 $registered_title = db_fetch_result($result, 0, "title");
425 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
426 $orig_site_url = db_fetch_result($result, 0, "site_url");
428 $owner_uid = db_fetch_result($result, 0, "owner_uid");
430 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
431 if (defined('DAEMON_EXTENDED_DEBUG')) {
432 _debug("update_rss_feed: checking favicon...");
434 check_feed_favicon($rss->channel["link"], $feed, $link);
437 if (!$registered_title || $registered_title == "[Unknown]") {
439 $feed_title = db_escape_string($rss->channel["title"]);
441 db_query($link, "UPDATE ttrss_feeds SET
442 title = '$feed_title' WHERE id = '$feed'");
445 $site_url = $rss->channel["link"];
446 // weird, weird Magpie
447 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
449 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
450 db_query($link, "UPDATE ttrss_feeds SET
451 site_url = '$site_url' WHERE id = '$feed'");
454 // print "I: " . $rss->channel["image"]["url"];
456 $icon_url = $rss->image["url"];
458 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
459 $icon_url = db_escape_string($icon_url);
460 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
463 if (defined('DAEMON_EXTENDED_DEBUG')) {
464 _debug("update_rss_feed: loading filters...");
469 $result = db_query($link, "SELECT reg_exp,
470 ttrss_filter_types.name AS name,
471 ttrss_filter_actions.name AS action,
474 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
476 owner_uid = $owner_uid AND
477 ttrss_filter_types.id = filter_type AND
478 ttrss_filter_actions.id = action_id AND
479 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
481 while ($line = db_fetch_assoc($result)) {
482 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
484 $filter["reg_exp"] = $line["reg_exp"];
485 $filter["action"] = $line["action"];
486 $filter["action_param"] = $line["action_param"];
487 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
489 array_push($filters[$line["name"]], $filter);
492 $iterator = $rss->items;
494 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
495 if (!$iterator || !is_array($iterator)) $iterator = $rss;
497 if (!is_array($iterator)) {
498 /* db_query($link, "UPDATE ttrss_feeds
499 SET last_error = 'Parse error: can\'t find any articles.'
500 WHERE id = '$feed'"); */
502 // clear any errors and mark feed as updated if fetched okay
503 // even if it's blank
505 db_query($link, "UPDATE ttrss_feeds
506 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
508 return; // no articles
511 if (defined('DAEMON_EXTENDED_DEBUG')) {
512 _debug("update_rss_feed: processing articles...");
515 foreach ($iterator as $item) {
517 $entry_guid = $item["id"];
519 if (!$entry_guid) $entry_guid = $item["guid"];
520 if (!$entry_guid) $entry_guid = $item["link"];
521 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
523 if (defined('DAEMON_EXTENDED_DEBUG')) {
524 _debug("update_rss_feed: guid $entry_guid");
527 if (!$entry_guid) continue;
529 $entry_timestamp = "";
531 $rss_2_date = $item['pubdate'];
532 $rss_1_date = $item['dc']['date'];
533 $atom_date = $item['issued'];
534 if (!$atom_date) $atom_date = $item['updated'];
536 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
537 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
538 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
540 if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
541 $entry_timestamp = time();
542 $no_orig_date = 'true';
544 $no_orig_date = 'false';
547 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
549 $entry_title = trim(strip_tags($item["title"]));
551 // strange Magpie workaround
552 $entry_link = $item["link_"];
553 if (!$entry_link) $entry_link = $item["link"];
555 if (!$entry_title) continue;
556 # if (!$entry_link) continue;
558 $entry_link = strip_tags($entry_link);
560 $entry_content = $item["content:escaped"];
562 if (!$entry_content) $entry_content = $item["content:encoded"];
563 if (!$entry_content) $entry_content = $item["content"];
564 if (!$entry_content) $entry_content = $item["atom_content"];
565 if (!$entry_content) $entry_content = $item["summary"];
566 if (!$entry_content) $entry_content = $item["description"];
568 // if (!$entry_content) continue;
571 if (is_array($entry_content)) {
572 $entry_content = $entry_content["encoded"];
573 if (!$entry_content) $entry_content = $entry_content["escaped"];
577 // print_r(htmlspecialchars($entry_content));
580 $entry_content_unescaped = $entry_content;
581 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
583 $entry_comments = strip_tags($item["comments"]);
585 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
587 if ($item['author']) {
589 if (is_array($item['author'])) {
591 if (!$entry_author) {
592 $entry_author = db_escape_string(strip_tags($item['author']['name']));
595 if (!$entry_author) {
596 $entry_author = db_escape_string(strip_tags($item['author']['email']));
600 if (!$entry_author) {
601 $entry_author = db_escape_string(strip_tags($item['author']));
605 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
607 $entry_guid = db_escape_string(strip_tags($entry_guid));
609 $result = db_query($link, "SELECT id FROM ttrss_entries
610 WHERE guid = '$entry_guid'");
612 $entry_content = db_escape_string($entry_content);
613 $entry_title = db_escape_string($entry_title);
614 $entry_link = db_escape_string($entry_link);
615 $entry_comments = db_escape_string($entry_comments);
617 $num_comments = db_escape_string($item["slash"]["comments"]);
619 if (!$num_comments) $num_comments = 0;
621 /* $dc_subject = $item['dc']['subject'];
623 $subject_tags = false;
625 if (is_array($dc_subject)) {
626 $subject_tags = $dc_subject;
627 } else if ($dc_subject) {
628 $subject_tags = array($dc_subject);
633 $entry_content = sanitize_rss($entry_content);
635 if (defined('DAEMON_EXTENDED_DEBUG')) {
636 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
639 db_query($link, "BEGIN");
641 if (db_num_rows($result) == 0) {
643 if (defined('DAEMON_EXTENDED_DEBUG')) {
644 _debug("update_rss_feed: base guid not found");
647 // base post entry does not exist, create it
649 $result = db_query($link,
650 "INSERT INTO ttrss_entries
666 '$entry_timestamp_fmt',
675 // we keep encountering the entry in feeds, so we need to
676 // update date_entered column so that we don't get horrible
677 // dupes when the entry gets purged and reinserted again e.g.
678 // in the case of SLOW SLOW OMG SLOW updating feeds
680 $base_entry_id = db_fetch_result($result, 0, "id");
682 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
683 WHERE id = '$base_entry_id'");
686 // now it should exist, if not - bad luck then
688 $result = db_query($link, "SELECT
689 id,content_hash,no_orig_date,title,
690 substring(date_entered,1,19) as date_entered,
691 substring(updated,1,19) as updated,
695 WHERE guid = '$entry_guid'");
697 if (db_num_rows($result) == 1) {
699 if (defined('DAEMON_EXTENDED_DEBUG')) {
700 _debug("update_rss_feed: base guid found, checking for user record");
703 // this will be used below in update handler
704 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
705 $orig_title = db_fetch_result($result, 0, "title");
706 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
707 $orig_date_entered = strtotime(db_fetch_result($result,
710 $ref_id = db_fetch_result($result, 0, "id");
712 // check for user post link to main table
714 // do we allow duplicate posts with same GUID in different feeds?
715 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
716 $dupcheck_qpart = "AND feed_id = '$feed'";
718 $dupcheck_qpart = "";
721 // error_reporting(0);
723 $article_filters = get_article_filters($filters, $entry_title,
724 $entry_content, $entry_link);
726 if (defined('DAEMON_EXTENDED_DEBUG')) {
727 _debug("update_rss_feed: article filters: ");
728 if (count($article_filters) != 0) {
729 print_r($article_filters);
733 if (find_article_filter($article_filters, "filter")) {
737 // error_reporting (DEFAULT_ERROR_LEVEL);
739 $result = db_query($link,
740 "SELECT ref_id FROM ttrss_user_entries WHERE
741 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
744 // okay it doesn't exist - create user entry
745 if (db_num_rows($result) == 0) {
747 if (defined('DAEMON_EXTENDED_DEBUG')) {
748 _debug("update_rss_feed: user record not found, creating...");
751 if (!find_article_filter($article_filters, 'catchup')) {
753 $last_read_qpart = 'NULL';
756 $last_read_qpart = 'NOW()';
759 if (find_article_filter($article_filters, 'mark')) {
765 $result = db_query($link,
766 "INSERT INTO ttrss_user_entries
767 (ref_id, owner_uid, feed_id, unread, last_read, marked)
768 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
769 $last_read_qpart, $marked)");
772 $post_needs_update = false;
774 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
775 ($content_hash != $orig_content_hash)) {
776 $post_needs_update = true;
779 if ($orig_title != $entry_title) {
780 $post_needs_update = true;
783 if ($orig_num_comments != $num_comments) {
784 $post_needs_update = true;
787 // this doesn't seem to be very reliable
789 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
790 // $post_needs_update = true;
793 // if post needs update, update it and mark all user entries
794 // linking to this post as updated
795 if ($post_needs_update) {
797 if (defined('DAEMON_EXTENDED_DEBUG')) {
798 _debug("update_rss_feed: post $entry_guid needs update...");
801 // print "<!-- post $orig_title needs update : $post_needs_update -->";
803 db_query($link, "UPDATE ttrss_entries
804 SET title = '$entry_title', content = '$entry_content',
805 num_comments = '$num_comments'
806 WHERE id = '$ref_id'");
808 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
809 db_query($link, "UPDATE ttrss_user_entries
810 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
812 db_query($link, "UPDATE ttrss_user_entries
813 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
819 db_query($link, "COMMIT");
821 if (defined('DAEMON_EXTENDED_DEBUG')) {
822 _debug("update_rss_feed: looking for tags...");
826 // <a href="..." rel="tag">Xorg</a>, //
830 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
831 $entry_content_unescaped, $entry_tags);
833 // print "<br>$entry_title : $entry_content_unescaped<br>";
834 // print_r($entry_tags);
837 $entry_tags = $entry_tags[1];
839 # check for manual tags
841 $tag_filter = find_article_filter($article_filters, "tag");
845 $manual_tags = trim_array(split(",", $tag_filter[1]));
847 foreach ($manual_tags as $tag) {
848 if (tag_is_valid($tag)) {
849 array_push($entry_tags, $tag);
854 /* if ($subject_tags) {
855 foreach ($subject_tags as $tag) {
856 if (tag_is_valid($tag)) {
857 array_push($entry_tags, $tag);
862 if (count($entry_tags) > 0) {
864 db_query($link, "BEGIN");
866 $result = db_query($link, "SELECT id,int_id
867 FROM ttrss_entries,ttrss_user_entries
868 WHERE guid = '$entry_guid'
869 AND feed_id = '$feed' AND ref_id = id
870 AND owner_uid = '$owner_uid'");
872 if (db_num_rows($result) == 1) {
874 $entry_id = db_fetch_result($result, 0, "id");
875 $entry_int_id = db_fetch_result($result, 0, "int_id");
877 foreach ($entry_tags as $tag) {
878 $tag = db_escape_string(mb_strtolower(strip_tags($tag)));
880 $tag = str_replace("+", " ", $tag);
881 $tag = str_replace("technorati tag: ", "", $tag);
883 if (!tag_is_valid($tag)) continue;
885 $result = db_query($link, "SELECT id FROM ttrss_tags
886 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
887 owner_uid = '$owner_uid' LIMIT 1");
889 // print db_fetch_result($result, 0, "id");
891 if ($result && db_num_rows($result) == 0) {
893 // print "tagging $entry_id as $tag<br>";
895 db_query($link, "INSERT INTO ttrss_tags
896 (owner_uid,tag_name,post_int_id)
897 VALUES ('$owner_uid','$tag', '$entry_int_id')");
901 db_query($link, "COMMIT");
905 db_query($link, "UPDATE ttrss_feeds
906 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
908 // db_query($link, "COMMIT");
911 $error_msg = db_escape_string(magpie_error());
913 "UPDATE ttrss_feeds SET last_error = '$error_msg',
914 last_updated = NOW() WHERE id = '$feed'");
917 if (defined('DAEMON_EXTENDED_DEBUG')) {
918 _debug("update_rss_feed: done");
923 function print_select($id, $default, $values, $attributes = "") {
924 print "<select name=\"$id\" id=\"$id\" $attributes>";
925 foreach ($values as $v) {
931 print "<option$sel>$v</option>";
936 function print_select_hash($id, $default, $values, $attributes = "") {
937 print "<select name=\"$id\" id='$id' $attributes>";
938 foreach (array_keys($values) as $v) {
944 print "<option $sel value=\"$v\">".$values[$v]."</option>";
950 function get_article_filters($filters, $title, $content, $link) {
953 if ($filters["title"]) {
954 foreach ($filters["title"] as $filter) {
955 $reg_exp = $filter["reg_exp"];
956 $inverse = $filter["inverse"];
957 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
958 ($inverse && !preg_match("/$reg_exp/i", $title))) {
960 array_push($matches, array($filter["action"], $filter["action_param"]));
965 if ($filters["content"]) {
966 foreach ($filters["content"] as $filter) {
967 $reg_exp = $filter["reg_exp"];
968 $inverse = $filter["inverse"];
970 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
971 ($inverse && !preg_match("/$reg_exp/i", $content))) {
973 array_push($matches, array($filter["action"], $filter["action_param"]));
978 if ($filters["both"]) {
979 foreach ($filters["both"] as $filter) {
980 $reg_exp = $filter["reg_exp"];
981 $inverse = $filter["inverse"];
984 if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) {
985 array_push($matches, array($filter["action"], $filter["action_param"]));
988 if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
989 array_push($matches, array($filter["action"], $filter["action_param"]));
995 if ($filters["link"]) {
996 $reg_exp = $filter["reg_exp"];
997 foreach ($filters["link"] as $filter) {
998 $reg_exp = $filter["reg_exp"];
999 $inverse = $filter["inverse"];
1001 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1002 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1004 array_push($matches, array($filter["action"], $filter["action_param"]));
1012 function find_article_filter($filters, $filter_name) {
1013 foreach ($filters as $f) {
1014 if ($f[0] == $filter_name) {
1021 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
1022 $rtl_content = false, $last_updated = false, $last_error = false) {
1024 if (file_exists($icon_file) && filesize($icon_file) > 0) {
1025 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
1027 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
1031 $rtl_tag = "dir=\"rtl\"";
1033 $rtl_tag = "dir=\"ltr\"";
1036 $error_notify_msg = "";
1039 $link_title = "Error: $last_error ($last_updated)";
1040 $error_notify_msg = "(Error)";
1041 } else if ($last_updated) {
1042 $link_title = "Updated: $last_updated";
1045 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
1046 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
1048 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
1049 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
1053 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
1058 $fctr_class = "class=\"invisible\"";
1061 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
1062 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
1064 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1065 print "<div class=\"feedExtInfo\">
1066 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1073 function getmicrotime() {
1074 list($usec, $sec) = explode(" ",microtime());
1075 return ((float)$usec + (float)$sec);
1078 function print_radio($id, $default, $values, $attributes = "") {
1079 foreach ($values as $v) {
1087 $sel .= " value=\"1\"";
1089 $sel .= " value=\"0\"";
1092 print "<input class=\"noborder\"
1093 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
1098 function initialize_user_prefs($link, $uid) {
1100 $uid = db_escape_string($uid);
1102 db_query($link, "BEGIN");
1104 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1106 $u_result = db_query($link, "SELECT pref_name
1107 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1109 $active_prefs = array();
1111 while ($line = db_fetch_assoc($u_result)) {
1112 array_push($active_prefs, $line["pref_name"]);
1115 while ($line = db_fetch_assoc($result)) {
1116 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1117 // print "adding " . $line["pref_name"] . "<br>";
1119 db_query($link, "INSERT INTO ttrss_user_prefs
1120 (owner_uid,pref_name,value) VALUES
1121 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1126 db_query($link, "COMMIT");
1130 function lookup_user_id($link, $user) {
1132 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1135 if (db_num_rows($result) == 1) {
1136 return db_fetch_result($result, 0, "id");
1142 function http_authenticate_user($link) {
1144 if (!$_SERVER["PHP_AUTH_USER"]) {
1146 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1147 header('HTTP/1.0 401 Unauthorized');
1151 $auth_result = authenticate_user($link,
1152 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1154 if (!$auth_result) {
1155 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1156 header('HTTP/1.0 401 Unauthorized');
1164 function authenticate_user($link, $login, $password, $force_auth = false) {
1166 if (!SINGLE_USER_MODE) {
1168 $pwd_hash = 'SHA1:' . sha1($password);
1170 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1171 $query = "SELECT id,login,access_level
1172 FROM ttrss_users WHERE
1175 $query = "SELECT id,login,access_level
1176 FROM ttrss_users WHERE
1177 login = '$login' AND pwd_hash = '$pwd_hash'";
1180 $result = db_query($link, $query);
1182 if (db_num_rows($result) == 1) {
1183 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1184 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1185 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1187 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1190 $user_theme = get_user_theme_path($link);
1192 $_SESSION["theme"] = $user_theme;
1193 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1195 initialize_user_prefs($link, $_SESSION["uid"]);
1204 $_SESSION["uid"] = 1;
1205 $_SESSION["name"] = "admin";
1207 $user_theme = get_user_theme_path($link);
1209 $_SESSION["theme"] = $user_theme;
1210 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1212 initialize_user_prefs($link, $_SESSION["uid"]);
1218 function make_password($length = 8) {
1221 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1225 while ($i < $length) {
1226 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1228 if (!strstr($password, $char)) {
1236 // this is called after user is created to initialize default feeds, labels
1239 // user preferences are checked on every login, not here
1241 function initialize_user($link, $uid) {
1243 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1244 values ('$uid','unread = true', 'Unread articles')");
1246 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1247 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1249 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1250 values ('$uid', 'Tiny Tiny RSS: New Releases',
1251 'http://tt-rss.spb.ru/releases.rss')");
1253 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1254 values ('$uid', 'Tiny Tiny RSS: Forum',
1255 'http://tt-rss.spb.ru/forum/rss.php')");
1258 function logout_user() {
1260 if (isset($_COOKIE[session_name()])) {
1261 setcookie(session_name(), '', time()-42000, '/');
1265 function get_script_urlpath() {
1266 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1269 function validate_session($link) {
1270 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
1271 if ($_SESSION["ip_address"]) {
1272 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1273 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
1279 /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
1281 //print_r($_SESSION);
1283 if (time() > $_SESSION["cookie_lifetime"]) {
1291 function login_sequence($link, $mobile = false) {
1292 if (!SINGLE_USER_MODE) {
1294 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1295 $swu = db_escape_string($_REQUEST["swu"]);
1297 $_SESSION["prefs_cache"] = false;
1298 return authenticate_user($link, $swu, null, true);
1302 $login_action = $_POST["login_action"];
1304 # try to authenticate user if called from login form
1305 if ($login_action == "do_login") {
1306 $login = $_POST["login"];
1307 $password = $_POST["password"];
1308 $remember_me = $_POST["remember_me"];
1310 if (authenticate_user($link, $login, $password)) {
1311 $_POST["password"] = "";
1313 header("Location: " . $_SERVER["REQUEST_URI"]);
1318 $_SESSION["login_error_msg"] = "Incorrect username or password";
1322 // print session_id();
1323 // print_r($_SESSION);
1325 if (!$_SESSION["uid"] || !validate_session($link)) {
1326 render_login_form($link, $mobile);
1329 /* bump login timestamp */
1330 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1335 return authenticate_user($link, "admin", null);
1339 function truncate_string($str, $max_len) {
1340 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1341 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1347 function get_user_theme_path($link) {
1348 $result = db_query($link, "SELECT theme_path
1350 ttrss_themes,ttrss_users
1351 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1352 if (db_num_rows($result) != 0) {
1353 return db_fetch_result($result, 0, "theme_path");
1359 function smart_date_time($timestamp) {
1360 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1361 return date("G:i", $timestamp);
1362 } else if (date("Y", $timestamp) == date("Y")) {
1363 return date("M d, G:i", $timestamp);
1365 return date("Y/m/d, G:i", $timestamp);
1369 function smart_date($timestamp) {
1370 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1372 } else if (date("Y", $timestamp) == date("Y")) {
1373 return date("D m", $timestamp);
1375 return date("Y/m/d", $timestamp);
1379 function sql_bool_to_string($s) {
1380 if ($s == "t" || $s == "1") {
1387 function sql_bool_to_bool($s) {
1388 if ($s == "t" || $s == "1") {
1396 function toggleEvenOdd($a) {
1403 function sanity_check($link) {
1408 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1409 $schema_version = db_fetch_result($result, 0, "schema_version");
1411 if ($schema_version != SCHEMA_VERSION) {
1415 if (DB_TYPE == "mysql") {
1416 $result = db_query($link, "SELECT true", false);
1417 if (db_num_rows($result) != 1) {
1422 error_reporting (DEFAULT_ERROR_LEVEL);
1424 if ($error_code != 0) {
1425 print_error_xml($error_code);
1432 function file_is_locked($filename) {
1434 $fp = fopen($filename, "r");
1435 error_reporting(DEFAULT_ERROR_LEVEL);
1437 if (flock($fp, LOCK_EX | LOCK_NB)) {
1438 flock($fp, LOCK_UN);
1448 function make_lockfile($filename) {
1449 $fp = fopen($filename, "w");
1451 if (flock($fp, LOCK_EX | LOCK_NB)) {
1458 function sql_random_function() {
1459 if (DB_TYPE == "mysql") {
1466 function catchup_feed($link, $feed, $cat_view) {
1468 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1473 $cat_qpart = "cat_id = '$feed'";
1475 $cat_qpart = "cat_id IS NULL";
1478 $tmp_result = db_query($link, "SELECT id
1479 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1482 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1484 $tmp_feed = $tmp_line["id"];
1486 db_query($link, "UPDATE ttrss_user_entries
1487 SET unread = false,last_read = NOW()
1488 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1491 } else if ($feed > 0) {
1493 $tmp_result = db_query($link, "SELECT id
1494 FROM ttrss_feeds WHERE parent_feed = '$feed'
1495 ORDER BY cat_id,title");
1497 $parent_ids = array();
1499 if (db_num_rows($tmp_result) > 0) {
1500 while ($p = db_fetch_assoc($tmp_result)) {
1501 array_push($parent_ids, "feed_id = " . $p["id"]);
1504 $children_qpart = implode(" OR ", $parent_ids);
1506 db_query($link, "UPDATE ttrss_user_entries
1507 SET unread = false,last_read = NOW()
1508 WHERE (feed_id = '$feed' OR $children_qpart)
1509 AND owner_uid = " . $_SESSION["uid"]);
1512 db_query($link, "UPDATE ttrss_user_entries
1513 SET unread = false,last_read = NOW()
1514 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1517 } else if ($feed < 0 && $feed > -10) { // special, like starred
1520 db_query($link, "UPDATE ttrss_user_entries
1521 SET unread = false,last_read = NOW()
1522 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1525 } else if ($feed < -10) { // label
1527 // TODO make this more efficient
1529 $label_id = -$feed - 11;
1531 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1532 WHERE id = '$label_id'");
1535 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1537 db_query($link, "BEGIN");
1539 $tmp2_result = db_query($link,
1543 ttrss_user_entries,ttrss_entries,ttrss_feeds
1545 ref_id = ttrss_entries.id AND
1546 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1548 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1550 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1551 db_query($link, "UPDATE
1554 unread = false, last_read = NOW()
1556 int_id = " . $tmp_line["int_id"]);
1559 db_query($link, "COMMIT");
1561 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1562 SET unread = false,last_read = NOW()
1565 AND owner_uid = ".$_SESSION["uid"]); */
1569 db_query($link, "BEGIN");
1571 $tag_name = db_escape_string($feed);
1573 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1574 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1576 while ($line = db_fetch_assoc($result)) {
1577 db_query($link, "UPDATE ttrss_user_entries SET
1578 unread = false, last_read = NOW()
1579 WHERE int_id = " . $line["post_int_id"]);
1581 db_query($link, "COMMIT");
1585 function update_generic_feed($link, $feed, $cat_view) {
1589 $cat_qpart = "cat_id = '$feed'";
1591 $cat_qpart = "cat_id IS NULL";
1594 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1595 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1597 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1598 $feed_url = $tmp_line["feed_url"];
1599 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1603 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1604 WHERE id = '$feed'");
1605 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1606 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1610 function getAllCounters($link, $omode = "tflc") {
1611 /* getLabelCounters($link);
1612 getFeedCounters($link);
1613 getTagCounters($link);
1614 getGlobalCounters($link);
1615 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1616 getCategoryCounters($link);
1619 if (!$omode) $omode = "tflc";
1621 getGlobalCounters($link);
1623 if (strchr($omode, "l")) getLabelCounters($link);
1624 if (strchr($omode, "f")) getFeedCounters($link);
1625 if (strchr($omode, "t")) getTagCounters($link);
1626 if (strchr($omode, "c")) {
1627 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1628 getCategoryCounters($link);
1633 function getCategoryCounters($link) {
1634 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1635 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1636 AND unread = true)) AS unread FROM ttrss_feeds
1638 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1640 while ($line = db_fetch_assoc($result)) {
1641 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1642 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1643 $line["unread"]."\"/>";
1647 function getCategoryUnread($link, $cat) {
1650 $cat_query = "cat_id = '$cat'";
1652 $cat_query = "cat_id IS NULL";
1655 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1657 AND owner_uid = " . $_SESSION["uid"]);
1659 $cat_feeds = array();
1660 while ($line = db_fetch_assoc($result)) {
1661 array_push($cat_feeds, "feed_id = " . $line["id"]);
1664 if (count($cat_feeds) == 0) return 0;
1666 $match_part = implode(" OR ", $cat_feeds);
1668 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1669 FROM ttrss_user_entries
1670 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1674 # this needs to be rewritten
1675 while ($line = db_fetch_assoc($result)) {
1676 $unread += $line["unread"];
1683 function getFeedUnread($link, $feed, $is_cat = false) {
1684 $n_feed = sprintf("%d", $feed);
1687 return getCategoryUnread($link, $n_feed);
1688 } else if ($n_feed == -1) {
1689 $match_part = "marked = true";
1690 } else if ($n_feed > 0) {
1692 $result = db_query($link, "SELECT id FROM ttrss_feeds
1693 WHERE parent_feed = '$n_feed'
1695 AND owner_uid = " . $_SESSION["uid"]);
1697 if (db_num_rows($result) > 0) {
1699 $linked_feeds = array();
1700 while ($line = db_fetch_assoc($result)) {
1701 array_push($linked_feeds, "feed_id = " . $line["id"]);
1704 array_push($linked_feeds, "feed_id = $n_feed");
1706 $match_part = implode(" OR ", $linked_feeds);
1708 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1709 FROM ttrss_user_entries
1710 WHERE unread = true AND ($match_part)
1711 AND owner_uid = " . $_SESSION["uid"]);
1715 # this needs to be rewritten
1716 while ($line = db_fetch_assoc($result)) {
1717 $unread += $line["unread"];
1723 $match_part = "feed_id = '$n_feed'";
1725 } else if ($feed < -10) {
1727 $label_id = -$feed - 11;
1729 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1730 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1732 $match_part = db_fetch_result($result, 0, "sql_exp");
1737 $result = db_query($link, "SELECT count(int_id) AS unread
1738 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1739 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1740 ttrss_user_entries.ref_id = ttrss_entries.id AND
1741 ttrss_feeds.hidden = false AND
1742 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1746 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1747 FROM ttrss_tags,ttrss_user_entries
1748 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1749 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1752 $unread = db_fetch_result($result, 0, "unread");
1757 /* FIXME this needs reworking */
1759 function getGlobalUnread($link, $user_id = false) {
1762 $user_id = $_SESSION["uid"];
1765 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1766 WHERE unread = true AND
1767 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1768 ttrss_user_entries.ref_id = ttrss_entries.id AND
1770 ttrss_user_entries.owner_uid = '$user_id'");
1771 $c_id = db_fetch_result($result, 0, "c_id");
1775 function getGlobalCounters($link, $global_unread = -1) {
1776 if ($global_unread == -1) {
1777 $global_unread = getGlobalUnread($link);
1779 print "<counter type=\"global\" id='global-unread'
1780 counter='$global_unread'/>";
1782 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1783 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1785 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1787 print "<counter type=\"global\" id='subscribed-feeds'
1788 counter='$subscribed_feeds'/>";
1792 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1795 if (!$_SESSION["tctr_last_value"]) {
1796 $_SESSION["tctr_last_value"] = array();
1800 $old_counters = $_SESSION["tctr_last_value"];
1802 $tctrs_modified = false;
1804 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1805 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1806 ttrss_user_entries.ref_id = ttrss_entries.id AND
1807 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1808 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1810 select tag_name,0 as count FROM ttrss_tags
1811 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1813 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1814 FROM ttrss_user_entries WHERE int_id = post_int_id
1815 AND unread = true)) AS count FROM ttrss_tags
1816 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
1820 while ($line = db_fetch_assoc($result)) {
1821 $tags[$line["tag_name"]] += $line["count"];
1824 foreach (array_keys($tags) as $tag) {
1825 $unread = $tags[$tag];
1827 $tag = htmlspecialchars($tag);
1829 if (!$smart_mode || $old_counters[$tag] != $unread) {
1830 $old_counters[$tag] = $unread;
1831 $tctrs_modified = true;
1832 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1837 if ($smart_mode && $tctrs_modified) {
1838 $_SESSION["tctr_last_value"] = $old_counters;
1843 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
1846 if (!$_SESSION["lctr_last_value"]) {
1847 $_SESSION["lctr_last_value"] = array();
1853 $old_counters = $_SESSION["lctr_last_value"];
1854 $lctrs_modified = false;
1856 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1857 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1858 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1859 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1861 $count = db_fetch_result($result, 0, "count");
1864 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1866 $ret_arr["-1"]["counter"] = $count;
1867 $ret_arr["-1"]["description"] = "Starred";
1870 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1871 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1873 while ($line = db_fetch_assoc($result)) {
1875 $id = -$line["id"] - 11;
1877 $label_name = $line["description"];
1879 error_reporting (0);
1881 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1882 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1883 ttrss_feeds.hidden = false AND
1884 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1885 ttrss_user_entries.ref_id = ttrss_entries.id AND
1886 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1888 $count = db_fetch_result($tmp_result, 0, "count");
1890 if (!$smart_mode || $old_counters[$id] != $count) {
1891 $old_counters[$id] = $count;
1892 $lctrs_modified = true;
1894 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1896 $ret_arr[$id]["counter"] = $count;
1897 $ret_arr[$id]["description"] = $label_name;
1901 error_reporting (DEFAULT_ERROR_LEVEL);
1904 if ($smart_mode && $lctrs_modified) {
1905 $_SESSION["lctr_last_value"] = $old_counters;
1911 /* function getFeedCounter($link, $id) {
1913 $result = db_query($link, "SELECT
1914 count(id) as count,last_error
1915 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1916 WHERE feed_id = '$id' AND unread = true
1917 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1918 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1920 $count = db_fetch_result($result, 0, "count");
1921 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1923 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1926 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1929 if (!$_SESSION["fctr_last_value"]) {
1930 $_SESSION["fctr_last_value"] = array();
1934 $old_counters = $_SESSION["fctr_last_value"];
1936 $result = db_query($link, "SELECT id,last_error,parent_feed,
1937 SUBSTRING(last_updated,1,19) AS last_updated,
1939 FROM ttrss_entries,ttrss_user_entries
1940 WHERE feed_id = ttrss_feeds.id AND
1941 ttrss_user_entries.ref_id = ttrss_entries.id
1942 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1943 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1944 AND parent_feed IS NULL");
1946 $fctrs_modified = false;
1948 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1950 while ($line = db_fetch_assoc($result)) {
1953 $count = $line["count"];
1954 $last_error = htmlspecialchars($line["last_error"]);
1956 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1957 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1959 $last_updated = date($short_date, strtotime($line["last_updated"]));
1962 $has_img = is_file(ICONS_DIR . "/$id.ico");
1964 $tmp_result = db_query($link,
1965 "SELECT id,COUNT(unread) AS unread
1966 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1967 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1968 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1970 if (db_num_rows($tmp_result) > 0) {
1971 while ($l = db_fetch_assoc($tmp_result)) {
1972 $count += $l["unread"];
1976 if (!$smart_mode || $old_counters[$id] != $count) {
1977 $old_counters[$id] = $count;
1978 $fctrs_modified = true;
1981 $error_part = "error=\"$last_error\"";
1987 $has_img_part = "hi=\"$has_img\"";
1992 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1996 if ($smart_mode && $fctrs_modified) {
1997 $_SESSION["fctr_last_value"] = $old_counters;
2001 function get_script_dt_add() {
2002 if (strpos(VERSION, ".99") === false) {
2009 function get_pgsql_version($link) {
2010 $result = db_query($link, "SELECT version() AS version");
2011 $version = split(" ", db_fetch_result($result, 0, "version"));
2015 function print_error_xml($code, $add_msg = "") {
2018 $error_msg = $ERRORS[$code];
2021 $error_msg = "$error_msg; $add_msg";
2024 print "<rpc-reply>";
2025 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
2026 print "</rpc-reply>";
2029 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2030 $auth_login = '', $auth_pass = '') {
2032 # check for feed:http://url
2033 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2035 # check for feed://URL
2036 if (strpos($feed_link, "//") === 0) {
2037 $feed_link = "http:$feed_link";
2040 if ($feed_link == "") return;
2042 if ($cat_id == "0" || !$cat_id) {
2043 $cat_qpart = "NULL";
2045 $cat_qpart = "'$cat_id'";
2048 $result = db_query($link,
2049 "SELECT id FROM ttrss_feeds
2050 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2052 if (db_num_rows($result) == 0) {
2054 $result = db_query($link,
2055 "INSERT INTO ttrss_feeds
2056 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
2057 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2058 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
2060 $result = db_query($link,
2061 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2062 AND owner_uid = " . $_SESSION["uid"]);
2064 $feed_id = db_fetch_result($result, 0, "id");
2067 update_rss_feed($link, $feed_link, $feed_id, true);
2076 function print_feed_select($link, $id, $default_id = "",
2077 $attributes = "", $include_all_feeds = true) {
2079 print "<select id=\"$id\" name=\"$id\" $attributes>";
2080 if ($include_all_feeds) {
2081 print "<option value=\"0\">All feeds</option>";
2084 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2085 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2087 if (db_num_rows($result) > 0 && $include_all_feeds) {
2088 print "<option disabled>--------</option>";
2091 while ($line = db_fetch_assoc($result)) {
2092 if ($line["id"] == $default_id) {
2093 $is_selected = "selected";
2097 printf("<option $is_selected value='%d'>%s</option>",
2098 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
2104 function print_feed_cat_select($link, $id, $default_id = "",
2105 $attributes = "", $include_all_cats = true) {
2107 print "<select id=\"$id\" name=\"$id\" $attributes>";
2109 if ($include_all_cats) {
2110 print "<option value=\"0\">".__('Uncategorized')."</option>";
2113 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2114 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2116 if (db_num_rows($result) > 0 && $include_all_cats) {
2117 print "<option disabled>--------</option>";
2120 while ($line = db_fetch_assoc($result)) {
2121 if ($line["id"] == $default_id) {
2122 $is_selected = "selected";
2126 printf("<option $is_selected value='%d'>%s</option>",
2127 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
2133 function checkbox_to_sql_bool($val) {
2134 return ($val == "on") ? "true" : "false";
2137 function getFeedCatTitle($link, $id) {
2139 return __("Special");
2140 } else if ($id < -10) {
2141 return __("Labels");
2142 } else if ($id > 0) {
2143 $result = db_query($link, "SELECT ttrss_feed_categories.title
2144 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2145 cat_id = ttrss_feed_categories.id");
2146 if (db_num_rows($result) == 1) {
2147 return db_fetch_result($result, 0, "title");
2149 return __("Uncategorized");
2152 return "getFeedCatTitle($id) failed";
2157 function getFeedTitle($link, $id) {
2159 return __("Starred articles");
2160 } else if ($id < -10) {
2161 $label_id = -10 - $id;
2162 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2163 if (db_num_rows($result) == 1) {
2164 return db_fetch_result($result, 0, "description");
2166 return "Unknown label ($label_id)";
2169 } else if ($id > 0) {
2170 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2171 if (db_num_rows($result) == 1) {
2172 return db_fetch_result($result, 0, "title");
2174 return "Unknown feed ($id)";
2177 return "getFeedTitle($id) failed";
2182 function get_session_cookie_name() {
2183 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
2186 function print_init_params($link) {
2187 print "<init-params>";
2188 if ($_SESSION["stored-params"]) {
2189 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2191 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2192 print "<param key=\"$key\" value=\"$value\"/>";
2197 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2198 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
2199 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
2201 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2202 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2204 print "<param key=\"hide_read_feeds\" value=\"" .
2205 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
2207 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2208 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
2210 print "<param key=\"confirm_feed_catchup\" value=\"" .
2211 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
2213 print "<param key=\"cdm_auto_catchup\" value=\"" .
2214 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
2216 print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>";
2218 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>";
2220 print "<param key=\"default_view_mode\" value=\"" .
2221 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2223 print "<param key=\"default_view_limit\" value=\"" .
2224 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
2226 print "<param key=\"prefs_active_tab\" value=\"" .
2227 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2229 print "<param key=\"infobox_disable_overlay\" value=\"" .
2230 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2232 print "</init-params>";
2235 function print_runtime_info($link) {
2236 print "<runtime-info>";
2237 if (ENABLE_UPDATE_DAEMON) {
2238 print "<param key=\"daemon_is_running\" value=\"".
2239 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2241 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
2243 if ($_SESSION["last_version_check"] + 600 < time()) {
2244 $new_version_details = check_for_update($link);
2246 print "<param key=\"new_version_available\" value=\"".
2247 sprintf("%d", $new_version_details != ""). "\"/>";
2249 $_SESSION["last_version_check"] = time();
2253 print "</runtime-info>";
2256 function getSearchSql($search, $match_on) {
2258 $search_query_part = "";
2260 $keywords = split(" ", $search);
2261 $query_keywords = array();
2263 if ($match_on == "both") {
2265 foreach ($keywords as $k) {
2266 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2267 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2270 $search_query_part = implode("AND", $query_keywords) . " AND ";
2272 } else if ($match_on == "title") {
2274 foreach ($keywords as $k) {
2275 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2278 $search_query_part = implode("AND", $query_keywords) . " AND ";
2280 } else if ($match_on == "content") {
2282 foreach ($keywords as $k) {
2283 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2287 $search_query_part = implode("AND", $query_keywords);
2289 return $search_query_part;
2292 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0) {
2296 $search_query_part = getSearchSql($search, $match_on);
2297 $search_query_part .= " AND ";
2300 $search_query_part = "";
2303 $view_query_part = "";
2305 if ($view_mode == "adaptive") {
2307 $view_query_part = " ";
2308 } else if ($feed != -1) {
2309 $unread = getFeedUnread($link, $feed, $cat_view);
2311 $view_query_part = " unread = true AND ";
2316 if ($view_mode == "marked") {
2317 $view_query_part = " marked = true AND ";
2320 if ($view_mode == "unread") {
2321 $view_query_part = " unread = true AND ";
2325 $limit_query_part = "LIMIT " . $limit;
2328 $vfeed_query_part = "";
2330 // override query strategy and enable feed display when searching globally
2331 if ($search && $search_mode == "all_feeds") {
2332 $query_strategy_part = "ttrss_entries.id > 0";
2333 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2334 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2335 $query_strategy_part = "ttrss_entries.id > 0";
2336 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2337 id = feed_id) as feed_title,";
2338 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2340 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2342 $tmp_result = false;
2345 $tmp_result = db_query($link, "SELECT id
2346 FROM ttrss_feeds WHERE cat_id = '$feed'");
2348 $tmp_result = db_query($link, "SELECT id
2349 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2350 WHERE id = '$feed') AND id != '$feed'");
2353 $cat_siblings = array();
2355 if (db_num_rows($tmp_result) > 0) {
2356 while ($p = db_fetch_assoc($tmp_result)) {
2357 array_push($cat_siblings, "feed_id = " . $p["id"]);
2360 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2361 $feed, implode(" OR ", $cat_siblings));
2364 $query_strategy_part = "ttrss_entries.id > 0";
2367 } else if ($feed >= 0) {
2372 $query_strategy_part = "cat_id = '$feed'";
2374 $query_strategy_part = "cat_id IS NULL";
2377 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2380 $tmp_result = db_query($link, "SELECT id
2381 FROM ttrss_feeds WHERE parent_feed = '$feed'
2382 ORDER BY cat_id,title");
2384 $parent_ids = array();
2386 if (db_num_rows($tmp_result) > 0) {
2387 while ($p = db_fetch_assoc($tmp_result)) {
2388 array_push($parent_ids, "feed_id = " . $p["id"]);
2391 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2392 $feed, implode(" OR ", $parent_ids));
2394 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2396 $query_strategy_part = "feed_id = '$feed'";
2399 } else if ($feed == -1) { // starred virtual feed
2400 $query_strategy_part = "marked = true";
2401 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2402 } else if ($feed <= -10) { // labels
2403 $label_id = -$feed - 11;
2405 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2406 WHERE id = '$label_id'");
2408 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2410 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2412 $query_strategy_part = "id > 0"; // dumb
2415 if (get_pref($link, 'REVERSE_HEADLINES')) {
2416 $order_by = "updated";
2418 $order_by = "updated DESC";
2421 if ($override_order) {
2422 $order_by = $override_order;
2427 if ($search && $search_mode == "all_feeds") {
2428 $feed_title = __("Global search results")." ($search)";
2429 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2430 $feed_title = __("Tag search results")." ($search, $feed)";
2431 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2432 $feed_title = $feed;
2433 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2438 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2439 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2440 $feed_title = db_fetch_result($result, 0, "title");
2442 $feed_title = __("Uncategorized");
2446 $feed_title = __("Category search results")." ($search, $feed_title)";
2451 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2452 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2454 $feed_title = db_fetch_result($result, 0, "title");
2455 $feed_site_url = db_fetch_result($result, 0, "site_url");
2456 $last_error = db_fetch_result($result, 0, "last_error");
2459 $feed_title = __("Feed search results") . " ($search, $feed_title)";
2463 } else if ($feed == -1) {
2464 $feed_title = __("Starred articles");
2465 } else if ($feed < -10) {
2466 $label_id = -$feed - 11;
2467 $result = db_query($link, "SELECT description FROM ttrss_labels
2468 WHERE id = '$label_id'");
2469 $feed_title = db_fetch_result($result, 0, "description");
2472 $feed_title = __("Label search results") . " ($search, $feed_title)";
2478 $feed_title = db_unescape_string($feed_title);
2480 if ($feed < -10) error_reporting (0);
2482 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2485 $feed_kind = "Feeds";
2487 $feed_kind = "Labels";
2490 $content_query_part = "content as content_preview,";
2492 if ($limit_query_part) {
2493 $offset_query_part = "OFFSET $offset";
2498 ttrss_entries.id,ttrss_entries.title,
2499 SUBSTRING(updated,1,16) as updated,
2500 unread,feed_id,marked,link,last_read,
2501 SUBSTRING(last_read,1,19) as last_read_noms,
2504 SUBSTRING(updated,1,19) as updated_noms,
2507 ttrss_entries,ttrss_user_entries,ttrss_feeds
2509 ttrss_feeds.hidden = false AND
2510 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2511 ttrss_user_entries.ref_id = ttrss_entries.id AND
2512 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2515 $query_strategy_part ORDER BY $order_by
2516 $limit_query_part $offset_query_part";
2518 $result = db_query($link, $query);
2520 if ($_GET["debug"]) print $query;
2525 $feed_kind = "Tags";
2527 $result = db_query($link, "SELECT
2529 ttrss_entries.id as id,title,
2530 SUBSTRING(updated,1,16) as updated,
2532 marked,link,last_read,
2533 SUBSTRING(last_read,1,19) as last_read_noms,
2536 SUBSTRING(updated,1,19) as updated_noms
2538 ttrss_entries,ttrss_user_entries,ttrss_tags
2540 ref_id = ttrss_entries.id AND
2541 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2542 post_int_id = int_id AND tag_name = '$feed' AND
2545 $query_strategy_part ORDER BY $order_by
2546 $limit_query_part");
2549 return array($result, $feed_title, $feed_site_url, $last_error);
2553 function generate_syndicated_feed($link, $feed, $is_cat,
2554 $search, $search_mode, $match_on) {
2556 $qfh_ret = queryFeedHeadlines($link, $feed,
2557 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2559 $result = $qfh_ret[0];
2560 $feed_title = htmlspecialchars($qfh_ret[1]);
2561 $feed_site_url = $qfh_ret[2];
2562 $last_error = $qfh_ret[3];
2564 print "<rss version=\"2.0\">
2566 <title>$feed_title</title>
2567 <link>$feed_site_url</link>
2568 <generator>Tiny Tiny RSS v".VERSION."</generator>";
2570 while ($line = db_fetch_assoc($result)) {
2572 print "<id>" . htmlspecialchars($line["guid"]) . "</id>";
2573 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2575 $rfc822_date = date('r', strtotime($line["updated"]));
2577 print "<pubDate>$rfc822_date</pubDate>";
2580 htmlspecialchars($line["title"]) . "</title>";
2582 print "<description>" .
2583 htmlspecialchars($line["content_preview"]) . "</description>";
2588 print "</channel></rss>";
2592 function getCategoryTitle($link, $cat_id) {
2594 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2597 if (db_num_rows($result) == 1) {
2598 return db_fetch_result($result, 0, "title");
2600 return "Uncategorized";
2604 function sanitize_rss($str) {
2607 $res = preg_replace('/<script.*?>/i',
2608 "<p class=\"scriptWarn\">Disabled script: ", $res);
2610 $res = preg_replace('/<\/script.*?>/i', "</p>", $res);
2612 /* $res = preg_replace('/<embed.*?>/i', "", $res);
2614 $res = preg_replace('/<object.*?>.*?<\/object>/i',
2615 "<p class=\"objectWarn\">(Disabled html object
2616 - flash or other embedded content)</p>", $res); */
2621 function send_headlines_digests($link, $limit = 100) {
2623 if (!DIGEST_ENABLE) return false;
2625 $user_limit = DIGEST_EMAIL_LIMIT;
2628 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2630 if (DB_TYPE == "pgsql") {
2631 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2632 } else if (DB_TYPE == "mysql") {
2633 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2636 $result = db_query($link, "SELECT id,email FROM ttrss_users
2637 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2639 while ($line = db_fetch_assoc($result)) {
2640 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2641 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2643 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2644 $digest = $tuple[0];
2645 $headlines_count = $tuple[1];
2647 if ($headlines_count > 0) {
2648 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2649 "[tt-rss] New headlines for last 24 hours", $digest,
2650 "From: " . MAIL_FROM . "\n".
2651 "Content-Type: text/plain; charset=\"utf-8\"\n".
2652 "Content-Transfer-Encoding: 8bit\n");
2654 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2655 WHERE id = " . $line["id"]);
2657 print "No headlines\n";
2662 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2666 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2667 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
2668 $tmp .= "=======================================================\n\n";
2670 if (DB_TYPE == "pgsql") {
2671 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
2672 } else if (DB_TYPE == "mysql") {
2673 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2676 $result = db_query($link, "SELECT ttrss_entries.title,
2677 ttrss_feeds.title AS feed_title,
2680 SUBSTRING(last_updated,1,19) AS last_updated
2682 ttrss_user_entries,ttrss_entries,ttrss_feeds
2684 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
2685 AND include_in_digest = true
2687 AND ttrss_user_entries.owner_uid = $user_id
2688 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2691 $cur_feed_title = "";
2693 $headlines_count = db_num_rows($result);
2695 while ($line = db_fetch_assoc($result)) {
2696 $updated = smart_date_time(strtotime($line["last_updated"]));
2697 $feed_title = $line["feed_title"];
2699 if ($cur_feed_title != $feed_title) {
2700 $cur_feed_title = $feed_title;
2702 $tmp .= "$feed_title\n\n";
2705 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2706 $tmp .= " " . trim($line["link"]) . "\n";
2711 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
2712 DIGEST_HOSTNAME . "\n".
2713 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
2716 return array($tmp, $headlines_count);
2719 function check_for_update($link, $brief_fmt = true) {
2720 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2722 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
2727 $rss = fetch_rss($releases_feed);
2728 error_reporting (DEFAULT_ERROR_LEVEL);
2732 $items = $rss->items;
2734 if (!$items || !is_array($items)) $items = $rss->entries;
2735 if (!$items || !is_array($items)) $items = $rss;
2737 if (!is_array($items) || count($items) == 0) {
2741 $latest_item = $items[0];
2743 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
2745 $release_url = sanitize_rss($latest_item["link"]);
2746 $content = sanitize_rss($latest_item["description"]);
2748 if (version_compare(VERSION, $latest_version) == -1) {
2750 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
2751 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
2752 <div id=\"milestoneDetails\">$content</div>");
2754 return "New version of Tiny-Tiny RSS ($latest_version) is available:
2755 <div class='milestoneDetails'>$content</div>
2756 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
2757 download and update information.";
2764 function markArticlesById($link, $ids, $cmode) {
2768 foreach ($ids as $id) {
2769 array_push($tmp_ids, "ref_id = '$id'");
2772 $ids_qpart = join(" OR ", $tmp_ids);
2775 db_query($link, "UPDATE ttrss_user_entries SET
2776 marked = false,last_read = NOW()
2777 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2778 } else if ($cmode == 1) {
2779 db_query($link, "UPDATE ttrss_user_entries SET
2781 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2783 db_query($link, "UPDATE ttrss_user_entries SET
2784 marked = NOT marked,last_read = NOW()
2785 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2789 function catchupArticlesById($link, $ids, $cmode) {
2793 foreach ($ids as $id) {
2794 array_push($tmp_ids, "ref_id = '$id'");
2797 $ids_qpart = join(" OR ", $tmp_ids);
2800 db_query($link, "UPDATE ttrss_user_entries SET
2801 unread = false,last_read = NOW()
2802 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2803 } else if ($cmode == 1) {
2804 db_query($link, "UPDATE ttrss_user_entries SET
2806 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2808 db_query($link, "UPDATE ttrss_user_entries SET
2809 unread = NOT unread,last_read = NOW()
2810 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2814 function catchupArticleById($link, $id, $cmode) {
2817 db_query($link, "UPDATE ttrss_user_entries SET
2818 unread = false,last_read = NOW()
2819 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2820 } else if ($cmode == 1) {
2821 db_query($link, "UPDATE ttrss_user_entries SET
2823 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2825 db_query($link, "UPDATE ttrss_user_entries SET
2826 unread = NOT unread,last_read = NOW()
2827 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
2831 function escape_for_form($s) {
2832 return htmlspecialchars(db_unescape_string($s));
2835 function make_guid_from_title($title) {
2836 return preg_replace("/[ \"\',.:;]/", "-",
2837 mb_strtolower(strip_tags($title)));
2840 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2841 $bottom = false, $rtl_content = false, $feed_id = 0,
2842 $is_cat = false, $search = false, $match_on = false,
2843 $search_mode = false, $offset = 0, $limit = 0) {
2845 $user_page_offset = $offset + 1;
2848 $class = "headlinesSubToolbar";
2849 $tid = "headlineActionsTop";
2851 $class = "headlinesSubToolbar";
2852 $tid = "headlineActionsBottom";
2855 print "<table class=\"$class\" id=\"$tid\"
2856 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2864 $page_prev_link = "javascript:viewFeedGoPage(-1)";
2865 $page_next_link = "javascript:viewFeedGoPage(1)";
2866 $page_first_link = "javascript:viewFeedGoPage(0)";
2868 $catchup_page_link = "javascript:catchupPage()";
2869 $catchup_feed_link = "javascript:catchupCurrentFeed()";
2871 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2873 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
2874 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
2875 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
2877 $tog_unread_link = "javascript:selectionToggleUnread()";
2878 $tog_marked_link = "javascript:selectionToggleMarked()";
2882 $sel_all_link = "javascript:cdmSelectArticles('all')";
2883 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
2884 $sel_none_link = "javascript:cdmSelectArticles('none')";
2886 $tog_unread_link = "javascript:selectionToggleUnread(true)";
2887 $tog_marked_link = "javascript:selectionToggleMarked(true)";
2891 if (!strstr($_SESSION["client.userAgent"], "MSIE")) {
2893 print "<td class=\"headlineActions$rtl_cpart\">
2894 <ul class=\"headlineDropdownMenu\">
2897 <a href=\"$sel_all_link\">".__('All')."</a>,
2898 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
2899 <a href=\"$sel_none_link\">".__('None')."</a></li>
2900 <li class=\"vsep\"> </li>
2901 <li class=\"top\">Toggle<ul>
2902 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
2903 <li onclick=\"$tog_marked_link\">".__('Starred')."</li></ul></li>
2904 <li class=\"vsep\"> </li>
2905 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
2906 <li onclick=\"$catchup_page_link\">".__('This page')."</li>
2907 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
2908 <li class=\"vsep\"> </li>";
2912 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
2913 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
2914 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
2922 // old style subtoolbar:
2924 print "<td class=\"headlineActions$rtl_cpart\">".
2926 <a href=\"$sel_all_link\">".__('All')."</a>,
2927 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
2928 <a href=\"$sel_none_link\">".__('None')."</a>
2930 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
2931 <a href=\"$tog_marked_link\">".__('Starred')."</a>
2933 __('Mark as read:')."
2934 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
2935 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
2940 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
2941 print "<td class=\"headlineActions$rtl_cpart\">
2942 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2943 '$match_on', '$feed_id', '$is_cat');\">
2944 ".__('Convert to Label')."</a></td>";
2947 print "<td class=\"headlineTitle$rtl_cpart\">";
2949 if ($feed_site_url) {
2951 $target = "target=\"_blank\"";
2953 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
2959 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
2962 if ($user_page_offset > 1) {
2963 print " [$user_page_offset] ";
2969 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
2970 <img class=\"noborder\"
2971 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
2976 print "</tr></table>";
2980 function outputFeedList($link, $tags = false) {
2982 print "<ul class=\"feedList\" id=\"feedList\">";
2984 $owner_uid = $_SESSION["uid"];
2988 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2989 print "<li class=\"feedCat\">".__('Special')."</li>";
2990 print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
2993 $num_starred = getFeedUnread($link, -1);
2997 if ($num_starred > 0) $class .= "Unread";
2999 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
3000 "images/mark_set.png", $link);
3002 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3008 if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
3010 $result = db_query($link, "SELECT id,sql_exp,description FROM
3011 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3013 if (db_num_rows($result) > 0) {
3014 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3015 print "<li class=\"feedCat\">".__('Labels')."</li>";
3016 print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
3018 print "<li><hr></li>";
3022 while ($line = db_fetch_assoc($result)) {
3024 error_reporting (0);
3026 $label_id = -$line['id'] - 11;
3027 $count = getFeedUnread($link, $label_id);
3035 error_reporting (DEFAULT_ERROR_LEVEL);
3037 printFeedEntry($label_id,
3038 $class, db_unescape_string($line["description"]),
3039 $count, "images/label.png", $link);
3043 if (db_num_rows($result) > 0) {
3044 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3051 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3052 print "<li><hr></li>";
3055 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3056 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3057 $order_by_qpart = "category,unread DESC,title";
3059 $order_by_qpart = "category,title";
3062 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3063 $order_by_qpart = "unread DESC,title";
3065 $order_by_qpart = "title";
3069 $result = db_query($link, "SELECT ttrss_feeds.*,
3070 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3071 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3072 WHERE feed_id = ttrss_feeds.id AND unread = true
3073 AND ttrss_user_entries.ref_id = ttrss_entries.id
3074 AND owner_uid = '$owner_uid') as unread,
3076 ttrss_feed_categories.title AS category,
3077 ttrss_feed_categories.collapsed
3078 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3079 ON (ttrss_feed_categories.id = cat_id)
3081 ttrss_feeds.hidden = false AND
3082 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3083 ORDER BY $order_by_qpart");
3085 $actid = $_GET["actid"];
3095 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3097 while ($line = db_fetch_assoc($result)) {
3099 $feed = db_unescape_string($line["title"]);
3100 $feed_id = $line["id"];
3102 $subop = $_GET["subop"];
3104 $unread = $line["unread"];
3106 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3107 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3109 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3112 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3115 $rtl_tag = "dir=\"RTL\"";
3120 $tmp_result = db_query($link,
3121 "SELECT id,COUNT(unread) AS unread
3122 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3123 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3124 WHERE parent_feed = '$feed_id' AND unread = true
3125 GROUP BY ttrss_feeds.id");
3127 if (db_num_rows($tmp_result) > 0) {
3128 while ($l = db_fetch_assoc($tmp_result)) {
3129 $unread += $l["unread"];
3133 $cat_id = $line["cat_id"];
3135 $tmp_category = $line["category"];
3137 if (!$tmp_category) {
3138 $tmp_category = __("Uncategorized");
3141 // $class = ($lnum % 2) ? "even" : "odd";
3143 if ($line["last_error"]) {
3149 if ($unread > 0) $class .= "Unread";
3151 if ($actid == $feed_id) {
3152 $class .= "Selected";
3155 $total_unread += $unread;
3157 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3163 $category = $tmp_category;
3165 $collapsed = $line["collapsed"];
3167 // workaround for NULL category
3168 if ($category == __("Uncategorized")) {
3169 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3174 if ($collapsed == "t" || $collapsed == "1") {
3175 $holder_class = "invisible";
3178 $holder_class = "feedCatHolder";
3182 $cat_id = sprintf("%d", $cat_id);
3184 $cat_unread = getCategoryUnread($link, $cat_id);
3186 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3188 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3189 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
3190 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
3191 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3192 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3195 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
3196 // -> keyboard navigation, etc.
3197 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
3200 printFeedEntry($feed_id, $class, $feed, $unread,
3201 ICONS_DIR."/$feed_id.ico", $link, $rtl_content,
3202 $last_updated, $line["last_error"]);
3207 if (db_num_rows($result) == 0) {
3208 print "<li>".__('No feeds to display.')."</li>";
3215 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3216 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3217 post_int_id = ttrss_user_entries.int_id AND
3218 unread = true AND ref_id = ttrss_entries.id
3219 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3221 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3222 ORDER BY tag_name"); */
3224 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3225 print "<li class=\"feedCat\">".__('Tags')."</li>";
3226 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3229 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3230 FROM ttrss_user_entries WHERE int_id = post_int_id
3231 AND unread = true)) AS count FROM ttrss_tags
3232 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
3236 while ($line = db_fetch_assoc($result)) {
3237 $tags[$line["tag_name"]] += $line["count"];
3240 foreach (array_keys($tags) as $tag) {
3242 $unread = $tags[$tag];
3250 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3254 if (db_num_rows($result) == 0) {
3255 print "<li>No tags to display.</li>";
3258 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3268 function get_article_tags($link, $id) {
3270 $a_id = db_escape_string($id);
3272 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3273 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
3274 ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1) ORDER BY tag_name");
3278 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3279 array_push($tags, $tmp_line["tag_name"]);
3285 function trim_value(&$value) {
3286 $value = trim($value);
3289 function trim_array($array) {
3291 array_walk($tmp, 'trim_value');
3295 function tag_is_valid($tag) {
3296 if ($tag == '') return false;
3297 if (preg_match("/^[0-9]*$/", $tag)) return false;
3299 $tag = iconv("utf-8", "utf-8", $tag);
3300 if (!$tag) return false;
3305 function render_login_form($link, $mobile = false) {
3307 require_once "login_form.php";
3309 require_once "mobile/login_form.php";
3313 // from http://developer.apple.com/internet/safari/faq.html
3314 function no_cache_incantation() {
3315 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3316 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3317 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3318 header("Cache-Control: post-check=0, pre-check=0", false);
3319 header("Pragma: no-cache"); // HTTP/1.0
3322 function format_warning($msg, $id = "") {
3323 return "<div class=\"warning\" id=\"$id\">
3324 <img src=\"images/sign_excl.png\">$msg</div>";
3327 function format_notice($msg) {
3328 return "<div class=\"notice\">
3329 <img src=\"images/sign_info.png\">$msg</div>";
3332 function format_error($msg) {
3333 return "<div class=\"error\">
3334 <img src=\"images/sign_excl.png\">$msg</div>";
3337 function print_notice($msg) {
3338 return print format_notice($msg);
3341 function print_warning($msg) {
3342 return print format_warning($msg);
3345 function print_error($msg) {
3346 return print format_error($msg);
3350 function T_sprintf() {
3351 $args = func_get_args();
3352 return vsprintf(__(array_shift($args)), $args);