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