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