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