4 require_once 'config.php';
5 require_once 'db-prefs.php';
7 // $_SESSION["uid"] = PLACEHOLDER_UID; // FIXME: placeholder
8 // $_SESSION["name"] = PLACEHOLDER_NAME;
10 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
12 function purge_feed($link, $feed_id, $purge_interval) {
14 if (DB_TYPE
== "pgsql") {
15 db_query($link, "DELETE FROM ttrss_entries WHERE
16 marked = false AND feed_id = '$feed_id' AND
17 date_entered < NOW() - INTERVAL '$purge_interval days'");
19 db_query($link, "DELETE FROM ttrss_entries WHERE
20 marked = false AND feed_id = '$feed_id' AND
21 date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
25 function global_purge_old_posts($link, $do_output = false) {
27 $result = db_query($link,
28 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds");
30 while ($line = db_fetch_assoc($result)) {
32 $feed_id = $line["id"];
33 $purge_interval = $line["purge_interval"];
34 $owner_uid = $line["owner_uid"];
36 if ($purge_interval == 0) {
38 $tmp_result = db_query($link,
39 "SELECT value FROM ttrss_user_prefs WHERE
40 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
42 if (db_num_rows($tmp_result) != 0) {
43 $purge_interval = db_fetch_result($tmp_result, 0, "value");
48 print "<feed id='$feed_id' p_intl='$purge_interval'/>";
51 if ($purge_interval > 0) {
52 purge_feed($link, $feed_id, $purge_interval);
58 function purge_old_posts($link) {
60 $user_id = $_SESSION["uid"];
62 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
63 WHERE owner_uid = '$user_id'");
65 while ($line = db_fetch_assoc($result)) {
67 $feed_id = $line["id"];
68 $purge_interval = $line["purge_interval"];
70 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
72 if ($purge_interval > 0) {
73 purge_feed($link, $feed_id, $purge_interval);
78 function update_all_feeds($link, $fetch) {
80 if (WEB_DEMO_MODE
) return;
82 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
83 if (!$_GET["daemon"]) {
88 db_query($link, "BEGIN");
90 $user_id = $_SESSION["uid"];
92 $result = db_query($link, "SELECT feed_url,id,
93 substring(last_updated,1,19) as last_updated,
94 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
96 while ($line = db_fetch_assoc($result)) {
97 $upd_intl = $line["update_interval"];
99 if (!$upd_intl ||
$upd_intl == 0) {
100 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
103 if ($fetch ||
(!$line["last_updated"] ||
104 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
106 update_rss_feed($link, $line["feed_url"], $line["id"]);
110 purge_old_posts($link);
112 db_query($link, "COMMIT");
116 function check_feed_favicon($feed_url, $feed, $link) {
117 $feed_url = str_replace("http://", "", $feed_url);
118 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
120 $icon_url = "http://$feed_url/favicon.ico";
121 $icon_file = ICONS_DIR
. "/$feed.ico";
123 if (!file_exists($icon_file)) {
126 $r = fopen($icon_url, "r");
127 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
130 $tmpfname = tempnam("/tmp", "ttrssicon");
132 $t = fopen($tmpfname, "w");
135 $buf = fread($r, 16384);
143 if (!rename($tmpfname, $icon_file)) {
147 chmod($icon_file, 0644);
149 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
155 function update_rss_feed($link, $feed_url, $feed) {
157 if (WEB_DEMO_MODE
) return;
159 $feed = db_escape_string($feed);
162 $rss = fetch_rss($feed_url);
164 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
166 db_query($link, "BEGIN");
168 $feed = db_escape_string($feed);
172 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
173 check_feed_favicon($feed_url, $feed, $link);
176 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
178 $registered_title = db_fetch_result($result, 0, "title");
179 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
181 if (!$registered_title) {
182 $feed_title = db_escape_string($rss->channel
["title"]);
183 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
186 // print "I: " . $rss->channel["image"]["url"];
188 $icon_url = $rss->image
["url"];
190 if ($icon_url && !$orig_icon_url) {
191 $icon_url = db_escape_string($icon_url);
192 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
198 $result = db_query($link, "SELECT reg_exp,
199 (SELECT name FROM ttrss_filter_types
200 WHERE id = filter_type) as name
201 FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]);
203 while ($line = db_fetch_assoc($result)) {
204 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
205 array_push($filters[$line["name"]], $line["reg_exp"]);
208 foreach ($rss->items
as $item) {
210 $entry_guid = $item["id"];
212 if (!$entry_guid) $entry_guid = $item["guid"];
213 if (!$entry_guid) $entry_guid = $item["link"];
215 if (!$entry_guid) continue;
217 $entry_timestamp = "";
219 $rss_2_date = $item['pubdate'];
220 $rss_1_date = $item['dc']['date'];
221 $atom_date = $item['issued'];
223 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
224 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
225 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
227 if ($entry_timestamp == "") {
228 $entry_timestamp = time();
229 $no_orig_date = 'true';
231 $no_orig_date = 'false';
234 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
236 $entry_title = $item["title"];
237 $entry_link = $item["link"];
239 if (!$entry_title) continue;
240 if (!$entry_link) continue;
242 $entry_content = $item["content:escaped"];
244 if (!$entry_content) $entry_content = $item["content:encoded"];
245 if (!$entry_content) $entry_content = $item["content"];
246 if (!$entry_content) $entry_content = $item["description"];
248 // if (!$entry_content) continue;
251 if (is_array($entry_content)) {
252 $entry_content = $entry_content["encoded"];
253 if (!$entry_content) $entry_content = $entry_content["escaped"];
257 // print_r($entry_content);
259 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
261 $entry_comments = $item["comments"];
263 $entry_guid = db_escape_string($entry_guid);
265 $result = db_query($link, "
267 id,last_read,no_orig_date,title,feed_id,content_hash,
268 substring(updated,1,19) as updated
272 guid = '$entry_guid' AND
273 owner_uid = " . $_SESSION["uid"]." AND
276 // print db_num_rows($result) . "$entry_guid<br/>";
278 if (db_num_rows($result) == 0) {
281 if (is_filtered($entry_title, $entry_content, $filters)) {
284 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
286 //$entry_guid = db_escape_string($entry_guid);
287 $entry_content = db_escape_string($entry_content);
288 $entry_title = db_escape_string($entry_title);
289 $entry_link = db_escape_string($entry_link);
290 $entry_comments = db_escape_string($entry_comments);
309 '$entry_timestamp_fmt',
315 NOW(),".$_SESSION["uid"].")";
317 $result = db_query($link, $query);
321 $orig_entry_id = db_fetch_result($result, 0, "id");
322 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
324 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
326 if ($orig_feed_id != $feed) {
327 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
331 $entry_is_modified = false;
333 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
335 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
336 $orig_last_read = db_fetch_result($result, 0, "last_read");
337 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
338 $orig_title = db_fetch_result($result, 0, "title");
340 $last_read_qpart = "";
342 if ($orig_content_hash != $content_hash) {
343 // print "$orig_content_hash :: $content_hash<br>";
345 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
346 $last_read_qpart = 'last_read = null,';
348 $entry_is_modified = true;
351 if ($orig_title != $entry_title) {
352 $entry_is_modified = true;
355 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
356 $entry_is_modified = true;
359 if ($entry_is_modified) {
361 // print "$entry_guid Modified!<br>";
363 $entry_comments = db_escape_string($entry_comments);
364 $entry_content = db_escape_string($entry_content);
365 $entry_title = db_escape_string($entry_title);
366 $entry_link = db_escape_string($entry_link);
368 $query = "UPDATE ttrss_entries
371 title = '$entry_title',
372 link = '$entry_link',
373 updated = '$entry_timestamp_fmt',
374 content = '$entry_content',
375 comments = '$entry_comments',
376 content_hash = '$content_hash'
378 id = '$orig_entry_id'";
380 $result = db_query($link, $query);
385 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
389 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
392 $entry_tags = $entry_tags[1];
394 if (count($entry_tags) > 0) {
396 $result = db_query($link, "SELECT id FROM ttrss_entries
397 WHERE guid = '$entry_guid'
398 AND feed_id = '$feed'
399 AND owner_uid = " . $_SESSION["uid"]);
401 if (!$result ||
db_num_rows($result) != 1) {
405 $entry_id = db_fetch_result($result, 0, "id");
407 foreach ($entry_tags as $tag) {
408 $tag = db_escape_string(strtolower($tag));
410 $tag = str_replace("technorati tag: ", "", $tag);
412 $result = db_query($link, "SELECT id FROM ttrss_tags
413 WHERE tag_name = '$tag' AND post_id = '$entry_id' AND
414 owner_uid = ".$_SESSION["uid"]." LIMIT 1");
416 // print db_fetch_result($result, 0, "id");
418 if ($result && db_num_rows($result) == 0) {
420 // print "tagging $entry_id as $tag<br>";
422 db_query($link, "INSERT INTO ttrss_tags (owner_uid,tag_name,post_id)
423 VALUES ('".$_SESSION["uid"]."','$tag', '$entry_id')");
429 db_query($link, "UPDATE ttrss_feeds
430 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
433 $error_msg = db_escape_string(magpie_error());
435 "UPDATE ttrss_feeds SET last_error = '$error_msg',
436 last_updated = NOW() WHERE id = '$feed'");
439 db_query($link, "COMMIT");
443 function print_select($id, $default, $values, $attributes = "") {
444 print "<select id=\"$id\" $attributes>";
445 foreach ($values as $v) {
451 print "<option$sel>$v</option>";
456 function is_filtered($title, $content, $filters) {
458 if ($filters["title"]) {
459 foreach ($filters["title"] as $title_filter) {
460 if (preg_match("/$title_filter/i", $title))
465 if ($filters["content"]) {
466 foreach ($filters["content"] as $content_filter) {
467 if (preg_match("/$content_filter/i", $content))
472 if ($filters["both"]) {
473 foreach ($filters["both"] as $filter) {
474 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
482 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
484 if (file_exists($icon_file) && filesize($icon_file) > 0) {
485 $feed_icon = "<img src=\"$icon_file\">";
487 $feed_icon = "<img src=\"images/blank_icon.gif\">";
490 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
492 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
493 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
497 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
502 $fctr_class = "class=\"invisible\"";
505 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
506 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
512 function getmicrotime() {
513 list($usec, $sec) = explode(" ",microtime());
514 return ((float)$usec +
(float)$sec);
517 function print_radio($id, $default, $values, $attributes = "") {
518 foreach ($values as $v) {
526 $sel .= " value=\"1\"";
528 $sel .= " value=\"0\"";
531 print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v ";
536 function initialize_user_prefs($link, $uid) {
538 $uid = db_escape_string($uid);
540 db_query($link, "BEGIN");
542 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
544 $u_result = db_query($link, "SELECT pref_name
545 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
547 $active_prefs = array();
549 while ($line = db_fetch_assoc($u_result)) {
550 array_push($active_prefs, $line["pref_name"]);
553 while ($line = db_fetch_assoc($result)) {
554 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
555 // print "adding " . $line["pref_name"] . "<br>";
557 db_query($link, "INSERT INTO ttrss_user_prefs
558 (owner_uid,pref_name,value) VALUES
559 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
564 db_query($link, "COMMIT");
568 function authenticate_user($link, $login, $password) {
570 $pwd_hash = 'SHA1:' . sha1($password);
572 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
573 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
575 if (db_num_rows($result) == 1) {
576 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
577 $_SESSION["name"] = db_fetch_result($result, 0, "login");
578 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
580 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
590 function http_authenticate_user($link) {
592 if (!$_SERVER['PHP_AUTH_USER']) {
594 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
595 header('HTTP/1.0 401 Unauthorized');
596 print "<h1>401 Unathorized</h1>";
601 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
602 $password = db_escape_string($_SERVER['PHP_AUTH_PW']);
604 return authenticate_user($link, $login, $password);
608 function make_password($length = 8) {
611 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
615 while ($i < $length) {
616 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
618 if (!strstr($password, $char)) {
626 // this is called after user is created to initialize default feeds, labels
629 // user preferences are checked on every login, not here
631 function initialize_user($link, $uid) {
633 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
634 values ('$uid','unread = true', 'Unread articles')");
636 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
637 values ('$uid','last_read is null and unread = false', 'Updated articles')");
639 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
640 values ('$uid', 'Tiny Tiny RSS Dev. Feed',
641 'http://bah.spb.su/darcsweb/darcsweb.cgi?r=tt-rss;a=rss')");