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