]> git.wh0rd.org - tt-rss.git/blob - functions.php
support omode in rpc getAllCounters
[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 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
17 require_once "magpierss/rss_fetch.inc";
18 require_once 'magpierss/rss_utils.inc';
19 } else if (RSS_BACKEND_TYPE == "simplepie") {
20 require_once 'simplepie/simplepie.inc';
21 }
22
23 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
24
25 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
26
27 $rows = -1;
28
29 if (DB_TYPE == "pgsql") {
30 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
31 marked = false AND feed_id = '$feed_id' AND
32 (SELECT date_entered FROM ttrss_entries WHERE
33 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
34
35 $pg_version = get_pgsql_version($link);
36
37 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
38
39 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
40 ttrss_entries.id = ref_id AND
41 marked = false AND
42 feed_id = '$feed_id' AND
43 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
44
45 } else {
46
47 $result = db_query($link, "DELETE FROM ttrss_user_entries
48 USING ttrss_entries
49 WHERE ttrss_entries.id = ref_id AND
50 marked = false AND
51 feed_id = '$feed_id' AND
52 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
53 }
54
55 $rows = pg_affected_rows($result);
56
57 } else {
58
59 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
60 marked = false AND feed_id = '$feed_id' AND
61 (SELECT date_entered FROM ttrss_entries WHERE
62 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
63
64 $result = db_query($link, "DELETE FROM ttrss_user_entries
65 USING ttrss_user_entries, ttrss_entries
66 WHERE ttrss_entries.id = ref_id AND
67 marked = false AND
68 feed_id = '$feed_id' AND
69 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
70
71 $rows = mysql_affected_rows($link);
72
73 }
74
75 if ($debug) {
76 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
77 }
78 }
79
80 function global_purge_old_posts($link, $do_output = false, $limit = false) {
81
82 $random_qpart = sql_random_function();
83
84 if ($limit) {
85 $limit_qpart = "LIMIT $limit";
86 } else {
87 $limit_qpart = "";
88 }
89
90 $result = db_query($link,
91 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
92 ORDER BY $random_qpart $limit_qpart");
93
94 while ($line = db_fetch_assoc($result)) {
95
96 $feed_id = $line["id"];
97 $purge_interval = $line["purge_interval"];
98 $owner_uid = $line["owner_uid"];
99
100 if ($purge_interval == 0) {
101
102 $tmp_result = db_query($link,
103 "SELECT value FROM ttrss_user_prefs WHERE
104 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
105
106 if (db_num_rows($tmp_result) != 0) {
107 $purge_interval = db_fetch_result($tmp_result, 0, "value");
108 }
109 }
110
111 if ($do_output) {
112 // print "Feed $feed_id: purge interval = $purge_interval\n";
113 }
114
115 if ($purge_interval > 0) {
116 purge_feed($link, $feed_id, $purge_interval, $do_output);
117 }
118 }
119
120 // purge orphaned posts in main content table
121 db_query($link, "DELETE FROM ttrss_entries WHERE
122 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
123
124 }
125
126 function purge_old_posts($link) {
127
128 $user_id = $_SESSION["uid"];
129
130 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
131 WHERE owner_uid = '$user_id'");
132
133 while ($line = db_fetch_assoc($result)) {
134
135 $feed_id = $line["id"];
136 $purge_interval = $line["purge_interval"];
137
138 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
139
140 if ($purge_interval > 0) {
141 purge_feed($link, $feed_id, $purge_interval);
142 }
143 }
144
145 // purge orphaned posts in main content table
146 db_query($link, "DELETE FROM ttrss_entries WHERE
147 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
148 }
149
150 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
151
152 if (WEB_DEMO_MODE) return;
153
154 if (!$user_id) {
155 $user_id = $_SESSION["uid"];
156 purge_old_posts($link);
157 }
158
159 // db_query($link, "BEGIN");
160
161 if (MAX_UPDATE_TIME > 0) {
162 if (DB_TYPE == "mysql") {
163 $q_order = "RAND()";
164 } else {
165 $q_order = "RANDOM()";
166 }
167 } else {
168 $q_order = "last_updated DESC";
169 }
170
171 $result = db_query($link, "SELECT feed_url,id,
172 SUBSTRING(last_updated,1,19) AS last_updated,
173 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
174 ORDER BY $q_order");
175
176 $upd_start = time();
177
178 while ($line = db_fetch_assoc($result)) {
179 $upd_intl = $line["update_interval"];
180
181 if (!$upd_intl || $upd_intl == 0) {
182 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
183 }
184
185 if ($upd_intl < 0) {
186 // Updates for this feed are disabled
187 continue;
188 }
189
190 if ($fetch || (!$line["last_updated"] ||
191 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
192
193 // print "<!-- feed: ".$line["feed_url"]." -->";
194
195 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
196
197 $upd_elapsed = time() - $upd_start;
198
199 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
200 return;
201 }
202 }
203 }
204
205 // db_query($link, "COMMIT");
206
207 }
208
209 function fetch_file_contents($url) {
210 if (USE_CURL_FOR_ICONS) {
211 $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
212
213 $ch = curl_init($url);
214 $fp = fopen($tmpfile, "w");
215
216 if ($fp) {
217 curl_setopt($ch, CURLOPT_FILE, $fp);
218 curl_exec($ch);
219 curl_close($ch);
220 fclose($fp);
221 }
222
223 $contents = file_get_contents($tmpfile);
224 unlink($tmpfile);
225
226 return $contents;
227
228 } else {
229 return file_get_contents($url);
230 }
231
232 }
233
234 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
235 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
236
237 function get_favicon_url($url) {
238
239 if ($html = @fetch_file_contents($url)) {
240
241 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
242 // Attempt to grab a favicon link from their webpage url
243 $linkUrl = html_entity_decode($matches[1]);
244
245 if (substr($linkUrl, 0, 1) == '/') {
246 $urlParts = parse_url($url);
247 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
248 } else if (substr($linkUrl, 0, 7) == 'http://') {
249 $faviconURL = $linkUrl;
250 } else if (substr($url, -1, 1) == '/') {
251 $faviconURL = $url.$linkUrl;
252 } else {
253 $faviconURL = $url.'/'.$linkUrl;
254 }
255
256 } else {
257 // If unsuccessful, attempt to "guess" the favicon location
258 $urlParts = parse_url($url);
259 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
260 }
261 }
262
263 // Run a test to see if what we have attempted to get actually exists.
264 if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) {
265 return $faviconURL;
266 } else {
267 return false;
268 }
269 }
270
271 function url_validate($link) {
272
273 $url_parts = @parse_url($link);
274
275 if ( empty( $url_parts["host"] ) )
276 return false;
277
278 if ( !empty( $url_parts["path"] ) ) {
279 $documentpath = $url_parts["path"];
280 } else {
281 $documentpath = "/";
282 }
283
284 if ( !empty( $url_parts["query"] ) )
285 $documentpath .= "?" . $url_parts["query"];
286
287 $host = $url_parts["host"];
288 $port = $url_parts["port"];
289
290 if ( empty($port) )
291 $port = "80";
292
293 $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
294
295 if ( !$socket )
296 return false;
297
298 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
299
300 $http_response = fgets( $socket, 22 );
301
302 $responses = "/(200 OK)|(30[0-9] Moved)/";
303 if ( preg_match($responses, $http_response) ) {
304 fclose($socket);
305 return true;
306 } else {
307 return false;
308 }
309
310 }
311
312 function check_feed_favicon($site_url, $feed, $link) {
313 $favicon_url = get_favicon_url($site_url);
314
315 # print "FAVICON [$site_url]: $favicon_url\n";
316
317 error_reporting(0);
318
319 $icon_file = ICONS_DIR . "/$feed.ico";
320
321 if ($favicon_url && !file_exists($icon_file)) {
322 $contents = fetch_file_contents($favicon_url);
323
324 $fp = fopen($icon_file, "w");
325
326 if ($fp) {
327 fwrite($fp, $contents);
328 fclose($fp);
329 chmod($icon_file, 0644);
330 }
331 }
332
333 error_reporting(DEFAULT_ERROR_LEVEL);
334
335 }
336
337 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
338
339 if (WEB_DEMO_MODE) return;
340
341 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
342 return;
343 }
344
345 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
346 FROM ttrss_feeds WHERE id = '$feed'");
347
348 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
349 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
350
351 $update_interval = db_fetch_result($result, 0, "update_interval");
352
353 if ($update_interval < 0) { return; }
354
355 $feed = db_escape_string($feed);
356
357 $fetch_url = $feed_url;
358
359 if ($auth_login && $auth_pass) {
360 $url_parts = array();
361 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
362
363 if ($url_parts[1] && $url_parts[2]) {
364 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
365 }
366
367 }
368
369 if (RSS_BACKEND_TYPE == "magpie") {
370 error_reporting(0);
371 $rss = fetch_rss($fetch_url);
372 error_reporting (DEFAULT_ERROR_LEVEL);
373 } else if (RSS_BACKEND_TYPE == "simplepie") {
374
375 if (!file_exists(SIMPLEPIE_CACHE_DIR)) {
376 mkdir(SIMPLEPIE_CACHE_DIR);
377 }
378
379 $rss = new SimplePie();
380 $rss->feed_url($fetch_url);
381 $rss->cache_location(SIMPLEPIE_CACHE_DIR);
382 $rss->init();
383 }
384
385 $feed = db_escape_string($feed);
386
387 $rss_check = $rss;
388
389 if (RSS_BACKEND_TYPE == "simplepie") {
390 $rss_check = $rss->data;
391 }
392
393 if ($rss_check) {
394
395 // db_query($link, "BEGIN");
396
397 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
398 FROM ttrss_feeds WHERE id = '$feed'");
399
400 $registered_title = db_fetch_result($result, 0, "title");
401 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
402 $orig_site_url = db_fetch_result($result, 0, "site_url");
403
404 $owner_uid = db_fetch_result($result, 0, "owner_uid");
405
406 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
407 check_feed_favicon($rss->channel["link"], $feed, $link);
408 }
409
410 if (!$registered_title || $registered_title == "[Unknown]") {
411
412 if (RSS_BACKEND_TYPE == "magpie") {
413 $feed_title = db_escape_string($rss->channel["title"]);
414 } else {
415 $feed_title = $rss->get_feed_title();
416 }
417
418 db_query($link, "UPDATE ttrss_feeds SET
419 title = '$feed_title' WHERE id = '$feed'");
420 }
421
422 if (RSS_BACKEND_TYPE == "magpie") {
423 $site_url = $rss->channel["link"];
424 // weird, weird Magpie
425 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
426 } else {
427 $site_url = $rss->get_feed_link();
428 }
429
430 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
431 db_query($link, "UPDATE ttrss_feeds SET
432 site_url = '$site_url' WHERE id = '$feed'");
433 }
434
435 // print "I: " . $rss->channel["image"]["url"];
436
437 if (RSS_BACKEND_TYPE == "magpie") {
438 $icon_url = $rss->image["url"];
439 } else {
440 $icon_url = $rss->get_image_url(); # FIXME
441 }
442
443 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
444 $icon_url = db_escape_string($icon_url);
445 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
446 }
447
448
449 $filters = array();
450
451 $result = db_query($link, "SELECT reg_exp,
452 ttrss_filter_types.name AS name,
453 ttrss_filter_actions.name AS action
454 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
455 enabled = true AND
456 owner_uid = $owner_uid AND
457 ttrss_filter_types.id = filter_type AND
458 ttrss_filter_actions.id = action_id AND
459 (feed_id IS NULL OR feed_id = '$feed')");
460
461 while ($line = db_fetch_assoc($result)) {
462 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
463
464 $filter["reg_exp"] = $line["reg_exp"];
465 $filter["action"] = $line["action"];
466
467 array_push($filters[$line["name"]], $filter);
468 }
469
470 if (RSS_BACKEND_TYPE == "magpie") {
471 $iterator = $rss->items;
472
473 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
474 if (!$iterator || !is_array($iterator)) $iterator = $rss;
475
476 } else {
477 $iterator = $rss->get_items();
478 }
479
480 if (!is_array($iterator)) {
481 /* db_query($link, "UPDATE ttrss_feeds
482 SET last_error = 'Parse error: can\'t find any articles.'
483 WHERE id = '$feed'"); */
484 return; // WTF?
485 }
486
487 foreach ($iterator as $item) {
488
489 if (RSS_BACKEND_TYPE == "magpie") {
490
491 $entry_guid = $item["id"];
492
493 if (!$entry_guid) $entry_guid = $item["guid"];
494 if (!$entry_guid) $entry_guid = $item["link"];
495 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
496
497 if (!$entry_guid) continue;
498
499 $entry_timestamp = "";
500
501 $rss_2_date = $item['pubdate'];
502 $rss_1_date = $item['dc']['date'];
503 $atom_date = $item['issued'];
504 if (!$atom_date) $atom_date = $item['updated'];
505
506 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
507 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
508 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
509
510 if ($entry_timestamp == "") {
511 $entry_timestamp = time();
512 $no_orig_date = 'true';
513 } else {
514 $no_orig_date = 'false';
515 }
516
517 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
518
519 $entry_title = trim(strip_tags($item["title"]));
520
521 // strange Magpie workaround
522 $entry_link = $item["link_"];
523 if (!$entry_link) $entry_link = $item["link"];
524
525 if (!$entry_title) continue;
526 # if (!$entry_link) continue;
527
528 $entry_link = strip_tags($entry_link);
529
530 $entry_content = $item["content:escaped"];
531
532 if (!$entry_content) $entry_content = $item["content:encoded"];
533 if (!$entry_content) $entry_content = $item["content"];
534 if (!$entry_content) $entry_content = $item["atom_content"];
535 if (!$entry_content) $entry_content = $item["summary"];
536 if (!$entry_content) $entry_content = $item["description"];
537
538 // if (!$entry_content) continue;
539
540 // WTF
541 if (is_array($entry_content)) {
542 $entry_content = $entry_content["encoded"];
543 if (!$entry_content) $entry_content = $entry_content["escaped"];
544 }
545
546 // print_r($item);
547 // print_r(htmlspecialchars($entry_content));
548 // print "<br>";
549
550 $entry_content_unescaped = $entry_content;
551 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
552
553 $entry_comments = strip_tags($item["comments"]);
554
555 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
556
557 if (!$entry_author) {
558 $entry_author = db_escape_string(strip_tags($item['author']));
559 }
560
561 $entry_guid = db_escape_string(strip_tags($entry_guid));
562
563 $result = db_query($link, "SELECT id FROM ttrss_entries
564 WHERE guid = '$entry_guid'");
565
566 $entry_content = db_escape_string($entry_content);
567 $entry_title = db_escape_string($entry_title);
568 $entry_link = db_escape_string($entry_link);
569 $entry_comments = db_escape_string($entry_comments);
570
571 $num_comments = db_escape_string($item["slash"]["comments"]);
572
573 if (!$num_comments) $num_comments = 0;
574 }
575
576 # sanitize content
577
578 $entry_content = sanitize_rss($entry_content);
579
580 db_query($link, "BEGIN");
581
582 if (db_num_rows($result) == 0) {
583
584 // base post entry does not exist, create it
585
586 $result = db_query($link,
587 "INSERT INTO ttrss_entries
588 (title,
589 guid,
590 link,
591 updated,
592 content,
593 content_hash,
594 no_orig_date,
595 date_entered,
596 comments,
597 num_comments,
598 author)
599 VALUES
600 ('$entry_title',
601 '$entry_guid',
602 '$entry_link',
603 '$entry_timestamp_fmt',
604 '$entry_content',
605 '$content_hash',
606 $no_orig_date,
607 NOW(),
608 '$entry_comments',
609 '$num_comments',
610 '$entry_author')");
611 } else {
612 // we keep encountering the entry in feeds, so we need to
613 // update date_entered column so that we don't get horrible
614 // dupes when the entry gets purged and reinserted again e.g.
615 // in the case of SLOW SLOW OMG SLOW updating feeds
616
617 $base_entry_id = db_fetch_result($result, 0, "id");
618
619 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
620 WHERE id = '$base_entry_id'");
621 }
622
623 // now it should exist, if not - bad luck then
624
625 $result = db_query($link, "SELECT
626 id,content_hash,no_orig_date,title,
627 substring(date_entered,1,19) as date_entered,
628 substring(updated,1,19) as updated,
629 num_comments
630 FROM
631 ttrss_entries
632 WHERE guid = '$entry_guid'");
633
634 if (db_num_rows($result) == 1) {
635
636 // this will be used below in update handler
637 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
638 $orig_title = db_fetch_result($result, 0, "title");
639 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
640 $orig_date_entered = strtotime(db_fetch_result($result,
641 0, "date_entered"));
642
643 $ref_id = db_fetch_result($result, 0, "id");
644
645 // check for user post link to main table
646
647 // do we allow duplicate posts with same GUID in different feeds?
648 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
649 $dupcheck_qpart = "AND feed_id = '$feed'";
650 } else {
651 $dupcheck_qpart = "";
652 }
653
654 // error_reporting(0);
655
656 $filter_name = get_filter_name($entry_title, $entry_content,
657 $entry_link, $filters);
658
659 if ($filter_name == "filter") {
660 continue;
661 }
662
663 // error_reporting (DEFAULT_ERROR_LEVEL);
664
665 $result = db_query($link,
666 "SELECT ref_id FROM ttrss_user_entries WHERE
667 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
668 $dupcheck_qpart");
669
670 // okay it doesn't exist - create user entry
671 if (db_num_rows($result) == 0) {
672
673 if ($filter_name != 'catchup') {
674 $unread = 'true';
675 $last_read_qpart = 'NULL';
676 } else {
677 $unread = 'false';
678 $last_read_qpart = 'NOW()';
679 }
680
681 if ($filter_name == 'mark') {
682 $marked = 'true';
683 } else {
684 $marked = 'false';
685 }
686
687 $result = db_query($link,
688 "INSERT INTO ttrss_user_entries
689 (ref_id, owner_uid, feed_id, unread, last_read, marked)
690 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
691 $last_read_qpart, $marked)");
692 }
693
694 $post_needs_update = false;
695
696 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
697 ($content_hash != $orig_content_hash)) {
698 $post_needs_update = true;
699 }
700
701 if ($orig_title != $entry_title) {
702 $post_needs_update = true;
703 }
704
705 if ($orig_num_comments != $num_comments) {
706 $post_needs_update = true;
707 }
708
709 // this doesn't seem to be very reliable
710 //
711 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
712 // $post_needs_update = true;
713 // }
714
715 // if post needs update, update it and mark all user entries
716 // linking to this post as updated
717 if ($post_needs_update) {
718
719 // print "<!-- post $orig_title needs update : $post_needs_update -->";
720
721 db_query($link, "UPDATE ttrss_entries
722 SET title = '$entry_title', content = '$entry_content',
723 num_comments = '$num_comments'
724 WHERE id = '$ref_id'");
725
726 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
727 db_query($link, "UPDATE ttrss_user_entries
728 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
729 } else {
730 db_query($link, "UPDATE ttrss_user_entries
731 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
732 }
733
734 }
735 }
736
737 db_query($link, "COMMIT");
738
739 /* taaaags */
740 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
741
742 $entry_tags = null;
743
744 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
745 $entry_content_unescaped, $entry_tags);
746
747 // print "<br>$entry_title : $entry_content_unescaped<br>";
748 // print_r($entry_tags);
749 // print "<br>";
750
751 $entry_tags = $entry_tags[1];
752
753 if (count($entry_tags) > 0) {
754
755 db_query($link, "BEGIN");
756
757 $result = db_query($link, "SELECT id,int_id
758 FROM ttrss_entries,ttrss_user_entries
759 WHERE guid = '$entry_guid'
760 AND feed_id = '$feed' AND ref_id = id
761 AND owner_uid = '$owner_uid'");
762
763 if (db_num_rows($result) == 1) {
764
765 $entry_id = db_fetch_result($result, 0, "id");
766 $entry_int_id = db_fetch_result($result, 0, "int_id");
767
768 foreach ($entry_tags as $tag) {
769 $tag = db_escape_string(strtolower($tag));
770
771 $tag = str_replace("+", " ", $tag);
772 $tag = str_replace("technorati tag: ", "", $tag);
773
774 $result = db_query($link, "SELECT id FROM ttrss_tags
775 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
776 owner_uid = '$owner_uid' LIMIT 1");
777
778 // print db_fetch_result($result, 0, "id");
779
780 if ($result && db_num_rows($result) == 0) {
781
782 // print "tagging $entry_id as $tag<br>";
783
784 db_query($link, "INSERT INTO ttrss_tags
785 (owner_uid,tag_name,post_int_id)
786 VALUES ('$owner_uid','$tag', '$entry_int_id')");
787 }
788 }
789 }
790 db_query($link, "COMMIT");
791 }
792 }
793
794 db_query($link, "UPDATE ttrss_feeds
795 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
796
797 // db_query($link, "COMMIT");
798
799 } else {
800 $error_msg = db_escape_string(magpie_error());
801 db_query($link,
802 "UPDATE ttrss_feeds SET last_error = '$error_msg',
803 last_updated = NOW() WHERE id = '$feed'");
804 }
805
806 }
807
808 function print_select($id, $default, $values, $attributes = "") {
809 print "<select name=\"$id\" id=\"$id\" $attributes>";
810 foreach ($values as $v) {
811 if ($v == $default)
812 $sel = " selected";
813 else
814 $sel = "";
815
816 print "<option$sel>$v</option>";
817 }
818 print "</select>";
819 }
820
821 function print_select_hash($id, $default, $values, $attributes = "") {
822 print "<select name=\"$id\" id='$id' $attributes>";
823 foreach (array_keys($values) as $v) {
824 if ($v == $default)
825 $sel = "selected";
826 else
827 $sel = "";
828
829 print "<option $sel value=\"$v\">".$values[$v]."</option>";
830 }
831
832 print "</select>";
833 }
834
835 function get_filter_name($title, $content, $link, $filters) {
836
837 if ($filters["title"]) {
838 foreach ($filters["title"] as $filter) {
839 $reg_exp = $filter["reg_exp"];
840 if (preg_match("/$reg_exp/i", $title)) {
841 return $filter["action"];
842 }
843 }
844 }
845
846 if ($filters["content"]) {
847 foreach ($filters["content"] as $filter) {
848 $reg_exp = $filter["reg_exp"];
849 if (preg_match("/$reg_exp/i", $content)) {
850 return $filter["action"];
851 }
852 }
853 }
854
855 if ($filters["both"]) {
856 foreach ($filters["both"] as $filter) {
857 $reg_exp = $filter["reg_exp"];
858 if (preg_match("/$reg_exp/i", $title) ||
859 preg_match("/$reg_exp/i", $content)) {
860 return $filter["action"];
861 }
862 }
863 }
864
865 if ($filters["link"]) {
866 $reg_exp = $filter["reg_exp"];
867 foreach ($filters["link"] as $filter) {
868 $reg_exp = $filter["reg_exp"];
869 if (preg_match("/$reg_exp/i", $link)) {
870 return $filter["action"];
871 }
872 }
873 }
874
875 return false;
876 }
877
878 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
879 $rtl_content = false, $last_updated = false, $last_error = false) {
880
881 if (file_exists($icon_file) && filesize($icon_file) > 0) {
882 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
883 } else {
884 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
885 }
886
887 if ($rtl_content) {
888 $rtl_tag = "dir=\"rtl\"";
889 } else {
890 $rtl_tag = "dir=\"ltr\"";
891 }
892
893 $error_notify_msg = "";
894
895 if ($last_error) {
896 $link_title = "Error: $last_error ($last_updated)";
897 $error_notify_msg = "(Error)";
898 } else if ($last_updated) {
899 $link_title = "Updated: $last_updated";
900 }
901
902 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
903 href=\"#\" onclick=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>";
904
905 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
906 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
907 print "$feed_icon";
908 }
909
910 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
911
912 if ($unread != 0) {
913 $fctr_class = "";
914 } else {
915 $fctr_class = "class=\"invisible\"";
916 }
917
918 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
919 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
920
921 if (get_pref($link, "EXTENDED_FEEDLIST")) {
922 print "<div class=\"feedExtInfo\">
923 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
924 }
925
926 print "</li>";
927
928 }
929
930 function getmicrotime() {
931 list($usec, $sec) = explode(" ",microtime());
932 return ((float)$usec + (float)$sec);
933 }
934
935 function print_radio($id, $default, $values, $attributes = "") {
936 foreach ($values as $v) {
937
938 if ($v == $default)
939 $sel = "checked";
940 else
941 $sel = "";
942
943 if ($v == "Yes") {
944 $sel .= " value=\"1\"";
945 } else {
946 $sel .= " value=\"0\"";
947 }
948
949 print "<input class=\"noborder\"
950 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
951
952 }
953 }
954
955 function initialize_user_prefs($link, $uid) {
956
957 $uid = db_escape_string($uid);
958
959 db_query($link, "BEGIN");
960
961 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
962
963 $u_result = db_query($link, "SELECT pref_name
964 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
965
966 $active_prefs = array();
967
968 while ($line = db_fetch_assoc($u_result)) {
969 array_push($active_prefs, $line["pref_name"]);
970 }
971
972 while ($line = db_fetch_assoc($result)) {
973 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
974 // print "adding " . $line["pref_name"] . "<br>";
975
976 db_query($link, "INSERT INTO ttrss_user_prefs
977 (owner_uid,pref_name,value) VALUES
978 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
979
980 }
981 }
982
983 db_query($link, "COMMIT");
984
985 }
986
987 function lookup_user_id($link, $user) {
988
989 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
990 login = '$login'");
991
992 if (db_num_rows($result) == 1) {
993 return db_fetch_result($result, 0, "id");
994 } else {
995 return false;
996 }
997 }
998
999 function http_authenticate_user($link) {
1000
1001 if (!$_SERVER["PHP_AUTH_USER"]) {
1002
1003 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1004 header('HTTP/1.0 401 Unauthorized');
1005 exit;
1006
1007 } else {
1008 $auth_result = authenticate_user($link,
1009 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1010
1011 if (!$auth_result) {
1012 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1013 header('HTTP/1.0 401 Unauthorized');
1014 exit;
1015 }
1016 }
1017
1018 return true;
1019 }
1020
1021 function authenticate_user($link, $login, $password, $force_auth = false) {
1022
1023 if (!SINGLE_USER_MODE) {
1024
1025 $pwd_hash = 'SHA1:' . sha1($password);
1026
1027 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1028 $query = "SELECT id,login,access_level
1029 FROM ttrss_users WHERE
1030 login = '$login'";
1031 } else {
1032 $query = "SELECT id,login,access_level
1033 FROM ttrss_users WHERE
1034 login = '$login' AND pwd_hash = '$pwd_hash'";
1035 }
1036
1037 $result = db_query($link, $query);
1038
1039 if (db_num_rows($result) == 1) {
1040 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1041 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1042 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1043
1044 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1045 $_SESSION["uid"]);
1046
1047 $user_theme = get_user_theme_path($link);
1048
1049 $_SESSION["theme"] = $user_theme;
1050 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1051
1052 initialize_user_prefs($link, $_SESSION["uid"]);
1053
1054 return true;
1055 }
1056
1057 return false;
1058
1059 } else {
1060
1061 $_SESSION["uid"] = 1;
1062 $_SESSION["name"] = "admin";
1063
1064 $user_theme = get_user_theme_path($link);
1065
1066 $_SESSION["theme"] = $user_theme;
1067 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1068
1069 initialize_user_prefs($link, $_SESSION["uid"]);
1070
1071 return true;
1072 }
1073 }
1074
1075 function make_password($length = 8) {
1076
1077 $password = "";
1078 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1079
1080 $i = 0;
1081
1082 while ($i < $length) {
1083 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1084
1085 if (!strstr($password, $char)) {
1086 $password .= $char;
1087 $i++;
1088 }
1089 }
1090 return $password;
1091 }
1092
1093 // this is called after user is created to initialize default feeds, labels
1094 // or whatever else
1095
1096 // user preferences are checked on every login, not here
1097
1098 function initialize_user($link, $uid) {
1099
1100 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1101 values ('$uid','unread = true', 'Unread articles')");
1102
1103 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1104 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1105
1106 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1107 values ('$uid', 'Tiny Tiny RSS: New Releases',
1108 'http://tt-rss.spb.ru/releases.rss')");
1109
1110 }
1111
1112 function logout_user() {
1113 session_destroy();
1114 if (isset($_COOKIE[session_name()])) {
1115 setcookie(session_name(), '', time()-42000, '/');
1116 }
1117 }
1118
1119 function get_script_urlpath() {
1120 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
1121 }
1122
1123 function get_login_redirect() {
1124 $server = $_SERVER["SERVER_NAME"];
1125
1126 if (ENABLE_LOGIN_SSL) {
1127 $protocol = "https";
1128 } else {
1129 $protocol = "http";
1130 }
1131
1132 $url_path = get_script_urlpath();
1133
1134 $redirect_uri = "$protocol://$server$url_path/login.php";
1135
1136 return $redirect_uri;
1137 }
1138
1139 function validate_session($link) {
1140 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
1141 if ($_SESSION["ip_address"]) {
1142 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
1143 return false;
1144 }
1145 }
1146 }
1147 return true;
1148 }
1149
1150 function basic_nosid_redirect_check() {
1151 if (!SINGLE_USER_MODE) {
1152 if (!$_COOKIE[get_session_cookie_name()]) {
1153 $redirect_uri = get_login_redirect();
1154 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1155 header("Location: $redirect_uri?rt=$return_to");
1156 exit;
1157 }
1158 }
1159 }
1160
1161 function login_sequence($link) {
1162 if (!SINGLE_USER_MODE) {
1163
1164 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1165 $swu = db_escape_string($_REQUEST["swu"]);
1166 if ($swu) {
1167 $_SESSION["prefs_cache"] = false;
1168 return authenticate_user($link, $swu, null, true);
1169 }
1170 }
1171
1172 if (!validate_session($link)) {
1173 logout_user();
1174 $redirect_uri = get_login_redirect();
1175 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1176 header("Location: $redirect_uri?rt=$return_to");
1177 exit;
1178 }
1179
1180 if (!USE_HTTP_AUTH) {
1181 if (!$_SESSION["uid"]) {
1182 $redirect_uri = get_login_redirect();
1183 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
1184 header("Location: $redirect_uri?rt=$return_to");
1185 exit;
1186 }
1187 } else {
1188 if (!$_SESSION["uid"]) {
1189 if (!$_SERVER["PHP_AUTH_USER"]) {
1190
1191 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1192 header('HTTP/1.0 401 Unauthorized');
1193 exit;
1194
1195 } else {
1196 $auth_result = authenticate_user($link,
1197 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1198
1199 if (!$auth_result) {
1200 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
1201 header('HTTP/1.0 401 Unauthorized');
1202 exit;
1203 }
1204 }
1205 }
1206 }
1207 } else {
1208 return authenticate_user($link, "admin", null);
1209 }
1210 }
1211
1212 function truncate_string($str, $max_len) {
1213 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1214 return mb_substr($str, 0, $max_len, "utf-8") . "...";
1215 } else {
1216 return $str;
1217 }
1218 }
1219
1220 function get_user_theme_path($link) {
1221 $result = db_query($link, "SELECT theme_path
1222 FROM
1223 ttrss_themes,ttrss_users
1224 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
1225 if (db_num_rows($result) != 0) {
1226 return db_fetch_result($result, 0, "theme_path");
1227 } else {
1228 return null;
1229 }
1230 }
1231
1232 function smart_date_time($timestamp) {
1233 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1234 return date("G:i", $timestamp);
1235 } else if (date("Y", $timestamp) == date("Y")) {
1236 return date("M d, G:i", $timestamp);
1237 } else {
1238 return date("Y/m/d G:i", $timestamp);
1239 }
1240 }
1241
1242 function smart_date($timestamp) {
1243 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1244 return "Today";
1245 } else if (date("Y", $timestamp) == date("Y")) {
1246 return date("D m", $timestamp);
1247 } else {
1248 return date("Y/m/d", $timestamp);
1249 }
1250 }
1251
1252 function sql_bool_to_string($s) {
1253 if ($s == "t" || $s == "1") {
1254 return "true";
1255 } else {
1256 return "false";
1257 }
1258 }
1259
1260 function sql_bool_to_bool($s) {
1261 if ($s == "t" || $s == "1") {
1262 return true;
1263 } else {
1264 return false;
1265 }
1266 }
1267
1268
1269 function toggleEvenOdd($a) {
1270 if ($a == "even")
1271 return "odd";
1272 else
1273 return "even";
1274 }
1275
1276 function sanity_check($link) {
1277
1278 error_reporting(0);
1279
1280 $error_code = 0;
1281 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1282 $schema_version = db_fetch_result($result, 0, "schema_version");
1283
1284 if ($schema_version != SCHEMA_VERSION) {
1285 $error_code = 5;
1286 }
1287
1288 if (DB_TYPE == "mysql") {
1289 $result = db_query($link, "SELECT true", false);
1290 if (db_num_rows($result) != 1) {
1291 $error_code = 10;
1292 }
1293 }
1294
1295 error_reporting (DEFAULT_ERROR_LEVEL);
1296
1297 if ($error_code != 0) {
1298 print_error_xml($error_code);
1299 return false;
1300 } else {
1301 return true;
1302 }
1303 }
1304
1305 function file_is_locked($filename) {
1306 error_reporting(0);
1307 $fp = fopen($filename, "r");
1308 error_reporting(DEFAULT_ERROR_LEVEL);
1309 if ($fp) {
1310 if (flock($fp, LOCK_EX | LOCK_NB)) {
1311 flock($fp, LOCK_UN);
1312 fclose($fp);
1313 return false;
1314 }
1315 fclose($fp);
1316 return true;
1317 }
1318 return false;
1319 }
1320
1321 function make_lockfile($filename) {
1322 $fp = fopen($filename, "w");
1323
1324 if (flock($fp, LOCK_EX | LOCK_NB)) {
1325 return $fp;
1326 } else {
1327 return false;
1328 }
1329 }
1330
1331 function sql_random_function() {
1332 if (DB_TYPE == "mysql") {
1333 return "RAND()";
1334 } else {
1335 return "RANDOM()";
1336 }
1337 }
1338
1339 function catchup_feed($link, $feed, $cat_view) {
1340
1341 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
1342
1343 if ($cat_view) {
1344
1345 if ($feed > 0) {
1346 $cat_qpart = "cat_id = '$feed'";
1347 } else {
1348 $cat_qpart = "cat_id IS NULL";
1349 }
1350
1351 $tmp_result = db_query($link, "SELECT id
1352 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1353 $_SESSION["uid"]);
1354
1355 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1356
1357 $tmp_feed = $tmp_line["id"];
1358
1359 db_query($link, "UPDATE ttrss_user_entries
1360 SET unread = false,last_read = NOW()
1361 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1362 }
1363
1364 } else if ($feed > 0) {
1365
1366 $tmp_result = db_query($link, "SELECT id
1367 FROM ttrss_feeds WHERE parent_feed = '$feed'
1368 ORDER BY cat_id,title");
1369
1370 $parent_ids = array();
1371
1372 if (db_num_rows($tmp_result) > 0) {
1373 while ($p = db_fetch_assoc($tmp_result)) {
1374 array_push($parent_ids, "feed_id = " . $p["id"]);
1375 }
1376
1377 $children_qpart = implode(" OR ", $parent_ids);
1378
1379 db_query($link, "UPDATE ttrss_user_entries
1380 SET unread = false,last_read = NOW()
1381 WHERE (feed_id = '$feed' OR $children_qpart)
1382 AND owner_uid = " . $_SESSION["uid"]);
1383
1384 } else {
1385 db_query($link, "UPDATE ttrss_user_entries
1386 SET unread = false,last_read = NOW()
1387 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1388 }
1389
1390 } else if ($feed < 0 && $feed > -10) { // special, like starred
1391
1392 if ($feed == -1) {
1393 db_query($link, "UPDATE ttrss_user_entries
1394 SET unread = false,last_read = NOW()
1395 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1396 }
1397
1398 } else if ($feed < -10) { // label
1399
1400 // TODO make this more efficient
1401
1402 $label_id = -$feed - 11;
1403
1404 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1405 WHERE id = '$label_id'");
1406
1407 if ($tmp_result) {
1408 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1409
1410 db_query($link, "BEGIN");
1411
1412 $tmp2_result = db_query($link,
1413 "SELECT
1414 int_id
1415 FROM
1416 ttrss_user_entries,ttrss_entries,ttrss_feeds
1417 WHERE
1418 ref_id = ttrss_entries.id AND
1419 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1420 $sql_exp AND
1421 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1422
1423 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1424 db_query($link, "UPDATE
1425 ttrss_user_entries
1426 SET
1427 unread = false, last_read = NOW()
1428 WHERE
1429 int_id = " . $tmp_line["int_id"]);
1430 }
1431
1432 db_query($link, "COMMIT");
1433
1434 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1435 SET unread = false,last_read = NOW()
1436 WHERE $sql_exp
1437 AND ref_id = id
1438 AND owner_uid = ".$_SESSION["uid"]); */
1439 }
1440 }
1441 } else { // tag
1442 db_query($link, "BEGIN");
1443
1444 $tag_name = db_escape_string($feed);
1445
1446 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1447 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1448
1449 while ($line = db_fetch_assoc($result)) {
1450 db_query($link, "UPDATE ttrss_user_entries SET
1451 unread = false, last_read = NOW()
1452 WHERE int_id = " . $line["post_int_id"]);
1453 }
1454 db_query($link, "COMMIT");
1455 }
1456 }
1457
1458 function update_generic_feed($link, $feed, $cat_view) {
1459 if ($cat_view) {
1460
1461 if ($feed > 0) {
1462 $cat_qpart = "cat_id = '$feed'";
1463 } else {
1464 $cat_qpart = "cat_id IS NULL";
1465 }
1466
1467 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1468 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1469
1470 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1471 $feed_url = $tmp_line["feed_url"];
1472 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1473 }
1474
1475 } else {
1476 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1477 WHERE id = '$feed'");
1478 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1479 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1480 }
1481 }
1482
1483 function getAllCounters($link, $omode = "tflc") {
1484 /* getLabelCounters($link);
1485 getFeedCounters($link);
1486 getTagCounters($link);
1487 getGlobalCounters($link);
1488 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1489 getCategoryCounters($link);
1490 } */
1491
1492 if (!$omode) $omode = "tflc";
1493
1494 getGlobalCounters($link);
1495
1496 if (strchr($omode, "l")) getLabelCounters($link);
1497 if (strchr($omode, "f")) getFeedCounters($link);
1498 if (strchr($omode, "t")) getTagCounters($link);
1499 if (strchr($omode, "c")) {
1500 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1501 getCategoryCounters($link);
1502 }
1503 }
1504 }
1505
1506 function getCategoryCounters($link) {
1507 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1508 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1509 AND unread = true)) AS unread FROM ttrss_feeds
1510 WHERE
1511 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1512
1513 while ($line = db_fetch_assoc($result)) {
1514 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1515 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1516 $line["unread"]."\"/>";
1517 }
1518 }
1519
1520 function getCategoryUnread($link, $cat) {
1521
1522 if ($cat != 0) {
1523 $cat_query = "cat_id = '$cat'";
1524 } else {
1525 $cat_query = "cat_id IS NULL";
1526 }
1527
1528 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
1529 AND hidden = false
1530 AND owner_uid = " . $_SESSION["uid"]);
1531
1532 $cat_feeds = array();
1533 while ($line = db_fetch_assoc($result)) {
1534 array_push($cat_feeds, "feed_id = " . $line["id"]);
1535 }
1536
1537 if (count($cat_feeds) == 0) return 0;
1538
1539 $match_part = implode(" OR ", $cat_feeds);
1540
1541 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1542 FROM ttrss_user_entries
1543 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
1544
1545 $unread = 0;
1546
1547 # this needs to be rewritten
1548 while ($line = db_fetch_assoc($result)) {
1549 $unread += $line["unread"];
1550 }
1551
1552 return $unread;
1553
1554 }
1555
1556 function getFeedUnread($link, $feed, $is_cat = false) {
1557 $n_feed = sprintf("%d", $feed);
1558
1559 if ($is_cat) {
1560 return getCategoryUnread($link, $n_feed);
1561 } else if ($n_feed == -1) {
1562 $match_part = "marked = true";
1563 } else if ($n_feed > 0) {
1564
1565 $result = db_query($link, "SELECT id FROM ttrss_feeds
1566 WHERE parent_feed = '$n_feed'
1567 AND hidden = false
1568 AND owner_uid = " . $_SESSION["uid"]);
1569
1570 if (db_num_rows($result) > 0) {
1571
1572 $linked_feeds = array();
1573 while ($line = db_fetch_assoc($result)) {
1574 array_push($linked_feeds, "feed_id = " . $line["id"]);
1575 }
1576
1577 array_push($linked_feeds, "feed_id = $n_feed");
1578
1579 $match_part = implode(" OR ", $linked_feeds);
1580
1581 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1582 FROM ttrss_user_entries
1583 WHERE unread = true AND ($match_part)
1584 AND owner_uid = " . $_SESSION["uid"]);
1585
1586 $unread = 0;
1587
1588 # this needs to be rewritten
1589 while ($line = db_fetch_assoc($result)) {
1590 $unread += $line["unread"];
1591 }
1592
1593 return $unread;
1594
1595 } else {
1596 $match_part = "feed_id = '$n_feed'";
1597 }
1598 } else if ($feed < -10) {
1599
1600 $label_id = -$feed - 11;
1601
1602 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1603 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1604
1605 $match_part = db_fetch_result($result, 0, "sql_exp");
1606 }
1607
1608 if ($match_part) {
1609
1610 $result = db_query($link, "SELECT count(int_id) AS unread
1611 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1612 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1613 ttrss_user_entries.ref_id = ttrss_entries.id AND
1614 ttrss_feeds.hidden = false AND
1615 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
1616
1617 } else {
1618
1619 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1620 FROM ttrss_tags,ttrss_user_entries
1621 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1622 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1623 }
1624
1625 $unread = db_fetch_result($result, 0, "unread");
1626
1627 return $unread;
1628 }
1629
1630 /* FIXME this needs reworking */
1631
1632 function getGlobalUnread($link, $user_id = false) {
1633
1634 if (!$user_id) {
1635 $user_id = $_SESSION["uid"];
1636 }
1637
1638 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1639 WHERE unread = true AND
1640 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1641 ttrss_user_entries.ref_id = ttrss_entries.id AND
1642 hidden = false AND
1643 ttrss_user_entries.owner_uid = '$user_id'");
1644 $c_id = db_fetch_result($result, 0, "c_id");
1645 return $c_id;
1646 }
1647
1648 function getGlobalCounters($link, $global_unread = -1) {
1649 if ($global_unread == -1) {
1650 $global_unread = getGlobalUnread($link);
1651 }
1652 print "<counter type=\"global\" id='global-unread'
1653 counter='$global_unread'/>";
1654
1655 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1656 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1657
1658 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1659
1660 print "<counter type=\"global\" id='subscribed-feeds'
1661 counter='$subscribed_feeds'/>";
1662
1663 }
1664
1665 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1666
1667 if ($smart_mode) {
1668 if (!$_SESSION["tctr_last_value"]) {
1669 $_SESSION["tctr_last_value"] = array();
1670 }
1671 }
1672
1673 $old_counters = $_SESSION["tctr_last_value"];
1674
1675 $tctrs_modified = false;
1676
1677 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1678 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1679 ttrss_user_entries.ref_id = ttrss_entries.id AND
1680 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1681 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1682 UNION
1683 select tag_name,0 as count FROM ttrss_tags
1684 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1685
1686 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1687 FROM ttrss_user_entries WHERE int_id = post_int_id
1688 AND unread = true)) AS count FROM ttrss_tags
1689 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1690
1691 $tags = array();
1692
1693 while ($line = db_fetch_assoc($result)) {
1694 $tags[$line["tag_name"]] += $line["count"];
1695 }
1696
1697 foreach (array_keys($tags) as $tag) {
1698 $unread = $tags[$tag];
1699
1700 $tag = htmlspecialchars($tag);
1701
1702 if (!$smart_mode || $old_counters[$tag] != $unread) {
1703 $old_counters[$tag] = $unread;
1704 $tctrs_modified = true;
1705 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1706 }
1707
1708 }
1709
1710 if ($smart_mode && $tctrs_modified) {
1711 $_SESSION["tctr_last_value"] = $old_counters;
1712 }
1713
1714 }
1715
1716 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
1717
1718 if ($smart_mode) {
1719 if (!$_SESSION["lctr_last_value"]) {
1720 $_SESSION["lctr_last_value"] = array();
1721 }
1722 }
1723
1724 $ret_arr = array();
1725
1726 $old_counters = $_SESSION["lctr_last_value"];
1727 $lctrs_modified = false;
1728
1729 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1730 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1731 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1732 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1733
1734 $count = db_fetch_result($result, 0, "count");
1735
1736 if (!$ret_mode) {
1737 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1738 } else {
1739 $ret_arr["-1"]["counter"] = $count;
1740 $ret_arr["-1"]["description"] = "Starred";
1741 }
1742
1743 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1744 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1745
1746 while ($line = db_fetch_assoc($result)) {
1747
1748 $id = -$line["id"] - 11;
1749
1750 $label_name = $line["description"];
1751
1752 error_reporting (0);
1753
1754 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
1755 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1756 ttrss_feeds.hidden = false AND
1757 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1758 ttrss_user_entries.ref_id = ttrss_entries.id AND
1759 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
1760
1761 $count = db_fetch_result($tmp_result, 0, "count");
1762
1763 if (!$smart_mode || $old_counters[$id] != $count) {
1764 $old_counters[$id] = $count;
1765 $lctrs_modified = true;
1766 if (!$ret_mode) {
1767 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1768 } else {
1769 $ret_arr[$id]["counter"] = $count;
1770 $ret_arr[$id]["description"] = $label_name;
1771 }
1772 }
1773
1774 error_reporting (DEFAULT_ERROR_LEVEL);
1775 }
1776
1777 if ($smart_mode && $lctrs_modified) {
1778 $_SESSION["lctr_last_value"] = $old_counters;
1779 }
1780
1781 return $ret_arr;
1782 }
1783
1784 /* function getFeedCounter($link, $id) {
1785
1786 $result = db_query($link, "SELECT
1787 count(id) as count,last_error
1788 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1789 WHERE feed_id = '$id' AND unread = true
1790 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1791 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1792
1793 $count = db_fetch_result($result, 0, "count");
1794 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1795
1796 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1797 } */
1798
1799 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1800
1801 if ($smart_mode) {
1802 if (!$_SESSION["fctr_last_value"]) {
1803 $_SESSION["fctr_last_value"] = array();
1804 }
1805 }
1806
1807 $old_counters = $_SESSION["fctr_last_value"];
1808
1809 $result = db_query($link, "SELECT id,last_error,parent_feed,
1810 SUBSTRING(last_updated,1,19) AS last_updated,
1811 (SELECT count(id)
1812 FROM ttrss_entries,ttrss_user_entries
1813 WHERE feed_id = ttrss_feeds.id AND
1814 ttrss_user_entries.ref_id = ttrss_entries.id
1815 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1816 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1817 AND parent_feed IS NULL");
1818
1819 $fctrs_modified = false;
1820
1821 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1822
1823 while ($line = db_fetch_assoc($result)) {
1824
1825 $id = $line["id"];
1826 $count = $line["count"];
1827 $last_error = htmlspecialchars($line["last_error"]);
1828
1829 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1830 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1831 } else {
1832 $last_updated = date($short_date, strtotime($line["last_updated"]));
1833 }
1834
1835 $has_img = is_file(ICONS_DIR . "/$id.ico");
1836
1837 $tmp_result = db_query($link,
1838 "SELECT id,COUNT(unread) AS unread
1839 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1840 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1841 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1842
1843 if (db_num_rows($tmp_result) > 0) {
1844 while ($l = db_fetch_assoc($tmp_result)) {
1845 $count += $l["unread"];
1846 }
1847 }
1848
1849 if (!$smart_mode || $old_counters[$id] != $count) {
1850 $old_counters[$id] = $count;
1851 $fctrs_modified = true;
1852
1853 if ($last_error) {
1854 $error_part = "error=\"$last_error\"";
1855 } else {
1856 $error_part = "";
1857 }
1858
1859 if ($has_img) {
1860 $has_img_part = "hi=\"$has_img\"";
1861 } else {
1862 $has_img_part = "";
1863 }
1864
1865 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1866 }
1867 }
1868
1869 if ($smart_mode && $fctrs_modified) {
1870 $_SESSION["fctr_last_value"] = $old_counters;
1871 }
1872 }
1873
1874 function get_script_dt_add() {
1875 if (strpos(VERSION, "99") === false) {
1876 return VERSION;
1877 } else {
1878 return time();
1879 }
1880 }
1881
1882 function get_pgsql_version($link) {
1883 $result = db_query($link, "SELECT version() AS version");
1884 $version = split(" ", db_fetch_result($result, 0, "version"));
1885 return $version[1];
1886 }
1887
1888 function print_error_xml($code, $add_msg = "") {
1889 global $ERRORS;
1890
1891 $error_msg = $ERRORS[$code];
1892
1893 if ($add_msg) {
1894 $error_msg = "$error_msg; $add_msg";
1895 }
1896
1897 print "<rpc-reply>";
1898 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1899 print "</rpc-reply>";
1900 }
1901
1902 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
1903
1904 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
1905
1906 if ($feed_link == "") return;
1907
1908 if ($cat_id == "0" || !$cat_id) {
1909 $cat_qpart = "NULL";
1910 } else {
1911 $cat_qpart = "'$cat_id'";
1912 }
1913
1914 $result = db_query($link,
1915 "SELECT id FROM ttrss_feeds
1916 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
1917
1918 if (db_num_rows($result) == 0) {
1919
1920 $result = db_query($link,
1921 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
1922 VALUES ('".$_SESSION["uid"]."', '$feed_link',
1923 '[Unknown]', $cat_qpart)");
1924
1925 $result = db_query($link,
1926 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
1927 AND owner_uid = " . $_SESSION["uid"]);
1928
1929 $feed_id = db_fetch_result($result, 0, "id");
1930
1931 if ($feed_id) {
1932 update_rss_feed($link, $feed_link, $feed_id, true);
1933 }
1934
1935 return true;
1936 } else {
1937 return false;
1938 }
1939 }
1940
1941 function print_feed_select($link, $id, $default_id = "",
1942 $attributes = "", $include_all_feeds = true) {
1943
1944 print "<select id=\"$id\" name=\"$id\" $attributes>";
1945 if ($include_all_feeds) {
1946 print "<option value=\"0\">All feeds</option>";
1947 }
1948
1949 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
1950 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1951
1952 if (db_num_rows($result) > 0 && $include_all_feeds) {
1953 print "<option disabled>--------</option>";
1954 }
1955
1956 while ($line = db_fetch_assoc($result)) {
1957 if ($line["id"] == $default_id) {
1958 $is_selected = "selected";
1959 } else {
1960 $is_selected = "";
1961 }
1962 printf("<option $is_selected value='%d'>%s</option>",
1963 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1964 }
1965
1966 print "</select>";
1967 }
1968
1969 function print_feed_cat_select($link, $id, $default_id = "",
1970 $attributes = "", $include_all_cats = true) {
1971
1972 print "<select id=\"$id\" name=\"$id\" $attributes>";
1973
1974 if ($include_all_cats) {
1975 print "<option value=\"0\">Uncategorized</option>";
1976 }
1977
1978 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
1979 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
1980
1981 if (db_num_rows($result) > 0 && $include_all_cats) {
1982 print "<option disabled>--------</option>";
1983 }
1984
1985 while ($line = db_fetch_assoc($result)) {
1986 if ($line["id"] == $default_id) {
1987 $is_selected = "selected";
1988 } else {
1989 $is_selected = "";
1990 }
1991 printf("<option $is_selected value='%d'>%s</option>",
1992 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
1993 }
1994
1995 print "</select>";
1996 }
1997
1998 function checkbox_to_sql_bool($val) {
1999 return ($val == "on") ? "true" : "false";
2000 }
2001
2002 function getFeedCatTitle($link, $id) {
2003 if ($id == -1) {
2004 return "Special";
2005 } else if ($id < -10) {
2006 return "Labels";
2007 } else if ($id > 0) {
2008 $result = db_query($link, "SELECT ttrss_feed_categories.title
2009 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2010 cat_id = ttrss_feed_categories.id");
2011 if (db_num_rows($result) == 1) {
2012 return db_fetch_result($result, 0, "title");
2013 } else {
2014 return "Uncategorized";
2015 }
2016 } else {
2017 return "getFeedCatTitle($id) failed";
2018 }
2019
2020 }
2021
2022 function getFeedTitle($link, $id) {
2023 if ($id == -1) {
2024 return "Starred articles";
2025 } else if ($id < -10) {
2026 $label_id = -10 - $id;
2027 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2028 if (db_num_rows($result) == 1) {
2029 return db_fetch_result($result, 0, "description");
2030 } else {
2031 return "Unknown label ($label_id)";
2032 }
2033
2034 } else if ($id > 0) {
2035 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2036 if (db_num_rows($result) == 1) {
2037 return db_fetch_result($result, 0, "title");
2038 } else {
2039 return "Unknown feed ($id)";
2040 }
2041 } else {
2042 return "getFeedTitle($id) failed";
2043 }
2044
2045 }
2046
2047 function get_session_cookie_name() {
2048 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
2049 }
2050
2051 function print_init_params($link) {
2052 print "<init-params>";
2053 if ($_SESSION["stored-params"]) {
2054 foreach (array_keys($_SESSION["stored-params"]) as $key) {
2055 if ($key) {
2056 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2057 print "<param key=\"$key\" value=\"$value\"/>";
2058 }
2059 }
2060 }
2061
2062 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2063 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
2064 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
2065
2066 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2067 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2068
2069 print "<param key=\"hide_read_feeds\" value=\"" .
2070 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
2071
2072 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2073 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
2074
2075 print "<param key=\"confirm_feed_catchup\" value=\"" .
2076 sprintf("%d", get_pref($link, "CONFIRM_FEED_CATCHUP")) . "\"/>";
2077
2078 print "</init-params>";
2079 }
2080
2081 function print_runtime_info($link) {
2082 print "<runtime-info>";
2083 if (ENABLE_UPDATE_DAEMON) {
2084 print "<param key=\"daemon_is_running\" value=\"".
2085 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2086 }
2087 print "</runtime-info>";
2088 }
2089
2090 function getSearchSql($search, $match_on) {
2091
2092 $search_query_part = "";
2093
2094 $keywords = split(" ", $search);
2095 $query_keywords = array();
2096
2097 if ($match_on == "both") {
2098
2099 foreach ($keywords as $k) {
2100 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2101 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2102 }
2103
2104 $search_query_part = implode("AND", $query_keywords) . " AND ";
2105
2106 } else if ($match_on == "title") {
2107
2108 foreach ($keywords as $k) {
2109 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2110 }
2111
2112 $search_query_part = implode("AND", $query_keywords) . " AND ";
2113
2114 } else if ($match_on == "content") {
2115
2116 foreach ($keywords as $k) {
2117 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2118 }
2119 }
2120
2121 $search_query_part = implode("AND", $query_keywords);
2122
2123 return $search_query_part;
2124 }
2125
2126 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) {
2127 if ($search) {
2128
2129 $search_query_part = getSearchSql($search, $match_on);
2130 $search_query_part .= " AND ";
2131
2132 } else {
2133 $search_query_part = "";
2134 }
2135
2136 $view_query_part = "";
2137
2138 if ($view_mode == "adaptive") {
2139 if ($search) {
2140 $view_query_part = " ";
2141 } else if ($feed != -1) {
2142 $unread = getFeedUnread($link, $feed, $cat_view);
2143 if ($unread > 0) {
2144 $view_query_part = " unread = true AND ";
2145 }
2146 }
2147 }
2148
2149 if ($view_mode == "marked") {
2150 $view_query_part = " marked = true AND ";
2151 }
2152
2153 if ($view_mode == "unread") {
2154 $view_query_part = " unread = true AND ";
2155 }
2156
2157 if ($limit > 0) {
2158 $limit_query_part = "LIMIT " . $limit;
2159 }
2160
2161 $vfeed_query_part = "";
2162
2163 // override query strategy and enable feed display when searching globally
2164 if ($search && $search_mode == "all_feeds") {
2165 $query_strategy_part = "ttrss_entries.id > 0";
2166 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2167 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2168 $query_strategy_part = "ttrss_entries.id > 0";
2169 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2170 id = feed_id) as feed_title,";
2171 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2172
2173 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2174
2175 $tmp_result = false;
2176
2177 if ($cat_view) {
2178 $tmp_result = db_query($link, "SELECT id
2179 FROM ttrss_feeds WHERE cat_id = '$feed'");
2180 } else {
2181 $tmp_result = db_query($link, "SELECT id
2182 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2183 WHERE id = '$feed') AND id != '$feed'");
2184 }
2185
2186 $cat_siblings = array();
2187
2188 if (db_num_rows($tmp_result) > 0) {
2189 while ($p = db_fetch_assoc($tmp_result)) {
2190 array_push($cat_siblings, "feed_id = " . $p["id"]);
2191 }
2192
2193 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2194 $feed, implode(" OR ", $cat_siblings));
2195
2196 } else {
2197 $query_strategy_part = "ttrss_entries.id > 0";
2198 }
2199
2200 } else if ($feed >= 0) {
2201
2202 if ($cat_view) {
2203
2204 if ($feed > 0) {
2205 $query_strategy_part = "cat_id = '$feed'";
2206 } else {
2207 $query_strategy_part = "cat_id IS NULL";
2208 }
2209
2210 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2211
2212 } else {
2213 $tmp_result = db_query($link, "SELECT id
2214 FROM ttrss_feeds WHERE parent_feed = '$feed'
2215 ORDER BY cat_id,title");
2216
2217 $parent_ids = array();
2218
2219 if (db_num_rows($tmp_result) > 0) {
2220 while ($p = db_fetch_assoc($tmp_result)) {
2221 array_push($parent_ids, "feed_id = " . $p["id"]);
2222 }
2223
2224 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2225 $feed, implode(" OR ", $parent_ids));
2226
2227 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2228 } else {
2229 $query_strategy_part = "feed_id = '$feed'";
2230 }
2231 }
2232 } else if ($feed == -1) { // starred virtual feed
2233 $query_strategy_part = "marked = true";
2234 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2235 } else if ($feed <= -10) { // labels
2236 $label_id = -$feed - 11;
2237
2238 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2239 WHERE id = '$label_id'");
2240
2241 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2242
2243 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2244 } else {
2245 $query_strategy_part = "id > 0"; // dumb
2246 }
2247
2248 if (get_pref($link, 'REVERSE_HEADLINES')) {
2249 $order_by = "updated";
2250 } else {
2251 $order_by = "updated DESC";
2252 }
2253
2254 if ($override_order) {
2255 $order_by = $override_order;
2256 }
2257
2258 $feed_title = "";
2259
2260 if ($search && $search_mode == "all_feeds") {
2261 $feed_title = "Global search results ($search)";
2262 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2263 $feed_title = "Tag search results ($search, $feed)";
2264 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2265 $feed_title = $feed;
2266 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2267
2268 if ($cat_view) {
2269
2270 if ($feed != 0) {
2271 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2272 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2273 $feed_title = db_fetch_result($result, 0, "title");
2274 } else {
2275 $feed_title = "Uncategorized";
2276 }
2277
2278 if ($search) {
2279 $feed_title = "Category search results ($search, $feed_title)";
2280 }
2281
2282 } else {
2283
2284 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2285 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2286
2287 $feed_title = db_fetch_result($result, 0, "title");
2288 $feed_site_url = db_fetch_result($result, 0, "site_url");
2289 $last_error = db_fetch_result($result, 0, "last_error");
2290
2291 if ($search) {
2292 $feed_title = "Feed search results ($search, $feed_title)";
2293 }
2294 }
2295
2296 } else if ($feed == -1) {
2297 $feed_title = "Starred articles";
2298 } else if ($feed < -10) {
2299 $label_id = -$feed - 11;
2300 $result = db_query($link, "SELECT description FROM ttrss_labels
2301 WHERE id = '$label_id'");
2302 $feed_title = db_fetch_result($result, 0, "description");
2303
2304 if ($search) {
2305 $feed_title = "Label search results ($search, $feed_title)";
2306 }
2307 } else {
2308 $feed_title = "?";
2309 }
2310
2311 $feed_title = db_unescape_string($feed_title);
2312
2313 if ($feed < -10) error_reporting (0);
2314
2315 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2316
2317 if ($feed >= 0) {
2318 $feed_kind = "Feeds";
2319 } else {
2320 $feed_kind = "Labels";
2321 }
2322
2323 $content_query_part = "content as content_preview,";
2324
2325 $query = "SELECT
2326 guid,
2327 ttrss_entries.id,ttrss_entries.title,
2328 SUBSTRING(updated,1,16) as updated,
2329 unread,feed_id,marked,link,last_read,
2330 SUBSTRING(last_read,1,19) as last_read_noms,
2331 $vfeed_query_part
2332 $content_query_part
2333 SUBSTRING(updated,1,19) as updated_noms,
2334 author
2335 FROM
2336 ttrss_entries,ttrss_user_entries,ttrss_feeds
2337 WHERE
2338 ttrss_feeds.hidden = false AND
2339 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2340 ttrss_user_entries.ref_id = ttrss_entries.id AND
2341 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2342 $search_query_part
2343 $view_query_part
2344 $query_strategy_part ORDER BY $order_by
2345 $limit_query_part";
2346
2347 $result = db_query($link, $query);
2348
2349 if ($_GET["debug"]) print $query;
2350
2351 } else {
2352 // browsing by tag
2353
2354 $feed_kind = "Tags";
2355
2356 $result = db_query($link, "SELECT
2357 guid,
2358 ttrss_entries.id as id,title,
2359 SUBSTRING(updated,1,16) as updated,
2360 unread,feed_id,
2361 marked,link,last_read,
2362 SUBSTRING(last_read,1,19) as last_read_noms,
2363 $vfeed_query_part
2364 $content_query_part
2365 SUBSTRING(updated,1,19) as updated_noms
2366 FROM
2367 ttrss_entries,ttrss_user_entries,ttrss_tags
2368 WHERE
2369 ref_id = ttrss_entries.id AND
2370 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2371 post_int_id = int_id AND tag_name = '$feed' AND
2372 $view_query_part
2373 $search_query_part
2374 $query_strategy_part ORDER BY $order_by
2375 $limit_query_part");
2376 }
2377
2378 return array($result, $feed_title, $feed_site_url, $last_error);
2379
2380 }
2381
2382 function generate_syndicated_feed($link, $feed, $is_cat,
2383 $search, $search_mode, $match_on) {
2384
2385 $qfh_ret = queryFeedHeadlines($link, $feed,
2386 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
2387
2388 $result = $qfh_ret[0];
2389 $feed_title = htmlspecialchars($qfh_ret[1]);
2390 $feed_site_url = $qfh_ret[2];
2391 $last_error = $qfh_ret[3];
2392
2393 print "<rss version=\"2.0\">
2394 <channel>
2395 <title>$feed_title</title>
2396 <link>$feed_site_url</link>
2397 <generator>Tiny Tiny RSS v".VERSION."</generator>";
2398
2399 while ($line = db_fetch_assoc($result)) {
2400 print "<item>";
2401 print "<id>" . htmlspecialchars($line["guid"]) . "</id>";
2402 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2403
2404 $rfc822_date = date('r', strtotime($line["updated"]));
2405
2406 print "<pubDate>$rfc822_date</pubDate>";
2407
2408 print "<title>" .
2409 htmlspecialchars($line["title"]) . "</title>";
2410
2411 print "<description>" .
2412 htmlspecialchars($line["content_preview"]) . "</description>";
2413
2414 print "</item>";
2415 }
2416
2417 print "</channel></rss>";
2418
2419 }
2420
2421 function getCategoryTitle($link, $cat_id) {
2422
2423 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2424 id = '$cat_id'");
2425
2426 if (db_num_rows($result) == 1) {
2427 return db_fetch_result($result, 0, "title");
2428 } else {
2429 return "Uncategorized";
2430 }
2431 }
2432
2433 function sanitize_rss($str) {
2434 $res = $str;
2435
2436 $res = preg_replace('/<script.*?>/i',
2437 "<p class=\"scriptWarn\">Disabled script: ", $res);
2438
2439 $res = preg_replace('/<\/script.*?>/i', "</p>", $res);
2440
2441 /* $res = preg_replace('/<embed.*?>/i', "", $res);
2442
2443 $res = preg_replace('/<object.*?>.*?<\/object>/i',
2444 "<p class=\"objectWarn\">(Disabled html object
2445 - flash or other embedded content)</p>", $res); */
2446
2447 return $res;
2448 }
2449
2450 function send_headlines_digests($link, $limit = 100) {
2451
2452 if (!DIGEST_ENABLE) return false;
2453
2454 $user_limit = DIGEST_EMAIL_LIMIT;
2455 $days = 1;
2456
2457 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2458
2459 if (DB_TYPE == "pgsql") {
2460 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2461 } else if (DB_TYPE == "mysql") {
2462 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2463 }
2464
2465 $result = db_query($link, "SELECT id,email FROM ttrss_users
2466 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2467
2468 while ($line = db_fetch_assoc($result)) {
2469 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2470 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2471
2472 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2473 $digest = $tuple[0];
2474 $headlines_count = $tuple[1];
2475
2476 if ($headlines_count > 0) {
2477 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2478 "[tt-rss] New headlines for last 24 hours", $digest,
2479 "From: " . MAIL_FROM . "\n".
2480 "Content-Type: text/plain; charset=\"utf-8\"\n".
2481 "Content-Transfer-Encoding: 8bit\n");
2482 print "RC=$rc\n";
2483 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2484 WHERE id = " . $line["id"]);
2485 } else {
2486 print "No headlines\n";
2487 }
2488 }
2489 }
2490
2491 // $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2492
2493 }
2494
2495 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
2496 $tmp = "New headlines for last 24 hours, as of " . date("Y/m/d H:m") . "\n";
2497 $tmp .= "=======================================================\n\n";
2498
2499 if (DB_TYPE == "pgsql") {
2500 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
2501 } else if (DB_TYPE == "mysql") {
2502 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2503 }
2504
2505 $result = db_query($link, "SELECT ttrss_entries.title,
2506 ttrss_feeds.title AS feed_title,
2507 date_entered,
2508 link,
2509 SUBSTRING(last_updated,1,19) AS last_updated
2510 FROM
2511 ttrss_user_entries,ttrss_entries,ttrss_feeds
2512 WHERE
2513 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
2514 AND include_in_digest = true
2515 AND $interval_query
2516 AND ttrss_user_entries.owner_uid = $user_id
2517 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2518 LIMIT $limit");
2519
2520 $cur_feed_title = "";
2521
2522 $headlines_count = db_num_rows($result);
2523
2524 while ($line = db_fetch_assoc($result)) {
2525 $updated = smart_date_time(strtotime($line["last_updated"]));
2526 $feed_title = $line["feed_title"];
2527
2528 if ($cur_feed_title != $feed_title) {
2529 $cur_feed_title = $feed_title;
2530
2531 $tmp .= "$feed_title\n\n";
2532 }
2533
2534 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2535 $tmp .= " " . trim($line["link"]) . "\n";
2536 $tmp .= "\n";
2537 }
2538
2539 $tmp .= "--- \n";
2540 $tmp .= "You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at " .
2541 DIGEST_HOSTNAME . "\n".
2542 "To unsubscribe, visit your configuration options or contact instance owner.\n";
2543
2544
2545 return array($tmp, $headlines_count);
2546 }
2547
2548 function check_for_update($link) {
2549 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2550
2551 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
2552 return;
2553 }
2554
2555 error_reporting(0);
2556 $rss = fetch_rss($releases_feed);
2557 error_reporting (DEFAULT_ERROR_LEVEL);
2558
2559 if ($rss) {
2560
2561 $items = $rss->items;
2562
2563 if (!$items || !is_array($items)) $items = $rss->entries;
2564 if (!$items || !is_array($items)) $items = $rss;
2565
2566 if (!is_array($items) || count($items) == 0) {
2567 return;
2568 }
2569
2570 $latest_item = $items[0];
2571
2572 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
2573
2574 $release_url = sanitize_rss($latest_item["link"]);
2575 $content = sanitize_rss($latest_item["description"]);
2576
2577 if (version_compare(VERSION, $latest_version) == -1) {
2578 return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\">
2579 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
2580 <div id=\"milestoneDetails\">$content</div></div>";
2581 }
2582 }
2583 }
2584
2585 function markArticlesById($link, $ids, $cmode) {
2586
2587 $tmp_ids = array();
2588
2589 foreach ($ids as $id) {
2590 array_push($tmp_ids, "ref_id = '$id'");
2591 }
2592
2593 $ids_qpart = join(" OR ", $tmp_ids);
2594
2595 if ($cmode == 0) {
2596 db_query($link, "UPDATE ttrss_user_entries SET
2597 marked = false,last_read = NOW()
2598 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2599 } else if ($cmode == 1) {
2600 db_query($link, "UPDATE ttrss_user_entries SET
2601 marked = true
2602 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2603 } else {
2604 db_query($link, "UPDATE ttrss_user_entries SET
2605 marked = NOT marked,last_read = NOW()
2606 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2607 }
2608 }
2609
2610 function catchupArticlesById($link, $ids, $cmode) {
2611
2612 $tmp_ids = array();
2613
2614 foreach ($ids as $id) {
2615 array_push($tmp_ids, "ref_id = '$id'");
2616 }
2617
2618 $ids_qpart = join(" OR ", $tmp_ids);
2619
2620 if ($cmode == 0) {
2621 db_query($link, "UPDATE ttrss_user_entries SET
2622 unread = false,last_read = NOW()
2623 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2624 } else if ($cmode == 1) {
2625 db_query($link, "UPDATE ttrss_user_entries SET
2626 unread = true
2627 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2628 } else {
2629 db_query($link, "UPDATE ttrss_user_entries SET
2630 unread = NOT unread,last_read = NOW()
2631 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2632 }
2633 }
2634
2635 function escape_for_form($s) {
2636 return htmlspecialchars(db_unescape_string($s));
2637 }
2638
2639 function make_guid_from_title($title) {
2640 return preg_replace("/[ \"\',.:;]/", "-",
2641 mb_strtolower(strip_tags($title)));
2642 }
2643
2644 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2645 $bottom = false, $rtl_content = false, $feed_id = 0,
2646 $is_cat = false, $search = false, $match_on = false,
2647 $search_mode = false) {
2648
2649 if (!$bottom) {
2650 $class = "headlinesSubToolbar";
2651 $tid = "headlineActionsTop";
2652 } else {
2653 $class = "headlinesSubToolbar";
2654 $tid = "headlineActionsBottom";
2655 }
2656
2657 print "<table class=\"$class\" id=\"$tid\"
2658 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2659
2660 if ($rtl_content) {
2661 $rtl_cpart = "RTL";
2662 } else {
2663 $rtl_cpart = "";
2664 }
2665
2666 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2667
2668 print "<td class=\"headlineActions$rtl_cpart\">
2669 Select:
2670 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)\">All</a>,
2671 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>,
2672 <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)\">None</a>
2673 &nbsp;&nbsp;
2674 Toggle: <a href=\"javascript:selectionToggleUnread()\">Unread</a>,
2675 <a href=\"javascript:selectionToggleMarked()\">Starred</a>
2676 &nbsp;&nbsp;
2677 Mark as read:
2678 <a href=\"#\" onclick=\"catchupPage()\">Page</a>,
2679 <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>";
2680 print "</td>";
2681
2682 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
2683 print "<td class=\"headlineActions$rtl_cpart\">
2684 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2685 '$match_on', '$feed_id', '$is_cat');\">
2686 Convert this search to label</a></td>";
2687 }
2688
2689 } else {
2690
2691 print "<td class=\"headlineActions$rtl_cpart\">
2692 Select:
2693 <a href=\"javascript:cdmSelectArticles('all')\">All</a>,
2694 <a href=\"javascript:cdmSelectArticles('unread')\">Unread</a>,
2695 <a href=\"javascript:cdmSelectArticles('none')\">None</a>
2696 &nbsp;&nbsp;
2697 Toggle: <a href=\"javascript:selectionToggleUnread(true)\">Unread</a>,
2698 <a href=\"javascript:selectionToggleMarked(true)\">Starred</a>
2699 &nbsp;&nbsp;
2700 Mark as read:
2701 <a href=\"#\" onclick=\"catchupPage()\">Page</a>,
2702 <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>";
2703
2704 print "</td>";
2705
2706 }
2707
2708 print "<td class=\"headlineTitle$rtl_cpart\">";
2709
2710 if ($feed_site_url) {
2711 if (!$bottom) {
2712 $target = "target=\"_blank\"";
2713 }
2714 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
2715 } else {
2716 print $feed_title;
2717 }
2718
2719 if ($search) {
2720 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
2721 }
2722
2723 if (!$bottom) {
2724 print "&nbsp;
2725 <a target=\"_new\"
2726 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
2727 <img class=\"noborder\"
2728 alt=\"Generated feed\" src=\"images/feed-icon-12x12.png\">
2729 </a>";
2730 }
2731
2732 print "</td>";
2733 print "</tr></table>";
2734
2735 }
2736
2737 function outputFeedList($link, $tags = false) {
2738
2739 print "<ul class=\"feedList\" id=\"feedList\">\n";
2740
2741 $owner_uid = $_SESSION["uid"];
2742
2743 /* virtual feeds */
2744
2745 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2746 print "<li class=\"feedCat\">Special</li>";
2747 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2748 }
2749
2750 $num_starred = getFeedUnread($link, -1);
2751
2752 $class = "virt";
2753
2754 if ($num_starred > 0) $class .= "Unread";
2755
2756 printFeedEntry(-1, $class, "Starred articles", $num_starred,
2757 "images/mark_set.png", $link);
2758
2759 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2760 print "</ul>\n";
2761 }
2762
2763 if (!$tags) {
2764
2765 if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
2766
2767 $result = db_query($link, "SELECT id,sql_exp,description FROM
2768 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
2769
2770 if (db_num_rows($result) > 0) {
2771 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2772 print "<li class=\"feedCat\">Labels</li>";
2773 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2774 } else {
2775 print "<li><hr></li>";
2776 }
2777 }
2778
2779 while ($line = db_fetch_assoc($result)) {
2780
2781 error_reporting (0);
2782
2783 $label_id = -$line['id'] - 11;
2784 $count = getFeedUnread($link, $label_id);
2785
2786 $class = "label";
2787
2788 if ($count > 0) {
2789 $class .= "Unread";
2790 }
2791
2792 error_reporting (DEFAULT_ERROR_LEVEL);
2793
2794 printFeedEntry($label_id,
2795 $class, db_unescape_string($line["description"]),
2796 $count, "images/label.png", $link);
2797
2798 }
2799
2800 if (db_num_rows($result) > 0) {
2801 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2802 print "</ul>";
2803 }
2804 }
2805
2806 }
2807
2808 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
2809 print "<li><hr></li>";
2810 }
2811
2812 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2813 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2814 $order_by_qpart = "category,unread DESC,title";
2815 } else {
2816 $order_by_qpart = "category,title";
2817 }
2818 } else {
2819 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2820 $order_by_qpart = "unread DESC,title";
2821 } else {
2822 $order_by_qpart = "title";
2823 }
2824 }
2825
2826 $result = db_query($link, "SELECT ttrss_feeds.*,
2827 SUBSTRING(last_updated,1,19) AS last_updated_noms,
2828 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
2829 WHERE feed_id = ttrss_feeds.id AND unread = true
2830 AND ttrss_user_entries.ref_id = ttrss_entries.id
2831 AND owner_uid = '$owner_uid') as unread,
2832 cat_id,last_error,
2833 ttrss_feed_categories.title AS category,
2834 ttrss_feed_categories.collapsed
2835 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
2836 ON (ttrss_feed_categories.id = cat_id)
2837 WHERE
2838 ttrss_feeds.hidden = false AND
2839 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
2840 ORDER BY $order_by_qpart");
2841
2842 $actid = $_GET["actid"];
2843
2844 /* real feeds */
2845
2846 $lnum = 0;
2847
2848 $total_unread = 0;
2849
2850 $category = "";
2851
2852 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2853
2854 while ($line = db_fetch_assoc($result)) {
2855
2856 $feed = db_unescape_string($line["title"]);
2857 $feed_id = $line["id"];
2858
2859 $subop = $_GET["subop"];
2860
2861 $unread = $line["unread"];
2862
2863 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2864 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
2865 } else {
2866 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
2867 }
2868
2869 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
2870
2871 if ($rtl_content) {
2872 $rtl_tag = "dir=\"RTL\"";
2873 } else {
2874 $rtl_tag = "";
2875 }
2876
2877 $tmp_result = db_query($link,
2878 "SELECT id,COUNT(unread) AS unread
2879 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2880 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
2881 WHERE parent_feed = '$feed_id' AND unread = true
2882 GROUP BY ttrss_feeds.id");
2883
2884 if (db_num_rows($tmp_result) > 0) {
2885 while ($l = db_fetch_assoc($tmp_result)) {
2886 $unread += $l["unread"];
2887 }
2888 }
2889
2890 $cat_id = $line["cat_id"];
2891
2892 $tmp_category = $line["category"];
2893
2894 if (!$tmp_category) {
2895 $tmp_category = "Uncategorized";
2896 }
2897
2898 // $class = ($lnum % 2) ? "even" : "odd";
2899
2900 if ($line["last_error"]) {
2901 $class = "error";
2902 } else {
2903 $class = "feed";
2904 }
2905
2906 if ($unread > 0) $class .= "Unread";
2907
2908 if ($actid == $feed_id) {
2909 $class .= "Selected";
2910 }
2911
2912 $total_unread += $unread;
2913
2914 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
2915
2916 if ($category) {
2917 print "</ul></li>";
2918 }
2919
2920 $category = $tmp_category;
2921
2922 $collapsed = $line["collapsed"];
2923
2924 // workaround for NULL category
2925 if ($category == "Uncategorized") {
2926 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
2927 $collapsed = "t";
2928 }
2929 }
2930
2931 if ($collapsed == "t" || $collapsed == "1") {
2932 $holder_class = "invisible";
2933 $ellipsis = "...";
2934 } else {
2935 $holder_class = "";
2936 $ellipsis = "";
2937 }
2938
2939 $cat_id = sprintf("%d", $cat_id);
2940
2941 $cat_unread = getCategoryUnread($link, $cat_id);
2942
2943 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
2944
2945 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
2946 <a id=\"FCATN-$cat_id\" href=\"#\"onclick=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
2947 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
2948 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
2949 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
2950 </a></li>";
2951
2952 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
2953 // -> keyboard navigation, etc.
2954 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
2955 }
2956
2957 printFeedEntry($feed_id, $class, $feed, $unread,
2958 "icons/$feed_id.ico", $link, $rtl_content,
2959 $last_updated, $line["last_error"]);
2960
2961 ++$lnum;
2962 }
2963
2964 if (db_num_rows($result) == 0) {
2965 print "<li>No feeds to display.</li>";
2966 }
2967
2968 } else {
2969
2970 // tags
2971
2972 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2973 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2974 post_int_id = ttrss_user_entries.int_id AND
2975 unread = true AND ref_id = ttrss_entries.id
2976 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
2977 UNION
2978 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
2979 ORDER BY tag_name"); */
2980
2981 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2982 print "<li class=\"feedCat\">Tags</li>";
2983 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2984 }
2985
2986 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
2987 FROM ttrss_user_entries WHERE int_id = post_int_id
2988 AND unread = true)) AS count FROM ttrss_tags
2989 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
2990
2991 $tags = array();
2992
2993 while ($line = db_fetch_assoc($result)) {
2994 $tags[$line["tag_name"]] += $line["count"];
2995 }
2996
2997 foreach (array_keys($tags) as $tag) {
2998
2999 $unread = $tags[$tag];
3000
3001 $class = "tag";
3002
3003 if ($unread > 0) {
3004 $class .= "Unread";
3005 }
3006
3007 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3008
3009 }
3010
3011 if (db_num_rows($result) == 0) {
3012 print "<li>No tags to display.</li>";
3013 }
3014
3015 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3016 print "</ul>\n";
3017 }
3018
3019 }
3020
3021 print "</ul>";
3022
3023 }
3024
3025 function get_article_tags($link, $id) {
3026
3027 $a_id = db_escape_string($id);
3028
3029 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3030 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
3031 ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."') ORDER BY tag_name");
3032
3033 $tags = array();
3034
3035 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3036 array_push($tags, $tmp_line["tag_name"]);
3037 }
3038
3039 return $tags;
3040 }
3041
3042 ?>