]> git.wh0rd.org - tt-rss.git/blob - functions.php
user details for user manager
[tt-rss.git] / functions.php
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_old_posts($link) {
13
14 $user_id = $_SESSION["uid"];
15
16 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
17 WHERE owner_uid = '$user_id'");
18
19 while ($line = db_fetch_assoc($result)) {
20
21 $feed_id = $line["id"];
22 $purge_interval = $line["purge_interval"];
23
24 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
25
26 if ($purge_interval > 0) {
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
34 marked = false AND feed_id = '$feed_id' AND
35 date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
36 }
37 }
38 }
39 }
40
41 function update_all_feeds($link, $fetch) {
42
43 if (WEB_DEMO_MODE) return;
44
45 if (get_pref($link, 'DAEMON_REFRESH_ONLY')) {
46 if (!$_GET["daemon"]) {
47 return;
48 }
49 }
50
51 db_query($link, "BEGIN");
52
53 $user_id = $_SESSION["uid"];
54
55 $result = db_query($link, "SELECT feed_url,id,
56 substring(last_updated,1,19) as last_updated,
57 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
58
59 while ($line = db_fetch_assoc($result)) {
60 $upd_intl = $line["update_interval"];
61
62 if (!$upd_intl || $upd_intl == 0) {
63 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL');
64 }
65
66 if (!$line["last_updated"] ||
67 time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) {
68
69 update_rss_feed($link, $line["feed_url"], $line["id"]);
70 }
71 }
72
73 purge_old_posts($link);
74
75 db_query($link, "COMMIT");
76
77 }
78
79 function check_feed_favicon($feed_url, $feed, $link) {
80 $feed_url = str_replace("http://", "", $feed_url);
81 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
82
83 $icon_url = "http://$feed_url/favicon.ico";
84 $icon_file = ICONS_DIR . "/$feed.ico";
85
86 if (!file_exists($icon_file)) {
87
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
105 error_reporting(0);
106 if (!rename($tmpfname, $icon_file)) {
107 unlink($tmpfname);
108 }
109
110 chmod($icon_file, 0644);
111
112 error_reporting (E_ERROR | E_WARNING | E_PARSE);
113
114 }
115 }
116 }
117
118 function update_rss_feed($link, $feed_url, $feed) {
119
120 if (WEB_DEMO_MODE) return;
121
122 $feed = db_escape_string($feed);
123
124 error_reporting(0);
125 $rss = fetch_rss($feed_url);
126
127 error_reporting (E_ERROR | E_WARNING | E_PARSE);
128
129 db_query($link, "BEGIN");
130
131 $feed = db_escape_string($feed);
132
133 if ($rss) {
134
135 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
136 check_feed_favicon($feed_url, $feed, $link);
137 }
138
139 $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'");
140
141 $registered_title = db_fetch_result($result, 0, "title");
142 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
143
144 if (!$registered_title) {
145 $feed_title = db_escape_string($rss->channel["title"]);
146 db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
147 }
148
149 // print "I: " . $rss->channel["image"]["url"];
150
151 $icon_url = $rss->image["url"];
152
153 if ($icon_url && !$orig_icon_url) {
154 $icon_url = db_escape_string($icon_url);
155 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
156 }
157
158
159 $filters = array();
160
161 $result = db_query($link, "SELECT reg_exp,
162 (SELECT name FROM ttrss_filter_types
163 WHERE id = filter_type) as name
164 FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]);
165
166 while ($line = db_fetch_assoc($result)) {
167 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
168 array_push($filters[$line["name"]], $line["reg_exp"]);
169 }
170
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"];
177
178 if (!$entry_guid) continue;
179
180 $entry_timestamp = "";
181
182 $rss_2_date = $item['pubdate'];
183 $rss_1_date = $item['dc']['date'];
184 $atom_date = $item['issued'];
185
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);
189
190 if ($entry_timestamp == "") {
191 $entry_timestamp = time();
192 $no_orig_date = 'true';
193 } else {
194 $no_orig_date = 'false';
195 }
196
197 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
198
199 $entry_title = $item["title"];
200 $entry_link = $item["link"];
201
202 if (!$entry_title) continue;
203 if (!$entry_link) continue;
204
205 $entry_content = $item["content:escaped"];
206
207 if (!$entry_content) $entry_content = $item["content:encoded"];
208 if (!$entry_content) $entry_content = $item["content"];
209 if (!$entry_content) $entry_content = $item["description"];
210
211 // if (!$entry_content) continue;
212
213 // WTF
214 if (is_array($entry_content)) {
215 $entry_content = $entry_content["encoded"];
216 if (!$entry_content) $entry_content = $entry_content["escaped"];
217 }
218
219 // print_r($item);
220 // print_r($entry_content);
221
222 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
223
224 $entry_comments = $item["comments"];
225
226 $entry_guid = db_escape_string($entry_guid);
227
228 $result = db_query($link, "
229 SELECT
230 id,last_read,no_orig_date,title,feed_id,content_hash,
231 substring(updated,1,19) as updated
232 FROM
233 ttrss_entries
234 WHERE
235 guid = '$entry_guid' AND owner_uid = " . $_SESSION["uid"]);
236
237 // print db_num_rows($result) . "$entry_guid<br/>";
238
239 if (db_num_rows($result) == 0) {
240
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
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);
252
253 $query = "INSERT
254 INTO ttrss_entries
255 (title,
256 guid,
257 link,
258 updated,
259 content,
260 content_hash,
261 feed_id,
262 comments,
263 no_orig_date,
264 date_entered,
265 owner_uid)
266 VALUES
267 ('$entry_title',
268 '$entry_guid',
269 '$entry_link',
270 '$entry_timestamp_fmt',
271 '$entry_content',
272 '$content_hash',
273 '$feed',
274 '$entry_comments',
275 $no_orig_date,
276 NOW(),".$_SESSION["uid"].")";
277
278 $result = db_query($link, $query);
279
280 } else {
281
282 $orig_entry_id = db_fetch_result($result, 0, "id");
283 $orig_feed_id = db_fetch_result($result, 0, "feed_id");
284
285 // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>";
286
287 if ($orig_feed_id != $feed) {
288 // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
289 continue;
290 }
291
292 $entry_is_modified = false;
293
294 $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
295
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");
300
301 $last_read_qpart = "";
302
303 if ($orig_content_hash != $content_hash) {
304 // print "$orig_content_hash :: $content_hash<br>";
305
306 if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) {
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;
318 }
319
320 if ($entry_is_modified) {
321
322 // print "$entry_guid Modified!<br>";
323
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);
328
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
341 $result = db_query($link, $query);
342 }
343 }
344
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
358 WHERE guid = '$entry_guid' AND owner_uid = " . $_SESSION["uid"]);
359
360 if (!$result || db_num_rows($result) != 1) {
361 return;
362 }
363
364 $entry_id = db_fetch_result($result, 0, "id");
365
366 foreach ($entry_tags as $tag) {
367 $tag = db_escape_string(strtolower($tag));
368
369 $result = db_query($link, "SELECT id FROM ttrss_tags
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");
374
375 if ($result && db_num_rows($result) == 0) {
376
377 // print "tagging $entry_id as $tag<br>";
378
379 db_query($link, "INSERT INTO ttrss_tags (owner_uid,tag_name,post_id)
380 VALUES ('".$_SESSION["uid"]."','$tag', '$entry_id')");
381 }
382 }
383 }
384 }
385
386 db_query($link, "UPDATE ttrss_feeds
387 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
388
389 } else {
390 $error_msg = db_escape_string(magpie_error());
391 db_query($link,
392 "UPDATE ttrss_feeds SET last_error = '$error_msg',
393 last_updated = NOW() WHERE id = '$feed'");
394 }
395
396 db_query($link, "COMMIT");
397
398 }
399
400 function print_select($id, $default, $values, $attributes = "") {
401 print "<select id=\"$id\" $attributes>";
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 }
412
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
439 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
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
447 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
448
449 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
450 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
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
469 function getmicrotime() {
470 list($usec, $sec) = explode(" ",microtime());
471 return ((float)$usec + (float)$sec);
472 }
473
474 function print_radio($id, $default, $values, $attributes = "") {
475 foreach ($values as $v) {
476
477 if ($v == $default)
478 $sel = "checked";
479 else
480 $sel = "";
481
482 if ($v == "Yes") {
483 $sel .= " value=\"1\"";
484 } else {
485 $sel .= " value=\"0\"";
486 }
487
488 print "<input type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
489
490 }
491 }
492
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 }
524
525 function authenticate_user($link, $login, $password) {
526
527 $pwd_hash = 'SHA1:' . sha1($password);
528
529 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
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");
535 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
536
537 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
538 $_SESSION["uid"]);
539
540 return true;
541 }
542
543 return false;
544
545 }
546
547 function http_authenticate_user($link) {
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']);
560
561 return authenticate_user($link, $login, $password);
562 }
563 }
564
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
602 ?>