]>
git.wh0rd.org - tt-rss.git/blob - functions.php
bb0217d3ebfa50321af89e338b41b2ac1f83e9e5
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 'config.php';
10 require_once 'db-prefs.php';
11 require_once 'compat.php';
12 require_once 'errors.php';
13 require_once 'version.php';
15 require_once 'magpierss/rss_utils.inc';
17 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
19 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
23 if (DB_TYPE
== "pgsql") {
24 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
25 marked = false AND feed_id = '$feed_id' AND
26 (SELECT date_entered FROM ttrss_entries WHERE
27 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
29 $pg_version = get_pgsql_version($link);
31 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
33 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
34 ttrss_entries.id = ref_id AND
36 feed_id = '$feed_id' AND
37 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
41 $result = db_query($link, "DELETE FROM ttrss_user_entries
43 WHERE ttrss_entries.id = ref_id AND
45 feed_id = '$feed_id' AND
46 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
49 $rows = pg_affected_rows($result);
53 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
54 marked = false AND feed_id = '$feed_id' AND
55 (SELECT date_entered FROM ttrss_entries WHERE
56 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
58 $result = db_query($link, "DELETE FROM ttrss_user_entries
59 USING ttrss_user_entries, ttrss_entries
60 WHERE ttrss_entries.id = ref_id AND
62 feed_id = '$feed_id' AND
63 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
65 $rows = mysql_affected_rows($link);
70 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
74 function global_purge_old_posts($link, $do_output = false, $limit = false) {
76 $random_qpart = sql_random_function();
79 $limit_qpart = "LIMIT $limit";
84 $result = db_query($link,
85 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
86 ORDER BY $random_qpart $limit_qpart");
88 while ($line = db_fetch_assoc($result)) {
90 $feed_id = $line["id"];
91 $purge_interval = $line["purge_interval"];
92 $owner_uid = $line["owner_uid"];
94 if ($purge_interval == 0) {
96 $tmp_result = db_query($link,
97 "SELECT value FROM ttrss_user_prefs WHERE
98 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
100 if (db_num_rows($tmp_result) != 0) {
101 $purge_interval = db_fetch_result($tmp_result, 0, "value");
106 // print "Feed $feed_id: purge interval = $purge_interval\n";
109 if ($purge_interval > 0) {
110 purge_feed($link, $feed_id, $purge_interval, $do_output);
114 // purge orphaned posts in main content table
115 db_query($link, "DELETE FROM ttrss_entries WHERE
116 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
120 function purge_old_posts($link) {
122 $user_id = $_SESSION["uid"];
124 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
125 WHERE owner_uid = '$user_id'");
127 while ($line = db_fetch_assoc($result)) {
129 $feed_id = $line["id"];
130 $purge_interval = $line["purge_interval"];
132 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
134 if ($purge_interval > 0) {
135 purge_feed($link, $feed_id, $purge_interval);
139 // purge orphaned posts in main content table
140 db_query($link, "DELETE FROM ttrss_entries WHERE
141 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
144 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
146 if (WEB_DEMO_MODE
) return;
149 $user_id = $_SESSION["uid"];
150 purge_old_posts($link);
153 // db_query($link, "BEGIN");
155 if (MAX_UPDATE_TIME
> 0) {
156 if (DB_TYPE
== "mysql") {
159 $q_order = "RANDOM()";
162 $q_order = "last_updated DESC";
165 $result = db_query($link, "SELECT feed_url,id,
166 SUBSTRING(last_updated,1,19) AS last_updated,
167 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
172 while ($line = db_fetch_assoc($result)) {
173 $upd_intl = $line["update_interval"];
175 if (!$upd_intl ||
$upd_intl == 0) {
176 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
180 // Updates for this feed are disabled
184 if ($fetch ||
(!$line["last_updated"] ||
185 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
187 // print "<!-- feed: ".$line["feed_url"]." -->";
189 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
191 $upd_elapsed = time() - $upd_start;
193 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
199 // db_query($link, "COMMIT");
203 function check_feed_favicon($feed_url, $feed, $link) {
204 $feed_url = str_replace("http://", "", $feed_url);
205 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
207 $icon_url = "http://$feed_url/favicon.ico";
208 $icon_file = ICONS_DIR
. "/$feed.ico";
210 if (!file_exists($icon_file)) {
213 $r = fopen($icon_url, "r");
214 error_reporting (DEFAULT_ERROR_LEVEL
);
217 $tmpfname = tempnam(TMP_DIRECTORY
, "ttrssicon");
219 $t = fopen($tmpfname, "w");
222 $buf = fread($r, 16384);
230 if (!rename($tmpfname, $icon_file)) {
234 chmod($icon_file, 0644);
236 error_reporting (DEFAULT_ERROR_LEVEL
);
242 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
244 if (WEB_DEMO_MODE
) return;
246 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
250 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
251 FROM ttrss_feeds WHERE id = '$feed'");
253 $auth_login = db_fetch_result($result, 0, "auth_login");
254 $auth_pass = db_fetch_result($result, 0, "auth_pass");
256 $update_interval = db_fetch_result($result, 0, "update_interval");
258 if ($update_interval < 0) { return; }
260 $feed = db_escape_string($feed);
262 $fetch_url = $feed_url;
264 if ($auth_login && $auth_pass) {
265 $url_parts = array();
266 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
268 if ($url_parts[1] && $url_parts[2]) {
269 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
274 $rss = fetch_rss($fetch_url);
276 error_reporting (DEFAULT_ERROR_LEVEL
);
278 $feed = db_escape_string($feed);
282 // db_query($link, "BEGIN");
284 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
285 FROM ttrss_feeds WHERE id = '$feed'");
287 $registered_title = db_fetch_result($result, 0, "title");
288 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
289 $orig_site_url = db_fetch_result($result, 0, "site_url");
291 $owner_uid = db_fetch_result($result, 0, "owner_uid");
293 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
294 check_feed_favicon($feed_url, $feed, $link);
297 if (!$registered_title ||
$registered_title == "[Unknown]") {
298 $feed_title = db_escape_string($rss->channel
["title"]);
299 db_query($link, "UPDATE ttrss_feeds SET
300 title = '$feed_title' WHERE id = '$feed'");
303 $site_url = $rss->channel
["link"];
304 // weird, weird Magpie
305 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
307 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
308 db_query($link, "UPDATE ttrss_feeds SET
309 site_url = '$site_url' WHERE id = '$feed'");
312 // print "I: " . $rss->channel["image"]["url"];
314 $icon_url = $rss->image
["url"];
316 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
317 $icon_url = db_escape_string($icon_url);
318 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
324 $result = db_query($link, "SELECT reg_exp,
325 ttrss_filter_types.name AS name,
326 ttrss_filter_actions.name AS action
327 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
328 owner_uid = $owner_uid AND
329 ttrss_filter_types.id = filter_type AND
330 ttrss_filter_actions.id = action_id AND
331 (feed_id IS NULL OR feed_id = '$feed')");
333 while ($line = db_fetch_assoc($result)) {
334 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
336 $filter["reg_exp"] = $line["reg_exp"];
337 $filter["action"] = $line["action"];
339 array_push($filters[$line["name"]], $filter);
342 $iterator = $rss->items
;
344 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
345 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
347 if (!is_array($iterator)) {
348 db_query($link, "UPDATE ttrss_feeds
349 SET last_error = 'Parse error: can\'t find any articles.'
350 WHERE id = '$feed'");
354 foreach ($iterator as $item) {
356 $entry_guid = $item["id"];
358 if (!$entry_guid) $entry_guid = $item["guid"];
359 if (!$entry_guid) $entry_guid = $item["link"];
361 if (!$entry_guid) continue;
363 $entry_timestamp = "";
365 $rss_2_date = $item['pubdate'];
366 $rss_1_date = $item['dc']['date'];
367 $atom_date = $item['issued'];
368 if (!$atom_date) $atom_date = $item['updated'];
370 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
371 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
372 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
374 if ($entry_timestamp == "") {
375 $entry_timestamp = time();
376 $no_orig_date = 'true';
378 $no_orig_date = 'false';
381 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
383 $entry_title = $item["title"];
385 // strange Magpie workaround
386 $entry_link = $item["link_"];
387 if (!$entry_link) $entry_link = $item["link"];
389 if (!$entry_title) continue;
390 if (!$entry_link) continue;
392 $entry_content = $item["content:escaped"];
394 if (!$entry_content) $entry_content = $item["content:encoded"];
395 if (!$entry_content) $entry_content = $item["content"];
396 if (!$entry_content) $entry_content = $item["summary"];
397 if (!$entry_content) $entry_content = $item["description"];
399 // if (!$entry_content) continue;
402 if (is_array($entry_content)) {
403 $entry_content = $entry_content["encoded"];
404 if (!$entry_content) $entry_content = $entry_content["escaped"];
408 // print_r(htmlspecialchars($entry_content));
411 $entry_content_unescaped = $entry_content;
412 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
414 $entry_comments = $item["comments"];
416 $entry_author = db_escape_string($item['dc']['creator']);
418 $entry_guid = db_escape_string($entry_guid);
420 $result = db_query($link, "SELECT id FROM ttrss_entries
421 WHERE guid = '$entry_guid'");
423 $entry_content = db_escape_string($entry_content);
424 $entry_title = db_escape_string($entry_title);
425 $entry_link = db_escape_string($entry_link);
426 $entry_comments = db_escape_string($entry_comments);
428 $num_comments = db_escape_string($item["slash"]["comments"]);
430 if (!$num_comments) $num_comments = 0;
432 db_query($link, "BEGIN");
434 if (db_num_rows($result) == 0) {
436 // base post entry does not exist, create it
438 $result = db_query($link,
439 "INSERT INTO ttrss_entries
455 '$entry_timestamp_fmt',
464 // we keep encountering the entry in feeds, so we need to
465 // update date_entered column so that we don't get horrible
466 // dupes when the entry gets purged and reinserted again e.g.
467 // in the case of SLOW SLOW OMG SLOW updating feeds
469 $base_entry_id = db_fetch_result($result, 0, "id");
471 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
472 WHERE id = '$base_entry_id'");
475 // now it should exist, if not - bad luck then
477 $result = db_query($link, "SELECT
478 id,content_hash,no_orig_date,title,
479 substring(date_entered,1,19) as date_entered,
480 substring(updated,1,19) as updated,
484 WHERE guid = '$entry_guid'");
486 if (db_num_rows($result) == 1) {
488 // this will be used below in update handler
489 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
490 $orig_title = db_fetch_result($result, 0, "title");
491 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
492 $orig_date_entered = strtotime(db_fetch_result($result,
495 $ref_id = db_fetch_result($result, 0, "id");
497 // check for user post link to main table
499 // do we allow duplicate posts with same GUID in different feeds?
500 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
501 $dupcheck_qpart = "AND feed_id = '$feed'";
503 $dupcheck_qpart = "";
506 // error_reporting(0);
508 $filter_name = get_filter_name($entry_title, $entry_content,
509 $entry_link, $filters);
511 if ($filter_name == "filter") {
515 // error_reporting (DEFAULT_ERROR_LEVEL);
517 $result = db_query($link,
518 "SELECT ref_id FROM ttrss_user_entries WHERE
519 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
522 // okay it doesn't exist - create user entry
523 if (db_num_rows($result) == 0) {
525 if ($filter_name != 'catchup') {
527 $last_read_qpart = 'NULL';
530 $last_read_qpart = 'NOW()';
533 $result = db_query($link,
534 "INSERT INTO ttrss_user_entries
535 (ref_id, owner_uid, feed_id, unread, last_read)
536 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
540 $post_needs_update = false;
542 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
543 ($content_hash != $orig_content_hash)) {
544 $post_needs_update = true;
547 if ($orig_title != $entry_title) {
548 $post_needs_update = true;
551 if ($orig_num_comments != $num_comments) {
552 $post_needs_update = true;
555 // this doesn't seem to be very reliable
557 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
558 // $post_needs_update = true;
561 // if post needs update, update it and mark all user entries
562 // linking to this post as updated
563 if ($post_needs_update) {
565 // print "<!-- post $orig_title needs update : $post_needs_update -->";
567 db_query($link, "UPDATE ttrss_entries
568 SET title = '$entry_title', content = '$entry_content',
569 num_comments = '$num_comments'
570 WHERE id = '$ref_id'");
572 db_query($link, "UPDATE ttrss_user_entries
573 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
578 db_query($link, "COMMIT");
581 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
585 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
586 $entry_content_unescaped, $entry_tags);
588 // print "<br>$entry_title : $entry_content_unescaped<br>";
589 // print_r($entry_tags);
592 $entry_tags = $entry_tags[1];
594 if (count($entry_tags) > 0) {
596 db_query($link, "BEGIN");
598 $result = db_query($link, "SELECT id,int_id
599 FROM ttrss_entries,ttrss_user_entries
600 WHERE guid = '$entry_guid'
601 AND feed_id = '$feed' AND ref_id = id
602 AND owner_uid = '$owner_uid'");
604 if (db_num_rows($result) == 1) {
606 $entry_id = db_fetch_result($result, 0, "id");
607 $entry_int_id = db_fetch_result($result, 0, "int_id");
609 foreach ($entry_tags as $tag) {
610 $tag = db_escape_string(strtolower($tag));
612 $tag = str_replace("+", " ", $tag);
613 $tag = str_replace("technorati tag: ", "", $tag);
615 $result = db_query($link, "SELECT id FROM ttrss_tags
616 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
617 owner_uid = '$owner_uid' LIMIT 1");
619 // print db_fetch_result($result, 0, "id");
621 if ($result && db_num_rows($result) == 0) {
623 // print "tagging $entry_id as $tag<br>";
625 db_query($link, "INSERT INTO ttrss_tags
626 (owner_uid,tag_name,post_int_id)
627 VALUES ('$owner_uid','$tag', '$entry_int_id')");
631 db_query($link, "COMMIT");
635 db_query($link, "UPDATE ttrss_feeds
636 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
638 // db_query($link, "COMMIT");
641 $error_msg = db_escape_string(magpie_error());
643 "UPDATE ttrss_feeds SET last_error = '$error_msg',
644 last_updated = NOW() WHERE id = '$feed'");
649 function print_select($id, $default, $values, $attributes = "") {
650 print "<select id=\"$id\" $attributes>";
651 foreach ($values as $v) {
657 print "<option$sel>$v</option>";
662 function get_filter_name($title, $content, $link, $filters) {
664 if ($filters["title"]) {
665 foreach ($filters["title"] as $filter) {
666 $reg_exp = $filter["reg_exp"];
667 if (preg_match("/$reg_exp/i", $title)) {
668 return $filter["action"];
673 if ($filters["content"]) {
674 foreach ($filters["content"] as $filter) {
675 $reg_exp = $filter["reg_exp"];
676 if (preg_match("/$reg_exp/i", $content)) {
677 return $filter["action"];
682 if ($filters["both"]) {
683 foreach ($filters["both"] as $filter) {
684 $reg_exp = $filter["reg_exp"];
685 if (preg_match("/$reg_exp/i", $title) ||
686 preg_match("/$reg_exp/i", $content)) {
687 return $filter["action"];
692 if ($filters["link"]) {
693 $reg_exp = $filter["reg_exp"];
694 foreach ($filters["link"] as $filter) {
695 $reg_exp = $filter["reg_exp"];
696 if (preg_match("/$reg_exp/i", $link)) {
697 return $filter["action"];
705 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
706 $rtl_content = false, $last_updated = false, $last_error = false) {
708 if (file_exists($icon_file) && filesize($icon_file) > 0) {
709 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
711 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
715 $rtl_tag = "dir=\"rtl\"";
717 $rtl_tag = "dir=\"ltr\"";
721 $link_title = "Error: $last_error ($last_updated)";
722 } else if ($last_updated) {
723 $link_title = "Updated: $last_updated";
726 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
728 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
729 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
733 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
738 $fctr_class = "class=\"invisible\"";
741 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
742 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
748 function getmicrotime() {
749 list($usec, $sec) = explode(" ",microtime());
750 return ((float)$usec +
(float)$sec);
753 function print_radio($id, $default, $values, $attributes = "") {
754 foreach ($values as $v) {
762 $sel .= " value=\"1\"";
764 $sel .= " value=\"0\"";
767 print "<input class=\"noborder\"
768 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
773 function initialize_user_prefs($link, $uid) {
775 $uid = db_escape_string($uid);
777 db_query($link, "BEGIN");
779 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
781 $u_result = db_query($link, "SELECT pref_name
782 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
784 $active_prefs = array();
786 while ($line = db_fetch_assoc($u_result)) {
787 array_push($active_prefs, $line["pref_name"]);
790 while ($line = db_fetch_assoc($result)) {
791 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
792 // print "adding " . $line["pref_name"] . "<br>";
794 db_query($link, "INSERT INTO ttrss_user_prefs
795 (owner_uid,pref_name,value) VALUES
796 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
801 db_query($link, "COMMIT");
805 function authenticate_user($link, $login, $password) {
807 $pwd_hash = 'SHA1:' . sha1($password);
809 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
810 login = '$login' AND pwd_hash = '$pwd_hash'");
812 if (db_num_rows($result) == 1) {
813 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
814 $_SESSION["name"] = db_fetch_result($result, 0, "login");
815 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
817 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
820 $user_theme = get_user_theme_path($link);
822 $_SESSION["theme"] = $user_theme;
823 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
825 initialize_user_prefs($link, $_SESSION["uid"]);
834 function make_password($length = 8) {
837 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
841 while ($i < $length) {
842 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
844 if (!strstr($password, $char)) {
852 // this is called after user is created to initialize default feeds, labels
855 // user preferences are checked on every login, not here
857 function initialize_user($link, $uid) {
859 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
860 values ('$uid','unread = true', 'Unread articles')");
862 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
863 values ('$uid','last_read is null and unread = false', 'Updated articles')");
865 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
866 values ('$uid', 'Tiny Tiny RSS: New Releases',
867 'http://tt-rss.spb.ru/releases.rss')");
871 function logout_user() {
873 if (isset($_COOKIE[session_name()])) {
874 setcookie(session_name(), '', time()-42000, '/');
878 function get_script_urlpath() {
879 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
882 function get_login_redirect() {
883 $server = $_SERVER["SERVER_NAME"];
885 if (ENABLE_LOGIN_SSL
) {
891 $url_path = get_script_urlpath();
893 $redirect_uri = "$protocol://$server$url_path/login.php";
895 return $redirect_uri;
898 function validate_session($link) {
899 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
900 if ($_SESSION["ip_address"]) {
901 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
909 function basic_nosid_redirect_check() {
910 if (!SINGLE_USER_MODE
) {
911 if (!$_COOKIE["ttrss_sid"]) {
912 $redirect_uri = get_login_redirect();
913 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
914 header("Location: $redirect_uri?rt=$return_to");
920 function login_sequence($link) {
921 if (!SINGLE_USER_MODE
) {
923 if (!validate_session($link)) {
925 $redirect_uri = get_login_redirect();
926 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
927 header("Location: $redirect_uri?rt=$return_to");
931 if (!USE_HTTP_AUTH
) {
932 if (!$_SESSION["uid"]) {
933 $redirect_uri = get_login_redirect();
934 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
935 header("Location: $redirect_uri?rt=$return_to");
939 if (!$_SESSION["uid"]) {
940 if (!$_SERVER["PHP_AUTH_USER"]) {
942 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
943 header('HTTP/1.0 401 Unauthorized');
947 $auth_result = authenticate_user($link,
948 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
951 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
952 header('HTTP/1.0 401 Unauthorized');
959 $_SESSION["uid"] = 1;
960 $_SESSION["name"] = "admin";
961 initialize_user_prefs($link, 1);
965 function truncate_string($str, $max_len) {
966 if (mb_strlen($str, "utf-8") > $max_len - 3) {
967 return mb_substr($str, 0, $max_len, "utf-8") . "...";
973 function get_user_theme_path($link) {
974 $result = db_query($link, "SELECT theme_path
976 ttrss_themes,ttrss_users
977 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
978 if (db_num_rows($result) != 0) {
979 return db_fetch_result($result, 0, "theme_path");
985 function smart_date_time($timestamp) {
986 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
987 return date("G:i", $timestamp);
988 } else if (date("Y", $timestamp) == date("Y")) {
989 return date("M d, G:i", $timestamp);
991 return date("Y/m/d G:i", $timestamp);
995 function smart_date($timestamp) {
996 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
998 } else if (date("Y", $timestamp) == date("Y")) {
999 return date("D m", $timestamp);
1001 return date("Y/m/d", $timestamp);
1005 function sql_bool_to_string($s) {
1006 if ($s == "t" ||
$s == "1") {
1013 function sql_bool_to_bool($s) {
1014 if ($s == "t" ||
$s == "1") {
1022 function toggleEvenOdd($a) {
1029 function sanity_check($link) {
1032 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1033 $schema_version = db_fetch_result($result, 0, "schema_version");
1035 if ($schema_version != SCHEMA_VERSION
) {
1039 if ($error_code != 0) {
1047 function file_is_locked($filename) {
1049 $fp = fopen($filename, "r");
1050 error_reporting(DEFAULT_ERROR_LEVEL
);
1052 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1053 flock($fp, LOCK_UN
);
1063 function make_lockfile($filename) {
1064 $fp = fopen($filename, "w");
1066 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1073 function sql_random_function() {
1074 if (DB_TYPE
== "mysql") {
1081 function catchup_feed($link, $feed, $cat_view) {
1082 if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1087 $cat_qpart = "cat_id = '$feed'";
1089 $cat_qpart = "cat_id IS NULL";
1092 $tmp_result = db_query($link, "SELECT id
1093 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1096 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1098 $tmp_feed = $tmp_line["id"];
1100 db_query($link, "UPDATE ttrss_user_entries
1101 SET unread = false,last_read = NOW()
1102 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1105 } else if ($feed > 0) {
1107 $tmp_result = db_query($link, "SELECT id
1108 FROM ttrss_feeds WHERE parent_feed = '$feed'
1109 ORDER BY cat_id,title");
1111 $parent_ids = array();
1113 if (db_num_rows($tmp_result) > 0) {
1114 while ($p = db_fetch_assoc($tmp_result)) {
1115 array_push($parent_ids, "feed_id = " . $p["id"]);
1118 $children_qpart = implode(" OR ", $parent_ids);
1120 db_query($link, "UPDATE ttrss_user_entries
1121 SET unread = false,last_read = NOW()
1122 WHERE (feed_id = '$feed' OR $children_qpart)
1123 AND owner_uid = " . $_SESSION["uid"]);
1126 db_query($link, "UPDATE ttrss_user_entries
1127 SET unread = false,last_read = NOW()
1128 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1131 } else if ($feed < 0 && $feed > -10) { // special, like starred
1134 db_query($link, "UPDATE ttrss_user_entries
1135 SET unread = false,last_read = NOW()
1136 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1139 } else if ($feed < -10) { // label
1141 // TODO make this more efficient
1143 $label_id = -$feed - 11;
1145 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1146 WHERE id = '$label_id'");
1149 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1151 db_query($link, "BEGIN");
1153 $tmp2_result = db_query($link,
1157 ttrss_user_entries,ttrss_entries
1161 owner_uid = " . $_SESSION["uid"]);
1163 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1164 db_query($link, "UPDATE
1167 unread = false, last_read = NOW()
1169 int_id = " . $tmp_line["int_id"]);
1172 db_query($link, "COMMIT");
1174 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1175 SET unread = false,last_read = NOW()
1178 AND owner_uid = ".$_SESSION["uid"]); */
1182 db_query($link, "BEGIN");
1184 $tag_name = db_escape_string($feed);
1186 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1187 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1189 while ($line = db_fetch_assoc($result)) {
1190 db_query($link, "UPDATE ttrss_user_entries SET
1191 unread = false, last_read = NOW()
1192 WHERE int_id = " . $line["post_int_id"]);
1194 db_query($link, "COMMIT");
1198 function update_generic_feed($link, $feed, $cat_view) {
1202 $cat_qpart = "cat_id = '$feed'";
1204 $cat_qpart = "cat_id IS NULL";
1207 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1208 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1210 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1211 $feed_url = $tmp_line["feed_url"];
1212 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1216 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1217 WHERE id = '$feed'");
1218 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1219 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1223 function getAllCounters($link) {
1224 getLabelCounters($link);
1225 getFeedCounters($link);
1226 getTagCounters($link);
1227 getGlobalCounters($link);
1228 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1229 getCategoryCounters($link);
1233 function getCategoryCounters($link) {
1234 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1235 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1236 AND unread = true)) AS unread FROM ttrss_feeds
1238 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1240 while ($line = db_fetch_assoc($result)) {
1241 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1242 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1243 $line["unread"]."\"/>";
1247 function getFeedUnread($link, $feed) {
1248 $n_feed = sprintf("%d", $feed);
1250 if ($n_feed == -1) {
1251 $match_part = "marked = true";
1252 } else if ($feed > 0) {
1253 $match_part = "feed_id = '$n_feed'";
1254 } else if ($feed < -10) {
1255 $label_id = -$feed - 11;
1257 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1258 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1260 $match_part = db_fetch_result($result, 0, "sql_exp");
1265 $result = db_query($link, "SELECT count(int_id) AS unread
1266 FROM ttrss_user_entries
1267 WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
1271 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1272 FROM ttrss_tags,ttrss_user_entries
1273 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1274 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1277 $unread = db_fetch_result($result, 0, "unread");
1281 /* FIXME this needs reworking */
1283 function getGlobalUnread($link) {
1284 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1285 WHERE unread = true AND
1286 ttrss_user_entries.ref_id = ttrss_entries.id AND
1287 owner_uid = " . $_SESSION["uid"]);
1288 $c_id = db_fetch_result($result, 0, "c_id");
1292 function getGlobalCounters($link, $global_unread = -1) {
1293 if ($global_unread == -1) {
1294 $global_unread = getGlobalUnread($link);
1296 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1299 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1302 if (!$_SESSION["tctr_last_value"]) {
1303 $_SESSION["tctr_last_value"] = array();
1307 $old_counters = $_SESSION["tctr_last_value"];
1309 $tctrs_modified = false;
1311 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1312 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1313 ttrss_user_entries.ref_id = ttrss_entries.id AND
1314 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1315 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1317 select tag_name,0 as count FROM ttrss_tags
1318 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1320 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1321 FROM ttrss_user_entries WHERE int_id = post_int_id
1322 AND unread = true)) AS count FROM ttrss_tags
1323 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1327 while ($line = db_fetch_assoc($result)) {
1328 $tags[$line["tag_name"]] +
= $line["count"];
1331 foreach (array_keys($tags) as $tag) {
1332 $unread = $tags[$tag];
1334 $tag = htmlspecialchars($tag);
1336 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1337 $old_counters[$tag] = $unread;
1338 $tctrs_modified = true;
1339 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1344 if ($smart_mode && $tctrs_modified) {
1345 $_SESSION["tctr_last_value"] = $old_counters;
1350 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1353 if (!$_SESSION["lctr_last_value"]) {
1354 $_SESSION["lctr_last_value"] = array();
1358 $old_counters = $_SESSION["lctr_last_value"];
1359 $lctrs_modified = false;
1361 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
1362 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1363 unread = true AND owner_uid = ".$_SESSION["uid"]);
1365 $count = db_fetch_result($result, 0, "count");
1367 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1369 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1370 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1372 while ($line = db_fetch_assoc($result)) {
1374 $id = -$line["id"] - 11;
1376 error_reporting (0);
1378 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
1379 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1380 ttrss_user_entries.ref_id = ttrss_entries.id AND
1381 owner_uid = ".$_SESSION["uid"]);
1383 $count = db_fetch_result($tmp_result, 0, "count");
1385 if (!$smart_mode ||
$old_counters[$id] != $count) {
1386 $old_counters[$id] = $count;
1387 $lctrs_modified = true;
1388 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1391 error_reporting (DEFAULT_ERROR_LEVEL
);
1394 if ($smart_mode && $lctrs_modified) {
1395 $_SESSION["lctr_last_value"] = $old_counters;
1399 /* function getFeedCounter($link, $id) {
1401 $result = db_query($link, "SELECT
1402 count(id) as count,last_error
1403 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1404 WHERE feed_id = '$id' AND unread = true
1405 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1406 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1408 $count = db_fetch_result($result, 0, "count");
1409 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1411 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1414 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1417 if (!$_SESSION["fctr_last_value"]) {
1418 $_SESSION["fctr_last_value"] = array();
1422 $old_counters = $_SESSION["fctr_last_value"];
1424 $result = db_query($link, "SELECT id,last_error,parent_feed,
1425 SUBSTRING(last_updated,1,19) AS last_updated,
1427 FROM ttrss_entries,ttrss_user_entries
1428 WHERE feed_id = ttrss_feeds.id AND
1429 ttrss_user_entries.ref_id = ttrss_entries.id
1430 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1431 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1432 AND parent_feed IS NULL");
1434 $fctrs_modified = false;
1436 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1438 while ($line = db_fetch_assoc($result)) {
1441 $count = $line["count"];
1442 $last_error = htmlspecialchars($line["last_error"]);
1444 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1445 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1447 $last_updated = date($short_date, strtotime($line["last_updated"]));
1450 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1452 $tmp_result = db_query($link,
1453 "SELECT id,COUNT(unread) AS unread
1454 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1455 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1456 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1458 if (db_num_rows($tmp_result) > 0) {
1459 while ($l = db_fetch_assoc($tmp_result)) {
1460 $count +
= $l["unread"];
1464 if (!$smart_mode ||
$old_counters[$id] != $count) {
1465 $old_counters[$id] = $count;
1466 $fctrs_modified = true;
1469 $error_part = "error=\"$last_error\"";
1475 $has_img_part = "hi=\"$has_img\"";
1480 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1484 if ($smart_mode && $fctrs_modified) {
1485 $_SESSION["fctr_last_value"] = $old_counters;
1489 function get_script_dt_add() {
1490 if (strpos(VERSION
, "99") === false) {
1497 function get_pgsql_version($link) {
1498 $result = db_query($link, "SELECT version() AS version");
1499 $version = split(" ", db_fetch_result($result, 0, "version"));
1503 function print_error_xml($code, $add_msg = "") {
1506 $error_msg = $ERRORS[$code];
1509 $error_msg = "$error_msg; $add_msg";
1512 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";