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