]> git.wh0rd.org - tt-rss.git/blame - functions.php
ttrss_feeds: add private column to hide from browser
[tt-rss.git] / functions.php
CommitLineData
40d13c28 1<?
f1a80dae 2
cce28758
AD
3 if ($_GET["debug"]) {
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
5 } else {
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
7 }
8
40d13c28 9 require_once 'config.php';
b619ff15 10 require_once 'db-prefs.php';
40d13c28 11
387234f3
AD
12 require_once 'magpierss/rss_utils.inc';
13
a3ee2a38
AD
14 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
15
fefa6ca3 16 function purge_feed($link, $feed_id, $purge_interval) {
4c193675 17
fefa6ca3 18 if (DB_TYPE == "pgsql") {
35d8cf43 19 db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 20 marked = false AND feed_id = '$feed_id' AND
35d8cf43
AD
21 (SELECT date_entered FROM ttrss_entries WHERE
22 id = ref_id) < NOW() - INTERVAL '$purge_interval days'");
fefa6ca3 23 } else {
35d8cf43 24 db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 25 marked = false AND feed_id = '$feed_id' AND
35d8cf43
AD
26 (SELECT date_entered FROM ttrss_entries WHERE
27 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
fefa6ca3
AD
28 }
29 }
30
31 function global_purge_old_posts($link, $do_output = false) {
32
33 $result = db_query($link,
34 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds");
35
36 while ($line = db_fetch_assoc($result)) {
37
38 $feed_id = $line["id"];
39 $purge_interval = $line["purge_interval"];
40 $owner_uid = $line["owner_uid"];
41
42 if ($purge_interval == 0) {
43
44 $tmp_result = db_query($link,
45 "SELECT value FROM ttrss_user_prefs WHERE
46 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
47
48 if (db_num_rows($tmp_result) != 0) {
49 $purge_interval = db_fetch_result($tmp_result, 0, "value");
50 }
51 }
52
53 if ($do_output) {
54 print "<feed id='$feed_id' p_intl='$purge_interval'/>";
55 }
56
57 if ($purge_interval > 0) {
58 purge_feed($link, $feed_id, $purge_interval);
59 }
60 }
61
71604ca4
AD
62 // purge orphaned posts in main content table
63 db_query($link, "DELETE FROM ttrss_entries WHERE
64 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
65
fefa6ca3
AD
66 }
67
b6eefba5 68 function purge_old_posts($link) {
5d73494a 69
f1a80dae
AD
70 $user_id = $_SESSION["uid"];
71
72 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
73 WHERE owner_uid = '$user_id'");
5d73494a
AD
74
75 while ($line = db_fetch_assoc($result)) {
76
77 $feed_id = $line["id"];
78 $purge_interval = $line["purge_interval"];
79
b619ff15 80 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 81
140aae81 82 if ($purge_interval > 0) {
fefa6ca3 83 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
84 }
85 }
71604ca4
AD
86
87 // purge orphaned posts in main content table
88 db_query($link, "DELETE FROM ttrss_entries WHERE
89 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
90 }
91
1f2b01ed 92 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 93
4769ddaf 94 if (WEB_DEMO_MODE) return;
b0b4abcf 95
a2770077
AD
96 if (!$user_id) {
97 $user_id = $_SESSION["uid"];
98 purge_old_posts($link);
99 }
100
25af8dad 101// db_query($link, "BEGIN");
b82af8c3 102
d148926e 103 $result = db_query($link, "SELECT feed_url,id,
798f722b 104 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd
AD
105 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
106 ORDER BY last_updated DESC");
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
1f2b01ed 118 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
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
47c6c988 173 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
a88c1f36
AD
174 FROM ttrss_feeds WHERE id = '$feed'");
175
47c6c988
AD
176 $auth_login = db_fetch_result($result, 0, "auth_login");
177 $auth_pass = db_fetch_result($result, 0, "auth_pass");
178
a88c1f36
AD
179 $update_interval = db_fetch_result($result, 0, "update_interval");
180
181 if ($update_interval < 0) { return; }
182
ab3d0b99
AD
183 $feed = db_escape_string($feed);
184
47c6c988
AD
185 $fetch_url = $feed_url;
186
187 if ($auth_login && $auth_pass) {
188 $url_parts = array();
189 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
190
191 if ($url_parts[1] && $url_parts[2]) {
192 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
193 }
194
195 }
3ad5aa85 196 error_reporting(0);
47c6c988 197 $rss = fetch_rss($fetch_url);
ab3d0b99 198
cce28758 199 error_reporting (DEFAULT_ERROR_LEVEL);
76798ff3 200
b6eefba5 201 $feed = db_escape_string($feed);
dcee8f61 202
40d13c28 203 if ($rss) {
b82af8c3 204
dd8c76a9
AD
205 db_query($link, "BEGIN");
206
a88c1f36 207 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 208 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 209
b6eefba5
AD
210 $registered_title = db_fetch_result($result, 0, "title");
211 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 212 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 213
7fed1940
AD
214 $owner_uid = db_fetch_result($result, 0, "owner_uid");
215
a2770077
AD
216 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
217 check_feed_favicon($feed_url, $feed, $link);
218 }
219
746b249f 220 if (!$registered_title || $registered_title == "[Unknown]") {
e1305a97 221 $feed_title = db_escape_string($rss->channel["title"]);
f324892e
AD
222 db_query($link, "UPDATE ttrss_feeds SET
223 title = '$feed_title' WHERE id = '$feed'");
224 }
225
147f7691 226 $site_url = $rss->channel["link"];
832b7bfc
AD
227 // weird, weird Magpie
228 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
147f7691
AD
229
230 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
231 db_query($link, "UPDATE ttrss_feeds SET
232 site_url = '$site_url' WHERE id = '$feed'");
331900c6 233 }
40d13c28 234
b7f4bda2
AD
235// print "I: " . $rss->channel["image"]["url"];
236
237 $icon_url = $rss->image["url"];
238
147f7691 239 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
240 $icon_url = db_escape_string($icon_url);
241 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
242 }
243
e6155a06
AD
244
245 $filters = array();
246
4b3dff6e 247 $result = db_query($link, "SELECT reg_exp,
db42b934
AD
248 ttrss_filter_types.name AS name,
249 ttrss_filter_actions.name AS action
250 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
251 owner_uid = $owner_uid AND
252 ttrss_filter_types.id = filter_type AND
253 ttrss_filter_actions.id = action_id AND
ead60402 254 (feed_id IS NULL OR feed_id = '$feed')");
e6155a06 255
b6eefba5 256 while ($line = db_fetch_assoc($result)) {
e6155a06 257 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
258
259 $filter["reg_exp"] = $line["reg_exp"];
260 $filter["action"] = $line["action"];
261
262 array_push($filters[$line["name"]], $filter);
e6155a06
AD
263 }
264
ddb68b81
AD
265 $iterator = $rss->items;
266
267 if (!$iterator) $iterator = $rss->entries;
268 if (!$iterator) $iterator = $rss;
269
270 foreach ($iterator as $item) {
40d13c28
AD
271
272 $entry_guid = $item["id"];
273
274 if (!$entry_guid) $entry_guid = $item["guid"];
275 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
276
277 if (!$entry_guid) continue;
a116f569 278
9c9c7e6b 279 $entry_timestamp = "";
b82af8c3 280
9c9c7e6b
AD
281 $rss_2_date = $item['pubdate'];
282 $rss_1_date = $item['dc']['date'];
283 $atom_date = $item['issued'];
59ba2c75 284 if (!$atom_date) $atom_date = $item['updated'];
b197f117 285
9c9c7e6b
AD
286 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
287 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
288 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
289
290 if ($entry_timestamp == "") {
291 $entry_timestamp = time();
292 $no_orig_date = 'true';
466001c4
AD
293 } else {
294 $no_orig_date = 'false';
b82af8c3 295 }
b197f117 296
466001c4 297 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 298
40d13c28 299 $entry_title = $item["title"];
ddb68b81
AD
300
301 // strange Magpie workaround
302 $entry_link = $item["link_"];
303 if (!$entry_link) $entry_link = $item["link"];
71ad3959
AD
304
305 if (!$entry_title) continue;
306 if (!$entry_link) continue;
307
1696229f
AD
308 $entry_content = $item["content:escaped"];
309
310 if (!$entry_content) $entry_content = $item["content:encoded"];
40d13c28 311 if (!$entry_content) $entry_content = $item["content"];
372ced8b 312 if (!$entry_content) $entry_content = $item["summary"];
1696229f 313 if (!$entry_content) $entry_content = $item["description"];
a2015351 314
a116f569 315// if (!$entry_content) continue;
a2015351 316
8add756a
AD
317 // WTF
318 if (is_array($entry_content)) {
319 $entry_content = $entry_content["encoded"];
1696229f 320 if (!$entry_content) $entry_content = $entry_content["escaped"];
8add756a
AD
321 }
322
1696229f 323// print_r($item);
372ced8b
AD
324// print_r(htmlspecialchars($entry_content));
325// print "<br>";
1696229f 326
372ced8b 327 $entry_content_unescaped = $entry_content;
466001c4 328 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 329
a1ea1e12
AD
330 $entry_comments = $item["comments"];
331
b6eefba5 332 $entry_guid = db_escape_string($entry_guid);
2651fc4f 333
05732aa0
AD
334 $result = db_query($link, "SELECT id FROM ttrss_entries
335 WHERE guid = '$entry_guid'");
4c193675 336
b17fcb1a
AD
337 $entry_content = db_escape_string($entry_content);
338 $entry_title = db_escape_string($entry_title);
339 $entry_link = db_escape_string($entry_link);
340 $entry_comments = db_escape_string($entry_comments);
341
27f089dc 342 $num_comments = db_escape_string($item["slash"]["comments"]);
11b0dce2 343
e31073bd
AD
344 if (!$num_comments) $num_comments = 0;
345
4c193675
AD
346 if (db_num_rows($result) == 0) {
347
348 // base post entry does not exist, create it
349
4c193675
AD
350 $result = db_query($link,
351 "INSERT INTO ttrss_entries
352 (title,
353 guid,
354 link,
355 updated,
356 content,
357 content_hash,
358 no_orig_date,
359 date_entered,
11b0dce2
AD
360 comments,
361 num_comments)
4c193675
AD
362 VALUES
363 ('$entry_title',
364 '$entry_guid',
365 '$entry_link',
366 '$entry_timestamp_fmt',
367 '$entry_content',
368 '$content_hash',
369 $no_orig_date,
370 NOW(),
11b0dce2
AD
371 '$entry_comments',
372 '$num_comments')");
8926aab8
AD
373 } else {
374 // we keep encountering the entry in feeds, so we need to
375 // update date_entered column so that we don't get horrible
376 // dupes when the entry gets purged and reinserted again e.g.
377 // in the case of SLOW SLOW OMG SLOW updating feeds
378
379 $base_entry_id = db_fetch_result($result, 0, "id");
380
381 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
382 WHERE id = '$base_entry_id'");
4c193675
AD
383 }
384
385 // now it should exist, if not - bad luck then
386
6385315d
AD
387 $result = db_query($link, "SELECT
388 id,content_hash,no_orig_date,title,
8926aab8 389 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
390 substring(updated,1,19) as updated,
391 num_comments
6385315d
AD
392 FROM
393 ttrss_entries
394 WHERE guid = '$entry_guid'");
4c193675
AD
395
396 if (db_num_rows($result) == 1) {
397
11b0dce2
AD
398 // this will be used below in update handler
399 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
400 $orig_title = db_fetch_result($result, 0, "title");
401 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
402 $orig_date_entered = strtotime(db_fetch_result($result,
403 0, "date_entered"));
6385315d 404
11b0dce2 405 $ref_id = db_fetch_result($result, 0, "id");
4c193675 406
11b0dce2 407 // check for user post link to main table
4c193675 408
11b0dce2
AD
409 // do we allow duplicate posts with same GUID in different feeds?
410 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
411 $dupcheck_qpart = "AND feed_id = '$feed'";
412 } else {
413 $dupcheck_qpart = "";
414 }
71604ca4 415
11b0dce2 416// error_reporting(0);
19c9cb11 417
11b0dce2
AD
418 $filter_name = get_filter_name($entry_title, $entry_content,
419 $entry_link, $filters);
19c9cb11 420
11b0dce2
AD
421 if ($filter_name == "filter") {
422 continue;
423 }
19c9cb11 424
11b0dce2 425// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 426
11b0dce2
AD
427 $result = db_query($link,
428 "SELECT ref_id FROM ttrss_user_entries WHERE
429 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
430 $dupcheck_qpart");
431
432 // okay it doesn't exist - create user entry
433 if (db_num_rows($result) == 0) {
434
435 if ($filter_name != 'catchup') {
436 $unread = 'true';
437 $last_read_qpart = 'NULL';
438 } else {
439 $unread = 'false';
440 $last_read_qpart = 'NOW()';
441 }
19c9cb11 442
11b0dce2
AD
443 $result = db_query($link,
444 "INSERT INTO ttrss_user_entries
445 (ref_id, owner_uid, feed_id, unread, last_read)
446 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
447 $last_read_qpart)");
448 }
449
6385315d
AD
450 $post_needs_update = false;
451
a2770077 452 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
6385315d
AD
453 ($content_hash != $orig_content_hash)) {
454 $post_needs_update = true;
455 }
456
457 if ($orig_title != $entry_title) {
458 $post_needs_update = true;
459 }
460
11b0dce2
AD
461 if ($orig_num_comments != $num_comments) {
462 $post_needs_update = true;
463 }
464
6385315d
AD
465// this doesn't seem to be very reliable
466//
467// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
468// $post_needs_update = true;
469// }
470
471 // if post needs update, update it and mark all user entries
1c73bc0c 472 // linking to this post as updated
6385315d
AD
473 if ($post_needs_update) {
474
475// print "<!-- post $orig_title needs update : $post_needs_update -->";
476
6385315d 477 db_query($link, "UPDATE ttrss_entries
11b0dce2
AD
478 SET title = '$entry_title', content = '$entry_content',
479 num_comments = '$num_comments'
6385315d
AD
480 WHERE id = '$ref_id'");
481
482 db_query($link, "UPDATE ttrss_user_entries
483 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
484
485 }
4c193675
AD
486 }
487
eb36b4eb
AD
488 /* taaaags */
489 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
490
05732aa0 491 $entry_tags = null;
eb36b4eb 492
372ced8b 493 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
ee2c3050
AD
494 $entry_content_unescaped, $entry_tags);
495
496// print "<br>$entry_title : $entry_content_unescaped<br>";
497// print_r($entry_tags);
372ced8b 498// print "<br>";
eb36b4eb
AD
499
500 $entry_tags = $entry_tags[1];
501
502 if (count($entry_tags) > 0) {
503
05732aa0
AD
504 $result = db_query($link, "SELECT id,int_id
505 FROM ttrss_entries,ttrss_user_entries
25da6909 506 WHERE guid = '$entry_guid'
05732aa0 507 AND feed_id = '$feed' AND ref_id = id
7fed1940 508 AND owner_uid = '$owner_uid'");
eb36b4eb 509
fe99ab12 510 if (db_num_rows($result) == 1) {
eb36b4eb 511
fe99ab12
AD
512 $entry_id = db_fetch_result($result, 0, "id");
513 $entry_int_id = db_fetch_result($result, 0, "int_id");
514
515 foreach ($entry_tags as $tag) {
516 $tag = db_escape_string(strtolower($tag));
31483fc1
AD
517
518 $tag = str_replace("+", " ", $tag);
fe99ab12
AD
519 $tag = str_replace("technorati tag: ", "", $tag);
520
521 $result = db_query($link, "SELECT id FROM ttrss_tags
522 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
523 owner_uid = '$owner_uid' LIMIT 1");
524
525 // print db_fetch_result($result, 0, "id");
526
527 if ($result && db_num_rows($result) == 0) {
528
529 // print "tagging $entry_id as $tag<br>";
530
531 db_query($link, "INSERT INTO ttrss_tags
532 (owner_uid,tag_name,post_int_id)
533 VALUES ('$owner_uid','$tag', '$entry_int_id')");
534 }
535 }
eb36b4eb 536 }
05732aa0 537 }
4c193675 538 }
40d13c28 539
ab3d0b99
AD
540 db_query($link, "UPDATE ttrss_feeds
541 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 542
dd8c76a9
AD
543 db_query($link, "COMMIT");
544
ab3d0b99
AD
545 } else {
546 $error_msg = db_escape_string(magpie_error());
547 db_query($link,
aa5f9f5f
AD
548 "UPDATE ttrss_feeds SET last_error = '$error_msg',
549 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
550 }
551
552 }
553
f175937c
AD
554 function print_select($id, $default, $values, $attributes = "") {
555 print "<select id=\"$id\" $attributes>";
a0d53889
AD
556 foreach ($values as $v) {
557 if ($v == $default)
558 $sel = " selected";
559 else
560 $sel = "";
561
562 print "<option$sel>$v</option>";
563 }
564 print "</select>";
565 }
40d13c28 566
19c9cb11 567 function get_filter_name($title, $content, $link, $filters) {
e6155a06
AD
568
569 if ($filters["title"]) {
19c9cb11
AD
570 foreach ($filters["title"] as $filter) {
571 $reg_exp = $filter["reg_exp"];
572 if (preg_match("/$reg_exp/i", $title)) {
573 return $filter["action"];
574 }
e6155a06
AD
575 }
576 }
577
578 if ($filters["content"]) {
19c9cb11
AD
579 foreach ($filters["content"] as $filter) {
580 $reg_exp = $filter["reg_exp"];
581 if (preg_match("/$reg_exp/i", $content)) {
582 return $filter["action"];
583 }
e6155a06
AD
584 }
585 }
586
587 if ($filters["both"]) {
588 foreach ($filters["both"] as $filter) {
19c9cb11
AD
589 $reg_exp = $filter["reg_exp"];
590 if (preg_match("/$reg_exp/i", $title) ||
591 preg_match("/$reg_exp/i", $content)) {
592 return $filter["action"];
593 }
e6155a06
AD
594 }
595 }
596
3a933f22 597 if ($filters["link"]) {
19c9cb11
AD
598 $reg_exp = $filter["reg_exp"];
599 foreach ($filters["link"] as $filter) {
600 $reg_exp = $filter["reg_exp"];
601 if (preg_match("/$reg_exp/i", $link)) {
602 return $filter["action"];
603 }
3a933f22
AD
604 }
605 }
606
e6155a06
AD
607 return false;
608 }
609
4668523d 610 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) {
254e0e4b
AD
611
612 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 613 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 614 } else {
023fe037 615 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
616 }
617
8143ae1f 618 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
254e0e4b
AD
619
620 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 621 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
622 print "$feed_icon";
623 }
624
625 print "<span id=\"FEEDN-$feed_id\">$feed</span>";
626
627 if ($unread != 0) {
628 $fctr_class = "";
629 } else {
630 $fctr_class = "class=\"invisible\"";
631 }
632
633 print "<span $fctr_class id=\"FEEDCTR-$feed_id\">
634 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
635
636 print "</li>";
637
638 }
639
406d9489
AD
640 function getmicrotime() {
641 list($usec, $sec) = explode(" ",microtime());
642 return ((float)$usec + (float)$sec);
643 }
644
77e96719
AD
645 function print_radio($id, $default, $values, $attributes = "") {
646 foreach ($values as $v) {
647
648 if ($v == $default)
5da169d9 649 $sel = "checked";
77e96719 650 else
5da169d9
AD
651 $sel = "";
652
653 if ($v == "Yes") {
654 $sel .= " value=\"1\"";
655 } else {
656 $sel .= " value=\"0\"";
657 }
77e96719
AD
658
659 print "<input type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
660
661 }
662 }
663
ff485f1d
AD
664 function initialize_user_prefs($link, $uid) {
665
666 $uid = db_escape_string($uid);
667
668 db_query($link, "BEGIN");
669
670 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
671
672 $u_result = db_query($link, "SELECT pref_name
673 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
674
675 $active_prefs = array();
676
677 while ($line = db_fetch_assoc($u_result)) {
678 array_push($active_prefs, $line["pref_name"]);
679 }
680
681 while ($line = db_fetch_assoc($result)) {
682 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
683// print "adding " . $line["pref_name"] . "<br>";
684
685 db_query($link, "INSERT INTO ttrss_user_prefs
686 (owner_uid,pref_name,value) VALUES
687 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
688
689 }
690 }
691
692 db_query($link, "COMMIT");
693
694 }
c8437f35
AD
695
696 function authenticate_user($link, $login, $password) {
697
698 $pwd_hash = 'SHA1:' . sha1($password);
699
203b6d25 700 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
09829e2a
AD
701 login = '$login' AND ((pwd_hash = '$password' AND '$password' = 'password')
702 OR pwd_hash = '$pwd_hash')");
c8437f35
AD
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
503eb349
AD
712 $user_theme = get_user_theme_path($link);
713
714 $_SESSION["theme"] = $user_theme;
715
f557cd78
AD
716 initialize_user_prefs($link, $_SESSION["uid"]);
717
c8437f35
AD
718 return true;
719 }
ff485f1d 720
c8437f35
AD
721 return false;
722
723 }
724
e6cb77a0
AD
725 function make_password($length = 8) {
726
727 $password = "";
798f722b
AD
728 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
729
730 $i = 0;
e6cb77a0
AD
731
732 while ($i < $length) {
733 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
734
735 if (!strstr($password, $char)) {
736 $password .= $char;
737 $i++;
738 }
739 }
740 return $password;
741 }
742
743 // this is called after user is created to initialize default feeds, labels
744 // or whatever else
745
746 // user preferences are checked on every login, not here
747
748 function initialize_user($link, $uid) {
749
750 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
751 values ('$uid','unread = true', 'Unread articles')");
752
753 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
754 values ('$uid','last_read is null and unread = false', 'Updated articles')");
755
756 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 757 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 758 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
759
760 }
e6cb77a0 761
b8aa49bc 762 function logout_user() {
f557cd78 763 session_destroy();
b8aa49bc
AD
764 }
765
75836f33 766 function get_script_urlpath() {
87a79fa4 767 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
768 }
769
770 function get_login_redirect() {
771 $server = $_SERVER["SERVER_NAME"];
772
773 if (ENABLE_LOGIN_SSL) {
774 $protocol = "https";
775 } else {
776 $protocol = "http";
777 }
778
779 $url_path = get_script_urlpath();
780
781 $redirect_uri = "$protocol://$server$url_path/login.php";
782
783 return $redirect_uri;
784 }
785
b8aa49bc
AD
786 function login_sequence($link) {
787 if (!SINGLE_USER_MODE) {
75836f33 788
b8aa49bc
AD
789 if (!USE_HTTP_AUTH) {
790 if (!$_SESSION["uid"]) {
75836f33 791 $redirect_uri = get_login_redirect();
e31dca14
AD
792 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
793 header("Location: $redirect_uri?rt=$return_to");
b8aa49bc
AD
794 exit;
795 }
796 } else {
f557cd78
AD
797 if (!$_SESSION["uid"]) {
798 if (!$_SERVER["PHP_AUTH_USER"]) {
799
800 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
801 header('HTTP/1.0 401 Unauthorized');
802 exit;
803
804 } else {
805 $auth_result = authenticate_user($link,
806 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
807
808 if (!$auth_result) {
809 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
810 header('HTTP/1.0 401 Unauthorized');
811 exit;
812 }
813 }
814 }
b8aa49bc
AD
815 }
816 } else {
817 $_SESSION["uid"] = 1;
818 $_SESSION["name"] = "admin";
c7a03b7a 819 initialize_user_prefs($link, 1);
b8aa49bc
AD
820 }
821 }
3547842a
AD
822
823 function truncate_string($str, $max_len) {
824 if (strlen($str) > $max_len) {
825 return substr($str, 0, $max_len) . "...";
826 } else {
827 return $str;
828 }
829 }
54a60e1a
AD
830
831 function get_user_theme_path($link) {
798f722b
AD
832 $result = db_query($link, "SELECT theme_path
833 FROM
834 ttrss_themes,ttrss_users
835 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
836 if (db_num_rows($result) != 0) {
837 return db_fetch_result($result, 0, "theme_path");
838 } else {
839 return null;
840 }
841 }
be773442
AD
842
843 function smart_date_time($timestamp) {
844 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
845 return date("G:i", $timestamp);
f26450f1 846 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
847 return date("M d, G:i", $timestamp);
848 } else {
849 return date("Y/m/d G:i");
850 }
851 }
852
853 function smart_date($timestamp) {
854 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
855 return "Today";
f26450f1 856 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
857 return date("D m", $timestamp);
858 } else {
859 return date("Y/m/d");
860 }
861 }
a654a595
AD
862
863 function sql_bool_to_string($s) {
864 if ($s == "t" || $s == "1") {
865 return "true";
866 } else {
867 return "false";
868 }
869 }
0ea4fb50
AD
870
871 function toggleEvenOdd($a) {
872 if ($a == "even")
873 return "odd";
874 else
875 return "even";
876 }
6043fb7e
AD
877
878 function sanity_check($link) {
9cbca41f 879
6043fb7e
AD
880 $error_code = 0;
881 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
882 $schema_version = db_fetch_result($result, 0, "schema_version");
883
884 if ($schema_version != SCHEMA_VERSION) {
885 $error_code = 5;
886 }
887
6043fb7e 888 if ($error_code != 0) {
9cbca41f 889 print "<error error-code='$error_code'/>";
6043fb7e
AD
890 return false;
891 } else {
892 return true;
9cbca41f 893 }
6043fb7e
AD
894 }
895
40d13c28 896?>