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