]> git.wh0rd.org - tt-rss.git/blob - classes/feeds.php
format_headlines_list: prevent warning on first_id changed check
[tt-rss.git] / classes / feeds.php
1 <?php
2 require_once "colors.php";
3
4 class Feeds extends Handler_Protected {
5
6 private $params;
7
8 function csrf_ignore($method) {
9 $csrf_ignored = array("index", "feedbrowser", "quickaddfeed", "search");
10
11 return array_search($method, $csrf_ignored) !== false;
12 }
13
14 private function format_headline_subtoolbar($feed_site_url, $feed_title,
15 $feed_id, $is_cat, $search,
16 $error, $feed_last_updated) {
17
18 $catchup_sel_link = "catchupSelection()";
19
20 $archive_sel_link = "archiveSelection()";
21 $delete_sel_link = "deleteSelection()";
22
23 $sel_all_link = "selectArticles('all')";
24 $sel_unread_link = "selectArticles('unread')";
25 $sel_none_link = "selectArticles('none')";
26 $sel_inv_link = "selectArticles('invert')";
27
28 $tog_unread_link = "selectionToggleUnread()";
29 $tog_marked_link = "selectionToggleMarked()";
30 $tog_published_link = "selectionTogglePublished()";
31
32 $set_score_link = "setSelectionScore()";
33
34 if ($is_cat) $cat_q = "&is_cat=$is_cat";
35
36 if ($search) {
37 $search_q = "&q=$search";
38 } else {
39 $search_q = "";
40 }
41
42 $reply = "<span class=\"holder\">";
43
44 $rss_link = htmlspecialchars(get_self_url_prefix() .
45 "/public.php?op=rss&id=$feed_id$cat_q$search_q");
46
47 // right part
48
49 $error_class = $error ? "error" : "";
50
51 $reply .= "<span class='r'>
52 <a href=\"#\"
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>";
56
57
58 # $reply .= "<span>";
59 $reply .= "<span id='feed_title' class='$error_class'>";
60
61 if ($feed_site_url) {
62 $last_updated = T_sprintf("Last updated: %s",
63 $feed_last_updated);
64
65 $target = "target=\"_blank\"";
66 $reply .= "<a title=\"$last_updated\" $target href=\"$feed_site_url\">".
67 truncate_string(strip_tags($feed_title), 30)."</a>";
68
69 if ($error) {
70 $error = htmlspecialchars($error);
71 $reply .= "&nbsp;<img title=\"$error\" src='images/error.png' alt='error' class=\"noborder\">";
72 }
73
74 } else {
75 $reply .= strip_tags($feed_title);
76 }
77
78 $reply .= "</span>";
79
80 $reply .= "</span>";
81
82 # $reply .= "</span>";
83
84 // left part
85
86 $reply .= "<span class=\"main\">";
87 $reply .= "<span id='selected_prompt'></span>";
88
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>";
94
95 $reply .= "</span> "; */
96
97 $reply .= "<select dojoType=\"dijit.form.Select\"
98 onchange=\"headlineActionsChange(this)\">";
99
100 $reply .= "<option value=\"0\" disabled='1'>".__('Select...')."</option>";
101
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>";
106
107 $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection toggle:')."</option>";
108
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>";
112
113 $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection:')."</option>";
114
115 $reply .= "<option value=\"$catchup_sel_link\">".__('Mark as read')."</option>";
116 $reply .= "<option value=\"$set_score_link\">".__('Set score')."</option>";
117
118 if ($feed_id != "0") {
119 $reply .= "<option value=\"$archive_sel_link\">".__('Archive')."</option>";
120 } else {
121 $reply .= "<option value=\"$archive_sel_link\">".__('Move back')."</option>";
122 $reply .= "<option value=\"$delete_sel_link\">".__('Delete')."</option>";
123
124 }
125
126 if (PluginHost::getInstance()->get_plugin("mail")) {
127 $reply .= "<option value=\"emailArticle(false)\">".__('Forward by email').
128 "</option>";
129 }
130
131 if (PluginHost::getInstance()->get_plugin("mailto")) {
132 $reply .= "<option value=\"mailtoArticle(false)\">".__('Forward by email').
133 "</option>";
134 }
135
136 $reply .= "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
137
138 //$reply .= "<option value=\"catchupPage()\">".__('Mark as read')."</option>";
139
140 $reply .= "<option value=\"displayDlg('".__("View as RSS")."','generatedFeed', '$feed_id:$is_cat:$rss_link')\">".__('View as RSS')."</option>";
141
142 $reply .= "</select>";
143
144 //$reply .= "</h2";
145
146 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HEADLINE_TOOLBAR_BUTTON) as $p) {
147 $reply .= $p->hook_headline_toolbar_button($feed_id, $is_cat);
148 }
149
150 $reply .= "</span></span>";
151
152 return $reply;
153 }
154
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) {
159
160 $disable_cache = false;
161
162 $reply = array();
163
164 $rgba_cache = array();
165
166 $timing_info = microtime(true);
167
168 $topmost_article_ids = array();
169
170 if (!$offset) $offset = 0;
171 if ($method == "undefined") $method = "";
172
173 $method_split = explode(":", $method);
174
175 if ($method == "ForceUpdate" && $feed > 0 && is_numeric($feed)) {
176 // Update the feed if required with some basic flood control
177
178 $any_needs_curl = false;
179
180 if (ini_get("open_basedir")) {
181 $pluginhost = PluginHost::getInstance();
182 foreach ($pluginhost->get_plugins() as $plugin) {
183 $flags = $plugin->flags();
184
185 if (isset($flags["needs_curl"]) && $flags["needs_curl"]) {
186 $any_needs_curl = true;
187 break;
188 }
189 }
190 }
191
192 //if ($_REQUEST["debug"]) print "<!-- any_needs_curl: $any_needs_curl -->";
193
194 if (!$any_needs_curl) {
195
196 $sth = $this->pdo->prepare("SELECT cache_images," . SUBSTRING_FOR_DATE . "(last_updated,1,19) AS last_updated
197 FROM ttrss_feeds WHERE id = ?");
198 $sth->execute([$feed]);
199
200 if ($row = $sth->fetch()) {
201 $last_updated = strtotime($row["last_updated"]);
202 $cache_images = $row["cache_images"];
203
204 if (!$cache_images && time() - $last_updated > 120) {
205 RSSUtils::update_rss_feed($feed, true);
206 } else {
207 $sth = $this->pdo->prepare("UPDATE ttrss_feeds
208 SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
209 WHERE id = ?");
210 $sth->execute([$feed]);
211 }
212 }
213 } else {
214 $sth = $this->pdo->prepare("UPDATE ttrss_feeds
215 SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
216 WHERE id = ?");
217 $sth->execute([$feed]);
218 }
219 }
220
221 if ($method_split[0] == "MarkAllReadGR") {
222 $this->catchup_feed($method_split[1], false);
223 }
224
225 // FIXME: might break tag display?
226
227 if (is_numeric($feed) && $feed > 0 && !$cat_view) {
228 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? LIMIT 1");
229 $sth->execute([$feed]);
230
231 if (!$sth->fetch()) {
232 $reply['content'] = "<div align='center'>".__('Feed not found.')."</div>";
233 }
234 }
235
236 @$search = $_REQUEST["query"];
237 @$search_language = $_REQUEST["search_language"]; // PGSQL only
238
239 if ($search) {
240 $disable_cache = true;
241 }
242
243 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
244
245 if (!$cat_view && is_numeric($feed) && $feed < PLUGIN_FEED_BASE_INDEX && $feed > LABEL_BASE_INDEX) {
246 $handler = PluginHost::getInstance()->get_feed_handler(
247 PluginHost::feed_to_pfeed_id($feed));
248
249 if ($handler) {
250 $options = array(
251 "limit" => $limit,
252 "view_mode" => $view_mode,
253 "cat_view" => $cat_view,
254 "search" => $search,
255 "override_order" => $override_order,
256 "offset" => $offset,
257 "owner_uid" => $_SESSION["uid"],
258 "filter" => false,
259 "since_id" => 0,
260 "include_children" => $include_children);
261
262 $qfh_ret = $handler->get_headlines(PluginHost::feed_to_pfeed_id($feed),
263 $options);
264 }
265
266 } else {
267
268 $params = array(
269 "feed" => $feed,
270 "limit" => $limit,
271 "view_mode" => $view_mode,
272 "cat_view" => $cat_view,
273 "search" => $search,
274 "search_language" => $search_language,
275 "override_order" => $override_order,
276 "offset" => $offset,
277 "include_children" => $include_children,
278 "check_first_id" => $check_first_id,
279 "skip_first_id_check" => $skip_first_id_check
280 );
281
282 $qfh_ret = $this->queryFeedHeadlines($params);
283 }
284
285 $vfeed_group_enabled = get_pref("VFEED_GROUP_BY_FEED") && $feed != -6;
286
287 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
288
289 $result = $qfh_ret[0]; // this could be either a PDO query result or a -1 if first id changed
290 $feed_title = $qfh_ret[1];
291 $feed_site_url = $qfh_ret[2];
292 $last_error = $qfh_ret[3];
293 $last_updated = strpos($qfh_ret[4], '1970-') === FALSE ?
294 make_local_datetime($qfh_ret[4], false) : __("Never");
295 $highlight_words = $qfh_ret[5];
296 $reply['first_id'] = $qfh_ret[6];
297 $reply['search_query'] = [$search, $search_language];
298
299 $vgroup_last_feed = $vgr_last_feed;
300
301 $reply['toolbar'] = $this->format_headline_subtoolbar($feed_site_url,
302 $feed_title,
303 $feed, $cat_view, $search,
304 $last_error, $last_updated);
305
306 if ($offset == 0) {
307 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HEADLINES_BEFORE) as $p) {
308 $reply['content'] .= $p->hook_headlines_before($feed, $cat_view, $qfh_ret);
309 }
310 }
311
312 $reply['content'] = '';
313
314 $headlines_count = 0;
315
316 $lnum = $offset;
317 $num_unread = 0;
318 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PS", $timing_info);
319 $expand_cdm = get_pref('CDM_EXPANDED');
320
321 if (is_object($result)) {
322
323 while ($line = $result->fetch()) {
324
325 ++$headlines_count;
326
327 $line["content_preview"] = "&mdash; " . truncate_string(strip_tags($line["content"]), 250);
328
329 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
330 $line = $p->hook_query_headlines($line, 250, false);
331 }
332
333 if (get_pref('SHOW_CONTENT_PREVIEW')) {
334 $content_preview = $line["content_preview"];
335 }
336
337 $id = $line["id"];
338 $feed_id = $line["feed_id"];
339 $label_cache = $line["label_cache"];
340 $labels = false;
341
342 if ($label_cache) {
343 $label_cache = json_decode($label_cache, true);
344
345 if ($label_cache) {
346 if ($label_cache["no-labels"] == 1)
347 $labels = array();
348 else
349 $labels = $label_cache;
350 }
351 }
352
353 if (!is_array($labels)) $labels = Article::get_article_labels($id);
354
355 $labels_str = "<span class=\"HLLCTR-$id\">";
356 $labels_str .= Article::format_article_labels($labels);
357 $labels_str .= "</span>";
358
359 if (count($topmost_article_ids) < 3) {
360 array_push($topmost_article_ids, $id);
361 }
362
363 $class = "";
364
365 if ($line["unread"]) {
366 $class .= " Unread";
367 ++$num_unread;
368 }
369
370 if ($line["marked"]) {
371 $marked_pic = "<img
372 src=\"images/mark_set.png\"
373 class=\"markedPic\" alt=\"Unstar article\"
374 onclick='toggleMark($id)'>";
375 $class .= " marked";
376 } else {
377 $marked_pic = "<img
378 src=\"images/mark_unset.png\"
379 class=\"markedPic\" alt=\"Star article\"
380 onclick='toggleMark($id)'>";
381 }
382
383 if ($line["published"]) {
384 $published_pic = "<img src=\"images/pub_set.png\"
385 class=\"pubPic\"
386 alt=\"Unpublish article\" onclick='togglePub($id)'>";
387 $class .= " published";
388 } else {
389 $published_pic = "<img src=\"images/pub_unset.png\"
390 class=\"pubPic\"
391 alt=\"Publish article\" onclick='togglePub($id)'>";
392 }
393
394 $updated_fmt = make_local_datetime($line["updated"], false, false, false, true);
395 $date_entered_fmt = T_sprintf("Imported at %s",
396 make_local_datetime($line["date_entered"], false));
397
398 $score = $line["score"];
399
400 $score_pic = "images/" . get_score_pic($score);
401
402 $score_pic = "<img class='hlScorePic' score='$score' onclick='changeScore($id, this)' src=\"$score_pic\"
403 title=\"$score\">";
404
405 if ($score > 500) {
406 $hlc_suffix = "high";
407 } else if ($score < -100) {
408 $hlc_suffix = "low";
409 } else {
410 $hlc_suffix = "";
411 }
412
413 $entry_author = $line["author"];
414
415 if ($entry_author) {
416 $entry_author = " &mdash; $entry_author";
417 }
418
419 $has_feed_icon = feeds::feedHasIcon($feed_id);
420
421 if ($has_feed_icon) {
422 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
423 } else {
424 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/pub_set.png\" alt=\"\">";
425 }
426
427 $entry_site_url = $line["site_url"];
428
429 //setting feed headline background color, needs to change text color based on dark/light
430 $fav_color = $line['favicon_avg_color'];
431
432 require_once "colors.php";
433
434 if ($fav_color && $fav_color != 'fail') {
435 if (!isset($rgba_cache[$feed_id])) {
436 $rgba_cache[$feed_id] = join(",", _color_unpack($fav_color));
437 }
438 }
439
440 if (!get_pref('COMBINED_DISPLAY_MODE')) {
441
442 if ($vfeed_group_enabled) {
443 if ($feed_id != $vgroup_last_feed && $line["feed_title"]) {
444
445 $vgroup_last_feed = $feed_id;
446
447 $vf_catchup_link = "<a class='catchup' onclick='catchupFeedInGroup($feed_id);' href='#'>".__('mark feed as read')."</a>";
448
449 $reply['content'] .= "<div data-feed-id='$feed_id' id='FTITLE-$feed_id' class='cdmFeedTitle'>".
450 "<div style='float : right'>$feed_icon_img</div>".
451 "<a class='title' href=\"#\" onclick=\"viewfeed({feed:$feed_id})\">".
452 $line["feed_title"]."</a>
453 $vf_catchup_link</div>";
454
455
456 }
457 }
458
459 $mouseover_attrs = "onmouseover='postMouseIn(event, $id)'
460 onmouseout='postMouseOut($id)'";
461
462 $reply['content'] .= "<div class='hl hlMenuAttach $class' data-orig-feed-id='$feed_id' data-article-id='$id' id='RROW-$id' $mouseover_attrs>";
463
464 $reply['content'] .= "<div class='hlLeft'>";
465
466 $reply['content'] .= "<input dojoType=\"dijit.form.CheckBox\"
467 type=\"checkbox\" onclick=\"toggleSelectRow2(this)\"
468 class='rchk'>";
469
470 $reply['content'] .= "$marked_pic";
471 $reply['content'] .= "$published_pic";
472
473 $reply['content'] .= "</div>";
474
475 $reply['content'] .= "<div onclick='return hlClicked(event, $id)'
476 class=\"hlTitle\"><span class='hlContent $hlc_suffix'>";
477 $reply['content'] .= "<a id=\"RTITLE-$id\" class=\"title $hlc_suffix\"
478 href=\"" . htmlspecialchars($line["link"]) . "\"
479 onclick=\"\">" .
480 truncate_string($line["title"], 200);
481
482 if (get_pref('SHOW_CONTENT_PREVIEW')) {
483 $reply['content'] .= "<span class=\"contentPreview\">" . $line["content_preview"] . "</span>";
484 }
485
486 $reply['content'] .= "</a></span>";
487
488 $reply['content'] .= $labels_str;
489
490 $reply['content'] .= "</div>";
491
492 if (!$vfeed_group_enabled) {
493 if (@$line["feed_title"]) {
494 $rgba = @$rgba_cache[$feed_id];
495
496 $reply['content'] .= "<span class=\"hlFeed\"><a style=\"background : rgba($rgba, 0.3)\" href=\"#\" onclick=\"viewfeed({feed:$feed_id})\">".
497 truncate_string($line["feed_title"],30)."</a></span>";
498 }
499 }
500
501
502 $reply['content'] .= "<span class=\"hlUpdated\">";
503
504 $reply['content'] .= "<div title='$date_entered_fmt'>$updated_fmt</div>
505 </span>";
506
507 $reply['content'] .= "<div class=\"hlRight\">";
508
509 $reply['content'] .= $score_pic;
510
511 if ($line["feed_title"] && !$vfeed_group_enabled) {
512
513 $reply['content'] .= "<span onclick=\"viewfeed({feed:$feed_id})\"
514 style=\"cursor : pointer\"
515 title=\"".htmlspecialchars($line['feed_title'])."\">
516 $feed_icon_img</span>";
517 }
518
519 $reply['content'] .= "</div>";
520 $reply['content'] .= "</div>";
521
522 } else {
523
524 if ($line["tag_cache"])
525 $tags = explode(",", $line["tag_cache"]);
526 else
527 $tags = false;
528
529 $line["content"] = sanitize($line["content"],
530 $line['hide_images'], false, $entry_site_url, $highlight_words, $line["id"]);
531
532 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_CDM) as $p) {
533 $line = $p->hook_render_article_cdm($line);
534 }
535
536 if ($vfeed_group_enabled && $line["feed_title"]) {
537 if ($feed_id != $vgroup_last_feed) {
538
539 $vgroup_last_feed = $feed_id;
540
541 $vf_catchup_link = "<a class='catchup' onclick='catchupFeedInGroup($feed_id);' href='#'>".__('mark feed as read')."</a>";
542
543 $feed_icon_src = Feeds::getFeedIcon($feed_id);
544 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"$feed_icon_src\">";
545
546 $reply['content'] .= "<div data-feed-id='$feed_id' id='FTITLE-$feed_id' class='cdmFeedTitle'>".
547 "<div style=\"float : right\">$feed_icon_img</div>".
548 "<a href=\"#\" class='title' onclick=\"viewfeed({feed:$feed_id})\">".
549 $line["feed_title"]."</a> $vf_catchup_link</div>";
550
551 }
552 }
553
554 $mouseover_attrs = "onmouseover='postMouseIn(event, $id)'
555 onmouseout='postMouseOut($id)'";
556
557 $expanded_class = $expand_cdm ? "expanded" : "expandable";
558
559 $tmp_content = "<div class=\"cdm $hlc_suffix $expanded_class $class\"
560 id=\"RROW-$id\" data-article-id='$id' data-orig-feed-id='$feed_id' $mouseover_attrs>";
561
562 $tmp_content .= "<div class=\"cdmHeader\">";
563 $tmp_content .= "<div style=\"vertical-align : middle\">";
564
565 $tmp_content .= "<input dojoType=\"dijit.form.CheckBox\"
566 type=\"checkbox\" onclick=\"toggleSelectRow2(this, false, true)\"
567 class='rchk'>";
568
569 $tmp_content .= "$marked_pic";
570 $tmp_content .= "$published_pic";
571
572 $tmp_content .= "</div>";
573
574 if ($highlight_words && count($highlight_words) > 0) {
575 foreach ($highlight_words as $word) {
576 $line["title"] = preg_replace("/(\Q$word\E)/i",
577 "<span class=\"highlight\">$1</span>", $line["title"]);
578 }
579 }
580
581 // data-article-id included for context menu
582 $tmp_content .= "<span id=\"RTITLE-$id\"
583 onclick=\"return cdmClicked(event, $id);\"
584 data-article-id=\"$id\"
585 class=\"titleWrap hlMenuAttach $hlc_suffix\">
586 <a class=\"title $hlc_suffix\"
587 title=\"".htmlspecialchars($line["title"])."\"
588 target=\"_blank\" rel=\"noopener noreferrer\" href=\"".
589 htmlspecialchars($line["link"])."\">".
590 $line["title"] .
591 "</a> <span class=\"author\">$entry_author</span>";
592
593 $tmp_content .= $labels_str;
594
595 $tmp_content .= "<span class='collapseBtn' style='display : none'>
596 <img src=\"images/collapse.png\" onclick=\"cdmCollapseArticle(event, $id)\"
597 title=\"".__("Collapse article")."\"/></span>";
598
599 if (!$expand_cdm)
600 $content_hidden = "style=\"display : none\"";
601 else
602 $excerpt_hidden = "style=\"display : none\"";
603
604 $tmp_content .= "<span $excerpt_hidden id=\"CEXC-$id\" class=\"cdmExcerpt\">" . $content_preview . "</span>";
605
606 $tmp_content .= "</span>";
607
608 if (!$vfeed_group_enabled) {
609 if (@$line["feed_title"]) {
610 $rgba = @$rgba_cache[$feed_id];
611
612 $tmp_content .= "<div class=\"hlFeed\">
613 <a href=\"#\" style=\"background-color: rgba($rgba,0.3)\"
614 onclick=\"viewfeed({feed:$feed_id})\">".
615 truncate_string($line["feed_title"],30)."</a>
616 </div>";
617 }
618 }
619
620 $tmp_content .= "<span class='updated' title='$date_entered_fmt'>$updated_fmt</span>";
621
622 $tmp_content .= "<div class='scoreWrap' style=\"vertical-align : middle\">";
623 $tmp_content .= "$score_pic";
624
625 if (!get_pref("VFEED_GROUP_BY_FEED") && $line["feed_title"]) {
626 $tmp_content .= "<span style=\"cursor : pointer\"
627 title=\"".htmlspecialchars($line["feed_title"])."\"
628 onclick=\"viewfeed({feed:$feed_id})\">$feed_icon_img</span>";
629 }
630 $tmp_content .= "</div>"; //scoreWrap
631
632 $tmp_content .= "</div>"; //cdmHeader
633
634 $tmp_content .= "<div class=\"cdmContent\" $content_hidden
635 onclick=\"return cdmClicked(event, $id, true);\"
636 id=\"CICD-$id\">";
637
638 $tmp_content .= "<div id=\"POSTNOTE-$id\">";
639 if ($line['note']) {
640 $tmp_content .= Article::format_article_note($id, $line['note']);
641 }
642 $tmp_content .= "</div>"; //POSTNOTE
643
644 if (!$line['lang']) $line['lang'] = 'en';
645
646 $tmp_content .= "<div class=\"cdmContentInner\" lang=\"".$line['lang']."\">";
647
648 if ($line["orig_feed_id"]) {
649
650 $ofgh = $this->pdo->prepare("SELECT * FROM ttrss_archived_feeds
651 WHERE id = ? AND owner_uid = ?");
652 $ofgh->execute([$line["orig_feed_id"], $_SESSION['uid']]);
653
654 if ($tmp_line = $ofgh->fetch()) {
655
656 $tmp_content .= "<div clear='both'>";
657 $tmp_content .= __("Originally from:");
658
659 $tmp_content .= "&nbsp;";
660
661 $tmp_content .= "<a target='_blank' rel='noopener noreferrer'
662 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
663 $tmp_line['title'] . "</a>";
664
665 $tmp_content .= "&nbsp;";
666
667 $tmp_content .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
668 $tmp_content .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_unset.png'></a>";
669
670 $tmp_content .= "</div>";
671 }
672 }
673
674 $tmp_content .= "<span id=\"CWRAP-$id\">";
675 $tmp_content .= "<span id=\"CENCW-$id\" class=\"cencw\" style=\"display : none\">";
676 $tmp_content .= htmlspecialchars($line["content"]);
677 $tmp_content .= "</span>";
678 $tmp_content .= "</span>";
679
680 $tmp_content .= "</div>"; //cdmContentInner
681
682 $tmp_content .= "<div class=\"cdmIntermediate\">";
683
684 $always_display_enclosures = $line["always_display_enclosures"];
685 $tmp_content .= Article::format_article_enclosures($id, $always_display_enclosures,
686 $line["content"], $line["hide_images"]);
687
688 $tmp_content .= "</div>"; // cdmIntermediate
689
690 $tmp_content .= "<div class=\"cdmFooter\" onclick=\"cdmFooterClick(event)\">";
691
692 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_LEFT_BUTTON) as $p) {
693 $tmp_content .= $p->hook_article_left_button($line);
694 }
695
696 $tags_str = Article::format_tags_string($tags, $id);
697
698 $tmp_content .= "<span class='left'>";
699
700 $tmp_content .= "<img src='images/tag.png' alt='Tags' title='Tags'>
701 <span id=\"ATSTR-$id\">$tags_str</span>
702 <a title=\"".__('Edit tags for this article')."\"
703 href=\"#\" onclick=\"editArticleTags($id)\">(+)</a>";
704
705 $num_comments = (int) $line["num_comments"];
706 $entry_comments = "";
707
708 if ($num_comments > 0) {
709 if ($line["comments"]) {
710 $comments_url = htmlspecialchars($line["comments"]);
711 } else {
712 $comments_url = htmlspecialchars($line["link"]);
713 }
714 $entry_comments = "<a class=\"postComments\"
715 target='_blank' rel='noopener noreferrer' href=\"$comments_url\">$num_comments ".
716 _ngettext("comment", "comments", $num_comments)."</a>";
717
718 } else {
719 if ($line["comments"] && $line["link"] != $line["comments"]) {
720 $entry_comments = "<a class=\"postComments\" target='_blank' rel='noopener noreferrer' href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
721 }
722 }
723
724 if ($entry_comments) $tmp_content .= "&nbsp;($entry_comments)";
725
726 $tmp_content .= "</span>";
727 $tmp_content .= "<div>";
728
729 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_BUTTON) as $p) {
730 $tmp_content .= $p->hook_article_button($line);
731 }
732
733 $tmp_content .= "</div>"; // buttons
734
735 $tmp_content .= "</div>"; // cdmFooter
736 $tmp_content .= "</div>"; // cdmContent
737 $tmp_content .= "</div>"; // RROW.cdm
738
739 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FORMAT_ARTICLE_CDM) as $p) {
740 $tmp_content = $p->hook_format_article_cdm($tmp_content, $line);
741 }
742
743 $reply['content'] .= $tmp_content;
744 }
745
746 ++$lnum;
747 }
748 }
749
750 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PE", $timing_info);
751
752 if (!$headlines_count) {
753
754 if (!is_numeric($result)) {
755
756 switch ($view_mode) {
757 case "unread":
758 $message = __("No unread articles found to display.");
759 break;
760 case "updated":
761 $message = __("No updated articles found to display.");
762 break;
763 case "marked":
764 $message = __("No starred articles found to display.");
765 break;
766 default:
767 if ($feed < LABEL_BASE_INDEX) {
768 $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.");
769 } else {
770 $message = __("No articles found to display.");
771 }
772 }
773
774 if (!$offset && $message) {
775 $reply['content'] = "<div class='whiteBox'>$message";
776
777 $reply['content'] .= "<p><span class=\"insensitive\">";
778
779 $sth = $this->pdo->prepare("SELECT " . SUBSTRING_FOR_DATE . "(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
780 WHERE owner_uid = ?");
781 $sth->execute([$_SESSION['uid']]);
782 $row = $sth->fetch();
783
784 $last_updated = make_local_datetime($row["last_updated"], false);
785
786 $reply['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
787
788 $sth = $this->pdo->prepare("SELECT COUNT(id) AS num_errors
789 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ?");
790 $sth->execute([$_SESSION['uid']]);
791 $row = $sth->fetch();
792
793 $num_errors = $row["num_errors"];
794
795 if ($num_errors > 0) {
796 $reply['content'] .= "<br/>";
797 $reply['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">" .
798 __('Some feeds have update errors (click for details)') . "</a>";
799 }
800 $reply['content'] .= "</span></p></div>";
801
802 }
803 } else if (is_numeric($result) && $result == -1) {
804 $reply['first_id_changed'] = true;
805 }
806 }
807
808 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H2", $timing_info);
809
810 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache,
811 $vgroup_last_feed, $reply);
812 }
813
814 function catchupAll() {
815 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
816 last_read = NOW(), unread = false WHERE unread = true AND owner_uid = ?");
817 $sth->execute([$_SESSION['uid']]);
818
819 CCache::zero_all($_SESSION["uid"]);
820 }
821
822 function view() {
823 $timing_info = microtime(true);
824
825 $reply = array();
826
827 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
828
829 $feed = $_REQUEST["feed"];
830 $method = $_REQUEST["m"];
831 $view_mode = $_REQUEST["view_mode"];
832 $limit = 30;
833 @$cat_view = $_REQUEST["cat"] == "true";
834 @$next_unread_feed = $_REQUEST["nuf"];
835 @$offset = $_REQUEST["skip"];
836 @$vgroup_last_feed = $_REQUEST["vgrlf"];
837 $order_by = $_REQUEST["order_by"];
838 $check_first_id = $_REQUEST["fid"];
839
840 if (is_numeric($feed)) $feed = (int) $feed;
841
842 /* Feed -5 is a special case: it is used to display auxiliary information
843 * when there's nothing to load - e.g. no stuff in fresh feed */
844
845 if ($feed == -5) {
846 print json_encode($this->generate_dashboard_feed());
847 return;
848 }
849
850 $sth = false;
851 if ($feed < LABEL_BASE_INDEX) {
852
853 $label_feed = Labels::feed_to_label_id($feed);
854
855 $sth = $this->pdo->prepare("SELECT id FROM ttrss_labels2 WHERE
856 id = ? AND owner_uid = ?");
857 $sth->execute([$label_feed, $_SESSION['uid']]);
858
859 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
860
861 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE
862 id = ? AND owner_uid = ?");
863 $sth->execute([$feed, $_SESSION['uid']]);
864
865 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
866
867 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories WHERE
868 id = ? AND owner_uid = ?");
869
870 $sth->execute([$feed, $_SESSION['uid']]);
871 }
872
873 if ($sth && !$sth->fetch()) {
874 print json_encode($this->generate_error_feed(__("Feed not found.")));
875 return;
876 }
877
878 /* Updating a label ccache means recalculating all of the caches
879 * so for performance reasons we don't do that here */
880
881 if ($feed >= 0) {
882 CCache::update($feed, $_SESSION["uid"], $cat_view);
883 }
884
885 set_pref("_DEFAULT_VIEW_MODE", $view_mode);
886 set_pref("_DEFAULT_VIEW_ORDER_BY", $order_by);
887
888 /* bump login timestamp if needed */
889 if (time() - $_SESSION["last_login_update"] > 3600) {
890 $sth = $this->pdo->prepare("UPDATE ttrss_users SET last_login = NOW() WHERE id = ?");
891 $sth->execute([$_SESSION['uid']]);
892
893 $_SESSION["last_login_update"] = time();
894 }
895
896 if (!$cat_view && is_numeric($feed) && $feed > 0) {
897 $sth = $this->pdo->prepare("UPDATE ttrss_feeds SET last_viewed = NOW()
898 WHERE id = ? AND owner_uid = ?");
899 $sth->execute([$feed, $_SESSION['uid']]);
900 }
901
902 $reply['headlines'] = array();
903
904 $override_order = false;
905 $skip_first_id_check = false;
906
907 switch ($order_by) {
908 case "title":
909 $override_order = "ttrss_entries.title, date_entered, updated";
910 break;
911 case "date_reverse":
912 $override_order = "score DESC, date_entered, updated";
913 $skip_first_id_check = true;
914 break;
915 case "feed_dates":
916 $override_order = "updated DESC";
917 break;
918 }
919
920 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
921
922 $ret = $this->format_headlines_list($feed, $method,
923 $view_mode, $limit, $cat_view, $offset,
924 $vgroup_last_feed, $override_order, true, $check_first_id, $skip_first_id_check);
925
926 //$topmost_article_ids = $ret[0];
927 $headlines_count = $ret[1];
928 /* $returned_feed = $ret[2]; */
929 $disable_cache = $ret[3];
930 $vgroup_last_feed = $ret[4];
931
932 //$reply['headlines']['content'] =& $ret[5]['content'];
933 //$reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
934
935 $reply['headlines'] = $ret[5];
936
937 if (!$next_unread_feed)
938 $reply['headlines']['id'] = $feed;
939 else
940 $reply['headlines']['id'] = $next_unread_feed;
941
942 $reply['headlines']['is_cat'] = (bool) $cat_view;
943
944 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
945
946 $reply['headlines-info'] = array("count" => (int) $headlines_count,
947 "vgroup_last_feed" => $vgroup_last_feed,
948 "disable_cache" => (bool) $disable_cache);
949
950 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
951
952 $reply['runtime-info'] = make_runtime_info();
953
954 print json_encode($reply);
955
956 }
957
958 private function generate_dashboard_feed() {
959 $reply = array();
960
961 $reply['headlines']['id'] = -5;
962 $reply['headlines']['is_cat'] = false;
963
964 $reply['headlines']['toolbar'] = '';
965
966 $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
967
968 $reply['headlines']['content'] .= "<p><span class=\"insensitive\">";
969
970 $sth = $this->pdo->prepare("SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
971 WHERE owner_uid = ?");
972 $sth->execute([$_SESSION['uid']]);
973 $row = $sth->fetch();
974
975 $last_updated = make_local_datetime($row["last_updated"], false);
976
977 $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
978
979 $sth = $this->pdo->prepare("SELECT COUNT(id) AS num_errors
980 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ?");
981 $sth->execute([$_SESSION['uid']]);
982 $row = $sth->fetch();
983
984 $num_errors = $row["num_errors"];
985
986 if ($num_errors > 0) {
987 $reply['headlines']['content'] .= "<br/>";
988 $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
989 __('Some feeds have update errors (click for details)')."</a>";
990 }
991 $reply['headlines']['content'] .= "</span></p>";
992
993 $reply['headlines-info'] = array("count" => 0,
994 "vgroup_last_feed" => '',
995 "unread" => 0,
996 "disable_cache" => true);
997
998 return $reply;
999 }
1000
1001 private function generate_error_feed($error) {
1002 $reply = array();
1003
1004 $reply['headlines']['id'] = -7;
1005 $reply['headlines']['is_cat'] = false;
1006
1007 $reply['headlines']['toolbar'] = '';
1008 $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
1009
1010 $reply['headlines-info'] = array("count" => 0,
1011 "vgroup_last_feed" => '',
1012 "unread" => 0,
1013 "disable_cache" => true);
1014
1015 return $reply;
1016 }
1017
1018 function quickAddFeed() {
1019 print_hidden("op", "rpc");
1020 print_hidden("method", "addfeed");
1021
1022 print "<div id='fadd_error_message' style='display : none' class='alert alert-danger'></div>";
1023
1024 print "<div id='fadd_multiple_notify' style='display : none'>";
1025 print_notice("Provided URL is a HTML page referencing multiple feeds, please select required feed from the dropdown menu below.");
1026 print "<p></div>";
1027
1028 print "<div class=\"dlgSec\">".__("Feed or site URL")."</div>";
1029 print "<div class=\"dlgSecCont\">";
1030
1031 print "<div style='float : right'>
1032 <img style='display : none'
1033 id='feed_add_spinner' src='images/indicator_white.gif'></div>";
1034
1035 print "<input style=\"font-size : 16px; width : 20em;\"
1036 placeHolder=\"".__("Feed or site URL")."\"
1037 dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"feed\" id=\"feedDlg_feedUrl\">";
1038
1039 print "<hr/>";
1040
1041 if (get_pref('ENABLE_FEED_CATS')) {
1042 print __('Place in category:') . " ";
1043 print_feed_cat_select("cat", false, 'dojoType="dijit.form.Select"');
1044 }
1045
1046 print "</div>";
1047
1048 print '<div id="feedDlg_feedsContainer" style="display : none">
1049
1050 <div class="dlgSec">' . __('Available feeds') . '</div>
1051 <div class="dlgSecCont">'.
1052 '<select id="feedDlg_feedContainerSelect"
1053 dojoType="dijit.form.Select" size="3">
1054 <script type="dojo/method" event="onChange" args="value">
1055 dijit.byId("feedDlg_feedUrl").attr("value", value);
1056 </script>
1057 </select>'.
1058 '</div></div>';
1059
1060 print "<div id='feedDlg_loginContainer' style='display : none'>
1061
1062 <div class=\"dlgSec\">".__("Authentication")."</div>
1063 <div class=\"dlgSecCont\">".
1064
1065 " <input dojoType=\"dijit.form.TextBox\" name='login'\"
1066 placeHolder=\"".__("Login")."\"
1067 autocomplete=\"new-password\"
1068 style=\"width : 10em;\"> ".
1069 " <input
1070 placeHolder=\"".__("Password")."\"
1071 dojoType=\"dijit.form.TextBox\" type='password'
1072 autocomplete=\"new-password\"
1073 style=\"width : 10em;\" name='pass'\">
1074 </div></div>";
1075
1076
1077 print "<div style=\"clear : both\">
1078 <input type=\"checkbox\" name=\"need_auth\" dojoType=\"dijit.form.CheckBox\" id=\"feedDlg_loginCheck\"
1079 onclick='checkboxToggleElement(this, \"feedDlg_loginContainer\")'>
1080 <label for=\"feedDlg_loginCheck\">".
1081 __('This feed requires authentication.')."</div>";
1082
1083 print "</form>";
1084
1085 print "<div class=\"dlgButtons\">
1086 <button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('feedAddDlg').execute()\">".__('Subscribe')."</button>";
1087
1088 if (!(defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER)) {
1089 print "<button dojoType=\"dijit.form.Button\" onclick=\"return feedBrowser()\">".__('More feeds')."</button>";
1090 }
1091
1092 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('feedAddDlg').hide()\">".__('Cancel')."</button>
1093 </div>";
1094
1095 //return;
1096 }
1097
1098 function feedBrowser() {
1099 if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER) return;
1100
1101 $browser_search = $_REQUEST["search"];
1102
1103 print_hidden("op", "rpc");
1104 print_hidden("method", "updateFeedBrowser");
1105
1106 print "<div dojoType=\"dijit.Toolbar\">
1107 <div style='float : right'>
1108 <img style='display : none'
1109 id='feed_browser_spinner' src='images/indicator_white.gif'>
1110 <input name=\"search\" dojoType=\"dijit.form.TextBox\" size=\"20\" type=\"search\"
1111 onchange=\"dijit.byId('feedBrowserDlg').update()\" value=\"$browser_search\">
1112 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').update()\">".__('Search')."</button>
1113 </div>";
1114
1115 print " <select name=\"mode\" dojoType=\"dijit.form.Select\" onchange=\"dijit.byId('feedBrowserDlg').update()\">
1116 <option value='1'>" . __('Popular feeds') . "</option>
1117 <option value='2'>" . __('Feed archive') . "</option>
1118 </select> ";
1119
1120 print __("limit:");
1121
1122 print " <select dojoType=\"dijit.form.Select\" name=\"limit\" onchange=\"dijit.byId('feedBrowserDlg').update()\">";
1123
1124 foreach (array(25, 50, 100, 200) as $l) {
1125 //$issel = ($l == $limit) ? "selected=\"1\"" : "";
1126 print "<option value=\"$l\">$l</option>";
1127 }
1128
1129 print "</select> ";
1130
1131 print "</div>";
1132
1133 require_once "feedbrowser.php";
1134
1135 print "<ul class='browseFeedList' id='browseFeedList'>";
1136 print make_feed_browser("", 25);
1137 print "</ul>";
1138
1139 print "<div align='center'>
1140 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').execute()\">".__('Subscribe')."</button>
1141 <button dojoType=\"dijit.form.Button\" style='display : none' id='feed_archive_remove' onclick=\"dijit.byId('feedBrowserDlg').removeFromArchive()\">".__('Remove')."</button>
1142 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').hide()\" >".__('Cancel')."</button></div>";
1143
1144 }
1145
1146 function search() {
1147 $this->params = explode(":", $_REQUEST["param"], 2);
1148
1149 $active_feed_id = sprintf("%d", $this->params[0]);
1150 $is_cat = $this->params[1] != "false";
1151
1152 print "<div class=\"dlgSec\">".__('Look for')."</div>";
1153
1154 print "<div class=\"dlgSecCont\">";
1155
1156 print "<input dojoType=\"dijit.form.ValidationTextBox\"
1157 style=\"font-size : 16px; width : 20em;\"
1158 required=\"1\" name=\"query\" type=\"search\" value=''>";
1159
1160 print "<hr/><span style='float : right'>".T_sprintf('in %s', $this->getFeedTitle($active_feed_id, $is_cat))."</span>";
1161
1162 if (DB_TYPE == "pgsql") {
1163 print "<hr/>";
1164 print_select("search_language", "", Pref_Feeds::$feed_languages,
1165 "dojoType='dijit.form.Select' title=\"".__('Used for word stemming')."\"");
1166 }
1167
1168 print "</div>";
1169
1170 print "<div class=\"dlgButtons\">";
1171
1172 if (count(PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEARCH)) == 0) {
1173 print "<div style=\"float : left\">
1174 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/SearchSyntax\">".__("Search syntax")."</a>
1175 </div>";
1176 }
1177
1178 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('searchDlg').execute()\">".__('Search')."</button>
1179 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('searchDlg').hide()\">".__('Cancel')."</button>
1180 </div>";
1181 }
1182
1183 function update_debugger() {
1184 header("Content-type: text/html");
1185
1186 $feed_id = (int)$_REQUEST["feed_id"];
1187 @$do_update = $_REQUEST["action"] == "do_update";
1188 $csrf_token = $_REQUEST["csrf_token"];
1189
1190 $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
1191 $sth->execute([$feed_id, $_SESSION['uid']]);
1192
1193 if (!$sth->fetch()) {
1194 print "Access denied.";
1195 return;
1196 }
1197
1198 $refetch_checked = isset($_REQUEST["force_refetch"]) ? "checked" : "";
1199 $rehash_checked = isset($_REQUEST["force_rehash"]) ? "checked" : "";
1200
1201 ?>
1202 <html>
1203 <head>
1204 <?php echo stylesheet_tag("css/default.css") ?>
1205 <title>Feed Debugger</title>
1206 </head>
1207 <body class="small_margins ttrss_utility claro">
1208 <h1>Feed Debugger: <?php echo "$feed_id: " . $this->getFeedTitle($feed_id) ?></h1>
1209 <form method="GET" action="">
1210 <input type="hidden" name="op" value="feeds">
1211 <input type="hidden" name="method" value="update_debugger">
1212 <input type="hidden" name="xdebug" value="1">
1213 <input type="hidden" name="csrf_token" value="<?php echo $csrf_token ?>">
1214 <input type="hidden" name="action" value="do_update">
1215 <input type="hidden" name="feed_id" value="<?php echo $feed_id ?>">
1216 <input type="checkbox" name="force_refetch" value="1" <?php echo $refetch_checked ?>> Force refetch<br/>
1217 <input type="checkbox" name="force_rehash" value="1" <?php echo $rehash_checked ?>> Force rehash<br/>
1218
1219 <p/><button type="submit">Continue</button>
1220 </form>
1221
1222 <hr>
1223
1224 <pre><?php
1225
1226 if ($do_update) {
1227 RSSUtils::update_rss_feed($feed_id, true);
1228 }
1229
1230 ?></pre>
1231
1232 </body>
1233 </html>
1234 <?php
1235
1236 }
1237
1238 static function catchup_feed($feed, $cat_view, $owner_uid = false, $mode = 'all', $search = false) {
1239
1240 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
1241
1242 $pdo = Db::pdo();
1243
1244 // Todo: all this interval stuff needs some generic generator function
1245
1246 $search_qpart = is_array($search) && $search[0] ? search_to_sql($search[0], $search[1])[0] : 'true';
1247
1248 switch ($mode) {
1249 case "1day":
1250 if (DB_TYPE == "pgsql") {
1251 $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
1252 } else {
1253 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
1254 }
1255 break;
1256 case "1week":
1257 if (DB_TYPE == "pgsql") {
1258 $date_qpart = "date_entered < NOW() - INTERVAL '1 week' ";
1259 } else {
1260 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) ";
1261 }
1262 break;
1263 case "2week":
1264 if (DB_TYPE == "pgsql") {
1265 $date_qpart = "date_entered < NOW() - INTERVAL '2 week' ";
1266 } else {
1267 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) ";
1268 }
1269 break;
1270 default:
1271 $date_qpart = "true";
1272 }
1273
1274 if (is_numeric($feed)) {
1275 if ($cat_view) {
1276
1277 if ($feed >= 0) {
1278
1279 if ($feed > 0) {
1280 $children = Feeds::getChildCategories($feed, $owner_uid);
1281 array_push($children, $feed);
1282
1283 $children = join(",", $children);
1284
1285 $cat_qpart = "cat_id IN ($children)";
1286 } else {
1287 $cat_qpart = "cat_id IS NULL";
1288 }
1289
1290 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1291 SET unread = false, last_read = NOW() WHERE ref_id IN
1292 (SELECT id FROM
1293 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1294 AND owner_uid = ? AND unread = true AND feed_id IN
1295 (SELECT id FROM ttrss_feeds WHERE $cat_qpart) AND $date_qpart AND $search_qpart) as tmp)");
1296 $sth->execute([$owner_uid]);
1297
1298 } else if ($feed == -2) {
1299
1300 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1301 SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
1302 FROM ttrss_user_labels2, ttrss_entries WHERE article_id = ref_id AND id = ref_id AND $date_qpart AND $search_qpart) > 0
1303 AND unread = true AND owner_uid = ?");
1304 $sth->execute([$owner_uid]);
1305 }
1306
1307 } else if ($feed > 0) {
1308
1309 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1310 SET unread = false, last_read = NOW() WHERE ref_id IN
1311 (SELECT id FROM
1312 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1313 AND owner_uid = ? AND unread = true AND feed_id = ? AND $date_qpart AND $search_qpart) as tmp)");
1314 $sth->execute([$owner_uid, $feed]);
1315
1316 } else if ($feed < 0 && $feed > LABEL_BASE_INDEX) { // special, like starred
1317
1318 if ($feed == -1) {
1319 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1320 SET unread = false, last_read = NOW() WHERE ref_id IN
1321 (SELECT id FROM
1322 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1323 AND owner_uid = ? AND unread = true AND marked = true AND $date_qpart AND $search_qpart) as tmp)");
1324 $sth->execute([$owner_uid]);
1325 }
1326
1327 if ($feed == -2) {
1328 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1329 SET unread = false, last_read = NOW() WHERE ref_id IN
1330 (SELECT id FROM
1331 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1332 AND owner_uid = ? AND unread = true AND published = true AND $date_qpart AND $search_qpart) as tmp)");
1333 $sth->execute([$owner_uid]);
1334 }
1335
1336 if ($feed == -3) {
1337
1338 $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE");
1339
1340 if (DB_TYPE == "pgsql") {
1341 $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
1342 } else {
1343 $match_part = "date_entered > DATE_SUB(NOW(),
1344 INTERVAL $intl HOUR) ";
1345 }
1346
1347 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1348 SET unread = false, last_read = NOW() WHERE ref_id IN
1349 (SELECT id FROM
1350 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1351 AND owner_uid = ? AND score >= 0 AND unread = true AND $date_qpart AND $match_part AND $search_qpart) as tmp)");
1352 $sth->execute([$owner_uid]);
1353 }
1354
1355 if ($feed == -4) {
1356 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1357 SET unread = false, last_read = NOW() WHERE ref_id IN
1358 (SELECT id FROM
1359 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1360 AND owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1361 $sth->execute([$owner_uid]);
1362 }
1363
1364 } else if ($feed < LABEL_BASE_INDEX) { // label
1365
1366 $label_id = Labels::feed_to_label_id($feed);
1367
1368 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1369 SET unread = false, last_read = NOW() WHERE ref_id IN
1370 (SELECT id FROM
1371 (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_user_labels2 WHERE ref_id = id
1372 AND label_id = ? AND ref_id = article_id
1373 AND owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1374 $sth->execute([$label_id, $owner_uid]);
1375
1376 }
1377
1378 CCache::update($feed, $owner_uid, $cat_view);
1379
1380 } else { // tag
1381 $sth = $pdo->prepare("UPDATE ttrss_user_entries
1382 SET unread = false, last_read = NOW() WHERE ref_id IN
1383 (SELECT id FROM
1384 (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_tags WHERE ref_id = ttrss_entries.id
1385 AND post_int_id = int_id AND tag_name = ?
1386 AND ttrss_user_entries.owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1387 $sth->execute([$feed, $owner_uid]);
1388
1389 }
1390 }
1391
1392 static function getFeedArticles($feed, $is_cat = false, $unread_only = false,
1393 $owner_uid = false) {
1394
1395 $n_feed = (int) $feed;
1396 $need_entries = false;
1397
1398 $pdo = Db::pdo();
1399
1400 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1401
1402 if ($unread_only) {
1403 $unread_qpart = "unread = true";
1404 } else {
1405 $unread_qpart = "true";
1406 }
1407
1408 $match_part = "";
1409
1410 if ($is_cat) {
1411 return Feeds::getCategoryUnread($n_feed, $owner_uid);
1412 } else if ($n_feed == -6) {
1413 return 0;
1414 } else if ($feed != "0" && $n_feed == 0) {
1415
1416 $sth = $pdo->prepare("SELECT SUM((SELECT COUNT(int_id)
1417 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
1418 AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags
1419 WHERE owner_uid = ? AND tag_name = ?");
1420
1421 $sth->execute([$owner_uid, $feed]);
1422 $row = $sth->fetch();
1423
1424 return $row["count"];
1425
1426 } else if ($n_feed == -1) {
1427 $match_part = "marked = true";
1428 } else if ($n_feed == -2) {
1429 $match_part = "published = true";
1430 } else if ($n_feed == -3) {
1431 $match_part = "unread = true AND score >= 0";
1432
1433 $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
1434
1435 if (DB_TYPE == "pgsql") {
1436 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1437 } else {
1438 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
1439 }
1440
1441 $need_entries = true;
1442
1443 } else if ($n_feed == -4) {
1444 $match_part = "true";
1445 } else if ($n_feed >= 0) {
1446
1447 if ($n_feed != 0) {
1448 $match_part = "feed_id = '$n_feed'";
1449 } else {
1450 $match_part = "feed_id IS NULL";
1451 }
1452
1453 } else if ($feed < LABEL_BASE_INDEX) {
1454
1455 $label_id = Labels::feed_to_label_id($feed);
1456
1457 return Feeds::getLabelUnread($label_id, $owner_uid);
1458 }
1459
1460 if ($match_part) {
1461
1462 if ($need_entries) {
1463 $from_qpart = "ttrss_user_entries,ttrss_entries";
1464 $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND";
1465 } else {
1466 $from_qpart = "ttrss_user_entries";
1467 $from_where = "";
1468 }
1469
1470 $sth = $pdo->prepare("SELECT count(int_id) AS unread
1471 FROM $from_qpart WHERE
1472 $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = ?");
1473 $sth->execute([$owner_uid]);
1474 $row = $sth->fetch();
1475
1476 return $row["unread"];
1477
1478 } else {
1479
1480 $sth = $pdo->prepare("SELECT COUNT(post_int_id) AS unread
1481 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
1482 WHERE tag_name = ? AND post_int_id = int_id AND ref_id = ttrss_entries.id
1483 AND $unread_qpart AND ttrss_tags.owner_uid = ,");
1484
1485 $sth->execute([$feed, $owner_uid]);
1486 $row = $sth->fetch();
1487
1488 return $row["unread"];
1489 }
1490 }
1491
1492 /**
1493 * @return array (code => Status code, message => error message if available)
1494 *
1495 * 0 - OK, Feed already exists
1496 * 1 - OK, Feed added
1497 * 2 - Invalid URL
1498 * 3 - URL content is HTML, no feeds available
1499 * 4 - URL content is HTML which contains multiple feeds.
1500 * Here you should call extractfeedurls in rpc-backend
1501 * to get all possible feeds.
1502 * 5 - Couldn't download the URL content.
1503 * 6 - Content is an invalid XML.
1504 */
1505 static function subscribe_to_feed($url, $cat_id = 0,
1506 $auth_login = '', $auth_pass = '') {
1507
1508 global $fetch_last_error;
1509 global $fetch_last_error_content;
1510
1511 $pdo = Db::pdo();
1512
1513 $url = fix_url($url);
1514
1515 if (!$url || !validate_feed_url($url)) return array("code" => 2);
1516
1517 $contents = @fetch_file_contents($url, false, $auth_login, $auth_pass);
1518
1519 if (!$contents) {
1520 if (preg_match("/cloudflare\.com/", $fetch_last_error_content)) {
1521 $fetch_last_error .= " (feed behind Cloudflare)";
1522 }
1523
1524 return array("code" => 5, "message" => $fetch_last_error);
1525 }
1526
1527 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SUBSCRIBE_FEED) as $plugin) {
1528 $contents = $plugin->hook_subscribe_feed($contents, $url, $auth_login, $auth_pass);
1529 }
1530
1531 if (is_html($contents)) {
1532 $feedUrls = get_feeds_from_html($url, $contents);
1533
1534 if (count($feedUrls) == 0) {
1535 return array("code" => 3);
1536 } else if (count($feedUrls) > 1) {
1537 return array("code" => 4, "feeds" => $feedUrls);
1538 }
1539 //use feed url as new URL
1540 $url = key($feedUrls);
1541 }
1542
1543 if (!$cat_id) $cat_id = null;
1544
1545 $sth = $pdo->prepare("SELECT id FROM ttrss_feeds
1546 WHERE feed_url = ? AND owner_uid = ?");
1547 $sth->execute([$url, $_SESSION['uid']]);
1548
1549 if ($row = $sth->fetch()) {
1550 return array("code" => 0, "feed_id" => (int) $row["id"]);
1551 } else {
1552 $sth = $pdo->prepare(
1553 "INSERT INTO ttrss_feeds
1554 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted)
1555 VALUES (?, ?, ?, ?, ?, ?, 0, false)");
1556
1557 $sth->execute([$_SESSION['uid'], $url, "[Unknown]", $cat_id, $auth_login, $auth_pass]);
1558
1559 $sth = $pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
1560 AND owner_uid = ?");
1561 $sth->execute([$url, $_SESSION['uid']]);
1562 $row = $sth->fetch();
1563
1564 $feed_id = $row["id"];
1565
1566 if ($feed_id) {
1567 RSSUtils::set_basic_feed_info($feed_id);
1568 }
1569
1570 return array("code" => 1, "feed_id" => (int) $feed_id);
1571
1572 }
1573 }
1574
1575 static function getIconFile($feed_id) {
1576 return ICONS_DIR . "/$feed_id.ico";
1577 }
1578
1579 static function feedHasIcon($id) {
1580 return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
1581 }
1582
1583 static function getFeedIcon($id) {
1584 switch ($id) {
1585 case 0:
1586 return "images/archive.png";
1587 break;
1588 case -1:
1589 return "images/star.png";
1590 break;
1591 case -2:
1592 return "images/feed.png";
1593 break;
1594 case -3:
1595 return "images/fresh.png";
1596 break;
1597 case -4:
1598 return "images/folder.png";
1599 break;
1600 case -6:
1601 return "images/time.png";
1602 break;
1603 default:
1604 if ($id < LABEL_BASE_INDEX) {
1605 return "images/label.png";
1606 } else {
1607 $icon = self::getIconFile($id);
1608
1609 if ($icon && file_exists($icon)) {
1610 return ICONS_URL . "/" . basename($icon) . "?" . filemtime($icon);
1611 }
1612 }
1613 break;
1614 }
1615
1616 return false;
1617 }
1618
1619 static function getFeedTitle($id, $cat = false) {
1620 $pdo = Db::pdo();
1621
1622 if ($cat) {
1623 return Feeds::getCategoryTitle($id);
1624 } else if ($id == -1) {
1625 return __("Starred articles");
1626 } else if ($id == -2) {
1627 return __("Published articles");
1628 } else if ($id == -3) {
1629 return __("Fresh articles");
1630 } else if ($id == -4) {
1631 return __("All articles");
1632 } else if ($id === 0 || $id === "0") {
1633 return __("Archived articles");
1634 } else if ($id == -6) {
1635 return __("Recently read");
1636 } else if ($id < LABEL_BASE_INDEX) {
1637
1638 $label_id = Labels::feed_to_label_id($id);
1639
1640 $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 WHERE id = ?");
1641 $sth->execute([$label_id]);
1642
1643 if ($row = $sth->fetch()) {
1644 return $row["caption"];
1645 } else {
1646 return "Unknown label ($label_id)";
1647 }
1648
1649 } else if (is_numeric($id) && $id > 0) {
1650
1651 $sth = $pdo->prepare("SELECT title FROM ttrss_feeds WHERE id = ?");
1652 $sth->execute([$id]);
1653
1654 if ($row = $sth->fetch()) {
1655 return $row["title"];
1656 } else {
1657 return "Unknown feed ($id)";
1658 }
1659
1660 } else {
1661 return $id;
1662 }
1663 }
1664
1665 static function getCategoryUnread($cat, $owner_uid = false) {
1666
1667 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1668
1669 $pdo = Db::pdo();
1670
1671 if ($cat >= 0) {
1672
1673 if (!$cat) $cat = null;
1674
1675 $sth = $pdo->prepare("SELECT id FROM ttrss_feeds
1676 WHERE (cat_id = :cat OR (:cat IS NULL AND cat_id IS NULL))
1677 AND owner_uid = :uid");
1678
1679 $sth->execute([":cat" => $cat, ":uid" => $owner_uid]);
1680
1681 $cat_feeds = array();
1682 while ($line = $sth->fetch()) {
1683 array_push($cat_feeds, "feed_id = " . $line["id"]);
1684 }
1685
1686 if (count($cat_feeds) == 0) return 0;
1687
1688 $match_part = implode(" OR ", $cat_feeds);
1689
1690 $sth = $pdo->prepare("SELECT COUNT(int_id) AS unread
1691 FROM ttrss_user_entries
1692 WHERE unread = true AND ($match_part)
1693 AND owner_uid = ?");
1694 $sth->execute([$owner_uid]);
1695
1696 $unread = 0;
1697
1698 # this needs to be rewritten
1699 while ($line = $sth->fetch()) {
1700 $unread += $line["unread"];
1701 }
1702
1703 return $unread;
1704 } else if ($cat == -1) {
1705 return getFeedUnread(-1) + getFeedUnread(-2) + getFeedUnread(-3) + getFeedUnread(0);
1706 } else if ($cat == -2) {
1707
1708 $sth = $pdo->prepare("SELECT COUNT(unread) AS unread FROM
1709 ttrss_user_entries, ttrss_user_labels2
1710 WHERE article_id = ref_id AND unread = true
1711 AND ttrss_user_entries.owner_uid = ?");
1712 $sth->execute([$owner_uid]);
1713 $row = $sth->fetch();
1714
1715 return $row["unread"];
1716 }
1717 }
1718
1719 // only accepts real cats (>= 0)
1720 static function getCategoryChildrenUnread($cat, $owner_uid = false) {
1721 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1722
1723 $pdo = Db::pdo();
1724
1725 $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories WHERE parent_cat = ?
1726 AND owner_uid = ?");
1727 $sth->execute([$cat, $owner_uid]);
1728
1729 $unread = 0;
1730
1731 while ($line = $sth->fetch()) {
1732 $unread += Feeds::getCategoryUnread($line["id"], $owner_uid);
1733 $unread += Feeds::getCategoryChildrenUnread($line["id"], $owner_uid);
1734 }
1735
1736 return $unread;
1737 }
1738
1739 static function getGlobalUnread($user_id = false) {
1740
1741 if (!$user_id) $user_id = $_SESSION["uid"];
1742
1743 $pdo = Db::pdo();
1744
1745 $sth = $pdo->prepare("SELECT SUM(value) AS c_id FROM ttrss_counters_cache
1746 WHERE owner_uid = ? AND feed_id > 0");
1747 $sth->execute([$user_id]);
1748 $row = $sth->fetch();
1749
1750 return $row["c_id"];
1751 }
1752
1753 static function getCategoryTitle($cat_id) {
1754
1755 if ($cat_id == -1) {
1756 return __("Special");
1757 } else if ($cat_id == -2) {
1758 return __("Labels");
1759 } else {
1760
1761 $pdo = Db::pdo();
1762
1763 $sth = $pdo->prepare("SELECT title FROM ttrss_feed_categories WHERE
1764 id = ?");
1765 $sth->execute([$cat_id]);
1766
1767 if ($row = $sth->fetch()) {
1768 return $row["title"];
1769 } else {
1770 return __("Uncategorized");
1771 }
1772 }
1773 }
1774
1775 static function getLabelUnread($label_id, $owner_uid = false) {
1776 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1777
1778 $pdo = Db::pdo();
1779
1780 $sth = $pdo->prepare("SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2
1781 WHERE owner_uid = ? AND unread = true AND label_id = ? AND article_id = ref_id");
1782
1783 $sth->execute([$owner_uid, $label_id]);
1784
1785 if ($row = $sth->fetch()) {
1786 return $row["unread"];
1787 } else {
1788 return 0;
1789 }
1790 }
1791
1792 static function queryFeedHeadlines($params) {
1793
1794 $pdo = Db::pdo();
1795
1796 // WARNING: due to highly dynamic nature of this query its going to quote parameters
1797 // right before adding them to SQL part
1798
1799 $feed = $params["feed"];
1800 $limit = isset($params["limit"]) ? $params["limit"] : 30;
1801 $view_mode = $params["view_mode"];
1802 $cat_view = isset($params["cat_view"]) ? $params["cat_view"] : false;
1803 $search = isset($params["search"]) ? $params["search"] : false;
1804 $search_language = isset($params["search_language"]) ? $params["search_language"] : "";
1805 $override_order = isset($params["override_order"]) ? $params["override_order"] : false;
1806 $offset = isset($params["offset"]) ? $params["offset"] : 0;
1807 $owner_uid = isset($params["owner_uid"]) ? $params["owner_uid"] : $_SESSION["uid"];
1808 $since_id = isset($params["since_id"]) ? $params["since_id"] : 0;
1809 $include_children = isset($params["include_children"]) ? $params["include_children"] : false;
1810 $ignore_vfeed_group = isset($params["ignore_vfeed_group"]) ? $params["ignore_vfeed_group"] : false;
1811 $override_strategy = isset($params["override_strategy"]) ? $params["override_strategy"] : false;
1812 $override_vfeed = isset($params["override_vfeed"]) ? $params["override_vfeed"] : false;
1813 $start_ts = isset($params["start_ts"]) ? $params["start_ts"] : false;
1814 $check_first_id = isset($params["check_first_id"]) ? $params["check_first_id"] : false;
1815 $skip_first_id_check = isset($params["skip_first_id_check"]) ? $params["skip_first_id_check"] : false;
1816
1817 $ext_tables_part = "";
1818 $limit_query_part = "";
1819
1820 $search_words = array();
1821
1822 if ($search) {
1823 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEARCH) as $plugin) {
1824 list($search_query_part, $search_words) = $plugin->hook_search($search);
1825 break;
1826 }
1827
1828 // fall back in case of no plugins
1829 if (!$search_query_part) {
1830 list($search_query_part, $search_words) = search_to_sql($search, $search_language);
1831 }
1832 $search_query_part .= " AND ";
1833 } else {
1834 $search_query_part = "";
1835 }
1836
1837 if ($since_id) {
1838 $since_id_part = "ttrss_entries.id > ".$pdo->quote($since_id)." AND ";
1839 } else {
1840 $since_id_part = "";
1841 }
1842
1843 $view_query_part = "";
1844
1845 if ($view_mode == "adaptive") {
1846 if ($search) {
1847 $view_query_part = " ";
1848 } else if ($feed != -1) {
1849
1850 $unread = getFeedUnread($feed, $cat_view);
1851
1852 if ($cat_view && $feed > 0 && $include_children)
1853 $unread += Feeds::getCategoryChildrenUnread($feed);
1854
1855 if ($unread > 0) {
1856 $view_query_part = " unread = true AND ";
1857 }
1858 }
1859 }
1860
1861 if ($view_mode == "marked") {
1862 $view_query_part = " marked = true AND ";
1863 }
1864
1865 if ($view_mode == "has_note") {
1866 $view_query_part = " (note IS NOT NULL AND note != '') AND ";
1867 }
1868
1869 if ($view_mode == "published") {
1870 $view_query_part = " published = true AND ";
1871 }
1872
1873 if ($view_mode == "unread" && $feed != -6) {
1874 $view_query_part = " unread = true AND ";
1875 }
1876
1877 if ($limit > 0) {
1878 $limit_query_part = "LIMIT " . (int)$limit;
1879 }
1880
1881 $allow_archived = false;
1882
1883 $vfeed_query_part = "";
1884
1885 /* tags */
1886 if (!is_numeric($feed)) {
1887 $query_strategy_part = "true";
1888 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
1889 id = feed_id) as feed_title,";
1890 } else if ($feed > 0) {
1891
1892 if ($cat_view) {
1893
1894 if ($feed > 0) {
1895 if ($include_children) {
1896 # sub-cats
1897 $subcats = Feeds::getChildCategories($feed, $owner_uid);
1898
1899 array_push($subcats, $feed);
1900 $query_strategy_part = "cat_id IN (".
1901 implode(",", $subcats).")";
1902
1903 } else {
1904 $query_strategy_part = "cat_id = " . $pdo->quote($feed);
1905 }
1906
1907 } else {
1908 $query_strategy_part = "cat_id IS NULL";
1909 }
1910
1911 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1912
1913 } else {
1914 $query_strategy_part = "feed_id = " . $pdo->quote($feed);
1915 }
1916 } else if ($feed == 0 && !$cat_view) { // archive virtual feed
1917 $query_strategy_part = "feed_id IS NULL";
1918 $allow_archived = true;
1919 } else if ($feed == 0 && $cat_view) { // uncategorized
1920 $query_strategy_part = "cat_id IS NULL AND feed_id IS NOT NULL";
1921 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1922 } else if ($feed == -1) { // starred virtual feed
1923 $query_strategy_part = "marked = true";
1924 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1925 $allow_archived = true;
1926
1927 if (!$override_order) {
1928 $override_order = "last_marked DESC, date_entered DESC, updated DESC";
1929 }
1930
1931 } else if ($feed == -2) { // published virtual feed OR labels category
1932
1933 if (!$cat_view) {
1934 $query_strategy_part = "published = true";
1935 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1936 $allow_archived = true;
1937
1938 if (!$override_order) {
1939 $override_order = "last_published DESC, date_entered DESC, updated DESC";
1940 }
1941
1942 } else {
1943 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1944
1945 $ext_tables_part = "ttrss_labels2,ttrss_user_labels2,";
1946
1947 $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
1948 ttrss_user_labels2.article_id = ref_id";
1949
1950 }
1951 } else if ($feed == -6) { // recently read
1952 $query_strategy_part = "unread = false AND last_read IS NOT NULL";
1953
1954 if (DB_TYPE == "pgsql") {
1955 $query_strategy_part .= " AND last_read > NOW() - INTERVAL '1 DAY' ";
1956 } else {
1957 $query_strategy_part .= " AND last_read > DATE_SUB(NOW(), INTERVAL 1 DAY) ";
1958 }
1959
1960 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1961 $allow_archived = true;
1962 $ignore_vfeed_group = true;
1963
1964 if (!$override_order) $override_order = "last_read DESC";
1965
1966 } else if ($feed == -3) { // fresh virtual feed
1967 $query_strategy_part = "unread = true AND score >= 0";
1968
1969 $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
1970
1971 if (DB_TYPE == "pgsql") {
1972 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1973 } else {
1974 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
1975 }
1976
1977 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1978 } else if ($feed == -4) { // all articles virtual feed
1979 $allow_archived = true;
1980 $query_strategy_part = "true";
1981 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1982 } else if ($feed <= LABEL_BASE_INDEX) { // labels
1983 $label_id = Labels::feed_to_label_id($feed);
1984
1985 $query_strategy_part = "label_id = ".$pdo->quote($label_id)." AND
1986 ttrss_labels2.id = ttrss_user_labels2.label_id AND
1987 ttrss_user_labels2.article_id = ref_id";
1988
1989 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
1990 $ext_tables_part = "ttrss_labels2,ttrss_user_labels2,";
1991 $allow_archived = true;
1992
1993 } else {
1994 $query_strategy_part = "true";
1995 }
1996
1997 $order_by = "score DESC, date_entered DESC, updated DESC";
1998
1999 if ($override_order) {
2000 $order_by = $override_order;
2001 }
2002
2003 if ($override_strategy) {
2004 $query_strategy_part = $override_strategy;
2005 }
2006
2007 if ($override_vfeed) {
2008 $vfeed_query_part = $override_vfeed;
2009 }
2010
2011 if ($search) {
2012 $feed_title = T_sprintf("Search results: %s", $search);
2013 } else {
2014 if ($cat_view) {
2015 $feed_title = Feeds::getCategoryTitle($feed);
2016 } else {
2017 if (is_numeric($feed) && $feed > 0) {
2018 $ssth = $pdo->prepare("SELECT title,site_url,last_error,last_updated
2019 FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
2020 $ssth->execute([$feed, $owner_uid]);
2021 $row = $ssth->fetch();
2022
2023 $feed_title = $row["title"];
2024 $feed_site_url = $row["site_url"];
2025 $last_error = $row["last_error"];
2026 $last_updated = $row["last_updated"];
2027 } else {
2028 $feed_title = Feeds::getFeedTitle($feed);
2029 }
2030 }
2031 }
2032
2033 $content_query_part = "content, ";
2034
2035 if ($limit_query_part) {
2036 $offset_query_part = "OFFSET " . (int)$offset;
2037 } else {
2038 $offset_query_part = "";
2039 }
2040
2041 if (is_numeric($feed)) {
2042 // proper override_order applied above
2043 if ($vfeed_query_part && !$ignore_vfeed_group && get_pref('VFEED_GROUP_BY_FEED', $owner_uid)) {
2044 if (!$override_order) {
2045 $order_by = "ttrss_feeds.title, ".$order_by;
2046 } else {
2047 $order_by = "ttrss_feeds.title, ".$override_order;
2048 }
2049 }
2050
2051 if (!$allow_archived) {
2052 $from_qpart = "${ext_tables_part}ttrss_entries LEFT JOIN ttrss_user_entries ON (ref_id = ttrss_entries.id),ttrss_feeds";
2053 $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
2054
2055 } else {
2056 $from_qpart = "${ext_tables_part}ttrss_entries LEFT JOIN ttrss_user_entries ON (ref_id = ttrss_entries.id)
2057 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
2058 }
2059
2060 if ($vfeed_query_part) $vfeed_query_part .= "favicon_avg_color,";
2061
2062 if ($start_ts) {
2063 $start_ts_formatted = date("Y/m/d H:i:s", strtotime($start_ts));
2064 $start_ts_query_part = "date_entered >= '$start_ts_formatted' AND";
2065 } else {
2066 $start_ts_query_part = "";
2067 }
2068
2069 $first_id = 0;
2070 $first_id_query_strategy_part = $query_strategy_part;
2071
2072 if ($feed == -3)
2073 $first_id_query_strategy_part = "true";
2074
2075 if (DB_TYPE == "pgsql") {
2076 $sanity_interval_qpart = "date_entered >= NOW() - INTERVAL '1 hour' AND";
2077 } else {
2078 $sanity_interval_qpart = "date_entered >= DATE_SUB(NOW(), INTERVAL 1 hour) AND";
2079 }
2080
2081 if (!$search && !$skip_first_id_check) {
2082 // if previous topmost article id changed that means our current pagination is no longer valid
2083 $query = "SELECT DISTINCT
2084 ttrss_feeds.title,
2085 date_entered,
2086 guid,
2087 ttrss_entries.id,
2088 ttrss_entries.title,
2089 updated,
2090 score,
2091 marked,
2092 published,
2093 last_marked,
2094 last_published,
2095 last_read
2096 FROM
2097 $from_qpart
2098 WHERE
2099 $feed_check_qpart
2100 ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
2101 $search_query_part
2102 $start_ts_query_part
2103 $since_id_part
2104 $sanity_interval_qpart
2105 $first_id_query_strategy_part ORDER BY $order_by LIMIT 1";
2106
2107 if ($_REQUEST["debug"]) {
2108 print $query;
2109 }
2110
2111 $res = $pdo->query($query);
2112
2113 if ($row = $res->fetch()) {
2114 $first_id = (int)$row["id"];
2115
2116 if ($offset > 0 && $first_id && $check_first_id && $first_id != $check_first_id) {
2117 return array(-1, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id);
2118 }
2119 }
2120 }
2121
2122 $query = "SELECT DISTINCT
2123 date_entered,
2124 guid,
2125 ttrss_entries.id,ttrss_entries.title,
2126 updated,
2127 label_cache,
2128 tag_cache,
2129 always_display_enclosures,
2130 site_url,
2131 note,
2132 num_comments,
2133 comments,
2134 int_id,
2135 uuid,
2136 lang,
2137 hide_images,
2138 unread,feed_id,marked,published,link,last_read,orig_feed_id,
2139 last_marked, last_published,
2140 $vfeed_query_part
2141 $content_query_part
2142 author,score
2143 FROM
2144 $from_qpart
2145 WHERE
2146 $feed_check_qpart
2147 ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
2148 $search_query_part
2149 $start_ts_query_part
2150 $view_query_part
2151 $since_id_part
2152 $query_strategy_part ORDER BY $order_by
2153 $limit_query_part $offset_query_part";
2154
2155 if ($_REQUEST["debug"]) print $query;
2156
2157 $res = $pdo->query($query);
2158
2159 } else {
2160 // browsing by tag
2161
2162 $query = "SELECT DISTINCT
2163 date_entered,
2164 guid,
2165 note,
2166 ttrss_entries.id as id,
2167 title,
2168 updated,
2169 unread,
2170 feed_id,
2171 orig_feed_id,
2172 marked,
2173 num_comments,
2174 comments,
2175 int_id,
2176 tag_cache,
2177 label_cache,
2178 link,
2179 lang,
2180 uuid,
2181 last_read,
2182 (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images,
2183 last_marked, last_published,
2184 $since_id_part
2185 $vfeed_query_part
2186 $content_query_part
2187 author, score
2188 FROM ttrss_entries, ttrss_user_entries, ttrss_tags
2189 WHERE
2190 ref_id = ttrss_entries.id AND
2191 ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
2192 post_int_id = int_id AND
2193 tag_name = ".$pdo->quote($feed)." AND
2194 $view_query_part
2195 $search_query_part
2196 $query_strategy_part ORDER BY $order_by
2197 $limit_query_part $offset_query_part";
2198
2199 if ($_REQUEST["debug"]) print $query;
2200
2201 $res = $pdo->query($query);
2202 }
2203
2204 return array($res, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id);
2205
2206 }
2207
2208 static function getParentCategories($cat, $owner_uid) {
2209 $rv = array();
2210
2211 $pdo = Db::pdo();
2212
2213 $sth = $pdo->prepare("SELECT parent_cat FROM ttrss_feed_categories
2214 WHERE id = ? AND parent_cat IS NOT NULL AND owner_uid = ?");
2215 $sth->execute([$cat, $owner_uid]);
2216
2217 while ($line = $sth->fetch()) {
2218 array_push($rv, $line["parent_cat"]);
2219 $rv = array_merge($rv, Feeds::getParentCategories($line["parent_cat"], $owner_uid));
2220 }
2221
2222 return $rv;
2223 }
2224
2225 static function getChildCategories($cat, $owner_uid) {
2226 $rv = array();
2227
2228 $pdo = Db::pdo();
2229
2230 $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories
2231 WHERE parent_cat = ? AND owner_uid = ?");
2232 $sth->execute([$cat, $owner_uid]);
2233
2234 while ($line = $sth->fetch()) {
2235 array_push($rv, $line["id"]);
2236 $rv = array_merge($rv, Feeds::getChildCategories($line["id"], $owner_uid));
2237 }
2238
2239 return $rv;
2240 }
2241
2242 static function getFeedCategory($feed) {
2243 $pdo = Db::pdo();
2244
2245 $sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds
2246 WHERE id = ?");
2247 $sth->execute([$feed]);
2248
2249 if ($row = $sth->fetch()) {
2250 return $row["cat_id"];
2251 } else {
2252 return false;
2253 }
2254
2255 }
2256
2257
2258 }
2259