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