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