feeds.php 58 KB

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