]> git.wh0rd.org - tt-rss.git/blame - functions.php
fix escaping in viewfeed
[tt-rss.git] / functions.php
CommitLineData
40d13c28 1<?
f1a80dae 2
894ebcf5 3/* if ($_GET["debug"]) {
cce28758
AD
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
5 } else {
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
894ebcf5 7 } */
cce28758 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
ad507f85
AD
17 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
18
19 $rows = -1;
4c193675 20
fefa6ca3 21 if (DB_TYPE == "pgsql") {
44e241cb 22/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 23 marked = false AND feed_id = '$feed_id' AND
35d8cf43 24 (SELECT date_entered FROM ttrss_entries WHERE
44e241cb
AD
25 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
26
27 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
28 ttrss_entries.id = ref_id AND
29 marked = false AND
30 feed_id = '$feed_id' AND
31 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
ad507f85
AD
32
33 $rows = pg_affected_rows($result);
34
fefa6ca3 35 } else {
30f1746f 36/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 37 marked = false AND feed_id = '$feed_id' AND
35d8cf43 38 (SELECT date_entered FROM ttrss_entries WHERE
30f1746f
AD
39 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
40
41 $result = db_query($link, "DELETE FROM ttrss_user_entries
42 USING ttrss_user_entries, ttrss_entries
43 WHERE ttrss_entries.id = ref_id AND
44 marked = false AND
45 feed_id = '$feed_id' AND
46 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
47
ad507f85
AD
48 $rows = mysql_affected_rows($link);
49
50 }
51
52 if ($debug) {
53 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
fefa6ca3
AD
54 }
55 }
56
44e241cb
AD
57 function global_purge_old_posts($link, $do_output = false, $limit = false) {
58
894ebcf5 59 $random_qpart = sql_random_function();
fefa6ca3 60
44e241cb
AD
61 if ($limit) {
62 $limit_qpart = "LIMIT $limit";
63 } else {
64 $limit_qpart = "";
65 }
66
fefa6ca3 67 $result = db_query($link,
44e241cb
AD
68 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
69 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
70
71 while ($line = db_fetch_assoc($result)) {
72
73 $feed_id = $line["id"];
74 $purge_interval = $line["purge_interval"];
75 $owner_uid = $line["owner_uid"];
76
77 if ($purge_interval == 0) {
78
79 $tmp_result = db_query($link,
80 "SELECT value FROM ttrss_user_prefs WHERE
81 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
82
83 if (db_num_rows($tmp_result) != 0) {
84 $purge_interval = db_fetch_result($tmp_result, 0, "value");
85 }
86 }
87
88 if ($do_output) {
ad507f85 89// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
90 }
91
92 if ($purge_interval > 0) {
ad507f85 93 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3
AD
94 }
95 }
96
71604ca4
AD
97 // purge orphaned posts in main content table
98 db_query($link, "DELETE FROM ttrss_entries WHERE
99 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
100
fefa6ca3
AD
101 }
102
b6eefba5 103 function purge_old_posts($link) {
5d73494a 104
f1a80dae
AD
105 $user_id = $_SESSION["uid"];
106
107 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
108 WHERE owner_uid = '$user_id'");
5d73494a
AD
109
110 while ($line = db_fetch_assoc($result)) {
111
112 $feed_id = $line["id"];
113 $purge_interval = $line["purge_interval"];
114
b619ff15 115 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 116
140aae81 117 if ($purge_interval > 0) {
fefa6ca3 118 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
119 }
120 }
71604ca4
AD
121
122 // purge orphaned posts in main content table
123 db_query($link, "DELETE FROM ttrss_entries WHERE
124 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
125 }
126
1f2b01ed 127 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 128
4769ddaf 129 if (WEB_DEMO_MODE) return;
b0b4abcf 130
a2770077
AD
131 if (!$user_id) {
132 $user_id = $_SESSION["uid"];
133 purge_old_posts($link);
134 }
135
25af8dad 136// db_query($link, "BEGIN");
b82af8c3 137
cbd8650d
AD
138 if (MAX_UPDATE_TIME > 0) {
139 if (DB_TYPE == "mysql") {
140 $q_order = "RAND()";
141 } else {
142 $q_order = "RANDOM()";
143 }
144 } else {
145 $q_order = "last_updated DESC";
146 }
147
d148926e 148 $result = db_query($link, "SELECT feed_url,id,
798f722b 149 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd 150 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
cbd8650d
AD
151 ORDER BY $q_order");
152
153 $upd_start = time();
40d13c28 154
b6eefba5 155 while ($line = db_fetch_assoc($result)) {
d148926e
AD
156 $upd_intl = $line["update_interval"];
157
b619ff15 158 if (!$upd_intl || $upd_intl == 0) {
a2770077 159 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
b619ff15 160 }
d148926e 161
c1e202b7
AD
162 if ($upd_intl < 0) {
163 // Updates for this feed are disabled
164 continue;
165 }
166
93d40f50
AD
167 if ($fetch || (!$line["last_updated"] ||
168 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 169
cbd8650d
AD
170// print "<!-- feed: ".$line["feed_url"]." -->";
171
1f2b01ed 172 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
cbd8650d
AD
173
174 $upd_elapsed = time() - $upd_start;
175
176 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
177 return;
178 }
d148926e 179 }
40d13c28
AD
180 }
181
25af8dad 182// db_query($link, "COMMIT");
b82af8c3 183
40d13c28
AD
184 }
185
9e997874 186 function check_feed_favicon($feed_url, $feed, $link) {
78800912
AD
187 $feed_url = str_replace("http://", "", $feed_url);
188 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
189
190 $icon_url = "http://$feed_url/favicon.ico";
273a2f6b 191 $icon_file = ICONS_DIR . "/$feed.ico";
78800912
AD
192
193 if (!file_exists($icon_file)) {
e695fdc8 194
78800912
AD
195 error_reporting(0);
196 $r = fopen($icon_url, "r");
cce28758 197 error_reporting (DEFAULT_ERROR_LEVEL);
78800912
AD
198
199 if ($r) {
7d7cbaf5 200 $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon");
78800912
AD
201
202 $t = fopen($tmpfname, "w");
203
204 while (!feof($r)) {
205 $buf = fread($r, 16384);
206 fwrite($t, $buf);
207 }
208
209 fclose($r);
210 fclose($t);
211
e695fdc8
AD
212 error_reporting(0);
213 if (!rename($tmpfname, $icon_file)) {
214 unlink($tmpfname);
215 }
717f5e64
AD
216
217 chmod($icon_file, 0644);
218
cce28758 219 error_reporting (DEFAULT_ERROR_LEVEL);
78800912
AD
220
221 }
222 }
223 }
224
ddb68b81 225 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 226
4769ddaf 227 if (WEB_DEMO_MODE) return;
b0b4abcf 228
ddb68b81 229 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
230 return;
231 }
232
47c6c988 233 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
a88c1f36
AD
234 FROM ttrss_feeds WHERE id = '$feed'");
235
47c6c988
AD
236 $auth_login = db_fetch_result($result, 0, "auth_login");
237 $auth_pass = db_fetch_result($result, 0, "auth_pass");
238
a88c1f36
AD
239 $update_interval = db_fetch_result($result, 0, "update_interval");
240
241 if ($update_interval < 0) { return; }
242
ab3d0b99
AD
243 $feed = db_escape_string($feed);
244
47c6c988
AD
245 $fetch_url = $feed_url;
246
247 if ($auth_login && $auth_pass) {
248 $url_parts = array();
249 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
250
251 if ($url_parts[1] && $url_parts[2]) {
252 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
253 }
254
255 }
3ad5aa85 256 error_reporting(0);
47c6c988 257 $rss = fetch_rss($fetch_url);
ab3d0b99 258
cce28758 259 error_reporting (DEFAULT_ERROR_LEVEL);
76798ff3 260
b6eefba5 261 $feed = db_escape_string($feed);
dcee8f61 262
40d13c28 263 if ($rss) {
b82af8c3 264
44e241cb 265// db_query($link, "BEGIN");
dd8c76a9 266
a88c1f36 267 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 268 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 269
b6eefba5
AD
270 $registered_title = db_fetch_result($result, 0, "title");
271 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 272 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 273
7fed1940
AD
274 $owner_uid = db_fetch_result($result, 0, "owner_uid");
275
a2770077
AD
276 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
277 check_feed_favicon($feed_url, $feed, $link);
278 }
279
746b249f 280 if (!$registered_title || $registered_title == "[Unknown]") {
e1305a97 281 $feed_title = db_escape_string($rss->channel["title"]);
f324892e
AD
282 db_query($link, "UPDATE ttrss_feeds SET
283 title = '$feed_title' WHERE id = '$feed'");
284 }
285
147f7691 286 $site_url = $rss->channel["link"];
832b7bfc
AD
287 // weird, weird Magpie
288 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
147f7691
AD
289
290 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
291 db_query($link, "UPDATE ttrss_feeds SET
292 site_url = '$site_url' WHERE id = '$feed'");
331900c6 293 }
40d13c28 294
b7f4bda2
AD
295// print "I: " . $rss->channel["image"]["url"];
296
297 $icon_url = $rss->image["url"];
298
147f7691 299 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
300 $icon_url = db_escape_string($icon_url);
301 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
302 }
303
e6155a06
AD
304
305 $filters = array();
306
4b3dff6e 307 $result = db_query($link, "SELECT reg_exp,
db42b934
AD
308 ttrss_filter_types.name AS name,
309 ttrss_filter_actions.name AS action
310 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
311 owner_uid = $owner_uid AND
312 ttrss_filter_types.id = filter_type AND
313 ttrss_filter_actions.id = action_id AND
ead60402 314 (feed_id IS NULL OR feed_id = '$feed')");
e6155a06 315
b6eefba5 316 while ($line = db_fetch_assoc($result)) {
e6155a06 317 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
318
319 $filter["reg_exp"] = $line["reg_exp"];
320 $filter["action"] = $line["action"];
321
322 array_push($filters[$line["name"]], $filter);
e6155a06
AD
323 }
324
ddb68b81
AD
325 $iterator = $rss->items;
326
c22789da
AD
327 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
328 if (!$iterator || !is_array($iterator)) $iterator = $rss;
329
330 if (!is_array($iterator)) {
9a7a7b41 331 db_query($link, "UPDATE ttrss_feeds
75bd0669 332 SET last_error = 'Parse error: can\'t find any articles.'
9a7a7b41 333 WHERE id = '$feed'");
c22789da
AD
334 return; // WTF?
335 }
ddb68b81
AD
336
337 foreach ($iterator as $item) {
40d13c28
AD
338
339 $entry_guid = $item["id"];
340
341 if (!$entry_guid) $entry_guid = $item["guid"];
342 if (!$entry_guid) $entry_guid = $item["link"];
466001c4
AD
343
344 if (!$entry_guid) continue;
a116f569 345
9c9c7e6b 346 $entry_timestamp = "";
b82af8c3 347
9c9c7e6b
AD
348 $rss_2_date = $item['pubdate'];
349 $rss_1_date = $item['dc']['date'];
350 $atom_date = $item['issued'];
59ba2c75 351 if (!$atom_date) $atom_date = $item['updated'];
b197f117 352
9c9c7e6b
AD
353 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
354 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
355 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3
AD
356
357 if ($entry_timestamp == "") {
358 $entry_timestamp = time();
359 $no_orig_date = 'true';
466001c4
AD
360 } else {
361 $no_orig_date = 'false';
b82af8c3 362 }
b197f117 363
466001c4 364 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
71ad3959 365
40d13c28 366 $entry_title = $item["title"];
ddb68b81
AD
367
368 // strange Magpie workaround
369 $entry_link = $item["link_"];
370 if (!$entry_link) $entry_link = $item["link"];
71ad3959
AD
371
372 if (!$entry_title) continue;
373 if (!$entry_link) continue;
374
1696229f
AD
375 $entry_content = $item["content:escaped"];
376
377 if (!$entry_content) $entry_content = $item["content:encoded"];
40d13c28 378 if (!$entry_content) $entry_content = $item["content"];
372ced8b 379 if (!$entry_content) $entry_content = $item["summary"];
1696229f 380 if (!$entry_content) $entry_content = $item["description"];
a2015351 381
a116f569 382// if (!$entry_content) continue;
a2015351 383
8add756a
AD
384 // WTF
385 if (is_array($entry_content)) {
386 $entry_content = $entry_content["encoded"];
1696229f 387 if (!$entry_content) $entry_content = $entry_content["escaped"];
8add756a
AD
388 }
389
1696229f 390// print_r($item);
372ced8b
AD
391// print_r(htmlspecialchars($entry_content));
392// print "<br>";
1696229f 393
372ced8b 394 $entry_content_unescaped = $entry_content;
466001c4 395 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
cb0bd8bd 396
a1ea1e12
AD
397 $entry_comments = $item["comments"];
398
b6104dee
AD
399 $entry_author = db_escape_string($item['dc']['creator']);
400
b6eefba5 401 $entry_guid = db_escape_string($entry_guid);
2651fc4f 402
05732aa0
AD
403 $result = db_query($link, "SELECT id FROM ttrss_entries
404 WHERE guid = '$entry_guid'");
4c193675 405
b17fcb1a
AD
406 $entry_content = db_escape_string($entry_content);
407 $entry_title = db_escape_string($entry_title);
408 $entry_link = db_escape_string($entry_link);
409 $entry_comments = db_escape_string($entry_comments);
410
27f089dc 411 $num_comments = db_escape_string($item["slash"]["comments"]);
11b0dce2 412
e31073bd
AD
413 if (!$num_comments) $num_comments = 0;
414
44e241cb
AD
415 db_query($link, "BEGIN");
416
4c193675
AD
417 if (db_num_rows($result) == 0) {
418
419 // base post entry does not exist, create it
420
4c193675
AD
421 $result = db_query($link,
422 "INSERT INTO ttrss_entries
423 (title,
424 guid,
425 link,
426 updated,
427 content,
428 content_hash,
429 no_orig_date,
430 date_entered,
11b0dce2 431 comments,
b6104dee
AD
432 num_comments,
433 author)
4c193675
AD
434 VALUES
435 ('$entry_title',
436 '$entry_guid',
437 '$entry_link',
438 '$entry_timestamp_fmt',
439 '$entry_content',
440 '$content_hash',
441 $no_orig_date,
442 NOW(),
11b0dce2 443 '$entry_comments',
b6104dee
AD
444 '$num_comments',
445 '$entry_author')");
8926aab8
AD
446 } else {
447 // we keep encountering the entry in feeds, so we need to
448 // update date_entered column so that we don't get horrible
449 // dupes when the entry gets purged and reinserted again e.g.
450 // in the case of SLOW SLOW OMG SLOW updating feeds
451
452 $base_entry_id = db_fetch_result($result, 0, "id");
453
454 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
455 WHERE id = '$base_entry_id'");
4c193675
AD
456 }
457
458 // now it should exist, if not - bad luck then
459
6385315d
AD
460 $result = db_query($link, "SELECT
461 id,content_hash,no_orig_date,title,
8926aab8 462 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
463 substring(updated,1,19) as updated,
464 num_comments
6385315d
AD
465 FROM
466 ttrss_entries
467 WHERE guid = '$entry_guid'");
4c193675
AD
468
469 if (db_num_rows($result) == 1) {
470
11b0dce2
AD
471 // this will be used below in update handler
472 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
473 $orig_title = db_fetch_result($result, 0, "title");
474 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
475 $orig_date_entered = strtotime(db_fetch_result($result,
476 0, "date_entered"));
6385315d 477
11b0dce2 478 $ref_id = db_fetch_result($result, 0, "id");
4c193675 479
11b0dce2 480 // check for user post link to main table
4c193675 481
11b0dce2
AD
482 // do we allow duplicate posts with same GUID in different feeds?
483 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
484 $dupcheck_qpart = "AND feed_id = '$feed'";
485 } else {
486 $dupcheck_qpart = "";
487 }
71604ca4 488
11b0dce2 489// error_reporting(0);
19c9cb11 490
11b0dce2
AD
491 $filter_name = get_filter_name($entry_title, $entry_content,
492 $entry_link, $filters);
19c9cb11 493
11b0dce2
AD
494 if ($filter_name == "filter") {
495 continue;
496 }
19c9cb11 497
11b0dce2 498// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 499
11b0dce2
AD
500 $result = db_query($link,
501 "SELECT ref_id FROM ttrss_user_entries WHERE
502 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
503 $dupcheck_qpart");
504
505 // okay it doesn't exist - create user entry
506 if (db_num_rows($result) == 0) {
507
508 if ($filter_name != 'catchup') {
509 $unread = 'true';
510 $last_read_qpart = 'NULL';
511 } else {
512 $unread = 'false';
513 $last_read_qpart = 'NOW()';
514 }
19c9cb11 515
11b0dce2
AD
516 $result = db_query($link,
517 "INSERT INTO ttrss_user_entries
518 (ref_id, owner_uid, feed_id, unread, last_read)
519 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
520 $last_read_qpart)");
521 }
522
6385315d
AD
523 $post_needs_update = false;
524
a2770077 525 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
6385315d
AD
526 ($content_hash != $orig_content_hash)) {
527 $post_needs_update = true;
528 }
529
530 if ($orig_title != $entry_title) {
531 $post_needs_update = true;
532 }
533
11b0dce2
AD
534 if ($orig_num_comments != $num_comments) {
535 $post_needs_update = true;
536 }
537
6385315d
AD
538// this doesn't seem to be very reliable
539//
540// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
541// $post_needs_update = true;
542// }
543
544 // if post needs update, update it and mark all user entries
1c73bc0c 545 // linking to this post as updated
6385315d
AD
546 if ($post_needs_update) {
547
548// print "<!-- post $orig_title needs update : $post_needs_update -->";
549
6385315d 550 db_query($link, "UPDATE ttrss_entries
11b0dce2
AD
551 SET title = '$entry_title', content = '$entry_content',
552 num_comments = '$num_comments'
6385315d
AD
553 WHERE id = '$ref_id'");
554
555 db_query($link, "UPDATE ttrss_user_entries
556 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
557
558 }
4c193675
AD
559 }
560
44e241cb
AD
561 db_query($link, "COMMIT");
562
eb36b4eb
AD
563 /* taaaags */
564 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
565
05732aa0 566 $entry_tags = null;
eb36b4eb 567
372ced8b 568 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
ee2c3050
AD
569 $entry_content_unescaped, $entry_tags);
570
571// print "<br>$entry_title : $entry_content_unescaped<br>";
572// print_r($entry_tags);
372ced8b 573// print "<br>";
eb36b4eb
AD
574
575 $entry_tags = $entry_tags[1];
576
577 if (count($entry_tags) > 0) {
578
44e241cb
AD
579 db_query($link, "BEGIN");
580
05732aa0
AD
581 $result = db_query($link, "SELECT id,int_id
582 FROM ttrss_entries,ttrss_user_entries
25da6909 583 WHERE guid = '$entry_guid'
05732aa0 584 AND feed_id = '$feed' AND ref_id = id
7fed1940 585 AND owner_uid = '$owner_uid'");
eb36b4eb 586
fe99ab12 587 if (db_num_rows($result) == 1) {
eb36b4eb 588
fe99ab12
AD
589 $entry_id = db_fetch_result($result, 0, "id");
590 $entry_int_id = db_fetch_result($result, 0, "int_id");
591
592 foreach ($entry_tags as $tag) {
593 $tag = db_escape_string(strtolower($tag));
31483fc1
AD
594
595 $tag = str_replace("+", " ", $tag);
fe99ab12
AD
596 $tag = str_replace("technorati tag: ", "", $tag);
597
598 $result = db_query($link, "SELECT id FROM ttrss_tags
599 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
600 owner_uid = '$owner_uid' LIMIT 1");
601
602 // print db_fetch_result($result, 0, "id");
603
604 if ($result && db_num_rows($result) == 0) {
605
606 // print "tagging $entry_id as $tag<br>";
607
608 db_query($link, "INSERT INTO ttrss_tags
609 (owner_uid,tag_name,post_int_id)
610 VALUES ('$owner_uid','$tag', '$entry_int_id')");
611 }
612 }
eb36b4eb 613 }
44e241cb 614 db_query($link, "COMMIT");
05732aa0 615 }
4c193675 616 }
40d13c28 617
ab3d0b99
AD
618 db_query($link, "UPDATE ttrss_feeds
619 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 620
44e241cb 621// db_query($link, "COMMIT");
dd8c76a9 622
ab3d0b99
AD
623 } else {
624 $error_msg = db_escape_string(magpie_error());
625 db_query($link,
aa5f9f5f
AD
626 "UPDATE ttrss_feeds SET last_error = '$error_msg',
627 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
628 }
629
630 }
631
f175937c
AD
632 function print_select($id, $default, $values, $attributes = "") {
633 print "<select id=\"$id\" $attributes>";
a0d53889
AD
634 foreach ($values as $v) {
635 if ($v == $default)
636 $sel = " selected";
637 else
638 $sel = "";
639
640 print "<option$sel>$v</option>";
641 }
642 print "</select>";
643 }
40d13c28 644
19c9cb11 645 function get_filter_name($title, $content, $link, $filters) {
e6155a06
AD
646
647 if ($filters["title"]) {
19c9cb11
AD
648 foreach ($filters["title"] as $filter) {
649 $reg_exp = $filter["reg_exp"];
650 if (preg_match("/$reg_exp/i", $title)) {
651 return $filter["action"];
652 }
e6155a06
AD
653 }
654 }
655
656 if ($filters["content"]) {
19c9cb11
AD
657 foreach ($filters["content"] as $filter) {
658 $reg_exp = $filter["reg_exp"];
659 if (preg_match("/$reg_exp/i", $content)) {
660 return $filter["action"];
661 }
e6155a06
AD
662 }
663 }
664
665 if ($filters["both"]) {
666 foreach ($filters["both"] as $filter) {
19c9cb11
AD
667 $reg_exp = $filter["reg_exp"];
668 if (preg_match("/$reg_exp/i", $title) ||
669 preg_match("/$reg_exp/i", $content)) {
670 return $filter["action"];
671 }
e6155a06
AD
672 }
673 }
674
3a933f22 675 if ($filters["link"]) {
19c9cb11
AD
676 $reg_exp = $filter["reg_exp"];
677 foreach ($filters["link"] as $filter) {
678 $reg_exp = $filter["reg_exp"];
679 if (preg_match("/$reg_exp/i", $link)) {
680 return $filter["action"];
681 }
3a933f22
AD
682 }
683 }
684
e6155a06
AD
685 return false;
686 }
687
9323147e
AD
688 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
689 $rtl_content = false) {
254e0e4b
AD
690
691 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 692 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 693 } else {
023fe037 694 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
695 }
696
9323147e
AD
697 if ($rtl_content) {
698 $rtl_tag = "dir=\"rtl\"";
699 } else {
700 $rtl_tag = "dir=\"ltr\"";
701 }
702
8143ae1f 703 $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
254e0e4b
AD
704
705 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 706 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
707 print "$feed_icon";
708 }
709
9323147e 710 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
711
712 if ($unread != 0) {
713 $fctr_class = "";
714 } else {
715 $fctr_class = "class=\"invisible\"";
716 }
717
9323147e 718 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b
AD
719 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
720
721 print "</li>";
722
723 }
724
406d9489
AD
725 function getmicrotime() {
726 list($usec, $sec) = explode(" ",microtime());
727 return ((float)$usec + (float)$sec);
728 }
729
77e96719
AD
730 function print_radio($id, $default, $values, $attributes = "") {
731 foreach ($values as $v) {
732
733 if ($v == $default)
5da169d9 734 $sel = "checked";
77e96719 735 else
5da169d9
AD
736 $sel = "";
737
738 if ($v == "Yes") {
739 $sel .= " value=\"1\"";
740 } else {
741 $sel .= " value=\"0\"";
742 }
77e96719 743
69654950
AD
744 print "<input class=\"noborder\"
745 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
746
747 }
748 }
749
ff485f1d
AD
750 function initialize_user_prefs($link, $uid) {
751
752 $uid = db_escape_string($uid);
753
754 db_query($link, "BEGIN");
755
756 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
757
758 $u_result = db_query($link, "SELECT pref_name
759 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
760
761 $active_prefs = array();
762
763 while ($line = db_fetch_assoc($u_result)) {
764 array_push($active_prefs, $line["pref_name"]);
765 }
766
767 while ($line = db_fetch_assoc($result)) {
768 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
769// print "adding " . $line["pref_name"] . "<br>";
770
771 db_query($link, "INSERT INTO ttrss_user_prefs
772 (owner_uid,pref_name,value) VALUES
773 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
774
775 }
776 }
777
778 db_query($link, "COMMIT");
779
780 }
c8437f35
AD
781
782 function authenticate_user($link, $login, $password) {
783
784 $pwd_hash = 'SHA1:' . sha1($password);
785
203b6d25 786 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
7f16656e 787 login = '$login' AND pwd_hash = '$pwd_hash'");
c8437f35
AD
788
789 if (db_num_rows($result) == 1) {
790 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
791 $_SESSION["name"] = db_fetch_result($result, 0, "login");
203b6d25 792 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
c8437f35 793
f6f32198
AD
794 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
795 $_SESSION["uid"]);
796
503eb349
AD
797 $user_theme = get_user_theme_path($link);
798
799 $_SESSION["theme"] = $user_theme;
916f788a 800 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
503eb349 801
f557cd78
AD
802 initialize_user_prefs($link, $_SESSION["uid"]);
803
c8437f35
AD
804 return true;
805 }
ff485f1d 806
c8437f35
AD
807 return false;
808
809 }
810
e6cb77a0
AD
811 function make_password($length = 8) {
812
813 $password = "";
798f722b
AD
814 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
815
816 $i = 0;
e6cb77a0
AD
817
818 while ($i < $length) {
819 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
820
821 if (!strstr($password, $char)) {
822 $password .= $char;
823 $i++;
824 }
825 }
826 return $password;
827 }
828
829 // this is called after user is created to initialize default feeds, labels
830 // or whatever else
831
832 // user preferences are checked on every login, not here
833
834 function initialize_user($link, $uid) {
835
836 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
837 values ('$uid','unread = true', 'Unread articles')");
838
839 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
840 values ('$uid','last_read is null and unread = false', 'Updated articles')");
841
842 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 843 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 844 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
845
846 }
e6cb77a0 847
b8aa49bc 848 function logout_user() {
5ccc1cf5
AD
849 session_destroy();
850 if (isset($_COOKIE[session_name()])) {
851 setcookie(session_name(), '', time()-42000, '/');
852 }
b8aa49bc
AD
853 }
854
75836f33 855 function get_script_urlpath() {
87a79fa4 856 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
857 }
858
859 function get_login_redirect() {
860 $server = $_SERVER["SERVER_NAME"];
861
862 if (ENABLE_LOGIN_SSL) {
863 $protocol = "https";
864 } else {
865 $protocol = "http";
866 }
867
868 $url_path = get_script_urlpath();
869
870 $redirect_uri = "$protocol://$server$url_path/login.php";
871
872 return $redirect_uri;
873 }
874
916f788a
AD
875 function validate_session($link) {
876 if (SESSION_CHECK_ADDRESS && !DATABASE_BACKED_SESSIONS && $_SESSION["uid"]) {
877 if ($_SESSION["ip_address"]) {
878 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
879 return false;
880 }
881 }
882 }
883 return true;
884 }
885
7ae65adf
AD
886 function basic_nosid_redirect_check() {
887 if (!SINGLE_USER_MODE) {
888 if (!$_COOKIE["ttrss_sid"]) {
889 $redirect_uri = get_login_redirect();
890 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
891 header("Location: $redirect_uri?rt=$return_to");
892 exit;
893 }
894 }
895 }
896
b8aa49bc
AD
897 function login_sequence($link) {
898 if (!SINGLE_USER_MODE) {
75836f33 899
916f788a
AD
900 if (!validate_session($link)) {
901 logout_user();
902 $redirect_uri = get_login_redirect();
903 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
904 header("Location: $redirect_uri?rt=$return_to");
905 exit;
906 }
907
b8aa49bc
AD
908 if (!USE_HTTP_AUTH) {
909 if (!$_SESSION["uid"]) {
75836f33 910 $redirect_uri = get_login_redirect();
e31dca14
AD
911 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
912 header("Location: $redirect_uri?rt=$return_to");
b8aa49bc
AD
913 exit;
914 }
915 } else {
f557cd78
AD
916 if (!$_SESSION["uid"]) {
917 if (!$_SERVER["PHP_AUTH_USER"]) {
918
919 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
920 header('HTTP/1.0 401 Unauthorized');
921 exit;
922
923 } else {
924 $auth_result = authenticate_user($link,
925 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
926
927 if (!$auth_result) {
928 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
929 header('HTTP/1.0 401 Unauthorized');
930 exit;
931 }
932 }
933 }
b8aa49bc
AD
934 }
935 } else {
936 $_SESSION["uid"] = 1;
937 $_SESSION["name"] = "admin";
c7a03b7a 938 initialize_user_prefs($link, 1);
b8aa49bc
AD
939 }
940 }
3547842a
AD
941
942 function truncate_string($str, $max_len) {
12db369c
AD
943 if (mb_strlen($str, "utf-8") > $max_len - 3) {
944 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
945 } else {
946 return $str;
947 }
948 }
54a60e1a
AD
949
950 function get_user_theme_path($link) {
798f722b
AD
951 $result = db_query($link, "SELECT theme_path
952 FROM
953 ttrss_themes,ttrss_users
954 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
955 if (db_num_rows($result) != 0) {
956 return db_fetch_result($result, 0, "theme_path");
957 } else {
958 return null;
959 }
960 }
be773442
AD
961
962 function smart_date_time($timestamp) {
963 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
964 return date("G:i", $timestamp);
f26450f1 965 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
966 return date("M d, G:i", $timestamp);
967 } else {
b02111c2 968 return date("Y/m/d G:i", $timestamp);
be773442
AD
969 }
970 }
971
972 function smart_date($timestamp) {
973 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
974 return "Today";
f26450f1 975 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
976 return date("D m", $timestamp);
977 } else {
b02111c2 978 return date("Y/m/d", $timestamp);
be773442
AD
979 }
980 }
a654a595
AD
981
982 function sql_bool_to_string($s) {
983 if ($s == "t" || $s == "1") {
984 return "true";
985 } else {
986 return "false";
987 }
988 }
e3c99f3b
AD
989
990 function sql_bool_to_bool($s) {
991 if ($s == "t" || $s == "1") {
992 return true;
993 } else {
994 return false;
995 }
996 }
0ea4fb50 997
e3c99f3b 998
0ea4fb50
AD
999 function toggleEvenOdd($a) {
1000 if ($a == "even")
1001 return "odd";
1002 else
1003 return "even";
1004 }
6043fb7e
AD
1005
1006 function sanity_check($link) {
9cbca41f 1007
6043fb7e
AD
1008 $error_code = 0;
1009 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1010 $schema_version = db_fetch_result($result, 0, "schema_version");
1011
1012 if ($schema_version != SCHEMA_VERSION) {
1013 $error_code = 5;
1014 }
1015
6043fb7e 1016 if ($error_code != 0) {
9cbca41f 1017 print "<error error-code='$error_code'/>";
6043fb7e
AD
1018 return false;
1019 } else {
1020 return true;
9cbca41f 1021 }
6043fb7e
AD
1022 }
1023
27981ca3
AD
1024 function file_is_locked($filename) {
1025 error_reporting(0);
1026 $fp = fopen($filename, "r");
1027 error_reporting(DEFAULT_ERROR_LEVEL);
1028 if ($fp) {
1029 if (flock($fp, LOCK_EX | LOCK_NB)) {
1030 flock($fp, LOCK_UN);
1031 fclose($fp);
1032 return false;
1033 }
1034 fclose($fp);
1035 return true;
1036 }
1037 return false;
1038 }
1039
fcb4c0c9
AD
1040 function make_lockfile($filename) {
1041 $fp = fopen($filename, "w");
1042
1043 if (flock($fp, LOCK_EX | LOCK_NB)) {
1044 return $fp;
1045 } else {
1046 return false;
1047 }
1048 }
1049
894ebcf5
AD
1050 function sql_random_function() {
1051 if (DB_TYPE == "mysql") {
1052 return "RAND()";
1053 } else {
1054 return "RANDOM()";
1055 }
1056 }
1057
40d13c28 1058?>