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