]>
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';
12 require_once 'errors.php';
14 require_once 'magpierss/rss_utils.inc';
16 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
18 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
22 if (DB_TYPE
== "pgsql") {
23 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
24 marked = false AND feed_id = '$feed_id' AND
25 (SELECT date_entered FROM ttrss_entries WHERE
26 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
28 $pg_version = get_pgsql_version($link);
30 if (preg_match("/^7\./", $pg_version) ||
preg_match("/^8\.0/", $pg_version)) {
32 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
33 ttrss_entries.id = ref_id AND
35 feed_id = '$feed_id' AND
36 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
40 $result = db_query($link, "DELETE FROM ttrss_user_entries
42 WHERE ttrss_entries.id = ref_id AND
44 feed_id = '$feed_id' AND
45 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
48 $rows = pg_affected_rows($result);
52 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
53 marked = false AND feed_id = '$feed_id' AND
54 (SELECT date_entered FROM ttrss_entries WHERE
55 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
57 $result = db_query($link, "DELETE FROM ttrss_user_entries
58 USING ttrss_user_entries, ttrss_entries
59 WHERE ttrss_entries.id = ref_id AND
61 feed_id = '$feed_id' AND
62 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
64 $rows = mysql_affected_rows($link);
69 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
73 function global_purge_old_posts($link, $do_output = false, $limit = false) {
75 $random_qpart = sql_random_function();
78 $limit_qpart = "LIMIT $limit";
83 $result = db_query($link,
84 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
85 ORDER BY $random_qpart $limit_qpart");
87 while ($line = db_fetch_assoc($result)) {
89 $feed_id = $line["id"];
90 $purge_interval = $line["purge_interval"];
91 $owner_uid = $line["owner_uid"];
93 if ($purge_interval == 0) {
95 $tmp_result = db_query($link,
96 "SELECT value FROM ttrss_user_prefs WHERE
97 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
99 if (db_num_rows($tmp_result) != 0) {
100 $purge_interval = db_fetch_result($tmp_result, 0, "value");
105 // print "Feed $feed_id: purge interval = $purge_interval\n";
108 if ($purge_interval > 0) {
109 purge_feed($link, $feed_id, $purge_interval, $do_output);
113 // purge orphaned posts in main content table
114 db_query($link, "DELETE FROM ttrss_entries WHERE
115 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
119 function purge_old_posts($link) {
121 $user_id = $_SESSION["uid"];
123 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
124 WHERE owner_uid = '$user_id'");
126 while ($line = db_fetch_assoc($result)) {
128 $feed_id = $line["id"];
129 $purge_interval = $line["purge_interval"];
131 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
133 if ($purge_interval > 0) {
134 purge_feed($link, $feed_id, $purge_interval);
138 // purge orphaned posts in main content table
139 db_query($link, "DELETE FROM ttrss_entries WHERE
140 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
143 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
145 if (WEB_DEMO_MODE
) return;
148 $user_id = $_SESSION["uid"];
149 purge_old_posts($link);
152 // db_query($link, "BEGIN");
154 if (MAX_UPDATE_TIME
> 0) {
155 if (DB_TYPE
== "mysql") {
158 $q_order = "RANDOM()";
161 $q_order = "last_updated DESC";
164 $result = db_query($link, "SELECT feed_url,id,
165 SUBSTRING(last_updated,1,19) AS last_updated,
166 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
171 while ($line = db_fetch_assoc($result)) {
172 $upd_intl = $line["update_interval"];
174 if (!$upd_intl ||
$upd_intl == 0) {
175 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
179 // Updates for this feed are disabled
183 if ($fetch ||
(!$line["last_updated"] ||
184 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
186 // print "<!-- feed: ".$line["feed_url"]." -->";
188 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
190 $upd_elapsed = time() - $upd_start;
192 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
198 // db_query($link, "COMMIT");
202 function check_feed_favicon($feed_url, $feed, $link) {
203 $feed_url = str_replace("http://", "", $feed_url);
204 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
206 $icon_url = "http://$feed_url/favicon.ico";
207 $icon_file = ICONS_DIR
. "/$feed.ico";
209 if (!file_exists($icon_file)) {
212 $r = fopen($icon_url, "r");
213 error_reporting (DEFAULT_ERROR_LEVEL
);
216 $tmpfname = tempnam(TMP_DIRECTORY
, "ttrssicon");
218 $t = fopen($tmpfname, "w");
221 $buf = fread($r, 16384);
229 if (!rename($tmpfname, $icon_file)) {
233 chmod($icon_file, 0644);
235 error_reporting (DEFAULT_ERROR_LEVEL
);
241 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
243 if (WEB_DEMO_MODE
) return;
245 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
249 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
250 FROM ttrss_feeds WHERE id = '$feed'");
252 $auth_login = db_fetch_result($result, 0, "auth_login");
253 $auth_pass = db_fetch_result($result, 0, "auth_pass");
255 $update_interval = db_fetch_result($result, 0, "update_interval");
257 if ($update_interval < 0) { return; }
259 $feed = db_escape_string($feed);
261 $fetch_url = $feed_url;
263 if ($auth_login && $auth_pass) {
264 $url_parts = array();
265 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
267 if ($url_parts[1] && $url_parts[2]) {
268 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
273 $rss = fetch_rss($fetch_url);
275 error_reporting (DEFAULT_ERROR_LEVEL
);
277 $feed = db_escape_string($feed);
281 // db_query($link, "BEGIN");
283 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
284 FROM ttrss_feeds WHERE id = '$feed'");
286 $registered_title = db_fetch_result($result, 0, "title");
287 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
288 $orig_site_url = db_fetch_result($result, 0, "site_url");
290 $owner_uid = db_fetch_result($result, 0, "owner_uid");
292 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
293 check_feed_favicon($feed_url, $feed, $link);
296 if (!$registered_title ||
$registered_title == "[Unknown]") {
297 $feed_title = db_escape_string($rss->channel
["title"]);
298 db_query($link, "UPDATE ttrss_feeds SET
299 title = '$feed_title' WHERE id = '$feed'");
302 $site_url = $rss->channel
["link"];
303 // weird, weird Magpie
304 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
306 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
307 db_query($link, "UPDATE ttrss_feeds SET
308 site_url = '$site_url' WHERE id = '$feed'");
311 // print "I: " . $rss->channel["image"]["url"];
313 $icon_url = $rss->image
["url"];
315 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
316 $icon_url = db_escape_string($icon_url);
317 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
323 $result = db_query($link, "SELECT reg_exp,
324 ttrss_filter_types.name AS name,
325 ttrss_filter_actions.name AS action
326 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
327 owner_uid = $owner_uid AND
328 ttrss_filter_types.id = filter_type AND
329 ttrss_filter_actions.id = action_id AND
330 (feed_id IS NULL OR feed_id = '$feed')");
332 while ($line = db_fetch_assoc($result)) {
333 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
335 $filter["reg_exp"] = $line["reg_exp"];
336 $filter["action"] = $line["action"];
338 array_push($filters[$line["name"]], $filter);
341 $iterator = $rss->items
;
343 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
344 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
346 if (!is_array($iterator)) {
347 db_query($link, "UPDATE ttrss_feeds
348 SET last_error = 'Parse error: can\'t find any articles.'
349 WHERE id = '$feed'");
353 foreach ($iterator as $item) {
355 $entry_guid = $item["id"];
357 if (!$entry_guid) $entry_guid = $item["guid"];
358 if (!$entry_guid) $entry_guid = $item["link"];
360 if (!$entry_guid) continue;
362 $entry_timestamp = "";
364 $rss_2_date = $item['pubdate'];
365 $rss_1_date = $item['dc']['date'];
366 $atom_date = $item['issued'];
367 if (!$atom_date) $atom_date = $item['updated'];
369 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
370 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
371 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
373 if ($entry_timestamp == "") {
374 $entry_timestamp = time();
375 $no_orig_date = 'true';
377 $no_orig_date = 'false';
380 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
382 $entry_title = $item["title"];
384 // strange Magpie workaround
385 $entry_link = $item["link_"];
386 if (!$entry_link) $entry_link = $item["link"];
388 if (!$entry_title) continue;
389 if (!$entry_link) continue;
391 $entry_content = $item["content:escaped"];
393 if (!$entry_content) $entry_content = $item["content:encoded"];
394 if (!$entry_content) $entry_content = $item["content"];
395 if (!$entry_content) $entry_content = $item["summary"];
396 if (!$entry_content) $entry_content = $item["description"];
398 // if (!$entry_content) continue;
401 if (is_array($entry_content)) {
402 $entry_content = $entry_content["encoded"];
403 if (!$entry_content) $entry_content = $entry_content["escaped"];
407 // print_r(htmlspecialchars($entry_content));
410 $entry_content_unescaped = $entry_content;
411 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
413 $entry_comments = $item["comments"];
415 $entry_author = db_escape_string($item['dc']['creator']);
417 $entry_guid = db_escape_string($entry_guid);
419 $result = db_query($link, "SELECT id FROM ttrss_entries
420 WHERE guid = '$entry_guid'");
422 $entry_content = db_escape_string($entry_content);
423 $entry_title = db_escape_string($entry_title);
424 $entry_link = db_escape_string($entry_link);
425 $entry_comments = db_escape_string($entry_comments);
427 $num_comments = db_escape_string($item["slash"]["comments"]);
429 if (!$num_comments) $num_comments = 0;
431 db_query($link, "BEGIN");
433 if (db_num_rows($result) == 0) {
435 // base post entry does not exist, create it
437 $result = db_query($link,
438 "INSERT INTO ttrss_entries
454 '$entry_timestamp_fmt',
463 // we keep encountering the entry in feeds, so we need to
464 // update date_entered column so that we don't get horrible
465 // dupes when the entry gets purged and reinserted again e.g.
466 // in the case of SLOW SLOW OMG SLOW updating feeds
468 $base_entry_id = db_fetch_result($result, 0, "id");
470 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
471 WHERE id = '$base_entry_id'");
474 // now it should exist, if not - bad luck then
476 $result = db_query($link, "SELECT
477 id,content_hash,no_orig_date,title,
478 substring(date_entered,1,19) as date_entered,
479 substring(updated,1,19) as updated,
483 WHERE guid = '$entry_guid'");
485 if (db_num_rows($result) == 1) {
487 // this will be used below in update handler
488 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
489 $orig_title = db_fetch_result($result, 0, "title");
490 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
491 $orig_date_entered = strtotime(db_fetch_result($result,
494 $ref_id = db_fetch_result($result, 0, "id");
496 // check for user post link to main table
498 // do we allow duplicate posts with same GUID in different feeds?
499 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
500 $dupcheck_qpart = "AND feed_id = '$feed'";
502 $dupcheck_qpart = "";
505 // error_reporting(0);
507 $filter_name = get_filter_name($entry_title, $entry_content,
508 $entry_link, $filters);
510 if ($filter_name == "filter") {
514 // error_reporting (DEFAULT_ERROR_LEVEL);
516 $result = db_query($link,
517 "SELECT ref_id FROM ttrss_user_entries WHERE
518 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
521 // okay it doesn't exist - create user entry
522 if (db_num_rows($result) == 0) {
524 if ($filter_name != 'catchup') {
526 $last_read_qpart = 'NULL';
529 $last_read_qpart = 'NOW()';
532 $result = db_query($link,
533 "INSERT INTO ttrss_user_entries
534 (ref_id, owner_uid, feed_id, unread, last_read)
535 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
539 $post_needs_update = false;
541 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
542 ($content_hash != $orig_content_hash)) {
543 $post_needs_update = true;
546 if ($orig_title != $entry_title) {
547 $post_needs_update = true;
550 if ($orig_num_comments != $num_comments) {
551 $post_needs_update = true;
554 // this doesn't seem to be very reliable
556 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
557 // $post_needs_update = true;
560 // if post needs update, update it and mark all user entries
561 // linking to this post as updated
562 if ($post_needs_update) {
564 // print "<!-- post $orig_title needs update : $post_needs_update -->";
566 db_query($link, "UPDATE ttrss_entries
567 SET title = '$entry_title', content = '$entry_content',
568 num_comments = '$num_comments'
569 WHERE id = '$ref_id'");
571 db_query($link, "UPDATE ttrss_user_entries
572 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
577 db_query($link, "COMMIT");
580 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
584 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
585 $entry_content_unescaped, $entry_tags);
587 // print "<br>$entry_title : $entry_content_unescaped<br>";
588 // print_r($entry_tags);
591 $entry_tags = $entry_tags[1];
593 if (count($entry_tags) > 0) {
595 db_query($link, "BEGIN");
597 $result = db_query($link, "SELECT id,int_id
598 FROM ttrss_entries,ttrss_user_entries
599 WHERE guid = '$entry_guid'
600 AND feed_id = '$feed' AND ref_id = id
601 AND owner_uid = '$owner_uid'");
603 if (db_num_rows($result) == 1) {
605 $entry_id = db_fetch_result($result, 0, "id");
606 $entry_int_id = db_fetch_result($result, 0, "int_id");
608 foreach ($entry_tags as $tag) {
609 $tag = db_escape_string(strtolower($tag));
611 $tag = str_replace("+", " ", $tag);
612 $tag = str_replace("technorati tag: ", "", $tag);
614 $result = db_query($link, "SELECT id FROM ttrss_tags
615 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
616 owner_uid = '$owner_uid' LIMIT 1");
618 // print db_fetch_result($result, 0, "id");
620 if ($result && db_num_rows($result) == 0) {
622 // print "tagging $entry_id as $tag<br>";
624 db_query($link, "INSERT INTO ttrss_tags
625 (owner_uid,tag_name,post_int_id)
626 VALUES ('$owner_uid','$tag', '$entry_int_id')");
630 db_query($link, "COMMIT");
634 db_query($link, "UPDATE ttrss_feeds
635 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
637 // db_query($link, "COMMIT");
640 $error_msg = db_escape_string(magpie_error());
642 "UPDATE ttrss_feeds SET last_error = '$error_msg',
643 last_updated = NOW() WHERE id = '$feed'");
648 function print_select($id, $default, $values, $attributes = "") {
649 print "<select id=\"$id\" $attributes>";
650 foreach ($values as $v) {
656 print "<option$sel>$v</option>";
661 function get_filter_name($title, $content, $link, $filters) {
663 if ($filters["title"]) {
664 foreach ($filters["title"] as $filter) {
665 $reg_exp = $filter["reg_exp"];
666 if (preg_match("/$reg_exp/i", $title)) {
667 return $filter["action"];
672 if ($filters["content"]) {
673 foreach ($filters["content"] as $filter) {
674 $reg_exp = $filter["reg_exp"];
675 if (preg_match("/$reg_exp/i", $content)) {
676 return $filter["action"];
681 if ($filters["both"]) {
682 foreach ($filters["both"] as $filter) {
683 $reg_exp = $filter["reg_exp"];
684 if (preg_match("/$reg_exp/i", $title) ||
685 preg_match("/$reg_exp/i", $content)) {
686 return $filter["action"];
691 if ($filters["link"]) {
692 $reg_exp = $filter["reg_exp"];
693 foreach ($filters["link"] as $filter) {
694 $reg_exp = $filter["reg_exp"];
695 if (preg_match("/$reg_exp/i", $link)) {
696 return $filter["action"];
704 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
705 $rtl_content = false, $last_updated = false, $last_error = false) {
707 if (file_exists($icon_file) && filesize($icon_file) > 0) {
708 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
710 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
714 $rtl_tag = "dir=\"rtl\"";
716 $rtl_tag = "dir=\"ltr\"";
720 $link_title = "Error: $last_error ($last_updated)";
721 } else if ($last_updated) {
722 $link_title = "Updated: $last_updated";
725 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
727 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
728 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
732 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
737 $fctr_class = "class=\"invisible\"";
740 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
741 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
747 function getmicrotime() {
748 list($usec, $sec) = explode(" ",microtime());
749 return ((float)$usec +
(float)$sec);
752 function print_radio($id, $default, $values, $attributes = "") {
753 foreach ($values as $v) {
761 $sel .= " value=\"1\"";
763 $sel .= " value=\"0\"";
766 print "<input class=\"noborder\"
767 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
772 function initialize_user_prefs($link, $uid) {
774 $uid = db_escape_string($uid);
776 db_query($link, "BEGIN");
778 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
780 $u_result = db_query($link, "SELECT pref_name
781 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
783 $active_prefs = array();
785 while ($line = db_fetch_assoc($u_result)) {
786 array_push($active_prefs, $line["pref_name"]);
789 while ($line = db_fetch_assoc($result)) {
790 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
791 // print "adding " . $line["pref_name"] . "<br>";
793 db_query($link, "INSERT INTO ttrss_user_prefs
794 (owner_uid,pref_name,value) VALUES
795 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
800 db_query($link, "COMMIT");
804 function authenticate_user($link, $login, $password) {
806 $pwd_hash = 'SHA1:' . sha1($password);
808 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
809 login = '$login' AND pwd_hash = '$pwd_hash'");
811 if (db_num_rows($result) == 1) {
812 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
813 $_SESSION["name"] = db_fetch_result($result, 0, "login");
814 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
816 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
819 $user_theme = get_user_theme_path($link);
821 $_SESSION["theme"] = $user_theme;
822 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
824 initialize_user_prefs($link, $_SESSION["uid"]);
833 function make_password($length = 8) {
836 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
840 while ($i < $length) {
841 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
843 if (!strstr($password, $char)) {
851 // this is called after user is created to initialize default feeds, labels
854 // user preferences are checked on every login, not here
856 function initialize_user($link, $uid) {
858 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
859 values ('$uid','unread = true', 'Unread articles')");
861 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
862 values ('$uid','last_read is null and unread = false', 'Updated articles')");
864 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
865 values ('$uid', 'Tiny Tiny RSS: New Releases',
866 'http://tt-rss.spb.ru/releases.rss')");
870 function logout_user() {
872 if (isset($_COOKIE[session_name()])) {
873 setcookie(session_name(), '', time()-42000, '/');
877 function get_script_urlpath() {
878 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
881 function get_login_redirect() {
882 $server = $_SERVER["SERVER_NAME"];
884 if (ENABLE_LOGIN_SSL
) {
890 $url_path = get_script_urlpath();
892 $redirect_uri = "$protocol://$server$url_path/login.php";
894 return $redirect_uri;
897 function validate_session($link) {
898 if (SESSION_CHECK_ADDRESS
&& $_SESSION["uid"]) {
899 if ($_SESSION["ip_address"]) {
900 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
908 function basic_nosid_redirect_check() {
909 if (!SINGLE_USER_MODE
) {
910 if (!$_COOKIE["ttrss_sid"]) {
911 $redirect_uri = get_login_redirect();
912 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
913 header("Location: $redirect_uri?rt=$return_to");
919 function login_sequence($link) {
920 if (!SINGLE_USER_MODE
) {
922 if (!validate_session($link)) {
924 $redirect_uri = get_login_redirect();
925 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
926 header("Location: $redirect_uri?rt=$return_to");
930 if (!USE_HTTP_AUTH
) {
931 if (!$_SESSION["uid"]) {
932 $redirect_uri = get_login_redirect();
933 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
934 header("Location: $redirect_uri?rt=$return_to");
938 if (!$_SESSION["uid"]) {
939 if (!$_SERVER["PHP_AUTH_USER"]) {
941 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
942 header('HTTP/1.0 401 Unauthorized');
946 $auth_result = authenticate_user($link,
947 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
950 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
951 header('HTTP/1.0 401 Unauthorized');
958 $_SESSION["uid"] = 1;
959 $_SESSION["name"] = "admin";
960 initialize_user_prefs($link, 1);
964 function truncate_string($str, $max_len) {
965 if (mb_strlen($str, "utf-8") > $max_len - 3) {
966 return mb_substr($str, 0, $max_len, "utf-8") . "...";
972 function get_user_theme_path($link) {
973 $result = db_query($link, "SELECT theme_path
975 ttrss_themes,ttrss_users
976 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
977 if (db_num_rows($result) != 0) {
978 return db_fetch_result($result, 0, "theme_path");
984 function smart_date_time($timestamp) {
985 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
986 return date("G:i", $timestamp);
987 } else if (date("Y", $timestamp) == date("Y")) {
988 return date("M d, G:i", $timestamp);
990 return date("Y/m/d G:i", $timestamp);
994 function smart_date($timestamp) {
995 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
997 } else if (date("Y", $timestamp) == date("Y")) {
998 return date("D m", $timestamp);
1000 return date("Y/m/d", $timestamp);
1004 function sql_bool_to_string($s) {
1005 if ($s == "t" ||
$s == "1") {
1012 function sql_bool_to_bool($s) {
1013 if ($s == "t" ||
$s == "1") {
1021 function toggleEvenOdd($a) {
1028 function sanity_check($link) {
1031 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1032 $schema_version = db_fetch_result($result, 0, "schema_version");
1034 if ($schema_version != SCHEMA_VERSION
) {
1038 if ($error_code != 0) {
1039 print "<error error-code='$error_code'/>";
1046 function file_is_locked($filename) {
1048 $fp = fopen($filename, "r");
1049 error_reporting(DEFAULT_ERROR_LEVEL
);
1051 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1052 flock($fp, LOCK_UN
);
1062 function make_lockfile($filename) {
1063 $fp = fopen($filename, "w");
1065 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1072 function sql_random_function() {
1073 if (DB_TYPE
== "mysql") {
1080 function catchup_feed($link, $feed, $cat_view) {
1081 if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1086 $cat_qpart = "cat_id = '$feed'";
1088 $cat_qpart = "cat_id IS NULL";
1091 $tmp_result = db_query($link, "SELECT id
1092 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1095 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1097 $tmp_feed = $tmp_line["id"];
1099 db_query($link, "UPDATE ttrss_user_entries
1100 SET unread = false,last_read = NOW()
1101 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1104 } else if ($feed > 0) {
1106 $tmp_result = db_query($link, "SELECT id
1107 FROM ttrss_feeds WHERE parent_feed = '$feed'
1108 ORDER BY cat_id,title");
1110 $parent_ids = array();
1112 if (db_num_rows($tmp_result) > 0) {
1113 while ($p = db_fetch_assoc($tmp_result)) {
1114 array_push($parent_ids, "feed_id = " . $p["id"]);
1117 $children_qpart = implode(" OR ", $parent_ids);
1119 db_query($link, "UPDATE ttrss_user_entries
1120 SET unread = false,last_read = NOW()
1121 WHERE (feed_id = '$feed' OR $children_qpart)
1122 AND owner_uid = " . $_SESSION["uid"]);
1125 db_query($link, "UPDATE ttrss_user_entries
1126 SET unread = false,last_read = NOW()
1127 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1130 } else if ($feed < 0 && $feed > -10) { // special, like starred
1133 db_query($link, "UPDATE ttrss_user_entries
1134 SET unread = false,last_read = NOW()
1135 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1138 } else if ($feed < -10) { // label
1140 // TODO make this more efficient
1142 $label_id = -$feed - 11;
1144 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1145 WHERE id = '$label_id'");
1148 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1150 db_query($link, "BEGIN");
1152 $tmp2_result = db_query($link,
1156 ttrss_user_entries,ttrss_entries
1160 owner_uid = " . $_SESSION["uid"]);
1162 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1163 db_query($link, "UPDATE
1166 unread = false, last_read = NOW()
1168 int_id = " . $tmp_line["int_id"]);
1171 db_query($link, "COMMIT");
1173 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1174 SET unread = false,last_read = NOW()
1177 AND owner_uid = ".$_SESSION["uid"]); */
1181 db_query($link, "BEGIN");
1183 $tag_name = db_escape_string($feed);
1185 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1186 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1188 while ($line = db_fetch_assoc($result)) {
1189 db_query($link, "UPDATE ttrss_user_entries SET
1190 unread = false, last_read = NOW()
1191 WHERE int_id = " . $line["post_int_id"]);
1193 db_query($link, "COMMIT");
1197 function update_generic_feed($link, $feed, $cat_view) {
1201 $cat_qpart = "cat_id = '$feed'";
1203 $cat_qpart = "cat_id IS NULL";
1206 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1207 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1209 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1210 $feed_url = $tmp_line["feed_url"];
1211 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1215 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1216 WHERE id = '$feed'");
1217 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1218 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON
);
1222 function getAllCounters($link) {
1223 getLabelCounters($link);
1224 getFeedCounters($link);
1225 getTagCounters($link);
1226 getGlobalCounters($link);
1227 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1228 getCategoryCounters($link);
1232 function getCategoryCounters($link) {
1233 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1234 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1235 AND unread = true)) AS unread FROM ttrss_feeds
1237 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1239 while ($line = db_fetch_assoc($result)) {
1240 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1241 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1242 $line["unread"]."\"/>";
1246 function getFeedUnread($link, $feed) {
1247 $n_feed = sprintf("%d", $feed);
1249 if ($n_feed == -1) {
1250 $match_part = "marked = true";
1251 } else if ($feed > 0) {
1252 $match_part = "feed_id = '$n_feed'";
1253 } else if ($feed < -10) {
1254 $label_id = -$feed - 11;
1256 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1257 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1259 $match_part = db_fetch_result($result, 0, "sql_exp");
1264 $result = db_query($link, "SELECT count(int_id) AS unread
1265 FROM ttrss_user_entries
1266 WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
1270 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1271 FROM ttrss_tags,ttrss_user_entries
1272 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1273 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1276 $unread = db_fetch_result($result, 0, "unread");
1280 /* FIXME this needs reworking */
1282 function getGlobalUnread($link) {
1283 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1284 WHERE unread = true AND
1285 ttrss_user_entries.ref_id = ttrss_entries.id AND
1286 owner_uid = " . $_SESSION["uid"]);
1287 $c_id = db_fetch_result($result, 0, "c_id");
1291 function getGlobalCounters($link, $global_unread = -1) {
1292 if ($global_unread == -1) {
1293 $global_unread = getGlobalUnread($link);
1295 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1298 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1301 if (!$_SESSION["tctr_last_value"]) {
1302 $_SESSION["tctr_last_value"] = array();
1306 $old_counters = $_SESSION["tctr_last_value"];
1308 $tctrs_modified = false;
1310 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1311 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1312 ttrss_user_entries.ref_id = ttrss_entries.id AND
1313 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1314 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1316 select tag_name,0 as count FROM ttrss_tags
1317 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1319 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1320 FROM ttrss_user_entries WHERE int_id = post_int_id
1321 AND unread = true)) AS count FROM ttrss_tags
1322 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1326 while ($line = db_fetch_assoc($result)) {
1327 $tags[$line["tag_name"]] +
= $line["count"];
1330 foreach (array_keys($tags) as $tag) {
1331 $unread = $tags[$tag];
1333 $tag = htmlspecialchars($tag);
1335 if (!$smart_mode ||
$old_counters[$tag] != $unread) {
1336 $old_counters[$tag] = $unread;
1337 $tctrs_modified = true;
1338 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1343 if ($smart_mode && $tctrs_modified) {
1344 $_SESSION["tctr_last_value"] = $old_counters;
1349 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1352 if (!$_SESSION["lctr_last_value"]) {
1353 $_SESSION["lctr_last_value"] = array();
1357 $old_counters = $_SESSION["lctr_last_value"];
1358 $lctrs_modified = false;
1360 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
1361 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1362 unread = true AND owner_uid = ".$_SESSION["uid"]);
1364 $count = db_fetch_result($result, 0, "count");
1366 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1368 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1369 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1371 while ($line = db_fetch_assoc($result)) {
1373 $id = -$line["id"] - 11;
1375 error_reporting (0);
1377 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
1378 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1379 ttrss_user_entries.ref_id = ttrss_entries.id AND
1380 owner_uid = ".$_SESSION["uid"]);
1382 $count = db_fetch_result($tmp_result, 0, "count");
1384 if (!$smart_mode ||
$old_counters[$id] != $count) {
1385 $old_counters[$id] = $count;
1386 $lctrs_modified = true;
1387 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1390 error_reporting (DEFAULT_ERROR_LEVEL
);
1393 if ($smart_mode && $lctrs_modified) {
1394 $_SESSION["lctr_last_value"] = $old_counters;
1398 /* function getFeedCounter($link, $id) {
1400 $result = db_query($link, "SELECT
1401 count(id) as count,last_error
1402 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1403 WHERE feed_id = '$id' AND unread = true
1404 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1405 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1407 $count = db_fetch_result($result, 0, "count");
1408 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1410 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1413 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS
) {
1416 if (!$_SESSION["fctr_last_value"]) {
1417 $_SESSION["fctr_last_value"] = array();
1421 $old_counters = $_SESSION["fctr_last_value"];
1423 $result = db_query($link, "SELECT id,last_error,parent_feed,
1424 SUBSTRING(last_updated,1,19) AS last_updated,
1426 FROM ttrss_entries,ttrss_user_entries
1427 WHERE feed_id = ttrss_feeds.id AND
1428 ttrss_user_entries.ref_id = ttrss_entries.id
1429 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1430 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1431 AND parent_feed IS NULL");
1433 $fctrs_modified = false;
1435 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1437 while ($line = db_fetch_assoc($result)) {
1440 $count = $line["count"];
1441 $last_error = htmlspecialchars($line["last_error"]);
1443 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1444 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1446 $last_updated = date($short_date, strtotime($line["last_updated"]));
1449 $has_img = is_file(ICONS_DIR
. "/$id.ico");
1451 $tmp_result = db_query($link,
1452 "SELECT id,COUNT(unread) AS unread
1453 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1454 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1455 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1457 if (db_num_rows($tmp_result) > 0) {
1458 while ($l = db_fetch_assoc($tmp_result)) {
1459 $count +
= $l["unread"];
1463 if (!$smart_mode ||
$old_counters[$id] != $count) {
1464 $old_counters[$id] = $count;
1465 $fctrs_modified = true;
1468 $error_part = "error=\"$last_error\"";
1474 $has_img_part = "hi=\"$has_img\"";
1479 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1483 if ($smart_mode && $fctrs_modified) {
1484 $_SESSION["fctr_last_value"] = $old_counters;
1488 function get_script_dt_add() {
1489 if (strpos(VERSION
, "99") === false) {
1496 function get_pgsql_version($link) {
1497 $result = db_query($link, "SELECT version() AS version");
1498 $version = split(" ", db_fetch_result($result, 0, "version"));
1502 function print_error_xml($code, $add_msg = "") {
1505 $error_msg = $ERRORS[$code];
1508 $error_msg = "$error_msg; $add_msg";
1511 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";