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