]> git.wh0rd.org - tt-rss.git/blame - functions.php
import rss_utils.inc for parse_w3cdtf()
[tt-rss.git] / functions.php
CommitLineData
40d13c28 1<?
f1a80dae
AD
2 session_start();
3
cce28758
AD
4 if ($_GET["debug"]) {
5 define('DEFAULT_ERROR_LEVEL', E_ALL);
6 } else {
7 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
8 }
9
40d13c28 10 require_once 'config.php';
b619ff15 11 require_once 'db-prefs.php';
40d13c28 12
387234f3
AD
13 require_once 'magpierss/rss_utils.inc';
14
a3ee2a38
AD
15 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
16
fefa6ca3 17 function purge_feed($link, $feed_id, $purge_interval) {
4c193675 18
fefa6ca3 19 if (DB_TYPE == "pgsql") {
35d8cf43 20 db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 21 marked = false AND feed_id = '$feed_id' AND
35d8cf43
AD
22 (SELECT date_entered FROM ttrss_entries WHERE
23 id = ref_id) < NOW() - INTERVAL '$purge_interval days'");
fefa6ca3 24 } else {
35d8cf43 25 db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 26 marked = false AND feed_id = '$feed_id' AND
35d8cf43
AD
27 (SELECT date_entered FROM ttrss_entries WHERE
28 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
fefa6ca3
AD
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
71604ca4
AD
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
fefa6ca3
AD
67 }
68
b6eefba5 69 function purge_old_posts($link) {
5d73494a 70
f1a80dae
AD
71 $user_id = $_SESSION["uid"];
72
73 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
74 WHERE owner_uid = '$user_id'");
5d73494a
AD
75
76 while ($line = db_fetch_assoc($result)) {
77
78 $feed_id = $line["id"];
79 $purge_interval = $line["purge_interval"];
80
b619ff15 81 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 82
140aae81 83 if ($purge_interval > 0) {
fefa6ca3 84 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
85 }
86 }
71604ca4
AD
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");
c3a8d71a
AD
91 }
92
a2770077 93 function update_all_feeds($link, $fetch, $user_id = false) {
40d13c28 94
4769ddaf 95 if (WEB_DEMO_MODE) return;
b0b4abcf 96
a2770077
AD
97 if (!$user_id) {
98 $user_id = $_SESSION["uid"];
99 purge_old_posts($link);
100 }
101
25af8dad 102// db_query($link, "BEGIN");
b82af8c3 103
d148926e
AD
104 $result = db_query($link, "SELECT feed_url,id,
105 substring(last_updated,1,19) as last_updated,
f1a80dae 106 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'");
40d13c28 107
b6eefba5 108 while ($line = db_fetch_assoc($result)) {
d148926e
AD
109 $upd_intl = $line["update_interval"];
110
b619ff15 111 if (!$upd_intl || $upd_intl == 0) {
a2770077 112 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
b619ff15 113 }
d148926e 114
93d40f50
AD
115 if ($fetch || (!$line["last_updated"] ||
116 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 117
8143ae1f 118 update_rss_feed($link, $line["feed_url"], $line["id"]);
d148926e 119 }
40d13c28
AD
120 }
121
25af8dad 122// db_query($link, "COMMIT");
b82af8c3 123
40d13c28
AD
124 }
125
9e997874 126 function check_feed_favicon($feed_url, $feed, $link) {
78800912
AD
127 $feed_url = str_replace("http://", "", $feed_url);
128 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
129
130 $icon_url = "http://$feed_url/favicon.ico";
273a2f6b 131 $icon_file = ICONS_DIR . "/$feed.ico";
78800912
AD
132
133 if (!file_exists($icon_file)) {
e695fdc8 134
78800912
AD
135 error_reporting(0);
136 $r = fopen($icon_url, "r");
cce28758 137 error_reporting (DEFAULT_ERROR_LEVEL);
78800912
AD
138
139 if ($r) {
140 $tmpfname = tempnam("/tmp", "ttrssicon");
141
142 $t = fopen($tmpfname, "w");
143
144 while (!feof($r)) {
145 $buf = fread($r, 16384);
146 fwrite($t, $buf);
147 }
148
149 fclose($r);
150 fclose($t);
151
e695fdc8
AD
152 error_reporting(0);
153 if (!rename($tmpfname, $icon_file)) {
154 unlink($tmpfname);
155 }
717f5e64
AD
156
157 chmod($icon_file, 0644);
158
cce28758 159 error_reporting (DEFAULT_ERROR_LEVEL);
78800912
AD
160
161 }
162 }
163 }
164
ddb68b81 165 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 166
4769ddaf 167 if (WEB_DEMO_MODE) return;
b0b4abcf 168
ddb68b81 169 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
170 return;
171 }
172
ab3d0b99
AD
173 $feed = db_escape_string($feed);
174
3ad5aa85 175 error_reporting(0);
40d13c28 176 $rss = fetch_rss($feed_url);
ab3d0b99 177
cce28758 178 error_reporting (DEFAULT_ERROR_LEVEL);
76798ff3 179
b6eefba5 180 $feed = db_escape_string($feed);
dcee8f61 181
40d13c28 182 if ($rss) {
b82af8c3 183
dd8c76a9
AD
184 db_query($link, "BEGIN");
185
7fed1940 186 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 187 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 188
b6eefba5
AD
189 $registered_title = db_fetch_result($result, 0, "title");
190 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 191 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 192
7fed1940
AD
193 $owner_uid = db_fetch_result($result, 0, "owner_uid");
194
a2770077
AD
195 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
196 check_feed_favicon($feed_url, $feed, $link);
197 }
198
331900c6 199 if (!$registered_title) {
e1305a97 200 $feed_title = db_escape_string($rss->channel["title"]);
f324892e
AD
201 db_query($link, "UPDATE ttrss_feeds SET
202 title = '$feed_title' WHERE id = '$feed'");
203 }
204
147f7691
AD
205 $site_url = $rss->channel["link"];
206
207 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
208 $site_url = db_escape_string($rss->channel["link"]);
209 db_query($link, "UPDATE ttrss_feeds SET
210 site_url = '$site_url' WHERE id = '$feed'");
331900c6 211 }
40d13c28 212
b7f4bda2
AD
213// print "I: " . $rss->channel["image"]["url"];
214
215 $icon_url = $rss->image["url"];
216
147f7691 217 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
218 $icon_url = db_escape_string($icon_url);
219 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
220 }
221
e6155a06
AD
222
223 $filters = array();
224
4b3dff6e 225 $result = db_query($link, "SELECT reg_exp,
e6155a06
AD
226 (SELECT name FROM ttrss_filter_types
227 WHERE id = filter_type) as name
ead60402
AD
228 FROM ttrss_filters WHERE
229 owner_uid = $owner_uid AND
230 (feed_id IS NULL OR feed_id = '$feed')");
e6155a06 231
b6eefba5 232 while ($line = db_fetch_assoc($result)) {
e6155a06 233 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
4b3dff6e 234 array_push($filters[$line["name"]], $line["reg_exp"]);
e6155a06
AD
235 }
236
ddb68b81
AD
237 $iterator = $rss->items;
238
239 if (!$iterator) $iterator = $rss->entries;
240 if (!$iterator) $iterator = $rss;
241
242 foreach ($iterator as $item) {
40d13c28
AD
243
244 $entry_guid = $item["id"];
245
246 if (!$entry_guid) $entry_guid = $item["guid"];
247 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
248
249 if (!$entry_guid) continue;
a116f569 250
9c9c7e6b 251 $entry_timestamp = "";
b82af8c3 252
9c9c7e6b
AD
253 $rss_2_date = $item['pubdate'];
254 $rss_1_date = $item['dc']['date'];
255 $atom_date = $item['issued'];
b197f117 256
9c9c7e6b
AD
257 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
258 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
259 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
260
261 if ($entry_timestamp == "") {
262 $entry_timestamp = time();
263 $no_orig_date = 'true';
466001c4
AD
264 } else {
265 $no_orig_date = 'false';
b82af8c3 266 }
b197f117 267
466001c4 268 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 269
40d13c28 270 $entry_title = $item["title"];
ddb68b81
AD
271
272 // strange Magpie workaround
273 $entry_link = $item["link_"];
274 if (!$entry_link) $entry_link = $item["link"];
71ad3959
AD
275
276 if (!$entry_title) continue;
277 if (!$entry_link) continue;
278
1696229f
AD
279 $entry_content = $item["content:escaped"];
280
ddb68b81 281 if (!$entry_content) $entry_content = $item["summary"];
1696229f 282 if (!$entry_content) $entry_content = $item["content:encoded"];
40d13c28 283 if (!$entry_content) $entry_content = $item["content"];
1696229f 284 if (!$entry_content) $entry_content = $item["description"];
a2015351 285
ee2c3050
AD
286 $entry_content_unescaped = $entry_content;
287
a116f569 288// if (!$entry_content) continue;
a2015351 289
8add756a
AD
290 // WTF
291 if (is_array($entry_content)) {
292 $entry_content = $entry_content["encoded"];
1696229f 293 if (!$entry_content) $entry_content = $entry_content["escaped"];
8add756a
AD
294 }
295
1696229f
AD
296// print_r($item);
297// print_r($entry_content);
298
466001c4 299 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 300
a1ea1e12
AD
301 $entry_comments = $item["comments"];
302
b6eefba5 303 $entry_guid = db_escape_string($entry_guid);
2651fc4f 304
05732aa0
AD
305 $result = db_query($link, "SELECT id FROM ttrss_entries
306 WHERE guid = '$entry_guid'");
4c193675 307
b17fcb1a
AD
308 $entry_content = db_escape_string($entry_content);
309 $entry_title = db_escape_string($entry_title);
310 $entry_link = db_escape_string($entry_link);
311 $entry_comments = db_escape_string($entry_comments);
312
4c193675
AD
313 if (db_num_rows($result) == 0) {
314
315 // base post entry does not exist, create it
316
4c193675
AD
317 $result = db_query($link,
318 "INSERT INTO ttrss_entries
319 (title,
320 guid,
321 link,
322 updated,
323 content,
324 content_hash,
325 no_orig_date,
326 date_entered,
327 comments)
328 VALUES
329 ('$entry_title',
330 '$entry_guid',
331 '$entry_link',
332 '$entry_timestamp_fmt',
333 '$entry_content',
334 '$content_hash',
335 $no_orig_date,
336 NOW(),
337 '$entry_comments')");
338 }
339
340 // now it should exist, if not - bad luck then
341
6385315d
AD
342 $result = db_query($link, "SELECT
343 id,content_hash,no_orig_date,title,
344 substring(updated,1,19) as updated
345 FROM
346 ttrss_entries
347 WHERE guid = '$entry_guid'");
4c193675
AD
348
349 if (db_num_rows($result) == 1) {
350
6385315d
AD
351 // this will be used below in update handler
352 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
353// $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
354// $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
355 $orig_title = db_fetch_result($result, 0, "title");
356
4c193675
AD
357 $ref_id = db_fetch_result($result, 0, "id");
358
359 // check for user post link to main table
360
71604ca4 361 // do we allow duplicate posts with same GUID in different feeds?
a2770077 362 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
71604ca4
AD
363 $dupcheck_qpart = "AND feed_id = '$feed'";
364 } else {
365 $dupcheck_qpart = "";
366 }
367
3a933f22
AD
368 error_reporting(0);
369 if (is_filtered($entry_title, $entry_content, $entry_link, $filters)) {
370 continue;
371 }
cce28758 372 error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 373
4c193675
AD
374 $result = db_query($link,
375 "SELECT ref_id FROM ttrss_user_entries WHERE
71604ca4
AD
376 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
377 $dupcheck_qpart");
4c193675
AD
378
379 // okay it doesn't exist - create user entry
4c193675 380 if (db_num_rows($result) == 0) {
4c193675
AD
381 $result = db_query($link,
382 "INSERT INTO ttrss_user_entries
383 (ref_id, owner_uid, feed_id)
384 VALUES ('$ref_id', '$owner_uid', '$feed')");
4c193675 385 }
6385315d
AD
386
387 $post_needs_update = false;
388
a2770077 389 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
6385315d
AD
390 ($content_hash != $orig_content_hash)) {
391 $post_needs_update = true;
392 }
393
394 if ($orig_title != $entry_title) {
395 $post_needs_update = true;
396 }
397
398// this doesn't seem to be very reliable
399//
400// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
401// $post_needs_update = true;
402// }
403
404 // if post needs update, update it and mark all user entries
1c73bc0c 405 // linking to this post as updated
6385315d
AD
406 if ($post_needs_update) {
407
408// print "<!-- post $orig_title needs update : $post_needs_update -->";
409
6385315d
AD
410 db_query($link, "UPDATE ttrss_entries
411 SET title = '$entry_title', content = '$entry_content'
412 WHERE id = '$ref_id'");
413
414 db_query($link, "UPDATE ttrss_user_entries
415 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
416
417 }
4c193675
AD
418 }
419
eb36b4eb
AD
420 /* taaaags */
421 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
422
05732aa0 423 $entry_tags = null;
eb36b4eb 424
ee2c3050
AD
425 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i",
426 $entry_content_unescaped, $entry_tags);
427
428// print "<br>$entry_title : $entry_content_unescaped<br>";
429// print_r($entry_tags);
eb36b4eb
AD
430
431 $entry_tags = $entry_tags[1];
432
433 if (count($entry_tags) > 0) {
434
05732aa0
AD
435 $result = db_query($link, "SELECT id,int_id
436 FROM ttrss_entries,ttrss_user_entries
25da6909 437 WHERE guid = '$entry_guid'
05732aa0 438 AND feed_id = '$feed' AND ref_id = id
7fed1940 439 AND owner_uid = '$owner_uid'");
eb36b4eb 440
fe99ab12 441 if (db_num_rows($result) == 1) {
eb36b4eb 442
fe99ab12
AD
443 $entry_id = db_fetch_result($result, 0, "id");
444 $entry_int_id = db_fetch_result($result, 0, "int_id");
445
446 foreach ($entry_tags as $tag) {
447 $tag = db_escape_string(strtolower($tag));
448
449 $tag = str_replace("technorati tag: ", "", $tag);
450
451 $result = db_query($link, "SELECT id FROM ttrss_tags
452 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
453 owner_uid = '$owner_uid' LIMIT 1");
454
455 // print db_fetch_result($result, 0, "id");
456
457 if ($result && db_num_rows($result) == 0) {
458
459 // print "tagging $entry_id as $tag<br>";
460
461 db_query($link, "INSERT INTO ttrss_tags
462 (owner_uid,tag_name,post_int_id)
463 VALUES ('$owner_uid','$tag', '$entry_int_id')");
464 }
465 }
eb36b4eb 466 }
05732aa0 467 }
4c193675 468 }
40d13c28 469
ab3d0b99
AD
470 db_query($link, "UPDATE ttrss_feeds
471 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 472
dd8c76a9
AD
473 db_query($link, "COMMIT");
474
ab3d0b99
AD
475 } else {
476 $error_msg = db_escape_string(magpie_error());
477 db_query($link,
aa5f9f5f
AD
478 "UPDATE ttrss_feeds SET last_error = '$error_msg',
479 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
480 }
481
482 }
483
f175937c
AD
484 function print_select($id, $default, $values, $attributes = "") {
485 print "<select id=\"$id\" $attributes>";
a0d53889
AD
486 foreach ($values as $v) {
487 if ($v == $default)
488 $sel = " selected";
489 else
490 $sel = "";
491
492 print "<option$sel>$v</option>";
493 }
494 print "</select>";
495 }
40d13c28 496
3a933f22 497 function is_filtered($title, $content, $link, $filters) {
e6155a06
AD
498
499 if ($filters["title"]) {
500 foreach ($filters["title"] as $title_filter) {
501 if (preg_match("/$title_filter/i", $title))
502 return true;
503 }
504 }
505
506 if ($filters["content"]) {
507 foreach ($filters["content"] as $content_filter) {
508 if (preg_match("/$content_filter/i", $content))
509 return true;
510 }
511 }
512
513 if ($filters["both"]) {
514 foreach ($filters["both"] as $filter) {
515 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
516 return true;
517 }
518 }
519
3a933f22
AD
520 if ($filters["link"]) {
521 foreach ($filters["link"] as $link_filter) {
522 if (preg_match("/$link_filter/i", $link))
523 return true;
524 }
525 }
526
e6155a06
AD
527 return false;
528 }
529
4668523d 530 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
254e0e4b
AD
531
532 if (file_exists($icon_file) && filesize($icon_file) > 0) {
533 $feed_icon = "<img src=\"$icon_file\">";
534 } else {
535 $feed_icon = "<img src=\"images/blank_icon.gif\">";
536 }
537
8143ae1f 538 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
254e0e4b
AD
539
540 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 541 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
542 print "$feed_icon";
543 }
544
545 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
546
547 if ($unread != 0) {
548 $fctr_class = "";
549 } else {
550 $fctr_class = "class=\"invisible\"";
551 }
552
553 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
554 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
555
556 print "</li>";
557
558 }
559
406d9489
AD
560 function getmicrotime() {
561 list($usec, $sec) = explode(" ",microtime());
562 return ((float)$usec + (float)$sec);
563 }
564
77e96719
AD
565 function print_radio($id, $default, $values, $attributes = "") {
566 foreach ($values as $v) {
567
568 if ($v == $default)
5da169d9 569 $sel = "checked";
77e96719 570 else
5da169d9
AD
571 $sel = "";
572
573 if ($v == "Yes") {
574 $sel .= " value=\"1\"";
575 } else {
576 $sel .= " value=\"0\"";
577 }
77e96719
AD
578
579 print "<input type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
580
581 }
582 }
583
ff485f1d
AD
584 function initialize_user_prefs($link, $uid) {
585
586 $uid = db_escape_string($uid);
587
588 db_query($link, "BEGIN");
589
590 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
591
592 $u_result = db_query($link, "SELECT pref_name
593 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
594
595 $active_prefs = array();
596
597 while ($line = db_fetch_assoc($u_result)) {
598 array_push($active_prefs, $line["pref_name"]);
599 }
600
601 while ($line = db_fetch_assoc($result)) {
602 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
603// print "adding " . $line["pref_name"] . "<br>";
604
605 db_query($link, "INSERT INTO ttrss_user_prefs
606 (owner_uid,pref_name,value) VALUES
607 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
608
609 }
610 }
611
612 db_query($link, "COMMIT");
613
614 }
c8437f35
AD
615
616 function authenticate_user($link, $login, $password) {
617
618 $pwd_hash = 'SHA1:' . sha1($password);
619
203b6d25 620 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
c8437f35
AD
621 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
622
623 if (db_num_rows($result) == 1) {
624 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
625 $_SESSION["name"] = db_fetch_result($result, 0, "login");
203b6d25 626 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
c8437f35 627
f6f32198
AD
628 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
629 $_SESSION["uid"]);
630
503eb349
AD
631 $user_theme = get_user_theme_path($link);
632
633 $_SESSION["theme"] = $user_theme;
634
f557cd78
AD
635 initialize_user_prefs($link, $_SESSION["uid"]);
636
c8437f35
AD
637 return true;
638 }
ff485f1d 639
c8437f35
AD
640 return false;
641
642 }
643
e6cb77a0
AD
644 function make_password($length = 8) {
645
646 $password = "";
647 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
648
649 $i = 0;
650
651 while ($i < $length) {
652 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
653
654 if (!strstr($password, $char)) {
655 $password .= $char;
656 $i++;
657 }
658 }
659 return $password;
660 }
661
662 // this is called after user is created to initialize default feeds, labels
663 // or whatever else
664
665 // user preferences are checked on every login, not here
666
667 function initialize_user($link, $uid) {
668
669 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
670 values ('$uid','unread = true', 'Unread articles')");
671
672 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
673 values ('$uid','last_read is null and unread = false', 'Updated articles')");
674
675 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 676 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 677 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
678
679 }
e6cb77a0 680
b8aa49bc 681 function logout_user() {
f557cd78 682 session_destroy();
b8aa49bc
AD
683 }
684
685 function login_sequence($link) {
686 if (!SINGLE_USER_MODE) {
687
688 if (!USE_HTTP_AUTH) {
689 if (!$_SESSION["uid"]) {
690 header("Location: login.php?rt=tt-rss.php");
691 exit;
692 }
693 } else {
f557cd78
AD
694 if (!$_SESSION["uid"]) {
695 if (!$_SERVER["PHP_AUTH_USER"]) {
696
697 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
698 header('HTTP/1.0 401 Unauthorized');
699 exit;
700
701 } else {
702 $auth_result = authenticate_user($link,
703 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
704
705 if (!$auth_result) {
706 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
707 header('HTTP/1.0 401 Unauthorized');
708 exit;
709 }
710 }
711 }
b8aa49bc
AD
712 }
713 } else {
714 $_SESSION["uid"] = 1;
715 $_SESSION["name"] = "admin";
c7a03b7a 716 initialize_user_prefs($link, 1);
b8aa49bc
AD
717 }
718 }
3547842a
AD
719
720 function truncate_string($str, $max_len) {
721 if (strlen($str) > $max_len) {
722 return substr($str, 0, $max_len) . "...";
723 } else {
724 return $str;
725 }
726 }
54a60e1a
AD
727
728 function get_user_theme_path($link) {
729 $result = db_query($link, "SELECT theme_path FROM ttrss_themes
730 WHERE id = (SELECT theme_id FROM ttrss_users
731 WHERE id = " . $_SESSION["uid"] . ")");
732 if (db_num_rows($result) != 0) {
733 return db_fetch_result($result, 0, "theme_path");
734 } else {
735 return null;
736 }
737 }
40d13c28 738?>