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