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