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