]>
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 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
28 ttrss_entries.id = ref_id AND
30 feed_id = '$feed_id' AND
31 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
33 $rows = pg_affected_rows($result);
36 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
37 marked = false AND feed_id = '$feed_id' AND
38 (SELECT date_entered FROM ttrss_entries WHERE
39 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
41 $result = db_query($link, "DELETE FROM ttrss_user_entries
42 USING ttrss_user_entries, ttrss_entries
43 WHERE ttrss_entries.id = ref_id AND
45 feed_id = '$feed_id' AND
46 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
48 $rows = mysql_affected_rows($link);
53 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
57 function global_purge_old_posts($link, $do_output = false, $limit = false) {
59 $random_qpart = sql_random_function();
62 $limit_qpart = "LIMIT $limit";
67 $result = db_query($link,
68 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
69 ORDER BY $random_qpart $limit_qpart");
71 while ($line = db_fetch_assoc($result)) {
73 $feed_id = $line["id"];
74 $purge_interval = $line["purge_interval"];
75 $owner_uid = $line["owner_uid"];
77 if ($purge_interval == 0) {
79 $tmp_result = db_query($link,
80 "SELECT value FROM ttrss_user_prefs WHERE
81 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
83 if (db_num_rows($tmp_result) != 0) {
84 $purge_interval = db_fetch_result($tmp_result, 0, "value");
89 // print "Feed $feed_id: purge interval = $purge_interval\n";
92 if ($purge_interval > 0) {
93 purge_feed($link, $feed_id, $purge_interval, $do_output);
97 // purge orphaned posts in main content table
98 db_query($link, "DELETE FROM ttrss_entries WHERE
99 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
103 function purge_old_posts($link) {
105 $user_id = $_SESSION["uid"];
107 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
108 WHERE owner_uid = '$user_id'");
110 while ($line = db_fetch_assoc($result)) {
112 $feed_id = $line["id"];
113 $purge_interval = $line["purge_interval"];
115 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
117 if ($purge_interval > 0) {
118 purge_feed($link, $feed_id, $purge_interval);
122 // purge orphaned posts in main content table
123 db_query($link, "DELETE FROM ttrss_entries WHERE
124 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
127 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
129 if (WEB_DEMO_MODE
) return;
132 $user_id = $_SESSION["uid"];
133 purge_old_posts($link);
136 // db_query($link, "BEGIN");
138 if (MAX_UPDATE_TIME
> 0) {
139 if (DB_TYPE
== "mysql") {
142 $q_order = "RANDOM()";
145 $q_order = "last_updated DESC";
148 $result = db_query($link, "SELECT feed_url,id,
149 SUBSTRING(last_updated,1,19) AS last_updated,
150 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
155 while ($line = db_fetch_assoc($result)) {
156 $upd_intl = $line["update_interval"];
158 if (!$upd_intl ||
$upd_intl == 0) {
159 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
163 // Updates for this feed are disabled
167 if ($fetch ||
(!$line["last_updated"] ||
168 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
170 // print "<!-- feed: ".$line["feed_url"]." -->";
172 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
174 $upd_elapsed = time() - $upd_start;
176 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
182 // db_query($link, "COMMIT");
186 function check_feed_favicon($feed_url, $feed, $link) {
187 $feed_url = str_replace("http://", "", $feed_url);
188 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
190 $icon_url = "http://$feed_url/favicon.ico";
191 $icon_file = ICONS_DIR
. "/$feed.ico";
193 if (!file_exists($icon_file)) {
196 $r = fopen($icon_url, "r");
197 error_reporting (DEFAULT_ERROR_LEVEL
);
200 $tmpfname = tempnam(TMP_DIRECTORY
, "ttrssicon");
202 $t = fopen($tmpfname, "w");
205 $buf = fread($r, 16384);
213 if (!rename($tmpfname, $icon_file)) {
217 chmod($icon_file, 0644);
219 error_reporting (DEFAULT_ERROR_LEVEL
);
225 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
227 if (WEB_DEMO_MODE
) return;
229 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
233 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
234 FROM ttrss_feeds WHERE id = '$feed'");
236 $auth_login = db_fetch_result($result, 0, "auth_login");
237 $auth_pass = db_fetch_result($result, 0, "auth_pass");
239 $update_interval = db_fetch_result($result, 0, "update_interval");
241 if ($update_interval < 0) { return; }
243 $feed = db_escape_string($feed);
245 $fetch_url = $feed_url;
247 if ($auth_login && $auth_pass) {
248 $url_parts = array();
249 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
251 if ($url_parts[1] && $url_parts[2]) {
252 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
257 $rss = fetch_rss($fetch_url);
259 error_reporting (DEFAULT_ERROR_LEVEL
);
261 $feed = db_escape_string($feed);
265 // db_query($link, "BEGIN");
267 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
268 FROM ttrss_feeds WHERE id = '$feed'");
270 $registered_title = db_fetch_result($result, 0, "title");
271 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
272 $orig_site_url = db_fetch_result($result, 0, "site_url");
274 $owner_uid = db_fetch_result($result, 0, "owner_uid");
276 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
277 check_feed_favicon($feed_url, $feed, $link);
280 if (!$registered_title ||
$registered_title == "[Unknown]") {
281 $feed_title = db_escape_string($rss->channel
["title"]);
282 db_query($link, "UPDATE ttrss_feeds SET
283 title = '$feed_title' WHERE id = '$feed'");
286 $site_url = $rss->channel
["link"];
287 // weird, weird Magpie
288 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
290 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
291 db_query($link, "UPDATE ttrss_feeds SET
292 site_url = '$site_url' WHERE id = '$feed'");
295 // print "I: " . $rss->channel["image"]["url"];
297 $icon_url = $rss->image
["url"];
299 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
300 $icon_url = db_escape_string($icon_url);
301 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
307 $result = db_query($link, "SELECT reg_exp,
308 ttrss_filter_types.name AS name,
309 ttrss_filter_actions.name AS action
310 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
311 owner_uid = $owner_uid AND
312 ttrss_filter_types.id = filter_type AND
313 ttrss_filter_actions.id = action_id AND
314 (feed_id IS NULL OR feed_id = '$feed')");
316 while ($line = db_fetch_assoc($result)) {
317 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
319 $filter["reg_exp"] = $line["reg_exp"];
320 $filter["action"] = $line["action"];
322 array_push($filters[$line["name"]], $filter);
325 $iterator = $rss->items
;
327 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
328 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
330 if (!is_array($iterator)) {
331 db_query($link, "UPDATE ttrss_feeds
332 SET last_error = 'Parse error: can\'t find any articles.'
333 WHERE id = '$feed'");
337 foreach ($iterator as $item) {
339 $entry_guid = $item["id"];
341 if (!$entry_guid) $entry_guid = $item["guid"];
342 if (!$entry_guid) $entry_guid = $item["link"];
344 if (!$entry_guid) continue;
346 $entry_timestamp = "";
348 $rss_2_date = $item['pubdate'];
349 $rss_1_date = $item['dc']['date'];
350 $atom_date = $item['issued'];
351 if (!$atom_date) $atom_date = $item['updated'];
353 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
354 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
355 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
357 if ($entry_timestamp == "") {
358 $entry_timestamp = time();
359 $no_orig_date = 'true';
361 $no_orig_date = 'false';
364 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
366 $entry_title = $item["title"];
368 // strange Magpie workaround
369 $entry_link = $item["link_"];
370 if (!$entry_link) $entry_link = $item["link"];
372 if (!$entry_title) continue;
373 if (!$entry_link) continue;
375 $entry_content = $item["content:escaped"];
377 if (!$entry_content) $entry_content = $item["content:encoded"];
378 if (!$entry_content) $entry_content = $item["content"];
379 if (!$entry_content) $entry_content = $item["summary"];
380 if (!$entry_content) $entry_content = $item["description"];
382 // if (!$entry_content) continue;
385 if (is_array($entry_content)) {
386 $entry_content = $entry_content["encoded"];
387 if (!$entry_content) $entry_content = $entry_content["escaped"];
391 // print_r(htmlspecialchars($entry_content));
394 $entry_content_unescaped = $entry_content;
395 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
397 $entry_comments = $item["comments"];
399 $entry_author = db_escape_string($item['dc']['creator']);
401 $entry_guid = db_escape_string($entry_guid);
403 $result = db_query($link, "SELECT id FROM ttrss_entries
404 WHERE guid = '$entry_guid'");
406 $entry_content = db_escape_string($entry_content);
407 $entry_title = db_escape_string($entry_title);
408 $entry_link = db_escape_string($entry_link);
409 $entry_comments = db_escape_string($entry_comments);
411 $num_comments = db_escape_string($item["slash"]["comments"]);
413 if (!$num_comments) $num_comments = 0;
415 db_query($link, "BEGIN");
417 if (db_num_rows($result) == 0) {
419 // base post entry does not exist, create it
421 $result = db_query($link,
422 "INSERT INTO ttrss_entries
438 '$entry_timestamp_fmt',
447 // we keep encountering the entry in feeds, so we need to
448 // update date_entered column so that we don't get horrible
449 // dupes when the entry gets purged and reinserted again e.g.
450 // in the case of SLOW SLOW OMG SLOW updating feeds
452 $base_entry_id = db_fetch_result($result, 0, "id");
454 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
455 WHERE id = '$base_entry_id'");
458 // now it should exist, if not - bad luck then
460 $result = db_query($link, "SELECT
461 id,content_hash,no_orig_date,title,
462 substring(date_entered,1,19) as date_entered,
463 substring(updated,1,19) as updated,
467 WHERE guid = '$entry_guid'");
469 if (db_num_rows($result) == 1) {
471 // this will be used below in update handler
472 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
473 $orig_title = db_fetch_result($result, 0, "title");
474 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
475 $orig_date_entered = strtotime(db_fetch_result($result,
478 $ref_id = db_fetch_result($result, 0, "id");
480 // check for user post link to main table
482 // do we allow duplicate posts with same GUID in different feeds?
483 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
484 $dupcheck_qpart = "AND feed_id = '$feed'";
486 $dupcheck_qpart = "";
489 // error_reporting(0);
491 $filter_name = get_filter_name($entry_title, $entry_content,
492 $entry_link, $filters);
494 if ($filter_name == "filter") {
498 // error_reporting (DEFAULT_ERROR_LEVEL);
500 $result = db_query($link,
501 "SELECT ref_id FROM ttrss_user_entries WHERE
502 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
505 // okay it doesn't exist - create user entry
506 if (db_num_rows($result) == 0) {
508 if ($filter_name != 'catchup') {
510 $last_read_qpart = 'NULL';
513 $last_read_qpart = 'NOW()';
516 $result = db_query($link,
517 "INSERT INTO ttrss_user_entries
518 (ref_id, owner_uid, feed_id, unread, last_read)
519 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
523 $post_needs_update = false;
525 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
526 ($content_hash != $orig_content_hash)) {
527 $post_needs_update = true;
530 if ($orig_title != $entry_title) {
531 $post_needs_update = true;
534 if ($orig_num_comments != $num_comments) {
535 $post_needs_update = true;
538 // this doesn't seem to be very reliable
540 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
541 // $post_needs_update = true;
544 // if post needs update, update it and mark all user entries
545 // linking to this post as updated
546 if ($post_needs_update) {
548 // print "<!-- post $orig_title needs update : $post_needs_update -->";
550 db_query($link, "UPDATE ttrss_entries
551 SET title = '$entry_title', content = '$entry_content',
552 num_comments = '$num_comments'
553 WHERE id = '$ref_id'");
555 db_query($link, "UPDATE ttrss_user_entries
556 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
561 db_query($link, "COMMIT");
564 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
568 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
569 $entry_content_unescaped, $entry_tags);
571 // print "<br>$entry_title : $entry_content_unescaped<br>";
572 // print_r($entry_tags);
575 $entry_tags = $entry_tags[1];
577 if (count($entry_tags) > 0) {
579 db_query($link, "BEGIN");
581 $result = db_query($link, "SELECT id,int_id
582 FROM ttrss_entries,ttrss_user_entries
583 WHERE guid = '$entry_guid'
584 AND feed_id = '$feed' AND ref_id = id
585 AND owner_uid = '$owner_uid'");
587 if (db_num_rows($result) == 1) {
589 $entry_id = db_fetch_result($result, 0, "id");
590 $entry_int_id = db_fetch_result($result, 0, "int_id");
592 foreach ($entry_tags as $tag) {
593 $tag = db_escape_string(strtolower($tag));
595 $tag = str_replace("+", " ", $tag);
596 $tag = str_replace("technorati tag: ", "", $tag);
598 $result = db_query($link, "SELECT id FROM ttrss_tags
599 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
600 owner_uid = '$owner_uid' LIMIT 1");
602 // print db_fetch_result($result, 0, "id");
604 if ($result && db_num_rows($result) == 0) {
606 // print "tagging $entry_id as $tag<br>";
608 db_query($link, "INSERT INTO ttrss_tags
609 (owner_uid,tag_name,post_int_id)
610 VALUES ('$owner_uid','$tag', '$entry_int_id')");
614 db_query($link, "COMMIT");
618 db_query($link, "UPDATE ttrss_feeds
619 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
621 // db_query($link, "COMMIT");
624 $error_msg = db_escape_string(magpie_error());
626 "UPDATE ttrss_feeds SET last_error = '$error_msg',
627 last_updated = NOW() WHERE id = '$feed'");
632 function print_select($id, $default, $values, $attributes = "") {
633 print "<select id=\"$id\" $attributes>";
634 foreach ($values as $v) {
640 print "<option$sel>$v</option>";
645 function get_filter_name($title, $content, $link, $filters) {
647 if ($filters["title"]) {
648 foreach ($filters["title"] as $filter) {
649 $reg_exp = $filter["reg_exp"];
650 if (preg_match("/$reg_exp/i", $title)) {
651 return $filter["action"];
656 if ($filters["content"]) {
657 foreach ($filters["content"] as $filter) {
658 $reg_exp = $filter["reg_exp"];
659 if (preg_match("/$reg_exp/i", $content)) {
660 return $filter["action"];
665 if ($filters["both"]) {
666 foreach ($filters["both"] as $filter) {
667 $reg_exp = $filter["reg_exp"];
668 if (preg_match("/$reg_exp/i", $title) ||
669 preg_match("/$reg_exp/i", $content)) {
670 return $filter["action"];
675 if ($filters["link"]) {
676 $reg_exp = $filter["reg_exp"];
677 foreach ($filters["link"] as $filter) {
678 $reg_exp = $filter["reg_exp"];
679 if (preg_match("/$reg_exp/i", $link)) {
680 return $filter["action"];
688 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
689 $rtl_content = false) {
691 if (file_exists($icon_file) && filesize($icon_file) > 0) {
692 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
694 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
698 $rtl_tag = "dir=\"rtl\"";
700 $rtl_tag = "dir=\"ltr\"";
703 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
705 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
706 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
710 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
715 $fctr_class = "class=\"invisible\"";
718 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
719 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
725 function getmicrotime() {
726 list($usec, $sec) = explode(" ",microtime());
727 return ((float)$usec +
(float)$sec);
730 function print_radio($id, $default, $values, $attributes = "") {
731 foreach ($values as $v) {
739 $sel .= " value=\"1\"";
741 $sel .= " value=\"0\"";
744 print "<input class=\"noborder\"
745 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
750 function initialize_user_prefs($link, $uid) {
752 $uid = db_escape_string($uid);
754 db_query($link, "BEGIN");
756 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
758 $u_result = db_query($link, "SELECT pref_name
759 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
761 $active_prefs = array();
763 while ($line = db_fetch_assoc($u_result)) {
764 array_push($active_prefs, $line["pref_name"]);
767 while ($line = db_fetch_assoc($result)) {
768 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
769 // print "adding " . $line["pref_name"] . "<br>";
771 db_query($link, "INSERT INTO ttrss_user_prefs
772 (owner_uid,pref_name,value) VALUES
773 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
778 db_query($link, "COMMIT");
782 function authenticate_user($link, $login, $password) {
784 $pwd_hash = 'SHA1:' . sha1($password);
786 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
787 login = '$login' AND pwd_hash = '$pwd_hash'");
789 if (db_num_rows($result) == 1) {
790 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
791 $_SESSION["name"] = db_fetch_result($result, 0, "login");
792 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
794 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
797 $user_theme = get_user_theme_path($link);
799 $_SESSION["theme"] = $user_theme;
800 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
802 initialize_user_prefs($link, $_SESSION["uid"]);
811 function make_password($length = 8) {
814 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
818 while ($i < $length) {
819 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
821 if (!strstr($password, $char)) {
829 // this is called after user is created to initialize default feeds, labels
832 // user preferences are checked on every login, not here
834 function initialize_user($link, $uid) {
836 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
837 values ('$uid','unread = true', 'Unread articles')");
839 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
840 values ('$uid','last_read is null and unread = false', 'Updated articles')");
842 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
843 values ('$uid', 'Tiny Tiny RSS: New Releases',
844 'http://tt-rss.spb.ru/releases.rss')");
848 function logout_user() {
850 if (isset($_COOKIE[session_name()])) {
851 setcookie(session_name(), '', time()-42000, '/');
855 function get_script_urlpath() {
856 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
859 function get_login_redirect() {
860 $server = $_SERVER["SERVER_NAME"];
862 if (ENABLE_LOGIN_SSL
) {
868 $url_path = get_script_urlpath();
870 $redirect_uri = "$protocol://$server$url_path/login.php";
872 return $redirect_uri;
875 function validate_session($link) {
876 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
877 if ($_SESSION["ip_address"]) {
878 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
886 function basic_nosid_redirect_check() {
887 if (!SINGLE_USER_MODE
) {
888 if (!$_COOKIE["ttrss_sid"]) {
889 $redirect_uri = get_login_redirect();
890 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
891 header("Location: $redirect_uri?rt=$return_to");
897 function login_sequence($link) {
898 if (!SINGLE_USER_MODE
) {
900 if (!validate_session($link)) {
902 $redirect_uri = get_login_redirect();
903 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
904 header("Location: $redirect_uri?rt=$return_to");
908 if (!USE_HTTP_AUTH
) {
909 if (!$_SESSION["uid"]) {
910 $redirect_uri = get_login_redirect();
911 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
912 header("Location: $redirect_uri?rt=$return_to");
916 if (!$_SESSION["uid"]) {
917 if (!$_SERVER["PHP_AUTH_USER"]) {
919 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
920 header('HTTP/1.0 401 Unauthorized');
924 $auth_result = authenticate_user($link,
925 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
928 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
929 header('HTTP/1.0 401 Unauthorized');
936 $_SESSION["uid"] = 1;
937 $_SESSION["name"] = "admin";
938 initialize_user_prefs($link, 1);
942 function truncate_string($str, $max_len) {
943 if (mb_strlen($str, "utf-8") > $max_len - 3) {
944 return mb_substr($str, 0, $max_len, "utf-8") . "...";
950 function get_user_theme_path($link) {
951 $result = db_query($link, "SELECT theme_path
953 ttrss_themes,ttrss_users
954 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
955 if (db_num_rows($result) != 0) {
956 return db_fetch_result($result, 0, "theme_path");
962 function smart_date_time($timestamp) {
963 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
964 return date("G:i", $timestamp);
965 } else if (date("Y", $timestamp) == date("Y")) {
966 return date("M d, G:i", $timestamp);
968 return date("Y/m/d G:i", $timestamp);
972 function smart_date($timestamp) {
973 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
975 } else if (date("Y", $timestamp) == date("Y")) {
976 return date("D m", $timestamp);
978 return date("Y/m/d", $timestamp);
982 function sql_bool_to_string($s) {
983 if ($s == "t" ||
$s == "1") {
990 function sql_bool_to_bool($s) {
991 if ($s == "t" ||
$s == "1") {
999 function toggleEvenOdd($a) {
1006 function sanity_check($link) {
1009 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1010 $schema_version = db_fetch_result($result, 0, "schema_version");
1012 if ($schema_version != SCHEMA_VERSION
) {
1016 if ($error_code != 0) {
1017 print "<error error-code='$error_code'/>";
1024 function file_is_locked($filename) {
1026 $fp = fopen($filename, "r");
1027 error_reporting(DEFAULT_ERROR_LEVEL
);
1029 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1030 flock($fp, LOCK_UN
);
1040 function make_lockfile($filename) {
1041 $fp = fopen($filename, "w");
1043 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1050 function sql_random_function() {
1051 if (DB_TYPE
== "mysql") {
1058 function catchup_feed($link, $feed, $cat_view) {
1059 if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1064 $cat_qpart = "cat_id = '$feed'";
1066 $cat_qpart = "cat_id IS NULL";
1069 $tmp_result = db_query($link, "SELECT id
1070 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1073 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1075 $tmp_feed = $tmp_line["id"];
1077 db_query($link, "UPDATE ttrss_user_entries
1078 SET unread = false,last_read = NOW()
1079 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1082 } else if ($feed > 0) {
1084 $tmp_result = db_query($link, "SELECT id
1085 FROM ttrss_feeds WHERE parent_feed = '$feed'
1086 ORDER BY cat_id,title");
1088 $parent_ids = array();
1090 if (db_num_rows($tmp_result) > 0) {
1091 while ($p = db_fetch_assoc($tmp_result)) {
1092 array_push($parent_ids, "feed_id = " . $p["id"]);
1095 $children_qpart = implode(" OR ", $parent_ids);
1097 db_query($link, "UPDATE ttrss_user_entries
1098 SET unread = false,last_read = NOW()
1099 WHERE (feed_id = '$feed' OR $children_qpart)
1100 AND owner_uid = " . $_SESSION["uid"]);
1103 db_query($link, "UPDATE ttrss_user_entries
1104 SET unread = false,last_read = NOW()
1105 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1108 } else if ($feed < 0 && $feed > -10) { // special, like starred
1111 db_query($link, "UPDATE ttrss_user_entries
1112 SET unread = false,last_read = NOW()
1113 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1116 } else if ($feed < -10) { // label
1118 // TODO make this more efficient
1120 $label_id = -$feed - 11;
1122 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1123 WHERE id = '$label_id'");
1126 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1128 db_query($link, "BEGIN");
1130 $tmp2_result = db_query($link,
1134 ttrss_user_entries,ttrss_entries
1138 owner_uid = " . $_SESSION["uid"]);
1140 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1141 db_query($link, "UPDATE
1144 unread = false, last_read = NOW()
1146 int_id = " . $tmp_line["int_id"]);
1149 db_query($link, "COMMIT");
1151 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1152 SET unread = false,last_read = NOW()
1155 AND owner_uid = ".$_SESSION["uid"]); */
1159 db_query($link, "BEGIN");
1161 $tag_name = db_escape_string($feed);
1163 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1164 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1166 while ($line = db_fetch_assoc($result)) {
1167 db_query($link, "UPDATE ttrss_user_entries SET
1168 unread = false, last_read = NOW()
1169 WHERE int_id = " . $line["post_int_id"]);
1171 db_query($link, "COMMIT");
1175 function update_generic_feed($link, $feed, $cat_view) {
1179 $cat_qpart = "cat_id = '$feed'";
1181 $cat_qpart = "cat_id IS NULL";
1184 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1185 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1187 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1188 $feed_url = $tmp_line["feed_url"];
1189 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1193 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1194 WHERE id = '$feed'");
1195 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1196 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1200 function getAllCounters($link) {
1201 getLabelCounters($link);
1202 getFeedCounters($link);
1203 getTagCounters($link);
1204 getGlobalCounters($link);
1205 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1206 getCategoryCounters($link);
1210 function getCategoryCounters($link) {
1211 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1212 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1213 AND unread = true)) AS unread FROM ttrss_feeds
1215 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1217 while ($line = db_fetch_assoc($result)) {
1218 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1219 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1220 $line["unread"]."\"/>";
1224 function getFeedUnread($link, $feed) {
1225 $n_feed = sprintf("%d", $feed);
1227 if ($n_feed == -1) {
1228 $match_part = "marked = true";
1229 } else if ($feed > 0) {
1230 $match_part = "feed_id = '$n_feed'";
1231 } else if ($feed < -10) {
1232 $label_id = -$feed - 11;
1234 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1235 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1237 $match_part = db_fetch_result($result, 0, "sql_exp");
1242 $result = db_query($link, "SELECT count(int_id) AS unread
1243 FROM ttrss_user_entries
1244 WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
1248 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1249 FROM ttrss_tags,ttrss_user_entries
1250 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1251 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1254 $unread = db_fetch_result($result, 0, "unread");
1258 /* FIXME this needs reworking */
1260 function getGlobalUnread($link) {
1261 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1262 WHERE unread = true AND
1263 ttrss_user_entries.ref_id = ttrss_entries.id AND
1264 owner_uid = " . $_SESSION["uid"]);
1265 $c_id = db_fetch_result($result, 0, "c_id");
1269 function getGlobalCounters($link, $global_unread = -1) {
1270 if ($global_unread == -1) {
1271 $global_unread = getGlobalUnread($link);
1273 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1276 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1279 if (!$_SESSION["tctr_last_value"]) {
1280 $_SESSION["tctr_last_value"] = array();
1284 $old_counters = $_SESSION["tctr_last_value"];
1286 $tctrs_modified = false;
1288 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1289 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1290 ttrss_user_entries.ref_id = ttrss_entries.id AND
1291 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1292 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1294 select tag_name,0 as count FROM ttrss_tags
1295 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1297 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1298 FROM ttrss_user_entries WHERE int_id = post_int_id
1299 AND unread = true)) AS count FROM ttrss_tags
1300 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1304 while ($line = db_fetch_assoc($result)) {
1305 $tags[$line["tag_name"]] +
= $line["count"];
1308 foreach (array_keys($tags) as $tag) {
1309 $unread = $tags[$tag];
1311 $tag = htmlspecialchars($tag);
1313 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1314 $old_counters[$tag] = $unread;
1315 $tctrs_modified = true;
1316 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1321 if ($smart_mode && $tctrs_modified) {
1322 $_SESSION["tctr_last_value"] = $old_counters;
1327 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1330 if (!$_SESSION["lctr_last_value"]) {
1331 $_SESSION["lctr_last_value"] = array();
1335 $old_counters = $_SESSION["lctr_last_value"];
1336 $lctrs_modified = false;
1338 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
1339 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1340 unread = true AND owner_uid = ".$_SESSION["uid"]);
1342 $count = db_fetch_result($result, 0, "count");
1344 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1346 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1347 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1349 while ($line = db_fetch_assoc($result)) {
1351 $id = -$line["id"] - 11;
1353 error_reporting (0);
1355 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
1356 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1357 ttrss_user_entries.ref_id = ttrss_entries.id AND
1358 owner_uid = ".$_SESSION["uid"]);
1360 $count = db_fetch_result($tmp_result, 0, "count");
1362 if (!$smart_mode ||
$old_counters[$id] != $count) {
1363 $old_counters[$id] = $count;
1364 $lctrs_modified = true;
1365 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1368 error_reporting (DEFAULT_ERROR_LEVEL
);
1371 if ($smart_mode && $lctrs_modified) {
1372 $_SESSION["lctr_last_value"] = $old_counters;
1376 /* function getFeedCounter($link, $id) {
1378 $result = db_query($link, "SELECT
1379 count(id) as count,last_error
1380 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1381 WHERE feed_id = '$id' AND unread = true
1382 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1383 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1385 $count = db_fetch_result($result, 0, "count");
1386 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1388 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1391 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1394 if (!$_SESSION["fctr_last_value"]) {
1395 $_SESSION["fctr_last_value"] = array();
1399 $old_counters = $_SESSION["fctr_last_value"];
1401 $result = db_query($link, "SELECT id,last_error,parent_feed,
1403 FROM ttrss_entries,ttrss_user_entries
1404 WHERE feed_id = ttrss_feeds.id AND
1405 ttrss_user_entries.ref_id = ttrss_entries.id
1406 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1407 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1408 AND parent_feed IS NULL");
1410 $fctrs_modified = false;
1412 while ($line = db_fetch_assoc($result)) {
1415 $count = $line["count"];
1416 $last_error = htmlspecialchars($line["last_error"]);
1418 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1420 $tmp_result = db_query($link,
1421 "SELECT id,COUNT(unread) AS unread
1422 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1423 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1424 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1426 if (db_num_rows($tmp_result) > 0) {
1427 while ($l = db_fetch_assoc($tmp_result)) {
1428 $count +
= $l["unread"];
1432 if (!$smart_mode ||
$old_counters[$id] != $count) {
1433 $old_counters[$id] = $count;
1434 $fctrs_modified = true;
1437 $error_part = "error=\"$last_error\"";
1443 $has_img_part = "hi=\"$has_img\"";
1448 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part/>";
1452 if ($smart_mode && $fctrs_modified) {
1453 $_SESSION["fctr_last_value"] = $old_counters;