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