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