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