]> git.wh0rd.org - tt-rss.git/blame - functions.php
remove early mark-as-read hack on feed catchup
[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
44e241cb
AD
533 db_query($link, "BEGIN");
534
4c193675
AD
535 if (db_num_rows($result) == 0) {
536
537 // base post entry does not exist, create it
538
4c193675
AD
539 $result = db_query($link,
540 "INSERT INTO ttrss_entries
541 (title,
542 guid,
543 link,
544 updated,
545 content,
546 content_hash,
547 no_orig_date,
548 date_entered,
11b0dce2 549 comments,
b6104dee
AD
550 num_comments,
551 author)
4c193675
AD
552 VALUES
553 ('$entry_title',
554 '$entry_guid',
555 '$entry_link',
556 '$entry_timestamp_fmt',
557 '$entry_content',
558 '$content_hash',
559 $no_orig_date,
560 NOW(),
11b0dce2 561 '$entry_comments',
b6104dee
AD
562 '$num_comments',
563 '$entry_author')");
8926aab8
AD
564 } else {
565 // we keep encountering the entry in feeds, so we need to
566 // update date_entered column so that we don't get horrible
567 // dupes when the entry gets purged and reinserted again e.g.
568 // in the case of SLOW SLOW OMG SLOW updating feeds
569
570 $base_entry_id = db_fetch_result($result, 0, "id");
571
572 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
573 WHERE id = '$base_entry_id'");
4c193675
AD
574 }
575
576 // now it should exist, if not - bad luck then
577
6385315d
AD
578 $result = db_query($link, "SELECT
579 id,content_hash,no_orig_date,title,
8926aab8 580 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
581 substring(updated,1,19) as updated,
582 num_comments
6385315d
AD
583 FROM
584 ttrss_entries
585 WHERE guid = '$entry_guid'");
4c193675
AD
586
587 if (db_num_rows($result) == 1) {
588
11b0dce2
AD
589 // this will be used below in update handler
590 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
591 $orig_title = db_fetch_result($result, 0, "title");
592 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
593 $orig_date_entered = strtotime(db_fetch_result($result,
594 0, "date_entered"));
6385315d 595
11b0dce2 596 $ref_id = db_fetch_result($result, 0, "id");
4c193675 597
11b0dce2 598 // check for user post link to main table
4c193675 599
11b0dce2
AD
600 // do we allow duplicate posts with same GUID in different feeds?
601 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
602 $dupcheck_qpart = "AND feed_id = '$feed'";
603 } else {
604 $dupcheck_qpart = "";
605 }
71604ca4 606
11b0dce2 607// error_reporting(0);
19c9cb11 608
11b0dce2
AD
609 $filter_name = get_filter_name($entry_title, $entry_content,
610 $entry_link, $filters);
19c9cb11 611
11b0dce2
AD
612 if ($filter_name == "filter") {
613 continue;
614 }
19c9cb11 615
11b0dce2 616// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 617
11b0dce2
AD
618 $result = db_query($link,
619 "SELECT ref_id FROM ttrss_user_entries WHERE
620 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
621 $dupcheck_qpart");
622
623 // okay it doesn't exist - create user entry
624 if (db_num_rows($result) == 0) {
625
626 if ($filter_name != 'catchup') {
627 $unread = 'true';
628 $last_read_qpart = 'NULL';
629 } else {
630 $unread = 'false';
631 $last_read_qpart = 'NOW()';
632 }
dd7d3187
AD
633
634 if ($filter_name == 'mark') {
635 $marked = 'true';
636 } else {
637 $marked = 'false';
638 }
19c9cb11 639
11b0dce2
AD
640 $result = db_query($link,
641 "INSERT INTO ttrss_user_entries
dd7d3187 642 (ref_id, owner_uid, feed_id, unread, last_read, marked)
11b0dce2 643 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
dd7d3187 644 $last_read_qpart, $marked)");
11b0dce2
AD
645 }
646
6385315d
AD
647 $post_needs_update = false;
648
a2770077 649 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
6385315d
AD
650 ($content_hash != $orig_content_hash)) {
651 $post_needs_update = true;
652 }
653
654 if ($orig_title != $entry_title) {
655 $post_needs_update = true;
656 }
657
11b0dce2
AD
658 if ($orig_num_comments != $num_comments) {
659 $post_needs_update = true;
660 }
661
6385315d
AD
662// this doesn't seem to be very reliable
663//
664// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
665// $post_needs_update = true;
666// }
667
668 // if post needs update, update it and mark all user entries
1c73bc0c 669 // linking to this post as updated
6385315d
AD
670 if ($post_needs_update) {
671
672// print "<!-- post $orig_title needs update : $post_needs_update -->";
673
6385315d 674 db_query($link, "UPDATE ttrss_entries
11b0dce2
AD
675 SET title = '$entry_title', content = '$entry_content',
676 num_comments = '$num_comments'
6385315d
AD
677 WHERE id = '$ref_id'");
678
4919fb42
AD
679 if (get_pref($link, "MARK_UNREAD_ON_UPDATE")) {
680 db_query($link, "UPDATE ttrss_user_entries
681 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
682 } else {
683 db_query($link, "UPDATE ttrss_user_entries
684 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
685 }
6385315d
AD
686
687 }
4c193675
AD
688 }
689
44e241cb
AD
690 db_query($link, "COMMIT");
691
eb36b4eb
AD
692 /* taaaags */
693 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
694
05732aa0 695 $entry_tags = null;
eb36b4eb 696
372ced8b 697 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
ee2c3050
AD
698 $entry_content_unescaped, $entry_tags);
699
700// print "<br>$entry_title : $entry_content_unescaped<br>";
701// print_r($entry_tags);
372ced8b 702// print "<br>";
eb36b4eb
AD
703
704 $entry_tags = $entry_tags[1];
705
706 if (count($entry_tags) > 0) {
707
44e241cb
AD
708 db_query($link, "BEGIN");
709
05732aa0
AD
710 $result = db_query($link, "SELECT id,int_id
711 FROM ttrss_entries,ttrss_user_entries
25da6909 712 WHERE guid = '$entry_guid'
05732aa0 713 AND feed_id = '$feed' AND ref_id = id
7fed1940 714 AND owner_uid = '$owner_uid'");
eb36b4eb 715
fe99ab12 716 if (db_num_rows($result) == 1) {
eb36b4eb 717
fe99ab12
AD
718 $entry_id = db_fetch_result($result, 0, "id");
719 $entry_int_id = db_fetch_result($result, 0, "int_id");
720
721 foreach ($entry_tags as $tag) {
722 $tag = db_escape_string(strtolower($tag));
31483fc1
AD
723
724 $tag = str_replace("+", " ", $tag);
fe99ab12
AD
725 $tag = str_replace("technorati tag: ", "", $tag);
726
727 $result = db_query($link, "SELECT id FROM ttrss_tags
728 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
729 owner_uid = '$owner_uid' LIMIT 1");
730
731 // print db_fetch_result($result, 0, "id");
732
733 if ($result && db_num_rows($result) == 0) {
734
735 // print "tagging $entry_id as $tag<br>";
736
737 db_query($link, "INSERT INTO ttrss_tags
738 (owner_uid,tag_name,post_int_id)
739 VALUES ('$owner_uid','$tag', '$entry_int_id')");
740 }
741 }
eb36b4eb 742 }
44e241cb 743 db_query($link, "COMMIT");
05732aa0 744 }
4c193675 745 }
40d13c28 746
ab3d0b99
AD
747 db_query($link, "UPDATE ttrss_feeds
748 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 749
44e241cb 750// db_query($link, "COMMIT");
dd8c76a9 751
ab3d0b99
AD
752 } else {
753 $error_msg = db_escape_string(magpie_error());
754 db_query($link,
aa5f9f5f
AD
755 "UPDATE ttrss_feeds SET last_error = '$error_msg',
756 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
757 }
758
759 }
760
f175937c 761 function print_select($id, $default, $values, $attributes = "") {
79f3553b 762 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
763 foreach ($values as $v) {
764 if ($v == $default)
765 $sel = " selected";
766 else
767 $sel = "";
768
769 print "<option$sel>$v</option>";
770 }
771 print "</select>";
772 }
40d13c28 773
79f3553b
AD
774 function print_select_hash($id, $default, $values, $attributes = "") {
775 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
776 foreach (array_keys($values) as $v) {
777 if ($v == $default)
778 $sel = "selected";
779 else
780 $sel = "";
781
782 print "<option $sel value=\"$v\">".$values[$v]."</option>";
783 }
784
785 print "</select>";
786 }
787
19c9cb11 788 function get_filter_name($title, $content, $link, $filters) {
e6155a06
AD
789
790 if ($filters["title"]) {
19c9cb11
AD
791 foreach ($filters["title"] as $filter) {
792 $reg_exp = $filter["reg_exp"];
793 if (preg_match("/$reg_exp/i", $title)) {
794 return $filter["action"];
795 }
e6155a06
AD
796 }
797 }
798
799 if ($filters["content"]) {
19c9cb11
AD
800 foreach ($filters["content"] as $filter) {
801 $reg_exp = $filter["reg_exp"];
802 if (preg_match("/$reg_exp/i", $content)) {
803 return $filter["action"];
804 }
e6155a06
AD
805 }
806 }
807
808 if ($filters["both"]) {
809 foreach ($filters["both"] as $filter) {
19c9cb11
AD
810 $reg_exp = $filter["reg_exp"];
811 if (preg_match("/$reg_exp/i", $title) ||
812 preg_match("/$reg_exp/i", $content)) {
813 return $filter["action"];
814 }
e6155a06
AD
815 }
816 }
817
3a933f22 818 if ($filters["link"]) {
19c9cb11
AD
819 $reg_exp = $filter["reg_exp"];
820 foreach ($filters["link"] as $filter) {
821 $reg_exp = $filter["reg_exp"];
822 if (preg_match("/$reg_exp/i", $link)) {
823 return $filter["action"];
824 }
3a933f22
AD
825 }
826 }
827
e6155a06
AD
828 return false;
829 }
830
9323147e 831 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 832 $rtl_content = false, $last_updated = false, $last_error = false) {
254e0e4b
AD
833
834 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 835 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 836 } else {
023fe037 837 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
838 }
839
9323147e
AD
840 if ($rtl_content) {
841 $rtl_tag = "dir=\"rtl\"";
842 } else {
843 $rtl_tag = "dir=\"ltr\"";
844 }
845
78d5212c
AD
846 $error_notify_msg = "";
847
fb1fb4ab
AD
848 if ($last_error) {
849 $link_title = "Error: $last_error ($last_updated)";
78d5212c 850 $error_notify_msg = "(Error)";
ad780e9c 851 } else if ($last_updated) {
fb1fb4ab
AD
852 $link_title = "Updated: $last_updated";
853 }
854
767e2486 855 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
254e0e4b
AD
856
857 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 858 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
859 print "$feed_icon";
860 }
861
9323147e 862 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
863
864 if ($unread != 0) {
865 $fctr_class = "";
866 } else {
867 $fctr_class = "class=\"invisible\"";
868 }
869
9323147e 870 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b 871 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
78d5212c
AD
872
873 if (get_pref($link, "EXTENDED_FEEDLIST")) {
874 print "<div class=\"feedExtInfo\">
875 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
876 }
877
254e0e4b
AD
878 print "</li>";
879
880 }
881
406d9489
AD
882 function getmicrotime() {
883 list($usec, $sec) = explode(" ",microtime());
884 return ((float)$usec + (float)$sec);
885 }
886
77e96719
AD
887 function print_radio($id, $default, $values, $attributes = "") {
888 foreach ($values as $v) {
889
890 if ($v == $default)
5da169d9 891 $sel = "checked";
77e96719 892 else
5da169d9
AD
893 $sel = "";
894
895 if ($v == "Yes") {
896 $sel .= " value=\"1\"";
897 } else {
898 $sel .= " value=\"0\"";
899 }
77e96719 900
69654950
AD
901 print "<input class=\"noborder\"
902 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
903
904 }
905 }
906
ff485f1d
AD
907 function initialize_user_prefs($link, $uid) {
908
909 $uid = db_escape_string($uid);
910
911 db_query($link, "BEGIN");
912
913 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
914
915 $u_result = db_query($link, "SELECT pref_name
916 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
917
918 $active_prefs = array();
919
920 while ($line = db_fetch_assoc($u_result)) {
921 array_push($active_prefs, $line["pref_name"]);
922 }
923
924 while ($line = db_fetch_assoc($result)) {
925 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
926// print "adding " . $line["pref_name"] . "<br>";
927
928 db_query($link, "INSERT INTO ttrss_user_prefs
929 (owner_uid,pref_name,value) VALUES
930 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
931
932 }
933 }
934
935 db_query($link, "COMMIT");
936
937 }
956c7629
AD
938
939 function lookup_user_id($link, $user) {
940
941 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
942 login = '$login'");
943
944 if (db_num_rows($result) == 1) {
945 return db_fetch_result($result, 0, "id");
946 } else {
947 return false;
948 }
949 }
950
18664970
AD
951 function http_authenticate_user($link) {
952
953 if (!$_SERVER["PHP_AUTH_USER"]) {
954
955 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
956 header('HTTP/1.0 401 Unauthorized');
957 exit;
958
959 } else {
960 $auth_result = authenticate_user($link,
961 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
962
963 if (!$auth_result) {
964 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
965 header('HTTP/1.0 401 Unauthorized');
966 exit;
967 }
968 }
969
970 return true;
971 }
972
c8437f35
AD
973 function authenticate_user($link, $login, $password) {
974
131b01b3 975 if (!SINGLE_USER_MODE) {
c8437f35 976
131b01b3
AD
977 $pwd_hash = 'SHA1:' . sha1($password);
978
979 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
980 login = '$login' AND pwd_hash = '$pwd_hash'");
981
982 if (db_num_rows($result) == 1) {
983 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
984 $_SESSION["name"] = db_fetch_result($result, 0, "login");
985 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
986
987 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
988 $_SESSION["uid"]);
989
990 $user_theme = get_user_theme_path($link);
991
992 $_SESSION["theme"] = $user_theme;
993 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
994
995 initialize_user_prefs($link, $_SESSION["uid"]);
996
997 return true;
998 }
999
1000 return false;
503eb349 1001
131b01b3 1002 } else {
503eb349 1003
131b01b3
AD
1004 $_SESSION["uid"] = 1;
1005 $_SESSION["name"] = "admin";
f557cd78 1006
0bbba72d
AD
1007 $user_theme = get_user_theme_path($link);
1008
1009 $_SESSION["theme"] = $user_theme;
1010 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1011
1012 initialize_user_prefs($link, $_SESSION["uid"]);
1013
c8437f35
AD
1014 return true;
1015 }
c8437f35
AD
1016 }
1017
e6cb77a0
AD
1018 function make_password($length = 8) {
1019
1020 $password = "";
798f722b
AD
1021 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1022
1023 $i = 0;
e6cb77a0
AD
1024
1025 while ($i < $length) {
1026 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1027
1028 if (!strstr($password, $char)) {
1029 $password .= $char;
1030 $i++;
1031 }
1032 }
1033 return $password;
1034 }
1035
1036 // this is called after user is created to initialize default feeds, labels
1037 // or whatever else
1038
1039 // user preferences are checked on every login, not here
1040
1041 function initialize_user($link, $uid) {
1042
1043 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1044 values ('$uid','unread = true', 'Unread articles')");
1045
1046 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1047 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1048
1049 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1050 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 1051 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b
AD
1052
1053 }
e6cb77a0 1054
b8aa49bc 1055 function logout_user() {
5ccc1cf5
AD
1056 session_destroy();
1057 if (isset($_COOKIE[session_name()])) {
1058 setcookie(session_name(), '', time()-42000, '/');
1059 }
b8aa49bc
AD
1060 }
1061
75836f33 1062 function get_script_urlpath() {
87a79fa4 1063 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1064 }
1065
1066 function get_login_redirect() {
1067 $server = $_SERVER["SERVER_NAME"];
1068
1069 if (ENABLE_LOGIN_SSL) {
1070 $protocol = "https";
1071 } else {
1072 $protocol = "http";
1073 }
1074
1075 $url_path = get_script_urlpath();
1076
1077 $redirect_uri = "$protocol://$server$url_path/login.php";
1078
1079 return $redirect_uri;
1080 }
1081
916f788a 1082 function validate_session($link) {
a2e9b457 1083 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
1084 if ($_SESSION["ip_address"]) {
1085 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1086 return false;
1087 }
1088 }
1089 }
1090 return true;
1091 }
1092
7ae65adf
AD
1093 function basic_nosid_redirect_check() {
1094 if (!SINGLE_USER_MODE) {
3dd46f19 1095 if (!$_COOKIE[get_session_cookie_name()]) {
7ae65adf
AD
1096 $redirect_uri = get_login_redirect();
1097 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1098 header("Location: $redirect_uri?rt=$return_to");
1099 exit;
1100 }
1101 }
1102 }
1103
b8aa49bc
AD
1104 function login_sequence($link) {
1105 if (!SINGLE_USER_MODE) {
75836f33 1106
916f788a
AD
1107 if (!validate_session($link)) {
1108 logout_user();
1109 $redirect_uri = get_login_redirect();
1110 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1111 header("Location: $redirect_uri?rt=$return_to");
1112 exit;
1113 }
1114
b8aa49bc
AD
1115 if (!USE_HTTP_AUTH) {
1116 if (!$_SESSION["uid"]) {
75836f33 1117 $redirect_uri = get_login_redirect();
e31dca14
AD
1118 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1119 header("Location: $redirect_uri?rt=$return_to");
b8aa49bc
AD
1120 exit;
1121 }
1122 } else {
f557cd78
AD
1123 if (!$_SESSION["uid"]) {
1124 if (!$_SERVER["PHP_AUTH_USER"]) {
1125
1126 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1127 header('HTTP/1.0 401 Unauthorized');
1128 exit;
1129
1130 } else {
1131 $auth_result = authenticate_user($link,
1132 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1133
1134 if (!$auth_result) {
1135 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1136 header('HTTP/1.0 401 Unauthorized');
1137 exit;
1138 }
1139 }
1140 }
b8aa49bc
AD
1141 }
1142 } else {
0bbba72d 1143 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1144 }
1145 }
3547842a
AD
1146
1147 function truncate_string($str, $max_len) {
12db369c
AD
1148 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1149 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
1150 } else {
1151 return $str;
1152 }
1153 }
54a60e1a
AD
1154
1155 function get_user_theme_path($link) {
798f722b
AD
1156 $result = db_query($link, "SELECT theme_path
1157 FROM
1158 ttrss_themes,ttrss_users
1159 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
1160 if (db_num_rows($result) != 0) {
1161 return db_fetch_result($result, 0, "theme_path");
1162 } else {
1163 return null;
1164 }
1165 }
be773442
AD
1166
1167 function smart_date_time($timestamp) {
1168 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1169 return date("G:i", $timestamp);
f26450f1 1170 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1171 return date("M d, G:i", $timestamp);
1172 } else {
b02111c2 1173 return date("Y/m/d G:i", $timestamp);
be773442
AD
1174 }
1175 }
1176
1177 function smart_date($timestamp) {
1178 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1179 return "Today";
f26450f1 1180 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1181 return date("D m", $timestamp);
1182 } else {
b02111c2 1183 return date("Y/m/d", $timestamp);
be773442
AD
1184 }
1185 }
a654a595
AD
1186
1187 function sql_bool_to_string($s) {
1188 if ($s == "t" || $s == "1") {
1189 return "true";
1190 } else {
1191 return "false";
1192 }
1193 }
e3c99f3b
AD
1194
1195 function sql_bool_to_bool($s) {
1196 if ($s == "t" || $s == "1") {
1197 return true;
1198 } else {
1199 return false;
1200 }
1201 }
0ea4fb50 1202
e3c99f3b 1203
0ea4fb50
AD
1204 function toggleEvenOdd($a) {
1205 if ($a == "even")
1206 return "odd";
1207 else
1208 return "even";
1209 }
6043fb7e
AD
1210
1211 function sanity_check($link) {
9cbca41f 1212
6043fb7e
AD
1213 $error_code = 0;
1214 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1215 $schema_version = db_fetch_result($result, 0, "schema_version");
1216
1217 if ($schema_version != SCHEMA_VERSION) {
1218 $error_code = 5;
1219 }
1220
6043fb7e 1221 if ($error_code != 0) {
3560a0be 1222 print_error_xml(5);
6043fb7e
AD
1223 return false;
1224 } else {
1225 return true;
9cbca41f 1226 }
6043fb7e
AD
1227 }
1228
27981ca3
AD
1229 function file_is_locked($filename) {
1230 error_reporting(0);
1231 $fp = fopen($filename, "r");
1232 error_reporting(DEFAULT_ERROR_LEVEL);
1233 if ($fp) {
1234 if (flock($fp, LOCK_EX | LOCK_NB)) {
1235 flock($fp, LOCK_UN);
1236 fclose($fp);
1237 return false;
1238 }
1239 fclose($fp);
1240 return true;
1241 }
1242 return false;
1243 }
1244
fcb4c0c9
AD
1245 function make_lockfile($filename) {
1246 $fp = fopen($filename, "w");
1247
1248 if (flock($fp, LOCK_EX | LOCK_NB)) {
1249 return $fp;
1250 } else {
1251 return false;
1252 }
1253 }
1254
894ebcf5
AD
1255 function sql_random_function() {
1256 if (DB_TYPE == "mysql") {
1257 return "RAND()";
1258 } else {
1259 return "RANDOM()";
1260 }
1261 }
1262
23aa0d16 1263 function catchup_feed($link, $feed, $cat_view) {
88040f57
AD
1264
1265 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
23aa0d16
AD
1266
1267 if ($cat_view) {
1268
1269 if ($feed > 0) {
1270 $cat_qpart = "cat_id = '$feed'";
1271 } else {
1272 $cat_qpart = "cat_id IS NULL";
1273 }
1274
1275 $tmp_result = db_query($link, "SELECT id
1276 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1277 $_SESSION["uid"]);
1278
1279 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1280
1281 $tmp_feed = $tmp_line["id"];
1282
1283 db_query($link, "UPDATE ttrss_user_entries
1284 SET unread = false,last_read = NOW()
1285 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1286 }
1287
1288 } else if ($feed > 0) {
1289
1290 $tmp_result = db_query($link, "SELECT id
1291 FROM ttrss_feeds WHERE parent_feed = '$feed'
1292 ORDER BY cat_id,title");
1293
1294 $parent_ids = array();
1295
1296 if (db_num_rows($tmp_result) > 0) {
1297 while ($p = db_fetch_assoc($tmp_result)) {
1298 array_push($parent_ids, "feed_id = " . $p["id"]);
1299 }
1300
1301 $children_qpart = implode(" OR ", $parent_ids);
1302
1303 db_query($link, "UPDATE ttrss_user_entries
1304 SET unread = false,last_read = NOW()
1305 WHERE (feed_id = '$feed' OR $children_qpart)
1306 AND owner_uid = " . $_SESSION["uid"]);
1307
1308 } else {
1309 db_query($link, "UPDATE ttrss_user_entries
1310 SET unread = false,last_read = NOW()
1311 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1312 }
1313
1314 } else if ($feed < 0 && $feed > -10) { // special, like starred
1315
1316 if ($feed == -1) {
1317 db_query($link, "UPDATE ttrss_user_entries
1318 SET unread = false,last_read = NOW()
1319 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1320 }
1321
1322 } else if ($feed < -10) { // label
1323
1324 // TODO make this more efficient
1325
1326 $label_id = -$feed - 11;
1327
1328 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1329 WHERE id = '$label_id'");
1330
1331 if ($tmp_result) {
1332 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1333
1334 db_query($link, "BEGIN");
1335
1336 $tmp2_result = db_query($link,
1337 "SELECT
1338 int_id
1339 FROM
88040f57 1340 ttrss_user_entries,ttrss_entries,ttrss_feeds
23aa0d16 1341 WHERE
88040f57
AD
1342 ref_id = ttrss_entries.id AND
1343 ttrss_user_entries.feed_id = ttrss_feeds.id AND
23aa0d16 1344 $sql_exp AND
88040f57 1345 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
23aa0d16
AD
1346
1347 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1348 db_query($link, "UPDATE
1349 ttrss_user_entries
1350 SET
1351 unread = false, last_read = NOW()
1352 WHERE
1353 int_id = " . $tmp_line["int_id"]);
1354 }
1355
1356 db_query($link, "COMMIT");
1357
1358/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1359 SET unread = false,last_read = NOW()
1360 WHERE $sql_exp
1361 AND ref_id = id
1362 AND owner_uid = ".$_SESSION["uid"]); */
1363 }
1364 }
1365 } else { // tag
1366 db_query($link, "BEGIN");
1367
1368 $tag_name = db_escape_string($feed);
1369
1370 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1371 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1372
1373 while ($line = db_fetch_assoc($result)) {
1374 db_query($link, "UPDATE ttrss_user_entries SET
1375 unread = false, last_read = NOW()
1376 WHERE int_id = " . $line["post_int_id"]);
1377 }
1378 db_query($link, "COMMIT");
1379 }
1380 }
1381
1382 function update_generic_feed($link, $feed, $cat_view) {
1383 if ($cat_view) {
1384
1385 if ($feed > 0) {
1386 $cat_qpart = "cat_id = '$feed'";
1387 } else {
1388 $cat_qpart = "cat_id IS NULL";
1389 }
1390
1391 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1392 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1393
1394 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1395 $feed_url = $tmp_line["feed_url"];
1396 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1397 }
1398
1399 } else {
1400 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1401 WHERE id = '$feed'");
1402 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1403 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1404 }
1405 }
a9cb1f83
AD
1406
1407 function getAllCounters($link) {
1408 getLabelCounters($link);
1409 getFeedCounters($link);
1410 getTagCounters($link);
1411 getGlobalCounters($link);
1412 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1413 getCategoryCounters($link);
1414 }
1415 }
1416
1417 function getCategoryCounters($link) {
1418 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1419 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1420 AND unread = true)) AS unread FROM ttrss_feeds
1421 WHERE
1422 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1423
1424 while ($line = db_fetch_assoc($result)) {
1425 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1426 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1427 $line["unread"]."\"/>";
1428 }
1429 }
1430
f295c368
AD
1431 function getCategoryUnread($link, $cat) {
1432
18664970
AD
1433 if ($cat != 0) {
1434 $cat_query = "cat_id = '$cat'";
1435 } else {
1436 $cat_query = "cat_id IS NULL";
1437 }
1438
1439 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
f295c368
AD
1440 AND owner_uid = " . $_SESSION["uid"]);
1441
1442 $cat_feeds = array();
1443 while ($line = db_fetch_assoc($result)) {
1444 array_push($cat_feeds, "feed_id = " . $line["id"]);
1445 }
1446
18664970
AD
1447 if (count($cat_feeds) == 0) return 0;
1448
f295c368
AD
1449 $match_part = implode(" OR ", $cat_feeds);
1450
1451 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1452 FROM ttrss_user_entries
4919fb42 1453 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
f295c368
AD
1454
1455 $unread = 0;
1456
1457 # this needs to be rewritten
1458 while ($line = db_fetch_assoc($result)) {
1459 $unread += $line["unread"];
1460 }
1461
1462 return $unread;
1463
1464 }
1465
1466 function getFeedUnread($link, $feed, $is_cat = false) {
a9cb1f83 1467 $n_feed = sprintf("%d", $feed);
f295c368
AD
1468
1469 if ($is_cat) {
831ff047 1470 return getCategoryUnread($link, $n_feed);
f295c368 1471 } else if ($n_feed == -1) {
a9cb1f83 1472 $match_part = "marked = true";
4919fb42 1473 } else if ($n_feed > 0) {
831ff047 1474
4919fb42
AD
1475 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE parent_feed = '$n_feed'
1476 AND owner_uid = " . $_SESSION["uid"]);
831ff047
AD
1477
1478 if (db_num_rows($result) > 0) {
4919fb42 1479
831ff047
AD
1480 $linked_feeds = array();
1481 while ($line = db_fetch_assoc($result)) {
1482 array_push($linked_feeds, "feed_id = " . $line["id"]);
1483 }
1484
1485 $match_part = implode(" OR ", $linked_feeds);
1486
4919fb42
AD
1487 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1488 FROM ttrss_user_entries
1489 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1490
1491 $unread = 0;
1492
1493 # this needs to be rewritten
1494 while ($line = db_fetch_assoc($result)) {
1495 $unread += $line["unread"];
1496 }
1497
1498 return $unread;
1499
831ff047
AD
1500 } else {
1501 $match_part = "feed_id = '$n_feed'";
1502 }
a9cb1f83
AD
1503 } else if ($feed < -10) {
1504 $label_id = -$feed - 11;
1505
1506 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1507 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1508
1509 $match_part = db_fetch_result($result, 0, "sql_exp");
1510 }
1511
1512 if ($match_part) {
1513
1514 $result = db_query($link, "SELECT count(int_id) AS unread
88040f57
AD
1515 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1516 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1517 ttrss_user_entries.ref_id = ttrss_entries.id AND
1518 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
1519
1520 } else {
1521
1522 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1523 FROM ttrss_tags,ttrss_user_entries
1524 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1525 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1526 }
1527
1528 $unread = db_fetch_result($result, 0, "unread");
1529 return $unread;
1530 }
1531
1532 /* FIXME this needs reworking */
1533
1534 function getGlobalUnread($link) {
1535 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1536 WHERE unread = true AND
1537 ttrss_user_entries.ref_id = ttrss_entries.id AND
1538 owner_uid = " . $_SESSION["uid"]);
1539 $c_id = db_fetch_result($result, 0, "c_id");
1540 return $c_id;
1541 }
1542
1543 function getGlobalCounters($link, $global_unread = -1) {
1544 if ($global_unread == -1) {
1545 $global_unread = getGlobalUnread($link);
1546 }
1547 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1548 }
1549
1550 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1551
1552 if ($smart_mode) {
1553 if (!$_SESSION["tctr_last_value"]) {
1554 $_SESSION["tctr_last_value"] = array();
1555 }
1556 }
1557
1558 $old_counters = $_SESSION["tctr_last_value"];
1559
1560 $tctrs_modified = false;
1561
1562/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1563 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1564 ttrss_user_entries.ref_id = ttrss_entries.id AND
1565 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1566 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1567 UNION
1568 select tag_name,0 as count FROM ttrss_tags
1569 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1570
1571 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1572 FROM ttrss_user_entries WHERE int_id = post_int_id
1573 AND unread = true)) AS count FROM ttrss_tags
1574 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1575
1576 $tags = array();
1577
1578 while ($line = db_fetch_assoc($result)) {
1579 $tags[$line["tag_name"]] += $line["count"];
1580 }
1581
1582 foreach (array_keys($tags) as $tag) {
1583 $unread = $tags[$tag];
1584
1585 $tag = htmlspecialchars($tag);
1586
1587 if (!$smart_mode || $old_counters[$tag] != $unread) {
1588 $old_counters[$tag] = $unread;
1589 $tctrs_modified = true;
1590 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1591 }
1592
1593 }
1594
1595 if ($smart_mode && $tctrs_modified) {
1596 $_SESSION["tctr_last_value"] = $old_counters;
1597 }
1598
1599 }
1600
ef393de7 1601 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
a9cb1f83
AD
1602
1603 if ($smart_mode) {
1604 if (!$_SESSION["lctr_last_value"]) {
1605 $_SESSION["lctr_last_value"] = array();
1606 }
1607 }
1608
ef393de7
AD
1609 $ret_arr = array();
1610
a9cb1f83
AD
1611 $old_counters = $_SESSION["lctr_last_value"];
1612 $lctrs_modified = false;
1613
88040f57 1614 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 1615 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57
AD
1616 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1617 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1618
1619 $count = db_fetch_result($result, 0, "count");
1620
ef393de7
AD
1621 if (!$ret_mode) {
1622 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1623 } else {
1624 $ret_arr["-1"]["counter"] = $count;
1625 $ret_arr["-1"]["description"] = "Starred";
1626 }
a9cb1f83
AD
1627
1628 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1629 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1630
1631 while ($line = db_fetch_assoc($result)) {
1632
1633 $id = -$line["id"] - 11;
1634
ef393de7
AD
1635 $label_name = $line["description"];
1636
a9cb1f83
AD
1637 error_reporting (0);
1638
88040f57 1639 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
a9cb1f83 1640 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
88040f57 1641 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 1642 ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 1643 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1644
1645 $count = db_fetch_result($tmp_result, 0, "count");
1646
1647 if (!$smart_mode || $old_counters[$id] != $count) {
1648 $old_counters[$id] = $count;
1649 $lctrs_modified = true;
ef393de7
AD
1650 if (!$ret_mode) {
1651 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1652 } else {
1653 $ret_arr[$id]["counter"] = $count;
1654 $ret_arr[$id]["description"] = $label_name;
1655 }
a9cb1f83
AD
1656 }
1657
1658 error_reporting (DEFAULT_ERROR_LEVEL);
1659 }
1660
1661 if ($smart_mode && $lctrs_modified) {
1662 $_SESSION["lctr_last_value"] = $old_counters;
1663 }
ef393de7
AD
1664
1665 return $ret_arr;
a9cb1f83
AD
1666 }
1667
1668/* function getFeedCounter($link, $id) {
1669
1670 $result = db_query($link, "SELECT
1671 count(id) as count,last_error
1672 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1673 WHERE feed_id = '$id' AND unread = true
1674 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1675 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1676
1677 $count = db_fetch_result($result, 0, "count");
1678 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1679
1680 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1681 } */
1682
1683 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1684
1685 if ($smart_mode) {
1686 if (!$_SESSION["fctr_last_value"]) {
1687 $_SESSION["fctr_last_value"] = array();
1688 }
1689 }
1690
1691 $old_counters = $_SESSION["fctr_last_value"];
1692
1693 $result = db_query($link, "SELECT id,last_error,parent_feed,
fb1fb4ab 1694 SUBSTRING(last_updated,1,19) AS last_updated,
a9cb1f83
AD
1695 (SELECT count(id)
1696 FROM ttrss_entries,ttrss_user_entries
1697 WHERE feed_id = ttrss_feeds.id AND
1698 ttrss_user_entries.ref_id = ttrss_entries.id
1699 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1700 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1701 AND parent_feed IS NULL");
1702
1703 $fctrs_modified = false;
1704
fb1fb4ab
AD
1705 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1706
a9cb1f83
AD
1707 while ($line = db_fetch_assoc($result)) {
1708
1709 $id = $line["id"];
1710 $count = $line["count"];
1711 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
1712
1713 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1714 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1715 } else {
1716 $last_updated = date($short_date, strtotime($line["last_updated"]));
1717 }
1718
a9cb1f83
AD
1719 $has_img = is_file(ICONS_DIR . "/$id.ico");
1720
1721 $tmp_result = db_query($link,
1722 "SELECT id,COUNT(unread) AS unread
1723 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1724 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1725 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1726
1727 if (db_num_rows($tmp_result) > 0) {
1728 while ($l = db_fetch_assoc($tmp_result)) {
1729 $count += $l["unread"];
1730 }
1731 }
1732
1733 if (!$smart_mode || $old_counters[$id] != $count) {
1734 $old_counters[$id] = $count;
1735 $fctrs_modified = true;
1736
1737 if ($last_error) {
1738 $error_part = "error=\"$last_error\"";
1739 } else {
1740 $error_part = "";
1741 }
1742
1743 if ($has_img) {
1744 $has_img_part = "hi=\"$has_img\"";
1745 } else {
1746 $has_img_part = "";
1747 }
1748
fb1fb4ab 1749 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
a9cb1f83
AD
1750 }
1751 }
1752
1753 if ($smart_mode && $fctrs_modified) {
1754 $_SESSION["fctr_last_value"] = $old_counters;
1755 }
1756 }
1757
1b758780
AD
1758 function get_script_dt_add() {
1759 if (strpos(VERSION, "99") === false) {
1760 return VERSION;
1761 } else {
1762 return time();
1763 }
1764 }
1765
6e7f8d26
AD
1766 function get_pgsql_version($link) {
1767 $result = db_query($link, "SELECT version() AS version");
1768 $version = split(" ", db_fetch_result($result, 0, "version"));
1769 return $version[1];
1770 }
1771
af106b0e
AD
1772 function print_error_xml($code, $add_msg = "") {
1773 global $ERRORS;
1774
1775 $error_msg = $ERRORS[$code];
1776
1777 if ($add_msg) {
1778 $error_msg = "$error_msg; $add_msg";
1779 }
1780
4c2abbc1 1781 print "<rpc-reply>";
af106b0e 1782 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 1783 print "</rpc-reply>";
af106b0e 1784 }
956c7629
AD
1785
1786 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1787
1788 if ($cat_id == "0" || !$cat_id) {
1789 $cat_qpart = "NULL";
1790 } else {
1791 $cat_qpart = "'$cat_id'";
1792 }
1793
1794 $result = db_query($link,
1795 "SELECT id FROM ttrss_feeds
1796 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1797
1798 if (db_num_rows($result) == 0) {
1799
1800 $result = db_query($link,
1801 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1802 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1803 '[Unknown]', $cat_qpart)");
1804
1805 $result = db_query($link,
1806 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1807 AND owner_uid = " . $_SESSION["uid"]);
1808
1809 $feed_id = db_fetch_result($result, 0, "id");
1810
1811 if ($feed_id) {
1812 update_rss_feed($link, $feed_link, $feed_id, true);
1813 }
1814
1815 return true;
1816 } else {
1817 return false;
1818 }
1819 }
1820
673d54ca
AD
1821 function print_feed_select($link, $id, $default_id = "",
1822 $attributes = "", $include_all_feeds = true) {
1823
79f3553b 1824 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 1825 if ($include_all_feeds) {
79f3553b 1826 print "<option value=\"0\">All feeds</option>";
673d54ca
AD
1827 }
1828
1829 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1830 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1831
1832 if (db_num_rows($result) > 0 && $include_all_feeds) {
1833 print "<option disabled>--------</option>";
1834 }
1835
1836 while ($line = db_fetch_assoc($result)) {
1837 if ($line["id"] == $default_id) {
1838 $is_selected = "selected";
1839 } else {
1840 $is_selected = "";
1841 }
79f3553b 1842 printf("<option $is_selected value='%d'>%s</option>",
07164479 1843 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
673d54ca
AD
1844 }
1845
1846 print "</select>";
1847 }
1848
1849 function print_feed_cat_select($link, $id, $default_id = "",
1850 $attributes = "", $include_all_cats = true) {
1851
79f3553b 1852 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
1853
1854 if ($include_all_cats) {
14f69488 1855 print "<option value=\"0\">Uncategorized</option>";
673d54ca
AD
1856 }
1857
1858 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1859 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1860
1861 if (db_num_rows($result) > 0 && $include_all_cats) {
1862 print "<option disabled>--------</option>";
1863 }
1864
1865 while ($line = db_fetch_assoc($result)) {
1866 if ($line["id"] == $default_id) {
1867 $is_selected = "selected";
1868 } else {
1869 $is_selected = "";
1870 }
14f69488 1871 printf("<option $is_selected value='%d'>%s</option>",
07164479 1872 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
673d54ca
AD
1873 }
1874
1875 print "</select>";
1876 }
1877
14f69488
AD
1878 function checkbox_to_sql_bool($val) {
1879 return ($val == "on") ? "true" : "false";
1880 }
86b682ce
AD
1881
1882 function getFeedCatTitle($link, $id) {
1883 if ($id == -1) {
1884 return "Special";
1885 } else if ($id < -10) {
1886 return "Labels";
1887 } else if ($id > 0) {
1888 $result = db_query($link, "SELECT ttrss_feed_categories.title
1889 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
1890 cat_id = ttrss_feed_categories.id");
1891 if (db_num_rows($result) == 1) {
1892 return db_fetch_result($result, 0, "title");
1893 } else {
eb4311d0 1894 return "Uncategorized";
86b682ce
AD
1895 }
1896 } else {
1897 return "getFeedCatTitle($id) failed";
1898 }
1899
1900 }
1901
1902 function getFeedTitle($link, $id) {
1903 if ($id == -1) {
1904 return "Starred articles";
1905 } else if ($id < -10) {
1906 $label_id = -10 - $id;
1907 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
1908 if (db_num_rows($result) == 1) {
1909 return db_fetch_result($result, 0, "description");
1910 } else {
1911 return "Unknown label ($label_id)";
1912 }
1913
1914 } else if ($id > 0) {
1915 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
1916 if (db_num_rows($result) == 1) {
1917 return db_fetch_result($result, 0, "title");
1918 } else {
1919 return "Unknown feed ($id)";
1920 }
1921 } else {
1922 return "getFeedTitle($id) failed";
1923 }
1924
1925 }
3dd46f19
AD
1926
1927 function get_session_cookie_name() {
1928 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
1929 }
3ac2b520
AD
1930
1931 function print_init_params($link) {
1932 print "<init-params>";
1933 if ($_SESSION["stored-params"]) {
1934 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
1935 if ($key) {
1936 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
1937 print "<param key=\"$key\" value=\"$value\"/>";
1938 }
3ac2b520
AD
1939 }
1940 }
1941
1942 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
1943 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
0d51e25d 1944 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
3ac2b520
AD
1945
1946 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
1947 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
1948
e8bd0da9
AD
1949 print "<param key=\"hide_read_feeds\" value=\"" .
1950 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
1951
c9268ed5
AD
1952 print "<param key=\"feeds_sort_by_unread\" value=\"" .
1953 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
1954
3ac2b520
AD
1955 print "</init-params>";
1956 }
f54f515f
AD
1957
1958 function print_runtime_info($link) {
1959 print "<runtime-info>";
71ad883b
AD
1960 if (ENABLE_UPDATE_DAEMON) {
1961 print "<param key=\"daemon_is_running\" value=\"".
1962 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
1963 }
f54f515f
AD
1964 print "</runtime-info>";
1965 }
ef393de7 1966
88040f57 1967 function getSearchSql($search, $match_on) {
ef393de7 1968
88040f57 1969 $search_query_part = "";
e20c9d88 1970
88040f57
AD
1971 $keywords = split(" ", $search);
1972 $query_keywords = array();
e20c9d88 1973
88040f57 1974 if ($match_on == "both") {
e20c9d88 1975
88040f57
AD
1976 foreach ($keywords as $k) {
1977 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
1978 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
1979 }
e20c9d88 1980
88040f57 1981 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 1982
88040f57 1983 } else if ($match_on == "title") {
e20c9d88 1984
88040f57
AD
1985 foreach ($keywords as $k) {
1986 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
1987 }
e20c9d88 1988
88040f57 1989 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 1990
88040f57
AD
1991 } else if ($match_on == "content") {
1992
1993 foreach ($keywords as $k) {
1994 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
1995 }
1996 }
1997
1998 $search_query_part = implode("AND", $query_keywords);
1999
2000 return $search_query_part;
2001 }
2002
2003 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
2004
2005 if ($search) {
2006
2007 $search_query_part = getSearchSql($search, $match_on);
2008 $search_query_part .= " AND ";
e20c9d88 2009
ef393de7
AD
2010 } else {
2011 $search_query_part = "";
2012 }
2013
2014 $view_query_part = "";
2015
2016 if ($view_mode == "adaptive") {
2017 if ($search) {
2018 $view_query_part = " ";
2019 } else if ($feed != -1) {
f295c368 2020 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
2021 if ($unread > 0) {
2022 $view_query_part = " unread = true AND ";
2023 }
2024 }
2025 }
2026
2027 if ($view_mode == "marked") {
2028 $view_query_part = " marked = true AND ";
2029 }
2030
2031 if ($view_mode == "unread") {
2032 $view_query_part = " unread = true AND ";
2033 }
2034
2035 if ($limit > 0) {
2036 $limit_query_part = "LIMIT " . $limit;
2037 }
2038
2039 $vfeed_query_part = "";
2040
2041 // override query strategy and enable feed display when searching globally
2042 if ($search && $search_mode == "all_feeds") {
2043 $query_strategy_part = "ttrss_entries.id > 0";
2044 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2045 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2046 $query_strategy_part = "ttrss_entries.id > 0";
2047 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2048 id = feed_id) as feed_title,";
2049 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2050
2051 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2052
2053 $tmp_result = false;
2054
2055 if ($cat_view) {
2056 $tmp_result = db_query($link, "SELECT id
2057 FROM ttrss_feeds WHERE cat_id = '$feed'");
2058 } else {
2059 $tmp_result = db_query($link, "SELECT id
2060 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2061 WHERE id = '$feed') AND id != '$feed'");
2062 }
ef393de7
AD
2063
2064 $cat_siblings = array();
2065
2066 if (db_num_rows($tmp_result) > 0) {
2067 while ($p = db_fetch_assoc($tmp_result)) {
2068 array_push($cat_siblings, "feed_id = " . $p["id"]);
2069 }
2070
2071 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2072 $feed, implode(" OR ", $cat_siblings));
2073
2074 } else {
2075 $query_strategy_part = "ttrss_entries.id > 0";
2076 }
2077
2078 } else if ($feed >= 0) {
2079
2080 if ($cat_view) {
2081
2082 if ($feed > 0) {
2083 $query_strategy_part = "cat_id = '$feed'";
2084 } else {
2085 $query_strategy_part = "cat_id IS NULL";
2086 }
2087
2088 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2089
2090 } else {
2091 $tmp_result = db_query($link, "SELECT id
2092 FROM ttrss_feeds WHERE parent_feed = '$feed'
2093 ORDER BY cat_id,title");
2094
2095 $parent_ids = array();
2096
2097 if (db_num_rows($tmp_result) > 0) {
2098 while ($p = db_fetch_assoc($tmp_result)) {
2099 array_push($parent_ids, "feed_id = " . $p["id"]);
2100 }
2101
2102 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2103 $feed, implode(" OR ", $parent_ids));
2104
2105 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2106 } else {
2107 $query_strategy_part = "feed_id = '$feed'";
2108 }
2109 }
2110 } else if ($feed == -1) { // starred virtual feed
2111 $query_strategy_part = "marked = true";
2112 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2113 } else if ($feed <= -10) { // labels
2114 $label_id = -$feed - 11;
2115
2116 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2117 WHERE id = '$label_id'");
2118
2119 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2120
2121 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2122 } else {
2123 $query_strategy_part = "id > 0"; // dumb
2124 }
d6e5706d
AD
2125
2126 if (get_pref($link, 'REVERSE_HEADLINES')) {
2127 $order_by = "updated";
2128 } else {
2129 $order_by = "updated DESC";
2130 }
e939722a
AD
2131
2132 if ($override_order) {
2133 $order_by = $override_order;
2134 }
ef393de7
AD
2135
2136 $feed_title = "";
2137
2138 if ($search && $search_mode == "all_feeds") {
2139 $feed_title = "Global search results ($search)";
2140 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
e1eb2147 2141 $feed_title = "Tag search results ($search, $feed)";
ef393de7
AD
2142 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2143 $feed_title = $feed;
2144 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2145
2146 if ($cat_view) {
2147
2148 if ($feed != 0) {
2149 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2150 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2151 $feed_title = db_fetch_result($result, 0, "title");
2152 } else {
2153 $feed_title = "Uncategorized";
2154 }
e1eb2147
AD
2155
2156 if ($search) {
2157 $feed_title = "Category search results ($search, $feed_title)";
2158 }
2159
ef393de7
AD
2160 } else {
2161
2162 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2163 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2164
2165 $feed_title = db_fetch_result($result, 0, "title");
2166 $feed_site_url = db_fetch_result($result, 0, "site_url");
2167 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
2168
2169 if ($search) {
2170 $feed_title = "Feed search results ($search, $feed_title)";
2171 }
ef393de7
AD
2172 }
2173
2174 } else if ($feed == -1) {
2175 $feed_title = "Starred articles";
2176 } else if ($feed < -10) {
2177 $label_id = -$feed - 11;
2178 $result = db_query($link, "SELECT description FROM ttrss_labels
2179 WHERE id = '$label_id'");
2180 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
2181
2182 if ($search) {
2183 $feed_title = "Label search results ($search, $feed_title)";
2184 }
ef393de7
AD
2185 } else {
2186 $feed_title = "?";
2187 }
2188
2189 $feed_title = db_unescape_string($feed_title);
2190
2191 if ($feed < -10) error_reporting (0);
2192
2193 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2194
2195 if ($feed >= 0) {
2196 $feed_kind = "Feeds";
2197 } else {
2198 $feed_kind = "Labels";
2199 }
2200
2201 $content_query_part = "content as content_preview,";
2202
2203 $query = "SELECT
2204 ttrss_entries.id,ttrss_entries.title,
2205 SUBSTRING(updated,1,16) as updated,
2206 unread,feed_id,marked,link,last_read,
2207 SUBSTRING(last_read,1,19) as last_read_noms,
2208 $vfeed_query_part
2209 $content_query_part
2210 SUBSTRING(updated,1,19) as updated_noms
2211 FROM
2212 ttrss_entries,ttrss_user_entries,ttrss_feeds
2213 WHERE
2214 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2215 ttrss_user_entries.ref_id = ttrss_entries.id AND
2216 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2217 $search_query_part
2218 $view_query_part
2219 $query_strategy_part ORDER BY $order_by
2220 $limit_query_part";
2221
2222 $result = db_query($link, $query);
2223
2224 if ($_GET["debug"]) print $query;
2225
2226 } else {
2227 // browsing by tag
2228
2229 $feed_kind = "Tags";
2230
2231 $result = db_query($link, "SELECT
2232 ttrss_entries.id as id,title,
2233 SUBSTRING(updated,1,16) as updated,
2234 unread,feed_id,
2235 marked,link,last_read,
2236 SUBSTRING(last_read,1,19) as last_read_noms,
2237 $vfeed_query_part
2238 $content_query_part
2239 SUBSTRING(updated,1,19) as updated_noms
2240 FROM
2241 ttrss_entries,ttrss_user_entries,ttrss_tags
2242 WHERE
2243 ref_id = ttrss_entries.id AND
2244 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2245 post_int_id = int_id AND tag_name = '$feed' AND
2246 $view_query_part
2247 $search_query_part
2248 $query_strategy_part ORDER BY $order_by
2249 $limit_query_part");
2250 }
2251
c7188969 2252 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
2253
2254 }
2255
e1eb2147
AD
2256 function generate_syndicated_feed($link, $feed, $is_cat,
2257 $search, $search_mode, $match_on) {
18664970
AD
2258
2259 $qfh_ret = queryFeedHeadlines($link, $feed,
e939722a 2260 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
18664970
AD
2261
2262 $result = $qfh_ret[0];
59e2aab4 2263 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
2264 $feed_site_url = $qfh_ret[2];
2265 $last_error = $qfh_ret[3];
2266
2267 print "<rss version=\"2.0\">
2268 <channel>
2269 <title>$feed_title</title>
2270 <link>$feed_site_url</link>
2271 <generator>Tiny Tiny RSS v".VERSION."</generator>";
2272
2273 while ($line = db_fetch_assoc($result)) {
2274 print "<item>";
2275 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2276
2277 $rfc822_date = date('r', strtotime($line["updated"]));
2278
2279 print "<pubDate>$rfc822_date</pubDate>";
2280
2281 print "<title>" .
2282 htmlspecialchars($line["title"]) . "</title>";
2283
2284 print "<description>" .
2285 htmlspecialchars($line["content_preview"]) . "</description>";
2286
2287 print "</item>";
2288 }
2289
2290 print "</channel></rss>";
2291
2292 }
2293
0a6c4846
AD
2294 function getCategoryTitle($link, $cat_id) {
2295
2296 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2297 id = '$cat_id'");
2298
2299 if (db_num_rows($result) == 1) {
2300 return db_fetch_result($result, 0, "title");
2301 } else {
2302 return "Uncategorized";
2303 }
2304 }
2305
40d13c28 2306?>