]> git.wh0rd.org - tt-rss.git/blame - functions.php
added compatibility layer for mb_* functions (from Typo3, patch from Guillaume Bour)
[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';
5bc0bd27 11 require_once 'compat.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
1f2b01ed 93 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = 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 104 $result = db_query($link, "SELECT feed_url,id,
798f722b 105 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd
AD
106 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
107 ORDER BY last_updated DESC");
40d13c28 108
b6eefba5 109 while ($line = db_fetch_assoc($result)) {
d148926e
AD
110 $upd_intl = $line["update_interval"];
111
b619ff15 112 if (!$upd_intl || $upd_intl == 0) {
a2770077 113 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
b619ff15 114 }
d148926e 115
93d40f50
AD
116 if ($fetch || (!$line["last_updated"] ||
117 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 118
1f2b01ed 119 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
d148926e 120 }
40d13c28
AD
121 }
122
25af8dad 123// db_query($link, "COMMIT");
b82af8c3 124
40d13c28
AD
125 }
126
9e997874 127 function check_feed_favicon($feed_url, $feed, $link) {
78800912
AD
128 $feed_url = str_replace("http://", "", $feed_url);
129 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
130
131 $icon_url = "http://$feed_url/favicon.ico";
273a2f6b 132 $icon_file = ICONS_DIR . "/$feed.ico";
78800912
AD
133
134 if (!file_exists($icon_file)) {
e695fdc8 135
78800912
AD
136 error_reporting(0);
137 $r = fopen($icon_url, "r");
cce28758 138 error_reporting (DEFAULT_ERROR_LEVEL);
78800912
AD
139
140 if ($r) {
141 $tmpfname = tempnam("/tmp", "ttrssicon");
142
143 $t = fopen($tmpfname, "w");
144
145 while (!feof($r)) {
146 $buf = fread($r, 16384);
147 fwrite($t, $buf);
148 }
149
150 fclose($r);
151 fclose($t);
152
e695fdc8
AD
153 error_reporting(0);
154 if (!rename($tmpfname, $icon_file)) {
155 unlink($tmpfname);
156 }
717f5e64
AD
157
158 chmod($icon_file, 0644);
159
cce28758 160 error_reporting (DEFAULT_ERROR_LEVEL);
78800912
AD
161
162 }
163 }
164 }
165
ddb68b81 166 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 167
4769ddaf 168 if (WEB_DEMO_MODE) return;
b0b4abcf 169
ddb68b81 170 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
171 return;
172 }
173
47c6c988 174 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
a88c1f36
AD
175 FROM ttrss_feeds WHERE id = '$feed'");
176
47c6c988
AD
177 $auth_login = db_fetch_result($result, 0, "auth_login");
178 $auth_pass = db_fetch_result($result, 0, "auth_pass");
179
a88c1f36
AD
180 $update_interval = db_fetch_result($result, 0, "update_interval");
181
182 if ($update_interval < 0) { return; }
183
ab3d0b99
AD
184 $feed = db_escape_string($feed);
185
47c6c988
AD
186 $fetch_url = $feed_url;
187
188 if ($auth_login && $auth_pass) {
189 $url_parts = array();
190 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
191
192 if ($url_parts[1] && $url_parts[2]) {
193 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
194 }
195
196 }
3ad5aa85 197 error_reporting(0);
47c6c988 198 $rss = fetch_rss($fetch_url);
ab3d0b99 199
cce28758 200 error_reporting (DEFAULT_ERROR_LEVEL);
76798ff3 201
b6eefba5 202 $feed = db_escape_string($feed);
dcee8f61 203
40d13c28 204 if ($rss) {
b82af8c3 205
dd8c76a9
AD
206 db_query($link, "BEGIN");
207
a88c1f36 208 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 209 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 210
b6eefba5
AD
211 $registered_title = db_fetch_result($result, 0, "title");
212 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 213 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 214
7fed1940
AD
215 $owner_uid = db_fetch_result($result, 0, "owner_uid");
216
a2770077
AD
217 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
218 check_feed_favicon($feed_url, $feed, $link);
219 }
220
746b249f 221 if (!$registered_title || $registered_title == "[Unknown]") {
e1305a97 222 $feed_title = db_escape_string($rss->channel["title"]);
f324892e
AD
223 db_query($link, "UPDATE ttrss_feeds SET
224 title = '$feed_title' WHERE id = '$feed'");
225 }
226
147f7691 227 $site_url = $rss->channel["link"];
832b7bfc
AD
228 // weird, weird Magpie
229 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
147f7691
AD
230
231 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
232 db_query($link, "UPDATE ttrss_feeds SET
233 site_url = '$site_url' WHERE id = '$feed'");
331900c6 234 }
40d13c28 235
b7f4bda2
AD
236// print "I: " . $rss->channel["image"]["url"];
237
238 $icon_url = $rss->image["url"];
239
147f7691 240 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
241 $icon_url = db_escape_string($icon_url);
242 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
243 }
244
e6155a06
AD
245
246 $filters = array();
247
4b3dff6e 248 $result = db_query($link, "SELECT reg_exp,
db42b934
AD
249 ttrss_filter_types.name AS name,
250 ttrss_filter_actions.name AS action
251 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
252 owner_uid = $owner_uid AND
253 ttrss_filter_types.id = filter_type AND
254 ttrss_filter_actions.id = action_id AND
ead60402 255 (feed_id IS NULL OR feed_id = '$feed')");
e6155a06 256
b6eefba5 257 while ($line = db_fetch_assoc($result)) {
e6155a06 258 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
259
260 $filter["reg_exp"] = $line["reg_exp"];
261 $filter["action"] = $line["action"];
262
263 array_push($filters[$line["name"]], $filter);
e6155a06
AD
264 }
265
ddb68b81
AD
266 $iterator = $rss->items;
267
268 if (!$iterator) $iterator = $rss->entries;
269 if (!$iterator) $iterator = $rss;
270
271 foreach ($iterator as $item) {
40d13c28
AD
272
273 $entry_guid = $item["id"];
274
275 if (!$entry_guid) $entry_guid = $item["guid"];
276 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
277
278 if (!$entry_guid) continue;
a116f569 279
9c9c7e6b 280 $entry_timestamp = "";
b82af8c3 281
9c9c7e6b
AD
282 $rss_2_date = $item['pubdate'];
283 $rss_1_date = $item['dc']['date'];
284 $atom_date = $item['issued'];
59ba2c75 285 if (!$atom_date) $atom_date = $item['updated'];
b197f117 286
9c9c7e6b
AD
287 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
288 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
289 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
290
291 if ($entry_timestamp == "") {
292 $entry_timestamp = time();
293 $no_orig_date = 'true';
466001c4
AD
294 } else {
295 $no_orig_date = 'false';
b82af8c3 296 }
b197f117 297
466001c4 298 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 299
40d13c28 300 $entry_title = $item["title"];
ddb68b81
AD
301
302 // strange Magpie workaround
303 $entry_link = $item["link_"];
304 if (!$entry_link) $entry_link = $item["link"];
71ad3959
AD
305
306 if (!$entry_title) continue;
307 if (!$entry_link) continue;
308
1696229f
AD
309 $entry_content = $item["content:escaped"];
310
311 if (!$entry_content) $entry_content = $item["content:encoded"];
40d13c28 312 if (!$entry_content) $entry_content = $item["content"];
372ced8b 313 if (!$entry_content) $entry_content = $item["summary"];
1696229f 314 if (!$entry_content) $entry_content = $item["description"];
a2015351 315
a116f569 316// if (!$entry_content) continue;
a2015351 317
8add756a
AD
318 // WTF
319 if (is_array($entry_content)) {
320 $entry_content = $entry_content["encoded"];
1696229f 321 if (!$entry_content) $entry_content = $entry_content["escaped"];
8add756a
AD
322 }
323
1696229f 324// print_r($item);
372ced8b
AD
325// print_r(htmlspecialchars($entry_content));
326// print "<br>";
1696229f 327
372ced8b 328 $entry_content_unescaped = $entry_content;
466001c4 329 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 330
a1ea1e12
AD
331 $entry_comments = $item["comments"];
332
b6eefba5 333 $entry_guid = db_escape_string($entry_guid);
2651fc4f 334
05732aa0
AD
335 $result = db_query($link, "SELECT id FROM ttrss_entries
336 WHERE guid = '$entry_guid'");
4c193675 337
b17fcb1a
AD
338 $entry_content = db_escape_string($entry_content);
339 $entry_title = db_escape_string($entry_title);
340 $entry_link = db_escape_string($entry_link);
341 $entry_comments = db_escape_string($entry_comments);
342
27f089dc 343 $num_comments = db_escape_string($item["slash"]["comments"]);
11b0dce2 344
e31073bd
AD
345 if (!$num_comments) $num_comments = 0;
346
4c193675
AD
347 if (db_num_rows($result) == 0) {
348
349 // base post entry does not exist, create it
350
4c193675
AD
351 $result = db_query($link,
352 "INSERT INTO ttrss_entries
353 (title,
354 guid,
355 link,
356 updated,
357 content,
358 content_hash,
359 no_orig_date,
360 date_entered,
11b0dce2
AD
361 comments,
362 num_comments)
4c193675
AD
363 VALUES
364 ('$entry_title',
365 '$entry_guid',
366 '$entry_link',
367 '$entry_timestamp_fmt',
368 '$entry_content',
369 '$content_hash',
370 $no_orig_date,
371 NOW(),
11b0dce2
AD
372 '$entry_comments',
373 '$num_comments')");
8926aab8
AD
374 } else {
375 // we keep encountering the entry in feeds, so we need to
376 // update date_entered column so that we don't get horrible
377 // dupes when the entry gets purged and reinserted again e.g.
378 // in the case of SLOW SLOW OMG SLOW updating feeds
379
380 $base_entry_id = db_fetch_result($result, 0, "id");
381
382 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
383 WHERE id = '$base_entry_id'");
4c193675
AD
384 }
385
386 // now it should exist, if not - bad luck then
387
6385315d
AD
388 $result = db_query($link, "SELECT
389 id,content_hash,no_orig_date,title,
8926aab8 390 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
391 substring(updated,1,19) as updated,
392 num_comments
6385315d
AD
393 FROM
394 ttrss_entries
395 WHERE guid = '$entry_guid'");
4c193675
AD
396
397 if (db_num_rows($result) == 1) {
398
11b0dce2
AD
399 // this will be used below in update handler
400 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
401 $orig_title = db_fetch_result($result, 0, "title");
402 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
403 $orig_date_entered = strtotime(db_fetch_result($result,
404 0, "date_entered"));
6385315d 405
11b0dce2 406 $ref_id = db_fetch_result($result, 0, "id");
4c193675 407
11b0dce2 408 // check for user post link to main table
4c193675 409
11b0dce2
AD
410 // do we allow duplicate posts with same GUID in different feeds?
411 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
412 $dupcheck_qpart = "AND feed_id = '$feed'";
413 } else {
414 $dupcheck_qpart = "";
415 }
71604ca4 416
11b0dce2 417// error_reporting(0);
19c9cb11 418
11b0dce2
AD
419 $filter_name = get_filter_name($entry_title, $entry_content,
420 $entry_link, $filters);
19c9cb11 421
11b0dce2
AD
422 if ($filter_name == "filter") {
423 continue;
424 }
19c9cb11 425
11b0dce2 426// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 427
11b0dce2
AD
428 $result = db_query($link,
429 "SELECT ref_id FROM ttrss_user_entries WHERE
430 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
431 $dupcheck_qpart");
432
433 // okay it doesn't exist - create user entry
434 if (db_num_rows($result) == 0) {
435
436 if ($filter_name != 'catchup') {
437 $unread = 'true';
438 $last_read_qpart = 'NULL';
439 } else {
440 $unread = 'false';
441 $last_read_qpart = 'NOW()';
442 }
19c9cb11 443
11b0dce2
AD
444 $result = db_query($link,
445 "INSERT INTO ttrss_user_entries
446 (ref_id, owner_uid, feed_id, unread, last_read)
447 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
448 $last_read_qpart)");
449 }
450
6385315d
AD
451 $post_needs_update = false;
452
a2770077 453 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
6385315d
AD
454 ($content_hash != $orig_content_hash)) {
455 $post_needs_update = true;
456 }
457
458 if ($orig_title != $entry_title) {
459 $post_needs_update = true;
460 }
461
11b0dce2
AD
462 if ($orig_num_comments != $num_comments) {
463 $post_needs_update = true;
464 }
465
6385315d
AD
466// this doesn't seem to be very reliable
467//
468// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
469// $post_needs_update = true;
470// }
471
472 // if post needs update, update it and mark all user entries
1c73bc0c 473 // linking to this post as updated
6385315d
AD
474 if ($post_needs_update) {
475
476// print "<!-- post $orig_title needs update : $post_needs_update -->";
477
6385315d 478 db_query($link, "UPDATE ttrss_entries
11b0dce2
AD
479 SET title = '$entry_title', content = '$entry_content',
480 num_comments = '$num_comments'
6385315d
AD
481 WHERE id = '$ref_id'");
482
483 db_query($link, "UPDATE ttrss_user_entries
484 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
485
486 }
4c193675
AD
487 }
488
eb36b4eb
AD
489 /* taaaags */
490 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
491
05732aa0 492 $entry_tags = null;
eb36b4eb 493
372ced8b 494 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
ee2c3050
AD
495 $entry_content_unescaped, $entry_tags);
496
497// print "<br>$entry_title : $entry_content_unescaped<br>";
498// print_r($entry_tags);
372ced8b 499// print "<br>";
eb36b4eb
AD
500
501 $entry_tags = $entry_tags[1];
502
503 if (count($entry_tags) > 0) {
504
05732aa0
AD
505 $result = db_query($link, "SELECT id,int_id
506 FROM ttrss_entries,ttrss_user_entries
25da6909 507 WHERE guid = '$entry_guid'
05732aa0 508 AND feed_id = '$feed' AND ref_id = id
7fed1940 509 AND owner_uid = '$owner_uid'");
eb36b4eb 510
fe99ab12 511 if (db_num_rows($result) == 1) {
eb36b4eb 512
fe99ab12
AD
513 $entry_id = db_fetch_result($result, 0, "id");
514 $entry_int_id = db_fetch_result($result, 0, "int_id");
515
516 foreach ($entry_tags as $tag) {
517 $tag = db_escape_string(strtolower($tag));
31483fc1
AD
518
519 $tag = str_replace("+", " ", $tag);
fe99ab12
AD
520 $tag = str_replace("technorati tag: ", "", $tag);
521
522 $result = db_query($link, "SELECT id FROM ttrss_tags
523 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
524 owner_uid = '$owner_uid' LIMIT 1");
525
526 // print db_fetch_result($result, 0, "id");
527
528 if ($result && db_num_rows($result) == 0) {
529
530 // print "tagging $entry_id as $tag<br>";
531
532 db_query($link, "INSERT INTO ttrss_tags
533 (owner_uid,tag_name,post_int_id)
534 VALUES ('$owner_uid','$tag', '$entry_int_id')");
535 }
536 }
eb36b4eb 537 }
05732aa0 538 }
4c193675 539 }
40d13c28 540
ab3d0b99
AD
541 db_query($link, "UPDATE ttrss_feeds
542 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 543
dd8c76a9
AD
544 db_query($link, "COMMIT");
545
ab3d0b99
AD
546 } else {
547 $error_msg = db_escape_string(magpie_error());
548 db_query($link,
aa5f9f5f
AD
549 "UPDATE ttrss_feeds SET last_error = '$error_msg',
550 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
551 }
552
553 }
554
f175937c
AD
555 function print_select($id, $default, $values, $attributes = "") {
556 print "<select id=\"$id\" $attributes>";
a0d53889
AD
557 foreach ($values as $v) {
558 if ($v == $default)
559 $sel = " selected";
560 else
561 $sel = "";
562
563 print "<option$sel>$v</option>";
564 }
565 print "</select>";
566 }
40d13c28 567
19c9cb11 568 function get_filter_name($title, $content, $link, $filters) {
e6155a06
AD
569
570 if ($filters["title"]) {
19c9cb11
AD
571 foreach ($filters["title"] as $filter) {
572 $reg_exp = $filter["reg_exp"];
573 if (preg_match("/$reg_exp/i", $title)) {
574 return $filter["action"];
575 }
e6155a06
AD
576 }
577 }
578
579 if ($filters["content"]) {
19c9cb11
AD
580 foreach ($filters["content"] as $filter) {
581 $reg_exp = $filter["reg_exp"];
582 if (preg_match("/$reg_exp/i", $content)) {
583 return $filter["action"];
584 }
e6155a06
AD
585 }
586 }
587
588 if ($filters["both"]) {
589 foreach ($filters["both"] as $filter) {
19c9cb11
AD
590 $reg_exp = $filter["reg_exp"];
591 if (preg_match("/$reg_exp/i", $title) ||
592 preg_match("/$reg_exp/i", $content)) {
593 return $filter["action"];
594 }
e6155a06
AD
595 }
596 }
597
3a933f22 598 if ($filters["link"]) {
19c9cb11
AD
599 $reg_exp = $filter["reg_exp"];
600 foreach ($filters["link"] as $filter) {
601 $reg_exp = $filter["reg_exp"];
602 if (preg_match("/$reg_exp/i", $link)) {
603 return $filter["action"];
604 }
3a933f22
AD
605 }
606 }
607
e6155a06
AD
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) {
023fe037 614 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 615 } else {
023fe037 616 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
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
09829e2a
AD
702 login = '$login' AND ((pwd_hash = '$password' AND '$password' = 'password')
703 OR pwd_hash = '$pwd_hash')");
c8437f35
AD
704
705 if (db_num_rows($result) == 1) {
706 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
707 $_SESSION["name"] = db_fetch_result($result, 0, "login");
203b6d25 708 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
c8437f35 709
f6f32198
AD
710 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
711 $_SESSION["uid"]);
712
503eb349
AD
713 $user_theme = get_user_theme_path($link);
714
715 $_SESSION["theme"] = $user_theme;
716
f557cd78
AD
717 initialize_user_prefs($link, $_SESSION["uid"]);
718
c8437f35
AD
719 return true;
720 }
ff485f1d 721
c8437f35
AD
722 return false;
723
724 }
725
e6cb77a0
AD
726 function make_password($length = 8) {
727
728 $password = "";
798f722b
AD
729 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
730
731 $i = 0;
e6cb77a0
AD
732
733 while ($i < $length) {
734 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
735
736 if (!strstr($password, $char)) {
737 $password .= $char;
738 $i++;
739 }
740 }
741 return $password;
742 }
743
744 // this is called after user is created to initialize default feeds, labels
745 // or whatever else
746
747 // user preferences are checked on every login, not here
748
749 function initialize_user($link, $uid) {
750
751 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
752 values ('$uid','unread = true', 'Unread articles')");
753
754 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
755 values ('$uid','last_read is null and unread = false', 'Updated articles')");
756
757 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 758 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 759 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
760
761 }
e6cb77a0 762
b8aa49bc 763 function logout_user() {
f557cd78 764 session_destroy();
b8aa49bc
AD
765 }
766
75836f33 767 function get_script_urlpath() {
87a79fa4 768 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
769 }
770
771 function get_login_redirect() {
772 $server = $_SERVER["SERVER_NAME"];
773
774 if (ENABLE_LOGIN_SSL) {
775 $protocol = "https";
776 } else {
777 $protocol = "http";
778 }
779
780 $url_path = get_script_urlpath();
781
782 $redirect_uri = "$protocol://$server$url_path/login.php";
783
784 return $redirect_uri;
785 }
786
b8aa49bc
AD
787 function login_sequence($link) {
788 if (!SINGLE_USER_MODE) {
75836f33 789
b8aa49bc
AD
790 if (!USE_HTTP_AUTH) {
791 if (!$_SESSION["uid"]) {
75836f33 792 $redirect_uri = get_login_redirect();
e31dca14
AD
793 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
794 header("Location: $redirect_uri?rt=$return_to");
b8aa49bc
AD
795 exit;
796 }
797 } else {
f557cd78
AD
798 if (!$_SESSION["uid"]) {
799 if (!$_SERVER["PHP_AUTH_USER"]) {
800
801 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
802 header('HTTP/1.0 401 Unauthorized');
803 exit;
804
805 } else {
806 $auth_result = authenticate_user($link,
807 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
808
809 if (!$auth_result) {
810 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
811 header('HTTP/1.0 401 Unauthorized');
812 exit;
813 }
814 }
815 }
b8aa49bc
AD
816 }
817 } else {
818 $_SESSION["uid"] = 1;
819 $_SESSION["name"] = "admin";
c7a03b7a 820 initialize_user_prefs($link, 1);
b8aa49bc
AD
821 }
822 }
3547842a
AD
823
824 function truncate_string($str, $max_len) {
12db369c
AD
825 if (mb_strlen($str, "utf-8") > $max_len - 3) {
826 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
827 } else {
828 return $str;
829 }
830 }
54a60e1a
AD
831
832 function get_user_theme_path($link) {
798f722b
AD
833 $result = db_query($link, "SELECT theme_path
834 FROM
835 ttrss_themes,ttrss_users
836 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
837 if (db_num_rows($result) != 0) {
838 return db_fetch_result($result, 0, "theme_path");
839 } else {
840 return null;
841 }
842 }
be773442
AD
843
844 function smart_date_time($timestamp) {
845 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
846 return date("G:i", $timestamp);
f26450f1 847 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
848 return date("M d, G:i", $timestamp);
849 } else {
850 return date("Y/m/d G:i");
851 }
852 }
853
854 function smart_date($timestamp) {
855 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
856 return "Today";
f26450f1 857 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
858 return date("D m", $timestamp);
859 } else {
860 return date("Y/m/d");
861 }
862 }
a654a595
AD
863
864 function sql_bool_to_string($s) {
865 if ($s == "t" || $s == "1") {
866 return "true";
867 } else {
868 return "false";
869 }
870 }
e3c99f3b
AD
871
872 function sql_bool_to_bool($s) {
873 if ($s == "t" || $s == "1") {
874 return true;
875 } else {
876 return false;
877 }
878 }
0ea4fb50 879
e3c99f3b 880
0ea4fb50
AD
881 function toggleEvenOdd($a) {
882 if ($a == "even")
883 return "odd";
884 else
885 return "even";
886 }
6043fb7e
AD
887
888 function sanity_check($link) {
9cbca41f 889
6043fb7e
AD
890 $error_code = 0;
891 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
892 $schema_version = db_fetch_result($result, 0, "schema_version");
893
894 if ($schema_version != SCHEMA_VERSION) {
895 $error_code = 5;
896 }
897
6043fb7e 898 if ($error_code != 0) {
9cbca41f 899 print "<error error-code='$error_code'/>";
6043fb7e
AD
900 return false;
901 } else {
902 return true;
9cbca41f 903 }
6043fb7e
AD
904 }
905
40d13c28 906?>