feeds.php 65 KB

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