feeds.php 57 KB

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