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