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