]> git.wh0rd.org - tt-rss.git/blob - functions.php
initial split ttrss_entries work
[tt-rss.git] / functions.php
1 <?
2 session_start();
3
4 require_once 'config.php';
5 require_once 'db-prefs.php';
6
7 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
8
9 function purge_feed($link, $feed_id, $purge_interval) {
10
11 return; // FIXME disabled for now
12
13 if (DB_TYPE == "pgsql") {
14 db_query($link, "DELETE FROM ttrss_entries WHERE
15 marked = false AND feed_id = '$feed_id' AND
16 date_entered < NOW() - INTERVAL '$purge_interval days'");
17 } else {
18 db_query($link, "DELETE FROM ttrss_entries WHERE
19 marked = false AND feed_id = '$feed_id' AND
20 date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
21 }
22 }
23
24 function global_purge_old_posts($link, $do_output = false) {
25
26 $result = db_query($link,
27 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds");
28
29 while ($line = db_fetch_assoc($result)) {
30
31 $feed_id = $line["id"];
32 $purge_interval = $line["purge_interval"];
33 $owner_uid = $line["owner_uid"];
34
35 if ($purge_interval == 0) {
36
37 $tmp_result = db_query($link,
38 "SELECT value FROM ttrss_user_prefs WHERE
39 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
40
41 if (db_num_rows($tmp_result) != 0) {
42 $purge_interval = db_fetch_result($tmp_result, 0, "value");
43 }
44 }
45
46 if ($do_output) {
47 print "<feed id='$feed_id' p_intl='$purge_interval'/>";
48 }
49
50 if ($purge_interval > 0) {
51 purge_feed($link, $feed_id, $purge_interval);
52 }
53 }
54
55 }
56
57 function purge_old_posts($link) {
58
59 $user_id = $_SESSION["uid"];
60
61 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
62 WHERE owner_uid = '$user_id'");
63
64 while ($line = db_fetch_assoc($result)) {
65
66 $feed_id = $line["id"];
67 $purge_interval = $line["purge_interval"];
68
69 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
70
71 if ($purge_interval > 0) {
72 purge_feed($link, $feed_id, $purge_interval);
73 }
74 }
75 }
76
77 function update_all_feeds($link, $fetch) {
78
79 if (WEB_DEMO_MODE) return;
80
81 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
82 if (!$_GET["daemon"]) {
83 return;
84 }
85 }
86
87 db_query($link, "BEGIN");
88
89 $user_id = $_SESSION["uid"];
90
91 $result = db_query($link, "SELECT feed_url,id,
92 substring(last_updated,1,19) as last_updated,
93 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
94
95 while ($line = db_fetch_assoc($result)) {
96 $upd_intl = $line["update_interval"];
97
98 if (!$upd_intl || $upd_intl == 0) {
99 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
100 }
101
102 if ($fetch || (!$line["last_updated"] ||
103 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
104
105 update_rss_feed($link, $line["feed_url"], $line["id"]);
106 }
107 }
108
109 purge_old_posts($link);
110
111 db_query($link, "COMMIT");
112
113 }
114
115 function check_feed_favicon($feed_url, $feed, $link) {
116 $feed_url = str_replace("http://", "", $feed_url);
117 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
118
119 $icon_url = "http://$feed_url/favicon.ico";
120 $icon_file = ICONS_DIR . "/$feed.ico";
121
122 if (!file_exists($icon_file)) {
123
124 error_reporting(0);
125 $r = fopen($icon_url, "r");
126 error_reporting (E_ERROR | E_WARNING | E_PARSE);
127
128 if ($r) {
129 $tmpfname = tempnam("/tmp", "ttrssicon");
130
131 $t = fopen($tmpfname, "w");
132
133 while (!feof($r)) {
134 $buf = fread($r, 16384);
135 fwrite($t, $buf);
136 }
137
138 fclose($r);
139 fclose($t);
140
141 error_reporting(0);
142 if (!rename($tmpfname, $icon_file)) {
143 unlink($tmpfname);
144 }
145
146 chmod($icon_file, 0644);
147
148 error_reporting (E_ERROR | E_WARNING | E_PARSE);
149
150 }
151 }
152 }
153
154 function update_rss_feed($link, $feed_url, $feed) {
155
156 if (WEB_DEMO_MODE) return;
157
158 $feed = db_escape_string($feed);
159
160 error_reporting(0);
161 $rss = fetch_rss($feed_url);
162
163 error_reporting (E_ERROR | E_WARNING | E_PARSE);
164
165 db_query($link, "BEGIN");
166
167 $feed = db_escape_string($feed);
168
169 if ($rss) {
170
171 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
172 check_feed_favicon($feed_url, $feed, $link);
173 }
174
175 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
176
177 $registered_title = db_fetch_result($result, 0, "title");
178 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
179
180 if (!$registered_title) {
181 $feed_title = db_escape_string($rss->channel["title"]);
182 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
183 }
184
185 // print "I: " . $rss->channel["image"]["url"];
186
187 $icon_url = $rss->image["url"];
188
189 if ($icon_url && !$orig_icon_url) {
190 $icon_url = db_escape_string($icon_url);
191 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
192 }
193
194
195 $filters = array();
196
197 $result = db_query($link, "SELECT reg_exp,
198 (SELECT name FROM ttrss_filter_types
199 WHERE id = filter_type) as name
200 FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]);
201
202 while ($line = db_fetch_assoc($result)) {
203 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
204 array_push($filters[$line["name"]], $line["reg_exp"]);
205 }
206
207 foreach ($rss->items as $item) {
208
209 $entry_guid = $item["id"];
210
211 if (!$entry_guid) $entry_guid = $item["guid"];
212 if (!$entry_guid) $entry_guid = $item["link"];
213
214 if (!$entry_guid) continue;
215
216 $entry_timestamp = "";
217
218 $rss_2_date = $item['pubdate'];
219 $rss_1_date = $item['dc']['date'];
220 $atom_date = $item['issued'];
221
222 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
223 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
224 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
225
226 if ($entry_timestamp == "") {
227 $entry_timestamp = time();
228 $no_orig_date = 'true';
229 } else {
230 $no_orig_date = 'false';
231 }
232
233 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
234
235 $entry_title = $item["title"];
236 $entry_link = $item["link"];
237
238 if (!$entry_title) continue;
239 if (!$entry_link) continue;
240
241 $entry_content = $item["content:escaped"];
242
243 if (!$entry_content) $entry_content = $item["content:encoded"];
244 if (!$entry_content) $entry_content = $item["content"];
245 if (!$entry_content) $entry_content = $item["description"];
246
247 // if (!$entry_content) continue;
248
249 // WTF
250 if (is_array($entry_content)) {
251 $entry_content = $entry_content["encoded"];
252 if (!$entry_content) $entry_content = $entry_content["escaped"];
253 }
254
255 // print_r($item);
256 // print_r($entry_content);
257
258 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
259
260 $entry_comments = $item["comments"];
261
262 $entry_guid = db_escape_string($entry_guid);
263
264 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE guid = '$entry_guid'");
265
266 $owner_uid = $_SESSION["uid"];
267
268 if (db_num_rows($result) == 0) {
269
270 // base post entry does not exist, create it
271
272 error_reporting(0);
273 if (is_filtered($entry_title, $entry_content, $filters)) {
274 continue;
275 }
276 error_reporting (E_ERROR | E_WARNING | E_PARSE);
277
278 $entry_content = db_escape_string($entry_content);
279 $entry_title = db_escape_string($entry_title);
280 $entry_link = db_escape_string($entry_link);
281 $entry_comments = db_escape_string($entry_comments);
282
283 $result = db_query($link,
284 "INSERT INTO ttrss_entries
285 (title,
286 guid,
287 link,
288 updated,
289 content,
290 content_hash,
291 no_orig_date,
292 date_entered,
293 comments)
294 VALUES
295 ('$entry_title',
296 '$entry_guid',
297 '$entry_link',
298 '$entry_timestamp_fmt',
299 '$entry_content',
300 '$content_hash',
301 $no_orig_date,
302 NOW(),
303 '$entry_comments')");
304 }
305
306 // now it should exist, if not - bad luck then
307
308 $result = db_query($link, "SELECT id FROM ttrss_entries WHERE guid = '$entry_guid'");
309
310 if (db_num_rows($result) == 1) {
311
312 $ref_id = db_fetch_result($result, 0, "id");
313
314 // check for user post link to main table
315
316 $result = db_query($link,
317 "SELECT ref_id FROM ttrss_user_entries WHERE
318 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND feed_id = '$feed'");
319
320 // okay it doesn't exist - create user entry
321
322 if (db_num_rows($result) == 0) {
323
324 $result = db_query($link,
325 "INSERT INTO ttrss_user_entries
326 (ref_id, owner_uid, feed_id)
327 VALUES ('$ref_id', '$owner_uid', '$feed')");
328
329 }
330 }
331
332 /* $result = db_query($link, "
333 SELECT
334 id,last_read,no_orig_date,title,feed_id,content_hash,
335 substring(updated,1,19) as updated
336 FROM
337 ttrss_entries
338 WHERE
339 guid = '$entry_guid' AND
340 owner_uid = " . $_SESSION["uid"]." AND
341 feed_id = '$feed'");
342
343 if (db_num_rows($result) == 0) {
344
345 error_reporting(0);
346 if (is_filtered($entry_title, $entry_content, $filters)) {
347 continue;
348 }
349 error_reporting (E_ERROR | E_WARNING | E_PARSE);
350
351 //$entry_guid = db_escape_string($entry_guid);
352 $entry_content = db_escape_string($entry_content);
353 $entry_title = db_escape_string($entry_title);
354 $entry_link = db_escape_string($entry_link);
355 $entry_comments = db_escape_string($entry_comments);
356
357 $query = "INSERT
358 INTO ttrss_entries
359 (title,
360 guid,
361 link,
362 updated,
363 content,
364 content_hash,
365 feed_id,
366 comments,
367 no_orig_date,
368 date_entered,
369 owner_uid)
370 VALUES
371 ('$entry_title',
372 '$entry_guid',
373 '$entry_link',
374 '$entry_timestamp_fmt',
375 '$entry_content',
376 '$content_hash',
377 '$feed',
378 '$entry_comments',
379 $no_orig_date,
380 NOW(),".$_SESSION["uid"].")";
381
382 $result = db_query($link, $query);
383
384 } else {
385
386 $orig_entry_id = db_fetch_result($result, 0, "id");
387 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
388
389 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
390
391 if ($orig_feed_id != $feed) {
392 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
393 continue;
394 }
395
396 $entry_is_modified = false;
397
398 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
399
400 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
401 $orig_last_read = db_fetch_result($result, 0, "last_read");
402 $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
403 $orig_title = db_fetch_result($result, 0, "title");
404
405 $last_read_qpart = "";
406
407 if ($orig_content_hash != $content_hash) {
408 // print "$orig_content_hash :: $content_hash<br>";
409
410 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
411 $last_read_qpart = 'last_read = null,';
412 }
413 $entry_is_modified = true;
414 }
415
416 if ($orig_title != $entry_title) {
417 $entry_is_modified = true;
418 }
419
420 if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
421 $entry_is_modified = true;
422 }
423
424 if ($entry_is_modified) {
425
426 // print "$entry_guid Modified!<br>";
427
428 $entry_comments = db_escape_string($entry_comments);
429 $entry_content = db_escape_string($entry_content);
430 $entry_title = db_escape_string($entry_title);
431 $entry_link = db_escape_string($entry_link);
432
433 $query = "UPDATE ttrss_entries
434 SET
435 $last_read_qpart
436 title = '$entry_title',
437 link = '$entry_link',
438 updated = '$entry_timestamp_fmt',
439 content = '$entry_content',
440 comments = '$entry_comments',
441 content_hash = '$content_hash'
442 WHERE
443 id = '$orig_entry_id'";
444
445 $result = db_query($link, $query);
446 }
447 } */
448
449 /* taaaags */
450 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
451
452 /* $entry_tags = null;
453
454 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content,
455 $entry_tags);
456
457 $entry_tags = $entry_tags[1];
458
459 if (count($entry_tags) > 0) {
460
461 $result = db_query($link, "SELECT id FROM ttrss_entries
462 WHERE guid = '$entry_guid'
463 AND feed_id = '$feed'
464 AND owner_uid = " . $_SESSION["uid"]);
465
466 if (!$result || db_num_rows($result) != 1) {
467 return;
468 }
469
470 $entry_id = db_fetch_result($result, 0, "id");
471
472 foreach ($entry_tags as $tag) {
473 $tag = db_escape_string(strtolower($tag));
474
475 $tag = str_replace("technorati tag: ", "", $tag);
476
477 $result = db_query($link, "SELECT id FROM ttrss_tags
478 WHERE tag_name = '$tag' AND post_id = '$entry_id' AND
479 owner_uid = ".$_SESSION["uid"]." LIMIT 1");
480
481 // print db_fetch_result($result, 0, "id");
482
483 if ($result && db_num_rows($result) == 0) {
484
485 // print "tagging $entry_id as $tag<br>";
486
487 db_query($link, "INSERT INTO ttrss_tags (owner_uid,tag_name,post_id)
488 VALUES ('".$_SESSION["uid"]."','$tag', '$entry_id')");
489 }
490 }
491 } */
492 }
493
494 db_query($link, "UPDATE ttrss_feeds
495 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
496
497 } else {
498 $error_msg = db_escape_string(magpie_error());
499 db_query($link,
500 "UPDATE ttrss_feeds SET last_error = '$error_msg',
501 last_updated = NOW() WHERE id = '$feed'");
502 }
503
504 db_query($link, "COMMIT");
505
506 }
507
508 function print_select($id, $default, $values, $attributes = "") {
509 print "<select id=\"$id\" $attributes>";
510 foreach ($values as $v) {
511 if ($v == $default)
512 $sel = " selected";
513 else
514 $sel = "";
515
516 print "<option$sel>$v</option>";
517 }
518 print "</select>";
519 }
520
521 function is_filtered($title, $content, $filters) {
522
523 if ($filters["title"]) {
524 foreach ($filters["title"] as $title_filter) {
525 if (preg_match("/$title_filter/i", $title))
526 return true;
527 }
528 }
529
530 if ($filters["content"]) {
531 foreach ($filters["content"] as $content_filter) {
532 if (preg_match("/$content_filter/i", $content))
533 return true;
534 }
535 }
536
537 if ($filters["both"]) {
538 foreach ($filters["both"] as $filter) {
539 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
540 return true;
541 }
542 }
543
544 return false;
545 }
546
547 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
548
549 if (file_exists($icon_file) && filesize($icon_file) > 0) {
550 $feed_icon = "<img src=\"$icon_file\">";
551 } else {
552 $feed_icon = "<img src=\"images/blank_icon.gif\">";
553 }
554
555 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
556
557 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
558 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
559 print "$feed_icon";
560 }
561
562 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
563
564 if ($unread != 0) {
565 $fctr_class = "";
566 } else {
567 $fctr_class = "class=\"invisible\"";
568 }
569
570 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
571 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
572
573 print "</li>";
574
575 }
576
577 function getmicrotime() {
578 list($usec, $sec) = explode(" ",microtime());
579 return ((float)$usec + (float)$sec);
580 }
581
582 function print_radio($id, $default, $values, $attributes = "") {
583 foreach ($values as $v) {
584
585 if ($v == $default)
586 $sel = "checked";
587 else
588 $sel = "";
589
590 if ($v == "Yes") {
591 $sel .= " value=\"1\"";
592 } else {
593 $sel .= " value=\"0\"";
594 }
595
596 print "<input type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
597
598 }
599 }
600
601 function initialize_user_prefs($link, $uid) {
602
603 $uid = db_escape_string($uid);
604
605 db_query($link, "BEGIN");
606
607 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
608
609 $u_result = db_query($link, "SELECT pref_name
610 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
611
612 $active_prefs = array();
613
614 while ($line = db_fetch_assoc($u_result)) {
615 array_push($active_prefs, $line["pref_name"]);
616 }
617
618 while ($line = db_fetch_assoc($result)) {
619 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
620 // print "adding " . $line["pref_name"] . "<br>";
621
622 db_query($link, "INSERT INTO ttrss_user_prefs
623 (owner_uid,pref_name,value) VALUES
624 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
625
626 }
627 }
628
629 db_query($link, "COMMIT");
630
631 }
632
633 function authenticate_user($link, $login, $password) {
634
635 $pwd_hash = 'SHA1:' . sha1($password);
636
637 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
638 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
639
640 if (db_num_rows($result) == 1) {
641 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
642 $_SESSION["name"] = db_fetch_result($result, 0, "login");
643 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
644
645 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
646 $_SESSION["uid"]);
647
648 return true;
649 }
650
651 return false;
652
653 }
654
655 function http_authenticate_user($link) {
656
657 if (!$_SERVER['PHP_AUTH_USER']) {
658
659 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
660 header('HTTP/1.0 401 Unauthorized');
661 print "<h1>401 Unathorized</h1>";
662 exit;
663
664 } else {
665
666 $login = db_escape_string($_SERVER['PHP_AUTH_USER']);
667 $password = db_escape_string($_SERVER['PHP_AUTH_PW']);
668
669 return authenticate_user($link, $login, $password);
670 }
671 }
672
673 function make_password($length = 8) {
674
675 $password = "";
676 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
677
678 $i = 0;
679
680 while ($i < $length) {
681 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
682
683 if (!strstr($password, $char)) {
684 $password .= $char;
685 $i++;
686 }
687 }
688 return $password;
689 }
690
691 // this is called after user is created to initialize default feeds, labels
692 // or whatever else
693
694 // user preferences are checked on every login, not here
695
696 function initialize_user($link, $uid) {
697
698 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
699 values ('$uid','unread = true', 'Unread articles')");
700
701 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
702 values ('$uid','last_read is null and unread = false', 'Updated articles')");
703
704 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
705 values ('$uid', 'Tiny Tiny RSS Dev. Feed',
706 'http://bah.spb.su/darcsweb/darcsweb.cgi?r=tt-rss;a=rss')");
707
708 }
709
710 ?>