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