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