feeds.php 57 KB

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