feeds.php 67 KB

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