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