]> git.wh0rd.org - tt-rss.git/blob - classes/feeds.php
move the following to Feeds:
[tt-rss.git] / classes / feeds.php
1 <?php
2 require_once "colors.php";
3
4 class Feeds extends Handler_Protected {
5
6 private $params;
7
8 function csrf_ignore($method) {
9 $csrf_ignored = array("index", "feedbrowser", "quickaddfeed", "search");
10
11 return array_search($method, $csrf_ignored) !== false;
12 }
13
14 private function format_headline_subtoolbar($feed_site_url, $feed_title,
15 $feed_id, $is_cat, $search,
16 $error, $feed_last_updated) {
17
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) {
37 $search_q = "&q=$search";
38 } else {
39 $search_q = "";
40 }
41
42 $reply .= "<span class=\"holder\">";
43
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
49 $error_class = $error ? "error" : "";
50
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>";
59 $reply .= "<span id='feed_title' class='$error_class'>";
60
61 if ($feed_site_url) {
62 $last_updated = T_sprintf("Last updated: %s",
63 $feed_last_updated);
64
65 $target = "target=\"_blank\"";
66 $reply .= "<a title=\"$last_updated\" $target href=\"$feed_site_url\">".
67 truncate_string($feed_title, 30)."</a>";
68
69 if ($error) {
70 $error = htmlspecialchars($error);
71 $reply .= "&nbsp;<img title=\"$error\" src='images/error.png' alt='error' class=\"noborder\">";
72 }
73
74 } else {
75 $reply .= $feed_title;
76 }
77
78 $reply .= "</span>";
79
80 $reply .= "</span>";
81
82 # $reply .= "</span>";
83
84 // left part
85
86 $reply .= "<span class=\"main\">";
87 $reply .= "<span id='selected_prompt'></span>";
88
89 /*$reply .= "<span class=\"sel_links\">
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
95 $reply .= "</span> "; */
96
97 $reply .= "<select dojoType=\"dijit.form.Select\"
98 onchange=\"headlineActionsChange(this)\">";
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>";
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
126 if (PluginHost::getInstance()->get_plugin("mail")) {
127 $reply .= "<option value=\"emailArticle(false)\">".__('Forward by email').
128 "</option>";
129 }
130
131 if (PluginHost::getInstance()->get_plugin("mailto")) {
132 $reply .= "<option value=\"mailtoArticle(false)\">".__('Forward by email').
133 "</option>";
134 }
135
136 $reply .= "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
137
138 //$reply .= "<option value=\"catchupPage()\">".__('Mark as read')."</option>";
139
140 $reply .= "<option value=\"displayDlg('".__("View as RSS")."','generatedFeed', '$feed_id:$is_cat:$rss_link')\">".__('View as RSS')."</option>";
141
142 $reply .= "</select>";
143
144 //$reply .= "</h2";
145
146 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HEADLINE_TOOLBAR_BUTTON) as $p) {
147 $reply .= $p->hook_headline_toolbar_button($feed_id, $is_cat);
148 }
149
150 $reply .= "</span></span>";
151
152 return $reply;
153 }
154
155 private function format_headlines_list($feed, $method, $view_mode, $limit, $cat_view,
156 $offset, $vgr_last_feed = false,
157 $override_order = false, $include_children = false, $check_first_id = false,
158 $skip_first_id_check = false) {
159
160 $disable_cache = false;
161
162 $reply = array();
163
164 $rgba_cache = array();
165
166 $timing_info = microtime(true);
167
168 $topmost_article_ids = array();
169
170 if (!$offset) $offset = 0;
171 if ($method == "undefined") $method = "";
172
173 $method_split = explode(":", $method);
174
175 if ($method == "ForceUpdate" && $feed > 0 && is_numeric($feed)) {
176 // Update the feed if required with some basic flood control
177
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'");
199
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"));
203
204 if (!$cache_images && time() - $last_updated > 120) {
205 include "rssfuncs.php";
206 update_rss_feed($feed, true);
207 } else {
208 $this->dbh->query("UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
209 WHERE id = '$feed'");
210 }
211 }
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'");
215 }
216 }
217
218 if ($method_split[0] == "MarkAllReadGR") {
219 $this->catchup_feed($method_split[1], false);
220 }
221
222 // FIXME: might break tag display?
223
224 if (is_numeric($feed) && $feed > 0 && !$cat_view) {
225 $result = $this->dbh->query(
226 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
227
228 if ($this->dbh->num_rows($result) == 0) {
229 $reply['content'] = "<div align='center'>".__('Feed not found.')."</div>";
230 }
231 }
232
233 @$search = $this->dbh->escape_string($_REQUEST["query"]);
234 @$search_language = $this->dbh->escape_string($_REQUEST["search_language"]); // PGSQL only
235
236 if ($search) {
237 $disable_cache = true;
238 }
239
240 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
241
242
243 if (!$cat_view && is_numeric($feed) && $feed < PLUGIN_FEED_BASE_INDEX && $feed > LABEL_BASE_INDEX) {
244 $handler = PluginHost::getInstance()->get_feed_handler(
245 PluginHost::feed_to_pfeed_id($feed));
246
247 if ($handler) {
248 $options = array(
249 "limit" => $limit,
250 "view_mode" => $view_mode,
251 "cat_view" => $cat_view,
252 "search" => $search,
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 {
265 /*$qfh_ret = queryFeedHeadlines($feed, $limit, $view_mode, $cat_view,
266 $search, false, $override_order, $offset, 0,
267 false, 0, $include_children, $topid);*/
268
269 //function queryFeedHeadlines($feed, $limit,
270 // $view_mode, $cat_view, $search, $search_mode,
271 // $override_order = false, $offset = 0, $owner_uid = 0, $filter = false, $since_id = 0, $include_children = false,
272 // $ignore_vfeed_group = false, $override_strategy = false, $override_vfeed = false, $start_ts = false, $check_top_id = false) {
273
274 $params = array(
275 "feed" => $feed,
276 "limit" => $limit,
277 "view_mode" => $view_mode,
278 "cat_view" => $cat_view,
279 "search" => $search,
280 "search_language" => $search_language,
281 "override_order" => $override_order,
282 "offset" => $offset,
283 "include_children" => $include_children,
284 "check_first_id" => $check_first_id,
285 "skip_first_id_check" => $skip_first_id_check
286 );
287
288 $qfh_ret = queryFeedHeadlines($params);
289 }
290
291 $vfeed_group_enabled = get_pref("VFEED_GROUP_BY_FEED") && $feed != -6;
292
293 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
294
295 $result = $qfh_ret[0];
296 $feed_title = $qfh_ret[1];
297 $feed_site_url = $qfh_ret[2];
298 $last_error = $qfh_ret[3];
299 $last_updated = strpos($qfh_ret[4], '1970-') === FALSE ?
300 make_local_datetime($qfh_ret[4], false) : __("Never");
301 $highlight_words = $qfh_ret[5];
302 $reply['first_id'] = $qfh_ret[6];
303 $reply['search_query'] = [$search, $search_language];
304
305 $vgroup_last_feed = $vgr_last_feed;
306
307 $reply['toolbar'] = $this->format_headline_subtoolbar($feed_site_url,
308 $feed_title,
309 $feed, $cat_view, $search,
310 $last_error, $last_updated);
311
312 $headlines_count = is_numeric($result) ? 0 : $this->dbh->num_rows($result);
313
314 if ($offset == 0) {
315 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HEADLINES_BEFORE) as $p) {
316 $reply['content'] .= $p->hook_headlines_before($feed, $cat_view, $qfh_ret);
317 }
318 }
319
320 $reply['content'] = '';
321
322 if ($headlines_count > 0) {
323
324 $lnum = $offset;
325
326 $num_unread = 0;
327 $cur_feed_title = '';
328
329 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PS", $timing_info);
330
331 $expand_cdm = get_pref('CDM_EXPANDED');
332
333 while ($line = $this->dbh->fetch_assoc($result)) {
334
335 $line["content_preview"] = "&mdash; " . truncate_string(strip_tags($line["content"]), 250);
336
337 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
338 $line = $p->hook_query_headlines($line, 250, false);
339 }
340
341 if (get_pref('SHOW_CONTENT_PREVIEW')) {
342 $content_preview = $line["content_preview"];
343 }
344
345 $id = $line["id"];
346 $feed_id = $line["feed_id"];
347 $label_cache = $line["label_cache"];
348 $labels = false;
349
350 if ($label_cache) {
351 $label_cache = json_decode($label_cache, true);
352
353 if ($label_cache) {
354 if ($label_cache["no-labels"] == 1)
355 $labels = array();
356 else
357 $labels = $label_cache;
358 }
359 }
360
361 if (!is_array($labels)) $labels = get_article_labels($id);
362
363 $labels_str = "<span class=\"HLLCTR-$id\">";
364 $labels_str .= Article::format_article_labels($labels);
365 $labels_str .= "</span>";
366
367 if (count($topmost_article_ids) < 3) {
368 array_push($topmost_article_ids, $id);
369 }
370
371 $class = "";
372
373 if (sql_bool_to_bool($line["unread"])) {
374 $class .= " Unread";
375 ++$num_unread;
376 }
377
378 if (sql_bool_to_bool($line["marked"])) {
379 $marked_pic = "<img
380 src=\"images/mark_set.png\"
381 class=\"markedPic\" alt=\"Unstar article\"
382 onclick='toggleMark($id)'>";
383 $class .= " marked";
384 } else {
385 $marked_pic = "<img
386 src=\"images/mark_unset.png\"
387 class=\"markedPic\" alt=\"Star article\"
388 onclick='toggleMark($id)'>";
389 }
390
391 if (sql_bool_to_bool($line["published"])) {
392 $published_pic = "<img src=\"images/pub_set.png\"
393 class=\"pubPic\"
394 alt=\"Unpublish article\" onclick='togglePub($id)'>";
395 $class .= " published";
396 } else {
397 $published_pic = "<img src=\"images/pub_unset.png\"
398 class=\"pubPic\"
399 alt=\"Publish article\" onclick='togglePub($id)'>";
400 }
401
402 # $content_link = "<a target=\"_blank\" rel=\"noopener noreferrer\" href=\"".$line["link"]."\">" .
403 # $line["title"] . "</a>";
404
405 # $content_link = "<a
406 # href=\"" . htmlspecialchars($line["link"]) . "\"
407 # onclick=\"view($id,$feed_id);\">" .
408 # $line["title"] . "</a>";
409
410 # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
411 # $line["title"] . "</a>";
412
413 $updated_fmt = make_local_datetime($line["updated"], false, false, false, true);
414 $date_entered_fmt = T_sprintf("Imported at %s",
415 make_local_datetime($line["date_entered"], false));
416
417 $score = $line["score"];
418
419 $score_pic = "images/" . get_score_pic($score);
420
421 /* $score_title = __("(Click to change)");
422 $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\"
423 onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">"; */
424
425 $score_pic = "<img class='hlScorePic' score='$score' onclick='changeScore($id, this)' src=\"$score_pic\"
426 title=\"$score\">";
427
428 if ($score > 500) {
429 $hlc_suffix = "high";
430 } else if ($score < -100) {
431 $hlc_suffix = "low";
432 } else {
433 $hlc_suffix = "";
434 }
435
436 $entry_author = $line["author"];
437
438 if ($entry_author) {
439 $entry_author = " &mdash; $entry_author";
440 }
441
442 $has_feed_icon = feed_has_icon($feed_id);
443
444 if ($has_feed_icon) {
445 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
446 } else {
447 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/pub_set.png\" alt=\"\">";
448 }
449
450 $entry_site_url = $line["site_url"];
451
452 //setting feed headline background color, needs to change text color based on dark/light
453 $fav_color = $line['favicon_avg_color'];
454
455 require_once "colors.php";
456
457 if ($fav_color && $fav_color != 'fail') {
458 if (!isset($rgba_cache[$feed_id])) {
459 $rgba_cache[$feed_id] = join(",", _color_unpack($fav_color));
460 }
461 }
462
463 if (!get_pref('COMBINED_DISPLAY_MODE')) {
464
465 if ($vfeed_group_enabled) {
466 if ($feed_id != $vgroup_last_feed && $line["feed_title"]) {
467
468 $cur_feed_title = $line["feed_title"];
469 $vgroup_last_feed = $feed_id;
470
471 $cur_feed_title = htmlspecialchars($cur_feed_title);
472
473 $vf_catchup_link = "<a class='catchup' onclick='catchupFeedInGroup($feed_id);' href='#'>".__('mark feed as read')."</a>";
474
475 $reply['content'] .= "<div data-feed-id='$feed_id' id='FTITLE-$feed_id' class='cdmFeedTitle'>".
476 "<div style='float : right'>$feed_icon_img</div>".
477 "<a class='title' href=\"#\" onclick=\"viewfeed({feed:$feed_id})\">".
478 $line["feed_title"]."</a>
479 $vf_catchup_link</div>";
480
481
482 }
483 }
484
485 $mouseover_attrs = "onmouseover='postMouseIn(event, $id)'
486 onmouseout='postMouseOut($id)'";
487
488 $reply['content'] .= "<div class='hl hlMenuAttach $class' data-orig-feed-id='$feed_id' data-article-id='$id' id='RROW-$id' $mouseover_attrs>";
489
490 $reply['content'] .= "<div class='hlLeft'>";
491
492 $reply['content'] .= "<input dojoType=\"dijit.form.CheckBox\"
493 type=\"checkbox\" onclick=\"toggleSelectRow2(this)\"
494 class='rchk'>";
495
496 $reply['content'] .= "$marked_pic";
497 $reply['content'] .= "$published_pic";
498
499 $reply['content'] .= "</div>";
500
501 $reply['content'] .= "<div onclick='return hlClicked(event, $id)'
502 class=\"hlTitle\"><span class='hlContent $hlc_suffix'>";
503 $reply['content'] .= "<a id=\"RTITLE-$id\" class=\"title $hlc_suffix\"
504 href=\"" . htmlspecialchars($line["link"]) . "\"
505 onclick=\"\">" .
506 truncate_string($line["title"], 200);
507
508 if (get_pref('SHOW_CONTENT_PREVIEW')) {
509 $reply['content'] .= "<span class=\"contentPreview\">" . $line["content_preview"] . "</span>";
510 }
511
512 $reply['content'] .= "</a></span>";
513
514 $reply['content'] .= $labels_str;
515
516 $reply['content'] .= "</div>";
517
518 if (!$vfeed_group_enabled) {
519 if (@$line["feed_title"]) {
520 $rgba = @$rgba_cache[$feed_id];
521
522 $reply['content'] .= "<span class=\"hlFeed\"><a style=\"background : rgba($rgba, 0.3)\" href=\"#\" onclick=\"viewfeed({feed:$feed_id})\">".
523 truncate_string($line["feed_title"],30)."</a></span>";
524 }
525 }
526
527
528 $reply['content'] .= "<span class=\"hlUpdated\">";
529
530 $reply['content'] .= "<div title='$date_entered_fmt'>$updated_fmt</div>
531 </span>";
532
533 $reply['content'] .= "<div class=\"hlRight\">";
534
535 $reply['content'] .= $score_pic;
536
537 if ($line["feed_title"] && !$vfeed_group_enabled) {
538
539 $reply['content'] .= "<span onclick=\"viewfeed({feed:$feed_id})\"
540 style=\"cursor : pointer\"
541 title=\"".htmlspecialchars($line['feed_title'])."\">
542 $feed_icon_img</span>";
543 }
544
545 $reply['content'] .= "</div>";
546 $reply['content'] .= "</div>";
547
548 } else {
549
550 if ($line["tag_cache"])
551 $tags = explode(",", $line["tag_cache"]);
552 else
553 $tags = false;
554
555 $line["content"] = sanitize($line["content"],
556 sql_bool_to_bool($line['hide_images']), false, $entry_site_url, $highlight_words, $line["id"]);
557
558 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_CDM) as $p) {
559 $line = $p->hook_render_article_cdm($line);
560 }
561
562 if ($vfeed_group_enabled && $line["feed_title"]) {
563 if ($feed_id != $vgroup_last_feed) {
564
565 $cur_feed_title = $line["feed_title"];
566 $vgroup_last_feed = $feed_id;
567
568 $cur_feed_title = htmlspecialchars($cur_feed_title);
569
570 $vf_catchup_link = "<a class='catchup' onclick='catchupFeedInGroup($feed_id);' href='#'>".__('mark feed as read')."</a>";
571
572 $has_feed_icon = feed_has_icon($feed_id);
573
574 if ($has_feed_icon) {
575 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
576 } else {
577 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
578 }
579
580 $reply['content'] .= "<div data-feed-id='$feed_id' id='FTITLE-$feed_id' class='cdmFeedTitle'>".
581 "<div style=\"float : right\">$feed_icon_img</div>".
582 "<a href=\"#\" class='title' onclick=\"viewfeed({feed:$feed_id})\">".
583 $line["feed_title"]."</a> $vf_catchup_link</div>";
584
585 }
586 }
587
588 $mouseover_attrs = "onmouseover='postMouseIn(event, $id)'
589 onmouseout='postMouseOut($id)'";
590
591 $expanded_class = $expand_cdm ? "expanded" : "expandable";
592
593 $reply['content'] .= "<div class=\"cdm $hlc_suffix $expanded_class $class\"
594 id=\"RROW-$id\" data-article-id='$id' data-orig-feed-id='$feed_id' $mouseover_attrs>";
595
596 $reply['content'] .= "<div class=\"cdmHeader\">";
597 $reply['content'] .= "<div style=\"vertical-align : middle\">";
598
599 $reply['content'] .= "<input dojoType=\"dijit.form.CheckBox\"
600 type=\"checkbox\" onclick=\"toggleSelectRow2(this, false, true)\"
601 class='rchk'>";
602
603 $reply['content'] .= "$marked_pic";
604 $reply['content'] .= "$published_pic";
605
606 $reply['content'] .= "</div>";
607
608 if ($highlight_words && count($highlight_words > 0)) {
609 foreach ($highlight_words as $word) {
610 $line["title"] = preg_replace("/(\Q$word\E)/i",
611 "<span class=\"highlight\">$1</span>", $line["title"]);
612 }
613 }
614
615 // data-article-id included for context menu
616 $reply['content'] .= "<span id=\"RTITLE-$id\"
617 onclick=\"return cdmClicked(event, $id);\"
618 data-article-id=\"$id\"
619 class=\"titleWrap hlMenuAttach $hlc_suffix\">
620 <a class=\"title $hlc_suffix\"
621 title=\"".htmlspecialchars($line["title"])."\"
622 target=\"_blank\" rel=\"noopener noreferrer\" href=\"".
623 htmlspecialchars($line["link"])."\">".
624 $line["title"] .
625 "</a> <span class=\"author\">$entry_author</span>";
626
627 $reply['content'] .= $labels_str;
628
629 $reply['content'] .= "<span class='collapseBtn' style='display : none'>
630 <img src=\"images/collapse.png\" onclick=\"cdmCollapseArticle(event, $id)\"
631 title=\"".__("Collapse article")."\"/></span>";
632
633 if (!$expand_cdm)
634 $content_hidden = "style=\"display : none\"";
635 else
636 $excerpt_hidden = "style=\"display : none\"";
637
638 $reply['content'] .= "<span $excerpt_hidden id=\"CEXC-$id\" class=\"cdmExcerpt\">" . $content_preview . "</span>";
639
640 $reply['content'] .= "</span>";
641
642 if (!$vfeed_group_enabled) {
643 if (@$line["feed_title"]) {
644 $rgba = @$rgba_cache[$feed_id];
645
646 $reply['content'] .= "<div class=\"hlFeed\">
647 <a href=\"#\" style=\"background-color: rgba($rgba,0.3)\"
648 onclick=\"viewfeed({feed:$feed_id})\">".
649 truncate_string($line["feed_title"],30)."</a>
650 </div>";
651 }
652 }
653
654 $reply['content'] .= "<span class='updated' title='$date_entered_fmt'>
655 $updated_fmt</span>";
656
657 $reply['content'] .= "<div class='scoreWrap' style=\"vertical-align : middle\">";
658 $reply['content'] .= "$score_pic";
659
660 if (!get_pref("VFEED_GROUP_BY_FEED") && $line["feed_title"]) {
661 $reply['content'] .= "<span style=\"cursor : pointer\"
662 title=\"".htmlspecialchars($line["feed_title"])."\"
663 onclick=\"viewfeed({feed:$feed_id})\">$feed_icon_img</span>";
664 }
665 $reply['content'] .= "</div>";
666
667 $reply['content'] .= "</div>";
668
669 $reply['content'] .= "<div class=\"cdmContent\" $content_hidden
670 onclick=\"return cdmClicked(event, $id, true);\"
671 id=\"CICD-$id\">";
672
673 $reply['content'] .= "<div id=\"POSTNOTE-$id\">";
674 if ($line['note']) {
675 $reply['content'] .= Article::format_article_note($id, $line['note']);
676 }
677 $reply['content'] .= "</div>";
678
679 if (!$line['lang']) $line['lang'] = 'en';
680
681 $reply['content'] .= "<div class=\"cdmContentInner\" lang=\"".$line['lang']."\">";
682
683 if ($line["orig_feed_id"]) {
684
685 $tmp_result = $this->dbh->query("SELECT * FROM ttrss_archived_feeds
686 WHERE id = ".$line["orig_feed_id"] . " AND owner_uid = " . $_SESSION["uid"]);
687
688 if ($this->dbh->num_rows($tmp_result) != 0) {
689
690 $reply['content'] .= "<div clear='both'>";
691 $reply['content'] .= __("Originally from:");
692
693 $reply['content'] .= "&nbsp;";
694
695 $tmp_line = $this->dbh->fetch_assoc($tmp_result);
696
697 $reply['content'] .= "<a target='_blank' rel='noopener noreferrer'
698 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
699 $tmp_line['title'] . "</a>";
700
701 $reply['content'] .= "&nbsp;";
702
703 $reply['content'] .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
704 $reply['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_unset.png'></a>";
705
706 $reply['content'] .= "</div>";
707 }
708 }
709
710 $reply['content'] .= "<span id=\"CWRAP-$id\">";
711
712 $reply['content'] .= "<span id=\"CENCW-$id\" class=\"cencw\" style=\"display : none\">";
713 $reply['content'] .= htmlspecialchars($line["content"]);
714 $reply['content'] .= "</span>";
715
716 $reply['content'] .= "</span>";
717
718 $reply['content'] .= "</div>";
719
720 $reply['content'] .= "<div class=\"cdmIntermediate\">";
721
722 $always_display_enclosures = sql_bool_to_bool($line["always_display_enclosures"]);
723 $reply['content'] .= Article::format_article_enclosures($id, $always_display_enclosures, $line["content"], sql_bool_to_bool($line["hide_images"]));
724
725 $reply['content'] .= "</div>";
726
727 $reply['content'] .= "<div class=\"cdmFooter\" onclick=\"cdmFooterClick(event)\">";
728
729 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_LEFT_BUTTON) as $p) {
730 $reply['content'] .= $p->hook_article_left_button($line);
731 }
732
733 $tags_str = Article::format_tags_string($tags, $id);
734
735 $reply['content'] .= "<span class='left'>";
736
737 $reply['content'] .= "<img src='images/tag.png' alt='Tags' title='Tags'>
738 <span id=\"ATSTR-$id\">$tags_str</span>
739 <a title=\"".__('Edit tags for this article')."\"
740 href=\"#\" onclick=\"editArticleTags($id)\">(+)</a>";
741
742 $num_comments = (int) $line["num_comments"];
743 $entry_comments = "";
744
745 if ($num_comments > 0) {
746 if ($line["comments"]) {
747 $comments_url = htmlspecialchars($line["comments"]);
748 } else {
749 $comments_url = htmlspecialchars($line["link"]);
750 }
751 $entry_comments = "<a class=\"postComments\"
752 target='_blank' rel='noopener noreferrer' href=\"$comments_url\">$num_comments ".
753 _ngettext("comment", "comments", $num_comments)."</a>";
754
755 } else {
756 if ($line["comments"] && $line["link"] != $line["comments"]) {
757 $entry_comments = "<a class=\"postComments\" target='_blank' rel='noopener noreferrer' href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
758 }
759 }
760
761 if ($entry_comments) $reply['content'] .= "&nbsp;($entry_comments)";
762
763 $reply['content'] .= "</span>";
764 $reply['content'] .= "<div>";
765
766 // $reply['content'] .= "$marked_pic";
767 // $reply['content'] .= "$published_pic";
768
769 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_BUTTON) as $p) {
770 $reply['content'] .= $p->hook_article_button($line);
771 }
772
773 $reply['content'] .= "</div>";
774 $reply['content'] .= "</div>";
775
776 $reply['content'] .= "</div>";
777
778 $reply['content'] .= "</div>";
779
780 }
781
782 ++$lnum;
783 }
784
785 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PE", $timing_info);
786
787 } else if (!is_numeric($result)) {
788 $message = "";
789
790 switch ($view_mode) {
791 case "unread":
792 $message = __("No unread articles found to display.");
793 break;
794 case "updated":
795 $message = __("No updated articles found to display.");
796 break;
797 case "marked":
798 $message = __("No starred articles found to display.");
799 break;
800 default:
801 if ($feed < LABEL_BASE_INDEX) {
802 $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.");
803 } else {
804 $message = __("No articles found to display.");
805 }
806 }
807
808 if (!$offset && $message) {
809 $reply['content'] = "<div class='whiteBox'>$message";
810
811 $reply['content'] .= "<p><span class=\"insensitive\">";
812
813 $result = $this->dbh->query("SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
814 WHERE owner_uid = " . $_SESSION['uid']);
815
816 $last_updated = $this->dbh->fetch_result($result, 0, "last_updated");
817 $last_updated = make_local_datetime($last_updated, false);
818
819 $reply['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
820
821 $result = $this->dbh->query("SELECT COUNT(id) AS num_errors
822 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
823
824 $num_errors = $this->dbh->fetch_result($result, 0, "num_errors");
825
826 if ($num_errors > 0) {
827 $reply['content'] .= "<br/>";
828 $reply['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
829 __('Some feeds have update errors (click for details)')."</a>";
830 }
831 $reply['content'] .= "</span></p></div>";
832
833 }
834 } else if (is_numeric($result) && $result == -1) {
835 $reply['first_id_changed'] = true;
836 }
837
838 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H2", $timing_info);
839
840 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache,
841 $vgroup_last_feed, $reply);
842 }
843
844 function catchupAll() {
845 $this->dbh->query("UPDATE ttrss_user_entries SET
846 last_read = NOW(), unread = false WHERE unread = true AND owner_uid = " . $_SESSION["uid"]);
847 ccache_zero_all($_SESSION["uid"]);
848 }
849
850 function view() {
851 $timing_info = microtime(true);
852
853 $reply = array();
854
855 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
856
857 $feed = $this->dbh->escape_string($_REQUEST["feed"]);
858 $method = $this->dbh->escape_string($_REQUEST["m"]);
859 $view_mode = $this->dbh->escape_string($_REQUEST["view_mode"]);
860 $limit = 30;
861 @$cat_view = $_REQUEST["cat"] == "true";
862 @$next_unread_feed = $this->dbh->escape_string($_REQUEST["nuf"]);
863 @$offset = $this->dbh->escape_string($_REQUEST["skip"]);
864 @$vgroup_last_feed = $this->dbh->escape_string($_REQUEST["vgrlf"]);
865 $order_by = $this->dbh->escape_string($_REQUEST["order_by"]);
866 $check_first_id = $this->dbh->escape_string($_REQUEST["fid"]);
867
868 if (is_numeric($feed)) $feed = (int) $feed;
869
870 /* Feed -5 is a special case: it is used to display auxiliary information
871 * when there's nothing to load - e.g. no stuff in fresh feed */
872
873 if ($feed == -5) {
874 print json_encode($this->generate_dashboard_feed());
875 return;
876 }
877
878 $result = false;
879
880 if ($feed < LABEL_BASE_INDEX) {
881 $label_feed = feed_to_label_id($feed);
882 $result = $this->dbh->query("SELECT id FROM ttrss_labels2 WHERE
883 id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
884 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
885 $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
886 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
887 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
888 $result = $this->dbh->query("SELECT id FROM ttrss_feed_categories WHERE
889 id = '$feed' AND owner_uid = " . $_SESSION['uid']);
890 }
891
892 if ($result && $this->dbh->num_rows($result) == 0) {
893 print json_encode($this->generate_error_feed(__("Feed not found.")));
894 return;
895 }
896
897 /* Updating a label ccache means recalculating all of the caches
898 * so for performance reasons we don't do that here */
899
900 if ($feed >= 0) {
901 ccache_update($feed, $_SESSION["uid"], $cat_view);
902 }
903
904 set_pref("_DEFAULT_VIEW_MODE", $view_mode);
905 set_pref("_DEFAULT_VIEW_ORDER_BY", $order_by);
906
907 /* bump login timestamp if needed */
908 if (time() - $_SESSION["last_login_update"] > 3600) {
909 $this->dbh->query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
910 $_SESSION["uid"]);
911 $_SESSION["last_login_update"] = time();
912 }
913
914 if (!$cat_view && is_numeric($feed) && $feed > 0) {
915 $this->dbh->query("UPDATE ttrss_feeds SET last_viewed = NOW()
916 WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
917 }
918
919 $reply['headlines'] = array();
920
921 $override_order = false;
922 $skip_first_id_check = false;
923
924 switch ($order_by) {
925 case "title":
926 $override_order = "ttrss_entries.title";
927 break;
928 case "date_reverse":
929 $override_order = "score DESC, date_entered, updated";
930 $skip_first_id_check = true;
931 break;
932 case "feed_dates":
933 $override_order = "updated DESC";
934 break;
935 }
936
937 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
938
939 $ret = $this->format_headlines_list($feed, $method,
940 $view_mode, $limit, $cat_view, $offset,
941 $vgroup_last_feed, $override_order, true, $check_first_id, $skip_first_id_check);
942
943 //$topmost_article_ids = $ret[0];
944 $headlines_count = $ret[1];
945 /* $returned_feed = $ret[2]; */
946 $disable_cache = $ret[3];
947 $vgroup_last_feed = $ret[4];
948
949 //$reply['headlines']['content'] =& $ret[5]['content'];
950 //$reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
951
952 $reply['headlines'] = $ret[5];
953
954 if (!$next_unread_feed)
955 $reply['headlines']['id'] = $feed;
956 else
957 $reply['headlines']['id'] = $next_unread_feed;
958
959 $reply['headlines']['is_cat'] = (bool) $cat_view;
960
961 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
962
963 $reply['headlines-info'] = array("count" => (int) $headlines_count,
964 "vgroup_last_feed" => $vgroup_last_feed,
965 "disable_cache" => (bool) $disable_cache);
966
967 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
968
969 $reply['runtime-info'] = make_runtime_info();
970
971 print json_encode($reply);
972
973 }
974
975 private function generate_dashboard_feed() {
976 $reply = array();
977
978 $reply['headlines']['id'] = -5;
979 $reply['headlines']['is_cat'] = false;
980
981 $reply['headlines']['toolbar'] = '';
982
983 $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
984
985 $reply['headlines']['content'] .= "<p><span class=\"insensitive\">";
986
987 $result = $this->dbh->query("SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
988 WHERE owner_uid = " . $_SESSION['uid']);
989
990 $last_updated = $this->dbh->fetch_result($result, 0, "last_updated");
991 $last_updated = make_local_datetime($last_updated, false);
992
993 $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
994
995 $result = $this->dbh->query("SELECT COUNT(id) AS num_errors
996 FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
997
998 $num_errors = $this->dbh->fetch_result($result, 0, "num_errors");
999
1000 if ($num_errors > 0) {
1001 $reply['headlines']['content'] .= "<br/>";
1002 $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
1003 __('Some feeds have update errors (click for details)')."</a>";
1004 }
1005 $reply['headlines']['content'] .= "</span></p>";
1006
1007 $reply['headlines-info'] = array("count" => 0,
1008 "vgroup_last_feed" => '',
1009 "unread" => 0,
1010 "disable_cache" => true);
1011
1012 return $reply;
1013 }
1014
1015 private function generate_error_feed($error) {
1016 $reply = array();
1017
1018 $reply['headlines']['id'] = -7;
1019 $reply['headlines']['is_cat'] = false;
1020
1021 $reply['headlines']['toolbar'] = '';
1022 $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
1023
1024 $reply['headlines-info'] = array("count" => 0,
1025 "vgroup_last_feed" => '',
1026 "unread" => 0,
1027 "disable_cache" => true);
1028
1029 return $reply;
1030 }
1031
1032 function quickAddFeed() {
1033 print_hidden("op", "rpc");
1034 print_hidden("method", "addfeed");
1035
1036 print "<div id='fadd_error_message' style='display : none' class='alert alert-danger'></div>";
1037
1038 print "<div id='fadd_multiple_notify' style='display : none'>";
1039 print_notice("Provided URL is a HTML page referencing multiple feeds, please select required feed from the dropdown menu below.");
1040 print "<p></div>";
1041
1042 print "<div class=\"dlgSec\">".__("Feed or site URL")."</div>";
1043 print "<div class=\"dlgSecCont\">";
1044
1045 print "<div style='float : right'>
1046 <img style='display : none'
1047 id='feed_add_spinner' src='images/indicator_white.gif'></div>";
1048
1049 print "<input style=\"font-size : 16px; width : 20em;\"
1050 placeHolder=\"".__("Feed or site URL")."\"
1051 dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"feed\" id=\"feedDlg_feedUrl\">";
1052
1053 print "<hr/>";
1054
1055 if (get_pref('ENABLE_FEED_CATS')) {
1056 print __('Place in category:') . " ";
1057 print_feed_cat_select("cat", false, 'dojoType="dijit.form.Select"');
1058 }
1059
1060 print "</div>";
1061
1062 print '<div id="feedDlg_feedsContainer" style="display : none">
1063
1064 <div class="dlgSec">' . __('Available feeds') . '</div>
1065 <div class="dlgSecCont">'.
1066 '<select id="feedDlg_feedContainerSelect"
1067 dojoType="dijit.form.Select" size="3">
1068 <script type="dojo/method" event="onChange" args="value">
1069 dijit.byId("feedDlg_feedUrl").attr("value", value);
1070 </script>
1071 </select>'.
1072 '</div></div>';
1073
1074 print "<div id='feedDlg_loginContainer' style='display : none'>
1075
1076 <div class=\"dlgSec\">".__("Authentication")."</div>
1077 <div class=\"dlgSecCont\">".
1078
1079 " <input dojoType=\"dijit.form.TextBox\" name='login'\"
1080 placeHolder=\"".__("Login")."\"
1081 autocomplete=\"new-password\"
1082 style=\"width : 10em;\"> ".
1083 " <input
1084 placeHolder=\"".__("Password")."\"
1085 dojoType=\"dijit.form.TextBox\" type='password'
1086 autocomplete=\"new-password\"
1087 style=\"width : 10em;\" name='pass'\">
1088 </div></div>";
1089
1090
1091 print "<div style=\"clear : both\">
1092 <input type=\"checkbox\" name=\"need_auth\" dojoType=\"dijit.form.CheckBox\" id=\"feedDlg_loginCheck\"
1093 onclick='checkboxToggleElement(this, \"feedDlg_loginContainer\")'>
1094 <label for=\"feedDlg_loginCheck\">".
1095 __('This feed requires authentication.')."</div>";
1096
1097 print "</form>";
1098
1099 print "<div class=\"dlgButtons\">
1100 <button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('feedAddDlg').execute()\">".__('Subscribe')."</button>";
1101
1102 if (!(defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER)) {
1103 print "<button dojoType=\"dijit.form.Button\" onclick=\"return feedBrowser()\">".__('More feeds')."</button>";
1104 }
1105
1106 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('feedAddDlg').hide()\">".__('Cancel')."</button>
1107 </div>";
1108
1109 //return;
1110 }
1111
1112 function feedBrowser() {
1113 if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER) return;
1114
1115 $browser_search = $this->dbh->escape_string($_REQUEST["search"]);
1116
1117 print_hidden("op", "rpc");
1118 print_hidden("method", "updateFeedBrowser");
1119
1120 print "<div dojoType=\"dijit.Toolbar\">
1121 <div style='float : right'>
1122 <img style='display : none'
1123 id='feed_browser_spinner' src='images/indicator_white.gif'>
1124 <input name=\"search\" dojoType=\"dijit.form.TextBox\" size=\"20\" type=\"search\"
1125 onchange=\"dijit.byId('feedBrowserDlg').update()\" value=\"$browser_search\">
1126 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').update()\">".__('Search')."</button>
1127 </div>";
1128
1129 print " <select name=\"mode\" dojoType=\"dijit.form.Select\" onchange=\"dijit.byId('feedBrowserDlg').update()\">
1130 <option value='1'>" . __('Popular feeds') . "</option>
1131 <option value='2'>" . __('Feed archive') . "</option>
1132 </select> ";
1133
1134 print __("limit:");
1135
1136 print " <select dojoType=\"dijit.form.Select\" name=\"limit\" onchange=\"dijit.byId('feedBrowserDlg').update()\">";
1137
1138 foreach (array(25, 50, 100, 200) as $l) {
1139 //$issel = ($l == $limit) ? "selected=\"1\"" : "";
1140 print "<option value=\"$l\">$l</option>";
1141 }
1142
1143 print "</select> ";
1144
1145 print "</div>";
1146
1147 require_once "feedbrowser.php";
1148
1149 print "<ul class='browseFeedList' id='browseFeedList'>";
1150 print make_feed_browser("", 25);
1151 print "</ul>";
1152
1153 print "<div align='center'>
1154 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').execute()\">".__('Subscribe')."</button>
1155 <button dojoType=\"dijit.form.Button\" style='display : none' id='feed_archive_remove' onclick=\"dijit.byId('feedBrowserDlg').removeFromArchive()\">".__('Remove')."</button>
1156 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').hide()\" >".__('Cancel')."</button></div>";
1157
1158 }
1159
1160 function search() {
1161 $this->params = explode(":", $this->dbh->escape_string($_REQUEST["param"]), 2);
1162
1163 $active_feed_id = sprintf("%d", $this->params[0]);
1164 $is_cat = $this->params[1] != "false";
1165
1166 print "<div class=\"dlgSec\">".__('Look for')."</div>";
1167
1168 print "<div class=\"dlgSecCont\">";
1169
1170 print "<input dojoType=\"dijit.form.ValidationTextBox\"
1171 style=\"font-size : 16px; width : 20em;\"
1172 required=\"1\" name=\"query\" type=\"search\" value=''>";
1173
1174 print "<hr/><span style='float : right'>".T_sprintf('in %s', $this->getFeedTitle($active_feed_id, $is_cat))."</span>";
1175
1176 if (DB_TYPE == "pgsql") {
1177 print "<hr/>";
1178 print_select("search_language", "", Pref_Feeds::$feed_languages,
1179 "dojoType='dijit.form.Select' title=\"".__('Used for word stemming')."\"");
1180 }
1181
1182 print "</div>";
1183
1184 print "<div class=\"dlgButtons\">";
1185
1186 if (count(PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEARCH)) == 0) {
1187 print "<div style=\"float : left\">
1188 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/SearchSyntax\">".__("Search syntax")."</a>
1189 </div>";
1190 }
1191
1192 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('searchDlg').execute()\">".__('Search')."</button>
1193 <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('searchDlg').hide()\">".__('Cancel')."</button>
1194 </div>";
1195 }
1196
1197 function update_debugger() {
1198 header("Content-type: text/html");
1199
1200 $feed_id = (int)$_REQUEST["feed_id"];
1201 @$do_update = $_REQUEST["action"] == "do_update";
1202 $csrf_token = $_REQUEST["csrf_token"];
1203
1204 $refetch_checked = isset($_REQUEST["force_refetch"]) ? "checked" : "";
1205 $rehash_checked = isset($_REQUEST["force_rehash"]) ? "checked" : "";
1206
1207 ?>
1208 <html>
1209 <head>
1210 <link rel="stylesheet" type="text/css" href="css/utility.css">
1211 <title>Feed Debugger</title>
1212 </head>
1213 <body class="small_margins">
1214 <h1>Feed Debugger: <?php echo "$feed_id: " . $this->getFeedTitle($feed_id) ?></h1>
1215 <form method="GET" action="">
1216 <input type="hidden" name="op" value="feeds">
1217 <input type="hidden" name="method" value="update_debugger">
1218 <input type="hidden" name="xdebug" value="1">
1219 <input type="hidden" name="csrf_token" value="<?php echo $csrf_token ?>">
1220 <input type="hidden" name="action" value="do_update">
1221 <input type="hidden" name="feed_id" value="<?php echo $feed_id ?>">
1222 <input type="checkbox" name="force_refetch" value="1" <?php echo $refetch_checked ?>> Force refetch<br/>
1223 <input type="checkbox" name="force_rehash" value="1" <?php echo $rehash_checked ?>> Force rehash<br/>
1224
1225 <p/><button type="submit">Continue</button>
1226 </form>
1227
1228 <hr>
1229
1230 <pre><?php
1231
1232 if ($do_update) {
1233 include "rssfuncs.php";
1234 update_rss_feed($feed_id, true);
1235 }
1236
1237 ?></pre>
1238
1239 </body>
1240 </html>
1241 <?php
1242
1243 }
1244
1245 static function catchup_feed($feed, $cat_view, $owner_uid = false, $mode = 'all', $search = false) {
1246
1247 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
1248
1249 // Todo: all this interval stuff needs some generic generator function
1250
1251 $date_qpart = "false";
1252 $search_qpart = is_array($search) && $search[0] ? search_to_sql($search[0], $search[1])[0] : 'true';
1253
1254 switch ($mode) {
1255 case "1day":
1256 if (DB_TYPE == "pgsql") {
1257 $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
1258 } else {
1259 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
1260 }
1261 break;
1262 case "1week":
1263 if (DB_TYPE == "pgsql") {
1264 $date_qpart = "date_entered < NOW() - INTERVAL '1 week' ";
1265 } else {
1266 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) ";
1267 }
1268 break;
1269 case "2week":
1270 if (DB_TYPE == "pgsql") {
1271 $date_qpart = "date_entered < NOW() - INTERVAL '2 week' ";
1272 } else {
1273 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) ";
1274 }
1275 break;
1276 default:
1277 $date_qpart = "true";
1278 }
1279
1280 if (is_numeric($feed)) {
1281 if ($cat_view) {
1282
1283 if ($feed >= 0) {
1284
1285 if ($feed > 0) {
1286 $children = getChildCategories($feed, $owner_uid);
1287 array_push($children, $feed);
1288
1289 $children = join(",", $children);
1290
1291 $cat_qpart = "cat_id IN ($children)";
1292 } else {
1293 $cat_qpart = "cat_id IS NULL";
1294 }
1295
1296 db_query("UPDATE ttrss_user_entries
1297 SET unread = false, last_read = NOW() WHERE ref_id IN
1298 (SELECT id FROM
1299 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1300 AND owner_uid = $owner_uid AND unread = true AND feed_id IN
1301 (SELECT id FROM ttrss_feeds WHERE $cat_qpart) AND $date_qpart AND $search_qpart) as tmp)");
1302
1303 } else if ($feed == -2) {
1304
1305 db_query("UPDATE ttrss_user_entries
1306 SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
1307 FROM ttrss_user_labels2, ttrss_entries WHERE article_id = ref_id AND id = ref_id AND $date_qpart AND $search_qpart) > 0
1308 AND unread = true AND owner_uid = $owner_uid");
1309 }
1310
1311 } else if ($feed > 0) {
1312
1313 db_query("UPDATE ttrss_user_entries
1314 SET unread = false, last_read = NOW() WHERE ref_id IN
1315 (SELECT id FROM
1316 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1317 AND owner_uid = $owner_uid AND unread = true AND feed_id = $feed AND $date_qpart AND $search_qpart) as tmp)");
1318
1319 } else if ($feed < 0 && $feed > LABEL_BASE_INDEX) { // special, like starred
1320
1321 if ($feed == -1) {
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 marked = true AND $date_qpart AND $search_qpart) as tmp)");
1327 }
1328
1329 if ($feed == -2) {
1330 db_query("UPDATE ttrss_user_entries
1331 SET unread = false, last_read = NOW() WHERE ref_id IN
1332 (SELECT id FROM
1333 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1334 AND owner_uid = $owner_uid AND unread = true AND published = true AND $date_qpart AND $search_qpart) as tmp)");
1335 }
1336
1337 if ($feed == -3) {
1338
1339 $intl = get_pref("FRESH_ARTICLE_MAX_AGE");
1340
1341 if (DB_TYPE == "pgsql") {
1342 $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
1343 } else {
1344 $match_part = "date_entered > DATE_SUB(NOW(),
1345 INTERVAL $intl HOUR) ";
1346 }
1347
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 score >= 0 AND unread = true AND $date_qpart AND $match_part AND $search_qpart) as tmp)");
1353 }
1354
1355 if ($feed == -4) {
1356 db_query("UPDATE ttrss_user_entries
1357 SET unread = false, last_read = NOW() WHERE ref_id IN
1358 (SELECT id FROM
1359 (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
1360 AND owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1361 }
1362
1363 } else if ($feed < LABEL_BASE_INDEX) { // label
1364
1365 $label_id = feed_to_label_id($feed);
1366
1367 db_query("UPDATE ttrss_user_entries
1368 SET unread = false, last_read = NOW() WHERE ref_id IN
1369 (SELECT id FROM
1370 (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_user_labels2 WHERE ref_id = id
1371 AND label_id = '$label_id' AND ref_id = article_id
1372 AND owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1373
1374 }
1375
1376 ccache_update($feed, $owner_uid, $cat_view);
1377
1378 } else { // tag
1379 db_query("UPDATE ttrss_user_entries
1380 SET unread = false, last_read = NOW() WHERE ref_id IN
1381 (SELECT id FROM
1382 (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_tags WHERE ref_id = ttrss_entries.id
1383 AND post_int_id = int_id AND tag_name = '$feed'
1384 AND ttrss_user_entries.owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
1385
1386 }
1387 }
1388
1389 static function getFeedArticles($feed, $is_cat = false, $unread_only = false,
1390 $owner_uid = false) {
1391
1392 $n_feed = (int) $feed;
1393 $need_entries = false;
1394
1395 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1396
1397 if ($unread_only) {
1398 $unread_qpart = "unread = true";
1399 } else {
1400 $unread_qpart = "true";
1401 }
1402
1403 if ($is_cat) {
1404 return Feeds::getCategoryUnread($n_feed, $owner_uid);
1405 } else if ($n_feed == -6) {
1406 return 0;
1407 } else if ($feed != "0" && $n_feed == 0) {
1408
1409 $feed = db_escape_string($feed);
1410
1411 $result = db_query("SELECT SUM((SELECT COUNT(int_id)
1412 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
1413 AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags
1414 WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
1415 return db_fetch_result($result, 0, "count");
1416
1417 } else if ($n_feed == -1) {
1418 $match_part = "marked = true";
1419 } else if ($n_feed == -2) {
1420 $match_part = "published = true";
1421 } else if ($n_feed == -3) {
1422 $match_part = "unread = true AND score >= 0";
1423
1424 $intl = get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
1425
1426 if (DB_TYPE == "pgsql") {
1427 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
1428 } else {
1429 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
1430 }
1431
1432 $need_entries = true;
1433
1434 } else if ($n_feed == -4) {
1435 $match_part = "true";
1436 } else if ($n_feed >= 0) {
1437
1438 if ($n_feed != 0) {
1439 $match_part = "feed_id = '$n_feed'";
1440 } else {
1441 $match_part = "feed_id IS NULL";
1442 }
1443
1444 } else if ($feed < LABEL_BASE_INDEX) {
1445
1446 $label_id = feed_to_label_id($feed);
1447
1448 return getLabelUnread($label_id, $owner_uid);
1449
1450 }
1451
1452 if ($match_part) {
1453
1454 if ($need_entries) {
1455 $from_qpart = "ttrss_user_entries,ttrss_entries";
1456 $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND";
1457 } else {
1458 $from_qpart = "ttrss_user_entries";
1459 $from_where = "";
1460 }
1461
1462 $query = "SELECT count(int_id) AS unread
1463 FROM $from_qpart WHERE
1464 $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
1465
1466 //echo "[$feed/$query]\n";
1467
1468 $result = db_query($query);
1469
1470 } else {
1471
1472 $result = db_query("SELECT COUNT(post_int_id) AS unread
1473 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
1474 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
1475 AND $unread_qpart AND ttrss_tags.owner_uid = " . $owner_uid);
1476 }
1477
1478 $unread = db_fetch_result($result, 0, "unread");
1479
1480 return $unread;
1481 }
1482
1483 /**
1484 * @return array (code => Status code, message => error message if available)
1485 *
1486 * 0 - OK, Feed already exists
1487 * 1 - OK, Feed added
1488 * 2 - Invalid URL
1489 * 3 - URL content is HTML, no feeds available
1490 * 4 - URL content is HTML which contains multiple feeds.
1491 * Here you should call extractfeedurls in rpc-backend
1492 * to get all possible feeds.
1493 * 5 - Couldn't download the URL content.
1494 * 6 - Content is an invalid XML.
1495 */
1496 static function subscribe_to_feed($url, $cat_id = 0,
1497 $auth_login = '', $auth_pass = '') {
1498
1499 global $fetch_last_error;
1500 global $fetch_last_error_content;
1501
1502 require_once "include/rssfuncs.php";
1503
1504 $url = fix_url($url);
1505
1506 if (!$url || !validate_feed_url($url)) return array("code" => 2);
1507
1508 $contents = @fetch_file_contents($url, false, $auth_login, $auth_pass);
1509
1510 if (!$contents) {
1511 if (preg_match("/cloudflare\.com/", $fetch_last_error_content)) {
1512 $fetch_last_error .= " (feed behind Cloudflare)";
1513 }
1514
1515 return array("code" => 5, "message" => $fetch_last_error);
1516 }
1517
1518 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SUBSCRIBE_FEED) as $plugin) {
1519 $contents = $plugin->hook_subscribe_feed($contents, $url, $auth_login, $auth_pass);
1520 }
1521
1522 if (is_html($contents)) {
1523 $feedUrls = get_feeds_from_html($url, $contents);
1524
1525 if (count($feedUrls) == 0) {
1526 return array("code" => 3);
1527 } else if (count($feedUrls) > 1) {
1528 return array("code" => 4, "feeds" => $feedUrls);
1529 }
1530 //use feed url as new URL
1531 $url = key($feedUrls);
1532 }
1533
1534 if ($cat_id == "0" || !$cat_id) {
1535 $cat_qpart = "NULL";
1536 } else {
1537 $cat_qpart = "'$cat_id'";
1538 }
1539
1540 $result = db_query(
1541 "SELECT id FROM ttrss_feeds
1542 WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
1543
1544 $auth_pass_encrypted = 'false';
1545 $auth_pass = db_escape_string($auth_pass);
1546
1547 if (db_num_rows($result) == 0) {
1548 $result = db_query(
1549 "INSERT INTO ttrss_feeds
1550 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted)
1551 VALUES ('".$_SESSION["uid"]."', '$url',
1552 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass', 0, $auth_pass_encrypted)");
1553
1554 $result = db_query(
1555 "SELECT id FROM ttrss_feeds WHERE feed_url = '$url'
1556 AND owner_uid = " . $_SESSION["uid"]);
1557
1558 $feed_id = db_fetch_result($result, 0, "id");
1559
1560 if ($feed_id) {
1561 set_basic_feed_info($feed_id);
1562 }
1563
1564 return array("code" => 1, "feed_id" => (int) $feed_id);
1565 } else {
1566 return array("code" => 0, "feed_id" => (int) db_fetch_result($result, 0, "id"));
1567 }
1568 }
1569
1570 static function getFeedIcon($id) {
1571 switch ($id) {
1572 case 0:
1573 return "images/archive.png";
1574 break;
1575 case -1:
1576 return "images/star.png";
1577 break;
1578 case -2:
1579 return "images/feed.png";
1580 break;
1581 case -3:
1582 return "images/fresh.png";
1583 break;
1584 case -4:
1585 return "images/folder.png";
1586 break;
1587 case -6:
1588 return "images/time.png";
1589 break;
1590 default:
1591 if ($id < LABEL_BASE_INDEX) {
1592 return "images/label.png";
1593 } else {
1594 if (file_exists(ICONS_DIR . "/$id.ico"))
1595 return ICONS_URL . "/$id.ico";
1596 }
1597 break;
1598 }
1599
1600 return false;
1601 }
1602
1603 static function getFeedTitle($id, $cat = false) {
1604 if ($cat) {
1605 return getCategoryTitle($id);
1606 } else if ($id == -1) {
1607 return __("Starred articles");
1608 } else if ($id == -2) {
1609 return __("Published articles");
1610 } else if ($id == -3) {
1611 return __("Fresh articles");
1612 } else if ($id == -4) {
1613 return __("All articles");
1614 } else if ($id === 0 || $id === "0") {
1615 return __("Archived articles");
1616 } else if ($id == -6) {
1617 return __("Recently read");
1618 } else if ($id < LABEL_BASE_INDEX) {
1619 $label_id = feed_to_label_id($id);
1620 $result = db_query("SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
1621 if (db_num_rows($result) == 1) {
1622 return db_fetch_result($result, 0, "caption");
1623 } else {
1624 return "Unknown label ($label_id)";
1625 }
1626
1627 } else if (is_numeric($id) && $id > 0) {
1628 $result = db_query("SELECT title FROM ttrss_feeds WHERE id = '$id'");
1629 if (db_num_rows($result) == 1) {
1630 return db_fetch_result($result, 0, "title");
1631 } else {
1632 return "Unknown feed ($id)";
1633 }
1634 } else {
1635 return $id;
1636 }
1637 }
1638
1639 static function getCategoryUnread($cat, $owner_uid = false) {
1640
1641 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1642
1643 if ($cat >= 0) {
1644
1645 if ($cat != 0) {
1646 $cat_query = "cat_id = '$cat'";
1647 } else {
1648 $cat_query = "cat_id IS NULL";
1649 }
1650
1651 $result = db_query("SELECT id FROM ttrss_feeds WHERE $cat_query
1652 AND owner_uid = " . $owner_uid);
1653
1654 $cat_feeds = array();
1655 while ($line = db_fetch_assoc($result)) {
1656 array_push($cat_feeds, "feed_id = " . $line["id"]);
1657 }
1658
1659 if (count($cat_feeds) == 0) return 0;
1660
1661 $match_part = implode(" OR ", $cat_feeds);
1662
1663 $result = db_query("SELECT COUNT(int_id) AS unread
1664 FROM ttrss_user_entries
1665 WHERE unread = true AND ($match_part)
1666 AND owner_uid = " . $owner_uid);
1667
1668 $unread = 0;
1669
1670 # this needs to be rewritten
1671 while ($line = db_fetch_assoc($result)) {
1672 $unread += $line["unread"];
1673 }
1674
1675 return $unread;
1676 } else if ($cat == -1) {
1677 return getFeedUnread(-1) + getFeedUnread(-2) + getFeedUnread(-3) + getFeedUnread(0);
1678 } else if ($cat == -2) {
1679
1680 $result = db_query("
1681 SELECT COUNT(unread) AS unread FROM
1682 ttrss_user_entries, ttrss_user_labels2
1683 WHERE article_id = ref_id AND unread = true
1684 AND ttrss_user_entries.owner_uid = '$owner_uid'");
1685
1686 $unread = db_fetch_result($result, 0, "unread");
1687
1688 return $unread;
1689
1690 }
1691 }
1692
1693 // only accepts real cats (>= 0)
1694 static function getCategoryChildrenUnread($cat, $owner_uid = false) {
1695 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
1696
1697 $result = db_query("SELECT id FROM ttrss_feed_categories WHERE parent_cat = '$cat'
1698 AND owner_uid = $owner_uid");
1699
1700 $unread = 0;
1701
1702 while ($line = db_fetch_assoc($result)) {
1703 $unread += Feeds::getCategoryUnread($line["id"], $owner_uid);
1704 $unread += Feeds::getCategoryChildrenUnread($line["id"], $owner_uid);
1705 }
1706
1707 return $unread;
1708 }
1709
1710 }
1711