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