feeds.php 71 KB

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