feeds.php 56 KB

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