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