feeds.php 56 KB

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