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