]> git.wh0rd.org - tt-rss.git/blame - functions.php
add j,k bindings to subframes
[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 205 $site_url = $rss->channel["link"];
832b7bfc
AD
206 // weird, weird Magpie
207 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
147f7691
AD
208
209 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
210 db_query($link, "UPDATE ttrss_feeds SET
211 site_url = '$site_url' WHERE id = '$feed'");
331900c6 212 }
40d13c28 213
b7f4bda2
AD
214// print "I: " . $rss->channel["image"]["url"];
215
216 $icon_url = $rss->image["url"];
217
147f7691 218 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
219 $icon_url = db_escape_string($icon_url);
220 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
221 }
222
e6155a06
AD
223
224 $filters = array();
225
4b3dff6e 226 $result = db_query($link, "SELECT reg_exp,
e6155a06
AD
227 (SELECT name FROM ttrss_filter_types
228 WHERE id = filter_type) as name
ead60402
AD
229 FROM ttrss_filters WHERE
230 owner_uid = $owner_uid AND
231 (feed_id IS NULL OR feed_id = '$feed')");
e6155a06 232
b6eefba5 233 while ($line = db_fetch_assoc($result)) {
e6155a06 234 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
4b3dff6e 235 array_push($filters[$line["name"]], $line["reg_exp"]);
e6155a06
AD
236 }
237
ddb68b81
AD
238 $iterator = $rss->items;
239
240 if (!$iterator) $iterator = $rss->entries;
241 if (!$iterator) $iterator = $rss;
242
243 foreach ($iterator as $item) {
40d13c28
AD
244
245 $entry_guid = $item["id"];
246
247 if (!$entry_guid) $entry_guid = $item["guid"];
248 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
249
250 if (!$entry_guid) continue;
a116f569 251
9c9c7e6b 252 $entry_timestamp = "";
b82af8c3 253
9c9c7e6b
AD
254 $rss_2_date = $item['pubdate'];
255 $rss_1_date = $item['dc']['date'];
256 $atom_date = $item['issued'];
59ba2c75 257 if (!$atom_date) $atom_date = $item['updated'];
b197f117 258
9c9c7e6b
AD
259 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
260 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
261 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
262
263 if ($entry_timestamp == "") {
264 $entry_timestamp = time();
265 $no_orig_date = 'true';
466001c4
AD
266 } else {
267 $no_orig_date = 'false';
b82af8c3 268 }
b197f117 269
466001c4 270 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 271
40d13c28 272 $entry_title = $item["title"];
ddb68b81
AD
273
274 // strange Magpie workaround
275 $entry_link = $item["link_"];
276 if (!$entry_link) $entry_link = $item["link"];
71ad3959
AD
277
278 if (!$entry_title) continue;
279 if (!$entry_link) continue;
280
1696229f
AD
281 $entry_content = $item["content:escaped"];
282
ddb68b81 283 if (!$entry_content) $entry_content = $item["summary"];
1696229f 284 if (!$entry_content) $entry_content = $item["content:encoded"];
40d13c28 285 if (!$entry_content) $entry_content = $item["content"];
1696229f 286 if (!$entry_content) $entry_content = $item["description"];
a2015351 287
ee2c3050
AD
288 $entry_content_unescaped = $entry_content;
289
a116f569 290// if (!$entry_content) continue;
a2015351 291
8add756a
AD
292 // WTF
293 if (is_array($entry_content)) {
294 $entry_content = $entry_content["encoded"];
1696229f 295 if (!$entry_content) $entry_content = $entry_content["escaped"];
8add756a
AD
296 }
297
1696229f
AD
298// print_r($item);
299// print_r($entry_content);
300
466001c4 301 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 302
a1ea1e12
AD
303 $entry_comments = $item["comments"];
304
b6eefba5 305 $entry_guid = db_escape_string($entry_guid);
2651fc4f 306
05732aa0
AD
307 $result = db_query($link, "SELECT id FROM ttrss_entries
308 WHERE guid = '$entry_guid'");
4c193675 309
b17fcb1a
AD
310 $entry_content = db_escape_string($entry_content);
311 $entry_title = db_escape_string($entry_title);
312 $entry_link = db_escape_string($entry_link);
313 $entry_comments = db_escape_string($entry_comments);
314
4c193675
AD
315 if (db_num_rows($result) == 0) {
316
317 // base post entry does not exist, create it
318
4c193675
AD
319 $result = db_query($link,
320 "INSERT INTO ttrss_entries
321 (title,
322 guid,
323 link,
324 updated,
325 content,
326 content_hash,
327 no_orig_date,
328 date_entered,
329 comments)
330 VALUES
331 ('$entry_title',
332 '$entry_guid',
333 '$entry_link',
334 '$entry_timestamp_fmt',
335 '$entry_content',
336 '$content_hash',
337 $no_orig_date,
338 NOW(),
339 '$entry_comments')");
340 }
341
342 // now it should exist, if not - bad luck then
343
6385315d
AD
344 $result = db_query($link, "SELECT
345 id,content_hash,no_orig_date,title,
346 substring(updated,1,19) as updated
347 FROM
348 ttrss_entries
349 WHERE guid = '$entry_guid'");
4c193675
AD
350
351 if (db_num_rows($result) == 1) {
352
6385315d
AD
353 // this will be used below in update handler
354 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
355// $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated"));
356// $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date");
357 $orig_title = db_fetch_result($result, 0, "title");
358
4c193675
AD
359 $ref_id = db_fetch_result($result, 0, "id");
360
361 // check for user post link to main table
362
71604ca4 363 // do we allow duplicate posts with same GUID in different feeds?
a2770077 364 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
71604ca4
AD
365 $dupcheck_qpart = "AND feed_id = '$feed'";
366 } else {
367 $dupcheck_qpart = "";
368 }
369
3a933f22
AD
370 error_reporting(0);
371 if (is_filtered($entry_title, $entry_content, $entry_link, $filters)) {
372 continue;
373 }
cce28758 374 error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 375
4c193675
AD
376 $result = db_query($link,
377 "SELECT ref_id FROM ttrss_user_entries WHERE
71604ca4
AD
378 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
379 $dupcheck_qpart");
4c193675
AD
380
381 // okay it doesn't exist - create user entry
4c193675 382 if (db_num_rows($result) == 0) {
4c193675
AD
383 $result = db_query($link,
384 "INSERT INTO ttrss_user_entries
385 (ref_id, owner_uid, feed_id)
386 VALUES ('$ref_id', '$owner_uid', '$feed')");
4c193675 387 }
6385315d
AD
388
389 $post_needs_update = false;
390
a2770077 391 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
6385315d
AD
392 ($content_hash != $orig_content_hash)) {
393 $post_needs_update = true;
394 }
395
396 if ($orig_title != $entry_title) {
397 $post_needs_update = true;
398 }
399
400// this doesn't seem to be very reliable
401//
402// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
403// $post_needs_update = true;
404// }
405
406 // if post needs update, update it and mark all user entries
1c73bc0c 407 // linking to this post as updated
6385315d
AD
408 if ($post_needs_update) {
409
410// print "<!-- post $orig_title needs update : $post_needs_update -->";
411
6385315d
AD
412 db_query($link, "UPDATE ttrss_entries
413 SET title = '$entry_title', content = '$entry_content'
414 WHERE id = '$ref_id'");
415
416 db_query($link, "UPDATE ttrss_user_entries
417 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
418
419 }
4c193675
AD
420 }
421
eb36b4eb
AD
422 /* taaaags */
423 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
424
05732aa0 425 $entry_tags = null;
eb36b4eb 426
ee2c3050
AD
427 preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i",
428 $entry_content_unescaped, $entry_tags);
429
430// print "<br>$entry_title : $entry_content_unescaped<br>";
431// print_r($entry_tags);
eb36b4eb
AD
432
433 $entry_tags = $entry_tags[1];
434
435 if (count($entry_tags) > 0) {
436
05732aa0
AD
437 $result = db_query($link, "SELECT id,int_id
438 FROM ttrss_entries,ttrss_user_entries
25da6909 439 WHERE guid = '$entry_guid'
05732aa0 440 AND feed_id = '$feed' AND ref_id = id
7fed1940 441 AND owner_uid = '$owner_uid'");
eb36b4eb 442
fe99ab12 443 if (db_num_rows($result) == 1) {
eb36b4eb 444
fe99ab12
AD
445 $entry_id = db_fetch_result($result, 0, "id");
446 $entry_int_id = db_fetch_result($result, 0, "int_id");
447
448 foreach ($entry_tags as $tag) {
449 $tag = db_escape_string(strtolower($tag));
450
451 $tag = str_replace("technorati tag: ", "", $tag);
452
453 $result = db_query($link, "SELECT id FROM ttrss_tags
454 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
455 owner_uid = '$owner_uid' LIMIT 1");
456
457 // print db_fetch_result($result, 0, "id");
458
459 if ($result && db_num_rows($result) == 0) {
460
461 // print "tagging $entry_id as $tag<br>";
462
463 db_query($link, "INSERT INTO ttrss_tags
464 (owner_uid,tag_name,post_int_id)
465 VALUES ('$owner_uid','$tag', '$entry_int_id')");
466 }
467 }
eb36b4eb 468 }
05732aa0 469 }
4c193675 470 }
40d13c28 471
ab3d0b99
AD
472 db_query($link, "UPDATE ttrss_feeds
473 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 474
dd8c76a9
AD
475 db_query($link, "COMMIT");
476
ab3d0b99
AD
477 } else {
478 $error_msg = db_escape_string(magpie_error());
479 db_query($link,
aa5f9f5f
AD
480 "UPDATE ttrss_feeds SET last_error = '$error_msg',
481 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
482 }
483
484 }
485
f175937c
AD
486 function print_select($id, $default, $values, $attributes = "") {
487 print "<select id=\"$id\" $attributes>";
a0d53889
AD
488 foreach ($values as $v) {
489 if ($v == $default)
490 $sel = " selected";
491 else
492 $sel = "";
493
494 print "<option$sel>$v</option>";
495 }
496 print "</select>";
497 }
40d13c28 498
3a933f22 499 function is_filtered($title, $content, $link, $filters) {
e6155a06
AD
500
501 if ($filters["title"]) {
502 foreach ($filters["title"] as $title_filter) {
503 if (preg_match("/$title_filter/i", $title))
504 return true;
505 }
506 }
507
508 if ($filters["content"]) {
509 foreach ($filters["content"] as $content_filter) {
510 if (preg_match("/$content_filter/i", $content))
511 return true;
512 }
513 }
514
515 if ($filters["both"]) {
516 foreach ($filters["both"] as $filter) {
517 if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content))
518 return true;
519 }
520 }
521
3a933f22
AD
522 if ($filters["link"]) {
523 foreach ($filters["link"] as $link_filter) {
524 if (preg_match("/$link_filter/i", $link))
525 return true;
526 }
527 }
528
e6155a06
AD
529 return false;
530 }
531
4668523d 532 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
254e0e4b
AD
533
534 if (file_exists($icon_file) && filesize($icon_file) > 0) {
535 $feed_icon = "<img src=\"$icon_file\">";
536 } else {
537 $feed_icon = "<img src=\"images/blank_icon.gif\">";
538 }
539
8143ae1f 540 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
254e0e4b
AD
541
542 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 543 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
544 print "$feed_icon";
545 }
546
547 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
548
549 if ($unread != 0) {
550 $fctr_class = "";
551 } else {
552 $fctr_class = "class=\"invisible\"";
553 }
554
555 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
556 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
557
558 print "</li>";
559
560 }
561
406d9489
AD
562 function getmicrotime() {
563 list($usec, $sec) = explode(" ",microtime());
564 return ((float)$usec + (float)$sec);
565 }
566
77e96719
AD
567 function print_radio($id, $default, $values, $attributes = "") {
568 foreach ($values as $v) {
569
570 if ($v == $default)
5da169d9 571 $sel = "checked";
77e96719 572 else
5da169d9
AD
573 $sel = "";
574
575 if ($v == "Yes") {
576 $sel .= " value=\"1\"";
577 } else {
578 $sel .= " value=\"0\"";
579 }
77e96719
AD
580
581 print "<input type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
582
583 }
584 }
585
ff485f1d
AD
586 function initialize_user_prefs($link, $uid) {
587
588 $uid = db_escape_string($uid);
589
590 db_query($link, "BEGIN");
591
592 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
593
594 $u_result = db_query($link, "SELECT pref_name
595 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
596
597 $active_prefs = array();
598
599 while ($line = db_fetch_assoc($u_result)) {
600 array_push($active_prefs, $line["pref_name"]);
601 }
602
603 while ($line = db_fetch_assoc($result)) {
604 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
605// print "adding " . $line["pref_name"] . "<br>";
606
607 db_query($link, "INSERT INTO ttrss_user_prefs
608 (owner_uid,pref_name,value) VALUES
609 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
610
611 }
612 }
613
614 db_query($link, "COMMIT");
615
616 }
c8437f35
AD
617
618 function authenticate_user($link, $login, $password) {
619
620 $pwd_hash = 'SHA1:' . sha1($password);
621
203b6d25 622 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
c8437f35
AD
623 login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')");
624
625 if (db_num_rows($result) == 1) {
626 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
627 $_SESSION["name"] = db_fetch_result($result, 0, "login");
203b6d25 628 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
c8437f35 629
f6f32198
AD
630 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
631 $_SESSION["uid"]);
632
503eb349
AD
633 $user_theme = get_user_theme_path($link);
634
635 $_SESSION["theme"] = $user_theme;
636
f557cd78
AD
637 initialize_user_prefs($link, $_SESSION["uid"]);
638
c8437f35
AD
639 return true;
640 }
ff485f1d 641
c8437f35
AD
642 return false;
643
644 }
645
e6cb77a0
AD
646 function make_password($length = 8) {
647
648 $password = "";
649 $possible = "0123456789bcdfghjkmnpqrstvwxyz";
650
651 $i = 0;
652
653 while ($i < $length) {
654 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
655
656 if (!strstr($password, $char)) {
657 $password .= $char;
658 $i++;
659 }
660 }
661 return $password;
662 }
663
664 // this is called after user is created to initialize default feeds, labels
665 // or whatever else
666
667 // user preferences are checked on every login, not here
668
669 function initialize_user($link, $uid) {
670
671 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
672 values ('$uid','unread = true', 'Unread articles')");
673
674 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
675 values ('$uid','last_read is null and unread = false', 'Updated articles')");
676
677 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 678 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 679 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
680
681 }
e6cb77a0 682
b8aa49bc 683 function logout_user() {
f557cd78 684 session_destroy();
b8aa49bc
AD
685 }
686
687 function login_sequence($link) {
688 if (!SINGLE_USER_MODE) {
689
690 if (!USE_HTTP_AUTH) {
691 if (!$_SESSION["uid"]) {
692 header("Location: login.php?rt=tt-rss.php");
693 exit;
694 }
695 } else {
f557cd78
AD
696 if (!$_SESSION["uid"]) {
697 if (!$_SERVER["PHP_AUTH_USER"]) {
698
699 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
700 header('HTTP/1.0 401 Unauthorized');
701 exit;
702
703 } else {
704 $auth_result = authenticate_user($link,
705 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
706
707 if (!$auth_result) {
708 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
709 header('HTTP/1.0 401 Unauthorized');
710 exit;
711 }
712 }
713 }
b8aa49bc
AD
714 }
715 } else {
716 $_SESSION["uid"] = 1;
717 $_SESSION["name"] = "admin";
c7a03b7a 718 initialize_user_prefs($link, 1);
b8aa49bc
AD
719 }
720 }
3547842a
AD
721
722 function truncate_string($str, $max_len) {
723 if (strlen($str) > $max_len) {
724 return substr($str, 0, $max_len) . "...";
725 } else {
726 return $str;
727 }
728 }
54a60e1a
AD
729
730 function get_user_theme_path($link) {
731 $result = db_query($link, "SELECT theme_path FROM ttrss_themes
732 WHERE id = (SELECT theme_id FROM ttrss_users
733 WHERE id = " . $_SESSION["uid"] . ")");
734 if (db_num_rows($result) != 0) {
735 return db_fetch_result($result, 0, "theme_path");
736 } else {
737 return null;
738 }
739 }
40d13c28 740?>