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)) {
109 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
115 function update_rss_feed($link, $feed_url, $feed) {
117 if (WEB_DEMO_MODE
) return;
119 $feed = db_escape_string($feed);
122 $rss = fetch_rss($feed_url);
124 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
126 db_query($link, "BEGIN");
128 $feed = db_escape_string($feed);
132 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
133 check_feed_favicon($feed_url, $feed, $link);
136 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
138 $registered_title = db_fetch_result($result, 0, "title");
139 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
141 if (!$registered_title) {
142 $feed_title = db_escape_string($rss->channel
["title"]);
143 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
146 // print "I: " . $rss->channel["image"]["url"];
148 $icon_url = $rss->image
["url"];
150 if ($icon_url && !$orig_icon_url) {
151 $icon_url = db_escape_string($icon_url);
152 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
158 $result = db_query($link, "SELECT reg_exp,
159 (SELECT name FROM ttrss_filter_types
160 WHERE id = filter_type) as name
161 FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]);
163 while ($line = db_fetch_assoc($result)) {
164 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
165 array_push($filters[$line["name"]], $line["reg_exp"]);
168 foreach ($rss->items
as $item) {
170 $entry_guid = $item["id"];
172 if (!$entry_guid) $entry_guid = $item["guid"];
173 if (!$entry_guid) $entry_guid = $item["link"];
175 if (!$entry_guid) continue;
177 $entry_timestamp = "";
179 $rss_2_date = $item['pubdate'];
180 $rss_1_date = $item['dc']['date'];
181 $atom_date = $item['issued'];
183 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
184 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
185 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
187 if ($entry_timestamp == "") {
188 $entry_timestamp = time();
189 $no_orig_date = 'true';
191 $no_orig_date = 'false';
194 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
196 $entry_title = $item["title"];
197 $entry_link = $item["link"];
199 if (!$entry_title) continue;
200 if (!$entry_link) continue;
202 $entry_content = $item["content:escaped"];
204 if (!$entry_content) $entry_content = $item["content:encoded"];
205 if (!$entry_content) $entry_content = $item["content"];
206 if (!$entry_content) $entry_content = $item["description"];
208 // if (!$entry_content) continue;
211 if (is_array($entry_content)) {
212 $entry_content = $entry_content["encoded"];
213 if (!$entry_content) $entry_content = $entry_content["escaped"];
217 // print_r($entry_content);
219 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
221 $entry_comments = $item["comments"];
223 $entry_guid = db_escape_string($entry_guid);
225 $result = db_query($link, "
227 id,last_read,no_orig_date,title,feed_id,content_hash,
228 substring(updated,1,19) as updated
232 guid = '$entry_guid' AND owner_uid = " . $_SESSION["uid"]);
234 // print db_num_rows($result) . "$entry_guid<br/>";
236 if (db_num_rows($result) == 0) {
239 if (is_filtered($entry_title, $entry_content, $filters)) {
242 error_reporting (E_ERROR | E_WARNING | E_PARSE
);
244 //$entry_guid = db_escape_string($entry_guid);
245 $entry_content = db_escape_string($entry_content);
246 $entry_title = db_escape_string($entry_title);
247 $entry_link = db_escape_string($entry_link);
248 $entry_comments = db_escape_string($entry_comments);
267 '$entry_timestamp_fmt',
273 NOW(),".$_SESSION["uid"].")";
275 $result = db_query($link, $query);
279 $orig_entry_id = db_fetch_result($result, 0, "id");
280 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
282 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
284 if ($orig_feed_id != $feed) {
285 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
289 $entry_is_modified = false;
291 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
293 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
294 $orig_last_read = db_fetch_result($result, 0, "last_read");
295 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
296 $orig_title = db_fetch_result($result, 0, "title");
298 $last_read_qpart = "";
300 if ($orig_content_hash != $content_hash) {
301 // print "$orig_content_hash :: $content_hash<br>";
303 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
304 $last_read_qpart = 'last_read = null,';
306 $entry_is_modified = true;
309 if ($orig_title != $entry_title) {
310 $entry_is_modified = true;
313 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
314 $entry_is_modified = true;
317 if ($entry_is_modified) {
319 // print "$entry_guid Modified!<br>";
321 $entry_comments = db_escape_string($entry_comments);
322 $entry_content = db_escape_string($entry_content);
323 $entry_title = db_escape_string($entry_title);
324 $entry_link = db_escape_string($entry_link);
326 $query = "UPDATE ttrss_entries
329 title = '$entry_title',
330 link = '$entry_link',
331 updated = '$entry_timestamp_fmt',
332 content = '$entry_content',
333 comments = '$entry_comments',
334 content_hash = '$content_hash'
336 id = '$orig_entry_id'";
338 $result = db_query($link, $query);
343 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
347 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
350 $entry_tags = $entry_tags[1];
352 if (count($entry_tags) > 0) {
354 $result = db_query($link, "SELECT id FROM ttrss_entries
355 WHERE guid = '$entry_guid'");
357 if (!$result ||
db_num_rows($result) != 1) {
361 $entry_id = db_fetch_result($result, 0, "id");
363 foreach ($entry_tags as $tag) {
364 $tag = db_escape_string(strtolower($tag));
366 $result = db_query($link, "SELECT id FROM ttrss_tags
367 WHERE tag_name = '$tag' AND post_id = '$entry_id' AND owner_uid = ".$_SESSION["uid"]." LIMIT 1");
369 if ($result && db_num_rows($result) == 0) {
371 // print "tagging $entry_id as $tag<br>";
373 db_query($link, "INSERT INTO ttrss_tags (owner_uid,tag_name,post_id)
374 VALUES ('".$_SESSION["uid"]."','$tag', '$entry_id')");
380 db_query($link, "UPDATE ttrss_feeds
381 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
384 $error_msg = db_escape_string(magpie_error());
386 "UPDATE ttrss_feeds SET last_error = '$error_msg',
387 last_updated = NOW() WHERE id = '$feed'");
390 db_query($link, "COMMIT");
394 function print_select($id, $default, $values, $attributes = "") {
395 print "<select id=\"$id\" $attributes>";
396 foreach ($values as $v) {
402 print "<option$sel>$v</option>";
407 function is_filtered($title, $content, $filters) {
409 if ($filters["title"]) {
410 foreach ($filters["title"] as $title_filter) {
411 if (preg_match("/$title_filter/i", $title))
416 if ($filters["content"]) {
417 foreach ($filters["content"] as $content_filter) {
418 if (preg_match("/$content_filter/i", $content))
423 if ($filters["both"]) {
424 foreach ($filters["both"] as $filter) {
425 if (preg_match("/$filter/i", $title) ||
preg_match("/$filter/i", $content))
433 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
435 if (file_exists($icon_file) && filesize($icon_file) > 0) {
436 $feed_icon = "<img src=\"$icon_file\">";
438 $feed_icon = "<img src=\"images/blank_icon.gif\">";
441 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
443 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
444 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
448 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
453 $fctr_class = "class=\"invisible\"";
456 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
457 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
463 function getmicrotime() {
464 list($usec, $sec) = explode(" ",microtime());
465 return ((float)$usec +
(float)$sec);
468 function print_radio($id, $default, $values, $attributes = "") {
469 foreach ($values as $v) {
477 $sel .= " value=\"1\"";
479 $sel .= " value=\"0\"";
482 print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v ";
487 function initialize_user_prefs($link, $uid) {
489 $uid = db_escape_string($uid);
491 db_query($link, "BEGIN");
493 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
495 $u_result = db_query($link, "SELECT pref_name
496 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
498 $active_prefs = array();
500 while ($line = db_fetch_assoc($u_result)) {
501 array_push($active_prefs, $line["pref_name"]);
504 while ($line = db_fetch_assoc($result)) {
505 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
506 // print "adding " . $line["pref_name"] . "<br>";
508 db_query($link, "INSERT INTO ttrss_user_prefs
509 (owner_uid,pref_name,value) VALUES
510 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
515 db_query($link, "COMMIT");
519 function authenticate_user($link, $login, $password) {
521 $pwd_hash = 'SHA1:' . sha1($password);
523 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
524 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
526 if (db_num_rows($result) == 1) {
527 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
528 $_SESSION["name"] = db_fetch_result($result, 0, "login");
529 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
538 function http_authenticate_user($link) {
540 if (!$_SERVER['PHP_AUTH_USER']) {
542 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
543 header('HTTP/1.0 401 Unauthorized');
544 print "<h1>401 Unathorized</h1>";
549 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
550 $password = db_escape_string($_SERVER['PHP_AUTH_PW']);
552 return authenticate_user($link, $login, $password);