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