]> git.wh0rd.org - tt-rss.git/blob - functions.php
better fatal error handling by frontend (remove error.php)
[tt-rss.git] / functions.php
1 <?
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
14 require_once 'magpierss/rss_utils.inc';
15
16 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
17
18 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
19
20 $rows = -1;
21
22 if (DB_TYPE == "pgsql") {
23 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
24 marked = false AND feed_id = '$feed_id' AND
25 (SELECT date_entered FROM ttrss_entries WHERE
26 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
27
28 $pg_version = get_pgsql_version($link);
29
30 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
31
32 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
33 ttrss_entries.id = ref_id AND
34 marked = false AND
35 feed_id = '$feed_id' AND
36 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
37
38 } else {
39
40 $result = db_query($link, "DELETE FROM ttrss_user_entries
41 USING ttrss_entries
42 WHERE ttrss_entries.id = ref_id AND
43 marked = false AND
44 feed_id = '$feed_id' AND
45 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
46 }
47
48 $rows = pg_affected_rows($result);
49
50 } else {
51
52 /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
53 marked = false AND feed_id = '$feed_id' AND
54 (SELECT date_entered FROM ttrss_entries WHERE
55 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
56
57 $result = db_query($link, "DELETE FROM ttrss_user_entries
58 USING ttrss_user_entries, ttrss_entries
59 WHERE ttrss_entries.id = ref_id AND
60 marked = false AND
61 feed_id = '$feed_id' AND
62 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
63
64 $rows = mysql_affected_rows($link);
65
66 }
67
68 if ($debug) {
69 print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n";
70 }
71 }
72
73 function global_purge_old_posts($link, $do_output = false, $limit = false) {
74
75 $random_qpart = sql_random_function();
76
77 if ($limit) {
78 $limit_qpart = "LIMIT $limit";
79 } else {
80 $limit_qpart = "";
81 }
82
83 $result = db_query($link,
84 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
85 ORDER BY $random_qpart $limit_qpart");
86
87 while ($line = db_fetch_assoc($result)) {
88
89 $feed_id = $line["id"];
90 $purge_interval = $line["purge_interval"];
91 $owner_uid = $line["owner_uid"];
92
93 if ($purge_interval == 0) {
94
95 $tmp_result = db_query($link,
96 "SELECT value FROM ttrss_user_prefs WHERE
97 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
98
99 if (db_num_rows($tmp_result) != 0) {
100 $purge_interval = db_fetch_result($tmp_result, 0, "value");
101 }
102 }
103
104 if ($do_output) {
105 // print "Feed $feed_id: purge interval = $purge_interval\n";
106 }
107
108 if ($purge_interval > 0) {
109 purge_feed($link, $feed_id, $purge_interval, $do_output);
110 }
111 }
112
113 // purge orphaned posts in main content table
114 db_query($link, "DELETE FROM ttrss_entries WHERE
115 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
116
117 }
118
119 function purge_old_posts($link) {
120
121 $user_id = $_SESSION["uid"];
122
123 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
124 WHERE owner_uid = '$user_id'");
125
126 while ($line = db_fetch_assoc($result)) {
127
128 $feed_id = $line["id"];
129 $purge_interval = $line["purge_interval"];
130
131 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
132
133 if ($purge_interval > 0) {
134 purge_feed($link, $feed_id, $purge_interval);
135 }
136 }
137
138 // purge orphaned posts in main content table
139 db_query($link, "DELETE FROM ttrss_entries WHERE
140 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
141 }
142
143 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
144
145 if (WEB_DEMO_MODE) return;
146
147 if (!$user_id) {
148 $user_id = $_SESSION["uid"];
149 purge_old_posts($link);
150 }
151
152 // db_query($link, "BEGIN");
153
154 if (MAX_UPDATE_TIME > 0) {
155 if (DB_TYPE == "mysql") {
156 $q_order = "RAND()";
157 } else {
158 $q_order = "RANDOM()";
159 }
160 } else {
161 $q_order = "last_updated DESC";
162 }
163
164 $result = db_query($link, "SELECT feed_url,id,
165 SUBSTRING(last_updated,1,19) AS last_updated,
166 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
167 ORDER BY $q_order");
168
169 $upd_start = time();
170
171 while ($line = db_fetch_assoc($result)) {
172 $upd_intl = $line["update_interval"];
173
174 if (!$upd_intl || $upd_intl == 0) {
175 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id);
176 }
177
178 if ($upd_intl < 0) {
179 // Updates for this feed are disabled
180 continue;
181 }
182
183 if ($fetch || (!$line["last_updated"] ||
184 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
185
186 // print "<!-- feed: ".$line["feed_url"]." -->";
187
188 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
189
190 $upd_elapsed = time() - $upd_start;
191
192 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
193 return;
194 }
195 }
196 }
197
198 // db_query($link, "COMMIT");
199
200 }
201
202 function check_feed_favicon($feed_url, $feed, $link) {
203 $feed_url = str_replace("http://", "", $feed_url);
204 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
205
206 $icon_url = "http://$feed_url/favicon.ico";
207 $icon_file = ICONS_DIR . "/$feed.ico";
208
209 if (!file_exists($icon_file)) {
210
211 error_reporting(0);
212 $r = fopen($icon_url, "r");
213 error_reporting (DEFAULT_ERROR_LEVEL);
214
215 if ($r) {
216 $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon");
217
218 $t = fopen($tmpfname, "w");
219
220 while (!feof($r)) {
221 $buf = fread($r, 16384);
222 fwrite($t, $buf);
223 }
224
225 fclose($r);
226 fclose($t);
227
228 error_reporting(0);
229 if (!rename($tmpfname, $icon_file)) {
230 unlink($tmpfname);
231 }
232
233 chmod($icon_file, 0644);
234
235 error_reporting (DEFAULT_ERROR_LEVEL);
236
237 }
238 }
239 }
240
241 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
242
243 if (WEB_DEMO_MODE) return;
244
245 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
246 return;
247 }
248
249 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
250 FROM ttrss_feeds WHERE id = '$feed'");
251
252 $auth_login = db_fetch_result($result, 0, "auth_login");
253 $auth_pass = db_fetch_result($result, 0, "auth_pass");
254
255 $update_interval = db_fetch_result($result, 0, "update_interval");
256
257 if ($update_interval < 0) { return; }
258
259 $feed = db_escape_string($feed);
260
261 $fetch_url = $feed_url;
262
263 if ($auth_login && $auth_pass) {
264 $url_parts = array();
265 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
266
267 if ($url_parts[1] && $url_parts[2]) {
268 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
269 }
270
271 }
272 error_reporting(0);
273 $rss = fetch_rss($fetch_url);
274
275 error_reporting (DEFAULT_ERROR_LEVEL);
276
277 $feed = db_escape_string($feed);
278
279 if ($rss) {
280
281 // db_query($link, "BEGIN");
282
283 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
284 FROM ttrss_feeds WHERE id = '$feed'");
285
286 $registered_title = db_fetch_result($result, 0, "title");
287 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
288 $orig_site_url = db_fetch_result($result, 0, "site_url");
289
290 $owner_uid = db_fetch_result($result, 0, "owner_uid");
291
292 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) {
293 check_feed_favicon($feed_url, $feed, $link);
294 }
295
296 if (!$registered_title || $registered_title == "[Unknown]") {
297 $feed_title = db_escape_string($rss->channel["title"]);
298 db_query($link, "UPDATE ttrss_feeds SET
299 title = '$feed_title' WHERE id = '$feed'");
300 }
301
302 $site_url = $rss->channel["link"];
303 // weird, weird Magpie
304 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
305
306 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
307 db_query($link, "UPDATE ttrss_feeds SET
308 site_url = '$site_url' WHERE id = '$feed'");
309 }
310
311 // print "I: " . $rss->channel["image"]["url"];
312
313 $icon_url = $rss->image["url"];
314
315 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
316 $icon_url = db_escape_string($icon_url);
317 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
318 }
319
320
321 $filters = array();
322
323 $result = db_query($link, "SELECT reg_exp,
324 ttrss_filter_types.name AS name,
325 ttrss_filter_actions.name AS action
326 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
327 owner_uid = $owner_uid AND
328 ttrss_filter_types.id = filter_type AND
329 ttrss_filter_actions.id = action_id AND
330 (feed_id IS NULL OR feed_id = '$feed')");
331
332 while ($line = db_fetch_assoc($result)) {
333 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
334
335 $filter["reg_exp"] = $line["reg_exp"];
336 $filter["action"] = $line["action"];
337
338 array_push($filters[$line["name"]], $filter);
339 }
340
341 $iterator = $rss->items;
342
343 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
344 if (!$iterator || !is_array($iterator)) $iterator = $rss;
345
346 if (!is_array($iterator)) {
347 db_query($link, "UPDATE ttrss_feeds
348 SET last_error = 'Parse error: can\'t find any articles.'
349 WHERE id = '$feed'");
350 return; // WTF?
351 }
352
353 foreach ($iterator as $item) {
354
355 $entry_guid = $item["id"];
356
357 if (!$entry_guid) $entry_guid = $item["guid"];
358 if (!$entry_guid) $entry_guid = $item["link"];
359
360 if (!$entry_guid) continue;
361
362 $entry_timestamp = "";
363
364 $rss_2_date = $item['pubdate'];
365 $rss_1_date = $item['dc']['date'];
366 $atom_date = $item['issued'];
367 if (!$atom_date) $atom_date = $item['updated'];
368
369 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
370 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
371 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
372
373 if ($entry_timestamp == "") {
374 $entry_timestamp = time();
375 $no_orig_date = 'true';
376 } else {
377 $no_orig_date = 'false';
378 }
379
380 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
381
382 $entry_title = $item["title"];
383
384 // strange Magpie workaround
385 $entry_link = $item["link_"];
386 if (!$entry_link) $entry_link = $item["link"];
387
388 if (!$entry_title) continue;
389 if (!$entry_link) continue;
390
391 $entry_content = $item["content:escaped"];
392
393 if (!$entry_content) $entry_content = $item["content:encoded"];
394 if (!$entry_content) $entry_content = $item["content"];
395 if (!$entry_content) $entry_content = $item["summary"];
396 if (!$entry_content) $entry_content = $item["description"];
397
398 // if (!$entry_content) continue;
399
400 // WTF
401 if (is_array($entry_content)) {
402 $entry_content = $entry_content["encoded"];
403 if (!$entry_content) $entry_content = $entry_content["escaped"];
404 }
405
406 // print_r($item);
407 // print_r(htmlspecialchars($entry_content));
408 // print "<br>";
409
410 $entry_content_unescaped = $entry_content;
411 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
412
413 $entry_comments = $item["comments"];
414
415 $entry_author = db_escape_string($item['dc']['creator']);
416
417 $entry_guid = db_escape_string($entry_guid);
418
419 $result = db_query($link, "SELECT id FROM ttrss_entries
420 WHERE guid = '$entry_guid'");
421
422 $entry_content = db_escape_string($entry_content);
423 $entry_title = db_escape_string($entry_title);
424 $entry_link = db_escape_string($entry_link);
425 $entry_comments = db_escape_string($entry_comments);
426
427 $num_comments = db_escape_string($item["slash"]["comments"]);
428
429 if (!$num_comments) $num_comments = 0;
430
431 db_query($link, "BEGIN");
432
433 if (db_num_rows($result) == 0) {
434
435 // base post entry does not exist, create it
436
437 $result = db_query($link,
438 "INSERT INTO ttrss_entries
439 (title,
440 guid,
441 link,
442 updated,
443 content,
444 content_hash,
445 no_orig_date,
446 date_entered,
447 comments,
448 num_comments,
449 author)
450 VALUES
451 ('$entry_title',
452 '$entry_guid',
453 '$entry_link',
454 '$entry_timestamp_fmt',
455 '$entry_content',
456 '$content_hash',
457 $no_orig_date,
458 NOW(),
459 '$entry_comments',
460 '$num_comments',
461 '$entry_author')");
462 } else {
463 // we keep encountering the entry in feeds, so we need to
464 // update date_entered column so that we don't get horrible
465 // dupes when the entry gets purged and reinserted again e.g.
466 // in the case of SLOW SLOW OMG SLOW updating feeds
467
468 $base_entry_id = db_fetch_result($result, 0, "id");
469
470 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
471 WHERE id = '$base_entry_id'");
472 }
473
474 // now it should exist, if not - bad luck then
475
476 $result = db_query($link, "SELECT
477 id,content_hash,no_orig_date,title,
478 substring(date_entered,1,19) as date_entered,
479 substring(updated,1,19) as updated,
480 num_comments
481 FROM
482 ttrss_entries
483 WHERE guid = '$entry_guid'");
484
485 if (db_num_rows($result) == 1) {
486
487 // this will be used below in update handler
488 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
489 $orig_title = db_fetch_result($result, 0, "title");
490 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
491 $orig_date_entered = strtotime(db_fetch_result($result,
492 0, "date_entered"));
493
494 $ref_id = db_fetch_result($result, 0, "id");
495
496 // check for user post link to main table
497
498 // do we allow duplicate posts with same GUID in different feeds?
499 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) {
500 $dupcheck_qpart = "AND feed_id = '$feed'";
501 } else {
502 $dupcheck_qpart = "";
503 }
504
505 // error_reporting(0);
506
507 $filter_name = get_filter_name($entry_title, $entry_content,
508 $entry_link, $filters);
509
510 if ($filter_name == "filter") {
511 continue;
512 }
513
514 // error_reporting (DEFAULT_ERROR_LEVEL);
515
516 $result = db_query($link,
517 "SELECT ref_id FROM ttrss_user_entries WHERE
518 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
519 $dupcheck_qpart");
520
521 // okay it doesn't exist - create user entry
522 if (db_num_rows($result) == 0) {
523
524 if ($filter_name != 'catchup') {
525 $unread = 'true';
526 $last_read_qpart = 'NULL';
527 } else {
528 $unread = 'false';
529 $last_read_qpart = 'NOW()';
530 }
531
532 $result = db_query($link,
533 "INSERT INTO ttrss_user_entries
534 (ref_id, owner_uid, feed_id, unread, last_read)
535 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
536 $last_read_qpart)");
537 }
538
539 $post_needs_update = false;
540
541 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) &&
542 ($content_hash != $orig_content_hash)) {
543 $post_needs_update = true;
544 }
545
546 if ($orig_title != $entry_title) {
547 $post_needs_update = true;
548 }
549
550 if ($orig_num_comments != $num_comments) {
551 $post_needs_update = true;
552 }
553
554 // this doesn't seem to be very reliable
555 //
556 // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
557 // $post_needs_update = true;
558 // }
559
560 // if post needs update, update it and mark all user entries
561 // linking to this post as updated
562 if ($post_needs_update) {
563
564 // print "<!-- post $orig_title needs update : $post_needs_update -->";
565
566 db_query($link, "UPDATE ttrss_entries
567 SET title = '$entry_title', content = '$entry_content',
568 num_comments = '$num_comments'
569 WHERE id = '$ref_id'");
570
571 db_query($link, "UPDATE ttrss_user_entries
572 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
573
574 }
575 }
576
577 db_query($link, "COMMIT");
578
579 /* taaaags */
580 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
581
582 $entry_tags = null;
583
584 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
585 $entry_content_unescaped, $entry_tags);
586
587 // print "<br>$entry_title : $entry_content_unescaped<br>";
588 // print_r($entry_tags);
589 // print "<br>";
590
591 $entry_tags = $entry_tags[1];
592
593 if (count($entry_tags) > 0) {
594
595 db_query($link, "BEGIN");
596
597 $result = db_query($link, "SELECT id,int_id
598 FROM ttrss_entries,ttrss_user_entries
599 WHERE guid = '$entry_guid'
600 AND feed_id = '$feed' AND ref_id = id
601 AND owner_uid = '$owner_uid'");
602
603 if (db_num_rows($result) == 1) {
604
605 $entry_id = db_fetch_result($result, 0, "id");
606 $entry_int_id = db_fetch_result($result, 0, "int_id");
607
608 foreach ($entry_tags as $tag) {
609 $tag = db_escape_string(strtolower($tag));
610
611 $tag = str_replace("+", " ", $tag);
612 $tag = str_replace("technorati tag: ", "", $tag);
613
614 $result = db_query($link, "SELECT id FROM ttrss_tags
615 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
616 owner_uid = '$owner_uid' LIMIT 1");
617
618 // print db_fetch_result($result, 0, "id");
619
620 if ($result && db_num_rows($result) == 0) {
621
622 // print "tagging $entry_id as $tag<br>";
623
624 db_query($link, "INSERT INTO ttrss_tags
625 (owner_uid,tag_name,post_int_id)
626 VALUES ('$owner_uid','$tag', '$entry_int_id')");
627 }
628 }
629 }
630 db_query($link, "COMMIT");
631 }
632 }
633
634 db_query($link, "UPDATE ttrss_feeds
635 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
636
637 // db_query($link, "COMMIT");
638
639 } else {
640 $error_msg = db_escape_string(magpie_error());
641 db_query($link,
642 "UPDATE ttrss_feeds SET last_error = '$error_msg',
643 last_updated = NOW() WHERE id = '$feed'");
644 }
645
646 }
647
648 function print_select($id, $default, $values, $attributes = "") {
649 print "<select id=\"$id\" $attributes>";
650 foreach ($values as $v) {
651 if ($v == $default)
652 $sel = " selected";
653 else
654 $sel = "";
655
656 print "<option$sel>$v</option>";
657 }
658 print "</select>";
659 }
660
661 function get_filter_name($title, $content, $link, $filters) {
662
663 if ($filters["title"]) {
664 foreach ($filters["title"] as $filter) {
665 $reg_exp = $filter["reg_exp"];
666 if (preg_match("/$reg_exp/i", $title)) {
667 return $filter["action"];
668 }
669 }
670 }
671
672 if ($filters["content"]) {
673 foreach ($filters["content"] as $filter) {
674 $reg_exp = $filter["reg_exp"];
675 if (preg_match("/$reg_exp/i", $content)) {
676 return $filter["action"];
677 }
678 }
679 }
680
681 if ($filters["both"]) {
682 foreach ($filters["both"] as $filter) {
683 $reg_exp = $filter["reg_exp"];
684 if (preg_match("/$reg_exp/i", $title) ||
685 preg_match("/$reg_exp/i", $content)) {
686 return $filter["action"];
687 }
688 }
689 }
690
691 if ($filters["link"]) {
692 $reg_exp = $filter["reg_exp"];
693 foreach ($filters["link"] as $filter) {
694 $reg_exp = $filter["reg_exp"];
695 if (preg_match("/$reg_exp/i", $link)) {
696 return $filter["action"];
697 }
698 }
699 }
700
701 return false;
702 }
703
704 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
705 $rtl_content = false, $last_updated = false, $last_error = false) {
706
707 if (file_exists($icon_file) && filesize($icon_file) > 0) {
708 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
709 } else {
710 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
711 }
712
713 if ($rtl_content) {
714 $rtl_tag = "dir=\"rtl\"";
715 } else {
716 $rtl_tag = "dir=\"ltr\"";
717 }
718
719 if ($last_error) {
720 $link_title = "Error: $last_error ($last_updated)";
721 } else if ($last_updated) {
722 $link_title = "Updated: $last_updated";
723 }
724
725 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>";
726
727 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
728 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
729 print "$feed_icon";
730 }
731
732 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
733
734 if ($unread != 0) {
735 $fctr_class = "";
736 } else {
737 $fctr_class = "class=\"invisible\"";
738 }
739
740 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
741 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
742
743 print "</li>";
744
745 }
746
747 function getmicrotime() {
748 list($usec, $sec) = explode(" ",microtime());
749 return ((float)$usec + (float)$sec);
750 }
751
752 function print_radio($id, $default, $values, $attributes = "") {
753 foreach ($values as $v) {
754
755 if ($v == $default)
756 $sel = "checked";
757 else
758 $sel = "";
759
760 if ($v == "Yes") {
761 $sel .= " value=\"1\"";
762 } else {
763 $sel .= " value=\"0\"";
764 }
765
766 print "<input class=\"noborder\"
767 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
768
769 }
770 }
771
772 function initialize_user_prefs($link, $uid) {
773
774 $uid = db_escape_string($uid);
775
776 db_query($link, "BEGIN");
777
778 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
779
780 $u_result = db_query($link, "SELECT pref_name
781 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
782
783 $active_prefs = array();
784
785 while ($line = db_fetch_assoc($u_result)) {
786 array_push($active_prefs, $line["pref_name"]);
787 }
788
789 while ($line = db_fetch_assoc($result)) {
790 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
791 // print "adding " . $line["pref_name"] . "<br>";
792
793 db_query($link, "INSERT INTO ttrss_user_prefs
794 (owner_uid,pref_name,value) VALUES
795 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
796
797 }
798 }
799
800 db_query($link, "COMMIT");
801
802 }
803
804 function authenticate_user($link, $login, $password) {
805
806 $pwd_hash = 'SHA1:' . sha1($password);
807
808 $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE
809 login = '$login' AND pwd_hash = '$pwd_hash'");
810
811 if (db_num_rows($result) == 1) {
812 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
813 $_SESSION["name"] = db_fetch_result($result, 0, "login");
814 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
815
816 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
817 $_SESSION["uid"]);
818
819 $user_theme = get_user_theme_path($link);
820
821 $_SESSION["theme"] = $user_theme;
822 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
823
824 initialize_user_prefs($link, $_SESSION["uid"]);
825
826 return true;
827 }
828
829 return false;
830
831 }
832
833 function make_password($length = 8) {
834
835 $password = "";
836 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
837
838 $i = 0;
839
840 while ($i < $length) {
841 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
842
843 if (!strstr($password, $char)) {
844 $password .= $char;
845 $i++;
846 }
847 }
848 return $password;
849 }
850
851 // this is called after user is created to initialize default feeds, labels
852 // or whatever else
853
854 // user preferences are checked on every login, not here
855
856 function initialize_user($link, $uid) {
857
858 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
859 values ('$uid','unread = true', 'Unread articles')");
860
861 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
862 values ('$uid','last_read is null and unread = false', 'Updated articles')");
863
864 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
865 values ('$uid', 'Tiny Tiny RSS: New Releases',
866 'http://tt-rss.spb.ru/releases.rss')");
867
868 }
869
870 function logout_user() {
871 session_destroy();
872 if (isset($_COOKIE[session_name()])) {
873 setcookie(session_name(), '', time()-42000, '/');
874 }
875 }
876
877 function get_script_urlpath() {
878 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
879 }
880
881 function get_login_redirect() {
882 $server = $_SERVER["SERVER_NAME"];
883
884 if (ENABLE_LOGIN_SSL) {
885 $protocol = "https";
886 } else {
887 $protocol = "http";
888 }
889
890 $url_path = get_script_urlpath();
891
892 $redirect_uri = "$protocol://$server$url_path/login.php";
893
894 return $redirect_uri;
895 }
896
897 function validate_session($link) {
898 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
899 if ($_SESSION["ip_address"]) {
900 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
901 return false;
902 }
903 }
904 }
905 return true;
906 }
907
908 function basic_nosid_redirect_check() {
909 if (!SINGLE_USER_MODE) {
910 if (!$_COOKIE["ttrss_sid"]) {
911 $redirect_uri = get_login_redirect();
912 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
913 header("Location: $redirect_uri?rt=$return_to");
914 exit;
915 }
916 }
917 }
918
919 function login_sequence($link) {
920 if (!SINGLE_USER_MODE) {
921
922 if (!validate_session($link)) {
923 logout_user();
924 $redirect_uri = get_login_redirect();
925 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
926 header("Location: $redirect_uri?rt=$return_to");
927 exit;
928 }
929
930 if (!USE_HTTP_AUTH) {
931 if (!$_SESSION["uid"]) {
932 $redirect_uri = get_login_redirect();
933 $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]);
934 header("Location: $redirect_uri?rt=$return_to");
935 exit;
936 }
937 } else {
938 if (!$_SESSION["uid"]) {
939 if (!$_SERVER["PHP_AUTH_USER"]) {
940
941 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
942 header('HTTP/1.0 401 Unauthorized');
943 exit;
944
945 } else {
946 $auth_result = authenticate_user($link,
947 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
948
949 if (!$auth_result) {
950 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"');
951 header('HTTP/1.0 401 Unauthorized');
952 exit;
953 }
954 }
955 }
956 }
957 } else {
958 $_SESSION["uid"] = 1;
959 $_SESSION["name"] = "admin";
960 initialize_user_prefs($link, 1);
961 }
962 }
963
964 function truncate_string($str, $max_len) {
965 if (mb_strlen($str, "utf-8") > $max_len - 3) {
966 return mb_substr($str, 0, $max_len, "utf-8") . "...";
967 } else {
968 return $str;
969 }
970 }
971
972 function get_user_theme_path($link) {
973 $result = db_query($link, "SELECT theme_path
974 FROM
975 ttrss_themes,ttrss_users
976 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
977 if (db_num_rows($result) != 0) {
978 return db_fetch_result($result, 0, "theme_path");
979 } else {
980 return null;
981 }
982 }
983
984 function smart_date_time($timestamp) {
985 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
986 return date("G:i", $timestamp);
987 } else if (date("Y", $timestamp) == date("Y")) {
988 return date("M d, G:i", $timestamp);
989 } else {
990 return date("Y/m/d G:i", $timestamp);
991 }
992 }
993
994 function smart_date($timestamp) {
995 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
996 return "Today";
997 } else if (date("Y", $timestamp) == date("Y")) {
998 return date("D m", $timestamp);
999 } else {
1000 return date("Y/m/d", $timestamp);
1001 }
1002 }
1003
1004 function sql_bool_to_string($s) {
1005 if ($s == "t" || $s == "1") {
1006 return "true";
1007 } else {
1008 return "false";
1009 }
1010 }
1011
1012 function sql_bool_to_bool($s) {
1013 if ($s == "t" || $s == "1") {
1014 return true;
1015 } else {
1016 return false;
1017 }
1018 }
1019
1020
1021 function toggleEvenOdd($a) {
1022 if ($a == "even")
1023 return "odd";
1024 else
1025 return "even";
1026 }
1027
1028 function sanity_check($link) {
1029
1030 $error_code = 0;
1031 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1032 $schema_version = db_fetch_result($result, 0, "schema_version");
1033
1034 if ($schema_version != SCHEMA_VERSION) {
1035 $error_code = 5;
1036 }
1037
1038 if ($error_code != 0) {
1039 print "<error error-code='$error_code'/>";
1040 return false;
1041 } else {
1042 return true;
1043 }
1044 }
1045
1046 function file_is_locked($filename) {
1047 error_reporting(0);
1048 $fp = fopen($filename, "r");
1049 error_reporting(DEFAULT_ERROR_LEVEL);
1050 if ($fp) {
1051 if (flock($fp, LOCK_EX | LOCK_NB)) {
1052 flock($fp, LOCK_UN);
1053 fclose($fp);
1054 return false;
1055 }
1056 fclose($fp);
1057 return true;
1058 }
1059 return false;
1060 }
1061
1062 function make_lockfile($filename) {
1063 $fp = fopen($filename, "w");
1064
1065 if (flock($fp, LOCK_EX | LOCK_NB)) {
1066 return $fp;
1067 } else {
1068 return false;
1069 }
1070 }
1071
1072 function sql_random_function() {
1073 if (DB_TYPE == "mysql") {
1074 return "RAND()";
1075 } else {
1076 return "RANDOM()";
1077 }
1078 }
1079
1080 function catchup_feed($link, $feed, $cat_view) {
1081 if (preg_match("/^[0-9][0-9]*$/", $feed) != false && $feed >= 0) {
1082
1083 if ($cat_view) {
1084
1085 if ($feed > 0) {
1086 $cat_qpart = "cat_id = '$feed'";
1087 } else {
1088 $cat_qpart = "cat_id IS NULL";
1089 }
1090
1091 $tmp_result = db_query($link, "SELECT id
1092 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1093 $_SESSION["uid"]);
1094
1095 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1096
1097 $tmp_feed = $tmp_line["id"];
1098
1099 db_query($link, "UPDATE ttrss_user_entries
1100 SET unread = false,last_read = NOW()
1101 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1102 }
1103
1104 } else if ($feed > 0) {
1105
1106 $tmp_result = db_query($link, "SELECT id
1107 FROM ttrss_feeds WHERE parent_feed = '$feed'
1108 ORDER BY cat_id,title");
1109
1110 $parent_ids = array();
1111
1112 if (db_num_rows($tmp_result) > 0) {
1113 while ($p = db_fetch_assoc($tmp_result)) {
1114 array_push($parent_ids, "feed_id = " . $p["id"]);
1115 }
1116
1117 $children_qpart = implode(" OR ", $parent_ids);
1118
1119 db_query($link, "UPDATE ttrss_user_entries
1120 SET unread = false,last_read = NOW()
1121 WHERE (feed_id = '$feed' OR $children_qpart)
1122 AND owner_uid = " . $_SESSION["uid"]);
1123
1124 } else {
1125 db_query($link, "UPDATE ttrss_user_entries
1126 SET unread = false,last_read = NOW()
1127 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1128 }
1129
1130 } else if ($feed < 0 && $feed > -10) { // special, like starred
1131
1132 if ($feed == -1) {
1133 db_query($link, "UPDATE ttrss_user_entries
1134 SET unread = false,last_read = NOW()
1135 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1136 }
1137
1138 } else if ($feed < -10) { // label
1139
1140 // TODO make this more efficient
1141
1142 $label_id = -$feed - 11;
1143
1144 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1145 WHERE id = '$label_id'");
1146
1147 if ($tmp_result) {
1148 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1149
1150 db_query($link, "BEGIN");
1151
1152 $tmp2_result = db_query($link,
1153 "SELECT
1154 int_id
1155 FROM
1156 ttrss_user_entries,ttrss_entries
1157 WHERE
1158 ref_id = id AND
1159 $sql_exp AND
1160 owner_uid = " . $_SESSION["uid"]);
1161
1162 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1163 db_query($link, "UPDATE
1164 ttrss_user_entries
1165 SET
1166 unread = false, last_read = NOW()
1167 WHERE
1168 int_id = " . $tmp_line["int_id"]);
1169 }
1170
1171 db_query($link, "COMMIT");
1172
1173 /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1174 SET unread = false,last_read = NOW()
1175 WHERE $sql_exp
1176 AND ref_id = id
1177 AND owner_uid = ".$_SESSION["uid"]); */
1178 }
1179 }
1180 } else { // tag
1181 db_query($link, "BEGIN");
1182
1183 $tag_name = db_escape_string($feed);
1184
1185 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1186 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1187
1188 while ($line = db_fetch_assoc($result)) {
1189 db_query($link, "UPDATE ttrss_user_entries SET
1190 unread = false, last_read = NOW()
1191 WHERE int_id = " . $line["post_int_id"]);
1192 }
1193 db_query($link, "COMMIT");
1194 }
1195 }
1196
1197 function update_generic_feed($link, $feed, $cat_view) {
1198 if ($cat_view) {
1199
1200 if ($feed > 0) {
1201 $cat_qpart = "cat_id = '$feed'";
1202 } else {
1203 $cat_qpart = "cat_id IS NULL";
1204 }
1205
1206 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1207 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1208
1209 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1210 $feed_url = $tmp_line["feed_url"];
1211 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1212 }
1213
1214 } else {
1215 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1216 WHERE id = '$feed'");
1217 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1218 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1219 }
1220 }
1221
1222 function getAllCounters($link) {
1223 getLabelCounters($link);
1224 getFeedCounters($link);
1225 getTagCounters($link);
1226 getGlobalCounters($link);
1227 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1228 getCategoryCounters($link);
1229 }
1230 }
1231
1232 function getCategoryCounters($link) {
1233 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1234 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1235 AND unread = true)) AS unread FROM ttrss_feeds
1236 WHERE
1237 owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
1238
1239 while ($line = db_fetch_assoc($result)) {
1240 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1241 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1242 $line["unread"]."\"/>";
1243 }
1244 }
1245
1246 function getFeedUnread($link, $feed) {
1247 $n_feed = sprintf("%d", $feed);
1248
1249 if ($n_feed == -1) {
1250 $match_part = "marked = true";
1251 } else if ($feed > 0) {
1252 $match_part = "feed_id = '$n_feed'";
1253 } else if ($feed < -10) {
1254 $label_id = -$feed - 11;
1255
1256 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1257 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1258
1259 $match_part = db_fetch_result($result, 0, "sql_exp");
1260 }
1261
1262 if ($match_part) {
1263
1264 $result = db_query($link, "SELECT count(int_id) AS unread
1265 FROM ttrss_user_entries
1266 WHERE unread = true AND $match_part AND owner_uid = " . $_SESSION["uid"]);
1267
1268 } else {
1269
1270 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1271 FROM ttrss_tags,ttrss_user_entries
1272 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1273 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1274 }
1275
1276 $unread = db_fetch_result($result, 0, "unread");
1277 return $unread;
1278 }
1279
1280 /* FIXME this needs reworking */
1281
1282 function getGlobalUnread($link) {
1283 $result = db_query($link, "SELECT count(id) as c_id FROM ttrss_entries,ttrss_user_entries
1284 WHERE unread = true AND
1285 ttrss_user_entries.ref_id = ttrss_entries.id AND
1286 owner_uid = " . $_SESSION["uid"]);
1287 $c_id = db_fetch_result($result, 0, "c_id");
1288 return $c_id;
1289 }
1290
1291 function getGlobalCounters($link, $global_unread = -1) {
1292 if ($global_unread == -1) {
1293 $global_unread = getGlobalUnread($link);
1294 }
1295 print "<counter type=\"global\" id='global-unread' counter='$global_unread'/>";
1296 }
1297
1298 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1299
1300 if ($smart_mode) {
1301 if (!$_SESSION["tctr_last_value"]) {
1302 $_SESSION["tctr_last_value"] = array();
1303 }
1304 }
1305
1306 $old_counters = $_SESSION["tctr_last_value"];
1307
1308 $tctrs_modified = false;
1309
1310 /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1311 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1312 ttrss_user_entries.ref_id = ttrss_entries.id AND
1313 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1314 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1315 UNION
1316 select tag_name,0 as count FROM ttrss_tags
1317 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1318
1319 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1320 FROM ttrss_user_entries WHERE int_id = post_int_id
1321 AND unread = true)) AS count FROM ttrss_tags
1322 WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name");
1323
1324 $tags = array();
1325
1326 while ($line = db_fetch_assoc($result)) {
1327 $tags[$line["tag_name"]] += $line["count"];
1328 }
1329
1330 foreach (array_keys($tags) as $tag) {
1331 $unread = $tags[$tag];
1332
1333 $tag = htmlspecialchars($tag);
1334
1335 if (!$smart_mode || $old_counters[$tag] != $unread) {
1336 $old_counters[$tag] = $unread;
1337 $tctrs_modified = true;
1338 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1339 }
1340
1341 }
1342
1343 if ($smart_mode && $tctrs_modified) {
1344 $_SESSION["tctr_last_value"] = $old_counters;
1345 }
1346
1347 }
1348
1349 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1350
1351 if ($smart_mode) {
1352 if (!$_SESSION["lctr_last_value"]) {
1353 $_SESSION["lctr_last_value"] = array();
1354 }
1355 }
1356
1357 $old_counters = $_SESSION["lctr_last_value"];
1358 $lctrs_modified = false;
1359
1360 $result = db_query($link, "SELECT count(id) as count FROM ttrss_entries,ttrss_user_entries
1361 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
1362 unread = true AND owner_uid = ".$_SESSION["uid"]);
1363
1364 $count = db_fetch_result($result, 0, "count");
1365
1366 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1367
1368 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1369 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1370
1371 while ($line = db_fetch_assoc($result)) {
1372
1373 $id = -$line["id"] - 11;
1374
1375 error_reporting (0);
1376
1377 $tmp_result = db_query($link, "SELECT count(id) as count FROM ttrss_user_entries,ttrss_entries
1378 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
1379 ttrss_user_entries.ref_id = ttrss_entries.id AND
1380 owner_uid = ".$_SESSION["uid"]);
1381
1382 $count = db_fetch_result($tmp_result, 0, "count");
1383
1384 if (!$smart_mode || $old_counters[$id] != $count) {
1385 $old_counters[$id] = $count;
1386 $lctrs_modified = true;
1387 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1388 }
1389
1390 error_reporting (DEFAULT_ERROR_LEVEL);
1391 }
1392
1393 if ($smart_mode && $lctrs_modified) {
1394 $_SESSION["lctr_last_value"] = $old_counters;
1395 }
1396 }
1397
1398 /* function getFeedCounter($link, $id) {
1399
1400 $result = db_query($link, "SELECT
1401 count(id) as count,last_error
1402 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1403 WHERE feed_id = '$id' AND unread = true
1404 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1405 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1406
1407 $count = db_fetch_result($result, 0, "count");
1408 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1409
1410 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1411 } */
1412
1413 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1414
1415 if ($smart_mode) {
1416 if (!$_SESSION["fctr_last_value"]) {
1417 $_SESSION["fctr_last_value"] = array();
1418 }
1419 }
1420
1421 $old_counters = $_SESSION["fctr_last_value"];
1422
1423 $result = db_query($link, "SELECT id,last_error,parent_feed,
1424 SUBSTRING(last_updated,1,19) AS last_updated,
1425 (SELECT count(id)
1426 FROM ttrss_entries,ttrss_user_entries
1427 WHERE feed_id = ttrss_feeds.id AND
1428 ttrss_user_entries.ref_id = ttrss_entries.id
1429 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1430 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1431 AND parent_feed IS NULL");
1432
1433 $fctrs_modified = false;
1434
1435 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1436
1437 while ($line = db_fetch_assoc($result)) {
1438
1439 $id = $line["id"];
1440 $count = $line["count"];
1441 $last_error = htmlspecialchars($line["last_error"]);
1442
1443 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1444 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1445 } else {
1446 $last_updated = date($short_date, strtotime($line["last_updated"]));
1447 }
1448
1449 $has_img = is_file(ICONS_DIR . "/$id.ico");
1450
1451 $tmp_result = db_query($link,
1452 "SELECT id,COUNT(unread) AS unread
1453 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1454 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1455 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1456
1457 if (db_num_rows($tmp_result) > 0) {
1458 while ($l = db_fetch_assoc($tmp_result)) {
1459 $count += $l["unread"];
1460 }
1461 }
1462
1463 if (!$smart_mode || $old_counters[$id] != $count) {
1464 $old_counters[$id] = $count;
1465 $fctrs_modified = true;
1466
1467 if ($last_error) {
1468 $error_part = "error=\"$last_error\"";
1469 } else {
1470 $error_part = "";
1471 }
1472
1473 if ($has_img) {
1474 $has_img_part = "hi=\"$has_img\"";
1475 } else {
1476 $has_img_part = "";
1477 }
1478
1479 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
1480 }
1481 }
1482
1483 if ($smart_mode && $fctrs_modified) {
1484 $_SESSION["fctr_last_value"] = $old_counters;
1485 }
1486 }
1487
1488 function get_script_dt_add() {
1489 if (strpos(VERSION, "99") === false) {
1490 return VERSION;
1491 } else {
1492 return time();
1493 }
1494 }
1495
1496 function get_pgsql_version($link) {
1497 $result = db_query($link, "SELECT version() AS version");
1498 $version = split(" ", db_fetch_result($result, 0, "version"));
1499 return $version[1];
1500 }
1501
1502 function print_error_xml($code, $add_msg = "") {
1503 global $ERRORS;
1504
1505 $error_msg = $ERRORS[$code];
1506
1507 if ($add_msg) {
1508 $error_msg = "$error_msg; $add_msg";
1509 }
1510
1511 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
1512 }
1513 ?>