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