]>
git.wh0rd.org - tt-rss.git/blob - functions.php
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';
13 require_once 'magpierss/rss_utils.inc';
15 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
17 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
21 if (DB_TYPE
== "pgsql") {
22 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
23 marked = false AND feed_id = '$feed_id' AND
24 (SELECT date_entered FROM ttrss_entries WHERE
25 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
27 if (PG_VERSION
== "7.4" || PG_VERSION
== "8.0") {
29 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
30 ttrss_entries.id = ref_id AND
32 feed_id = '$feed_id' AND
33 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
37 $result = db_query($link, "DELETE FROM ttrss_user_entries
39 WHERE ttrss_entries.id = ref_id AND
41 feed_id = '$feed_id' AND
42 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
45 $rows = pg_affected_rows($result);
49 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
50 marked = false AND feed_id = '$feed_id' AND
51 (SELECT date_entered FROM ttrss_entries WHERE
52 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
54 $result = db_query($link, "DELETE FROM ttrss_user_entries
55 USING ttrss_user_entries, ttrss_entries
56 WHERE ttrss_entries.id = ref_id AND
58 feed_id = '$feed_id' AND
59 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
61 $rows = mysql_affected_rows($link);
66 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
70 function global_purge_old_posts($link, $do_output = false, $limit = false) {
72 $random_qpart = sql_random_function();
75 $limit_qpart = "LIMIT $limit";
80 $result = db_query($link,
81 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
82 ORDER BY $random_qpart $limit_qpart");
84 while ($line = db_fetch_assoc($result)) {
86 $feed_id = $line["id"];
87 $purge_interval = $line["purge_interval"];
88 $owner_uid = $line["owner_uid"];
90 if ($purge_interval == 0) {
92 $tmp_result = db_query($link,
93 "SELECT value FROM ttrss_user_prefs WHERE
94 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
96 if (db_num_rows($tmp_result) != 0) {
97 $purge_interval = db_fetch_result($tmp_result, 0, "value");
102 // print "Feed $feed_id: purge interval = $purge_interval\n";
105 if ($purge_interval > 0) {
106 purge_feed($link, $feed_id, $purge_interval, $do_output);
110 // purge orphaned posts in main content table
111 db_query($link, "DELETE FROM ttrss_entries WHERE
112 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
116 function purge_old_posts($link) {
118 $user_id = $_SESSION["uid"];
120 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
121 WHERE owner_uid = '$user_id'");
123 while ($line = db_fetch_assoc($result)) {
125 $feed_id = $line["id"];
126 $purge_interval = $line["purge_interval"];
128 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
130 if ($purge_interval > 0) {
131 purge_feed($link, $feed_id, $purge_interval);
135 // purge orphaned posts in main content table
136 db_query($link, "DELETE FROM ttrss_entries WHERE
137 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
140 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
142 if (WEB_DEMO_MODE
) return;
145 $user_id = $_SESSION["uid"];
146 purge_old_posts($link);
149 // db_query($link, "BEGIN");
151 if (MAX_UPDATE_TIME
> 0) {
152 if (DB_TYPE
== "mysql") {
155 $q_order = "RANDOM()";
158 $q_order = "last_updated DESC";
161 $result = db_query($link, "SELECT feed_url,id,
162 SUBSTRING(last_updated,1,19) AS last_updated,
163 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
168 while ($line = db_fetch_assoc($result)) {
169 $upd_intl = $line["update_interval"];
171 if (!$upd_intl ||
$upd_intl == 0) {
172 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
176 // Updates for this feed are disabled
180 if ($fetch ||
(!$line["last_updated"] ||
181 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
183 // print "<!-- feed: ".$line["feed_url"]." -->";
185 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
187 $upd_elapsed = time() - $upd_start;
189 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
195 // db_query($link, "COMMIT");
199 function check_feed_favicon($feed_url, $feed, $link) {
200 $feed_url = str_replace("http://", "", $feed_url);
201 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
203 $icon_url = "http://$feed_url/favicon.ico";
204 $icon_file = ICONS_DIR
. "/$feed.ico";
206 if (!file_exists($icon_file)) {
209 $r = fopen($icon_url, "r");
210 error_reporting (DEFAULT_ERROR_LEVEL
);
213 $tmpfname = tempnam(TMP_DIRECTORY
, "ttrssicon");
215 $t = fopen($tmpfname, "w");
218 $buf = fread($r, 16384);
226 if (!rename($tmpfname, $icon_file)) {
230 chmod($icon_file, 0644);
232 error_reporting (DEFAULT_ERROR_LEVEL
);
238 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
240 if (WEB_DEMO_MODE
) return;
242 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
246 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
247 FROM ttrss_feeds WHERE id = '$feed'");
249 $auth_login = db_fetch_result($result, 0, "auth_login");
250 $auth_pass = db_fetch_result($result, 0, "auth_pass");
252 $update_interval = db_fetch_result($result, 0, "update_interval");
254 if ($update_interval < 0) { return; }
256 $feed = db_escape_string($feed);
258 $fetch_url = $feed_url;
260 if ($auth_login && $auth_pass) {
261 $url_parts = array();
262 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
264 if ($url_parts[1] && $url_parts[2]) {
265 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
270 $rss = fetch_rss($fetch_url);
272 error_reporting (DEFAULT_ERROR_LEVEL
);
274 $feed = db_escape_string($feed);
278 // db_query($link, "BEGIN");
280 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
281 FROM ttrss_feeds WHERE id = '$feed'");
283 $registered_title = db_fetch_result($result, 0, "title");
284 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
285 $orig_site_url = db_fetch_result($result, 0, "site_url");
287 $owner_uid = db_fetch_result($result, 0, "owner_uid");
289 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
290 check_feed_favicon($feed_url, $feed, $link);
293 if (!$registered_title ||
$registered_title == "[Unknown]") {
294 $feed_title = db_escape_string($rss->channel
["title"]);
295 db_query($link, "UPDATE ttrss_feeds SET
296 title = '$feed_title' WHERE id = '$feed'");
299 $site_url = $rss->channel
["link"];
300 // weird, weird Magpie
301 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
303 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
304 db_query($link, "UPDATE ttrss_feeds SET
305 site_url = '$site_url' WHERE id = '$feed'");
308 // print "I: " . $rss->channel["image"]["url"];
310 $icon_url = $rss->image
["url"];
312 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
313 $icon_url = db_escape_string($icon_url);
314 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
320 $result = db_query($link, "SELECT reg_exp,
321 ttrss_filter_types.name AS name,
322 ttrss_filter_actions.name AS action
323 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
324 owner_uid = $owner_uid AND
325 ttrss_filter_types.id = filter_type AND
326 ttrss_filter_actions.id = action_id AND
327 (feed_id IS NULL OR feed_id = '$feed')");
329 while ($line = db_fetch_assoc($result)) {
330 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
332 $filter["reg_exp"] = $line["reg_exp"];
333 $filter["action"] = $line["action"];
335 array_push($filters[$line["name"]], $filter);
338 $iterator = $rss->items
;
340 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
341 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
343 if (!is_array($iterator)) {
344 db_query($link, "UPDATE ttrss_feeds
345 SET last_error = 'Parse error: can\'t find any articles.'
346 WHERE id = '$feed'");
350 foreach ($iterator as $item) {
352 $entry_guid = $item["id"];
354 if (!$entry_guid) $entry_guid = $item["guid"];
355 if (!$entry_guid) $entry_guid = $item["link"];
357 if (!$entry_guid) continue;
359 $entry_timestamp = "";
361 $rss_2_date = $item['pubdate'];
362 $rss_1_date = $item['dc']['date'];
363 $atom_date = $item['issued'];
364 if (!$atom_date) $atom_date = $item['updated'];
366 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
367 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
368 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
370 if ($entry_timestamp == "") {
371 $entry_timestamp = time();
372 $no_orig_date = 'true';
374 $no_orig_date = 'false';
377 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
379 $entry_title = $item["title"];
381 // strange Magpie workaround
382 $entry_link = $item["link_"];
383 if (!$entry_link) $entry_link = $item["link"];
385 if (!$entry_title) continue;
386 if (!$entry_link) continue;
388 $entry_content = $item["content:escaped"];
390 if (!$entry_content) $entry_content = $item["content:encoded"];
391 if (!$entry_content) $entry_content = $item["content"];
392 if (!$entry_content) $entry_content = $item["summary"];
393 if (!$entry_content) $entry_content = $item["description"];
395 // if (!$entry_content) continue;
398 if (is_array($entry_content)) {
399 $entry_content = $entry_content["encoded"];
400 if (!$entry_content) $entry_content = $entry_content["escaped"];
404 // print_r(htmlspecialchars($entry_content));
407 $entry_content_unescaped = $entry_content;
408 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
410 $entry_comments = $item["comments"];
412 $entry_author = db_escape_string($item['dc']['creator']);
414 $entry_guid = db_escape_string($entry_guid);
416 $result = db_query($link, "SELECT id FROM ttrss_entries
417 WHERE guid = '$entry_guid'");
419 $entry_content = db_escape_string($entry_content);
420 $entry_title = db_escape_string($entry_title);
421 $entry_link = db_escape_string($entry_link);
422 $entry_comments = db_escape_string($entry_comments);
424 $num_comments = db_escape_string($item["slash"]["comments"]);
426 if (!$num_comments) $num_comments = 0;
428 db_query($link, "BEGIN");
430 if (db_num_rows($result) == 0) {
432 // base post entry does not exist, create it
434 $result = db_query($link,
435 "INSERT INTO ttrss_entries
451 '$entry_timestamp_fmt',
460 // we keep encountering the entry in feeds, so we need to
461 // update date_entered column so that we don't get horrible
462 // dupes when the entry gets purged and reinserted again e.g.
463 // in the case of SLOW SLOW OMG SLOW updating feeds
465 $base_entry_id = db_fetch_result($result, 0, "id");
467 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
468 WHERE id = '$base_entry_id'");
471 // now it should exist, if not - bad luck then
473 $result = db_query($link, "SELECT
474 id,content_hash,no_orig_date,title,
475 substring(date_entered,1,19) as date_entered,
476 substring(updated,1,19) as updated,
480 WHERE guid = '$entry_guid'");
482 if (db_num_rows($result) == 1) {
484 // this will be used below in update handler
485 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
486 $orig_title = db_fetch_result($result, 0, "title");
487 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
488 $orig_date_entered = strtotime(db_fetch_result($result,
491 $ref_id = db_fetch_result($result, 0, "id");
493 // check for user post link to main table
495 // do we allow duplicate posts with same GUID in different feeds?
496 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
497 $dupcheck_qpart = "AND feed_id = '$feed'";
499 $dupcheck_qpart = "";
502 // error_reporting(0);
504 $filter_name = get_filter_name($entry_title, $entry_content,
505 $entry_link, $filters);
507 if ($filter_name == "filter") {
511 // error_reporting (DEFAULT_ERROR_LEVEL);
513 $result = db_query($link,
514 "SELECT ref_id FROM ttrss_user_entries WHERE
515 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
518 // okay it doesn't exist - create user entry
519 if (db_num_rows($result) == 0) {
521 if ($filter_name != 'catchup') {
523 $last_read_qpart = 'NULL';
526 $last_read_qpart = 'NOW()';
529 $result = db_query($link,
530 "INSERT INTO ttrss_user_entries
531 (ref_id, owner_uid, feed_id, unread, last_read)
532 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
536 $post_needs_update = false;
538 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
539 ($content_hash != $orig_content_hash)) {
540 $post_needs_update = true;
543 if ($orig_title != $entry_title) {
544 $post_needs_update = true;
547 if ($orig_num_comments != $num_comments) {
548 $post_needs_update = true;
551 // this doesn't seem to be very reliable
553 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
554 // $post_needs_update = true;
557 // if post needs update, update it and mark all user entries
558 // linking to this post as updated
559 if ($post_needs_update) {
561 // print "<!-- post $orig_title needs update : $post_needs_update -->";
563 db_query($link, "UPDATE ttrss_entries
564 SET title = '$entry_title', content = '$entry_content',
565 num_comments = '$num_comments'
566 WHERE id = '$ref_id'");
568 db_query($link, "UPDATE ttrss_user_entries
569 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
574 db_query($link, "COMMIT");
577 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
581 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
582 $entry_content_unescaped, $entry_tags);
584 // print "<br>$entry_title : $entry_content_unescaped<br>";
585 // print_r($entry_tags);
588 $entry_tags = $entry_tags[1];
590 if (count($entry_tags) > 0) {
592 db_query($link, "BEGIN");
594 $result = db_query($link, "SELECT id,int_id
595 FROM ttrss_entries,ttrss_user_entries
596 WHERE guid = '$entry_guid'
597 AND feed_id = '$feed' AND ref_id = id
598 AND owner_uid = '$owner_uid'");
600 if (db_num_rows($result) == 1) {
602 $entry_id = db_fetch_result($result, 0, "id");
603 $entry_int_id = db_fetch_result($result, 0, "int_id");
605 foreach ($entry_tags as $tag) {
606 $tag = db_escape_string(strtolower($tag));
608 $tag = str_replace("+", " ", $tag);
609 $tag = str_replace("technorati tag: ", "", $tag);
611 $result = db_query($link, "SELECT id FROM ttrss_tags
612 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
613 owner_uid = '$owner_uid' LIMIT 1");
615 // print db_fetch_result($result, 0, "id");
617 if ($result && db_num_rows($result) == 0) {
619 // print "tagging $entry_id as $tag<br>";
621 db_query($link, "INSERT INTO ttrss_tags
622 (owner_uid,tag_name,post_int_id)
623 VALUES ('$owner_uid','$tag', '$entry_int_id')");
627 db_query($link, "COMMIT");
631 db_query($link, "UPDATE ttrss_feeds
632 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
634 // db_query($link, "COMMIT");
637 $error_msg = db_escape_string(magpie_error());
639 "UPDATE ttrss_feeds SET last_error = '$error_msg',
640 last_updated = NOW() WHERE id = '$feed'");
645 function print_select($id, $default, $values, $attributes = "") {
646 print "<select id=\"$id\" $attributes>";
647 foreach ($values as $v) {
653 print "<option$sel>$v</option>";
658 function get_filter_name($title, $content, $link, $filters) {
660 if ($filters["title"]) {
661 foreach ($filters["title"] as $filter) {
662 $reg_exp = $filter["reg_exp"];
663 if (preg_match("/$reg_exp/i", $title)) {
664 return $filter["action"];
669 if ($filters["content"]) {
670 foreach ($filters["content"] as $filter) {
671 $reg_exp = $filter["reg_exp"];
672 if (preg_match("/$reg_exp/i", $content)) {
673 return $filter["action"];
678 if ($filters["both"]) {
679 foreach ($filters["both"] as $filter) {
680 $reg_exp = $filter["reg_exp"];
681 if (preg_match("/$reg_exp/i", $title) ||
682 preg_match("/$reg_exp/i", $content)) {
683 return $filter["action"];
688 if ($filters["link"]) {
689 $reg_exp = $filter["reg_exp"];
690 foreach ($filters["link"] as $filter) {
691 $reg_exp = $filter["reg_exp"];
692 if (preg_match("/$reg_exp/i", $link)) {
693 return $filter["action"];
701 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
702 $rtl_content = false, $last_updated = false, $last_error = false) {
704 if (file_exists($icon_file) && filesize($icon_file) > 0) {
705 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
707 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
711 $rtl_tag = "dir=\"rtl\"";
713 $rtl_tag = "dir=\"ltr\"";
717 $link_title = "Error: $last_error ($last_updated)";
718 } else if ($last_updated) {
719 $link_title = "Updated: $last_updated";
722 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
724 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
725 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
729 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
734 $fctr_class = "class=\"invisible\"";
737 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
738 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
744 function getmicrotime() {
745 list($usec, $sec) = explode(" ",microtime());
746 return ((float)$usec +
(float)$sec);
749 function print_radio($id, $default, $values, $attributes = "") {
750 foreach ($values as $v) {
758 $sel .= " value=\"1\"";
760 $sel .= " value=\"0\"";
763 print "<input class=\"noborder\"
764 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
769 function initialize_user_prefs($link, $uid) {
771 $uid = db_escape_string($uid);
773 db_query($link, "BEGIN");
775 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
777 $u_result = db_query($link, "SELECT pref_name
778 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
780 $active_prefs = array();
782 while ($line = db_fetch_assoc($u_result)) {
783 array_push($active_prefs, $line["pref_name"]);
786 while ($line = db_fetch_assoc($result)) {
787 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
788 // print "adding " . $line["pref_name"] . "<br>";
790 db_query($link, "INSERT INTO ttrss_user_prefs
791 (owner_uid,pref_name,value) VALUES
792 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
797 db_query($link, "COMMIT");
801 function authenticate_user($link, $login, $password) {
803 $pwd_hash = 'SHA1:' . sha1($password);
805 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
806 login = '$login' AND pwd_hash = '$pwd_hash'");
808 if (db_num_rows($result) == 1) {
809 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
810 $_SESSION["name"] = db_fetch_result($result, 0, "login");
811 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
813 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
816 $user_theme = get_user_theme_path($link);
818 $_SESSION["theme"] = $user_theme;
819 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
821 initialize_user_prefs($link, $_SESSION["uid"]);
830 function make_password($length = 8) {
833 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
837 while ($i < $length) {
838 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
840 if (!strstr($password, $char)) {
848 // this is called after user is created to initialize default feeds, labels
851 // user preferences are checked on every login, not here
853 function initialize_user($link, $uid) {
855 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
856 values ('$uid','unread = true', 'Unread articles')");
858 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
859 values ('$uid','last_read is null and unread = false', 'Updated articles')");
861 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
862 values ('$uid', 'Tiny Tiny RSS: New Releases',
863 'http://tt-rss.spb.ru/releases.rss')");
867 function logout_user() {
869 if (isset($_COOKIE[session_name()])) {
870 setcookie(session_name(), '', time()-42000, '/');
874 function get_script_urlpath() {
875 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
878 function get_login_redirect() {
879 $server = $_SERVER["SERVER_NAME"];
881 if (ENABLE_LOGIN_SSL
) {
887 $url_path = get_script_urlpath();
889 $redirect_uri = "$protocol://$server$url_path/login.php";
891 return $redirect_uri;
894 function validate_session($link) {
895 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
896 if ($_SESSION["ip_address"]) {
897 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
905 function basic_nosid_redirect_check() {
906 if (!SINGLE_USER_MODE
) {
907 if (!$_COOKIE["ttrss_sid"]) {
908 $redirect_uri = get_login_redirect();
909 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
910 header("Location: $redirect_uri?rt=$return_to");
916 function login_sequence($link) {
917 if (!SINGLE_USER_MODE
) {
919 if (!validate_session($link)) {
921 $redirect_uri = get_login_redirect();
922 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
923 header("Location: $redirect_uri?rt=$return_to");
927 if (!USE_HTTP_AUTH
) {
928 if (!$_SESSION["uid"]) {
929 $redirect_uri = get_login_redirect();
930 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
931 header("Location: $redirect_uri?rt=$return_to");
935 if (!$_SESSION["uid"]) {
936 if (!$_SERVER["PHP_AUTH_USER"]) {
938 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
939 header('HTTP/1.0 401 Unauthorized');
943 $auth_result = authenticate_user($link,
944 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
947 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
948 header('HTTP/1.0 401 Unauthorized');
955 $_SESSION["uid"] = 1;
956 $_SESSION["name"] = "admin";
957 initialize_user_prefs($link, 1);
961 function truncate_string($str, $max_len) {
962 if (mb_strlen($str, "utf-8") > $max_len - 3) {
963 return mb_substr($str, 0, $max_len, "utf-8") . "...";
969 function get_user_theme_path($link) {
970 $result = db_query($link, "SELECT theme_path
972 ttrss_themes,ttrss_users
973 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
974 if (db_num_rows($result) != 0) {
975 return db_fetch_result($result, 0, "theme_path");
981 function smart_date_time($timestamp) {
982 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
983 return date("G:i", $timestamp);
984 } else if (date("Y", $timestamp) == date("Y")) {
985 return date("M d, G:i", $timestamp);
987 return date("Y/m/d G:i", $timestamp);
991 function smart_date($timestamp) {
992 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
994 } else if (date("Y", $timestamp) == date("Y")) {
995 return date("D m", $timestamp);
997 return date("Y/m/d", $timestamp);
1001 function sql_bool_to_string($s) {
1002 if ($s == "t" ||
$s == "1") {
1009 function sql_bool_to_bool($s) {
1010 if ($s == "t" ||
$s == "1") {
1018 function toggleEvenOdd($a) {
1025 function sanity_check($link) {
1028 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1029 $schema_version = db_fetch_result($result, 0, "schema_version");
1031 if ($schema_version != SCHEMA_VERSION
) {
1035 if ($error_code != 0) {
1036 print "<error error-code='$error_code'/>";
1043 function file_is_locked($filename) {
1045 $fp = fopen($filename, "r");
1046 error_reporting(DEFAULT_ERROR_LEVEL
);
1048 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1049 flock($fp, LOCK_UN
);
1059 function make_lockfile($filename) {
1060 $fp = fopen($filename, "w");
1062 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1069 function sql_random_function() {
1070 if (DB_TYPE
== "mysql") {
1077 function catchup_feed($link, $feed, $cat_view) {
1078 if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1083 $cat_qpart = "cat_id = '$feed'";
1085 $cat_qpart = "cat_id IS NULL";
1088 $tmp_result = db_query($link, "SELECT id
1089 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1092 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1094 $tmp_feed = $tmp_line["id"];
1096 db_query($link, "UPDATE ttrss_user_entries
1097 SET unread = false,last_read = NOW()
1098 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1101 } else if ($feed > 0) {
1103 $tmp_result = db_query($link, "SELECT id
1104 FROM ttrss_feeds WHERE parent_feed = '$feed'
1105 ORDER BY cat_id,title");
1107 $parent_ids = array();
1109 if (db_num_rows($tmp_result) > 0) {
1110 while ($p = db_fetch_assoc($tmp_result)) {
1111 array_push($parent_ids, "feed_id = " . $p["id"]);
1114 $children_qpart = implode(" OR ", $parent_ids);
1116 db_query($link, "UPDATE ttrss_user_entries
1117 SET unread = false,last_read = NOW()
1118 WHERE (feed_id = '$feed' OR $children_qpart)
1119 AND owner_uid = " . $_SESSION["uid"]);
1122 db_query($link, "UPDATE ttrss_user_entries
1123 SET unread = false,last_read = NOW()
1124 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1127 } else if ($feed < 0 && $feed > -10) { // special, like starred
1130 db_query($link, "UPDATE ttrss_user_entries
1131 SET unread = false,last_read = NOW()
1132 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1135 } else if ($feed < -10) { // label
1137 // TODO make this more efficient
1139 $label_id = -$feed - 11;
1141 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1142 WHERE id = '$label_id'");
1145 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1147 db_query($link, "BEGIN");
1149 $tmp2_result = db_query($link,
1153 ttrss_user_entries,ttrss_entries
1157 owner_uid = " . $_SESSION["uid"]);
1159 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1160 db_query($link, "UPDATE
1163 unread = false, last_read = NOW()
1165 int_id = " . $tmp_line["int_id"]);
1168 db_query($link, "COMMIT");
1170 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1171 SET unread = false,last_read = NOW()
1174 AND owner_uid = ".$_SESSION["uid"]); */
1178 db_query($link, "BEGIN");
1180 $tag_name = db_escape_string($feed);
1182 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1183 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1185 while ($line = db_fetch_assoc($result)) {
1186 db_query($link, "UPDATE ttrss_user_entries SET
1187 unread = false, last_read = NOW()
1188 WHERE int_id = " . $line["post_int_id"]);
1190 db_query($link, "COMMIT");
1194 function update_generic_feed($link, $feed, $cat_view) {
1198 $cat_qpart = "cat_id = '$feed'";
1200 $cat_qpart = "cat_id IS NULL";
1203 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1204 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1206 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1207 $feed_url = $tmp_line["feed_url"];
1208 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1212 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1213 WHERE id = '$feed'");
1214 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1215 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1219 function getAllCounters($link) {
1220 getLabelCounters($link);
1221 getFeedCounters($link);
1222 getTagCounters($link);
1223 getGlobalCounters($link);
1224 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1225 getCategoryCounters($link);
1229 function getCategoryCounters($link) {
1230 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1231 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1232 AND unread = true)) AS unread FROM ttrss_feeds
1234 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1236 while ($line = db_fetch_assoc($result)) {
1237 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1238 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1239 $line["unread"]."\"/>";
1243 function getFeedUnread($link, $feed) {
1244 $n_feed = sprintf("%d", $feed);
1246 if ($n_feed == -1) {
1247 $match_part = "marked = true";
1248 } else if ($feed > 0) {
1249 $match_part = "feed_id = '$n_feed'";
1250 } else if ($feed < -10) {
1251 $label_id = -$feed - 11;
1253 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1254 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1256 $match_part = db_fetch_result($result, 0, "sql_exp");
1261 $result = db_query($link, "SELECT count(int_id) AS unread
1262 FROM ttrss_user_entries
1263 WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
1267 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1268 FROM ttrss_tags,ttrss_user_entries
1269 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1270 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1273 $unread = db_fetch_result($result, 0, "unread");
1277 /* FIXME this needs reworking */
1279 function getGlobalUnread($link) {
1280 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1281 WHERE unread = true AND
1282 ttrss_user_entries.ref_id = ttrss_entries.id AND
1283 owner_uid = " . $_SESSION["uid"]);
1284 $c_id = db_fetch_result($result, 0, "c_id");
1288 function getGlobalCounters($link, $global_unread = -1) {
1289 if ($global_unread == -1) {
1290 $global_unread = getGlobalUnread($link);
1292 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1295 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1298 if (!$_SESSION["tctr_last_value"]) {
1299 $_SESSION["tctr_last_value"] = array();
1303 $old_counters = $_SESSION["tctr_last_value"];
1305 $tctrs_modified = false;
1307 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1308 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1309 ttrss_user_entries.ref_id = ttrss_entries.id AND
1310 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1311 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1313 select tag_name,0 as count FROM ttrss_tags
1314 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1316 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1317 FROM ttrss_user_entries WHERE int_id = post_int_id
1318 AND unread = true)) AS count FROM ttrss_tags
1319 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1323 while ($line = db_fetch_assoc($result)) {
1324 $tags[$line["tag_name"]] +
= $line["count"];
1327 foreach (array_keys($tags) as $tag) {
1328 $unread = $tags[$tag];
1330 $tag = htmlspecialchars($tag);
1332 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1333 $old_counters[$tag] = $unread;
1334 $tctrs_modified = true;
1335 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1340 if ($smart_mode && $tctrs_modified) {
1341 $_SESSION["tctr_last_value"] = $old_counters;
1346 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1349 if (!$_SESSION["lctr_last_value"]) {
1350 $_SESSION["lctr_last_value"] = array();
1354 $old_counters = $_SESSION["lctr_last_value"];
1355 $lctrs_modified = false;
1357 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
1358 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1359 unread = true AND owner_uid = ".$_SESSION["uid"]);
1361 $count = db_fetch_result($result, 0, "count");
1363 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1365 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1366 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1368 while ($line = db_fetch_assoc($result)) {
1370 $id = -$line["id"] - 11;
1372 error_reporting (0);
1374 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
1375 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1376 ttrss_user_entries.ref_id = ttrss_entries.id AND
1377 owner_uid = ".$_SESSION["uid"]);
1379 $count = db_fetch_result($tmp_result, 0, "count");
1381 if (!$smart_mode ||
$old_counters[$id] != $count) {
1382 $old_counters[$id] = $count;
1383 $lctrs_modified = true;
1384 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1387 error_reporting (DEFAULT_ERROR_LEVEL
);
1390 if ($smart_mode && $lctrs_modified) {
1391 $_SESSION["lctr_last_value"] = $old_counters;
1395 /* function getFeedCounter($link, $id) {
1397 $result = db_query($link, "SELECT
1398 count(id) as count,last_error
1399 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1400 WHERE feed_id = '$id' AND unread = true
1401 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1402 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1404 $count = db_fetch_result($result, 0, "count");
1405 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1407 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1410 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1413 if (!$_SESSION["fctr_last_value"]) {
1414 $_SESSION["fctr_last_value"] = array();
1418 $old_counters = $_SESSION["fctr_last_value"];
1420 $result = db_query($link, "SELECT id,last_error,parent_feed,
1421 SUBSTRING(last_updated,1,19) AS last_updated,
1423 FROM ttrss_entries,ttrss_user_entries
1424 WHERE feed_id = ttrss_feeds.id AND
1425 ttrss_user_entries.ref_id = ttrss_entries.id
1426 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1427 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1428 AND parent_feed IS NULL");
1430 $fctrs_modified = false;
1432 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1434 while ($line = db_fetch_assoc($result)) {
1437 $count = $line["count"];
1438 $last_error = htmlspecialchars($line["last_error"]);
1440 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1441 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1443 $last_updated = date($short_date, strtotime($line["last_updated"]));
1446 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1448 $tmp_result = db_query($link,
1449 "SELECT id,COUNT(unread) AS unread
1450 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1451 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1452 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1454 if (db_num_rows($tmp_result) > 0) {
1455 while ($l = db_fetch_assoc($tmp_result)) {
1456 $count +
= $l["unread"];
1460 if (!$smart_mode ||
$old_counters[$id] != $count) {
1461 $old_counters[$id] = $count;
1462 $fctrs_modified = true;
1465 $error_part = "error=\"$last_error\"";
1471 $has_img_part = "hi=\"$has_img\"";
1476 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1480 if ($smart_mode && $fctrs_modified) {
1481 $_SESSION["fctr_last_value"] = $old_counters;