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