feeds.php 67 KB

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