feeds.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. <?php
  2. require_once "colors.php";
  3. class Feeds extends Handler_Protected {
  4. private $params;
  5. function csrf_ignore($method) {
  6. $csrf_ignored = array("index", "feedbrowser", "quickaddfeed", "search");
  7. return array_search($method, $csrf_ignored) !== false;
  8. }
  9. private function format_headline_subtoolbar($feed_site_url, $feed_title,
  10. $feed_id, $is_cat, $search,
  11. $error, $feed_last_updated) {
  12. if ($is_cat) $cat_q = "&is_cat=$is_cat";
  13. if ($search) {
  14. $search_q = "&q=$search";
  15. } else {
  16. $search_q = "";
  17. }
  18. $reply = "";
  19. $rss_link = htmlspecialchars(get_self_url_prefix() .
  20. "/public.php?op=rss&id=$feed_id$cat_q$search_q");
  21. $reply .= "<span class='left'>";
  22. $reply .= "<a href=\"#\"
  23. title=\"".__("Show as feed")."\"
  24. onclick=\"App.displayDlg('".__("Show as feed")."','generatedFeed', '$feed_id:$is_cat:$rss_link')\">
  25. <i class='icon-syndicate material-icons'>rss_feed</i></a>";
  26. $reply .= "<span id='feed_title'>";
  27. if ($feed_site_url) {
  28. $last_updated = T_sprintf("Last updated: %s", $feed_last_updated);
  29. $reply .= "<a title=\"$last_updated\" target='_blank' href=\"$feed_site_url\">".
  30. truncate_string(strip_tags($feed_title), 30)."</a>";
  31. } else {
  32. $reply .= strip_tags($feed_title);
  33. }
  34. if ($error)
  35. $reply .= " <i title=\"" . htmlspecialchars($error) . "\" class='material-icons icon-error'>error</i>";
  36. $reply .= "</span></span>";
  37. $reply .= "<span class=\"right\">";
  38. $reply .= "<span id='selected_prompt'></span>";
  39. $reply .= "&nbsp;";
  40. $reply .= "<select dojoType=\"dijit.form.Select\"
  41. onchange=\"Headlines.onActionChanged(this)\">";
  42. $reply .= "<option value=\"0\" disabled='1'>".__('Select...')."</option>";
  43. $reply .= "<option value=\"Headlines.select('all')\">".__('All')."</option>";
  44. $reply .= "<option value=\"Headlines.select('unread')\">".__('Unread')."</option>";
  45. $reply .= "<option value=\"Headlines.select('invert')\">".__('Invert')."</option>";
  46. $reply .= "<option value=\"Headlines.select('none')\">".__('None')."</option>";
  47. $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection toggle:')."</option>";
  48. $reply .= "<option value=\"Headlines.selectionToggleUnread()\">".__('Unread')."</option>
  49. <option value=\"Headlines.selectionToggleMarked()\">".__('Starred')."</option>
  50. <option value=\"Headlines.selectionTogglePublished()\">".__('Published')."</option>";
  51. $reply .= "<option value=\"0\" disabled=\"1\">".__('Selection:')."</option>";
  52. $reply .= "<option value=\"Headlines.catchupSelection()\">".__('Mark as read')."</option>";
  53. $reply .= "<option value=\"Article.selectionSetScore()\">".__('Set score')."</option>";
  54. if ($feed_id == 0 && !$is_cat) {
  55. $reply .= "<option value=\"Headlines.archiveSelection()\">".__('Move back')."</option>";
  56. $reply .= "<option value=\"Headlines.deleteSelection()\">".__('Delete')."</option>";
  57. } else {
  58. $reply .= "<option value=\"Headlines.archiveSelection()\">".__('Archive')."</option>";
  59. }
  60. if (PluginHost::getInstance()->get_plugin("mail")) {
  61. $reply .= "<option value=\"Plugins.Mail.send()\">".__('Forward by email').
  62. "</option>";
  63. }
  64. if (PluginHost::getInstance()->get_plugin("mailto")) {
  65. $reply .= "<option value=\"Plugins.Mailto.send()\">".__('Forward by email').
  66. "</option>";
  67. }
  68. $reply .= "<option value=\"0\" disabled=\"1\">".__('Feed:')."</option>";
  69. //$reply .= "<option value=\"catchupPage()\">".__('Mark as read')."</option>";
  70. $reply .= "<option value=\"App.displayDlg('".__("Show as feed")."','generatedFeed', '$feed_id:$is_cat:$rss_link')\">".
  71. __('Show as feed')."</option>";
  72. $reply .= "</select>";
  73. //$reply .= "</h2";
  74. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HEADLINE_TOOLBAR_BUTTON) as $p) {
  75. $reply .= $p->hook_headline_toolbar_button($feed_id, $is_cat);
  76. }
  77. $reply .= "</span>";
  78. return $reply;
  79. }
  80. private function format_headlines_list($feed, $method, $view_mode, $limit, $cat_view,
  81. $offset, $override_order = false, $include_children = false, $check_first_id = false,
  82. $skip_first_id_check = false, $order_by = false) {
  83. $disable_cache = false;
  84. $reply = array();
  85. $rgba_cache = array();
  86. $topmost_article_ids = array();
  87. if (!$offset) $offset = 0;
  88. if ($method == "undefined") $method = "";
  89. $method_split = explode(":", $method);
  90. if ($method == "ForceUpdate" && $feed > 0 && is_numeric($feed)) {
  91. $sth = $this->pdo->prepare("UPDATE ttrss_feeds
  92. SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
  93. WHERE id = ?");
  94. $sth->execute([$feed]);
  95. }
  96. if ($method_split[0] == "MarkAllReadGR") {
  97. $this->catchup_feed($method_split[1], false);
  98. }
  99. // FIXME: might break tag display?
  100. if (is_numeric($feed) && $feed > 0 && !$cat_view) {
  101. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? LIMIT 1");
  102. $sth->execute([$feed]);
  103. if (!$sth->fetch()) {
  104. $reply['content'] = "<div align='center'>".__('Feed not found.')."</div>";
  105. }
  106. }
  107. @$search = $_REQUEST["query"];
  108. @$search_language = $_REQUEST["search_language"]; // PGSQL only
  109. if ($search) {
  110. $disable_cache = true;
  111. }
  112. if (!$cat_view && is_numeric($feed) && $feed < PLUGIN_FEED_BASE_INDEX && $feed > LABEL_BASE_INDEX) {
  113. $handler = PluginHost::getInstance()->get_feed_handler(
  114. PluginHost::feed_to_pfeed_id($feed));
  115. if ($handler) {
  116. $options = array(
  117. "limit" => $limit,
  118. "view_mode" => $view_mode,
  119. "cat_view" => $cat_view,
  120. "search" => $search,
  121. "override_order" => $override_order,
  122. "offset" => $offset,
  123. "owner_uid" => $_SESSION["uid"],
  124. "filter" => false,
  125. "since_id" => 0,
  126. "include_children" => $include_children,
  127. "order_by" => $order_by);
  128. $qfh_ret = $handler->get_headlines(PluginHost::feed_to_pfeed_id($feed),
  129. $options);
  130. }
  131. } else {
  132. $params = array(
  133. "feed" => $feed,
  134. "limit" => $limit,
  135. "view_mode" => $view_mode,
  136. "cat_view" => $cat_view,
  137. "search" => $search,
  138. "search_language" => $search_language,
  139. "override_order" => $override_order,
  140. "offset" => $offset,
  141. "include_children" => $include_children,
  142. "check_first_id" => $check_first_id,
  143. "skip_first_id_check" => $skip_first_id_check,
  144. "order_by" => $order_by
  145. );
  146. $qfh_ret = $this->queryFeedHeadlines($params);
  147. }
  148. $vfeed_group_enabled = get_pref("VFEED_GROUP_BY_FEED") && $feed != -6;
  149. $result = $qfh_ret[0]; // this could be either a PDO query result or a -1 if first id changed
  150. $feed_title = $qfh_ret[1];
  151. $feed_site_url = $qfh_ret[2];
  152. $last_error = $qfh_ret[3];
  153. $last_updated = strpos($qfh_ret[4], '1970-') === FALSE ?
  154. make_local_datetime($qfh_ret[4], false) : __("Never");
  155. $highlight_words = $qfh_ret[5];
  156. $reply['first_id'] = $qfh_ret[6];
  157. $reply['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=\"insensitive\">";
  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=\"insensitive\" 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=\"insensitive\">";
  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=\"insensitive\" 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 "<div class=\"dlgSec\">".__("Feed or site URL")."</div>";
  478. print "<div class=\"dlgSecCont\">";
  479. print "<div style='float : right'>
  480. <img style='display : none'
  481. id='feed_add_spinner' src='images/indicator_white.gif'></div>";
  482. print "<input style=\"font-size : 16px; width : 20em;\"
  483. placeHolder=\"".__("Feed or site URL")."\"
  484. dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"feed\" id=\"feedDlg_feedUrl\">";
  485. print "<hr/>";
  486. if (get_pref('ENABLE_FEED_CATS')) {
  487. print __('Place in category:') . " ";
  488. print_feed_cat_select("cat", false, 'dojoType="dijit.form.Select"');
  489. }
  490. print "</div>";
  491. print '<div id="feedDlg_feedsContainer" style="display : none">
  492. <div class="dlgSec">' . __('Available feeds') . '</div>
  493. <div class="dlgSecCont">'.
  494. '<select id="feedDlg_feedContainerSelect"
  495. dojoType="dijit.form.Select" size="3">
  496. <script type="dojo/method" event="onChange" args="value">
  497. dijit.byId("feedDlg_feedUrl").attr("value", value);
  498. </script>
  499. </select>'.
  500. '</div></div>';
  501. print "<div id='feedDlg_loginContainer' style='display : none'>
  502. <div class=\"dlgSec\">".__("Authentication")."</div>
  503. <div class=\"dlgSecCont\">".
  504. " <input dojoType=\"dijit.form.TextBox\" name='login'\"
  505. placeHolder=\"".__("Login")."\"
  506. autocomplete=\"new-password\"
  507. style=\"width : 10em;\"> ".
  508. " <input
  509. placeHolder=\"".__("Password")."\"
  510. dojoType=\"dijit.form.TextBox\" type='password'
  511. autocomplete=\"new-password\"
  512. style=\"width : 10em;\" name='pass'\">
  513. </div></div>";
  514. print "<div style=\"clear : both\">
  515. <input type=\"checkbox\" name=\"need_auth\" dojoType=\"dijit.form.CheckBox\" id=\"feedDlg_loginCheck\"
  516. onclick='displayIfChecked(this, \"feedDlg_loginContainer\")'>
  517. <label for=\"feedDlg_loginCheck\">".
  518. __('This feed requires authentication.')."</div>";
  519. print "<div class=\"dlgButtons\">
  520. <button dojoType=\"dijit.form.Button\" class=\"alt-primary\" type=\"submit\" onclick=\"return dijit.byId('feedAddDlg').execute()\">".__('Subscribe')."</button>";
  521. if (!(defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER)) {
  522. print "<button dojoType=\"dijit.form.Button\" onclick=\"return CommonDialogs.feedBrowser()\">".__('More feeds')."</button>";
  523. }
  524. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('feedAddDlg').hide()\">".__('Cancel')."</button>
  525. </div>";
  526. print "</form>";
  527. //return;
  528. }
  529. function feedBrowser() {
  530. if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER) return;
  531. $browser_search = $_REQUEST["search"];
  532. print_hidden("op", "rpc");
  533. print_hidden("method", "updateFeedBrowser");
  534. print "<div dojoType=\"dijit.Toolbar\">
  535. <div style='float : right'>
  536. <img style='display : none'
  537. id='feed_browser_spinner' src='images/indicator_white.gif'>
  538. <input name=\"search\" dojoType=\"dijit.form.TextBox\" size=\"20\" type=\"search\"
  539. onchange=\"dijit.byId('feedBrowserDlg').update()\" value=\"$browser_search\">
  540. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').update()\">".__('Search')."</button>
  541. </div>";
  542. print " <select name=\"mode\" dojoType=\"dijit.form.Select\" onchange=\"dijit.byId('feedBrowserDlg').update()\">
  543. <option value='1'>" . __('Popular feeds') . "</option>
  544. <option value='2'>" . __('Feed archive') . "</option>
  545. </select> ";
  546. print __("limit:");
  547. print " <select dojoType=\"dijit.form.Select\" name=\"limit\" onchange=\"dijit.byId('feedBrowserDlg').update()\">";
  548. foreach (array(25, 50, 100, 200) as $l) {
  549. //$issel = ($l == $limit) ? "selected=\"1\"" : "";
  550. print "<option value=\"$l\">$l</option>";
  551. }
  552. print "</select> ";
  553. print "</div>";
  554. require_once "feedbrowser.php";
  555. print "<ul class='browseFeedList' id='browseFeedList'>";
  556. print make_feed_browser("", 25);
  557. print "</ul>";
  558. print "<div align='center'>
  559. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').execute()\">".__('Subscribe')."</button>
  560. <button dojoType=\"dijit.form.Button\" style='display : none' id='feed_archive_remove' onclick=\"dijit.byId('feedBrowserDlg').removeFromArchive()\">".__('Remove')."</button>
  561. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').hide()\" >".__('Cancel')."</button></div>";
  562. }
  563. function search() {
  564. $this->params = explode(":", $_REQUEST["param"], 2);
  565. $active_feed_id = sprintf("%d", $this->params[0]);
  566. $is_cat = $this->params[1] != "false";
  567. print "<form onsubmit='return false;'>";
  568. print "<div class=\"dlgSec\">".__('Look for')."</div>";
  569. print "<div class=\"dlgSecCont\">";
  570. print "<input dojoType=\"dijit.form.ValidationTextBox\"
  571. style=\"font-size : 16px; width : 20em;\"
  572. required=\"1\" name=\"query\" type=\"search\" value=''>";
  573. print "<hr/><span style='float : right'>".T_sprintf('in %s', $this->getFeedTitle($active_feed_id, $is_cat))."</span>";
  574. if (DB_TYPE == "pgsql") {
  575. print "<hr/>";
  576. print_select("search_language", "", Pref_Feeds::$feed_languages,
  577. "dojoType='dijit.form.Select' title=\"".__('Used for word stemming')."\"");
  578. }
  579. print "</div>";
  580. print "<div class=\"dlgButtons\">";
  581. if (count(PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEARCH)) == 0) {
  582. print "<div style=\"float : left\">
  583. <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/SearchSyntax\">".__("Search syntax")."</a>
  584. </div>";
  585. }
  586. print "<button dojoType=\"dijit.form.Button\" type=\"submit\" class=\"alt-primary\" onclick=\"dijit.byId('searchDlg').execute()\">".__('Search')."</button>
  587. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('searchDlg').hide()\">".__('Cancel')."</button>
  588. </div>";
  589. print "</form>";
  590. }
  591. function update_debugger() {
  592. header("Content-type: text/html");
  593. Debug::set_enabled(true);
  594. Debug::set_loglevel($_REQUEST["xdebug"]);
  595. $feed_id = (int)$_REQUEST["feed_id"];
  596. @$do_update = $_REQUEST["action"] == "do_update";
  597. $csrf_token = $_REQUEST["csrf_token"];
  598. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
  599. $sth->execute([$feed_id, $_SESSION['uid']]);
  600. if (!$sth->fetch()) {
  601. print "Access denied.";
  602. return;
  603. }
  604. $refetch_checked = isset($_REQUEST["force_refetch"]) ? "checked" : "";
  605. $rehash_checked = isset($_REQUEST["force_rehash"]) ? "checked" : "";
  606. ?>
  607. <html>
  608. <head>
  609. <?php echo stylesheet_tag("css/default.css") ?>
  610. <title>Feed Debugger</title>
  611. </head>
  612. <body class="small_margins ttrss_utility claro">
  613. <h1>Feed Debugger: <?php echo "$feed_id: " . $this->getFeedTitle($feed_id) ?></h1>
  614. <form method="GET" action="">
  615. <input type="hidden" name="op" value="feeds">
  616. <input type="hidden" name="method" value="update_debugger">
  617. <input type="hidden" name="xdebug" value="1">
  618. <input type="hidden" name="csrf_token" value="<?php echo $csrf_token ?>">
  619. <input type="hidden" name="action" value="do_update">
  620. <input type="hidden" name="feed_id" value="<?php echo $feed_id ?>">
  621. <input type="checkbox" name="force_refetch" value="1" <?php echo $refetch_checked ?>> Force refetch<br/>
  622. <input type="checkbox" name="force_rehash" value="1" <?php echo $rehash_checked ?>> Force rehash<br/>
  623. <p/><button type="submit">Continue</button>
  624. </form>
  625. <hr>
  626. <pre><?php
  627. if ($do_update) {
  628. RSSUtils::update_rss_feed($feed_id, true);
  629. }
  630. ?></pre>
  631. </body>
  632. </html>
  633. <?php
  634. }
  635. static function catchup_feed($feed, $cat_view, $owner_uid = false, $mode = 'all', $search = false) {
  636. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  637. $pdo = Db::pdo();
  638. // Todo: all this interval stuff needs some generic generator function
  639. $search_qpart = is_array($search) && $search[0] ? search_to_sql($search[0], $search[1])[0] : 'true';
  640. switch ($mode) {
  641. case "1day":
  642. if (DB_TYPE == "pgsql") {
  643. $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
  644. } else {
  645. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
  646. }
  647. break;
  648. case "1week":
  649. if (DB_TYPE == "pgsql") {
  650. $date_qpart = "date_entered < NOW() - INTERVAL '1 week' ";
  651. } else {
  652. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) ";
  653. }
  654. break;
  655. case "2week":
  656. if (DB_TYPE == "pgsql") {
  657. $date_qpart = "date_entered < NOW() - INTERVAL '2 week' ";
  658. } else {
  659. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) ";
  660. }
  661. break;
  662. default:
  663. $date_qpart = "true";
  664. }
  665. if (is_numeric($feed)) {
  666. if ($cat_view) {
  667. if ($feed >= 0) {
  668. if ($feed > 0) {
  669. $children = Feeds::getChildCategories($feed, $owner_uid);
  670. array_push($children, $feed);
  671. $children = array_map("intval", $children);
  672. $children = join(",", $children);
  673. $cat_qpart = "cat_id IN ($children)";
  674. } else {
  675. $cat_qpart = "cat_id IS NULL";
  676. }
  677. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  678. SET unread = false, last_read = NOW() WHERE ref_id IN
  679. (SELECT id FROM
  680. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  681. AND owner_uid = ? AND unread = true AND feed_id IN
  682. (SELECT id FROM ttrss_feeds WHERE $cat_qpart) AND $date_qpart AND $search_qpart) as tmp)");
  683. $sth->execute([$owner_uid]);
  684. } else if ($feed == -2) {
  685. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  686. SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
  687. FROM ttrss_user_labels2, ttrss_entries WHERE article_id = ref_id AND id = ref_id AND $date_qpart AND $search_qpart) > 0
  688. AND unread = true AND owner_uid = ?");
  689. $sth->execute([$owner_uid]);
  690. }
  691. } else if ($feed > 0) {
  692. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  693. SET unread = false, last_read = NOW() WHERE ref_id IN
  694. (SELECT id FROM
  695. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  696. AND owner_uid = ? AND unread = true AND feed_id = ? AND $date_qpart AND $search_qpart) as tmp)");
  697. $sth->execute([$owner_uid, $feed]);
  698. } else if ($feed < 0 && $feed > LABEL_BASE_INDEX) { // special, like starred
  699. if ($feed == -1) {
  700. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  701. SET unread = false, last_read = NOW() WHERE ref_id IN
  702. (SELECT id FROM
  703. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  704. AND owner_uid = ? AND unread = true AND marked = true AND $date_qpart AND $search_qpart) as tmp)");
  705. $sth->execute([$owner_uid]);
  706. }
  707. if ($feed == -2) {
  708. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  709. SET unread = false, last_read = NOW() WHERE ref_id IN
  710. (SELECT id FROM
  711. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  712. AND owner_uid = ? AND unread = true AND published = true AND $date_qpart AND $search_qpart) as tmp)");
  713. $sth->execute([$owner_uid]);
  714. }
  715. if ($feed == -3) {
  716. $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE");
  717. if (DB_TYPE == "pgsql") {
  718. $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
  719. } else {
  720. $match_part = "date_entered > DATE_SUB(NOW(),
  721. INTERVAL $intl HOUR) ";
  722. }
  723. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  724. SET unread = false, last_read = NOW() WHERE ref_id IN
  725. (SELECT id FROM
  726. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  727. AND owner_uid = ? AND score >= 0 AND unread = true AND $date_qpart AND $match_part AND $search_qpart) as tmp)");
  728. $sth->execute([$owner_uid]);
  729. }
  730. if ($feed == -4) {
  731. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  732. SET unread = false, last_read = NOW() WHERE ref_id IN
  733. (SELECT id FROM
  734. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  735. AND owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
  736. $sth->execute([$owner_uid]);
  737. }
  738. } else if ($feed < LABEL_BASE_INDEX) { // label
  739. $label_id = Labels::feed_to_label_id($feed);
  740. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  741. SET unread = false, last_read = NOW() WHERE ref_id IN
  742. (SELECT id FROM
  743. (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_user_labels2 WHERE ref_id = id
  744. AND label_id = ? AND ref_id = article_id
  745. AND owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
  746. $sth->execute([$label_id, $owner_uid]);
  747. }
  748. CCache::update($feed, $owner_uid, $cat_view);
  749. } else { // tag
  750. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  751. SET unread = false, last_read = NOW() WHERE ref_id IN
  752. (SELECT id FROM
  753. (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_tags WHERE ref_id = ttrss_entries.id
  754. AND post_int_id = int_id AND tag_name = ?
  755. AND ttrss_user_entries.owner_uid = ? AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
  756. $sth->execute([$feed, $owner_uid]);
  757. }
  758. }
  759. static function getFeedArticles($feed, $is_cat = false, $unread_only = false,
  760. $owner_uid = false) {
  761. $n_feed = (int) $feed;
  762. $need_entries = false;
  763. $pdo = Db::pdo();
  764. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  765. if ($unread_only) {
  766. $unread_qpart = "unread = true";
  767. } else {
  768. $unread_qpart = "true";
  769. }
  770. $match_part = "";
  771. if ($is_cat) {
  772. return Feeds::getCategoryUnread($n_feed, $owner_uid);
  773. } else if ($n_feed == -6) {
  774. return 0;
  775. } else if ($feed != "0" && $n_feed == 0) {
  776. $sth = $pdo->prepare("SELECT SUM((SELECT COUNT(int_id)
  777. FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
  778. AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags
  779. WHERE owner_uid = ? AND tag_name = ?");
  780. $sth->execute([$owner_uid, $feed]);
  781. $row = $sth->fetch();
  782. return $row["count"];
  783. } else if ($n_feed == -1) {
  784. $match_part = "marked = true";
  785. } else if ($n_feed == -2) {
  786. $match_part = "published = true";
  787. } else if ($n_feed == -3) {
  788. $match_part = "unread = true AND score >= 0";
  789. $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
  790. if (DB_TYPE == "pgsql") {
  791. $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
  792. } else {
  793. $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
  794. }
  795. $need_entries = true;
  796. } else if ($n_feed == -4) {
  797. $match_part = "true";
  798. } else if ($n_feed >= 0) {
  799. if ($n_feed != 0) {
  800. $match_part = "feed_id = " . (int)$n_feed;
  801. } else {
  802. $match_part = "feed_id IS NULL";
  803. }
  804. } else if ($feed < LABEL_BASE_INDEX) {
  805. $label_id = Labels::feed_to_label_id($feed);
  806. return Feeds::getLabelUnread($label_id, $owner_uid);
  807. }
  808. if ($match_part) {
  809. if ($need_entries) {
  810. $from_qpart = "ttrss_user_entries,ttrss_entries";
  811. $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND";
  812. } else {
  813. $from_qpart = "ttrss_user_entries";
  814. $from_where = "";
  815. }
  816. $sth = $pdo->prepare("SELECT count(int_id) AS unread
  817. FROM $from_qpart WHERE
  818. $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = ?");
  819. $sth->execute([$owner_uid]);
  820. $row = $sth->fetch();
  821. return $row["unread"];
  822. } else {
  823. $sth = $pdo->prepare("SELECT COUNT(post_int_id) AS unread
  824. FROM ttrss_tags,ttrss_user_entries,ttrss_entries
  825. WHERE tag_name = ? AND post_int_id = int_id AND ref_id = ttrss_entries.id
  826. AND $unread_qpart AND ttrss_tags.owner_uid = ,");
  827. $sth->execute([$feed, $owner_uid]);
  828. $row = $sth->fetch();
  829. return $row["unread"];
  830. }
  831. }
  832. /**
  833. * @return array (code => Status code, message => error message if available)
  834. *
  835. * 0 - OK, Feed already exists
  836. * 1 - OK, Feed added
  837. * 2 - Invalid URL
  838. * 3 - URL content is HTML, no feeds available
  839. * 4 - URL content is HTML which contains multiple feeds.
  840. * Here you should call extractfeedurls in rpc-backend
  841. * to get all possible feeds.
  842. * 5 - Couldn't download the URL content.
  843. * 6 - Content is an invalid XML.
  844. */
  845. static function subscribe_to_feed($url, $cat_id = 0,
  846. $auth_login = '', $auth_pass = '') {
  847. global $fetch_last_error;
  848. global $fetch_last_error_content;
  849. $pdo = Db::pdo();
  850. $url = fix_url($url);
  851. if (!$url || !validate_feed_url($url)) return array("code" => 2);
  852. $contents = @fetch_file_contents($url, false, $auth_login, $auth_pass);
  853. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SUBSCRIBE_FEED) as $plugin) {
  854. $contents = $plugin->hook_subscribe_feed($contents, $url, $auth_login, $auth_pass);
  855. }
  856. if (!$contents) {
  857. if (preg_match("/cloudflare\.com/", $fetch_last_error_content)) {
  858. $fetch_last_error .= " (feed behind Cloudflare)";
  859. }
  860. return array("code" => 5, "message" => $fetch_last_error);
  861. }
  862. if (is_html($contents)) {
  863. $feedUrls = get_feeds_from_html($url, $contents);
  864. if (count($feedUrls) == 0) {
  865. return array("code" => 3);
  866. } else if (count($feedUrls) > 1) {
  867. return array("code" => 4, "feeds" => $feedUrls);
  868. }
  869. //use feed url as new URL
  870. $url = key($feedUrls);
  871. }
  872. if (!$cat_id) $cat_id = null;
  873. $sth = $pdo->prepare("SELECT id FROM ttrss_feeds
  874. WHERE feed_url = ? AND owner_uid = ?");
  875. $sth->execute([$url, $_SESSION['uid']]);
  876. if ($row = $sth->fetch()) {
  877. return array("code" => 0, "feed_id" => (int) $row["id"]);
  878. } else {
  879. $sth = $pdo->prepare(
  880. "INSERT INTO ttrss_feeds
  881. (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted)
  882. VALUES (?, ?, ?, ?, ?, ?, 0, false)");
  883. $sth->execute([$_SESSION['uid'], $url, "[Unknown]", $cat_id, (string)$auth_login, (string)$auth_pass]);
  884. $sth = $pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ?
  885. AND owner_uid = ?");
  886. $sth->execute([$url, $_SESSION['uid']]);
  887. $row = $sth->fetch();
  888. $feed_id = $row["id"];
  889. if ($feed_id) {
  890. RSSUtils::set_basic_feed_info($feed_id);
  891. }
  892. return array("code" => 1, "feed_id" => (int) $feed_id);
  893. }
  894. }
  895. static function getIconFile($feed_id) {
  896. return ICONS_DIR . "/$feed_id.ico";
  897. }
  898. static function feedHasIcon($id) {
  899. return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
  900. }
  901. static function getFeedIcon($id) {
  902. switch ($id) {
  903. case 0:
  904. return "archive";
  905. break;
  906. case -1:
  907. return "star";
  908. break;
  909. case -2:
  910. return "rss_feed";
  911. break;
  912. case -3:
  913. return "whatshot";
  914. break;
  915. case -4:
  916. return "inbox";
  917. break;
  918. case -6:
  919. return "restore";
  920. break;
  921. default:
  922. if ($id < LABEL_BASE_INDEX) {
  923. return "label";
  924. } else {
  925. $icon = self::getIconFile($id);
  926. if ($icon && file_exists($icon)) {
  927. return ICONS_URL . "/" . basename($icon) . "?" . filemtime($icon);
  928. }
  929. }
  930. break;
  931. }
  932. return false;
  933. }
  934. static function getFeedTitle($id, $cat = false) {
  935. $pdo = Db::pdo();
  936. if ($cat) {
  937. return Feeds::getCategoryTitle($id);
  938. } else if ($id == -1) {
  939. return __("Starred articles");
  940. } else if ($id == -2) {
  941. return __("Published articles");
  942. } else if ($id == -3) {
  943. return __("Fresh articles");
  944. } else if ($id == -4) {
  945. return __("All articles");
  946. } else if ($id === 0 || $id === "0") {
  947. return __("Archived articles");
  948. } else if ($id == -6) {
  949. return __("Recently read");
  950. } else if ($id < LABEL_BASE_INDEX) {
  951. $label_id = Labels::feed_to_label_id($id);
  952. $sth = $pdo->prepare("SELECT caption FROM ttrss_labels2 WHERE id = ?");
  953. $sth->execute([$label_id]);
  954. if ($row = $sth->fetch()) {
  955. return $row["caption"];
  956. } else {
  957. return "Unknown label ($label_id)";
  958. }
  959. } else if (is_numeric($id) && $id > 0) {
  960. $sth = $pdo->prepare("SELECT title FROM ttrss_feeds WHERE id = ?");
  961. $sth->execute([$id]);
  962. if ($row = $sth->fetch()) {
  963. return $row["title"];
  964. } else {
  965. return "Unknown feed ($id)";
  966. }
  967. } else {
  968. return $id;
  969. }
  970. }
  971. static function getCategoryUnread($cat, $owner_uid = false) {
  972. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  973. $pdo = Db::pdo();
  974. if ($cat >= 0) {
  975. if (!$cat) $cat = null;
  976. $sth = $pdo->prepare("SELECT id FROM ttrss_feeds
  977. WHERE (cat_id = :cat OR (:cat IS NULL AND cat_id IS NULL))
  978. AND owner_uid = :uid");
  979. $sth->execute([":cat" => $cat, ":uid" => $owner_uid]);
  980. $cat_feeds = array();
  981. while ($line = $sth->fetch()) {
  982. array_push($cat_feeds, "feed_id = " . (int)$line["id"]);
  983. }
  984. if (count($cat_feeds) == 0) return 0;
  985. $match_part = implode(" OR ", $cat_feeds);
  986. $sth = $pdo->prepare("SELECT COUNT(int_id) AS unread
  987. FROM ttrss_user_entries
  988. WHERE unread = true AND ($match_part)
  989. AND owner_uid = ?");
  990. $sth->execute([$owner_uid]);
  991. $unread = 0;
  992. # this needs to be rewritten
  993. while ($line = $sth->fetch()) {
  994. $unread += $line["unread"];
  995. }
  996. return $unread;
  997. } else if ($cat == -1) {
  998. return getFeedUnread(-1) + getFeedUnread(-2) + getFeedUnread(-3) + getFeedUnread(0);
  999. } else if ($cat == -2) {
  1000. $sth = $pdo->prepare("SELECT COUNT(unread) AS unread FROM
  1001. ttrss_user_entries, ttrss_user_labels2
  1002. WHERE article_id = ref_id AND unread = true
  1003. AND ttrss_user_entries.owner_uid = ?");
  1004. $sth->execute([$owner_uid]);
  1005. $row = $sth->fetch();
  1006. return $row["unread"];
  1007. }
  1008. }
  1009. // only accepts real cats (>= 0)
  1010. static function getCategoryChildrenUnread($cat, $owner_uid = false) {
  1011. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1012. $pdo = Db::pdo();
  1013. $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories WHERE parent_cat = ?
  1014. AND owner_uid = ?");
  1015. $sth->execute([$cat, $owner_uid]);
  1016. $unread = 0;
  1017. while ($line = $sth->fetch()) {
  1018. $unread += Feeds::getCategoryUnread($line["id"], $owner_uid);
  1019. $unread += Feeds::getCategoryChildrenUnread($line["id"], $owner_uid);
  1020. }
  1021. return $unread;
  1022. }
  1023. static function getGlobalUnread($user_id = false) {
  1024. if (!$user_id) $user_id = $_SESSION["uid"];
  1025. $pdo = Db::pdo();
  1026. $sth = $pdo->prepare("SELECT SUM(value) AS c_id FROM ttrss_counters_cache
  1027. WHERE owner_uid = ? AND feed_id > 0");
  1028. $sth->execute([$user_id]);
  1029. $row = $sth->fetch();
  1030. return $row["c_id"];
  1031. }
  1032. static function getCategoryTitle($cat_id) {
  1033. if ($cat_id == -1) {
  1034. return __("Special");
  1035. } else if ($cat_id == -2) {
  1036. return __("Labels");
  1037. } else {
  1038. $pdo = Db::pdo();
  1039. $sth = $pdo->prepare("SELECT title FROM ttrss_feed_categories WHERE
  1040. id = ?");
  1041. $sth->execute([$cat_id]);
  1042. if ($row = $sth->fetch()) {
  1043. return $row["title"];
  1044. } else {
  1045. return __("Uncategorized");
  1046. }
  1047. }
  1048. }
  1049. static function getLabelUnread($label_id, $owner_uid = false) {
  1050. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1051. $pdo = Db::pdo();
  1052. $sth = $pdo->prepare("SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2
  1053. WHERE owner_uid = ? AND unread = true AND label_id = ? AND article_id = ref_id");
  1054. $sth->execute([$owner_uid, $label_id]);
  1055. if ($row = $sth->fetch()) {
  1056. return $row["unread"];
  1057. } else {
  1058. return 0;
  1059. }
  1060. }
  1061. static function queryFeedHeadlines($params) {
  1062. $pdo = Db::pdo();
  1063. // WARNING: due to highly dynamic nature of this query its going to quote parameters
  1064. // right before adding them to SQL part
  1065. $feed = $params["feed"];
  1066. $limit = isset($params["limit"]) ? $params["limit"] : 30;
  1067. $view_mode = $params["view_mode"];
  1068. $cat_view = isset($params["cat_view"]) ? $params["cat_view"] : false;
  1069. $search = isset($params["search"]) ? $params["search"] : false;
  1070. $search_language = isset($params["search_language"]) ? $params["search_language"] : "";
  1071. $override_order = isset($params["override_order"]) ? $params["override_order"] : false;
  1072. $offset = isset($params["offset"]) ? $params["offset"] : 0;
  1073. $owner_uid = isset($params["owner_uid"]) ? $params["owner_uid"] : $_SESSION["uid"];
  1074. $since_id = isset($params["since_id"]) ? $params["since_id"] : 0;
  1075. $include_children = isset($params["include_children"]) ? $params["include_children"] : false;
  1076. $ignore_vfeed_group = isset($params["ignore_vfeed_group"]) ? $params["ignore_vfeed_group"] : false;
  1077. $override_strategy = isset($params["override_strategy"]) ? $params["override_strategy"] : false;
  1078. $override_vfeed = isset($params["override_vfeed"]) ? $params["override_vfeed"] : false;
  1079. $start_ts = isset($params["start_ts"]) ? $params["start_ts"] : false;
  1080. $check_first_id = isset($params["check_first_id"]) ? $params["check_first_id"] : false;
  1081. $skip_first_id_check = isset($params["skip_first_id_check"]) ? $params["skip_first_id_check"] : false;
  1082. $order_by = isset($params["order_by"]) ? $params["order_by"] : false;
  1083. $ext_tables_part = "";
  1084. $limit_query_part = "";
  1085. $search_words = array();
  1086. if ($search) {
  1087. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEARCH) as $plugin) {
  1088. list($search_query_part, $search_words) = $plugin->hook_search($search);
  1089. break;
  1090. }
  1091. // fall back in case of no plugins
  1092. if (!$search_query_part) {
  1093. list($search_query_part, $search_words) = search_to_sql($search, $search_language);
  1094. }
  1095. $search_query_part .= " AND ";
  1096. } else {
  1097. $search_query_part = "";
  1098. }
  1099. if ($since_id) {
  1100. $since_id_part = "ttrss_entries.id > ".$pdo->quote($since_id)." AND ";
  1101. } else {
  1102. $since_id_part = "";
  1103. }
  1104. $view_query_part = "";
  1105. if ($view_mode == "adaptive") {
  1106. if ($search) {
  1107. $view_query_part = " ";
  1108. } else if ($feed != -1) {
  1109. $unread = getFeedUnread($feed, $cat_view);
  1110. if ($cat_view && $feed > 0 && $include_children)
  1111. $unread += Feeds::getCategoryChildrenUnread($feed);
  1112. if ($unread > 0) {
  1113. $view_query_part = " unread = true AND ";
  1114. }
  1115. }
  1116. }
  1117. if ($view_mode == "marked") {
  1118. $view_query_part = " marked = true AND ";
  1119. }
  1120. if ($view_mode == "has_note") {
  1121. $view_query_part = " (note IS NOT NULL AND note != '') AND ";
  1122. }
  1123. if ($view_mode == "published") {
  1124. $view_query_part = " published = true AND ";
  1125. }
  1126. if ($view_mode == "unread" && $feed != -6) {
  1127. $view_query_part = " unread = true AND ";
  1128. }
  1129. if ($limit > 0) {
  1130. $limit_query_part = "LIMIT " . (int)$limit;
  1131. }
  1132. $allow_archived = false;
  1133. $vfeed_query_part = "";
  1134. /* tags */
  1135. if (!is_numeric($feed)) {
  1136. $query_strategy_part = "true";
  1137. $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
  1138. id = feed_id) as feed_title,";
  1139. } else if ($feed > 0) {
  1140. if ($cat_view) {
  1141. if ($feed > 0) {
  1142. if ($include_children) {
  1143. # sub-cats
  1144. $subcats = Feeds::getChildCategories($feed, $owner_uid);
  1145. array_push($subcats, $feed);
  1146. $subcats = array_map("intval", $subcats);
  1147. $query_strategy_part = "cat_id IN (".
  1148. implode(",", $subcats).")";
  1149. } else {
  1150. $query_strategy_part = "cat_id = " . $pdo->quote($feed);
  1151. }
  1152. } else {
  1153. $query_strategy_part = "cat_id IS NULL";
  1154. }
  1155. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1156. } else {
  1157. $query_strategy_part = "feed_id = " . $pdo->quote($feed);
  1158. }
  1159. } else if ($feed == 0 && !$cat_view) { // archive virtual feed
  1160. $query_strategy_part = "feed_id IS NULL";
  1161. $allow_archived = true;
  1162. } else if ($feed == 0 && $cat_view) { // uncategorized
  1163. $query_strategy_part = "cat_id IS NULL AND feed_id IS NOT NULL";
  1164. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1165. } else if ($feed == -1) { // starred virtual feed
  1166. $query_strategy_part = "marked = true";
  1167. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1168. $allow_archived = true;
  1169. if (!$override_order) {
  1170. $override_order = "last_marked DESC, date_entered DESC, updated DESC";
  1171. }
  1172. } else if ($feed == -2) { // published virtual feed OR labels category
  1173. if (!$cat_view) {
  1174. $query_strategy_part = "published = true";
  1175. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1176. $allow_archived = true;
  1177. if (!$override_order) {
  1178. $override_order = "last_published DESC, date_entered DESC, updated DESC";
  1179. }
  1180. } else {
  1181. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1182. $ext_tables_part = "ttrss_labels2,ttrss_user_labels2,";
  1183. $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
  1184. ttrss_user_labels2.article_id = ref_id";
  1185. }
  1186. } else if ($feed == -6) { // recently read
  1187. $query_strategy_part = "unread = false AND last_read IS NOT NULL";
  1188. if (DB_TYPE == "pgsql") {
  1189. $query_strategy_part .= " AND last_read > NOW() - INTERVAL '1 DAY' ";
  1190. } else {
  1191. $query_strategy_part .= " AND last_read > DATE_SUB(NOW(), INTERVAL 1 DAY) ";
  1192. }
  1193. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1194. $allow_archived = true;
  1195. $ignore_vfeed_group = true;
  1196. if (!$override_order) $override_order = "last_read DESC";
  1197. } else if ($feed == -3) { // fresh virtual feed
  1198. $query_strategy_part = "unread = true AND score >= 0";
  1199. $intl = (int) get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
  1200. if (DB_TYPE == "pgsql") {
  1201. $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
  1202. } else {
  1203. $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
  1204. }
  1205. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1206. } else if ($feed == -4) { // all articles virtual feed
  1207. $allow_archived = true;
  1208. $query_strategy_part = "true";
  1209. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1210. } else if ($feed <= LABEL_BASE_INDEX) { // labels
  1211. $label_id = Labels::feed_to_label_id($feed);
  1212. $query_strategy_part = "label_id = ".$pdo->quote($label_id)." AND
  1213. ttrss_labels2.id = ttrss_user_labels2.label_id AND
  1214. ttrss_user_labels2.article_id = ref_id";
  1215. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1216. $ext_tables_part = "ttrss_labels2,ttrss_user_labels2,";
  1217. $allow_archived = true;
  1218. } else {
  1219. $query_strategy_part = "true";
  1220. }
  1221. $order_by = "score DESC, date_entered DESC, updated DESC";
  1222. if ($override_order) {
  1223. $order_by = $override_order;
  1224. }
  1225. if ($override_strategy) {
  1226. $query_strategy_part = $override_strategy;
  1227. }
  1228. if ($override_vfeed) {
  1229. $vfeed_query_part = $override_vfeed;
  1230. }
  1231. if ($search) {
  1232. $feed_title = T_sprintf("Search results: %s", $search);
  1233. } else {
  1234. if ($cat_view) {
  1235. $feed_title = Feeds::getCategoryTitle($feed);
  1236. } else {
  1237. if (is_numeric($feed) && $feed > 0) {
  1238. $ssth = $pdo->prepare("SELECT title,site_url,last_error,last_updated
  1239. FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
  1240. $ssth->execute([$feed, $owner_uid]);
  1241. $row = $ssth->fetch();
  1242. $feed_title = $row["title"];
  1243. $feed_site_url = $row["site_url"];
  1244. $last_error = $row["last_error"];
  1245. $last_updated = $row["last_updated"];
  1246. } else {
  1247. $feed_title = Feeds::getFeedTitle($feed);
  1248. }
  1249. }
  1250. }
  1251. $content_query_part = "content, ";
  1252. if ($limit_query_part) {
  1253. $offset_query_part = "OFFSET " . (int)$offset;
  1254. } else {
  1255. $offset_query_part = "";
  1256. }
  1257. if (is_numeric($feed)) {
  1258. // proper override_order applied above
  1259. if ($vfeed_query_part && !$ignore_vfeed_group && get_pref('VFEED_GROUP_BY_FEED', $owner_uid)) {
  1260. $yyiw_desc = $order_by == "date_reverse" ? "" : "desc";
  1261. if (!$override_order) {
  1262. $order_by = "yyiw $yyiw_desc, ttrss_feeds.title, ".$order_by;
  1263. } else {
  1264. $order_by = "yyiw $yyiw_desc, ttrss_feeds.title, ".$override_order;
  1265. }
  1266. }
  1267. if (!$allow_archived) {
  1268. $from_qpart = "${ext_tables_part}ttrss_entries LEFT JOIN ttrss_user_entries ON (ref_id = ttrss_entries.id),ttrss_feeds";
  1269. $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
  1270. } else {
  1271. $from_qpart = "${ext_tables_part}ttrss_entries LEFT JOIN ttrss_user_entries ON (ref_id = ttrss_entries.id)
  1272. LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
  1273. }
  1274. if ($vfeed_query_part) $vfeed_query_part .= "favicon_avg_color,";
  1275. if ($start_ts) {
  1276. $start_ts_formatted = date("Y/m/d H:i:s", strtotime($start_ts));
  1277. $start_ts_query_part = "date_entered >= '$start_ts_formatted' AND";
  1278. } else {
  1279. $start_ts_query_part = "";
  1280. }
  1281. $first_id = 0;
  1282. $first_id_query_strategy_part = $query_strategy_part;
  1283. if ($feed == -3)
  1284. $first_id_query_strategy_part = "true";
  1285. if (DB_TYPE == "pgsql") {
  1286. $sanity_interval_qpart = "date_entered >= NOW() - INTERVAL '1 hour' AND";
  1287. $yyiw_qpart = "to_char(date_entered, 'IYYY-IW') AS yyiw";
  1288. } else {
  1289. $sanity_interval_qpart = "date_entered >= DATE_SUB(NOW(), INTERVAL 1 hour) AND";
  1290. $yyiw_qpart = "date_format(date_entered, '%Y-%u') AS yyiw";
  1291. }
  1292. if (!$search && !$skip_first_id_check) {
  1293. // if previous topmost article id changed that means our current pagination is no longer valid
  1294. $query = "SELECT DISTINCT
  1295. ttrss_feeds.title,
  1296. date_entered,
  1297. $yyiw_qpart,
  1298. guid,
  1299. ttrss_entries.id,
  1300. ttrss_entries.title,
  1301. updated,
  1302. score,
  1303. marked,
  1304. published,
  1305. last_marked,
  1306. last_published,
  1307. last_read
  1308. FROM
  1309. $from_qpart
  1310. WHERE
  1311. $feed_check_qpart
  1312. ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
  1313. $search_query_part
  1314. $start_ts_query_part
  1315. $since_id_part
  1316. $sanity_interval_qpart
  1317. $first_id_query_strategy_part ORDER BY $order_by LIMIT 1";
  1318. /*if ($_REQUEST["debug"]) {
  1319. print $query;
  1320. }*/
  1321. $res = $pdo->query($query);
  1322. if ($row = $res->fetch()) {
  1323. $first_id = (int)$row["id"];
  1324. if ($offset > 0 && $first_id && $check_first_id && $first_id != $check_first_id) {
  1325. return array(-1, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id, $vfeed_query_part != "");
  1326. }
  1327. }
  1328. }
  1329. $query = "SELECT DISTINCT
  1330. date_entered,
  1331. $yyiw_qpart,
  1332. guid,
  1333. ttrss_entries.id,ttrss_entries.title,
  1334. updated,
  1335. label_cache,
  1336. tag_cache,
  1337. always_display_enclosures,
  1338. site_url,
  1339. note,
  1340. num_comments,
  1341. comments,
  1342. int_id,
  1343. uuid,
  1344. lang,
  1345. hide_images,
  1346. unread,feed_id,marked,published,link,last_read,orig_feed_id,
  1347. last_marked, last_published,
  1348. $vfeed_query_part
  1349. $content_query_part
  1350. author,score
  1351. FROM
  1352. $from_qpart
  1353. WHERE
  1354. $feed_check_qpart
  1355. ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
  1356. $search_query_part
  1357. $start_ts_query_part
  1358. $view_query_part
  1359. $since_id_part
  1360. $query_strategy_part ORDER BY $order_by
  1361. $limit_query_part $offset_query_part";
  1362. //if ($_REQUEST["debug"]) print $query;
  1363. $res = $pdo->query($query);
  1364. } else {
  1365. // browsing by tag
  1366. $query = "SELECT DISTINCT
  1367. date_entered,
  1368. guid,
  1369. note,
  1370. ttrss_entries.id as id,
  1371. title,
  1372. updated,
  1373. unread,
  1374. feed_id,
  1375. orig_feed_id,
  1376. marked,
  1377. published,
  1378. num_comments,
  1379. comments,
  1380. int_id,
  1381. tag_cache,
  1382. label_cache,
  1383. link,
  1384. lang,
  1385. uuid,
  1386. last_read,
  1387. (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images,
  1388. last_marked, last_published,
  1389. $since_id_part
  1390. $vfeed_query_part
  1391. $content_query_part
  1392. author, score
  1393. FROM ttrss_entries, ttrss_user_entries, ttrss_tags
  1394. WHERE
  1395. ref_id = ttrss_entries.id AND
  1396. ttrss_user_entries.owner_uid = ".$pdo->quote($owner_uid)." AND
  1397. post_int_id = int_id AND
  1398. tag_name = ".$pdo->quote($feed)." AND
  1399. $view_query_part
  1400. $search_query_part
  1401. $query_strategy_part ORDER BY $order_by
  1402. $limit_query_part $offset_query_part";
  1403. if ($_REQUEST["debug"]) print $query;
  1404. $res = $pdo->query($query);
  1405. }
  1406. return array($res, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id, $vfeed_query_part != "");
  1407. }
  1408. static function getParentCategories($cat, $owner_uid) {
  1409. $rv = array();
  1410. $pdo = Db::pdo();
  1411. $sth = $pdo->prepare("SELECT parent_cat FROM ttrss_feed_categories
  1412. WHERE id = ? AND parent_cat IS NOT NULL AND owner_uid = ?");
  1413. $sth->execute([$cat, $owner_uid]);
  1414. while ($line = $sth->fetch()) {
  1415. array_push($rv, $line["parent_cat"]);
  1416. $rv = array_merge($rv, Feeds::getParentCategories($line["parent_cat"], $owner_uid));
  1417. }
  1418. return $rv;
  1419. }
  1420. static function getChildCategories($cat, $owner_uid) {
  1421. $rv = array();
  1422. $pdo = Db::pdo();
  1423. $sth = $pdo->prepare("SELECT id FROM ttrss_feed_categories
  1424. WHERE parent_cat = ? AND owner_uid = ?");
  1425. $sth->execute([$cat, $owner_uid]);
  1426. while ($line = $sth->fetch()) {
  1427. array_push($rv, $line["id"]);
  1428. $rv = array_merge($rv, Feeds::getChildCategories($line["id"], $owner_uid));
  1429. }
  1430. return $rv;
  1431. }
  1432. static function getFeedCategory($feed) {
  1433. $pdo = Db::pdo();
  1434. $sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds
  1435. WHERE id = ?");
  1436. $sth->execute([$feed]);
  1437. if ($row = $sth->fetch()) {
  1438. return $row["cat_id"];
  1439. } else {
  1440. return false;
  1441. }
  1442. }
  1443. function color_of($name) {
  1444. $colormap = [ "#1cd7d7","#d91111","#1212d7","#8e16e5","#7b7b7b",
  1445. "#39f110","#0bbea6","#ec0e0e","#1534f2","#b9e416",
  1446. "#479af2","#f36b14","#10c7e9","#1e8fe7","#e22727" ];
  1447. $sum = 0;
  1448. for ($i = 0; $i < strlen($name); $i++) {
  1449. $sum += ord($name{$i});
  1450. }
  1451. $sum %= count($colormap);
  1452. return $colormap[$sum];
  1453. }
  1454. }