2 require_once "colors.php";
4 class Feeds
extends Handler_Protected
{
8 function csrf_ignore($method) {
9 $csrf_ignored = array("index", "feedbrowser", "quickaddfeed", "search");
11 return array_search($method, $csrf_ignored) !== false;
14 private function format_headline_subtoolbar($feed_site_url, $feed_title,
15 $feed_id, $is_cat, $search,
16 $error, $feed_last_updated) {
18 $catchup_sel_link = "catchupSelection()";
20 $archive_sel_link = "archiveSelection()";
21 $delete_sel_link = "deleteSelection()";
23 $sel_all_link = "selectArticles('all')";
24 $sel_unread_link = "selectArticles('unread')";
25 $sel_none_link = "selectArticles('none')";
26 $sel_inv_link = "selectArticles('invert')";
28 $tog_unread_link = "selectionToggleUnread()";
29 $tog_marked_link = "selectionToggleMarked()";
30 $tog_published_link = "selectionTogglePublished()";
32 $set_score_link = "setSelectionScore()";
34 if ($is_cat) $cat_q = "&is_cat=$is_cat";
37 $search_q = "&q=$search";
42 $reply = "<span class=\"holder\">";
44 $rss_link = htmlspecialchars(get_self_url_prefix() .
45 "/public.php?op=rss&id=$feed_id$cat_q$search_q");
49 $error_class = $error ?
"error" : "";
51 $reply .= "<span class='r'>
53 title=\"".__("View as RSS feed")."\"
54 onclick=\"displayDlg('".__("View as RSS")."','generatedFeed', '$feed_id:$is_cat:$rss_link')\">
55 <img class=\"noborder\" src=\"images/pub_set.png\"></a>";
59 $reply .= "<span id='feed_title' class='$error_class'>";
62 $last_updated = T_sprintf("Last updated: %s",
65 $target = "target=\"_blank\"";
66 $reply .= "<a title=\"$last_updated\" $target href=\"$feed_site_url\">".
67 truncate_string(strip_tags($feed_title), 30)."</a>";
70 $error = htmlspecialchars($error);
71 $reply .= " <img title=\"$error\" src='images/error.png' alt='error' class=\"noborder\">";
75 $reply .= strip_tags($feed_title);
82 # $reply .= "</span>";
86 $reply .= "<span class=\"main\">";
87 $reply .= "<span id='selected_prompt'></span>";
89 /*$reply .= "<span class=\"sel_links\">
90 <a href=\"#\" onclick=\"$sel_all_link\">".__('All')."</a>,
91 <a href=\"#\" onclick=\"$sel_unread_link\">".__('Unread')."</a>,
92 <a href=\"#\" onclick=\"$sel_inv_link\">".__('Invert')."</a>,
93 <a href=\"#\" onclick=\"$sel_none_link\">".__('None')."</a></li>";
95 $reply .= "</span> "; */
97 $reply .= "<select dojoType=\"dijit.form.Select\"
98 onchange=\"headlineActionsChange(this)\">";
100 $reply .= "<option value=\"0\" disabled='1'>".__('Select...')."</option>";
102 $reply .= "<option value=\"$sel_all_link\">".__('All')."</option>";
103 $reply .= "<option value=\"$sel_unread_link\">".__('Unread')."</option>";
104 $reply .= "<option value=\"$sel_inv_link\">".__('Invert')."</option>";
105 $reply .= "<option value=\"$sel_none_link\">".__('None')."</option>";
107 $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection toggle:')."</option>";
109 $reply .= "<option value=\"$tog_unread_link\">".__('Unread')."</option>
110 <option value=\"$tog_marked_link\">".__('Starred')."</option>
111 <option value=\"$tog_published_link\">".__('Published')."</option>";
113 $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection:')."</option>";
115 $reply .= "<option value=\"$catchup_sel_link\">".__('Mark as read')."</option>";
116 $reply .= "<option value=\"$set_score_link\">".__('Set score')."</option>";
118 if ($feed_id != "0") {
119 $reply .= "<option value=\"$archive_sel_link\">".__('Archive')."</option>";
121 $reply .= "<option value=\"$archive_sel_link\">".__('Move back')."</option>";
122 $reply .= "<option value=\"$delete_sel_link\">".__('Delete')."</option>";
126 if (PluginHost
::getInstance()->get_plugin("mail")) {
127 $reply .= "<option value=\"emailArticle(false)\">".__('Forward by email').
131 if (PluginHost
::getInstance()->get_plugin("mailto")) {
132 $reply .= "<option value=\"mailtoArticle(false)\">".__('Forward by email').
136 $reply .= "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
138 //$reply .= "<option value=\"catchupPage()\">".__('Mark as read')."</option>";
140 $reply .= "<option value=\"displayDlg('".__("View as RSS")."','generatedFeed', '$feed_id:$is_cat:$rss_link')\">".__('View as RSS')."</option>";
142 $reply .= "</select>";
146 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_HEADLINE_TOOLBAR_BUTTON
) as $p) {
147 $reply .= $p->hook_headline_toolbar_button($feed_id, $is_cat);
150 $reply .= "</span></span>";
155 private function format_headlines_list($feed, $method, $view_mode, $limit, $cat_view,
156 $offset, $vgr_last_feed = false,
157 $override_order = false, $include_children = false, $check_first_id = false,
158 $skip_first_id_check = false) {
160 $disable_cache = false;
164 $rgba_cache = array();
166 $timing_info = microtime(true);
168 $topmost_article_ids = array();
170 if (!$offset) $offset = 0;
171 if ($method == "undefined") $method = "";
173 $method_split = explode(":", $method);
175 if ($method == "ForceUpdate" && $feed > 0 && is_numeric($feed)) {
176 $sth = $this->pdo
->prepare("UPDATE ttrss_feeds
177 SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
179 $sth->execute([$feed]);
182 if ($method_split[0] == "MarkAllReadGR") {
183 $this->catchup_feed($method_split[1], false);
186 // FIXME: might break tag display?
188 if (is_numeric($feed) && $feed > 0 && !$cat_view) {
189 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE id = ? LIMIT 1");
190 $sth->execute([$feed]);
192 if (!$sth->fetch()) {
193 $reply['content'] = "<div align='center'>".__('Feed not found.')."</div>";
197 @$search = $_REQUEST["query"];
198 @$search_language = $_REQUEST["search_language"]; // PGSQL only
201 $disable_cache = true;
204 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
206 if (!$cat_view && is_numeric($feed) && $feed < PLUGIN_FEED_BASE_INDEX
&& $feed > LABEL_BASE_INDEX
) {
207 $handler = PluginHost
::getInstance()->get_feed_handler(
208 PluginHost
::feed_to_pfeed_id($feed));
213 "view_mode" => $view_mode,
214 "cat_view" => $cat_view,
216 "override_order" => $override_order,
218 "owner_uid" => $_SESSION["uid"],
221 "include_children" => $include_children);
223 $qfh_ret = $handler->get_headlines(PluginHost
::feed_to_pfeed_id($feed),
232 "view_mode" => $view_mode,
233 "cat_view" => $cat_view,
235 "search_language" => $search_language,
236 "override_order" => $override_order,
238 "include_children" => $include_children,
239 "check_first_id" => $check_first_id,
240 "skip_first_id_check" => $skip_first_id_check
243 $qfh_ret = $this->queryFeedHeadlines($params);
246 $vfeed_group_enabled = get_pref("VFEED_GROUP_BY_FEED") && $feed != -6;
248 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
250 $result = $qfh_ret[0]; // this could be either a PDO query result or a -1 if first id changed
251 $feed_title = $qfh_ret[1];
252 $feed_site_url = $qfh_ret[2];
253 $last_error = $qfh_ret[3];
254 $last_updated = strpos($qfh_ret[4], '1970-') === FALSE ?
255 make_local_datetime($qfh_ret[4], false) : __("Never");
256 $highlight_words = $qfh_ret[5];
257 $reply['first_id'] = $qfh_ret[6];
258 $reply['search_query'] = [$search, $search_language];
260 $vgroup_last_feed = $vgr_last_feed;
262 $reply['toolbar'] = $this->format_headline_subtoolbar($feed_site_url,
264 $feed, $cat_view, $search,
265 $last_error, $last_updated);
268 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_HEADLINES_BEFORE
) as $p) {
269 $reply['content'] .= $p->hook_headlines_before($feed, $cat_view, $qfh_ret);
273 $reply['content'] = '';
275 $headlines_count = 0;
279 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PS", $timing_info);
281 if (is_object($result)) {
283 while ($line = $result->fetch()) {
287 $line["content_preview"] = "— " . truncate_string(strip_tags($line["content"]), 250);
289 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_QUERY_HEADLINES
) as $p) {
290 $line = $p->hook_query_headlines($line, 250, false);
293 if (get_pref('SHOW_CONTENT_PREVIEW')) {
294 $content_preview = $line["content_preview"];
298 $feed_id = $line["feed_id"];
299 $label_cache = $line["label_cache"];
302 $mouseover_attrs = "onmouseover='postMouseIn(event, $id)' onmouseout='postMouseOut($id)'";
305 $label_cache = json_decode($label_cache, true);
308 if ($label_cache["no-labels"] == 1)
311 $labels = $label_cache;
315 if (!is_array($labels)) $labels = Article
::get_article_labels($id);
317 $labels_str = "<span class=\"HLLCTR-$id\">";
318 $labels_str .= Article
::format_article_labels($labels);
319 $labels_str .= "</span>";
321 if (count($topmost_article_ids) < 3) {
322 array_push($topmost_article_ids, $id);
327 if ($line["unread"]) {
332 $marked_pic_src = $line["marked"] ?
"mark_set.png" : "mark_unset.png";
333 $class .= $line["marked"] ?
" marked" : "";
334 $marked_pic = "<img src=\"images/$marked_pic_src\" class=\"markedPic\" onclick='toggleMark($id)'>";
336 $published_pic_src = $line["published"] ?
"pub_set.png" : "pub_unset.png";
337 $class .= $line["published"] ?
" published" : "";
338 $published_pic = "<img src=\"images/$published_pic_src\" class=\"pubPic\" onclick='togglePub($id)'>";
340 $updated_fmt = make_local_datetime($line["updated"], false, false, false, true);
341 $date_entered_fmt = T_sprintf("Imported at %s",
342 make_local_datetime($line["date_entered"], false));
344 $score = $line["score"];
346 $score_pic = "images/" . get_score_pic($score);
348 $score_pic = "<img class='score-pic' score='$score' onclick='changeScore($id, this)' src=\"$score_pic\"
352 $hlc_suffix = "high";
353 } else if ($score < -100) {
359 $entry_author = $line["author"];
362 $entry_author = " — $entry_author";
365 if (feeds
::feedHasIcon($feed_id)) {
366 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL
."/$feed_id.ico\" alt=\"\">";
368 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/pub_set.png\" alt=\"\">";
371 $entry_site_url = $line["site_url"];
373 //setting feed headline background color, needs to change text color based on dark/light
374 $fav_color = $line['favicon_avg_color'];
376 require_once "colors.php";
378 if ($fav_color && $fav_color != 'fail') {
379 if (!isset($rgba_cache[$feed_id])) {
380 $rgba_cache[$feed_id] = join(",", _color_unpack($fav_color));
384 if (!get_pref('COMBINED_DISPLAY_MODE')) {
386 if ($vfeed_group_enabled) {
387 if ($feed_id != $vgroup_last_feed && $line["feed_title"]) {
389 $vgroup_last_feed = $feed_id;
391 $vf_catchup_link = "<a class='catchup' onclick='catchupFeedInGroup($feed_id);' href='#'>".__('mark feed as read')."</a>";
393 $reply['content'] .= "<div data-feed-id='$feed_id' class='feed-titl'>".
394 "<div style='float : right'>$feed_icon_img</div>".
395 "<a class='title' href=\"#\" onclick=\"viewfeed({feed:$feed_id})\">".
396 $line["feed_title"]."</a>
397 $vf_catchup_link</div>";
403 $reply['content'] .= "<div class='hl hlMenuAttach $class' data-orig-feed-id='$feed_id' data-article-id='$id' id='RROW-$id' $mouseover_attrs>";
405 $reply['content'] .= "<div class='left'>";
407 $reply['content'] .= "<input dojoType=\"dijit.form.CheckBox\"
408 type=\"checkbox\" onclick=\"toggleSelectRow2(this)\"
411 $reply['content'] .= "$marked_pic";
412 $reply['content'] .= "$published_pic";
414 $reply['content'] .= "</div>";
416 $reply['content'] .= "<div onclick='return hlClicked(event, $id)'
417 class=\"title\"><span class='hl-content $hlc_suffix'>";
418 $reply['content'] .= "<a class=\"title $hlc_suffix\"
419 href=\"" . htmlspecialchars($line["link"]) . "\"
421 truncate_string($line["title"], 200);
423 if (get_pref('SHOW_CONTENT_PREVIEW')) {
424 $reply['content'] .= "<span class=\"preview\">" . $line["content_preview"] . "</span>";
427 $reply['content'] .= "</a></span>";
429 $reply['content'] .= $labels_str;
431 $reply['content'] .= "</div>";
433 if (!$vfeed_group_enabled) {
434 if (@$line["feed_title"]) {
435 $rgba = @$rgba_cache[$feed_id];
437 $reply['content'] .= "<span class=\"feed\"><a style=\"background : rgba($rgba, 0.3)\" href=\"#\" onclick=\"viewfeed({feed:$feed_id})\">".
438 truncate_string($line["feed_title"],30)."</a></span>";
443 $reply['content'] .= "<span class=\"updated\">";
445 $reply['content'] .= "<div title='$date_entered_fmt'>$updated_fmt</div>
448 $reply['content'] .= "<div class=\"right\">";
450 $reply['content'] .= $score_pic;
452 if ($line["feed_title"] && !$vfeed_group_enabled) {
454 $reply['content'] .= "<span onclick=\"viewfeed({feed:$feed_id})\"
455 style=\"cursor : pointer\"
456 title=\"".htmlspecialchars($line['feed_title'])."\">
457 $feed_icon_img</span>";
460 $reply['content'] .= "</div>";
461 $reply['content'] .= "</div>";
465 if ($line["tag_cache"])
466 $tags = explode(",", $line["tag_cache"]);
470 $line["content"] = sanitize($line["content"],
471 $line['hide_images'], false, $entry_site_url, $highlight_words, $line["id"]);
473 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_RENDER_ARTICLE_CDM
) as $p) {
474 $line = $p->hook_render_article_cdm($line);
477 $line['content'] = rewrite_cached_urls($line['content']);
479 if ($vfeed_group_enabled && $line["feed_title"]) {
480 if ($feed_id != $vgroup_last_feed) {
482 $vgroup_last_feed = $feed_id;
484 $vf_catchup_link = "<a class='catchup' onclick='catchupFeedInGroup($feed_id);' href='#'>".__('mark feed as read')."</a>";
486 $feed_icon_src = Feeds
::getFeedIcon($feed_id);
487 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"$feed_icon_src\">";
489 $reply['content'] .= "<div data-feed-id='$feed_id' class='feed-title'>".
490 "<div style=\"float : right\">$feed_icon_img</div>".
491 "<a href=\"#\" class='title' onclick=\"viewfeed({feed:$feed_id})\">".
492 $line["feed_title"]."</a> $vf_catchup_link</div>";
497 $content_encoded = htmlspecialchars($line["content"]);
499 $expanded_class = get_pref("CDM_EXPANDED") ?
"expanded" : "expandable";
500 $tmp_content = "<div class=\"cdm $expanded_class $hlc_suffix $class\"
501 id=\"RROW-$id\" data-content=\"$content_encoded\" data-article-id='$id' data-orig-feed-id='$feed_id' $mouseover_attrs>";
503 $tmp_content .= "<div class=\"header\">";
504 $tmp_content .= "<div style=\"vertical-align : middle\">";
506 $tmp_content .= "<input dojoType=\"dijit.form.CheckBox\"
507 type=\"checkbox\" onclick=\"toggleSelectRow2(this, false, true)\"
510 $tmp_content .= "$marked_pic";
511 $tmp_content .= "$published_pic";
513 $tmp_content .= "</div>";
515 if ($highlight_words && count($highlight_words) > 0) {
516 foreach ($highlight_words as $word) {
517 $word = preg_quote($word, "/");
519 $line["title"] = preg_replace("/($word)/i",
520 "<span class=\"highlight\">$1</span>", $line["title"]);
524 // data-article-id included for context menu
525 $tmp_content .= "<span
526 onclick=\"return cdmClicked(event, $id);\"
527 data-article-id=\"$id\"
528 class=\"titleWrap hlMenuAttach $hlc_suffix\">
529 <a class=\"title $hlc_suffix\"
530 title=\"".htmlspecialchars($line["title"])."\"
531 target=\"_blank\" rel=\"noopener noreferrer\" href=\"".
532 htmlspecialchars($line["link"])."\">".
534 "</a> <span class=\"author\">$entry_author</span>";
536 $tmp_content .= $labels_str;
538 $tmp_content .= "<span class='collapse'>
539 <img src=\"images/collapse.png\" onclick=\"return cdmCollapseActive(event)\"
540 title=\"".__("Collapse article")."\"/></span>";
542 $tmp_content .= "</span>";
544 if (!$vfeed_group_enabled) {
545 if (@$line["feed_title"]) {
546 $rgba = @$rgba_cache[$feed_id];
548 $tmp_content .= "<div class=\"feed\">
549 <a href=\"#\" style=\"background-color: rgba($rgba,0.3)\"
550 onclick=\"viewfeed({feed:$feed_id})\">".
551 truncate_string($line["feed_title"],30)."</a>
556 $tmp_content .= "<span class='updated' title='$date_entered_fmt'>$updated_fmt</span>";
558 $tmp_content .= "<div style=\"vertical-align : middle\">";
559 $tmp_content .= "$score_pic";
561 if (!get_pref("VFEED_GROUP_BY_FEED") && $line["feed_title"]) {
562 $tmp_content .= "<span style=\"cursor : pointer\"
563 title=\"".htmlspecialchars($line["feed_title"])."\"
564 onclick=\"viewfeed({feed:$feed_id})\">$feed_icon_img</span>";
566 $tmp_content .= "</div>"; //score wrapper2
568 $tmp_content .= "</div>"; //header
570 $tmp_content .= "<div class=\"content\" onclick=\"return cdmClicked(event, $id, true);\">";
572 $tmp_content .= "<div id=\"POSTNOTE-$id\">";
574 $tmp_content .= Article
::format_article_note($id, $line['note']);
576 $tmp_content .= "</div>"; //POSTNOTE
578 if (!$line['lang']) $line['lang'] = 'en';
580 // this is filled from RROW data-content
581 $tmp_content .= "<div class=\"content-inner\" lang=\"".$line['lang']."\">";
583 if ($line["orig_feed_id"]) {
585 $ofgh = $this->pdo
->prepare("SELECT * FROM ttrss_archived_feeds
586 WHERE id = ? AND owner_uid = ?");
587 $ofgh->execute([$line["orig_feed_id"], $_SESSION['uid']]);
589 if ($tmp_line = $ofgh->fetch()) {
591 $tmp_content .= "<div clear='both'>";
592 $tmp_content .= __("Originally from:");
594 $tmp_content .= " ";
596 $tmp_content .= "<a target='_blank' rel='noopener noreferrer'
597 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
598 $tmp_line['title'] . "</a>";
600 $tmp_content .= " ";
602 $tmp_content .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
603 $tmp_content .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_unset.png'></a>";
605 $tmp_content .= "</div>";
609 $tmp_content .= "</div>"; //content-inner
610 $tmp_content .= "<div class=\"intermediate\">";
612 $always_display_enclosures = $line["always_display_enclosures"];
613 $tmp_content .= Article
::format_article_enclosures($id, $always_display_enclosures,
614 $line["content"], $line["hide_images"]);
616 $tmp_content .= "</div>"; // cdmIntermediate
618 $tmp_content .= "<div class=\"footer\" onclick=\"event.stopPropagation()\">";
620 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_ARTICLE_LEFT_BUTTON
) as $p) {
621 $tmp_content .= $p->hook_article_left_button($line);
624 $tags_str = Article
::format_tags_string($tags, $id);
626 $tmp_content .= "<span class='left'>";
628 $tmp_content .= "<img src='images/tag.png' alt='Tags' title='Tags'>
629 <span id=\"ATSTR-$id\">$tags_str</span>
630 <a title=\"".__('Edit tags for this article')."\"
631 href=\"#\" onclick=\"editArticleTags($id)\">(+)</a>";
633 $num_comments = (int) $line["num_comments"];
634 $entry_comments = "";
636 if ($num_comments > 0) {
637 if ($line["comments"]) {
638 $comments_url = htmlspecialchars($line["comments"]);
640 $comments_url = htmlspecialchars($line["link"]);
642 $entry_comments = "<a class=\"comments\"
643 target='_blank' rel='noopener noreferrer' href=\"$comments_url\">$num_comments ".
644 _ngettext("comment", "comments", $num_comments)."</a>";
647 if ($line["comments"] && $line["link"] != $line["comments"]) {
648 $entry_comments = "<a class=\"comments\" target='_blank' rel='noopener noreferrer' href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
652 if ($entry_comments) $tmp_content .= " ($entry_comments)";
654 $tmp_content .= "</span>";
655 $tmp_content .= "<div>";
657 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_ARTICLE_BUTTON
) as $p) {
658 $tmp_content .= $p->hook_article_button($line);
661 $tmp_content .= "</div>"; // buttons
663 $tmp_content .= "</div>"; // cdm footer
664 $tmp_content .= "</div>"; // cdmContent
665 $tmp_content .= "</div>"; // RROW.cdm
667 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_FORMAT_ARTICLE_CDM
) as $p) {
668 $tmp_content = $p->hook_format_article_cdm($tmp_content, $line);
671 $reply['content'] .= $tmp_content;
678 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PE", $timing_info);
680 if (!$headlines_count) {
682 if (!is_numeric($result)) {
684 switch ($view_mode) {
686 $message = __("No unread articles found to display.");
689 $message = __("No updated articles found to display.");
692 $message = __("No starred articles found to display.");
695 if ($feed < LABEL_BASE_INDEX
) {
696 $message = __("No articles found to display. You can assign articles to labels manually from article header context menu (applies to all selected articles) or use a filter.");
698 $message = __("No articles found to display.");
702 if (!$offset && $message) {
703 $reply['content'] = "<div class='whiteBox'>$message";
705 $reply['content'] .= "<p><span class=\"insensitive\">";
707 $sth = $this->pdo
->prepare("SELECT " . SUBSTRING_FOR_DATE
. "(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
708 WHERE owner_uid = ?");
709 $sth->execute([$_SESSION['uid']]);
710 $row = $sth->fetch();
712 $last_updated = make_local_datetime($row["last_updated"], false);
714 $reply['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
716 $sth = $this->pdo
->prepare("SELECT COUNT(id) AS num_errors
717 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ?");
718 $sth->execute([$_SESSION['uid']]);
719 $row = $sth->fetch();
721 $num_errors = $row["num_errors"];
723 if ($num_errors > 0) {
724 $reply['content'] .= "<br/>";
725 $reply['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">" .
726 __('Some feeds have update errors (click for details)') . "</a>";
728 $reply['content'] .= "</span></p></div>";
731 } else if (is_numeric($result) && $result == -1) {
732 $reply['first_id_changed'] = true;
736 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H2", $timing_info);
738 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache,
739 $vgroup_last_feed, $reply);
742 function catchupAll() {
743 $sth = $this->pdo
->prepare("UPDATE ttrss_user_entries SET
744 last_read = NOW(), unread = false WHERE unread = true AND owner_uid = ?");
745 $sth->execute([$_SESSION['uid']]);
747 CCache
::zero_all($_SESSION["uid"]);
751 $timing_info = microtime(true);
755 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
757 $feed = $_REQUEST["feed"];
758 $method = $_REQUEST["m"];
759 $view_mode = $_REQUEST["view_mode"];
761 @$cat_view = $_REQUEST["cat"] == "true";
762 @$next_unread_feed = $_REQUEST["nuf"];
763 @$offset = $_REQUEST["skip"];
764 @$vgroup_last_feed = $_REQUEST["vgrlf"];
765 $order_by = $_REQUEST["order_by"];
766 $check_first_id = $_REQUEST["fid"];
768 if (is_numeric($feed)) $feed = (int) $feed;
770 /* Feed -5 is a special case: it is used to display auxiliary information
771 * when there's nothing to load - e.g. no stuff in fresh feed */
774 print json_encode($this->generate_dashboard_feed());
779 if ($feed < LABEL_BASE_INDEX
) {
781 $label_feed = Labels
::feed_to_label_id($feed);
783 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_labels2 WHERE
784 id = ? AND owner_uid = ?");
785 $sth->execute([$label_feed, $_SESSION['uid']]);
787 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
789 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE
790 id = ? AND owner_uid = ?");
791 $sth->execute([$feed, $_SESSION['uid']]);
793 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
795 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feed_categories WHERE
796 id = ? AND owner_uid = ?");
798 $sth->execute([$feed, $_SESSION['uid']]);
801 if ($sth && !$sth->fetch()) {
802 print json_encode($this->generate_error_feed(__("Feed not found.")));
806 /* Updating a label ccache means recalculating all of the caches
807 * so for performance reasons we don't do that here */
810 CCache
::update($feed, $_SESSION["uid"], $cat_view);
813 set_pref("_DEFAULT_VIEW_MODE", $view_mode);
814 set_pref("_DEFAULT_VIEW_ORDER_BY", $order_by);
816 /* bump login timestamp if needed */
817 if (time() - $_SESSION["last_login_update"] > 3600) {
818 $sth = $this->pdo
->prepare("UPDATE ttrss_users SET last_login = NOW() WHERE id = ?");
819 $sth->execute([$_SESSION['uid']]);
821 $_SESSION["last_login_update"] = time();
824 if (!$cat_view && is_numeric($feed) && $feed > 0) {
825 $sth = $this->pdo
->prepare("UPDATE ttrss_feeds SET last_viewed = NOW()
826 WHERE id = ? AND owner_uid = ?");
827 $sth->execute([$feed, $_SESSION['uid']]);
830 $reply['headlines'] = array();
832 $override_order = false;
833 $skip_first_id_check = false;
837 $override_order = "ttrss_entries.title, date_entered, updated";
840 $override_order = "score DESC, date_entered, updated";
841 $skip_first_id_check = true;
844 $override_order = "updated DESC";
848 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
850 $ret = $this->format_headlines_list($feed, $method,
851 $view_mode, $limit, $cat_view, $offset,
852 $vgroup_last_feed, $override_order, true, $check_first_id, $skip_first_id_check);
854 //$topmost_article_ids = $ret[0];
855 $headlines_count = $ret[1];
856 /* $returned_feed = $ret[2]; */
857 $disable_cache = $ret[3];
858 $vgroup_last_feed = $ret[4];
860 //$reply['headlines']['content'] =& $ret[5]['content'];
861 //$reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
863 $reply['headlines'] = $ret[5];
865 if (!$next_unread_feed)
866 $reply['headlines']['id'] = $feed;
868 $reply['headlines']['id'] = $next_unread_feed;
870 $reply['headlines']['is_cat'] = (bool) $cat_view;
872 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
874 $reply['headlines-info'] = array("count" => (int) $headlines_count,
875 "vgroup_last_feed" => $vgroup_last_feed,
876 "disable_cache" => (bool) $disable_cache);
878 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
880 $reply['runtime-info'] = make_runtime_info();
882 print json_encode($reply);
886 private function generate_dashboard_feed() {
889 $reply['headlines']['id'] = -5;
890 $reply['headlines']['is_cat'] = false;
892 $reply['headlines']['toolbar'] = '';
894 $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
896 $reply['headlines']['content'] .= "<p><span class=\"insensitive\">";
898 $sth = $this->pdo
->prepare("SELECT ".SUBSTRING_FOR_DATE
."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
899 WHERE owner_uid = ?");
900 $sth->execute([$_SESSION['uid']]);
901 $row = $sth->fetch();
903 $last_updated = make_local_datetime($row["last_updated"], false);
905 $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
907 $sth = $this->pdo
->prepare("SELECT COUNT(id) AS num_errors
908 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ?");
909 $sth->execute([$_SESSION['uid']]);
910 $row = $sth->fetch();
912 $num_errors = $row["num_errors"];
914 if ($num_errors > 0) {
915 $reply['headlines']['content'] .= "<br/>";
916 $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
917 __('Some feeds have update errors (click for details)')."</a>";
919 $reply['headlines']['content'] .= "</span></p>";
921 $reply['headlines-info'] = array("count" => 0,
922 "vgroup_last_feed" => '',
924 "disable_cache" => true);
929 private function generate_error_feed($error) {
932 $reply['headlines']['id'] = -7;
933 $reply['headlines']['is_cat'] = false;
935 $reply['headlines']['toolbar'] = '';
936 $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
938 $reply['headlines-info'] = array("count" => 0,
939 "vgroup_last_feed" => '',
941 "disable_cache" => true);
946 function quickAddFeed() {
947 print "<form onsubmit='return false'>";
949 print_hidden("op", "rpc");
950 print_hidden("method", "addfeed");
952 print "<div id='fadd_error_message' style='display : none' class='alert alert-danger'></div>";
954 print "<div id='fadd_multiple_notify' style='display : none'>";
955 print_notice("Provided URL is a HTML page referencing multiple feeds, please select required feed from the dropdown menu below.");
958 print "<div class=\"dlgSec\">".__("Feed or site URL")."</div>";
959 print "<div class=\"dlgSecCont\">";
961 print "<div style='float : right'>
962 <img style='display : none'
963 id='feed_add_spinner' src='images/indicator_white.gif'></div>";
965 print "<input style=\"font-size : 16px; width : 20em;\"
966 placeHolder=\"".__("Feed or site URL")."\"
967 dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"feed\" id=\"feedDlg_feedUrl\">";
971 if (get_pref('ENABLE_FEED_CATS')) {
972 print __('Place in category:') . " ";
973 print_feed_cat_select("cat", false, 'dojoType="dijit.form.Select"');
978 print '<div id="feedDlg_feedsContainer" style="display : none">
980 <div class="dlgSec">' . __('Available feeds') . '</div>
981 <div class="dlgSecCont">'.
982 '<select id="feedDlg_feedContainerSelect"
983 dojoType="dijit.form.Select" size="3">
984 <script type="dojo/method" event="onChange" args="value">
985 dijit.byId("feedDlg_feedUrl").attr("value", value);
990 print "<div id='feedDlg_loginContainer' style='display : none'>
992 <div class=\"dlgSec\">".__("Authentication")."</div>
993 <div class=\"dlgSecCont\">".
995 " <input dojoType=\"dijit.form.TextBox\" name='login'\"
996 placeHolder=\"".__("Login")."\"
997 autocomplete=\"new-password\"
998 style=\"width : 10em;\"> ".
1000 placeHolder=\"".__("Password")."\"
1001 dojoType=\"dijit.form.TextBox\" type='password'
1002 autocomplete=\"new-password\"
1003 style=\"width : 10em;\" name='pass'\">
1007 print "<div style=\"clear : both\">
1008 <input type=\"checkbox\" name=\"need_auth\" dojoType=\"dijit.form.CheckBox\" id=\"feedDlg_loginCheck\"
1009 onclick='checkboxToggleElement(this, \"feedDlg_loginContainer\")'>
1010 <label for=\"feedDlg_loginCheck\">".
1011 __('This feed requires authentication.')."</div>";
1013 print "<div class=\"dlgButtons\">
1014 <button dojoType=\"dijit.form.Button\" class=\"btn-primary\" type=\"submit\" onclick=\"return dijit.byId('feedAddDlg').execute()\">".__('Subscribe')."</button>";
1016 if (!(defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER
)) {
1017 print "<button dojoType=\"dijit.form.Button\" onclick=\"return feedBrowser()\">".__('More feeds')."</button>";
1020 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('feedAddDlg').hide()\">".__('Cancel')."</button>
1028 function feedBrowser() {
1029 if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER
) return;
1031 $browser_search = $_REQUEST["search"];
1033 print_hidden("op", "rpc");
1034 print_hidden("method", "updateFeedBrowser");
1036 print "<div dojoType=\"dijit.Toolbar\">
1037 <div style='float : right'>
1038 <img style='display : none'
1039 id='feed_browser_spinner' src='images/indicator_white.gif'>
1040 <input name=\"search\" dojoType=\"dijit.form.TextBox\" size=\"20\" type=\"search\"
1041 onchange=\"dijit.byId('feedBrowserDlg').update()\" value=\"$browser_search\">
1042 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').update()\">".__('Search')."</button>
1045 print " <select name=\"mode\" dojoType=\"dijit.form.Select\" onchange=\"dijit.byId('feedBrowserDlg').update()\">
1046 <option value='1'>" . __('Popular feeds') . "</option>
1047 <option value='2'>" . __('Feed archive') . "</option>
1052 print " <select dojoType=\"dijit.form.Select\" name=\"limit\" onchange=\"dijit.byId('feedBrowserDlg').update()\">";
1054 foreach (array(25, 50, 100, 200) as $l) {
1055 //$issel = ($l == $limit) ? "selected=\"1\"" : "";
1056 print "<option value=\"$l\">$l</option>";
1063 require_once "feedbrowser.php";
1065 print "<ul class='browseFeedList' id='browseFeedList'>";
1066 print make_feed_browser("", 25);
1069 print "<div align='center'>
1070 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').execute()\">".__('Subscribe')."</button>
1071 <button dojoType=\"dijit.form.Button\" style='display : none' id='feed_archive_remove' onclick=\"dijit.byId('feedBrowserDlg').removeFromArchive()\">".__('Remove')."</button>
1072 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').hide()\" >".__('Cancel')."</button></div>";
1077 $this->params
= explode(":", $_REQUEST["param"], 2);
1079 $active_feed_id = sprintf("%d", $this->params
[0]);
1080 $is_cat = $this->params
[1] != "false";
1082 print "<form onsubmit='return false;'>";
1084 print "<div class=\"dlgSec\">".__('Look for')."</div>";
1086 print "<div class=\"dlgSecCont\">";
1088 print "<input dojoType=\"dijit.form.ValidationTextBox\"
1089 style=\"font-size : 16px; width : 20em;\"
1090 required=\"1\" name=\"query\" type=\"search\" value=''>";
1092 print "<hr/><span style='float : right'>".T_sprintf('in %s', $this->getFeedTitle($active_feed_id, $is_cat))."</span>";
1094 if (DB_TYPE
== "pgsql") {
1096 print_select("search_language", "", Pref_Feeds
::$feed_languages,
1097 "dojoType='dijit.form.Select' title=\"".__('Used for word stemming')."\"");
1102 print "<div class=\"dlgButtons\">";
1104 if (count(PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_SEARCH
)) == 0) {
1105 print "<div style=\"float : left\">
1106 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/SearchSyntax\">".__("Search syntax")."</a>
1110 print "<button dojoType=\"dijit.form.Button\" type=\"submit\" class=\"btn-primary\" onclick=\"dijit.byId('searchDlg').execute()\">".__('Search')."</button>
1111 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('searchDlg').hide()\">".__('Cancel')."</button>
1117 function update_debugger() {
1118 header("Content-type: text/html");
1120 Debug
::set_enabled(true);
1121 Debug
::set_loglevel($_REQUEST["xdebug"]);
1123 $feed_id = (int)$_REQUEST["feed_id"];
1124 @$do_update = $_REQUEST["action"] == "do_update";
1125 $csrf_token = $_REQUEST["csrf_token"];
1127 $sth = $this->pdo
->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
1128 $sth->execute([$feed_id, $_SESSION['uid']]);
1130 if (!$sth->fetch()) {
1131 print "Access denied.";
1135 $refetch_checked = isset($_REQUEST["force_refetch"]) ?
"checked" : "";
1136 $rehash_checked = isset($_REQUEST["force_rehash"]) ?
"checked" : "";
1141 <?php
echo stylesheet_tag("css/default.css") ?
>
1142 <title
>Feed Debugger
</title
>
1144 <body
class="small_margins ttrss_utility claro">
1145 <h1
>Feed Debugger
: <?php
echo "$feed_id: " . $this->getFeedTitle($feed_id) ?
></h1
>
1146 <form method
="GET" action
="">
1147 <input type
="hidden" name
="op" value
="feeds">
1148 <input type
="hidden" name
="method" value
="update_debugger">
1149 <input type
="hidden" name
="xdebug" value
="1">
1150 <input type
="hidden" name
="csrf_token" value
="<?php echo $csrf_token ?>">
1151 <input type
="hidden" name
="action" value
="do_update">
1152 <input type
="hidden" name
="feed_id" value
="<?php echo $feed_id ?>">
1153 <input type
="checkbox" name
="force_refetch" value
="1" <?php
echo $refetch_checked ?
>> Force refetch
<br
/>
1154 <input type
="checkbox" name
="force_rehash" value
="1" <?php
echo $rehash_checked ?
>> Force rehash
<br
/>
1156 <p
/><button type
="submit">Continue</button
>
1164 RSSUtils
::update_rss_feed($feed_id, true);
1175 static function catchup_feed($feed, $cat_view, $owner_uid = false, $mode = 'all', $search = false) {
1177 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
1181 // Todo: all this interval stuff needs some generic generator function
1183 $search_qpart = is_array($search) && $search[0] ?
search_to_sql($search[0], $search[1])[0] : 'true';
1187 if (DB_TYPE
== "pgsql") {
1188 $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
1190 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
1194 if (DB_TYPE
== "pgsql") {
1195 $date_qpart = "date_entered < NOW() - INTERVAL '1 week' ";
1197 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) ";
1201 if (DB_TYPE
== "pgsql") {
1202 $date_qpart = "date_entered < NOW() - INTERVAL '2 week' ";
1204 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) ";
1208 $date_qpart = "true";
1211 if (is_numeric($feed)) {
1217 $children = Feeds
::getChildCategories($feed, $owner_uid);
1218 array_push($children, $feed);
1219 $children = array_map("intval", $children);
1221 $children = join(",", $children);
1223 $cat_qpart = "cat_id IN ($children)";
1225 $cat_qpart = "cat_id IS NULL";
1228 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1229 SET unread = false, last_read = NOW() WHERE ref_id IN
1231 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1232 AND owner_uid = ? AND unread = true AND feed_id IN
1233 (SELECT id FROM ttrss_feeds WHERE $cat_qpart) AND $date_qpart AND $search_qpart) as tmp)");
1234 $sth->execute([$owner_uid]);
1236 } else if ($feed == -2) {
1238 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1239 SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
1240 FROM ttrss_user_labels2, ttrss_entries WHERE article_id = ref_id AND id = ref_id AND $date_qpart AND $search_qpart) > 0
1241 AND unread = true AND owner_uid = ?");
1242 $sth->execute([$owner_uid]);
1245 } else if ($feed > 0) {
1247 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1248 SET unread = false, last_read = NOW() WHERE ref_id IN
1250 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1251 AND owner_uid = ? AND unread = true AND feed_id = ? AND $date_qpart AND $search_qpart) as tmp)");
1252 $sth->execute([$owner_uid, $feed]);
1254 } else if ($feed < 0 && $feed > LABEL_BASE_INDEX
) { // special, like starred
1257 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1258 SET unread = false, last_read = NOW() WHERE ref_id IN
1260 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1261 AND owner_uid = ? AND unread = true AND marked = true AND $date_qpart AND $search_qpart) as tmp)");
1262 $sth->execute([$owner_uid]);
1266 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1267 SET unread = false, last_read = NOW() WHERE ref_id IN
1269 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1270 AND owner_uid = ? AND unread = true AND published = true AND $date_qpart AND $search_qpart) as tmp)");
1271 $sth->execute([$owner_uid]);
1276 $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE");
1278 if (DB_TYPE
== "pgsql") {
1279 $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
1281 $match_part = "date_entered > DATE_SUB(NOW(),
1282 INTERVAL $intl HOUR) ";
1285 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1286 SET unread = false, last_read = NOW() WHERE ref_id IN
1288 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1289 AND owner_uid = ? AND score >= 0 AND unread = true AND $date_qpart AND $match_part AND $search_qpart) as tmp)");
1290 $sth->execute([$owner_uid]);
1294 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1295 SET unread = false, last_read = NOW() WHERE ref_id IN
1297 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1298 AND owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1299 $sth->execute([$owner_uid]);
1302 } else if ($feed < LABEL_BASE_INDEX
) { // label
1304 $label_id = Labels
::feed_to_label_id($feed);
1306 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1307 SET unread = false, last_read = NOW() WHERE ref_id IN
1309 (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_user_labels2 WHERE ref_id = id
1310 AND label_id = ? AND ref_id = article_id
1311 AND owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1312 $sth->execute([$label_id, $owner_uid]);
1316 CCache
::update($feed, $owner_uid, $cat_view);
1319 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1320 SET unread = false, last_read = NOW() WHERE ref_id IN
1322 (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_tags WHERE ref_id = ttrss_entries.id
1323 AND post_int_id = int_id AND tag_name = ?
1324 AND ttrss_user_entries.owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1325 $sth->execute([$feed, $owner_uid]);
1330 static function getFeedArticles($feed, $is_cat = false, $unread_only = false,
1331 $owner_uid = false) {
1333 $n_feed = (int) $feed;
1334 $need_entries = false;
1338 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1341 $unread_qpart = "unread = true";
1343 $unread_qpart = "true";
1349 return Feeds
::getCategoryUnread($n_feed, $owner_uid);
1350 } else if ($n_feed == -6) {
1352 } else if ($feed != "0" && $n_feed == 0) {
1354 $sth = $pdo->prepare("SELECT SUM((SELECT COUNT(int_id)
1355 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
1356 AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags
1357 WHERE owner_uid = ? AND tag_name = ?");
1359 $sth->execute([$owner_uid, $feed]);
1360 $row = $sth->fetch();
1362 return $row["count"];
1364 } else if ($n_feed == -1) {
1365 $match_part = "marked = true";
1366 } else if ($n_feed == -2) {
1367 $match_part = "published = true";
1368 } else if ($n_feed == -3) {
1369 $match_part = "unread = true AND score >= 0";
1371 $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
1373 if (DB_TYPE
== "pgsql") {
1374 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1376 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
1379 $need_entries = true;
1381 } else if ($n_feed == -4) {
1382 $match_part = "true";
1383 } else if ($n_feed >= 0) {
1386 $match_part = "feed_id = " . (int)$n_feed;
1388 $match_part = "feed_id IS NULL";
1391 } else if ($feed < LABEL_BASE_INDEX
) {
1393 $label_id = Labels
::feed_to_label_id($feed);
1395 return Feeds
::getLabelUnread($label_id, $owner_uid);
1400 if ($need_entries) {
1401 $from_qpart = "ttrss_user_entries,ttrss_entries";
1402 $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND";
1404 $from_qpart = "ttrss_user_entries";
1408 $sth = $pdo->prepare("SELECT count(int_id) AS unread
1409 FROM $from_qpart WHERE
1410 $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = ?");
1411 $sth->execute([$owner_uid]);
1412 $row = $sth->fetch();
1414 return $row["unread"];
1418 $sth = $pdo->prepare("SELECT COUNT(post_int_id) AS unread
1419 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
1420 WHERE tag_name = ? AND post_int_id = int_id AND ref_id = ttrss_entries.id
1421 AND $unread_qpart AND ttrss_tags.owner_uid = ,");
1423 $sth->execute([$feed, $owner_uid]);
1424 $row = $sth->fetch();
1426 return $row["unread"];
1431 * @return array (code => Status code, message => error message if available)
1433 * 0 - OK, Feed already exists
1434 * 1 - OK, Feed added
1436 * 3 - URL content is HTML, no feeds available
1437 * 4 - URL content is HTML which contains multiple feeds.
1438 * Here you should call extractfeedurls in rpc-backend
1439 * to get all possible feeds.
1440 * 5 - Couldn't download the URL content.
1441 * 6 - Content is an invalid XML.
1443 static function subscribe_to_feed($url, $cat_id = 0,
1444 $auth_login = '', $auth_pass = '') {
1446 global $fetch_last_error;
1447 global $fetch_last_error_content;
1451 $url = fix_url($url);
1453 if (!$url ||
!validate_feed_url($url)) return array("code" => 2);
1455 $contents = @fetch_file_contents
($url, false, $auth_login, $auth_pass);
1457 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_SUBSCRIBE_FEED
) as $plugin) {
1458 $contents = $plugin->hook_subscribe_feed($contents, $url, $auth_login, $auth_pass);
1462 if (preg_match("/cloudflare\.com/", $fetch_last_error_content)) {
1463 $fetch_last_error .= " (feed behind Cloudflare)";
1466 return array("code" => 5, "message" => $fetch_last_error);
1469 if (is_html($contents)) {
1470 $feedUrls = get_feeds_from_html($url, $contents);
1472 if (count($feedUrls) == 0) {
1473 return array("code" => 3);
1474 } else if (count($feedUrls) > 1) {
1475 return array("code" => 4, "feeds" => $feedUrls);
1477 //use feed url as new URL
1478 $url = key($feedUrls);
1481 if (!$cat_id) $cat_id = null;
1483 $sth = $pdo->prepare("SELECT id FROM ttrss_feeds
1484 WHERE feed_url = ? AND owner_uid = ?");
1485 $sth->execute([$url, $_SESSION['uid']]);
1487 if ($row = $sth->fetch()) {
1488 return array("code" => 0, "feed_id" => (int) $row["id"]);
1490 $sth = $pdo->prepare(
1491 "INSERT INTO ttrss_feeds
1492 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted)
1493 VALUES (?, ?, ?, ?, ?, ?, 0, false)");
1495 $sth->execute([$_SESSION['uid'], $url, "[Unknown]", $cat_id, (string)$auth_login, (string)$auth_pass]);
1497 $sth = $pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
1498 AND owner_uid = ?");
1499 $sth->execute([$url, $_SESSION['uid']]);
1500 $row = $sth->fetch();
1502 $feed_id = $row["id"];
1505 RSSUtils
::set_basic_feed_info($feed_id);
1508 return array("code" => 1, "feed_id" => (int) $feed_id);
1513 static function getIconFile($feed_id) {
1514 return ICONS_DIR
. "/$feed_id.ico";
1517 static function feedHasIcon($id) {
1518 return is_file(ICONS_DIR
. "/$id.ico") && filesize(ICONS_DIR
. "/$id.ico") > 0;
1521 static function getFeedIcon($id) {
1524 return "images/archive.png";
1527 return "images/star.png";
1530 return "images/feed.png";
1533 return "images/fresh.png";
1536 return "images/folder.png";
1539 return "images/time.png";
1542 if ($id < LABEL_BASE_INDEX
) {
1543 return "images/label.png";
1545 $icon = self
::getIconFile($id);
1547 if ($icon && file_exists($icon)) {
1548 return ICONS_URL
. "/" . basename($icon) . "?" . filemtime($icon);
1557 static function getFeedTitle($id, $cat = false) {
1561 return Feeds
::getCategoryTitle($id);
1562 } else if ($id == -1) {
1563 return __("Starred articles");
1564 } else if ($id == -2) {
1565 return __("Published articles");
1566 } else if ($id == -3) {
1567 return __("Fresh articles");
1568 } else if ($id == -4) {
1569 return __("All articles");
1570 } else if ($id === 0 ||
$id === "0") {
1571 return __("Archived articles");
1572 } else if ($id == -6) {
1573 return __("Recently read");
1574 } else if ($id < LABEL_BASE_INDEX
) {
1576 $label_id = Labels
::feed_to_label_id($id);
1578 $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 WHERE id = ?");
1579 $sth->execute([$label_id]);
1581 if ($row = $sth->fetch()) {
1582 return $row["caption"];
1584 return "Unknown label ($label_id)";
1587 } else if (is_numeric($id) && $id > 0) {
1589 $sth = $pdo->prepare("SELECT title FROM ttrss_feeds WHERE id = ?");
1590 $sth->execute([$id]);
1592 if ($row = $sth->fetch()) {
1593 return $row["title"];
1595 return "Unknown feed ($id)";
1603 static function getCategoryUnread($cat, $owner_uid = false) {
1605 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1611 if (!$cat) $cat = null;
1613 $sth = $pdo->prepare("SELECT id FROM ttrss_feeds
1614 WHERE (cat_id = :cat OR (:cat IS NULL AND cat_id IS NULL))
1615 AND owner_uid = :uid");
1617 $sth->execute([":cat" => $cat, ":uid" => $owner_uid]);
1619 $cat_feeds = array();
1620 while ($line = $sth->fetch()) {
1621 array_push($cat_feeds, "feed_id = " . (int)$line["id"]);
1624 if (count($cat_feeds) == 0) return 0;
1626 $match_part = implode(" OR ", $cat_feeds);
1628 $sth = $pdo->prepare("SELECT COUNT(int_id) AS unread
1629 FROM ttrss_user_entries
1630 WHERE unread = true AND ($match_part)
1631 AND owner_uid = ?");
1632 $sth->execute([$owner_uid]);
1636 # this needs to be rewritten
1637 while ($line = $sth->fetch()) {
1638 $unread +
= $line["unread"];
1642 } else if ($cat == -1) {
1643 return getFeedUnread(-1) +
getFeedUnread(-2) +
getFeedUnread(-3) +
getFeedUnread(0);
1644 } else if ($cat == -2) {
1646 $sth = $pdo->prepare("SELECT COUNT(unread) AS unread FROM
1647 ttrss_user_entries, ttrss_user_labels2
1648 WHERE article_id = ref_id AND unread = true
1649 AND ttrss_user_entries.owner_uid = ?");
1650 $sth->execute([$owner_uid]);
1651 $row = $sth->fetch();
1653 return $row["unread"];
1657 // only accepts real cats (>= 0)
1658 static function getCategoryChildrenUnread($cat, $owner_uid = false) {
1659 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1663 $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories WHERE parent_cat = ?
1664 AND owner_uid = ?");
1665 $sth->execute([$cat, $owner_uid]);
1669 while ($line = $sth->fetch()) {
1670 $unread +
= Feeds
::getCategoryUnread($line["id"], $owner_uid);
1671 $unread +
= Feeds
::getCategoryChildrenUnread($line["id"], $owner_uid);
1677 static function getGlobalUnread($user_id = false) {
1679 if (!$user_id) $user_id = $_SESSION["uid"];
1683 $sth = $pdo->prepare("SELECT SUM(value) AS c_id FROM ttrss_counters_cache
1684 WHERE owner_uid = ? AND feed_id > 0");
1685 $sth->execute([$user_id]);
1686 $row = $sth->fetch();
1688 return $row["c_id"];
1691 static function getCategoryTitle($cat_id) {
1693 if ($cat_id == -1) {
1694 return __("Special");
1695 } else if ($cat_id == -2) {
1696 return __("Labels");
1701 $sth = $pdo->prepare("SELECT title FROM ttrss_feed_categories WHERE
1703 $sth->execute([$cat_id]);
1705 if ($row = $sth->fetch()) {
1706 return $row["title"];
1708 return __("Uncategorized");
1713 static function getLabelUnread($label_id, $owner_uid = false) {
1714 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1718 $sth = $pdo->prepare("SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2
1719 WHERE owner_uid = ? AND unread = true AND label_id = ? AND article_id = ref_id");
1721 $sth->execute([$owner_uid, $label_id]);
1723 if ($row = $sth->fetch()) {
1724 return $row["unread"];
1730 static function queryFeedHeadlines($params) {
1734 // WARNING: due to highly dynamic nature of this query its going to quote parameters
1735 // right before adding them to SQL part
1737 $feed = $params["feed"];
1738 $limit = isset($params["limit"]) ?
$params["limit"] : 30;
1739 $view_mode = $params["view_mode"];
1740 $cat_view = isset($params["cat_view"]) ?
$params["cat_view"] : false;
1741 $search = isset($params["search"]) ?
$params["search"] : false;
1742 $search_language = isset($params["search_language"]) ?
$params["search_language"] : "";
1743 $override_order = isset($params["override_order"]) ?
$params["override_order"] : false;
1744 $offset = isset($params["offset"]) ?
$params["offset"] : 0;
1745 $owner_uid = isset($params["owner_uid"]) ?
$params["owner_uid"] : $_SESSION["uid"];
1746 $since_id = isset($params["since_id"]) ?
$params["since_id"] : 0;
1747 $include_children = isset($params["include_children"]) ?
$params["include_children"] : false;
1748 $ignore_vfeed_group = isset($params["ignore_vfeed_group"]) ?
$params["ignore_vfeed_group"] : false;
1749 $override_strategy = isset($params["override_strategy"]) ?
$params["override_strategy"] : false;
1750 $override_vfeed = isset($params["override_vfeed"]) ?
$params["override_vfeed"] : false;
1751 $start_ts = isset($params["start_ts"]) ?
$params["start_ts"] : false;
1752 $check_first_id = isset($params["check_first_id"]) ?
$params["check_first_id"] : false;
1753 $skip_first_id_check = isset($params["skip_first_id_check"]) ?
$params["skip_first_id_check"] : false;
1755 $ext_tables_part = "";
1756 $limit_query_part = "";
1758 $search_words = array();
1761 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_SEARCH
) as $plugin) {
1762 list($search_query_part, $search_words) = $plugin->hook_search($search);
1766 // fall back in case of no plugins
1767 if (!$search_query_part) {
1768 list($search_query_part, $search_words) = search_to_sql($search, $search_language);
1770 $search_query_part .= " AND ";
1772 $search_query_part = "";
1776 $since_id_part = "ttrss_entries.id > ".$pdo->quote($since_id)." AND ";
1778 $since_id_part = "";
1781 $view_query_part = "";
1783 if ($view_mode == "adaptive") {
1785 $view_query_part = " ";
1786 } else if ($feed != -1) {
1788 $unread = getFeedUnread($feed, $cat_view);
1790 if ($cat_view && $feed > 0 && $include_children)
1791 $unread +
= Feeds
::getCategoryChildrenUnread($feed);
1794 $view_query_part = " unread = true AND ";
1799 if ($view_mode == "marked") {
1800 $view_query_part = " marked = true AND ";
1803 if ($view_mode == "has_note") {
1804 $view_query_part = " (note IS NOT NULL AND note != '') AND ";
1807 if ($view_mode == "published") {
1808 $view_query_part = " published = true AND ";
1811 if ($view_mode == "unread" && $feed != -6) {
1812 $view_query_part = " unread = true AND ";
1816 $limit_query_part = "LIMIT " . (int)$limit;
1819 $allow_archived = false;
1821 $vfeed_query_part = "";
1824 if (!is_numeric($feed)) {
1825 $query_strategy_part = "true";
1826 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
1827 id = feed_id) as feed_title,";
1828 } else if ($feed > 0) {
1833 if ($include_children) {
1835 $subcats = Feeds
::getChildCategories($feed, $owner_uid);
1836 array_push($subcats, $feed);
1837 $subcats = array_map("intval", $subcats);
1839 $query_strategy_part = "cat_id IN (".
1840 implode(",", $subcats).")";
1843 $query_strategy_part = "cat_id = " . $pdo->quote($feed);
1847 $query_strategy_part = "cat_id IS NULL";
1850 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1853 $query_strategy_part = "feed_id = " . $pdo->quote($feed);
1855 } else if ($feed == 0 && !$cat_view) { // archive virtual feed
1856 $query_strategy_part = "feed_id IS NULL";
1857 $allow_archived = true;
1858 } else if ($feed == 0 && $cat_view) { // uncategorized
1859 $query_strategy_part = "cat_id IS NULL AND feed_id IS NOT NULL";
1860 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1861 } else if ($feed == -1) { // starred virtual feed
1862 $query_strategy_part = "marked = true";
1863 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1864 $allow_archived = true;
1866 if (!$override_order) {
1867 $override_order = "last_marked DESC, date_entered DESC, updated DESC";
1870 } else if ($feed == -2) { // published virtual feed OR labels category
1873 $query_strategy_part = "published = true";
1874 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1875 $allow_archived = true;
1877 if (!$override_order) {
1878 $override_order = "last_published DESC, date_entered DESC, updated DESC";
1882 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1884 $ext_tables_part = "ttrss_labels2,ttrss_user_labels2,";
1886 $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
1887 ttrss_user_labels2.article_id = ref_id";
1890 } else if ($feed == -6) { // recently read
1891 $query_strategy_part = "unread = false AND last_read IS NOT NULL";
1893 if (DB_TYPE
== "pgsql") {
1894 $query_strategy_part .= " AND last_read > NOW() - INTERVAL '1 DAY' ";
1896 $query_strategy_part .= " AND last_read > DATE_SUB(NOW(), INTERVAL 1 DAY) ";
1899 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1900 $allow_archived = true;
1901 $ignore_vfeed_group = true;
1903 if (!$override_order) $override_order = "last_read DESC";
1905 } else if ($feed == -3) { // fresh virtual feed
1906 $query_strategy_part = "unread = true AND score >= 0";
1908 $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
1910 if (DB_TYPE
== "pgsql") {
1911 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1913 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
1916 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1917 } else if ($feed == -4) { // all articles virtual feed
1918 $allow_archived = true;
1919 $query_strategy_part = "true";
1920 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1921 } else if ($feed <= LABEL_BASE_INDEX
) { // labels
1922 $label_id = Labels
::feed_to_label_id($feed);
1924 $query_strategy_part = "label_id = ".$pdo->quote($label_id)." AND
1925 ttrss_labels2.id = ttrss_user_labels2.label_id AND
1926 ttrss_user_labels2.article_id = ref_id";
1928 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1929 $ext_tables_part = "ttrss_labels2,ttrss_user_labels2,";
1930 $allow_archived = true;
1933 $query_strategy_part = "true";
1936 $order_by = "score DESC, date_entered DESC, updated DESC";
1938 if ($override_order) {
1939 $order_by = $override_order;
1942 if ($override_strategy) {
1943 $query_strategy_part = $override_strategy;
1946 if ($override_vfeed) {
1947 $vfeed_query_part = $override_vfeed;
1951 $feed_title = T_sprintf("Search results: %s", $search);
1954 $feed_title = Feeds
::getCategoryTitle($feed);
1956 if (is_numeric($feed) && $feed > 0) {
1957 $ssth = $pdo->prepare("SELECT title,site_url,last_error,last_updated
1958 FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
1959 $ssth->execute([$feed, $owner_uid]);
1960 $row = $ssth->fetch();
1962 $feed_title = $row["title"];
1963 $feed_site_url = $row["site_url"];
1964 $last_error = $row["last_error"];
1965 $last_updated = $row["last_updated"];
1967 $feed_title = Feeds
::getFeedTitle($feed);
1972 $content_query_part = "content, ";
1974 if ($limit_query_part) {
1975 $offset_query_part = "OFFSET " . (int)$offset;
1977 $offset_query_part = "";
1980 if (is_numeric($feed)) {
1981 // proper override_order applied above
1982 if ($vfeed_query_part && !$ignore_vfeed_group && get_pref('VFEED_GROUP_BY_FEED', $owner_uid)) {
1983 if (!$override_order) {
1984 $order_by = "ttrss_feeds.title, ".$order_by;
1986 $order_by = "ttrss_feeds.title, ".$override_order;
1990 if (!$allow_archived) {
1991 $from_qpart = "${ext_tables_part}ttrss_entries LEFT JOIN ttrss_user_entries ON (ref_id = ttrss_entries.id),ttrss_feeds";
1992 $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
1995 $from_qpart = "${ext_tables_part}ttrss_entries LEFT JOIN ttrss_user_entries ON (ref_id = ttrss_entries.id)
1996 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
1999 if ($vfeed_query_part) $vfeed_query_part .= "favicon_avg_color,";
2002 $start_ts_formatted = date("Y/m/d H:i:s", strtotime($start_ts));
2003 $start_ts_query_part = "date_entered >= '$start_ts_formatted' AND";
2005 $start_ts_query_part = "";
2009 $first_id_query_strategy_part = $query_strategy_part;
2012 $first_id_query_strategy_part = "true";
2014 if (DB_TYPE
== "pgsql") {
2015 $sanity_interval_qpart = "date_entered >= NOW() - INTERVAL '1 hour' AND";
2017 $sanity_interval_qpart = "date_entered >= DATE_SUB(NOW(), INTERVAL 1 hour) AND";
2020 if (!$search && !$skip_first_id_check) {
2021 // if previous topmost article id changed that means our current pagination is no longer valid
2022 $query = "SELECT DISTINCT
2027 ttrss_entries.title,
2039 ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
2041 $start_ts_query_part
2043 $sanity_interval_qpart
2044 $first_id_query_strategy_part ORDER BY $order_by LIMIT 1";
2046 /*if ($_REQUEST["debug"]) {
2050 $res = $pdo->query($query);
2052 if ($row = $res->fetch()) {
2053 $first_id = (int)$row["id"];
2055 if ($offset > 0 && $first_id && $check_first_id && $first_id != $check_first_id) {
2056 return array(-1, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id);
2061 $query = "SELECT DISTINCT
2064 ttrss_entries.id,ttrss_entries.title,
2068 always_display_enclosures,
2077 unread,feed_id,marked,published,link,last_read,orig_feed_id,
2078 last_marked, last_published,
2086 ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
2088 $start_ts_query_part
2091 $query_strategy_part ORDER BY $order_by
2092 $limit_query_part $offset_query_part";
2094 //if ($_REQUEST["debug"]) print $query;
2096 $res = $pdo->query($query);
2101 $query = "SELECT DISTINCT
2105 ttrss_entries.id as id,
2121 (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images,
2122 last_marked, last_published,
2127 FROM ttrss_entries, ttrss_user_entries, ttrss_tags
2129 ref_id = ttrss_entries.id AND
2130 ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
2131 post_int_id = int_id AND
2132 tag_name = ".$pdo->quote($feed)." AND
2135 $query_strategy_part ORDER BY $order_by
2136 $limit_query_part $offset_query_part";
2138 if ($_REQUEST["debug"]) print $query;
2140 $res = $pdo->query($query);
2143 return array($res, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id);
2147 static function getParentCategories($cat, $owner_uid) {
2152 $sth = $pdo->prepare("SELECT parent_cat FROM ttrss_feed_categories
2153 WHERE id = ? AND parent_cat IS NOT NULL AND owner_uid = ?");
2154 $sth->execute([$cat, $owner_uid]);
2156 while ($line = $sth->fetch()) {
2157 array_push($rv, $line["parent_cat"]);
2158 $rv = array_merge($rv, Feeds
::getParentCategories($line["parent_cat"], $owner_uid));
2164 static function getChildCategories($cat, $owner_uid) {
2169 $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories
2170 WHERE parent_cat = ? AND owner_uid = ?");
2171 $sth->execute([$cat, $owner_uid]);
2173 while ($line = $sth->fetch()) {
2174 array_push($rv, $line["id"]);
2175 $rv = array_merge($rv, Feeds
::getChildCategories($line["id"], $owner_uid));
2181 static function getFeedCategory($feed) {
2184 $sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds
2186 $sth->execute([$feed]);
2188 if ($row = $sth->fetch()) {
2189 return $row["cat_id"];