]> git.wh0rd.org - tt-rss.git/blob - functions.php
use author attribute
[tt-rss.git] / functions.php
1 <?
2
3 if ($_GET["debug"]) {
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
5 } else {
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
7 }
8
9 require_once 'config.php';
10 require_once 'db-prefs.php';
11 require_once 'compat.php';
12
13 require_once 'magpierss/rss_utils.inc';
14
15 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
16
17 function purge_feed($link, $feed_id, $purge_interval) {
18
19 if (DB_TYPE == "pgsql") {
20 db_query($link, "DELETE FROM ttrss_user_entries WHERE
21 marked = false AND feed_id = '$feed_id' AND
22 (SELECT date_entered FROM ttrss_entries WHERE
23 id = ref_id) < NOW() - INTERVAL '$purge_interval days'");
24 } else {
25 db_query($link, "DELETE FROM ttrss_user_entries WHERE
26 marked = false AND feed_id = '$feed_id' AND
27 (SELECT date_entered FROM ttrss_entries WHERE
28 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
29 }
30 }
31
32 function global_purge_old_posts($link, $do_output = false) {
33
34 $result = db_query($link,
35 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds");
36
37 while ($line = db_fetch_assoc($result)) {
38
39 $feed_id = $line["id"];
40 $purge_interval = $line["purge_interval"];
41 $owner_uid = $line["owner_uid"];
42
43 if ($purge_interval == 0) {
44
45 $tmp_result = db_query($link,
46 "SELECT value FROM ttrss_user_prefs WHERE
47 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
48
49 if (db_num_rows($tmp_result) != 0) {
50 $purge_interval = db_fetch_result($tmp_result, 0, "value");
51 }
52 }
53
54 if ($do_output) {
55 print "<feed id='$feed_id' p_intl='$purge_interval'/>";
56 }
57
58 if ($purge_interval > 0) {
59 purge_feed($link, $feed_id, $purge_interval);
60 }
61 }
62
63 // purge orphaned posts in main content table
64 db_query($link, "DELETE FROM ttrss_entries WHERE
65 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
66
67 }
68
69 function purge_old_posts($link) {
70
71 $user_id = $_SESSION["uid"];
72
73 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
74 WHERE owner_uid = '$user_id'");
75
76 while ($line = db_fetch_assoc($result)) {
77
78 $feed_id = $line["id"];
79 $purge_interval = $line["purge_interval"];
80
81 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
82
83 if ($purge_interval > 0) {
84 purge_feed($link, $feed_id, $purge_interval);
85 }
86 }
87
88 // purge orphaned posts in main content table
89 db_query($link, "DELETE FROM ttrss_entries WHERE
90 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
91 }
92
93 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
94
95 if (WEB_DEMO_MODE) return;
96
97 if (!$user_id) {
98 $user_id = $_SESSION["uid"];
99 purge_old_posts($link);
100 }
101
102 // db_query($link, "BEGIN");
103
104 if (MAX_UPDATE_TIME > 0) {
105 if (DB_TYPE == "mysql") {
106 $q_order = "RAND()";
107 } else {
108 $q_order = "RANDOM()";
109 }
110 } else {
111 $q_order = "last_updated DESC";
112 }
113
114 $result = db_query($link, "SELECT feed_url,id,
115 SUBSTRING(last_updated,1,19) AS last_updated,
116 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
117 ORDER BY $q_order");
118
119 $upd_start = time();
120
121 while ($line = db_fetch_assoc($result)) {
122 $upd_intl = $line["update_interval"];
123
124 if (!$upd_intl || $upd_intl == 0) {
125 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
126 }
127
128 if ($upd_intl < 0) {
129 // Updates for this feed are disabled
130 continue;
131 }
132
133 if ($fetch || (!$line["last_updated"] ||
134 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
135
136 // print "<!-- feed: ".$line["feed_url"]." -->";
137
138 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
139
140 $upd_elapsed = time() - $upd_start;
141
142 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
143 return;
144 }
145 }
146 }
147
148 // db_query($link, "COMMIT");
149
150 }
151
152 function check_feed_favicon($feed_url, $feed, $link) {
153 $feed_url = str_replace("http://", "", $feed_url);
154 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
155
156 $icon_url = "http://$feed_url/favicon.ico";
157 $icon_file = ICONS_DIR . "/$feed.ico";
158
159 if (!file_exists($icon_file)) {
160
161 error_reporting(0);
162 $r = fopen($icon_url, "r");
163 error_reporting (DEFAULT_ERROR_LEVEL);
164
165 if ($r) {
166 $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon");
167
168 $t = fopen($tmpfname, "w");
169
170 while (!feof($r)) {
171 $buf = fread($r, 16384);
172 fwrite($t, $buf);
173 }
174
175 fclose($r);
176 fclose($t);
177
178 error_reporting(0);
179 if (!rename($tmpfname, $icon_file)) {
180 unlink($tmpfname);
181 }
182
183 chmod($icon_file, 0644);
184
185 error_reporting (DEFAULT_ERROR_LEVEL);
186
187 }
188 }
189 }
190
191 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
192
193 if (WEB_DEMO_MODE) return;
194
195 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
196 return;
197 }
198
199 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
200 FROM ttrss_feeds WHERE id = '$feed'");
201
202 $auth_login = db_fetch_result($result, 0, "auth_login");
203 $auth_pass = db_fetch_result($result, 0, "auth_pass");
204
205 $update_interval = db_fetch_result($result, 0, "update_interval");
206
207 if ($update_interval < 0) { return; }
208
209 $feed = db_escape_string($feed);
210
211 $fetch_url = $feed_url;
212
213 if ($auth_login && $auth_pass) {
214 $url_parts = array();
215 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
216
217 if ($url_parts[1] && $url_parts[2]) {
218 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
219 }
220
221 }
222 error_reporting(0);
223 $rss = fetch_rss($fetch_url);
224
225 error_reporting (DEFAULT_ERROR_LEVEL);
226
227 $feed = db_escape_string($feed);
228
229 if ($rss) {
230
231 db_query($link, "BEGIN");
232
233 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
234 FROM ttrss_feeds WHERE id = '$feed'");
235
236 $registered_title = db_fetch_result($result, 0, "title");
237 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
238 $orig_site_url = db_fetch_result($result, 0, "site_url");
239
240 $owner_uid = db_fetch_result($result, 0, "owner_uid");
241
242 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
243 check_feed_favicon($feed_url, $feed, $link);
244 }
245
246 if (!$registered_title || $registered_title == "[Unknown]") {
247 $feed_title = db_escape_string($rss->channel["title"]);
248 db_query($link, "UPDATE ttrss_feeds SET
249 title = '$feed_title' WHERE id = '$feed'");
250 }
251
252 $site_url = $rss->channel["link"];
253 // weird, weird Magpie
254 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
255
256 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
257 db_query($link, "UPDATE ttrss_feeds SET
258 site_url = '$site_url' WHERE id = '$feed'");
259 }
260
261 // print "I: " . $rss->channel["image"]["url"];
262
263 $icon_url = $rss->image["url"];
264
265 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
266 $icon_url = db_escape_string($icon_url);
267 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
268 }
269
270
271 $filters = array();
272
273 $result = db_query($link, "SELECT reg_exp,
274 ttrss_filter_types.name AS name,
275 ttrss_filter_actions.name AS action
276 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
277 owner_uid = $owner_uid AND
278 ttrss_filter_types.id = filter_type AND
279 ttrss_filter_actions.id = action_id AND
280 (feed_id IS NULL OR feed_id = '$feed')");
281
282 while ($line = db_fetch_assoc($result)) {
283 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
284
285 $filter["reg_exp"] = $line["reg_exp"];
286 $filter["action"] = $line["action"];
287
288 array_push($filters[$line["name"]], $filter);
289 }
290
291 $iterator = $rss->items;
292
293 if (!$iterator) $iterator = $rss->entries;
294 if (!$iterator) $iterator = $rss;
295
296 foreach ($iterator as $item) {
297
298 $entry_guid = $item["id"];
299
300 if (!$entry_guid) $entry_guid = $item["guid"];
301 if (!$entry_guid) $entry_guid = $item["link"];
302
303 if (!$entry_guid) continue;
304
305 $entry_timestamp = "";
306
307 $rss_2_date = $item['pubdate'];
308 $rss_1_date = $item['dc']['date'];
309 $atom_date = $item['issued'];
310 if (!$atom_date) $atom_date = $item['updated'];
311
312 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
313 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
314 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
315
316 if ($entry_timestamp == "") {
317 $entry_timestamp = time();
318 $no_orig_date = 'true';
319 } else {
320 $no_orig_date = 'false';
321 }
322
323 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
324
325 $entry_title = $item["title"];
326
327 // strange Magpie workaround
328 $entry_link = $item["link_"];
329 if (!$entry_link) $entry_link = $item["link"];
330
331 if (!$entry_title) continue;
332 if (!$entry_link) continue;
333
334 $entry_content = $item["content:escaped"];
335
336 if (!$entry_content) $entry_content = $item["content:encoded"];
337 if (!$entry_content) $entry_content = $item["content"];
338 if (!$entry_content) $entry_content = $item["summary"];
339 if (!$entry_content) $entry_content = $item["description"];
340
341 // if (!$entry_content) continue;
342
343 // WTF
344 if (is_array($entry_content)) {
345 $entry_content = $entry_content["encoded"];
346 if (!$entry_content) $entry_content = $entry_content["escaped"];
347 }
348
349 // print_r($item);
350 // print_r(htmlspecialchars($entry_content));
351 // print "<br>";
352
353 $entry_content_unescaped = $entry_content;
354 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
355
356 $entry_comments = $item["comments"];
357
358 $entry_author = db_escape_string($item['dc']['creator']);
359
360 $entry_guid = db_escape_string($entry_guid);
361
362 $result = db_query($link, "SELECT id FROM ttrss_entries
363 WHERE guid = '$entry_guid'");
364
365 $entry_content = db_escape_string($entry_content);
366 $entry_title = db_escape_string($entry_title);
367 $entry_link = db_escape_string($entry_link);
368 $entry_comments = db_escape_string($entry_comments);
369
370 $num_comments = db_escape_string($item["slash"]["comments"]);
371
372 if (!$num_comments) $num_comments = 0;
373
374 if (db_num_rows($result) == 0) {
375
376 // base post entry does not exist, create it
377
378 $result = db_query($link,
379 "INSERT INTO ttrss_entries
380 (title,
381 guid,
382 link,
383 updated,
384 content,
385 content_hash,
386 no_orig_date,
387 date_entered,
388 comments,
389 num_comments,
390 author)
391 VALUES
392 ('$entry_title',
393 '$entry_guid',
394 '$entry_link',
395 '$entry_timestamp_fmt',
396 '$entry_content',
397 '$content_hash',
398 $no_orig_date,
399 NOW(),
400 '$entry_comments',
401 '$num_comments',
402 '$entry_author')");
403 } else {
404 // we keep encountering the entry in feeds, so we need to
405 // update date_entered column so that we don't get horrible
406 // dupes when the entry gets purged and reinserted again e.g.
407 // in the case of SLOW SLOW OMG SLOW updating feeds
408
409 $base_entry_id = db_fetch_result($result, 0, "id");
410
411 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
412 WHERE id = '$base_entry_id'");
413 }
414
415 // now it should exist, if not - bad luck then
416
417 $result = db_query($link, "SELECT
418 id,content_hash,no_orig_date,title,
419 substring(date_entered,1,19) as date_entered,
420 substring(updated,1,19) as updated,
421 num_comments
422 FROM
423 ttrss_entries
424 WHERE guid = '$entry_guid'");
425
426 if (db_num_rows($result) == 1) {
427
428 // this will be used below in update handler
429 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
430 $orig_title = db_fetch_result($result, 0, "title");
431 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
432 $orig_date_entered = strtotime(db_fetch_result($result,
433 0, "date_entered"));
434
435 $ref_id = db_fetch_result($result, 0, "id");
436
437 // check for user post link to main table
438
439 // do we allow duplicate posts with same GUID in different feeds?
440 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
441 $dupcheck_qpart = "AND feed_id = '$feed'";
442 } else {
443 $dupcheck_qpart = "";
444 }
445
446 // error_reporting(0);
447
448 $filter_name = get_filter_name($entry_title, $entry_content,
449 $entry_link, $filters);
450
451 if ($filter_name == "filter") {
452 continue;
453 }
454
455 // error_reporting (DEFAULT_ERROR_LEVEL);
456
457 $result = db_query($link,
458 "SELECT ref_id FROM ttrss_user_entries WHERE
459 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
460 $dupcheck_qpart");
461
462 // okay it doesn't exist - create user entry
463 if (db_num_rows($result) == 0) {
464
465 if ($filter_name != 'catchup') {
466 $unread = 'true';
467 $last_read_qpart = 'NULL';
468 } else {
469 $unread = 'false';
470 $last_read_qpart = 'NOW()';
471 }
472
473 $result = db_query($link,
474 "INSERT INTO ttrss_user_entries
475 (ref_id, owner_uid, feed_id, unread, last_read)
476 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
477 $last_read_qpart)");
478 }
479
480 $post_needs_update = false;
481
482 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
483 ($content_hash != $orig_content_hash)) {
484 $post_needs_update = true;
485 }
486
487 if ($orig_title != $entry_title) {
488 $post_needs_update = true;
489 }
490
491 if ($orig_num_comments != $num_comments) {
492 $post_needs_update = true;
493 }
494
495 // this doesn't seem to be very reliable
496 //
497 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
498 // $post_needs_update = true;
499 // }
500
501 // if post needs update, update it and mark all user entries
502 // linking to this post as updated
503 if ($post_needs_update) {
504
505 // print "<!-- post $orig_title needs update : $post_needs_update -->";
506
507 db_query($link, "UPDATE ttrss_entries
508 SET title = '$entry_title', content = '$entry_content',
509 num_comments = '$num_comments'
510 WHERE id = '$ref_id'");
511
512 db_query($link, "UPDATE ttrss_user_entries
513 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
514
515 }
516 }
517
518 /* taaaags */
519 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
520
521 $entry_tags = null;
522
523 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
524 $entry_content_unescaped, $entry_tags);
525
526 // print "<br>$entry_title : $entry_content_unescaped<br>";
527 // print_r($entry_tags);
528 // print "<br>";
529
530 $entry_tags = $entry_tags[1];
531
532 if (count($entry_tags) > 0) {
533
534 $result = db_query($link, "SELECT id,int_id
535 FROM ttrss_entries,ttrss_user_entries
536 WHERE guid = '$entry_guid'
537 AND feed_id = '$feed' AND ref_id = id
538 AND owner_uid = '$owner_uid'");
539
540 if (db_num_rows($result) == 1) {
541
542 $entry_id = db_fetch_result($result, 0, "id");
543 $entry_int_id = db_fetch_result($result, 0, "int_id");
544
545 foreach ($entry_tags as $tag) {
546 $tag = db_escape_string(strtolower($tag));
547
548 $tag = str_replace("+", " ", $tag);
549 $tag = str_replace("technorati tag: ", "", $tag);
550
551 $result = db_query($link, "SELECT id FROM ttrss_tags
552 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
553 owner_uid = '$owner_uid' LIMIT 1");
554
555 // print db_fetch_result($result, 0, "id");
556
557 if ($result && db_num_rows($result) == 0) {
558
559 // print "tagging $entry_id as $tag<br>";
560
561 db_query($link, "INSERT INTO ttrss_tags
562 (owner_uid,tag_name,post_int_id)
563 VALUES ('$owner_uid','$tag', '$entry_int_id')");
564 }
565 }
566 }
567 }
568 }
569
570 db_query($link, "UPDATE ttrss_feeds
571 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
572
573 db_query($link, "COMMIT");
574
575 } else {
576 $error_msg = db_escape_string(magpie_error());
577 db_query($link,
578 "UPDATE ttrss_feeds SET last_error = '$error_msg',
579 last_updated = NOW() WHERE id = '$feed'");
580 }
581
582 }
583
584 function print_select($id, $default, $values, $attributes = "") {
585 print "<select id=\"$id\" $attributes>";
586 foreach ($values as $v) {
587 if ($v == $default)
588 $sel = " selected";
589 else
590 $sel = "";
591
592 print "<option$sel>$v</option>";
593 }
594 print "</select>";
595 }
596
597 function get_filter_name($title, $content, $link, $filters) {
598
599 if ($filters["title"]) {
600 foreach ($filters["title"] as $filter) {
601 $reg_exp = $filter["reg_exp"];
602 if (preg_match("/$reg_exp/i", $title)) {
603 return $filter["action"];
604 }
605 }
606 }
607
608 if ($filters["content"]) {
609 foreach ($filters["content"] as $filter) {
610 $reg_exp = $filter["reg_exp"];
611 if (preg_match("/$reg_exp/i", $content)) {
612 return $filter["action"];
613 }
614 }
615 }
616
617 if ($filters["both"]) {
618 foreach ($filters["both"] as $filter) {
619 $reg_exp = $filter["reg_exp"];
620 if (preg_match("/$reg_exp/i", $title) ||
621 preg_match("/$reg_exp/i", $content)) {
622 return $filter["action"];
623 }
624 }
625 }
626
627 if ($filters["link"]) {
628 $reg_exp = $filter["reg_exp"];
629 foreach ($filters["link"] as $filter) {
630 $reg_exp = $filter["reg_exp"];
631 if (preg_match("/$reg_exp/i", $link)) {
632 return $filter["action"];
633 }
634 }
635 }
636
637 return false;
638 }
639
640 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
641
642 if (file_exists($icon_file) && filesize($icon_file) > 0) {
643 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
644 } else {
645 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
646 }
647
648 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
649
650 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
651 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
652 print "$feed_icon";
653 }
654
655 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
656
657 if ($unread != 0) {
658 $fctr_class = "";
659 } else {
660 $fctr_class = "class=\"invisible\"";
661 }
662
663 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
664 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
665
666 print "</li>";
667
668 }
669
670 function getmicrotime() {
671 list($usec, $sec) = explode(" ",microtime());
672 return ((float)$usec + (float)$sec);
673 }
674
675 function print_radio($id, $default, $values, $attributes = "") {
676 foreach ($values as $v) {
677
678 if ($v == $default)
679 $sel = "checked";
680 else
681 $sel = "";
682
683 if ($v == "Yes") {
684 $sel .= " value=\"1\"";
685 } else {
686 $sel .= " value=\"0\"";
687 }
688
689 print "<input class=\"noborder\"
690 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
691
692 }
693 }
694
695 function initialize_user_prefs($link, $uid) {
696
697 $uid = db_escape_string($uid);
698
699 db_query($link, "BEGIN");
700
701 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
702
703 $u_result = db_query($link, "SELECT pref_name
704 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
705
706 $active_prefs = array();
707
708 while ($line = db_fetch_assoc($u_result)) {
709 array_push($active_prefs, $line["pref_name"]);
710 }
711
712 while ($line = db_fetch_assoc($result)) {
713 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
714 // print "adding " . $line["pref_name"] . "<br>";
715
716 db_query($link, "INSERT INTO ttrss_user_prefs
717 (owner_uid,pref_name,value) VALUES
718 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
719
720 }
721 }
722
723 db_query($link, "COMMIT");
724
725 }
726
727 function authenticate_user($link, $login, $password) {
728
729 $pwd_hash = 'SHA1:' . sha1($password);
730
731 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
732 login = '$login' AND ((pwd_hash = '$password' AND '$password' = 'password')
733 OR pwd_hash = '$pwd_hash')");
734
735 if (db_num_rows($result) == 1) {
736 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
737 $_SESSION["name"] = db_fetch_result($result, 0, "login");
738 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
739
740 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
741 $_SESSION["uid"]);
742
743 $user_theme = get_user_theme_path($link);
744
745 $_SESSION["theme"] = $user_theme;
746
747 initialize_user_prefs($link, $_SESSION["uid"]);
748
749 return true;
750 }
751
752 return false;
753
754 }
755
756 function make_password($length = 8) {
757
758 $password = "";
759 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
760
761 $i = 0;
762
763 while ($i < $length) {
764 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
765
766 if (!strstr($password, $char)) {
767 $password .= $char;
768 $i++;
769 }
770 }
771 return $password;
772 }
773
774 // this is called after user is created to initialize default feeds, labels
775 // or whatever else
776
777 // user preferences are checked on every login, not here
778
779 function initialize_user($link, $uid) {
780
781 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
782 values ('$uid','unread = true', 'Unread articles')");
783
784 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
785 values ('$uid','last_read is null and unread = false', 'Updated articles')");
786
787 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
788 values ('$uid', 'Tiny Tiny RSS: New Releases',
789 'http://tt-rss.spb.ru/releases.rss')");
790
791 }
792
793 function logout_user() {
794 session_destroy();
795 }
796
797 function get_script_urlpath() {
798 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
799 }
800
801 function get_login_redirect() {
802 $server = $_SERVER["SERVER_NAME"];
803
804 if (ENABLE_LOGIN_SSL) {
805 $protocol = "https";
806 } else {
807 $protocol = "http";
808 }
809
810 $url_path = get_script_urlpath();
811
812 $redirect_uri = "$protocol://$server$url_path/login.php";
813
814 return $redirect_uri;
815 }
816
817 function login_sequence($link) {
818 if (!SINGLE_USER_MODE) {
819
820 if (!USE_HTTP_AUTH) {
821 if (!$_SESSION["uid"]) {
822 $redirect_uri = get_login_redirect();
823 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
824 header("Location: $redirect_uri?rt=$return_to");
825 exit;
826 }
827 } else {
828 if (!$_SESSION["uid"]) {
829 if (!$_SERVER["PHP_AUTH_USER"]) {
830
831 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
832 header('HTTP/1.0 401 Unauthorized');
833 exit;
834
835 } else {
836 $auth_result = authenticate_user($link,
837 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
838
839 if (!$auth_result) {
840 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
841 header('HTTP/1.0 401 Unauthorized');
842 exit;
843 }
844 }
845 }
846 }
847 } else {
848 $_SESSION["uid"] = 1;
849 $_SESSION["name"] = "admin";
850 initialize_user_prefs($link, 1);
851 }
852 }
853
854 function truncate_string($str, $max_len) {
855 if (mb_strlen($str, "utf-8") > $max_len - 3) {
856 return mb_substr($str, 0, $max_len, "utf-8") . "...";
857 } else {
858 return $str;
859 }
860 }
861
862 function get_user_theme_path($link) {
863 $result = db_query($link, "SELECT theme_path
864 FROM
865 ttrss_themes,ttrss_users
866 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
867 if (db_num_rows($result) != 0) {
868 return db_fetch_result($result, 0, "theme_path");
869 } else {
870 return null;
871 }
872 }
873
874 function smart_date_time($timestamp) {
875 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
876 return date("G:i", $timestamp);
877 } else if (date("Y", $timestamp) == date("Y")) {
878 return date("M d, G:i", $timestamp);
879 } else {
880 return date("Y/m/d G:i", $timestamp);
881 }
882 }
883
884 function smart_date($timestamp) {
885 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
886 return "Today";
887 } else if (date("Y", $timestamp) == date("Y")) {
888 return date("D m", $timestamp);
889 } else {
890 return date("Y/m/d", $timestamp);
891 }
892 }
893
894 function sql_bool_to_string($s) {
895 if ($s == "t" || $s == "1") {
896 return "true";
897 } else {
898 return "false";
899 }
900 }
901
902 function sql_bool_to_bool($s) {
903 if ($s == "t" || $s == "1") {
904 return true;
905 } else {
906 return false;
907 }
908 }
909
910
911 function toggleEvenOdd($a) {
912 if ($a == "even")
913 return "odd";
914 else
915 return "even";
916 }
917
918 function sanity_check($link) {
919
920 $error_code = 0;
921 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
922 $schema_version = db_fetch_result($result, 0, "schema_version");
923
924 if ($schema_version != SCHEMA_VERSION) {
925 $error_code = 5;
926 }
927
928 if ($error_code != 0) {
929 print "<error error-code='$error_code'/>";
930 return false;
931 } else {
932 return true;
933 }
934 }
935
936 function file_is_locked($filename) {
937 error_reporting(0);
938 $fp = fopen($filename, "r");
939 error_reporting(DEFAULT_ERROR_LEVEL);
940 if ($fp) {
941 if (flock($fp, LOCK_EX | LOCK_NB)) {
942 flock($fp, LOCK_UN);
943 fclose($fp);
944 return false;
945 }
946 fclose($fp);
947 return true;
948 }
949 return false;
950 }
951
952 function make_lockfile($filename) {
953 $fp = fopen($filename, "w");
954
955 if (flock($fp, LOCK_EX | LOCK_NB)) {
956 return $fp;
957 } else {
958 return false;
959 }
960 }
961
962 ?>