]> git.wh0rd.org Git - tt-rss.git/blob - classes/feeds.php
fix merge conflict with stylesheet tags
[tt-rss.git] / classes / feeds.php
1 <?php
2 require_once "colors.php";
3
4 class Feeds extends Handler_Protected {
5
6         function csrf_ignore($method) {
7                 $csrf_ignored = array("index");
8
9                 return array_search($method, $csrf_ignored) !== false;
10         }
11
12         private function make_gradient($end, $class) {
13                 $start = $class == "even" ? "#f0f0f0" : "#ffffff";
14
15                 return "style='background: linear-gradient(left , $start 6%, $end 100%);
16                         background: -o-linear-gradient(left , $start 6%, $end 100%);
17                         background: -moz-linear-gradient(left , $start 6%, $end 100%);
18                         background: -webkit-linear-gradient(left , $start 6%, $end 100%);
19                         background: -ms-linear-gradient(left , $start 6%, $end 100%);
20                         background: -webkit-gradient(linear, left top, right top,
21                                 color-stop(0.06, $start), color-stop(1, $end));'";
22         }
23
24         private function format_headline_subtoolbar($feed_site_url, $feed_title,
25                         $feed_id, $is_cat, $search, $match_on,
26                         $search_mode, $view_mode, $error) {
27
28                 $page_prev_link = "viewFeedGoPage(-1)";
29                 $page_next_link = "viewFeedGoPage(1)";
30                 $page_first_link = "viewFeedGoPage(0)";
31
32                 $catchup_page_link = "catchupPage()";
33                 $catchup_feed_link = "catchupCurrentFeed()";
34                 $catchup_sel_link = "catchupSelection()";
35
36                 $archive_sel_link = "archiveSelection()";
37                 $delete_sel_link = "deleteSelection()";
38
39                 $sel_all_link = "selectArticles('all')";
40                 $sel_unread_link = "selectArticles('unread')";
41                 $sel_none_link = "selectArticles('none')";
42                 $sel_inv_link = "selectArticles('invert')";
43
44                 $tog_unread_link = "selectionToggleUnread()";
45                 $tog_marked_link = "selectionToggleMarked()";
46                 $tog_published_link = "selectionTogglePublished()";
47
48                 $set_score_link = "setSelectionScore()";
49
50                 if ($is_cat) $cat_q = "&is_cat=$is_cat";
51
52                 if ($search) {
53                         $search_q = "&q=$search&m=$match_on&smode=$search_mode";
54                 } else {
55                         $search_q = "";
56                 }
57
58                 $rss_link = htmlspecialchars(get_self_url_prefix() .
59                         "/public.php?op=rss&id=$feed_id$cat_q$search_q");
60
61                 // right part
62
63                 $reply .= "<span class='r'>";
64                 $reply .= "<span id='feed_title'>";
65
66                 if ($feed_site_url) {
67                         $target = "target=\"_blank\"";
68                         $reply .= "<a title=\"".__("Visit the website")."\" $target href=\"$feed_site_url\">".
69                                 truncate_string($feed_title,30)."</a>";
70
71                         if ($error) {
72                                 $reply .= " (<span class=\"error\" title=\"$error\">Error</span>)";
73                         }
74
75                 } else {
76                         $reply .= $feed_title;
77                 }
78
79                 $reply .= "</span>";
80
81                 $reply .= "
82                         <a href=\"#\"
83                                 title=\"".__("View as RSS feed")."\"
84                                 onclick=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">
85                                 <img class=\"noborder\" style=\"vertical-align : middle\" src=\"images/pub_set.svg\"></a>";
86
87                 $reply .= "</span>";
88
89                 // left part
90
91                 $reply .= __('Select:')."
92                         <a href=\"#\" onclick=\"$sel_all_link\">".__('All')."</a>,
93                         <a href=\"#\" onclick=\"$sel_unread_link\">".__('Unread')."</a>,
94                         <a href=\"#\" onclick=\"$sel_inv_link\">".__('Invert')."</a>,
95                         <a href=\"#\" onclick=\"$sel_none_link\">".__('None')."</a></li>";
96
97                 $reply .= " ";
98
99                 $reply .= "<select dojoType=\"dijit.form.Select\"
100                         onchange=\"headlineActionsChange(this)\">";
101                 $reply .= "<option value=\"false\">".__('More...')."</option>";
102
103                 $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection toggle:')."</option>";
104
105                 $reply .= "<option value=\"$tog_unread_link\">".__('Unread')."</option>
106                         <option value=\"$tog_marked_link\">".__('Starred')."</option>
107                         <option value=\"$tog_published_link\">".__('Published')."</option>";
108
109                 $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection:')."</option>";
110
111                 $reply .= "<option value=\"$catchup_sel_link\">".__('Mark as read')."</option>";
112                 $reply .= "<option value=\"$set_score_link\">".__('Set score')."</option>";
113
114                 if ($feed_id != "0") {
115                         $reply .= "<option value=\"$archive_sel_link\">".__('Archive')."</option>";
116                 } else {
117                         $reply .= "<option value=\"$archive_sel_link\">".__('Move back')."</option>";
118                         $reply .= "<option value=\"$delete_sel_link\">".__('Delete')."</option>";
119
120                 }
121
122                 global $pluginhost;
123
124                 if ($pluginhost->get_plugin("mail")) {
125                         $reply .= "<option value=\"emailArticle(false)\">".__('Forward by email').
126                                 "</option>";
127                 }
128
129                 if ($pluginhost->get_plugin("mailto")) {
130                         $reply .= "<option value=\"mailtoArticle(false)\">".__('Forward by email').
131                                 "</option>";
132                 }
133
134                 $reply .= "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
135
136                 $reply .= "<option value=\"catchupPage()\">".__('Mark as read')."</option>";
137
138                 $reply .= "<option value=\"displayDlg('generatedFeed', '$feed_id:$is_cat:$rss_link')\">".__('View as RSS')."</option>";
139
140                 $reply .= "</select>";
141
142                 //$reply .= "</div>";
143
144                 //$reply .= "</h2";
145
146                 return $reply;
147         }
148
149         private function format_headlines_list($feed, $method, $view_mode, $limit, $cat_view,
150                                         $next_unread_feed, $offset, $vgr_last_feed = false,
151                                         $override_order = false, $include_children = false) {
152
153                 if (isset($_REQUEST["DevForceUpdate"]))
154                         header("Content-Type: text/plain");
155
156                 $disable_cache = false;
157
158                 $reply = array();
159
160                 $timing_info = microtime(true);
161
162                 $topmost_article_ids = array();
163
164                 if (!$offset) $offset = 0;
165                 if ($method == "undefined") $method = "";
166
167                 $method_split = explode(":", $method);
168
169                 if ($method == "ForceUpdate" && $feed > 0 && is_numeric($feed)) {
170                         // Update the feed if required with some basic flood control
171
172                         $result = db_query($this->link,
173                                 "SELECT cache_images,".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
174                                         FROM ttrss_feeds WHERE id = '$feed'");
175
176                                 if (db_num_rows($result) != 0) {
177                                         $last_updated = strtotime(db_fetch_result($result, 0, "last_updated"));
178                                         $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
179
180                                         if (!$cache_images && time() - $last_updated > 120 || isset($_REQUEST['DevForceUpdate'])) {
181                                                 include "rssfuncs.php";
182                                                 update_rss_feed($this->link, $feed, true, true);
183                                         } else {
184                                                 db_query($this->link, "UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
185                                                         WHERE id = '$feed'");
186                                         }
187                                 }
188                 }
189
190                 if ($method_split[0] == "MarkAllReadGR")  {
191                         catchup_feed($this->link, $method_split[1], false);
192                 }
193
194                 // FIXME: might break tag display?
195
196                 if (is_numeric($feed) && $feed > 0 && !$cat_view) {
197                         $result = db_query($this->link,
198                                 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
199
200                         if (db_num_rows($result) == 0) {
201                                 $reply['content'] = "<div align='center'>".__('Feed not found.')."</div>";
202                         }
203                 }
204
205                 @$search = db_escape_string($_REQUEST["query"]);
206
207                 if ($search) {
208                         $disable_cache = true;
209                 }
210
211                 @$search_mode = db_escape_string($_REQUEST["search_mode"]);
212                 $match_on = "both"; // deprecated, TODO: remove
213
214                 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
215
216 //              error_log("format_headlines_list: [" . $feed . "] method [" . $method . "]");
217                 if( $search_mode == '' && $method != '' ){
218                     $search_mode = $method;
219                 }
220 //              error_log("search_mode: " . $search_mode);
221                 $qfh_ret = queryFeedHeadlines($this->link, $feed, $limit, $view_mode, $cat_view,
222                         $search, $search_mode, $match_on, $override_order, $offset, 0,
223                         false, 0, $include_children);
224
225                 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
226
227                 $result = $qfh_ret[0];
228                 $feed_title = $qfh_ret[1];
229                 $feed_site_url = $qfh_ret[2];
230                 $last_error = $qfh_ret[3];
231
232                 $vgroup_last_feed = $vgr_last_feed;
233
234                 $reply['toolbar'] = $this->format_headline_subtoolbar($feed_site_url,
235                         $feed_title,
236                         $feed, $cat_view, $search, $match_on, $search_mode, $view_mode,
237                         $last_error);
238
239                 $headlines_count = db_num_rows($result);
240
241                 /* if (get_pref($this->link, 'COMBINED_DISPLAY_MODE')) {
242                         $button_plugins = array();
243                         foreach (explode(",", ARTICLE_BUTTON_PLUGINS) as $p) {
244                                 $pclass = "button_" . trim($p);
245
246                                 if (class_exists($pclass)) {
247                                         $plugin = new $pclass($link);
248                                         array_push($button_plugins, $plugin);
249                                 }
250                         }
251                 } */
252
253                 global $pluginhost;
254
255                 if (db_num_rows($result) > 0) {
256
257                         $lnum = $offset;
258
259                         $num_unread = 0;
260                         $cur_feed_title = '';
261
262                         $fresh_intl = get_pref($this->link, "FRESH_ARTICLE_MAX_AGE") * 60 * 60;
263
264                         if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PS", $timing_info);
265
266                         $expand_cdm = get_pref($this->link, 'CDM_EXPANDED');
267
268                         while ($line = db_fetch_assoc($result)) {
269                                 $class = ($lnum % 2) ? "even" : "odd";
270
271                                 $id = $line["id"];
272                                 $feed_id = $line["feed_id"];
273                                 $label_cache = $line["label_cache"];
274                                 $labels = false;
275                                 $label_row_style = "";
276
277                                 if ($label_cache) {
278                                         $label_cache = json_decode($label_cache, true);
279
280                                         if ($label_cache) {
281                                                 if ($label_cache["no-labels"] == 1)
282                                                         $labels = array();
283                                                 else
284                                                         $labels = $label_cache;
285                                         }
286                                 }
287
288                                 if (!is_array($labels)) $labels = get_article_labels($this->link, $id);
289
290                                 if (count($labels) > 0) {
291                                         for ($i = 0; $i < min(4, count($labels)); $i++) {
292                                                 $bg = rgb2hsl(_color_unpack($labels[$i][3]));
293
294                                                 if ($bg && $bg[1] > 0) {
295                                                         $bg[1] = 0.1;
296                                                         $bg[2] = 1;
297
298                                                         $bg = _color_pack(hsl2rgb($bg));
299                                                         $label_row_style = $this->make_gradient($bg, $class);;
300
301                                                         break;
302                                                 }
303                                         }
304                                 }
305
306                                 $labels_str = "<span id=\"HLLCTR-$id\">";
307                                 $labels_str .= format_article_labels($labels, $id);
308                                 $labels_str .= "</span>";
309
310                                 if (count($topmost_article_ids) < 3) {
311                                         array_push($topmost_article_ids, $id);
312                                 }
313
314                                 if ($line["unread"] == "t" || $line["unread"] == "1") {
315                                         $class .= " Unread";
316                                         ++$num_unread;
317                                         $is_unread = true;
318                                 } else {
319                                         $is_unread = false;
320                                 }
321
322                                 if ($line["marked"] == "t" || $line["marked"] == "1") {
323                                         $marked_pic = "<img id=\"FMPIC-$id\"
324                                                 src=\"".theme_image($this->link, 'images/mark_set.svg')."\"
325                                                 class=\"markedPic\" alt=\"Unstar article\"
326                                                 onclick='javascript:toggleMark($id)'>";
327                                 } else {
328                                         $marked_pic = "<img id=\"FMPIC-$id\"
329                                                 src=\"".theme_image($this->link, 'images/mark_unset.svg')."\"
330                                                 class=\"markedPic\" alt=\"Star article\"
331                                                 onclick='javascript:toggleMark($id)'>";
332                                 }
333
334                                 if ($line["published"] == "t" || $line["published"] == "1") {
335                                         $published_pic = "<img id=\"FPPIC-$id\" src=\"".theme_image($this->link,
336                                                 'images/pub_set.svg')."\"
337                                                 class=\"markedPic\"
338                                                 alt=\"Unpublish article\" onclick='javascript:togglePub($id)'>";
339                                 } else {
340                                         $published_pic = "<img id=\"FPPIC-$id\" src=\"".theme_image($this->link,
341                                                 'images/pub_unset.svg')."\"
342                                                 class=\"markedPic\"
343                                                 alt=\"Publish article\" onclick='javascript:togglePub($id)'>";
344                                 }
345
346 #                               $content_link = "<a target=\"_blank\" href=\"".$line["link"]."\">" .
347 #                                       $line["title"] . "</a>";
348
349 #                               $content_link = "<a
350 #                                       href=\"" . htmlspecialchars($line["link"]) . "\"
351 #                                       onclick=\"view($id,$feed_id);\">" .
352 #                                       $line["title"] . "</a>";
353
354 #                               $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
355 #                                       $line["title"] . "</a>";
356
357                                 $updated_fmt = make_local_datetime($this->link, $line["updated_noms"], false);
358
359                                 if (get_pref($this->link, 'SHOW_CONTENT_PREVIEW')) {
360                                         $content_preview = truncate_string(strip_tags($line["content_preview"]),
361                                                 100);
362                                 }
363
364                                 $score = $line["score"];
365
366                                 $score_pic = theme_image($this->link,
367                                         "images/" . get_score_pic($score));
368
369 /*                              $score_title = __("(Click to change)");
370                                 $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\"
371                                         onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">"; */
372
373                                 $score_pic = "<img class='hlScorePic' score='$score' onclick='changeScore($id, this)' src=\"$score_pic\"
374                                         title=\"$score\">";
375
376                                 if ($score > 500) {
377                                         $hlc_suffix = "H";
378                                 } else if ($score < -100) {
379                                         $hlc_suffix = "L";
380                                 } else {
381                                         $hlc_suffix = "";
382                                 }
383
384                                 $entry_author = $line["author"];
385
386                                 if ($entry_author) {
387                                         $entry_author = " - $entry_author";
388                                 }
389
390                                 $has_feed_icon = feed_has_icon($feed_id);
391
392                                 if ($has_feed_icon) {
393                                         $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
394                                 } else {
395                                         $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/pub_set.svg\" alt=\"\">";
396                                 }
397
398                                 $entry_site_url = $line["site_url"];
399
400                                 if (!get_pref($this->link, 'COMBINED_DISPLAY_MODE')) {
401
402                                         if (get_pref($this->link, 'VFEED_GROUP_BY_FEED')) {
403                                                 if ($feed_id != $vgroup_last_feed && $line["feed_title"]) {
404
405                                                         $cur_feed_title = $line["feed_title"];
406                                                         $vgroup_last_feed = $feed_id;
407
408                                                         $cur_feed_title = htmlspecialchars($cur_feed_title);
409
410                                                         $vf_catchup_link = "(<a class='catchup' onclick='catchupFeedInGroup($feed_id);' href='#'>".__('Mark as read')."</a>)";
411
412                                                         $reply['content'] .= "<div class='cdmFeedTitle'>".
413                                                                 "<div style=\"float : right\">$feed_icon_img</div>".
414                                                                 "<a class='title' href=\"#\" onclick=\"viewfeed($feed_id)\">".
415                                                                 $line["feed_title"]."</a> $vf_catchup_link</div>";
416
417                                                 }
418                                         }
419
420                                         $mouseover_attrs = "onmouseover='postMouseIn($id)'
421                                                 onmouseout='postMouseOut($id)'";
422
423                                         $reply['content'] .= "<div class='$class' id='RROW-$id' $label_row_style $mouseover_attrs>";
424
425                                         $reply['content'] .= "<div class='hlLeft'>";
426
427                                         $reply['content'] .= "<input dojoType=\"dijit.form.CheckBox\"
428                                                         type=\"checkbox\" onclick=\"toggleSelectRow2(this)\"
429                                                         id=\"RCHK-$id\">";
430
431                                         $reply['content'] .= "$marked_pic";
432                                         $reply['content'] .= "$published_pic";
433
434                                         $reply['content'] .= "</div>";
435
436                                         $reply['content'] .= "<div onclick='return hlClicked(event, $id)'
437                                                 class=\"hlTitle\"><span class='hlContent$hlc_suffix'>";
438                                         $reply['content'] .= "<a id=\"RTITLE-$id\"
439                                                 href=\"" . htmlspecialchars($line["link"]) . "\"
440                                                 onclick=\"\">" .
441                                                 truncate_string($line["title"], 200);
442
443                                         if (get_pref($this->link, 'SHOW_CONTENT_PREVIEW')) {
444                                                 if ($content_preview) {
445                                                         $reply['content'] .= "<span class=\"contentPreview\"> - $content_preview</span>";
446                                                 }
447                                         }
448
449                                         $reply['content'] .= "</a></span>";
450
451                                         $reply['content'] .= $labels_str;
452
453                                         $reply['content'] .= "</div>";
454
455                                         $reply['content'] .= "<span class=\"hlUpdated\">";
456
457                                         if (@$line["feed_title"]) {
458                                                         $reply['content'] .= "<div class=\"hlFeed\">
459                                                                 <a href=\"#\" onclick=\"viewfeed($feed_id)\">".
460                                                                 $line["feed_title"]."</a>
461                                                         </div>";
462                                                 }
463
464                                         $reply['content'] .= "$updated_fmt</span>";
465                                         $reply['content'] .= "<div class=\"hlRight\">";
466
467                                         $reply['content'] .= $score_pic;
468
469                                         if ($line["feed_title"] && !get_pref($this->link, 'VFEED_GROUP_BY_FEED')) {
470
471                                                 $reply['content'] .= "<span onclick=\"viewfeed($feed_id)\"
472                                                         style=\"cursor : pointer\"
473                                                         title=\"".htmlspecialchars($line['feed_title'])."\">
474                                                         $feed_icon_img<span>";
475                                         }
476
477                                         $reply['content'] .= "</div>";
478                                         $reply['content'] .= "</div>";
479
480                                 } else {
481
482                                         $line["tags"] = get_article_tags($this->link, $id, $_SESSION["uid"], $line["tag_cache"]);
483                                         unset($line["tag_cache"]);
484
485                                         $line["content"] = sanitize($this->link, $line["content_preview"],
486                                                         sql_bool_to_bool($line['hide_images']), false, $entry_site_url);
487
488                                         foreach ($pluginhost->get_hooks($pluginhost::HOOK_RENDER_ARTICLE_CDM) as $p) {
489                                                 $line = $p->hook_render_article_cdm($line);
490                                         }
491
492                                         if (get_pref($this->link, 'VFEED_GROUP_BY_FEED') && $line["feed_title"]) {
493                                                 if ($feed_id != $vgroup_last_feed) {
494
495                                                         $cur_feed_title = $line["feed_title"];
496                                                         $vgroup_last_feed = $feed_id;
497
498                                                         $cur_feed_title = htmlspecialchars($cur_feed_title);
499
500                                                         $vf_catchup_link = "(<a class='catchup' onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>".__('mark as read')."</a>)";
501
502                                                         $has_feed_icon = feed_has_icon($feed_id);
503
504                                                         if ($has_feed_icon) {
505                                                                 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
506                                                         } else {
507                                                                 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
508                                                         }
509
510                                                         $reply['content'] .= "<div class='cdmFeedTitle'>".
511                                                                 "<div style=\"float : right\">$feed_icon_img</div>".
512                                                                 "<a href=\"#\" class='title' onclick=\"viewfeed($feed_id)\">".
513                                                                 $line["feed_title"]."</a> $vf_catchup_link</div>";
514                                                 }
515                                         }
516
517                                         $mouseover_attrs = "onmouseover='postMouseIn($id)'
518                                                 onmouseout='postMouseOut($id)'";
519
520                                         $expanded_class = $expand_cdm ? "expanded" : "";
521
522                                         $reply['content'] .= "<div class=\"cdm $expanded_class $class\"
523                                                 id=\"RROW-$id\" $mouseover_attrs'>";
524
525                                         $reply['content'] .= "<div class=\"cdmHeader\">";
526
527                                         $reply['content'] .= "<div style=\"vertical-align : middle\">";
528
529                                         $reply['content'] .= "<input dojoType=\"dijit.form.CheckBox\"
530                                                         type=\"checkbox\" onclick=\"toggleSelectRow2(this, false, true)\"
531                                                         id=\"RCHK-$id\">";
532
533                                         $reply['content'] .= "$marked_pic";
534                                         $reply['content'] .= "$published_pic";
535
536                                         $reply['content'] .= "</div>";
537
538                                         $reply['content'] .= "<div id=\"PTITLE-FULL-$id\" style=\"display : none\">" .
539                                                 htmlspecialchars(strip_tags($line['title'])) . "</div>";
540
541                                         $reply['content'] .= "<span id=\"RTITLE-$id\"
542                                                 onclick=\"return cdmClicked(event, $id);\"
543                                                 class=\"titleWrap$hlc_suffix\">
544                                                 <a class=\"title\"
545                                                 title=\"".htmlspecialchars($line['title'])."\"
546                                                 target=\"_blank\" href=\"".
547                                                 htmlspecialchars($line["link"])."\">".
548                                                 $line["title"] .
549                                                 " <span class=\"author\">$entry_author</span></a>";
550
551                                         $reply['content'] .= $labels_str;
552
553                                         $reply['content'] .= "<span class='collapseBtn' style='display : none'>
554                                                 <img src=\"images/collapse.png\" onclick=\"cdmCollapseArticle(event, $id)\"
555                                                 title=\"".__("Collapse article")."\"/></span>";
556
557                                         if (!$expand_cdm)
558                                                 $content_hidden = "style=\"display : none\"";
559                                         else
560                                                 $excerpt_hidden = "style=\"display : none\"";
561
562                                         $reply['content'] .= "<span $excerpt_hidden
563                                                 id=\"CEXC-$id\" class=\"cdmExcerpt\"> - $content_preview</span>";
564                                         $reply['content'] .= "</span>";
565
566                                         if (!get_pref($this->link, 'VFEED_GROUP_BY_FEED')) {
567                                                 if (@$line["feed_title"]) {
568                                                         $reply['content'] .= "<div class=\"hlFeed\">
569                                                                 <a href=\"#\" onclick=\"viewfeed($feed_id)\">".
570                                                                 $line["feed_title"]."</a>
571                                                         </div>";
572                                                 }
573                                         }
574
575                                         $reply['content'] .= "<span class='updated'>$updated_fmt</span>";
576
577                                         $reply['content'] .= "<div style=\"vertical-align : middle\">";
578                                         $reply['content'] .= "$score_pic";
579
580                                         if (!get_pref($this->link, "VFEED_GROUP_BY_FEED") && $line["feed_title"]) {
581                                                 $reply['content'] .= "<span style=\"cursor : pointer\"
582                                                         title=\"".htmlspecialchars($line["feed_title"])."\"
583                                                         onclick=\"viewfeed($feed_id)\">$feed_icon_img</span>";
584                                         }
585                                         $reply['content'] .= "</div>";
586
587                                         $reply['content'] .= "</div>";
588
589                                         $reply['content'] .= "<div class=\"cdmContent\" $content_hidden
590                                                 onclick=\"return cdmClicked(event, $id);\"
591                                                 id=\"CICD-$id\">";
592
593                                         $reply['content'] .= "<div id=\"POSTNOTE-$id\">";
594                                         if ($line['note']) {
595                                                 $reply['content'] .= format_article_note($id, $line['note']);
596                                         }
597                                         $reply['content'] .= "</div>";
598
599
600                                         $reply['content'] .= "<div class=\"cdmContentInner\">";
601
602                         if ($line["orig_feed_id"]) {
603
604                                 $tmp_result = db_query($this->link, "SELECT * FROM ttrss_archived_feeds
605                                         WHERE id = ".$line["orig_feed_id"]);
606
607                                                 if (db_num_rows($tmp_result) != 0) {
608
609                                                         $reply['content'] .= "<div clear='both'>";
610                                                         $reply['content'] .= __("Originally from:");
611
612                                                         $reply['content'] .= "&nbsp;";
613
614                                                         $tmp_line = db_fetch_assoc($tmp_result);
615
616                                                         $reply['content'] .= "<a target='_blank'
617                                                                 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
618                                                                 $tmp_line['title'] . "</a>";
619
620                                                         $reply['content'] .= "&nbsp;";
621
622                                                         $reply['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
623                                                         $reply['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_unset.svg'></a>";
624
625                                                         $reply['content'] .= "</div>";
626                                                 }
627                                         }
628
629                                         $reply['content'] .= "<span id=\"CWRAP-$id\">";
630
631                                         if (!$expand_cdm) {
632                                                 $reply['content'] .= "<span id=\"CENCW-$id\">";
633                                                 $reply['content'] .= htmlspecialchars($line["content"]);
634                                                 $reply['content'] .= "</span.";
635
636                                         } else {
637                                                 $reply['content'] .= $line["content"];
638                                         }
639
640                                         $reply['content'] .= "</span>";
641
642                                         $always_display_enclosures = sql_bool_to_bool($line["always_display_enclosures"]);
643
644                                         $reply['content'] .= format_article_enclosures($this->link, $id, $always_display_enclosures,
645                                                 $line["content"]);
646
647                                         $reply['content'] .= "</div>";
648
649                                         $reply['content'] .= "<div class=\"cdmFooter\">";
650
651                                         $tags_str = format_tags_string($line["tags"], $id);
652
653                                         $reply['content'] .= "<img src='".theme_image($this->link,
654                                                         'images/tag.png')."' alt='Tags' title='Tags'>
655                                                 <span id=\"ATSTR-$id\">$tags_str</span>
656                                                 <a title=\"".__('Edit tags for this article')."\"
657                                                 href=\"#\" onclick=\"editArticleTags($id, $feed_id, true)\">(+)</a>";
658
659                                         $num_comments = $line["num_comments"];
660                                         $entry_comments = "";
661
662                                         if ($num_comments > 0) {
663                                                 if ($line["comments"]) {
664                                                         $comments_url = htmlspecialchars($line["comments"]);
665                                                 } else {
666                                                         $comments_url = htmlspecialchars($line["link"]);
667                                                 }
668                                                 $entry_comments = "<a target='_blank' href=\"$comments_url\">$num_comments comments</a>";
669                                         } else {
670                                                 if ($line["comments"] && $line["link"] != $line["comments"]) {
671                                                         $entry_comments = "<a target='_blank' href=\"".htmlspecialchars($line["comments"])."\">comments</a>";
672                                                 }
673                                         }
674
675                                         if ($entry_comments) $reply['content'] .= "&nbsp;($entry_comments)";
676
677                                         $reply['content'] .= "<div style=\"float : right\">";
678
679                                         foreach ($pluginhost->get_hooks($pluginhost::HOOK_ARTICLE_BUTTON) as $p) {
680                                                 $reply['content'] .= $p->hook_article_button($line);
681                                         }
682
683                                         $reply['content'] .= "</div>";
684                                         $reply['content'] .= "</div>";
685
686                                         $reply['content'] .= "</div>";
687
688                                         $reply['content'] .= "</div>";
689
690                                 }
691
692                                 ++$lnum;
693                         }
694
695                         if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PE", $timing_info);
696
697                 } else {
698                         $message = "";
699
700                         switch ($view_mode) {
701                                 case "unread":
702                                         $message = __("No unread articles found to display.");
703                                         break;
704                                 case "updated":
705                                         $message = __("No updated articles found to display.");
706                                         break;
707                                 case "marked":
708                                         $message = __("No starred articles found to display.");
709                                         break;
710                                 default:
711                                         if ($feed < -10) {
712                                                 $message = __("No articles found to display. You can assign articles to labels manually (see the Actions menu above) or use a filter.");
713                                         } else {
714                                                 $message = __("No articles found to display.");
715                                         }
716                         }
717
718                         if (!$offset && $message) {
719                                 $reply['content'] .= "<div class='whiteBox'>$message";
720
721                                 $reply['content'] .= "<p class=\"small\"><span class=\"insensitive\">";
722
723                                 $result = db_query($this->link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
724                                         WHERE owner_uid = " . $_SESSION['uid']);
725
726                                 $last_updated = db_fetch_result($result, 0, "last_updated");
727                                 $last_updated = make_local_datetime($this->link, $last_updated, false);
728
729                                 $reply['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
730
731                                 $result = db_query($this->link, "SELECT COUNT(id) AS num_errors
732                                         FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
733
734                                 $num_errors = db_fetch_result($result, 0, "num_errors");
735
736                                 if ($num_errors > 0) {
737                                         $reply['content'] .= "<br/>";
738                                         $reply['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
739                                                 __('Some feeds have update errors (click for details)')."</a>";
740                                 }
741                                 $reply['content'] .= "</span></p></div>";
742                         }
743                 }
744
745                 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H2", $timing_info);
746
747                 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache,
748                         $vgroup_last_feed, $reply);
749         }
750
751         function catchupAll() {
752                 db_query($this->link, "UPDATE ttrss_user_entries SET
753                                                 last_read = NOW(), unread = false WHERE unread = true AND owner_uid = " . $_SESSION["uid"]);
754                 ccache_zero_all($this->link, $_SESSION["uid"]);
755         }
756
757         function view() {
758                 $timing_info = microtime(true);
759
760                 $reply = array();
761
762                 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
763
764                 $omode = db_escape_string($_REQUEST["omode"]);
765
766                 $feed = db_escape_string($_REQUEST["feed"]);
767                 $method = db_escape_string($_REQUEST["m"]);
768                 $view_mode = db_escape_string($_REQUEST["view_mode"]);
769                 $limit = (int) get_pref($this->link, "DEFAULT_ARTICLE_LIMIT");
770                 @$cat_view = $_REQUEST["cat"] == "true";
771                 @$next_unread_feed = db_escape_string($_REQUEST["nuf"]);
772                 @$offset = db_escape_string($_REQUEST["skip"]);
773                 @$vgroup_last_feed = db_escape_string($_REQUEST["vgrlf"]);
774                 $order_by = db_escape_string($_REQUEST["order_by"]);
775
776                 if (is_numeric($feed)) $feed = (int) $feed;
777
778                 /* Feed -5 is a special case: it is used to display auxiliary information
779                  * when there's nothing to load - e.g. no stuff in fresh feed */
780
781                 if ($feed == -5) {
782                         print json_encode($this->generate_dashboard_feed($this->link));
783                         return;
784                 }
785
786                 $result = false;
787
788                 if ($feed < -10) {
789                         $label_feed = -11-$feed;
790                         $result = db_query($this->link, "SELECT id FROM ttrss_labels2 WHERE
791                                                         id = '$label_feed' AND owner_uid = " . $_SESSION['uid']);
792                 } else if (!$cat_view && is_numeric($feed) && $feed > 0) {
793                         $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
794                                                         id = '$feed' AND owner_uid = " . $_SESSION['uid']);
795                 } else if ($cat_view && is_numeric($feed) && $feed > 0) {
796                         $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE
797                                                         id = '$feed' AND owner_uid = " . $_SESSION['uid']);
798                 }
799
800                 if ($result && db_num_rows($result) == 0) {
801                         print json_encode($this->generate_error_feed($this->link, __("Feed not found.")));
802                         return;
803                 }
804
805                 /* Updating a label ccache means recalculating all of the caches
806                  * so for performance reasons we don't do that here */
807
808                 if ($feed >= 0) {
809                         ccache_update($this->link, $feed, $_SESSION["uid"], $cat_view);
810                 }
811
812                 set_pref($this->link, "_DEFAULT_VIEW_MODE", $view_mode);
813                 set_pref($this->link, "_DEFAULT_VIEW_LIMIT", $limit);
814                 set_pref($this->link, "_DEFAULT_VIEW_ORDER_BY", $order_by);
815
816                 if (!$cat_view && is_numeric($feed) && $feed > 0) {
817                         db_query($this->link, "UPDATE ttrss_feeds SET last_viewed = NOW()
818                                                         WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
819                 }
820
821                 $reply['headlines'] = array();
822
823                 if (!$next_unread_feed)
824                         $reply['headlines']['id'] = $feed;
825                 else
826                         $reply['headlines']['id'] = $next_unread_feed;
827
828                 $reply['headlines']['is_cat'] = (bool) $cat_view;
829
830                 $override_order = false;
831
832                 if (get_pref($this->link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) {
833                         $date_sort_field = "updated";
834                 } else {
835                         $date_sort_field = "date_entered";
836                 }
837
838                 switch ($order_by) {
839                         case "date":
840                                 if (get_pref($this->link, 'REVERSE_HEADLINES', $owner_uid)) {
841                                         $override_order = "$date_sort_field";
842                                 } else {
843                                         $override_order = "$date_sort_field DESC";
844                                 }
845                                 break;
846
847                         case "title":
848                                 if (get_pref($this->link, 'REVERSE_HEADLINES', $owner_uid)) {
849                                         $override_order = "title DESC, $date_sort_field";
850                                 } else {
851                                         $override_order = "title, $date_sort_field DESC";
852                                 }
853                                 break;
854
855                         case "score":
856                                 if (get_pref($this->link, 'REVERSE_HEADLINES', $owner_uid)) {
857                                         $override_order = "score, $date_sort_field";
858                                 } else {
859                                         $override_order = "score DESC, $date_sort_field DESC";
860                                 }
861                                 break;
862                 }
863
864                 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
865
866                 $ret = $this->format_headlines_list($feed, $method,
867                         $view_mode, $limit, $cat_view, $next_unread_feed, $offset,
868                         $vgroup_last_feed, $override_order, true);
869
870                 //$topmost_article_ids = $ret[0];
871                 $headlines_count = $ret[1];
872                 $returned_feed = $ret[2];
873                 $disable_cache = $ret[3];
874                 $vgroup_last_feed = $ret[4];
875
876                 $reply['headlines']['content'] =& $ret[5]['content'];
877                 $reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
878
879                 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
880
881                 $reply['headlines-info'] = array("count" => (int) $headlines_count,
882                                                 "vgroup_last_feed" => $vgroup_last_feed,
883                                                 "disable_cache" => (bool) $disable_cache);
884
885                 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
886
887                 $reply['runtime-info'] = make_runtime_info($this->link);
888
889                 print json_encode($reply);
890
891         }
892
893         private function generate_dashboard_feed($link) {
894                 $reply = array();
895
896                 $reply['headlines']['id'] = -5;
897                 $reply['headlines']['is_cat'] = false;
898
899                 $reply['headlines']['toolbar'] = '';
900                 $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
901
902                 $reply['headlines']['content'] .= "<p class=\"small\"><span class=\"insensitive\">";
903
904                 $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
905                         WHERE owner_uid = " . $_SESSION['uid']);
906
907                 $last_updated = db_fetch_result($result, 0, "last_updated");
908                 $last_updated = make_local_datetime($link, $last_updated, false);
909
910                 $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
911
912                 $result = db_query($link, "SELECT COUNT(id) AS num_errors
913                         FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
914
915                 $num_errors = db_fetch_result($result, 0, "num_errors");
916
917                 if ($num_errors > 0) {
918                         $reply['headlines']['content'] .= "<br/>";
919                         $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
920                                 __('Some feeds have update errors (click for details)')."</a>";
921                 }
922                 $reply['headlines']['content'] .= "</span></p>";
923
924                 $reply['headlines-info'] = array("count" => 0,
925                         "vgroup_last_feed" => '',
926                         "unread" => 0,
927                         "disable_cache" => true);
928
929                 return $reply;
930         }
931
932         private function generate_error_feed($link, $error) {
933                 $reply = array();
934
935                 $reply['headlines']['id'] = -6;
936                 $reply['headlines']['is_cat'] = false;
937
938                 $reply['headlines']['toolbar'] = '';
939                 $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
940
941                 $reply['headlines-info'] = array("count" => 0,
942                         "vgroup_last_feed" => '',
943                         "unread" => 0,
944                         "disable_cache" => true);
945
946                 return $reply;
947         }
948
949 }
950 ?>