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