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 if (DB_TYPE
== "mysql") {
60 $random_qpart = "RAND()";
62 $random_qpart = "RANDOM()";
66 $limit_qpart = "LIMIT $limit";
71 $result = db_query($link,
72 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
73 ORDER BY $random_qpart $limit_qpart");
75 while ($line = db_fetch_assoc($result)) {
77 $feed_id = $line["id"];
78 $purge_interval = $line["purge_interval"];
79 $owner_uid = $line["owner_uid"];
81 if ($purge_interval == 0) {
83 $tmp_result = db_query($link,
84 "SELECT value FROM ttrss_user_prefs WHERE
85 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
87 if (db_num_rows($tmp_result) != 0) {
88 $purge_interval = db_fetch_result($tmp_result, 0, "value");
93 // print "Feed $feed_id: purge interval = $purge_interval\n";
96 if ($purge_interval > 0) {
97 purge_feed($link, $feed_id, $purge_interval, $do_output);
101 // purge orphaned posts in main content table
102 db_query($link, "DELETE FROM ttrss_entries WHERE
103 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
107 function purge_old_posts($link) {
109 $user_id = $_SESSION["uid"];
111 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
112 WHERE owner_uid = '$user_id'");
114 while ($line = db_fetch_assoc($result)) {
116 $feed_id = $line["id"];
117 $purge_interval = $line["purge_interval"];
119 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
121 if ($purge_interval > 0) {
122 purge_feed($link, $feed_id, $purge_interval);
126 // purge orphaned posts in main content table
127 db_query($link, "DELETE FROM ttrss_entries WHERE
128 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
131 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
133 if (WEB_DEMO_MODE
) return;
136 $user_id = $_SESSION["uid"];
137 purge_old_posts($link);
140 // db_query($link, "BEGIN");
142 if (MAX_UPDATE_TIME
> 0) {
143 if (DB_TYPE
== "mysql") {
146 $q_order = "RANDOM()";
149 $q_order = "last_updated DESC";
152 $result = db_query($link, "SELECT feed_url,id,
153 SUBSTRING(last_updated,1,19) AS last_updated,
154 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
159 while ($line = db_fetch_assoc($result)) {
160 $upd_intl = $line["update_interval"];
162 if (!$upd_intl ||
$upd_intl == 0) {
163 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
167 // Updates for this feed are disabled
171 if ($fetch ||
(!$line["last_updated"] ||
172 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
174 // print "<!-- feed: ".$line["feed_url"]." -->";
176 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
178 $upd_elapsed = time() - $upd_start;
180 if (MAX_UPDATE_TIME
> 0 && $upd_elapsed > MAX_UPDATE_TIME
) {
186 // db_query($link, "COMMIT");
190 function check_feed_favicon($feed_url, $feed, $link) {
191 $feed_url = str_replace("http://", "", $feed_url);
192 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
194 $icon_url = "http://$feed_url/favicon.ico";
195 $icon_file = ICONS_DIR
. "/$feed.ico";
197 if (!file_exists($icon_file)) {
200 $r = fopen($icon_url, "r");
201 error_reporting (DEFAULT_ERROR_LEVEL
);
204 $tmpfname = tempnam(TMP_DIRECTORY
, "ttrssicon");
206 $t = fopen($tmpfname, "w");
209 $buf = fread($r, 16384);
217 if (!rename($tmpfname, $icon_file)) {
221 chmod($icon_file, 0644);
223 error_reporting (DEFAULT_ERROR_LEVEL
);
229 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
231 if (WEB_DEMO_MODE
) return;
233 if (DAEMON_REFRESH_ONLY
&& !$_GET["daemon"] && !$ignore_daemon) {
237 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
238 FROM ttrss_feeds WHERE id = '$feed'");
240 $auth_login = db_fetch_result($result, 0, "auth_login");
241 $auth_pass = db_fetch_result($result, 0, "auth_pass");
243 $update_interval = db_fetch_result($result, 0, "update_interval");
245 if ($update_interval < 0) { return; }
247 $feed = db_escape_string($feed);
249 $fetch_url = $feed_url;
251 if ($auth_login && $auth_pass) {
252 $url_parts = array();
253 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
255 if ($url_parts[1] && $url_parts[2]) {
256 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
261 $rss = fetch_rss($fetch_url);
263 error_reporting (DEFAULT_ERROR_LEVEL
);
265 $feed = db_escape_string($feed);
269 // db_query($link, "BEGIN");
271 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
272 FROM ttrss_feeds WHERE id = '$feed'");
274 $registered_title = db_fetch_result($result, 0, "title");
275 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
276 $orig_site_url = db_fetch_result($result, 0, "site_url");
278 $owner_uid = db_fetch_result($result, 0, "owner_uid");
280 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
281 check_feed_favicon($feed_url, $feed, $link);
284 if (!$registered_title ||
$registered_title == "[Unknown]") {
285 $feed_title = db_escape_string($rss->channel
["title"]);
286 db_query($link, "UPDATE ttrss_feeds SET
287 title = '$feed_title' WHERE id = '$feed'");
290 $site_url = $rss->channel
["link"];
291 // weird, weird Magpie
292 if (!$site_url) $site_url = db_escape_string($rss->channel
["link_"]);
294 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
295 db_query($link, "UPDATE ttrss_feeds SET
296 site_url = '$site_url' WHERE id = '$feed'");
299 // print "I: " . $rss->channel["image"]["url"];
301 $icon_url = $rss->image
["url"];
303 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
304 $icon_url = db_escape_string($icon_url);
305 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
311 $result = db_query($link, "SELECT reg_exp,
312 ttrss_filter_types.name AS name,
313 ttrss_filter_actions.name AS action
314 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
315 owner_uid = $owner_uid AND
316 ttrss_filter_types.id = filter_type AND
317 ttrss_filter_actions.id = action_id AND
318 (feed_id IS NULL OR feed_id = '$feed')");
320 while ($line = db_fetch_assoc($result)) {
321 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
323 $filter["reg_exp"] = $line["reg_exp"];
324 $filter["action"] = $line["action"];
326 array_push($filters[$line["name"]], $filter);
329 $iterator = $rss->items
;
331 if (!$iterator ||
!is_array($iterator)) $iterator = $rss->entries
;
332 if (!$iterator ||
!is_array($iterator)) $iterator = $rss;
334 if (!is_array($iterator)) {
335 db_query($link, "UPDATE ttrss_feeds
336 SET last_error = 'Parse error: can\'t find any articles.'
337 WHERE id = '$feed'");
341 foreach ($iterator as $item) {
343 $entry_guid = $item["id"];
345 if (!$entry_guid) $entry_guid = $item["guid"];
346 if (!$entry_guid) $entry_guid = $item["link"];
348 if (!$entry_guid) continue;
350 $entry_timestamp = "";
352 $rss_2_date = $item['pubdate'];
353 $rss_1_date = $item['dc']['date'];
354 $atom_date = $item['issued'];
355 if (!$atom_date) $atom_date = $item['updated'];
357 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
358 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
359 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
361 if ($entry_timestamp == "") {
362 $entry_timestamp = time();
363 $no_orig_date = 'true';
365 $no_orig_date = 'false';
368 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
370 $entry_title = $item["title"];
372 // strange Magpie workaround
373 $entry_link = $item["link_"];
374 if (!$entry_link) $entry_link = $item["link"];
376 if (!$entry_title) continue;
377 if (!$entry_link) continue;
379 $entry_content = $item["content:escaped"];
381 if (!$entry_content) $entry_content = $item["content:encoded"];
382 if (!$entry_content) $entry_content = $item["content"];
383 if (!$entry_content) $entry_content = $item["summary"];
384 if (!$entry_content) $entry_content = $item["description"];
386 // if (!$entry_content) continue;
389 if (is_array($entry_content)) {
390 $entry_content = $entry_content["encoded"];
391 if (!$entry_content) $entry_content = $entry_content["escaped"];
395 // print_r(htmlspecialchars($entry_content));
398 $entry_content_unescaped = $entry_content;
399 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
401 $entry_comments = $item["comments"];
403 $entry_author = db_escape_string($item['dc']['creator']);
405 $entry_guid = db_escape_string($entry_guid);
407 $result = db_query($link, "SELECT id FROM ttrss_entries
408 WHERE guid = '$entry_guid'");
410 $entry_content = db_escape_string($entry_content);
411 $entry_title = db_escape_string($entry_title);
412 $entry_link = db_escape_string($entry_link);
413 $entry_comments = db_escape_string($entry_comments);
415 $num_comments = db_escape_string($item["slash"]["comments"]);
417 if (!$num_comments) $num_comments = 0;
419 db_query($link, "BEGIN");
421 if (db_num_rows($result) == 0) {
423 // base post entry does not exist, create it
425 $result = db_query($link,
426 "INSERT INTO ttrss_entries
442 '$entry_timestamp_fmt',
451 // we keep encountering the entry in feeds, so we need to
452 // update date_entered column so that we don't get horrible
453 // dupes when the entry gets purged and reinserted again e.g.
454 // in the case of SLOW SLOW OMG SLOW updating feeds
456 $base_entry_id = db_fetch_result($result, 0, "id");
458 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
459 WHERE id = '$base_entry_id'");
462 // now it should exist, if not - bad luck then
464 $result = db_query($link, "SELECT
465 id,content_hash,no_orig_date,title,
466 substring(date_entered,1,19) as date_entered,
467 substring(updated,1,19) as updated,
471 WHERE guid = '$entry_guid'");
473 if (db_num_rows($result) == 1) {
475 // this will be used below in update handler
476 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
477 $orig_title = db_fetch_result($result, 0, "title");
478 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
479 $orig_date_entered = strtotime(db_fetch_result($result,
482 $ref_id = db_fetch_result($result, 0, "id");
484 // check for user post link to main table
486 // do we allow duplicate posts with same GUID in different feeds?
487 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
488 $dupcheck_qpart = "AND feed_id = '$feed'";
490 $dupcheck_qpart = "";
493 // error_reporting(0);
495 $filter_name = get_filter_name($entry_title, $entry_content,
496 $entry_link, $filters);
498 if ($filter_name == "filter") {
502 // error_reporting (DEFAULT_ERROR_LEVEL);
504 $result = db_query($link,
505 "SELECT ref_id FROM ttrss_user_entries WHERE
506 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
509 // okay it doesn't exist - create user entry
510 if (db_num_rows($result) == 0) {
512 if ($filter_name != 'catchup') {
514 $last_read_qpart = 'NULL';
517 $last_read_qpart = 'NOW()';
520 $result = db_query($link,
521 "INSERT INTO ttrss_user_entries
522 (ref_id, owner_uid, feed_id, unread, last_read)
523 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
527 $post_needs_update = false;
529 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
530 ($content_hash != $orig_content_hash)) {
531 $post_needs_update = true;
534 if ($orig_title != $entry_title) {
535 $post_needs_update = true;
538 if ($orig_num_comments != $num_comments) {
539 $post_needs_update = true;
542 // this doesn't seem to be very reliable
544 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
545 // $post_needs_update = true;
548 // if post needs update, update it and mark all user entries
549 // linking to this post as updated
550 if ($post_needs_update) {
552 // print "<!-- post $orig_title needs update : $post_needs_update -->";
554 db_query($link, "UPDATE ttrss_entries
555 SET title = '$entry_title', content = '$entry_content',
556 num_comments = '$num_comments'
557 WHERE id = '$ref_id'");
559 db_query($link, "UPDATE ttrss_user_entries
560 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
565 db_query($link, "COMMIT");
568 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
572 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
573 $entry_content_unescaped, $entry_tags);
575 // print "<br>$entry_title : $entry_content_unescaped<br>";
576 // print_r($entry_tags);
579 $entry_tags = $entry_tags[1];
581 if (count($entry_tags) > 0) {
583 db_query($link, "BEGIN");
585 $result = db_query($link, "SELECT id,int_id
586 FROM ttrss_entries,ttrss_user_entries
587 WHERE guid = '$entry_guid'
588 AND feed_id = '$feed' AND ref_id = id
589 AND owner_uid = '$owner_uid'");
591 if (db_num_rows($result) == 1) {
593 $entry_id = db_fetch_result($result, 0, "id");
594 $entry_int_id = db_fetch_result($result, 0, "int_id");
596 foreach ($entry_tags as $tag) {
597 $tag = db_escape_string(strtolower($tag));
599 $tag = str_replace("+", " ", $tag);
600 $tag = str_replace("technorati tag: ", "", $tag);
602 $result = db_query($link, "SELECT id FROM ttrss_tags
603 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
604 owner_uid = '$owner_uid' LIMIT 1");
606 // print db_fetch_result($result, 0, "id");
608 if ($result && db_num_rows($result) == 0) {
610 // print "tagging $entry_id as $tag<br>";
612 db_query($link, "INSERT INTO ttrss_tags
613 (owner_uid,tag_name,post_int_id)
614 VALUES ('$owner_uid','$tag', '$entry_int_id')");
618 db_query($link, "COMMIT");
622 db_query($link, "UPDATE ttrss_feeds
623 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
625 // db_query($link, "COMMIT");
628 $error_msg = db_escape_string(magpie_error());
630 "UPDATE ttrss_feeds SET last_error = '$error_msg',
631 last_updated = NOW() WHERE id = '$feed'");
636 function print_select($id, $default, $values, $attributes = "") {
637 print "<select id=\"$id\" $attributes>";
638 foreach ($values as $v) {
644 print "<option$sel>$v</option>";
649 function get_filter_name($title, $content, $link, $filters) {
651 if ($filters["title"]) {
652 foreach ($filters["title"] as $filter) {
653 $reg_exp = $filter["reg_exp"];
654 if (preg_match("/$reg_exp/i", $title)) {
655 return $filter["action"];
660 if ($filters["content"]) {
661 foreach ($filters["content"] as $filter) {
662 $reg_exp = $filter["reg_exp"];
663 if (preg_match("/$reg_exp/i", $content)) {
664 return $filter["action"];
669 if ($filters["both"]) {
670 foreach ($filters["both"] as $filter) {
671 $reg_exp = $filter["reg_exp"];
672 if (preg_match("/$reg_exp/i", $title) ||
673 preg_match("/$reg_exp/i", $content)) {
674 return $filter["action"];
679 if ($filters["link"]) {
680 $reg_exp = $filter["reg_exp"];
681 foreach ($filters["link"] as $filter) {
682 $reg_exp = $filter["reg_exp"];
683 if (preg_match("/$reg_exp/i", $link)) {
684 return $filter["action"];
692 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
694 if (file_exists($icon_file) && filesize($icon_file) > 0) {
695 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
697 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
700 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
702 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
703 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
707 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
712 $fctr_class = "class=\"invisible\"";
715 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
716 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
722 function getmicrotime() {
723 list($usec, $sec) = explode(" ",microtime());
724 return ((float)$usec +
(float)$sec);
727 function print_radio($id, $default, $values, $attributes = "") {
728 foreach ($values as $v) {
736 $sel .= " value=\"1\"";
738 $sel .= " value=\"0\"";
741 print "<input class=\"noborder\"
742 type=\"radio\" $sel $attributes name=\"$id\"> $v ";
747 function initialize_user_prefs($link, $uid) {
749 $uid = db_escape_string($uid);
751 db_query($link, "BEGIN");
753 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
755 $u_result = db_query($link, "SELECT pref_name
756 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
758 $active_prefs = array();
760 while ($line = db_fetch_assoc($u_result)) {
761 array_push($active_prefs, $line["pref_name"]);
764 while ($line = db_fetch_assoc($result)) {
765 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
766 // print "adding " . $line["pref_name"] . "<br>";
768 db_query($link, "INSERT INTO ttrss_user_prefs
769 (owner_uid,pref_name,value) VALUES
770 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
775 db_query($link, "COMMIT");
779 function authenticate_user($link, $login, $password) {
781 $pwd_hash = 'SHA1:' . sha1($password);
783 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
784 login = '$login' AND pwd_hash = '$pwd_hash'");
786 if (db_num_rows($result) == 1) {
787 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
788 $_SESSION["name"] = db_fetch_result($result, 0, "login");
789 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
791 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
794 $user_theme = get_user_theme_path($link);
796 $_SESSION["theme"] = $user_theme;
797 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
799 initialize_user_prefs($link, $_SESSION["uid"]);
808 function make_password($length = 8) {
811 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
815 while ($i < $length) {
816 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
818 if (!strstr($password, $char)) {
826 // this is called after user is created to initialize default feeds, labels
829 // user preferences are checked on every login, not here
831 function initialize_user($link, $uid) {
833 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
834 values ('$uid','unread = true', 'Unread articles')");
836 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
837 values ('$uid','last_read is null and unread = false', 'Updated articles')");
839 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
840 values ('$uid', 'Tiny Tiny RSS: New Releases',
841 'http://tt-rss.spb.ru/releases.rss')");
845 function logout_user() {
847 if (isset($_COOKIE[session_name()])) {
848 setcookie(session_name(), '', time()-42000, '/');
852 function get_script_urlpath() {
853 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
856 function get_login_redirect() {
857 $server = $_SERVER["SERVER_NAME"];
859 if (ENABLE_LOGIN_SSL
) {
865 $url_path = get_script_urlpath();
867 $redirect_uri = "$protocol://$server$url_path/login.php";
869 return $redirect_uri;
872 function validate_session($link) {
873 if (SESSION_CHECK_ADDRESS
&& !DATABASE_BACKED_SESSIONS
&& $_SESSION["uid"]) {
874 if ($_SESSION["ip_address"]) {
875 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
883 function basic_nosid_redirect_check() {
884 if (!SINGLE_USER_MODE
) {
885 if (!$_COOKIE["ttrss_sid"]) {
886 $redirect_uri = get_login_redirect();
887 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
888 header("Location: $redirect_uri?rt=$return_to");
894 function login_sequence($link) {
895 if (!SINGLE_USER_MODE
) {
897 if (!validate_session($link)) {
899 $redirect_uri = get_login_redirect();
900 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
901 header("Location: $redirect_uri?rt=$return_to");
905 if (!USE_HTTP_AUTH
) {
906 if (!$_SESSION["uid"]) {
907 $redirect_uri = get_login_redirect();
908 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
909 header("Location: $redirect_uri?rt=$return_to");
913 if (!$_SESSION["uid"]) {
914 if (!$_SERVER["PHP_AUTH_USER"]) {
916 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
917 header('HTTP/1.0 401 Unauthorized');
921 $auth_result = authenticate_user($link,
922 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
925 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
926 header('HTTP/1.0 401 Unauthorized');
933 $_SESSION["uid"] = 1;
934 $_SESSION["name"] = "admin";
935 initialize_user_prefs($link, 1);
939 function truncate_string($str, $max_len) {
940 if (mb_strlen($str, "utf-8") > $max_len - 3) {
941 return mb_substr($str, 0, $max_len, "utf-8") . "...";
947 function get_user_theme_path($link) {
948 $result = db_query($link, "SELECT theme_path
950 ttrss_themes,ttrss_users
951 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
952 if (db_num_rows($result) != 0) {
953 return db_fetch_result($result, 0, "theme_path");
959 function smart_date_time($timestamp) {
960 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
961 return date("G:i", $timestamp);
962 } else if (date("Y", $timestamp) == date("Y")) {
963 return date("M d, G:i", $timestamp);
965 return date("Y/m/d G:i", $timestamp);
969 function smart_date($timestamp) {
970 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
972 } else if (date("Y", $timestamp) == date("Y")) {
973 return date("D m", $timestamp);
975 return date("Y/m/d", $timestamp);
979 function sql_bool_to_string($s) {
980 if ($s == "t" ||
$s == "1") {
987 function sql_bool_to_bool($s) {
988 if ($s == "t" ||
$s == "1") {
996 function toggleEvenOdd($a) {
1003 function sanity_check($link) {
1006 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1007 $schema_version = db_fetch_result($result, 0, "schema_version");
1009 if ($schema_version != SCHEMA_VERSION
) {
1013 if ($error_code != 0) {
1014 print "<error error-code='$error_code'/>";
1021 function file_is_locked($filename) {
1023 $fp = fopen($filename, "r");
1024 error_reporting(DEFAULT_ERROR_LEVEL
);
1026 if (flock($fp, LOCK_EX | LOCK_NB
)) {
1027 flock($fp, LOCK_UN
);
1037 function make_lockfile($filename) {
1038 $fp = fopen($filename, "w");
1040 if (flock($fp, LOCK_EX | LOCK_NB
)) {