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