]>
git.wh0rd.org - tt-rss.git/blob - functions.php
adb71be231f2fc9ee96118e86e9847903aa41c20
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, $last_updated = false, $last_error = 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\"";
704 $link_title = "Error: $last_error ($last_updated)";
706 $link_title = "Updated: $last_updated";
709 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
711 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
712 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
716 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
721 $fctr_class = "class=\"invisible\"";
724 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
725 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
731 function getmicrotime() {
732 list($usec, $sec) = explode(" ",microtime());
733 return ((float)$usec +
(float)$sec);
736 function print_radio($id, $default, $values, $attributes = "") {
737 foreach ($values as $v) {
745 $sel .= " value=\"1\"";
747 $sel .= " value=\"0\"";
750 print "<input class=\"noborder\"
751 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
756 function initialize_user_prefs($link, $uid) {
758 $uid = db_escape_string($uid);
760 db_query($link, "BEGIN");
762 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
764 $u_result = db_query($link, "SELECT pref_name
765 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
767 $active_prefs = array();
769 while ($line = db_fetch_assoc($u_result)) {
770 array_push($active_prefs, $line["pref_name"]);
773 while ($line = db_fetch_assoc($result)) {
774 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
775 // print "adding " . $line["pref_name"] . "<br>";
777 db_query($link, "INSERT INTO ttrss_user_prefs
778 (owner_uid,pref_name,value) VALUES
779 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
784 db_query($link, "COMMIT");
788 function authenticate_user($link, $login, $password) {
790 $pwd_hash = 'SHA1:' . sha1($password);
792 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
793 login = '$login' AND pwd_hash = '$pwd_hash'");
795 if (db_num_rows($result) == 1) {
796 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
797 $_SESSION["name"] = db_fetch_result($result, 0, "login");
798 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
800 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
803 $user_theme = get_user_theme_path($link);
805 $_SESSION["theme"] = $user_theme;
806 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
808 initialize_user_prefs($link, $_SESSION["uid"]);
817 function make_password($length = 8) {
820 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
824 while ($i < $length) {
825 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
827 if (!strstr($password, $char)) {
835 // this is called after user is created to initialize default feeds, labels
838 // user preferences are checked on every login, not here
840 function initialize_user($link, $uid) {
842 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
843 values ('$uid','unread = true', 'Unread articles')");
845 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
846 values ('$uid','last_read is null and unread = false', 'Updated articles')");
848 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
849 values ('$uid', 'Tiny Tiny RSS: New Releases',
850 'http://tt-rss.spb.ru/releases.rss')");
854 function logout_user() {
856 if (isset($_COOKIE[session_name()])) {
857 setcookie(session_name(), '', time()-42000, '/');
861 function get_script_urlpath() {
862 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
865 function get_login_redirect() {
866 $server = $_SERVER["SERVER_NAME"];
868 if (ENABLE_LOGIN_SSL
) {
874 $url_path = get_script_urlpath();
876 $redirect_uri = "$protocol://$server$url_path/login.php";
878 return $redirect_uri;
881 function validate_session($link) {
882 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
883 if ($_SESSION["ip_address"]) {
884 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
892 function basic_nosid_redirect_check() {
893 if (!SINGLE_USER_MODE
) {
894 if (!$_COOKIE["ttrss_sid"]) {
895 $redirect_uri = get_login_redirect();
896 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
897 header("Location: $redirect_uri?rt=$return_to");
903 function login_sequence($link) {
904 if (!SINGLE_USER_MODE
) {
906 if (!validate_session($link)) {
908 $redirect_uri = get_login_redirect();
909 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
910 header("Location: $redirect_uri?rt=$return_to");
914 if (!USE_HTTP_AUTH
) {
915 if (!$_SESSION["uid"]) {
916 $redirect_uri = get_login_redirect();
917 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
918 header("Location: $redirect_uri?rt=$return_to");
922 if (!$_SESSION["uid"]) {
923 if (!$_SERVER["PHP_AUTH_USER"]) {
925 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
926 header('HTTP/1.0 401 Unauthorized');
930 $auth_result = authenticate_user($link,
931 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
934 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
935 header('HTTP/1.0 401 Unauthorized');
942 $_SESSION["uid"] = 1;
943 $_SESSION["name"] = "admin";
944 initialize_user_prefs($link, 1);
948 function truncate_string($str, $max_len) {
949 if (mb_strlen($str, "utf-8") > $max_len - 3) {
950 return mb_substr($str, 0, $max_len, "utf-8") . "...";
956 function get_user_theme_path($link) {
957 $result = db_query($link, "SELECT theme_path
959 ttrss_themes,ttrss_users
960 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
961 if (db_num_rows($result) != 0) {
962 return db_fetch_result($result, 0, "theme_path");
968 function smart_date_time($timestamp) {
969 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
970 return date("G:i", $timestamp);
971 } else if (date("Y", $timestamp) == date("Y")) {
972 return date("M d, G:i", $timestamp);
974 return date("Y/m/d G:i", $timestamp);
978 function smart_date($timestamp) {
979 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
981 } else if (date("Y", $timestamp) == date("Y")) {
982 return date("D m", $timestamp);
984 return date("Y/m/d", $timestamp);
988 function sql_bool_to_string($s) {
989 if ($s == "t" ||
$s == "1") {
996 function sql_bool_to_bool($s) {
997 if ($s == "t" ||
$s == "1") {
1005 function toggleEvenOdd($a) {
1012 function sanity_check($link) {
1015 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1016 $schema_version = db_fetch_result($result, 0, "schema_version");
1018 if ($schema_version != SCHEMA_VERSION
) {
1022 if ($error_code != 0) {
1023 print "<error error-code='$error_code'/>";
1030 function file_is_locked($filename) {
1032 $fp = fopen($filename, "r");
1033 error_reporting(DEFAULT_ERROR_LEVEL
);
1035 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1036 flock($fp, LOCK_UN
);
1046 function make_lockfile($filename) {
1047 $fp = fopen($filename, "w");
1049 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1056 function sql_random_function() {
1057 if (DB_TYPE
== "mysql") {
1064 function catchup_feed($link, $feed, $cat_view) {
1065 if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1070 $cat_qpart = "cat_id = '$feed'";
1072 $cat_qpart = "cat_id IS NULL";
1075 $tmp_result = db_query($link, "SELECT id
1076 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1079 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1081 $tmp_feed = $tmp_line["id"];
1083 db_query($link, "UPDATE ttrss_user_entries
1084 SET unread = false,last_read = NOW()
1085 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1088 } else if ($feed > 0) {
1090 $tmp_result = db_query($link, "SELECT id
1091 FROM ttrss_feeds WHERE parent_feed = '$feed'
1092 ORDER BY cat_id,title");
1094 $parent_ids = array();
1096 if (db_num_rows($tmp_result) > 0) {
1097 while ($p = db_fetch_assoc($tmp_result)) {
1098 array_push($parent_ids, "feed_id = " . $p["id"]);
1101 $children_qpart = implode(" OR ", $parent_ids);
1103 db_query($link, "UPDATE ttrss_user_entries
1104 SET unread = false,last_read = NOW()
1105 WHERE (feed_id = '$feed' OR $children_qpart)
1106 AND owner_uid = " . $_SESSION["uid"]);
1109 db_query($link, "UPDATE ttrss_user_entries
1110 SET unread = false,last_read = NOW()
1111 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1114 } else if ($feed < 0 && $feed > -10) { // special, like starred
1117 db_query($link, "UPDATE ttrss_user_entries
1118 SET unread = false,last_read = NOW()
1119 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1122 } else if ($feed < -10) { // label
1124 // TODO make this more efficient
1126 $label_id = -$feed - 11;
1128 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1129 WHERE id = '$label_id'");
1132 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1134 db_query($link, "BEGIN");
1136 $tmp2_result = db_query($link,
1140 ttrss_user_entries,ttrss_entries
1144 owner_uid = " . $_SESSION["uid"]);
1146 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1147 db_query($link, "UPDATE
1150 unread = false, last_read = NOW()
1152 int_id = " . $tmp_line["int_id"]);
1155 db_query($link, "COMMIT");
1157 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1158 SET unread = false,last_read = NOW()
1161 AND owner_uid = ".$_SESSION["uid"]); */
1165 db_query($link, "BEGIN");
1167 $tag_name = db_escape_string($feed);
1169 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1170 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1172 while ($line = db_fetch_assoc($result)) {
1173 db_query($link, "UPDATE ttrss_user_entries SET
1174 unread = false, last_read = NOW()
1175 WHERE int_id = " . $line["post_int_id"]);
1177 db_query($link, "COMMIT");
1181 function update_generic_feed($link, $feed, $cat_view) {
1185 $cat_qpart = "cat_id = '$feed'";
1187 $cat_qpart = "cat_id IS NULL";
1190 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1191 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1193 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1194 $feed_url = $tmp_line["feed_url"];
1195 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1199 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1200 WHERE id = '$feed'");
1201 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1202 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1206 function getAllCounters($link) {
1207 getLabelCounters($link);
1208 getFeedCounters($link);
1209 getTagCounters($link);
1210 getGlobalCounters($link);
1211 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1212 getCategoryCounters($link);
1216 function getCategoryCounters($link) {
1217 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1218 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1219 AND unread = true)) AS unread FROM ttrss_feeds
1221 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1223 while ($line = db_fetch_assoc($result)) {
1224 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1225 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1226 $line["unread"]."\"/>";
1230 function getFeedUnread($link, $feed) {
1231 $n_feed = sprintf("%d", $feed);
1233 if ($n_feed == -1) {
1234 $match_part = "marked = true";
1235 } else if ($feed > 0) {
1236 $match_part = "feed_id = '$n_feed'";
1237 } else if ($feed < -10) {
1238 $label_id = -$feed - 11;
1240 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1241 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1243 $match_part = db_fetch_result($result, 0, "sql_exp");
1248 $result = db_query($link, "SELECT count(int_id) AS unread
1249 FROM ttrss_user_entries
1250 WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
1254 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1255 FROM ttrss_tags,ttrss_user_entries
1256 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1257 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1260 $unread = db_fetch_result($result, 0, "unread");
1264 /* FIXME this needs reworking */
1266 function getGlobalUnread($link) {
1267 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1268 WHERE unread = true AND
1269 ttrss_user_entries.ref_id = ttrss_entries.id AND
1270 owner_uid = " . $_SESSION["uid"]);
1271 $c_id = db_fetch_result($result, 0, "c_id");
1275 function getGlobalCounters($link, $global_unread = -1) {
1276 if ($global_unread == -1) {
1277 $global_unread = getGlobalUnread($link);
1279 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1282 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1285 if (!$_SESSION["tctr_last_value"]) {
1286 $_SESSION["tctr_last_value"] = array();
1290 $old_counters = $_SESSION["tctr_last_value"];
1292 $tctrs_modified = false;
1294 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1295 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1296 ttrss_user_entries.ref_id = ttrss_entries.id AND
1297 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1298 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1300 select tag_name,0 as count FROM ttrss_tags
1301 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1303 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1304 FROM ttrss_user_entries WHERE int_id = post_int_id
1305 AND unread = true)) AS count FROM ttrss_tags
1306 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1310 while ($line = db_fetch_assoc($result)) {
1311 $tags[$line["tag_name"]] +
= $line["count"];
1314 foreach (array_keys($tags) as $tag) {
1315 $unread = $tags[$tag];
1317 $tag = htmlspecialchars($tag);
1319 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1320 $old_counters[$tag] = $unread;
1321 $tctrs_modified = true;
1322 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1327 if ($smart_mode && $tctrs_modified) {
1328 $_SESSION["tctr_last_value"] = $old_counters;
1333 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1336 if (!$_SESSION["lctr_last_value"]) {
1337 $_SESSION["lctr_last_value"] = array();
1341 $old_counters = $_SESSION["lctr_last_value"];
1342 $lctrs_modified = false;
1344 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
1345 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1346 unread = true AND owner_uid = ".$_SESSION["uid"]);
1348 $count = db_fetch_result($result, 0, "count");
1350 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1352 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1353 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1355 while ($line = db_fetch_assoc($result)) {
1357 $id = -$line["id"] - 11;
1359 error_reporting (0);
1361 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
1362 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1363 ttrss_user_entries.ref_id = ttrss_entries.id AND
1364 owner_uid = ".$_SESSION["uid"]);
1366 $count = db_fetch_result($tmp_result, 0, "count");
1368 if (!$smart_mode ||
$old_counters[$id] != $count) {
1369 $old_counters[$id] = $count;
1370 $lctrs_modified = true;
1371 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1374 error_reporting (DEFAULT_ERROR_LEVEL
);
1377 if ($smart_mode && $lctrs_modified) {
1378 $_SESSION["lctr_last_value"] = $old_counters;
1382 /* function getFeedCounter($link, $id) {
1384 $result = db_query($link, "SELECT
1385 count(id) as count,last_error
1386 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1387 WHERE feed_id = '$id' AND unread = true
1388 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1389 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1391 $count = db_fetch_result($result, 0, "count");
1392 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1394 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1397 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1400 if (!$_SESSION["fctr_last_value"]) {
1401 $_SESSION["fctr_last_value"] = array();
1405 $old_counters = $_SESSION["fctr_last_value"];
1407 $result = db_query($link, "SELECT id,last_error,parent_feed,
1408 SUBSTRING(last_updated,1,19) AS last_updated,
1410 FROM ttrss_entries,ttrss_user_entries
1411 WHERE feed_id = ttrss_feeds.id AND
1412 ttrss_user_entries.ref_id = ttrss_entries.id
1413 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1414 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1415 AND parent_feed IS NULL");
1417 $fctrs_modified = false;
1419 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1421 while ($line = db_fetch_assoc($result)) {
1424 $count = $line["count"];
1425 $last_error = htmlspecialchars($line["last_error"]);
1427 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1428 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1430 $last_updated = date($short_date, strtotime($line["last_updated"]));
1433 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1435 $tmp_result = db_query($link,
1436 "SELECT id,COUNT(unread) AS unread
1437 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1438 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1439 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1441 if (db_num_rows($tmp_result) > 0) {
1442 while ($l = db_fetch_assoc($tmp_result)) {
1443 $count +
= $l["unread"];
1447 if (!$smart_mode ||
$old_counters[$id] != $count) {
1448 $old_counters[$id] = $count;
1449 $fctrs_modified = true;
1452 $error_part = "error=\"$last_error\"";
1458 $has_img_part = "hi=\"$has_img\"";
1463 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1467 if ($smart_mode && $fctrs_modified) {
1468 $_SESSION["fctr_last_value"] = $old_counters;