]> git.wh0rd.org - tt-rss.git/blame - functions.php
move feed editor buttons to the right
[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';
af106b0e 12 require_once 'errors.php';
8911ac8b 13 require_once 'version.php';
40d13c28 14
7c5a308d
AD
15 if (RSS_BACKEND_TYPE == "magpie") {
16 require_once 'magpierss/rss_utils.inc';
17 } else if (RSS_BACKEND_TYPE == "simplepie") {
18 require_once 'simplepie/simplepie.inc';
19 }
387234f3 20
a3ee2a38
AD
21 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
22
ad507f85
AD
23 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
24
25 $rows = -1;
4c193675 26
fefa6ca3 27 if (DB_TYPE == "pgsql") {
44e241cb 28/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 29 marked = false AND feed_id = '$feed_id' AND
35d8cf43 30 (SELECT date_entered FROM ttrss_entries WHERE
44e241cb
AD
31 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
32
6e7f8d26
AD
33 $pg_version = get_pgsql_version($link);
34
35 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35
AD
36
37 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
38 ttrss_entries.id = ref_id AND
39 marked = false AND
40 feed_id = '$feed_id' AND
41 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
42
43 } else {
44
45 $result = db_query($link, "DELETE FROM ttrss_user_entries
46 USING ttrss_entries
47 WHERE ttrss_entries.id = ref_id AND
48 marked = false AND
49 feed_id = '$feed_id' AND
fc774155 50 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 51 }
ad507f85
AD
52
53 $rows = pg_affected_rows($result);
54
fefa6ca3 55 } else {
1e59ae35 56
30f1746f 57/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 58 marked = false AND feed_id = '$feed_id' AND
35d8cf43 59 (SELECT date_entered FROM ttrss_entries WHERE
30f1746f
AD
60 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
61
62 $result = db_query($link, "DELETE FROM ttrss_user_entries
63 USING ttrss_user_entries, ttrss_entries
64 WHERE ttrss_entries.id = ref_id AND
65 marked = false AND
66 feed_id = '$feed_id' AND
67 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
68
ad507f85
AD
69 $rows = mysql_affected_rows($link);
70
71 }
72
73 if ($debug) {
74 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
fefa6ca3
AD
75 }
76 }
77
44e241cb
AD
78 function global_purge_old_posts($link, $do_output = false, $limit = false) {
79
894ebcf5 80 $random_qpart = sql_random_function();
fefa6ca3 81
44e241cb
AD
82 if ($limit) {
83 $limit_qpart = "LIMIT $limit";
84 } else {
85 $limit_qpart = "";
86 }
87
fefa6ca3 88 $result = db_query($link,
44e241cb
AD
89 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
90 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
91
92 while ($line = db_fetch_assoc($result)) {
93
94 $feed_id = $line["id"];
95 $purge_interval = $line["purge_interval"];
96 $owner_uid = $line["owner_uid"];
97
98 if ($purge_interval == 0) {
99
100 $tmp_result = db_query($link,
101 "SELECT value FROM ttrss_user_prefs WHERE
102 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
103
104 if (db_num_rows($tmp_result) != 0) {
105 $purge_interval = db_fetch_result($tmp_result, 0, "value");
106 }
107 }
108
109 if ($do_output) {
ad507f85 110// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
111 }
112
113 if ($purge_interval > 0) {
ad507f85 114 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3
AD
115 }
116 }
117
71604ca4
AD
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");
121
fefa6ca3
AD
122 }
123
b6eefba5 124 function purge_old_posts($link) {
5d73494a 125
f1a80dae
AD
126 $user_id = $_SESSION["uid"];
127
128 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
129 WHERE owner_uid = '$user_id'");
5d73494a
AD
130
131 while ($line = db_fetch_assoc($result)) {
132
133 $feed_id = $line["id"];
134 $purge_interval = $line["purge_interval"];
135
b619ff15 136 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 137
140aae81 138 if ($purge_interval > 0) {
fefa6ca3 139 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
140 }
141 }
71604ca4
AD
142
143 // purge orphaned posts in main content table
144 db_query($link, "DELETE FROM ttrss_entries WHERE
145 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
146 }
147
1f2b01ed 148 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 149
4769ddaf 150 if (WEB_DEMO_MODE) return;
b0b4abcf 151
a2770077
AD
152 if (!$user_id) {
153 $user_id = $_SESSION["uid"];
154 purge_old_posts($link);
155 }
156
25af8dad 157// db_query($link, "BEGIN");
b82af8c3 158
cbd8650d
AD
159 if (MAX_UPDATE_TIME > 0) {
160 if (DB_TYPE == "mysql") {
161 $q_order = "RAND()";
162 } else {
163 $q_order = "RANDOM()";
164 }
165 } else {
166 $q_order = "last_updated DESC";
167 }
168
d148926e 169 $result = db_query($link, "SELECT feed_url,id,
798f722b 170 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd 171 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
cbd8650d
AD
172 ORDER BY $q_order");
173
174 $upd_start = time();
40d13c28 175
b6eefba5 176 while ($line = db_fetch_assoc($result)) {
d148926e
AD
177 $upd_intl = $line["update_interval"];
178
b619ff15 179 if (!$upd_intl || $upd_intl == 0) {
a2770077 180 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
b619ff15 181 }
d148926e 182
c1e202b7
AD
183 if ($upd_intl < 0) {
184 // Updates for this feed are disabled
185 continue;
186 }
187
93d40f50
AD
188 if ($fetch || (!$line["last_updated"] ||
189 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 190
cbd8650d
AD
191// print "<!-- feed: ".$line["feed_url"]." -->";
192
1f2b01ed 193 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
cbd8650d
AD
194
195 $upd_elapsed = time() - $upd_start;
196
197 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
198 return;
199 }
d148926e 200 }
40d13c28
AD
201 }
202
25af8dad 203// db_query($link, "COMMIT");
b82af8c3 204
40d13c28
AD
205 }
206
9e997874 207 function check_feed_favicon($feed_url, $feed, $link) {
78800912
AD
208 $feed_url = str_replace("http://", "", $feed_url);
209 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
210
211 $icon_url = "http://$feed_url/favicon.ico";
273a2f6b 212 $icon_file = ICONS_DIR . "/$feed.ico";
78800912
AD
213
214 if (!file_exists($icon_file)) {
e695fdc8 215
78800912
AD
216 error_reporting(0);
217 $r = fopen($icon_url, "r");
cce28758 218 error_reporting (DEFAULT_ERROR_LEVEL);
78800912
AD
219
220 if ($r) {
7d7cbaf5 221 $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon");
78800912
AD
222
223 $t = fopen($tmpfname, "w");
224
225 while (!feof($r)) {
226 $buf = fread($r, 16384);
227 fwrite($t, $buf);
228 }
229
230 fclose($r);
231 fclose($t);
232
e695fdc8
AD
233 error_reporting(0);
234 if (!rename($tmpfname, $icon_file)) {
235 unlink($tmpfname);
236 }
717f5e64
AD
237
238 chmod($icon_file, 0644);
239
cce28758 240 error_reporting (DEFAULT_ERROR_LEVEL);
78800912
AD
241
242 }
243 }
244 }
245
ddb68b81 246 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 247
4769ddaf 248 if (WEB_DEMO_MODE) return;
b0b4abcf 249
ddb68b81 250 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
251 return;
252 }
253
47c6c988 254 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
a88c1f36
AD
255 FROM ttrss_feeds WHERE id = '$feed'");
256
ff25e639
AD
257 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
258 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
47c6c988 259
a88c1f36
AD
260 $update_interval = db_fetch_result($result, 0, "update_interval");
261
262 if ($update_interval < 0) { return; }
263
ab3d0b99
AD
264 $feed = db_escape_string($feed);
265
47c6c988
AD
266 $fetch_url = $feed_url;
267
268 if ($auth_login && $auth_pass) {
269 $url_parts = array();
270 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
271
272 if ($url_parts[1] && $url_parts[2]) {
273 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
274 }
275
276 }
ab3d0b99 277
7c5a308d
AD
278 if (RSS_BACKEND_TYPE == "magpie") {
279 error_reporting(0);
280 $rss = fetch_rss($fetch_url);
281 error_reporting (DEFAULT_ERROR_LEVEL);
282 } else if (RSS_BACKEND_TYPE == "simplepie") {
283
284 if (!file_exists(SIMPLEPIE_CACHE_DIR)) {
285 mkdir(SIMPLEPIE_CACHE_DIR);
286 }
76798ff3 287
7c5a308d
AD
288 $rss = new SimplePie();
289 $rss->feed_url($fetch_url);
290 $rss->cache_location(SIMPLEPIE_CACHE_DIR);
291 $rss->init();
292 }
293
b6eefba5 294 $feed = db_escape_string($feed);
dcee8f61 295
7c5a308d 296 $rss_check = $rss;
b82af8c3 297
7c5a308d
AD
298 if (RSS_BACKEND_TYPE == "simplepie") {
299 $rss_check = $rss->data;
300 }
301
302 if ($rss_check) {
303
44e241cb 304// db_query($link, "BEGIN");
dd8c76a9 305
a88c1f36 306 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 307 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 308
b6eefba5
AD
309 $registered_title = db_fetch_result($result, 0, "title");
310 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 311 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 312
7fed1940
AD
313 $owner_uid = db_fetch_result($result, 0, "owner_uid");
314
a2770077
AD
315 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
316 check_feed_favicon($feed_url, $feed, $link);
317 }
318
746b249f 319 if (!$registered_title || $registered_title == "[Unknown]") {
7c5a308d
AD
320
321 if (RSS_BACKEND_TYPE == "magpie") {
322 $feed_title = db_escape_string($rss->channel["title"]);
323 } else {
324 $feed_title = $rss->get_feed_title();
325 }
326
f324892e
AD
327 db_query($link, "UPDATE ttrss_feeds SET
328 title = '$feed_title' WHERE id = '$feed'");
329 }
330
7c5a308d
AD
331 if (RSS_BACKEND_TYPE == "magpie") {
332 $site_url = $rss->channel["link"];
333 // weird, weird Magpie
334 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
335 } else {
336 $site_url = $rss->get_feed_link();
337 }
147f7691
AD
338
339 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
340 db_query($link, "UPDATE ttrss_feeds SET
341 site_url = '$site_url' WHERE id = '$feed'");
331900c6 342 }
40d13c28 343
b7f4bda2
AD
344// print "I: " . $rss->channel["image"]["url"];
345
7c5a308d
AD
346 if (RSS_BACKEND_TYPE == "magpie") {
347 $icon_url = $rss->image["url"];
348 } else {
349 $icon_url = $rss->get_image_url(); # FIXME
350 }
b7f4bda2 351
147f7691 352 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
353 $icon_url = db_escape_string($icon_url);
354 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
355 }
356
e6155a06
AD
357
358 $filters = array();
359
4b3dff6e 360 $result = db_query($link, "SELECT reg_exp,
db42b934
AD
361 ttrss_filter_types.name AS name,
362 ttrss_filter_actions.name AS action
363 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
364 owner_uid = $owner_uid AND
365 ttrss_filter_types.id = filter_type AND
366 ttrss_filter_actions.id = action_id AND
ead60402 367 (feed_id IS NULL OR feed_id = '$feed')");
e6155a06 368
b6eefba5 369 while ($line = db_fetch_assoc($result)) {
e6155a06 370 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
371
372 $filter["reg_exp"] = $line["reg_exp"];
373 $filter["action"] = $line["action"];
374
375 array_push($filters[$line["name"]], $filter);
e6155a06
AD
376 }
377
7c5a308d
AD
378 if (RSS_BACKEND_TYPE == "magpie") {
379 $iterator = $rss->items;
ddb68b81 380
7c5a308d
AD
381 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
382 if (!$iterator || !is_array($iterator)) $iterator = $rss;
383
384 } else {
385 $iterator = $rss->get_items();
386 }
c22789da
AD
387
388 if (!is_array($iterator)) {
39541e74 389 /* db_query($link, "UPDATE ttrss_feeds
75bd0669 390 SET last_error = 'Parse error: can\'t find any articles.'
39541e74 391 WHERE id = '$feed'"); */
c22789da
AD
392 return; // WTF?
393 }
ddb68b81
AD
394
395 foreach ($iterator as $item) {
7c5a308d
AD
396
397 if (RSS_BACKEND_TYPE == "magpie") {
398
399 $entry_guid = $item["id"];
400
401 if (!$entry_guid) $entry_guid = $item["guid"];
402 if (!$entry_guid) $entry_guid = $item["link"];
40d13c28 403
7c5a308d 404 if (!$entry_guid) continue;
40d13c28 405
7c5a308d
AD
406 $entry_timestamp = "";
407
408 $rss_2_date = $item['pubdate'];
409 $rss_1_date = $item['dc']['date'];
410 $atom_date = $item['issued'];
411 if (!$atom_date) $atom_date = $item['updated'];
b82af8c3 412
7c5a308d
AD
413 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
414 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
415 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
416
417 if ($entry_timestamp == "") {
418 $entry_timestamp = time();
419 $no_orig_date = 'true';
420 } else {
421 $no_orig_date = 'false';
422 }
423
424 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
425
426 $entry_title = $item["title"];
427
428 // strange Magpie workaround
429 $entry_link = $item["link_"];
430 if (!$entry_link) $entry_link = $item["link"];
431
432 if (!$entry_title) continue;
433 if (!$entry_link) continue;
434
435 $entry_content = $item["content:escaped"];
436
437 if (!$entry_content) $entry_content = $item["content:encoded"];
438 if (!$entry_content) $entry_content = $item["content"];
79b5d2d2 439 if (!$entry_content) $entry_content = $item["atom_content"];
7c5a308d
AD
440 if (!$entry_content) $entry_content = $item["summary"];
441 if (!$entry_content) $entry_content = $item["description"];
442
443 // if (!$entry_content) continue;
444
445 // WTF
446 if (is_array($entry_content)) {
447 $entry_content = $entry_content["encoded"];
448 if (!$entry_content) $entry_content = $entry_content["escaped"];
449 }
450
451 // print_r($item);
452 // print_r(htmlspecialchars($entry_content));
453 // print "<br>";
454
455 $entry_content_unescaped = $entry_content;
456 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
457
458 $entry_comments = $item["comments"];
459
460 $entry_author = db_escape_string($item['dc']['creator']);
461
462 $entry_guid = db_escape_string($entry_guid);
463
464 $result = db_query($link, "SELECT id FROM ttrss_entries
465 WHERE guid = '$entry_guid'");
466
467 $entry_content = db_escape_string($entry_content);
468 $entry_title = db_escape_string($entry_title);
469 $entry_link = db_escape_string($entry_link);
470 $entry_comments = db_escape_string($entry_comments);
471
472 $num_comments = db_escape_string($item["slash"]["comments"]);
473
474 if (!$num_comments) $num_comments = 0;
71ad3959 475
7c5a308d 476 } else if (RSS_BACKEND_TYPE == "simplepie") {
ddb68b81 477
7c5a308d 478 $entry_guid = $item->get_id();
71ad3959 479
7c5a308d
AD
480 if (!$entry_guid) {
481 $entry_guid = $item->get_permalink();
482 }
483
484 if (!$entry_guid) continue;
485
486 $entry_timestamp = $item->get_date("U");
487
488 if ($entry_timestamp == "") {
489 $entry_timestamp = time();
490 $no_orig_date = 'true';
491 } else {
492 $no_orig_date = 'false';
493 }
494
495 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
496
497 $entry_title = $item->get_title();
498 $entry_link = $item->get_permalink();
499
500 if (!$entry_title) continue;
501 if (!$entry_link) continue;
71ad3959 502
7c5a308d
AD
503 $entry_content = $item->get_description();
504
505// print_r(htmlspecialchars($entry_content));
506// print "<br>";
507
508 $entry_content_unescaped = $entry_content;
509 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
510
511 $entry_comments = ""; # FIXME
512
513 $entry_author = $item->get_author(0);
1696229f 514
7c5a308d
AD
515 $entry_author = db_escape_string($entry_author->name);
516
517 $entry_guid = db_escape_string($entry_guid);
a2015351 518
7c5a308d
AD
519 $result = db_query($link, "SELECT id FROM ttrss_entries
520 WHERE guid = '$entry_guid'");
521
522 $entry_content = db_escape_string($entry_content);
523 $entry_title = db_escape_string($entry_title);
524 $entry_link = db_escape_string($entry_link);
525 $entry_comments = db_escape_string($entry_comments);
526
527 $num_comments = 0; # FIXME
528
529 if (!$num_comments) $num_comments = 0;
a2015351 530
8add756a
AD
531 }
532
d48d160c 533 # sanitize content
183ad07b
AD
534
535 $entry_content = sanitize_rss($entry_content);
536 $entry_title = sanitize_rss($entry_title);
537 $entry_link = sanitize_rss($entry_link);
538 $entry_comments = sanitize_rss($entry_comments);
d48d160c 539
44e241cb
AD
540 db_query($link, "BEGIN");
541
4c193675
AD
542 if (db_num_rows($result) == 0) {
543
544 // base post entry does not exist, create it
545
4c193675
AD
546 $result = db_query($link,
547 "INSERT INTO ttrss_entries
548 (title,
549 guid,
550 link,
551 updated,
552 content,
553 content_hash,
554 no_orig_date,
555 date_entered,
11b0dce2 556 comments,
b6104dee
AD
557 num_comments,
558 author)
4c193675
AD
559 VALUES
560 ('$entry_title',
561 '$entry_guid',
562 '$entry_link',
563 '$entry_timestamp_fmt',
564 '$entry_content',
565 '$content_hash',
566 $no_orig_date,
567 NOW(),
11b0dce2 568 '$entry_comments',
b6104dee
AD
569 '$num_comments',
570 '$entry_author')");
8926aab8
AD
571 } else {
572 // we keep encountering the entry in feeds, so we need to
573 // update date_entered column so that we don't get horrible
574 // dupes when the entry gets purged and reinserted again e.g.
575 // in the case of SLOW SLOW OMG SLOW updating feeds
576
577 $base_entry_id = db_fetch_result($result, 0, "id");
578
579 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
580 WHERE id = '$base_entry_id'");
4c193675
AD
581 }
582
583 // now it should exist, if not - bad luck then
584
6385315d
AD
585 $result = db_query($link, "SELECT
586 id,content_hash,no_orig_date,title,
8926aab8 587 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
588 substring(updated,1,19) as updated,
589 num_comments
6385315d
AD
590 FROM
591 ttrss_entries
592 WHERE guid = '$entry_guid'");
4c193675
AD
593
594 if (db_num_rows($result) == 1) {
595
11b0dce2
AD
596 // this will be used below in update handler
597 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
598 $orig_title = db_fetch_result($result, 0, "title");
599 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
600 $orig_date_entered = strtotime(db_fetch_result($result,
601 0, "date_entered"));
6385315d 602
11b0dce2 603 $ref_id = db_fetch_result($result, 0, "id");
4c193675 604
11b0dce2 605 // check for user post link to main table
4c193675 606
11b0dce2
AD
607 // do we allow duplicate posts with same GUID in different feeds?
608 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
609 $dupcheck_qpart = "AND feed_id = '$feed'";
610 } else {
611 $dupcheck_qpart = "";
612 }
71604ca4 613
11b0dce2 614// error_reporting(0);
19c9cb11 615
11b0dce2
AD
616 $filter_name = get_filter_name($entry_title, $entry_content,
617 $entry_link, $filters);
19c9cb11 618
11b0dce2
AD
619 if ($filter_name == "filter") {
620 continue;
621 }
19c9cb11 622
11b0dce2 623// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 624
11b0dce2
AD
625 $result = db_query($link,
626 "SELECT ref_id FROM ttrss_user_entries WHERE
627 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
628 $dupcheck_qpart");
629
630 // okay it doesn't exist - create user entry
631 if (db_num_rows($result) == 0) {
632
633 if ($filter_name != 'catchup') {
634 $unread = 'true';
635 $last_read_qpart = 'NULL';
636 } else {
637 $unread = 'false';
638 $last_read_qpart = 'NOW()';
639 }
dd7d3187
AD
640
641 if ($filter_name == 'mark') {
642 $marked = 'true';
643 } else {
644 $marked = 'false';
645 }
19c9cb11 646
11b0dce2
AD
647 $result = db_query($link,
648 "INSERT INTO ttrss_user_entries
dd7d3187 649 (ref_id, owner_uid, feed_id, unread, last_read, marked)
11b0dce2 650 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
dd7d3187 651 $last_read_qpart, $marked)");
11b0dce2
AD
652 }
653
6385315d
AD
654 $post_needs_update = false;
655
a2770077 656 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
6385315d
AD
657 ($content_hash != $orig_content_hash)) {
658 $post_needs_update = true;
659 }
660
661 if ($orig_title != $entry_title) {
662 $post_needs_update = true;
663 }
664
11b0dce2
AD
665 if ($orig_num_comments != $num_comments) {
666 $post_needs_update = true;
667 }
668
6385315d
AD
669// this doesn't seem to be very reliable
670//
671// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
672// $post_needs_update = true;
673// }
674
675 // if post needs update, update it and mark all user entries
1c73bc0c 676 // linking to this post as updated
6385315d
AD
677 if ($post_needs_update) {
678
679// print "<!-- post $orig_title needs update : $post_needs_update -->";
680
6385315d 681 db_query($link, "UPDATE ttrss_entries
11b0dce2
AD
682 SET title = '$entry_title', content = '$entry_content',
683 num_comments = '$num_comments'
6385315d
AD
684 WHERE id = '$ref_id'");
685
68c1b64f 686 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid)) {
4919fb42
AD
687 db_query($link, "UPDATE ttrss_user_entries
688 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
689 } else {
690 db_query($link, "UPDATE ttrss_user_entries
691 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
692 }
6385315d
AD
693
694 }
4c193675
AD
695 }
696
44e241cb
AD
697 db_query($link, "COMMIT");
698
eb36b4eb
AD
699 /* taaaags */
700 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
701
05732aa0 702 $entry_tags = null;
eb36b4eb 703
372ced8b 704 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
ee2c3050
AD
705 $entry_content_unescaped, $entry_tags);
706
707// print "<br>$entry_title : $entry_content_unescaped<br>";
708// print_r($entry_tags);
372ced8b 709// print "<br>";
eb36b4eb
AD
710
711 $entry_tags = $entry_tags[1];
712
713 if (count($entry_tags) > 0) {
714
44e241cb
AD
715 db_query($link, "BEGIN");
716
05732aa0
AD
717 $result = db_query($link, "SELECT id,int_id
718 FROM ttrss_entries,ttrss_user_entries
25da6909 719 WHERE guid = '$entry_guid'
05732aa0 720 AND feed_id = '$feed' AND ref_id = id
7fed1940 721 AND owner_uid = '$owner_uid'");
eb36b4eb 722
fe99ab12 723 if (db_num_rows($result) == 1) {
eb36b4eb 724
fe99ab12
AD
725 $entry_id = db_fetch_result($result, 0, "id");
726 $entry_int_id = db_fetch_result($result, 0, "int_id");
727
728 foreach ($entry_tags as $tag) {
729 $tag = db_escape_string(strtolower($tag));
31483fc1
AD
730
731 $tag = str_replace("+", " ", $tag);
fe99ab12
AD
732 $tag = str_replace("technorati tag: ", "", $tag);
733
734 $result = db_query($link, "SELECT id FROM ttrss_tags
735 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
736 owner_uid = '$owner_uid' LIMIT 1");
737
738 // print db_fetch_result($result, 0, "id");
739
740 if ($result && db_num_rows($result) == 0) {
741
742 // print "tagging $entry_id as $tag<br>";
743
744 db_query($link, "INSERT INTO ttrss_tags
745 (owner_uid,tag_name,post_int_id)
746 VALUES ('$owner_uid','$tag', '$entry_int_id')");
747 }
748 }
eb36b4eb 749 }
44e241cb 750 db_query($link, "COMMIT");
05732aa0 751 }
4c193675 752 }
40d13c28 753
ab3d0b99
AD
754 db_query($link, "UPDATE ttrss_feeds
755 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 756
44e241cb 757// db_query($link, "COMMIT");
dd8c76a9 758
ab3d0b99
AD
759 } else {
760 $error_msg = db_escape_string(magpie_error());
761 db_query($link,
aa5f9f5f
AD
762 "UPDATE ttrss_feeds SET last_error = '$error_msg',
763 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
764 }
765
766 }
767
f175937c 768 function print_select($id, $default, $values, $attributes = "") {
79f3553b 769 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
770 foreach ($values as $v) {
771 if ($v == $default)
772 $sel = " selected";
773 else
774 $sel = "";
775
776 print "<option$sel>$v</option>";
777 }
778 print "</select>";
779 }
40d13c28 780
79f3553b
AD
781 function print_select_hash($id, $default, $values, $attributes = "") {
782 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
783 foreach (array_keys($values) as $v) {
784 if ($v == $default)
785 $sel = "selected";
786 else
787 $sel = "";
788
789 print "<option $sel value=\"$v\">".$values[$v]."</option>";
790 }
791
792 print "</select>";
793 }
794
19c9cb11 795 function get_filter_name($title, $content, $link, $filters) {
e6155a06
AD
796
797 if ($filters["title"]) {
19c9cb11
AD
798 foreach ($filters["title"] as $filter) {
799 $reg_exp = $filter["reg_exp"];
800 if (preg_match("/$reg_exp/i", $title)) {
801 return $filter["action"];
802 }
e6155a06
AD
803 }
804 }
805
806 if ($filters["content"]) {
19c9cb11
AD
807 foreach ($filters["content"] as $filter) {
808 $reg_exp = $filter["reg_exp"];
809 if (preg_match("/$reg_exp/i", $content)) {
810 return $filter["action"];
811 }
e6155a06
AD
812 }
813 }
814
815 if ($filters["both"]) {
816 foreach ($filters["both"] as $filter) {
19c9cb11
AD
817 $reg_exp = $filter["reg_exp"];
818 if (preg_match("/$reg_exp/i", $title) ||
819 preg_match("/$reg_exp/i", $content)) {
820 return $filter["action"];
821 }
e6155a06
AD
822 }
823 }
824
3a933f22 825 if ($filters["link"]) {
19c9cb11
AD
826 $reg_exp = $filter["reg_exp"];
827 foreach ($filters["link"] as $filter) {
828 $reg_exp = $filter["reg_exp"];
829 if (preg_match("/$reg_exp/i", $link)) {
830 return $filter["action"];
831 }
3a933f22
AD
832 }
833 }
834
e6155a06
AD
835 return false;
836 }
837
9323147e 838 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 839 $rtl_content = false, $last_updated = false, $last_error = false) {
254e0e4b
AD
840
841 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 842 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 843 } else {
023fe037 844 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
845 }
846
9323147e
AD
847 if ($rtl_content) {
848 $rtl_tag = "dir=\"rtl\"";
849 } else {
850 $rtl_tag = "dir=\"ltr\"";
851 }
852
78d5212c
AD
853 $error_notify_msg = "";
854
fb1fb4ab
AD
855 if ($last_error) {
856 $link_title = "Error: $last_error ($last_updated)";
78d5212c 857 $error_notify_msg = "(Error)";
ad780e9c 858 } else if ($last_updated) {
fb1fb4ab
AD
859 $link_title = "Updated: $last_updated";
860 }
861
767e2486 862 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
254e0e4b
AD
863
864 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 865 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
866 print "$feed_icon";
867 }
868
9323147e 869 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
870
871 if ($unread != 0) {
872 $fctr_class = "";
873 } else {
874 $fctr_class = "class=\"invisible\"";
875 }
876
9323147e 877 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b 878 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
78d5212c
AD
879
880 if (get_pref($link, "EXTENDED_FEEDLIST")) {
881 print "<div class=\"feedExtInfo\">
882 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
883 }
884
254e0e4b
AD
885 print "</li>";
886
887 }
888
406d9489
AD
889 function getmicrotime() {
890 list($usec, $sec) = explode(" ",microtime());
891 return ((float)$usec + (float)$sec);
892 }
893
77e96719
AD
894 function print_radio($id, $default, $values, $attributes = "") {
895 foreach ($values as $v) {
896
897 if ($v == $default)
5da169d9 898 $sel = "checked";
77e96719 899 else
5da169d9
AD
900 $sel = "";
901
902 if ($v == "Yes") {
903 $sel .= " value=\"1\"";
904 } else {
905 $sel .= " value=\"0\"";
906 }
77e96719 907
69654950
AD
908 print "<input class=\"noborder\"
909 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
910
911 }
912 }
913
ff485f1d
AD
914 function initialize_user_prefs($link, $uid) {
915
916 $uid = db_escape_string($uid);
917
918 db_query($link, "BEGIN");
919
920 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
921
922 $u_result = db_query($link, "SELECT pref_name
923 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
924
925 $active_prefs = array();
926
927 while ($line = db_fetch_assoc($u_result)) {
928 array_push($active_prefs, $line["pref_name"]);
929 }
930
931 while ($line = db_fetch_assoc($result)) {
932 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
933// print "adding " . $line["pref_name"] . "<br>";
934
935 db_query($link, "INSERT INTO ttrss_user_prefs
936 (owner_uid,pref_name,value) VALUES
937 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
938
939 }
940 }
941
942 db_query($link, "COMMIT");
943
944 }
956c7629
AD
945
946 function lookup_user_id($link, $user) {
947
948 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
949 login = '$login'");
950
951 if (db_num_rows($result) == 1) {
952 return db_fetch_result($result, 0, "id");
953 } else {
954 return false;
955 }
956 }
957
18664970
AD
958 function http_authenticate_user($link) {
959
960 if (!$_SERVER["PHP_AUTH_USER"]) {
961
962 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
963 header('HTTP/1.0 401 Unauthorized');
964 exit;
965
966 } else {
967 $auth_result = authenticate_user($link,
968 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
969
970 if (!$auth_result) {
971 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
972 header('HTTP/1.0 401 Unauthorized');
973 exit;
974 }
975 }
976
977 return true;
978 }
979
c8437f35
AD
980 function authenticate_user($link, $login, $password) {
981
131b01b3 982 if (!SINGLE_USER_MODE) {
c8437f35 983
131b01b3
AD
984 $pwd_hash = 'SHA1:' . sha1($password);
985
986 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
987 login = '$login' AND pwd_hash = '$pwd_hash'");
988
989 if (db_num_rows($result) == 1) {
990 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
991 $_SESSION["name"] = db_fetch_result($result, 0, "login");
992 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
993
994 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
995 $_SESSION["uid"]);
996
997 $user_theme = get_user_theme_path($link);
998
999 $_SESSION["theme"] = $user_theme;
1000 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1001
1002 initialize_user_prefs($link, $_SESSION["uid"]);
1003
1004 return true;
1005 }
1006
1007 return false;
503eb349 1008
131b01b3 1009 } else {
503eb349 1010
131b01b3
AD
1011 $_SESSION["uid"] = 1;
1012 $_SESSION["name"] = "admin";
f557cd78 1013
0bbba72d
AD
1014 $user_theme = get_user_theme_path($link);
1015
1016 $_SESSION["theme"] = $user_theme;
1017 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1018
1019 initialize_user_prefs($link, $_SESSION["uid"]);
1020
c8437f35
AD
1021 return true;
1022 }
c8437f35
AD
1023 }
1024
e6cb77a0
AD
1025 function make_password($length = 8) {
1026
1027 $password = "";
798f722b
AD
1028 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1029
1030 $i = 0;
e6cb77a0
AD
1031
1032 while ($i < $length) {
1033 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1034
1035 if (!strstr($password, $char)) {
1036 $password .= $char;
1037 $i++;
1038 }
1039 }
1040 return $password;
1041 }
1042
1043 // this is called after user is created to initialize default feeds, labels
1044 // or whatever else
1045
1046 // user preferences are checked on every login, not here
1047
1048 function initialize_user($link, $uid) {
1049
1050 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1051 values ('$uid','unread = true', 'Unread articles')");
1052
1053 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1054 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1055
1056 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1057 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 1058 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
1059
1060 }
e6cb77a0 1061
b8aa49bc 1062 function logout_user() {
5ccc1cf5
AD
1063 session_destroy();
1064 if (isset($_COOKIE[session_name()])) {
1065 setcookie(session_name(), '', time()-42000, '/');
1066 }
b8aa49bc
AD
1067 }
1068
75836f33 1069 function get_script_urlpath() {
87a79fa4 1070 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1071 }
1072
1073 function get_login_redirect() {
1074 $server = $_SERVER["SERVER_NAME"];
1075
1076 if (ENABLE_LOGIN_SSL) {
1077 $protocol = "https";
1078 } else {
1079 $protocol = "http";
1080 }
1081
1082 $url_path = get_script_urlpath();
1083
1084 $redirect_uri = "$protocol://$server$url_path/login.php";
1085
1086 return $redirect_uri;
1087 }
1088
916f788a 1089 function validate_session($link) {
a2e9b457 1090 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
1091 if ($_SESSION["ip_address"]) {
1092 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1093 return false;
1094 }
1095 }
1096 }
1097 return true;
1098 }
1099
7ae65adf
AD
1100 function basic_nosid_redirect_check() {
1101 if (!SINGLE_USER_MODE) {
3dd46f19 1102 if (!$_COOKIE[get_session_cookie_name()]) {
7ae65adf
AD
1103 $redirect_uri = get_login_redirect();
1104 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1105 header("Location: $redirect_uri?rt=$return_to");
1106 exit;
1107 }
1108 }
1109 }
1110
b8aa49bc
AD
1111 function login_sequence($link) {
1112 if (!SINGLE_USER_MODE) {
75836f33 1113
916f788a
AD
1114 if (!validate_session($link)) {
1115 logout_user();
1116 $redirect_uri = get_login_redirect();
1117 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1118 header("Location: $redirect_uri?rt=$return_to");
1119 exit;
1120 }
1121
b8aa49bc
AD
1122 if (!USE_HTTP_AUTH) {
1123 if (!$_SESSION["uid"]) {
75836f33 1124 $redirect_uri = get_login_redirect();
e31dca14
AD
1125 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1126 header("Location: $redirect_uri?rt=$return_to");
b8aa49bc
AD
1127 exit;
1128 }
1129 } else {
f557cd78
AD
1130 if (!$_SESSION["uid"]) {
1131 if (!$_SERVER["PHP_AUTH_USER"]) {
1132
1133 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1134 header('HTTP/1.0 401 Unauthorized');
1135 exit;
1136
1137 } else {
1138 $auth_result = authenticate_user($link,
1139 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1140
1141 if (!$auth_result) {
1142 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1143 header('HTTP/1.0 401 Unauthorized');
1144 exit;
1145 }
1146 }
1147 }
b8aa49bc
AD
1148 }
1149 } else {
0bbba72d 1150 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1151 }
1152 }
3547842a
AD
1153
1154 function truncate_string($str, $max_len) {
12db369c
AD
1155 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1156 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
1157 } else {
1158 return $str;
1159 }
1160 }
54a60e1a
AD
1161
1162 function get_user_theme_path($link) {
798f722b
AD
1163 $result = db_query($link, "SELECT theme_path
1164 FROM
1165 ttrss_themes,ttrss_users
1166 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
1167 if (db_num_rows($result) != 0) {
1168 return db_fetch_result($result, 0, "theme_path");
1169 } else {
1170 return null;
1171 }
1172 }
be773442
AD
1173
1174 function smart_date_time($timestamp) {
1175 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1176 return date("G:i", $timestamp);
f26450f1 1177 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1178 return date("M d, G:i", $timestamp);
1179 } else {
b02111c2 1180 return date("Y/m/d G:i", $timestamp);
be773442
AD
1181 }
1182 }
1183
1184 function smart_date($timestamp) {
1185 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1186 return "Today";
f26450f1 1187 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1188 return date("D m", $timestamp);
1189 } else {
b02111c2 1190 return date("Y/m/d", $timestamp);
be773442
AD
1191 }
1192 }
a654a595
AD
1193
1194 function sql_bool_to_string($s) {
1195 if ($s == "t" || $s == "1") {
1196 return "true";
1197 } else {
1198 return "false";
1199 }
1200 }
e3c99f3b
AD
1201
1202 function sql_bool_to_bool($s) {
1203 if ($s == "t" || $s == "1") {
1204 return true;
1205 } else {
1206 return false;
1207 }
1208 }
0ea4fb50 1209
e3c99f3b 1210
0ea4fb50
AD
1211 function toggleEvenOdd($a) {
1212 if ($a == "even")
1213 return "odd";
1214 else
1215 return "even";
1216 }
6043fb7e
AD
1217
1218 function sanity_check($link) {
9cbca41f 1219
aec3ce39
AD
1220 error_reporting(0);
1221
6043fb7e
AD
1222 $error_code = 0;
1223 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1224 $schema_version = db_fetch_result($result, 0, "schema_version");
1225
1226 if ($schema_version != SCHEMA_VERSION) {
1227 $error_code = 5;
1228 }
1229
aec3ce39
AD
1230 if (DB_TYPE == "mysql") {
1231 $result = db_query($link, "SELECT true", false);
1232 if (db_num_rows($result) != 1) {
1233 $error_code = 10;
1234 }
1235 }
1236
1237 error_reporting (DEFAULT_ERROR_LEVEL);
1238
6043fb7e 1239 if ($error_code != 0) {
aec3ce39 1240 print_error_xml($error_code);
6043fb7e
AD
1241 return false;
1242 } else {
1243 return true;
9cbca41f 1244 }
6043fb7e
AD
1245 }
1246
27981ca3
AD
1247 function file_is_locked($filename) {
1248 error_reporting(0);
1249 $fp = fopen($filename, "r");
1250 error_reporting(DEFAULT_ERROR_LEVEL);
1251 if ($fp) {
1252 if (flock($fp, LOCK_EX | LOCK_NB)) {
1253 flock($fp, LOCK_UN);
1254 fclose($fp);
1255 return false;
1256 }
1257 fclose($fp);
1258 return true;
1259 }
1260 return false;
1261 }
1262
fcb4c0c9
AD
1263 function make_lockfile($filename) {
1264 $fp = fopen($filename, "w");
1265
1266 if (flock($fp, LOCK_EX | LOCK_NB)) {
1267 return $fp;
1268 } else {
1269 return false;
1270 }
1271 }
1272
894ebcf5
AD
1273 function sql_random_function() {
1274 if (DB_TYPE == "mysql") {
1275 return "RAND()";
1276 } else {
1277 return "RANDOM()";
1278 }
1279 }
1280
23aa0d16 1281 function catchup_feed($link, $feed, $cat_view) {
88040f57
AD
1282
1283 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
23aa0d16
AD
1284
1285 if ($cat_view) {
1286
1287 if ($feed > 0) {
1288 $cat_qpart = "cat_id = '$feed'";
1289 } else {
1290 $cat_qpart = "cat_id IS NULL";
1291 }
1292
1293 $tmp_result = db_query($link, "SELECT id
1294 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1295 $_SESSION["uid"]);
1296
1297 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1298
1299 $tmp_feed = $tmp_line["id"];
1300
1301 db_query($link, "UPDATE ttrss_user_entries
1302 SET unread = false,last_read = NOW()
1303 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1304 }
1305
1306 } else if ($feed > 0) {
1307
1308 $tmp_result = db_query($link, "SELECT id
1309 FROM ttrss_feeds WHERE parent_feed = '$feed'
1310 ORDER BY cat_id,title");
1311
1312 $parent_ids = array();
1313
1314 if (db_num_rows($tmp_result) > 0) {
1315 while ($p = db_fetch_assoc($tmp_result)) {
1316 array_push($parent_ids, "feed_id = " . $p["id"]);
1317 }
1318
1319 $children_qpart = implode(" OR ", $parent_ids);
1320
1321 db_query($link, "UPDATE ttrss_user_entries
1322 SET unread = false,last_read = NOW()
1323 WHERE (feed_id = '$feed' OR $children_qpart)
1324 AND owner_uid = " . $_SESSION["uid"]);
1325
1326 } else {
1327 db_query($link, "UPDATE ttrss_user_entries
1328 SET unread = false,last_read = NOW()
1329 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1330 }
1331
1332 } else if ($feed < 0 && $feed > -10) { // special, like starred
1333
1334 if ($feed == -1) {
1335 db_query($link, "UPDATE ttrss_user_entries
1336 SET unread = false,last_read = NOW()
1337 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1338 }
1339
1340 } else if ($feed < -10) { // label
1341
1342 // TODO make this more efficient
1343
1344 $label_id = -$feed - 11;
1345
1346 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1347 WHERE id = '$label_id'");
1348
1349 if ($tmp_result) {
1350 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1351
1352 db_query($link, "BEGIN");
1353
1354 $tmp2_result = db_query($link,
1355 "SELECT
1356 int_id
1357 FROM
88040f57 1358 ttrss_user_entries,ttrss_entries,ttrss_feeds
23aa0d16 1359 WHERE
88040f57
AD
1360 ref_id = ttrss_entries.id AND
1361 ttrss_user_entries.feed_id = ttrss_feeds.id AND
23aa0d16 1362 $sql_exp AND
88040f57 1363 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
23aa0d16
AD
1364
1365 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1366 db_query($link, "UPDATE
1367 ttrss_user_entries
1368 SET
1369 unread = false, last_read = NOW()
1370 WHERE
1371 int_id = " . $tmp_line["int_id"]);
1372 }
1373
1374 db_query($link, "COMMIT");
1375
1376/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1377 SET unread = false,last_read = NOW()
1378 WHERE $sql_exp
1379 AND ref_id = id
1380 AND owner_uid = ".$_SESSION["uid"]); */
1381 }
1382 }
1383 } else { // tag
1384 db_query($link, "BEGIN");
1385
1386 $tag_name = db_escape_string($feed);
1387
1388 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1389 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1390
1391 while ($line = db_fetch_assoc($result)) {
1392 db_query($link, "UPDATE ttrss_user_entries SET
1393 unread = false, last_read = NOW()
1394 WHERE int_id = " . $line["post_int_id"]);
1395 }
1396 db_query($link, "COMMIT");
1397 }
1398 }
1399
1400 function update_generic_feed($link, $feed, $cat_view) {
1401 if ($cat_view) {
1402
1403 if ($feed > 0) {
1404 $cat_qpart = "cat_id = '$feed'";
1405 } else {
1406 $cat_qpart = "cat_id IS NULL";
1407 }
1408
1409 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1410 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1411
1412 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1413 $feed_url = $tmp_line["feed_url"];
1414 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1415 }
1416
1417 } else {
1418 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1419 WHERE id = '$feed'");
1420 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1421 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1422 }
1423 }
a9cb1f83
AD
1424
1425 function getAllCounters($link) {
1426 getLabelCounters($link);
1427 getFeedCounters($link);
1428 getTagCounters($link);
1429 getGlobalCounters($link);
1430 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1431 getCategoryCounters($link);
1432 }
1433 }
1434
1435 function getCategoryCounters($link) {
1436 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1437 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1438 AND unread = true)) AS unread FROM ttrss_feeds
1439 WHERE
1440 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1441
1442 while ($line = db_fetch_assoc($result)) {
1443 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1444 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1445 $line["unread"]."\"/>";
1446 }
1447 }
1448
f295c368
AD
1449 function getCategoryUnread($link, $cat) {
1450
18664970
AD
1451 if ($cat != 0) {
1452 $cat_query = "cat_id = '$cat'";
1453 } else {
1454 $cat_query = "cat_id IS NULL";
1455 }
1456
1457 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
f295c368
AD
1458 AND owner_uid = " . $_SESSION["uid"]);
1459
1460 $cat_feeds = array();
1461 while ($line = db_fetch_assoc($result)) {
1462 array_push($cat_feeds, "feed_id = " . $line["id"]);
1463 }
1464
18664970
AD
1465 if (count($cat_feeds) == 0) return 0;
1466
f295c368
AD
1467 $match_part = implode(" OR ", $cat_feeds);
1468
1469 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1470 FROM ttrss_user_entries
4919fb42 1471 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
f295c368
AD
1472
1473 $unread = 0;
1474
1475 # this needs to be rewritten
1476 while ($line = db_fetch_assoc($result)) {
1477 $unread += $line["unread"];
1478 }
1479
1480 return $unread;
1481
1482 }
1483
1484 function getFeedUnread($link, $feed, $is_cat = false) {
a9cb1f83 1485 $n_feed = sprintf("%d", $feed);
f295c368
AD
1486
1487 if ($is_cat) {
831ff047 1488 return getCategoryUnread($link, $n_feed);
f295c368 1489 } else if ($n_feed == -1) {
a9cb1f83 1490 $match_part = "marked = true";
4919fb42 1491 } else if ($n_feed > 0) {
831ff047 1492
4919fb42
AD
1493 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
1494 AND owner_uid = " . $_SESSION["uid"]);
831ff047
AD
1495
1496 if (db_num_rows($result) > 0) {
4919fb42 1497
831ff047
AD
1498 $linked_feeds = array();
1499 while ($line = db_fetch_assoc($result)) {
1500 array_push($linked_feeds, "feed_id = " . $line["id"]);
1501 }
1502
1503 $match_part = implode(" OR ", $linked_feeds);
1504
4919fb42
AD
1505 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1506 FROM ttrss_user_entries
1507 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1508
1509 $unread = 0;
1510
1511 # this needs to be rewritten
1512 while ($line = db_fetch_assoc($result)) {
1513 $unread += $line["unread"];
1514 }
1515
1516 return $unread;
1517
831ff047
AD
1518 } else {
1519 $match_part = "feed_id = '$n_feed'";
1520 }
a9cb1f83
AD
1521 } else if ($feed < -10) {
1522 $label_id = -$feed - 11;
1523
1524 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1525 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1526
1527 $match_part = db_fetch_result($result, 0, "sql_exp");
1528 }
1529
1530 if ($match_part) {
1531
1532 $result = db_query($link, "SELECT count(int_id) AS unread
88040f57
AD
1533 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1534 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1535 ttrss_user_entries.ref_id = ttrss_entries.id AND
1536 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
1537
1538 } else {
1539
1540 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1541 FROM ttrss_tags,ttrss_user_entries
1542 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1543 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1544 }
1545
1546 $unread = db_fetch_result($result, 0, "unread");
1547 return $unread;
1548 }
1549
1550 /* FIXME this needs reworking */
1551
1552 function getGlobalUnread($link) {
1553 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1554 WHERE unread = true AND
1555 ttrss_user_entries.ref_id = ttrss_entries.id AND
1556 owner_uid = " . $_SESSION["uid"]);
1557 $c_id = db_fetch_result($result, 0, "c_id");
1558 return $c_id;
1559 }
1560
1561 function getGlobalCounters($link, $global_unread = -1) {
1562 if ($global_unread == -1) {
1563 $global_unread = getGlobalUnread($link);
1564 }
7bf7e4d3
AD
1565 print "<counter type=\"global\" id='global-unread'
1566 counter='$global_unread'/>";
1567
1568 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1569 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1570
1571 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1572
1573 print "<counter type=\"global\" id='subscribed-feeds'
1574 counter='$subscribed_feeds'/>";
1575
a9cb1f83
AD
1576 }
1577
1578 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1579
1580 if ($smart_mode) {
1581 if (!$_SESSION["tctr_last_value"]) {
1582 $_SESSION["tctr_last_value"] = array();
1583 }
1584 }
1585
1586 $old_counters = $_SESSION["tctr_last_value"];
1587
1588 $tctrs_modified = false;
1589
1590/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1591 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1592 ttrss_user_entries.ref_id = ttrss_entries.id AND
1593 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1594 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1595 UNION
1596 select tag_name,0 as count FROM ttrss_tags
1597 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1598
1599 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1600 FROM ttrss_user_entries WHERE int_id = post_int_id
1601 AND unread = true)) AS count FROM ttrss_tags
1602 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1603
1604 $tags = array();
1605
1606 while ($line = db_fetch_assoc($result)) {
1607 $tags[$line["tag_name"]] += $line["count"];
1608 }
1609
1610 foreach (array_keys($tags) as $tag) {
1611 $unread = $tags[$tag];
1612
1613 $tag = htmlspecialchars($tag);
1614
1615 if (!$smart_mode || $old_counters[$tag] != $unread) {
1616 $old_counters[$tag] = $unread;
1617 $tctrs_modified = true;
1618 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1619 }
1620
1621 }
1622
1623 if ($smart_mode && $tctrs_modified) {
1624 $_SESSION["tctr_last_value"] = $old_counters;
1625 }
1626
1627 }
1628
ef393de7 1629 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
a9cb1f83
AD
1630
1631 if ($smart_mode) {
1632 if (!$_SESSION["lctr_last_value"]) {
1633 $_SESSION["lctr_last_value"] = array();
1634 }
1635 }
1636
ef393de7
AD
1637 $ret_arr = array();
1638
a9cb1f83
AD
1639 $old_counters = $_SESSION["lctr_last_value"];
1640 $lctrs_modified = false;
1641
88040f57 1642 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 1643 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57
AD
1644 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1645 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1646
1647 $count = db_fetch_result($result, 0, "count");
1648
ef393de7
AD
1649 if (!$ret_mode) {
1650 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1651 } else {
1652 $ret_arr["-1"]["counter"] = $count;
1653 $ret_arr["-1"]["description"] = "Starred";
1654 }
a9cb1f83
AD
1655
1656 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1657 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1658
1659 while ($line = db_fetch_assoc($result)) {
1660
1661 $id = -$line["id"] - 11;
1662
ef393de7
AD
1663 $label_name = $line["description"];
1664
a9cb1f83
AD
1665 error_reporting (0);
1666
88040f57 1667 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
a9cb1f83 1668 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
88040f57 1669 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 1670 ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 1671 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1672
1673 $count = db_fetch_result($tmp_result, 0, "count");
1674
1675 if (!$smart_mode || $old_counters[$id] != $count) {
1676 $old_counters[$id] = $count;
1677 $lctrs_modified = true;
ef393de7
AD
1678 if (!$ret_mode) {
1679 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1680 } else {
1681 $ret_arr[$id]["counter"] = $count;
1682 $ret_arr[$id]["description"] = $label_name;
1683 }
a9cb1f83
AD
1684 }
1685
1686 error_reporting (DEFAULT_ERROR_LEVEL);
1687 }
1688
1689 if ($smart_mode && $lctrs_modified) {
1690 $_SESSION["lctr_last_value"] = $old_counters;
1691 }
ef393de7
AD
1692
1693 return $ret_arr;
a9cb1f83
AD
1694 }
1695
1696/* function getFeedCounter($link, $id) {
1697
1698 $result = db_query($link, "SELECT
1699 count(id) as count,last_error
1700 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1701 WHERE feed_id = '$id' AND unread = true
1702 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1703 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1704
1705 $count = db_fetch_result($result, 0, "count");
1706 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1707
1708 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1709 } */
1710
1711 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1712
1713 if ($smart_mode) {
1714 if (!$_SESSION["fctr_last_value"]) {
1715 $_SESSION["fctr_last_value"] = array();
1716 }
1717 }
1718
1719 $old_counters = $_SESSION["fctr_last_value"];
1720
1721 $result = db_query($link, "SELECT id,last_error,parent_feed,
fb1fb4ab 1722 SUBSTRING(last_updated,1,19) AS last_updated,
a9cb1f83
AD
1723 (SELECT count(id)
1724 FROM ttrss_entries,ttrss_user_entries
1725 WHERE feed_id = ttrss_feeds.id AND
1726 ttrss_user_entries.ref_id = ttrss_entries.id
1727 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1728 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1729 AND parent_feed IS NULL");
1730
1731 $fctrs_modified = false;
1732
fb1fb4ab
AD
1733 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1734
a9cb1f83
AD
1735 while ($line = db_fetch_assoc($result)) {
1736
1737 $id = $line["id"];
1738 $count = $line["count"];
1739 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
1740
1741 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1742 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1743 } else {
1744 $last_updated = date($short_date, strtotime($line["last_updated"]));
1745 }
1746
a9cb1f83
AD
1747 $has_img = is_file(ICONS_DIR . "/$id.ico");
1748
1749 $tmp_result = db_query($link,
1750 "SELECT id,COUNT(unread) AS unread
1751 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1752 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1753 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1754
1755 if (db_num_rows($tmp_result) > 0) {
1756 while ($l = db_fetch_assoc($tmp_result)) {
1757 $count += $l["unread"];
1758 }
1759 }
1760
1761 if (!$smart_mode || $old_counters[$id] != $count) {
1762 $old_counters[$id] = $count;
1763 $fctrs_modified = true;
1764
1765 if ($last_error) {
1766 $error_part = "error=\"$last_error\"";
1767 } else {
1768 $error_part = "";
1769 }
1770
1771 if ($has_img) {
1772 $has_img_part = "hi=\"$has_img\"";
1773 } else {
1774 $has_img_part = "";
1775 }
1776
fb1fb4ab 1777 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
a9cb1f83
AD
1778 }
1779 }
1780
1781 if ($smart_mode && $fctrs_modified) {
1782 $_SESSION["fctr_last_value"] = $old_counters;
1783 }
1784 }
1785
1b758780
AD
1786 function get_script_dt_add() {
1787 if (strpos(VERSION, "99") === false) {
1788 return VERSION;
1789 } else {
1790 return time();
1791 }
1792 }
1793
6e7f8d26
AD
1794 function get_pgsql_version($link) {
1795 $result = db_query($link, "SELECT version() AS version");
1796 $version = split(" ", db_fetch_result($result, 0, "version"));
1797 return $version[1];
1798 }
1799
af106b0e
AD
1800 function print_error_xml($code, $add_msg = "") {
1801 global $ERRORS;
1802
1803 $error_msg = $ERRORS[$code];
1804
1805 if ($add_msg) {
1806 $error_msg = "$error_msg; $add_msg";
1807 }
1808
4c2abbc1 1809 print "<rpc-reply>";
af106b0e 1810 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 1811 print "</rpc-reply>";
af106b0e 1812 }
956c7629
AD
1813
1814 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1815
1816 if ($cat_id == "0" || !$cat_id) {
1817 $cat_qpart = "NULL";
1818 } else {
1819 $cat_qpart = "'$cat_id'";
1820 }
1821
1822 $result = db_query($link,
1823 "SELECT id FROM ttrss_feeds
1824 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1825
1826 if (db_num_rows($result) == 0) {
1827
1828 $result = db_query($link,
1829 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1830 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1831 '[Unknown]', $cat_qpart)");
1832
1833 $result = db_query($link,
1834 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1835 AND owner_uid = " . $_SESSION["uid"]);
1836
1837 $feed_id = db_fetch_result($result, 0, "id");
1838
1839 if ($feed_id) {
1840 update_rss_feed($link, $feed_link, $feed_id, true);
1841 }
1842
1843 return true;
1844 } else {
1845 return false;
1846 }
1847 }
1848
673d54ca
AD
1849 function print_feed_select($link, $id, $default_id = "",
1850 $attributes = "", $include_all_feeds = true) {
1851
79f3553b 1852 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 1853 if ($include_all_feeds) {
79f3553b 1854 print "<option value=\"0\">All feeds</option>";
673d54ca
AD
1855 }
1856
1857 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1858 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1859
1860 if (db_num_rows($result) > 0 && $include_all_feeds) {
1861 print "<option disabled>--------</option>";
1862 }
1863
1864 while ($line = db_fetch_assoc($result)) {
1865 if ($line["id"] == $default_id) {
1866 $is_selected = "selected";
1867 } else {
1868 $is_selected = "";
1869 }
79f3553b 1870 printf("<option $is_selected value='%d'>%s</option>",
07164479 1871 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
673d54ca
AD
1872 }
1873
1874 print "</select>";
1875 }
1876
1877 function print_feed_cat_select($link, $id, $default_id = "",
1878 $attributes = "", $include_all_cats = true) {
1879
79f3553b 1880 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
1881
1882 if ($include_all_cats) {
14f69488 1883 print "<option value=\"0\">Uncategorized</option>";
673d54ca
AD
1884 }
1885
1886 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1887 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1888
1889 if (db_num_rows($result) > 0 && $include_all_cats) {
1890 print "<option disabled>--------</option>";
1891 }
1892
1893 while ($line = db_fetch_assoc($result)) {
1894 if ($line["id"] == $default_id) {
1895 $is_selected = "selected";
1896 } else {
1897 $is_selected = "";
1898 }
14f69488 1899 printf("<option $is_selected value='%d'>%s</option>",
07164479 1900 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
673d54ca
AD
1901 }
1902
1903 print "</select>";
1904 }
1905
14f69488
AD
1906 function checkbox_to_sql_bool($val) {
1907 return ($val == "on") ? "true" : "false";
1908 }
86b682ce
AD
1909
1910 function getFeedCatTitle($link, $id) {
1911 if ($id == -1) {
1912 return "Special";
1913 } else if ($id < -10) {
1914 return "Labels";
1915 } else if ($id > 0) {
1916 $result = db_query($link, "SELECT ttrss_feed_categories.title
1917 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1918 cat_id = ttrss_feed_categories.id");
1919 if (db_num_rows($result) == 1) {
1920 return db_fetch_result($result, 0, "title");
1921 } else {
eb4311d0 1922 return "Uncategorized";
86b682ce
AD
1923 }
1924 } else {
1925 return "getFeedCatTitle($id) failed";
1926 }
1927
1928 }
1929
1930 function getFeedTitle($link, $id) {
1931 if ($id == -1) {
1932 return "Starred articles";
1933 } else if ($id < -10) {
1934 $label_id = -10 - $id;
1935 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
1936 if (db_num_rows($result) == 1) {
1937 return db_fetch_result($result, 0, "description");
1938 } else {
1939 return "Unknown label ($label_id)";
1940 }
1941
1942 } else if ($id > 0) {
1943 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
1944 if (db_num_rows($result) == 1) {
1945 return db_fetch_result($result, 0, "title");
1946 } else {
1947 return "Unknown feed ($id)";
1948 }
1949 } else {
1950 return "getFeedTitle($id) failed";
1951 }
1952
1953 }
3dd46f19
AD
1954
1955 function get_session_cookie_name() {
1956 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
1957 }
3ac2b520
AD
1958
1959 function print_init_params($link) {
1960 print "<init-params>";
1961 if ($_SESSION["stored-params"]) {
1962 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
1963 if ($key) {
1964 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
1965 print "<param key=\"$key\" value=\"$value\"/>";
1966 }
3ac2b520
AD
1967 }
1968 }
1969
1970 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
1971 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
0d51e25d 1972 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
3ac2b520
AD
1973
1974 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
1975 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
1976
e8bd0da9
AD
1977 print "<param key=\"hide_read_feeds\" value=\"" .
1978 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
1979
c9268ed5
AD
1980 print "<param key=\"feeds_sort_by_unread\" value=\"" .
1981 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
1982
3ac2b520
AD
1983 print "</init-params>";
1984 }
f54f515f
AD
1985
1986 function print_runtime_info($link) {
1987 print "<runtime-info>";
71ad883b
AD
1988 if (ENABLE_UPDATE_DAEMON) {
1989 print "<param key=\"daemon_is_running\" value=\"".
1990 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
1991 }
f54f515f
AD
1992 print "</runtime-info>";
1993 }
ef393de7 1994
88040f57 1995 function getSearchSql($search, $match_on) {
ef393de7 1996
88040f57 1997 $search_query_part = "";
e20c9d88 1998
88040f57
AD
1999 $keywords = split(" ", $search);
2000 $query_keywords = array();
e20c9d88 2001
88040f57 2002 if ($match_on == "both") {
e20c9d88 2003
88040f57
AD
2004 foreach ($keywords as $k) {
2005 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2006 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2007 }
e20c9d88 2008
88040f57 2009 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2010
88040f57 2011 } else if ($match_on == "title") {
e20c9d88 2012
88040f57
AD
2013 foreach ($keywords as $k) {
2014 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2015 }
e20c9d88 2016
88040f57 2017 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2018
88040f57
AD
2019 } else if ($match_on == "content") {
2020
2021 foreach ($keywords as $k) {
2022 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2023 }
2024 }
2025
2026 $search_query_part = implode("AND", $query_keywords);
2027
2028 return $search_query_part;
2029 }
2030
2031 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
88040f57
AD
2032 if ($search) {
2033
2034 $search_query_part = getSearchSql($search, $match_on);
2035 $search_query_part .= " AND ";
e20c9d88 2036
ef393de7
AD
2037 } else {
2038 $search_query_part = "";
2039 }
2040
2041 $view_query_part = "";
2042
2043 if ($view_mode == "adaptive") {
2044 if ($search) {
2045 $view_query_part = " ";
2046 } else if ($feed != -1) {
f295c368 2047 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
2048 if ($unread > 0) {
2049 $view_query_part = " unread = true AND ";
2050 }
2051 }
2052 }
2053
2054 if ($view_mode == "marked") {
2055 $view_query_part = " marked = true AND ";
2056 }
2057
2058 if ($view_mode == "unread") {
2059 $view_query_part = " unread = true AND ";
2060 }
2061
2062 if ($limit > 0) {
2063 $limit_query_part = "LIMIT " . $limit;
2064 }
2065
2066 $vfeed_query_part = "";
2067
2068 // override query strategy and enable feed display when searching globally
2069 if ($search && $search_mode == "all_feeds") {
2070 $query_strategy_part = "ttrss_entries.id > 0";
2071 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2072 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2073 $query_strategy_part = "ttrss_entries.id > 0";
2074 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2075 id = feed_id) as feed_title,";
2076 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2077
2078 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2079
2080 $tmp_result = false;
2081
2082 if ($cat_view) {
2083 $tmp_result = db_query($link, "SELECT id
2084 FROM ttrss_feeds WHERE cat_id = '$feed'");
2085 } else {
2086 $tmp_result = db_query($link, "SELECT id
2087 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2088 WHERE id = '$feed') AND id != '$feed'");
2089 }
ef393de7
AD
2090
2091 $cat_siblings = array();
2092
2093 if (db_num_rows($tmp_result) > 0) {
2094 while ($p = db_fetch_assoc($tmp_result)) {
2095 array_push($cat_siblings, "feed_id = " . $p["id"]);
2096 }
2097
2098 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2099 $feed, implode(" OR ", $cat_siblings));
2100
2101 } else {
2102 $query_strategy_part = "ttrss_entries.id > 0";
2103 }
2104
2105 } else if ($feed >= 0) {
2106
2107 if ($cat_view) {
5c365f60 2108
ef393de7
AD
2109 if ($feed > 0) {
2110 $query_strategy_part = "cat_id = '$feed'";
2111 } else {
2112 $query_strategy_part = "cat_id IS NULL";
2113 }
2114
2115 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 2116
ef393de7
AD
2117 } else {
2118 $tmp_result = db_query($link, "SELECT id
2119 FROM ttrss_feeds WHERE parent_feed = '$feed'
2120 ORDER BY cat_id,title");
2121
2122 $parent_ids = array();
2123
2124 if (db_num_rows($tmp_result) > 0) {
2125 while ($p = db_fetch_assoc($tmp_result)) {
2126 array_push($parent_ids, "feed_id = " . $p["id"]);
2127 }
2128
2129 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2130 $feed, implode(" OR ", $parent_ids));
2131
2132 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2133 } else {
2134 $query_strategy_part = "feed_id = '$feed'";
2135 }
2136 }
2137 } else if ($feed == -1) { // starred virtual feed
2138 $query_strategy_part = "marked = true";
2139 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2140 } else if ($feed <= -10) { // labels
2141 $label_id = -$feed - 11;
2142
2143 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2144 WHERE id = '$label_id'");
2145
2146 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2147
2148 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2149 } else {
2150 $query_strategy_part = "id > 0"; // dumb
2151 }
d6e5706d
AD
2152
2153 if (get_pref($link, 'REVERSE_HEADLINES')) {
2154 $order_by = "updated";
2155 } else {
2156 $order_by = "updated DESC";
2157 }
e939722a
AD
2158
2159 if ($override_order) {
2160 $order_by = $override_order;
2161 }
ef393de7
AD
2162
2163 $feed_title = "";
2164
2165 if ($search && $search_mode == "all_feeds") {
2166 $feed_title = "Global search results ($search)";
2167 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
e1eb2147 2168 $feed_title = "Tag search results ($search, $feed)";
ef393de7
AD
2169 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2170 $feed_title = $feed;
2171 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2172
2173 if ($cat_view) {
5c365f60 2174
ef393de7
AD
2175 if ($feed != 0) {
2176 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2177 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2178 $feed_title = db_fetch_result($result, 0, "title");
2179 } else {
2180 $feed_title = "Uncategorized";
2181 }
e1eb2147
AD
2182
2183 if ($search) {
2184 $feed_title = "Category search results ($search, $feed_title)";
2185 }
2186
ef393de7
AD
2187 } else {
2188
2189 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2190 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2191
2192 $feed_title = db_fetch_result($result, 0, "title");
2193 $feed_site_url = db_fetch_result($result, 0, "site_url");
2194 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
2195
2196 if ($search) {
2197 $feed_title = "Feed search results ($search, $feed_title)";
2198 }
ef393de7
AD
2199 }
2200
2201 } else if ($feed == -1) {
2202 $feed_title = "Starred articles";
2203 } else if ($feed < -10) {
2204 $label_id = -$feed - 11;
2205 $result = db_query($link, "SELECT description FROM ttrss_labels
2206 WHERE id = '$label_id'");
2207 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
2208
2209 if ($search) {
2210 $feed_title = "Label search results ($search, $feed_title)";
2211 }
ef393de7
AD
2212 } else {
2213 $feed_title = "?";
2214 }
2215
2216 $feed_title = db_unescape_string($feed_title);
2217
2218 if ($feed < -10) error_reporting (0);
2219
2220 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2221
2222 if ($feed >= 0) {
2223 $feed_kind = "Feeds";
2224 } else {
2225 $feed_kind = "Labels";
2226 }
2227
2228 $content_query_part = "content as content_preview,";
2229
2230 $query = "SELECT
2231 ttrss_entries.id,ttrss_entries.title,
2232 SUBSTRING(updated,1,16) as updated,
2233 unread,feed_id,marked,link,last_read,
2234 SUBSTRING(last_read,1,19) as last_read_noms,
2235 $vfeed_query_part
2236 $content_query_part
2237 SUBSTRING(updated,1,19) as updated_noms
2238 FROM
2239 ttrss_entries,ttrss_user_entries,ttrss_feeds
2240 WHERE
2241 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2242 ttrss_user_entries.ref_id = ttrss_entries.id AND
2243 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2244 $search_query_part
2245 $view_query_part
2246 $query_strategy_part ORDER BY $order_by
2247 $limit_query_part";
2248
2249 $result = db_query($link, $query);
2250
2251 if ($_GET["debug"]) print $query;
2252
2253 } else {
2254 // browsing by tag
2255
2256 $feed_kind = "Tags";
2257
2258 $result = db_query($link, "SELECT
2259 ttrss_entries.id as id,title,
2260 SUBSTRING(updated,1,16) as updated,
2261 unread,feed_id,
2262 marked,link,last_read,
2263 SUBSTRING(last_read,1,19) as last_read_noms,
2264 $vfeed_query_part
2265 $content_query_part
2266 SUBSTRING(updated,1,19) as updated_noms
2267 FROM
2268 ttrss_entries,ttrss_user_entries,ttrss_tags
2269 WHERE
2270 ref_id = ttrss_entries.id AND
2271 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2272 post_int_id = int_id AND tag_name = '$feed' AND
2273 $view_query_part
2274 $search_query_part
2275 $query_strategy_part ORDER BY $order_by
2276 $limit_query_part");
2277 }
2278
c7188969 2279 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
2280
2281 }
2282
e1eb2147
AD
2283 function generate_syndicated_feed($link, $feed, $is_cat,
2284 $search, $search_mode, $match_on) {
18664970
AD
2285
2286 $qfh_ret = queryFeedHeadlines($link, $feed,
e939722a 2287 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
18664970
AD
2288
2289 $result = $qfh_ret[0];
59e2aab4 2290 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
2291 $feed_site_url = $qfh_ret[2];
2292 $last_error = $qfh_ret[3];
2293
2294 print "<rss version=\"2.0\">
2295 <channel>
2296 <title>$feed_title</title>
2297 <link>$feed_site_url</link>
2298 <generator>Tiny Tiny RSS v".VERSION."</generator>";
2299
2300 while ($line = db_fetch_assoc($result)) {
2301 print "<item>";
2302 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2303
2304 $rfc822_date = date('r', strtotime($line["updated"]));
2305
2306 print "<pubDate>$rfc822_date</pubDate>";
2307
2308 print "<title>" .
2309 htmlspecialchars($line["title"]) . "</title>";
2310
2311 print "<description>" .
2312 htmlspecialchars($line["content_preview"]) . "</description>";
2313
2314 print "</item>";
2315 }
2316
2317 print "</channel></rss>";
2318
2319 }
2320
0a6c4846
AD
2321 function getCategoryTitle($link, $cat_id) {
2322
2323 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2324 id = '$cat_id'");
2325
2326 if (db_num_rows($result) == 1) {
2327 return db_fetch_result($result, 0, "title");
2328 } else {
2329 return "Uncategorized";
2330 }
2331 }
2332
183ad07b
AD
2333 function sanitize_rss($str) {
2334 $res = "";
2335
2336 $res = preg_replace('/<script.*?>/i',
2337 "<p class=\"scriptWarn\">", $str);
2338
2339 $res = preg_replace('/<\/script>/i',
2340 "</p>", $res);
2341
2342 return $res;
2343 }
40d13c28 2344?>