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