feeds.php 67 KB

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