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_old_posts($link) {
14 $user_id = $_SESSION["uid"];
16 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
17 WHERE owner_uid = '$user_id'");
19 while ($line = db_fetch_assoc($result)) {
21 $feed_id = $line["id"];
22 $purge_interval = $line["purge_interval"];
24 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
26 if ($purge_interval > 0) {
28 if (DB_TYPE
== "pgsql") {
29 db_query($link, "DELETE FROM ttrss_entries WHERE
30 marked = false AND feed_id = '$feed_id' AND
31 date_entered < NOW() - INTERVAL '$purge_interval days'");
33 db_query($link, "DELETE FROM ttrss_entries WHERE
34 marked = false AND feed_id = '$feed_id' AND
35 date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
41 function update_all_feeds($link, $fetch) {
43 if (WEB_DEMO_MODE
) return;
45 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
46 if (!$_GET["daemon"]) {
51 db_query($link, "BEGIN");
53 $user_id = $_SESSION["uid"];
55 $result = db_query($link, "SELECT feed_url,id,
56 substring(last_updated,1,19) as last_updated,
57 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
59 while ($line = db_fetch_assoc($result)) {
60 $upd_intl = $line["update_interval"];
62 if (!$upd_intl ||
$upd_intl == 0) {
63 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
66 if (!$line["last_updated"] ||
67 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
69 update_rss_feed($link, $line["feed_url"], $line["id"]);
73 purge_old_posts($link);
75 db_query($link, "COMMIT");
79 function check_feed_favicon($feed_url, $feed, $link) {
80 $feed_url = str_replace("http://", "", $feed_url);
81 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
83 $icon_url = "http://$feed_url/favicon.ico";
84 $icon_file = ICONS_DIR
. "/$feed.ico";
86 if (!file_exists($icon_file)) {
89 $r = fopen($icon_url, "r");
90 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
93 $tmpfname = tempnam("/tmp", "ttrssicon");
95 $t = fopen($tmpfname, "w");
98 $buf = fread($r, 16384);
106 if (!rename($tmpfname, $icon_file)) {
110 chmod($icon_file, 0644);
112 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
118 function update_rss_feed($link, $feed_url, $feed) {
120 if (WEB_DEMO_MODE
) return;
122 $feed = db_escape_string($feed);
125 $rss = fetch_rss($feed_url);
127 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
129 db_query($link, "BEGIN");
131 $feed = db_escape_string($feed);
135 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
136 check_feed_favicon($feed_url, $feed, $link);
139 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
141 $registered_title = db_fetch_result($result, 0, "title");
142 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
144 if (!$registered_title) {
145 $feed_title = db_escape_string($rss->channel
["title"]);
146 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
149 // print "I: " . $rss->channel["image"]["url"];
151 $icon_url = $rss->image
["url"];
153 if ($icon_url && !$orig_icon_url) {
154 $icon_url = db_escape_string($icon_url);
155 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
161 $result = db_query($link, "SELECT reg_exp,
162 (SELECT name FROM ttrss_filter_types
163 WHERE id = filter_type) as name
164 FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]);
166 while ($line = db_fetch_assoc($result)) {
167 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
168 array_push($filters[$line["name"]], $line["reg_exp"]);
171 foreach ($rss->items
as $item) {
173 $entry_guid = $item["id"];
175 if (!$entry_guid) $entry_guid = $item["guid"];
176 if (!$entry_guid) $entry_guid = $item["link"];
178 if (!$entry_guid) continue;
180 $entry_timestamp = "";
182 $rss_2_date = $item['pubdate'];
183 $rss_1_date = $item['dc']['date'];
184 $atom_date = $item['issued'];
186 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
187 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
188 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
190 if ($entry_timestamp == "") {
191 $entry_timestamp = time();
192 $no_orig_date = 'true';
194 $no_orig_date = 'false';
197 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
199 $entry_title = $item["title"];
200 $entry_link = $item["link"];
202 if (!$entry_title) continue;
203 if (!$entry_link) continue;
205 $entry_content = $item["content:escaped"];
207 if (!$entry_content) $entry_content = $item["content:encoded"];
208 if (!$entry_content) $entry_content = $item["content"];
209 if (!$entry_content) $entry_content = $item["description"];
211 // if (!$entry_content) continue;
214 if (is_array($entry_content)) {
215 $entry_content = $entry_content["encoded"];
216 if (!$entry_content) $entry_content = $entry_content["escaped"];
220 // print_r($entry_content);
222 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
224 $entry_comments = $item["comments"];
226 $entry_guid = db_escape_string($entry_guid);
228 $result = db_query($link, "
230 id,last_read,no_orig_date,title,feed_id,content_hash,
231 substring(updated,1,19) as updated
235 guid = '$entry_guid' AND owner_uid = " . $_SESSION["uid"]);
237 // print db_num_rows($result) . "$entry_guid<br/>";
239 if (db_num_rows($result) == 0) {
242 if (is_filtered($entry_title, $entry_content, $filters)) {
245 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
247 //$entry_guid = db_escape_string($entry_guid);
248 $entry_content = db_escape_string($entry_content);
249 $entry_title = db_escape_string($entry_title);
250 $entry_link = db_escape_string($entry_link);
251 $entry_comments = db_escape_string($entry_comments);
270 '$entry_timestamp_fmt',
276 NOW(),".$_SESSION["uid"].")";
278 $result = db_query($link, $query);
282 $orig_entry_id = db_fetch_result($result, 0, "id");
283 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
285 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
287 if ($orig_feed_id != $feed) {
288 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
292 $entry_is_modified = false;
294 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
296 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
297 $orig_last_read = db_fetch_result($result, 0, "last_read");
298 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
299 $orig_title = db_fetch_result($result, 0, "title");
301 $last_read_qpart = "";
303 if ($orig_content_hash != $content_hash) {
304 // print "$orig_content_hash :: $content_hash<br>";
306 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
307 $last_read_qpart = 'last_read = null,';
309 $entry_is_modified = true;
312 if ($orig_title != $entry_title) {
313 $entry_is_modified = true;
316 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
317 $entry_is_modified = true;
320 if ($entry_is_modified) {
322 // print "$entry_guid Modified!<br>";
324 $entry_comments = db_escape_string($entry_comments);
325 $entry_content = db_escape_string($entry_content);
326 $entry_title = db_escape_string($entry_title);
327 $entry_link = db_escape_string($entry_link);
329 $query = "UPDATE ttrss_entries
332 title = '$entry_title',
333 link = '$entry_link',
334 updated = '$entry_timestamp_fmt',
335 content = '$entry_content',
336 comments = '$entry_comments',
337 content_hash = '$content_hash'
339 id = '$orig_entry_id'";
341 $result = db_query($link, $query);
346 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
350 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
353 $entry_tags = $entry_tags[1];
355 if (count($entry_tags) > 0) {
357 $result = db_query($link, "SELECT id FROM ttrss_entries
358 WHERE guid = '$entry_guid' AND owner_uid = " . $_SESSION["uid"]);
360 if (!$result ||
db_num_rows($result) != 1) {
364 $entry_id = db_fetch_result($result, 0, "id");
366 foreach ($entry_tags as $tag) {
367 $tag = db_escape_string(strtolower($tag));
369 $result = db_query($link, "SELECT id FROM ttrss_tags
370 WHERE tag_name = '$tag' AND post_id = '$entry_id' AND
371 owner_uid = ".$_SESSION["uid"]." LIMIT 1");
373 // print db_fetch_result($result, 0, "id");
375 if ($result && db_num_rows($result) == 0) {
377 // print "tagging $entry_id as $tag<br>";
379 db_query($link, "INSERT INTO ttrss_tags (owner_uid,tag_name,post_id)
380 VALUES ('".$_SESSION["uid"]."','$tag', '$entry_id')");
386 db_query($link, "UPDATE ttrss_feeds
387 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
390 $error_msg = db_escape_string(magpie_error());
392 "UPDATE ttrss_feeds SET last_error = '$error_msg',
393 last_updated = NOW() WHERE id = '$feed'");
396 db_query($link, "COMMIT");
400 function print_select($id, $default, $values, $attributes = "") {
401 print "<select id=\"$id\" $attributes>";
402 foreach ($values as $v) {
408 print "<option$sel>$v</option>";
413 function is_filtered($title, $content, $filters) {
415 if ($filters["title"]) {
416 foreach ($filters["title"] as $title_filter) {
417 if (preg_match("/$title_filter/i", $title))
422 if ($filters["content"]) {
423 foreach ($filters["content"] as $content_filter) {
424 if (preg_match("/$content_filter/i", $content))
429 if ($filters["both"]) {
430 foreach ($filters["both"] as $filter) {
431 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
439 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
441 if (file_exists($icon_file) && filesize($icon_file) > 0) {
442 $feed_icon = "<img src=\"$icon_file\">";
444 $feed_icon = "<img src=\"images/blank_icon.gif\">";
447 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
449 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
450 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
454 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
459 $fctr_class = "class=\"invisible\"";
462 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
463 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
469 function getmicrotime() {
470 list($usec, $sec) = explode(" ",microtime());
471 return ((float)$usec +
(float)$sec);
474 function print_radio($id, $default, $values, $attributes = "") {
475 foreach ($values as $v) {
483 $sel .= " value=\"1\"";
485 $sel .= " value=\"0\"";
488 print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v ";
493 function initialize_user_prefs($link, $uid) {
495 $uid = db_escape_string($uid);
497 db_query($link, "BEGIN");
499 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
501 $u_result = db_query($link, "SELECT pref_name
502 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
504 $active_prefs = array();
506 while ($line = db_fetch_assoc($u_result)) {
507 array_push($active_prefs, $line["pref_name"]);
510 while ($line = db_fetch_assoc($result)) {
511 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
512 // print "adding " . $line["pref_name"] . "<br>";
514 db_query($link, "INSERT INTO ttrss_user_prefs
515 (owner_uid,pref_name,value) VALUES
516 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
521 db_query($link, "COMMIT");
525 function authenticate_user($link, $login, $password) {
527 $pwd_hash = 'SHA1:' . sha1($password);
529 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
530 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
532 if (db_num_rows($result) == 1) {
533 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
534 $_SESSION["name"] = db_fetch_result($result, 0, "login");
535 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
537 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
547 function http_authenticate_user($link) {
549 if (!$_SERVER['PHP_AUTH_USER']) {
551 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
552 header('HTTP/1.0 401 Unauthorized');
553 print "<h1>401 Unathorized</h1>";
558 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
559 $password = db_escape_string($_SERVER['PHP_AUTH_PW']);
561 return authenticate_user($link, $login, $password);
565 function make_password($length = 8) {
568 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
572 while ($i < $length) {
573 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
575 if (!strstr($password, $char)) {
583 // this is called after user is created to initialize default feeds, labels
586 // user preferences are checked on every login, not here
588 function initialize_user($link, $uid) {
590 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
591 values ('$uid','unread = true', 'Unread articles')");
593 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
594 values ('$uid','last_read is null and unread = false', 'Updated articles')");
596 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
597 values ('$uid', 'Tiny Tiny RSS Dev. Feed',
598 'http://bah.spb.su/darcsweb/darcsweb.cgi?r=tt-rss;a=rss')");