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