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