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