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