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