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. $tmp_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. $tmp_content .= "<div class=\"cdmHeader\">";
  419. $tmp_content .= "<div style=\"vertical-align : middle\">";
  420. $tmp_content .= "<input dojoType=\"dijit.form.CheckBox\"
  421. type=\"checkbox\" onclick=\"toggleSelectRow2(this, false, true)\"
  422. class='rchk'>";
  423. $tmp_content .= "$marked_pic";
  424. $tmp_content .= "$published_pic";
  425. $tmp_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. $tmp_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. $tmp_content .= $labels_str;
  444. $tmp_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. $tmp_content .= "<span $excerpt_hidden id=\"CEXC-$id\" class=\"cdmExcerpt\">" . $content_preview . "</span>";
  452. $tmp_content .= "</span>";
  453. if (!$vfeed_group_enabled) {
  454. if (@$line["feed_title"]) {
  455. $rgba = @$rgba_cache[$feed_id];
  456. $tmp_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. $tmp_content .= "<span class='updated' title='$date_entered_fmt'>$updated_fmt</span>";
  464. $tmp_content .= "<div class='scoreWrap' style=\"vertical-align : middle\">";
  465. $tmp_content .= "$score_pic";
  466. if (!get_pref("VFEED_GROUP_BY_FEED") && $line["feed_title"]) {
  467. $tmp_content .= "<span style=\"cursor : pointer\"
  468. title=\"".htmlspecialchars($line["feed_title"])."\"
  469. onclick=\"viewfeed({feed:$feed_id})\">$feed_icon_img</span>";
  470. }
  471. $tmp_content .= "</div>"; //scoreWrap
  472. $tmp_content .= "</div>"; //cdmHeader
  473. $tmp_content .= "<div class=\"cdmContent\" $content_hidden
  474. onclick=\"return cdmClicked(event, $id, true);\"
  475. id=\"CICD-$id\">";
  476. $tmp_content .= "<div id=\"POSTNOTE-$id\">";
  477. if ($line['note']) {
  478. $tmp_content .= Article::format_article_note($id, $line['note']);
  479. }
  480. $tmp_content .= "</div>"; //POSTNOTE
  481. if (!$line['lang']) $line['lang'] = 'en';
  482. $tmp_content .= "<div class=\"cdmContentInner\" lang=\"".$line['lang']."\">";
  483. if ($line["orig_feed_id"]) {
  484. $tmp_result = $this->dbh->query("SELECT * FROM ttrss_archived_feeds
  485. WHERE id = ".$line["orig_feed_id"] . " AND owner_uid = " . $_SESSION["uid"]);
  486. if ($this->dbh->num_rows($tmp_result) != 0) {
  487. $tmp_content .= "<div clear='both'>";
  488. $tmp_content .= __("Originally from:");
  489. $tmp_content .= "&nbsp;";
  490. $tmp_line = $this->dbh->fetch_assoc($tmp_result);
  491. $tmp_content .= "<a target='_blank' rel='noopener noreferrer'
  492. href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
  493. $tmp_line['title'] . "</a>";
  494. $tmp_content .= "&nbsp;";
  495. $tmp_content .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
  496. $tmp_content .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_unset.png'></a>";
  497. $tmp_content .= "</div>";
  498. }
  499. }
  500. $tmp_content .= "<span id=\"CWRAP-$id\">";
  501. $tmp_content .= "<span id=\"CENCW-$id\" class=\"cencw\" style=\"display : none\">";
  502. $tmp_content .= htmlspecialchars($line["content"]);
  503. $tmp_content .= "</span>";
  504. $tmp_content .= "</span>";
  505. $tmp_content .= "</div>"; //cdmContentInner
  506. $tmp_content .= "<div class=\"cdmIntermediate\">";
  507. $always_display_enclosures = sql_bool_to_bool($line["always_display_enclosures"]);
  508. $tmp_content .= Article::format_article_enclosures($id, $always_display_enclosures, $line["content"], sql_bool_to_bool($line["hide_images"]));
  509. $tmp_content .= "</div>"; // cdmIntermediate
  510. $tmp_content .= "<div class=\"cdmFooter\" onclick=\"cdmFooterClick(event)\">";
  511. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_LEFT_BUTTON) as $p) {
  512. $tmp_content .= $p->hook_article_left_button($line);
  513. }
  514. $tags_str = Article::format_tags_string($tags, $id);
  515. $tmp_content .= "<span class='left'>";
  516. $tmp_content .= "<img src='images/tag.png' alt='Tags' title='Tags'>
  517. <span id=\"ATSTR-$id\">$tags_str</span>
  518. <a title=\"".__('Edit tags for this article')."\"
  519. href=\"#\" onclick=\"editArticleTags($id)\">(+)</a>";
  520. $num_comments = (int) $line["num_comments"];
  521. $entry_comments = "";
  522. if ($num_comments > 0) {
  523. if ($line["comments"]) {
  524. $comments_url = htmlspecialchars($line["comments"]);
  525. } else {
  526. $comments_url = htmlspecialchars($line["link"]);
  527. }
  528. $entry_comments = "<a class=\"postComments\"
  529. target='_blank' rel='noopener noreferrer' href=\"$comments_url\">$num_comments ".
  530. _ngettext("comment", "comments", $num_comments)."</a>";
  531. } else {
  532. if ($line["comments"] && $line["link"] != $line["comments"]) {
  533. $entry_comments = "<a class=\"postComments\" target='_blank' rel='noopener noreferrer' href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
  534. }
  535. }
  536. if ($entry_comments) $tmp_content .= "&nbsp;($entry_comments)";
  537. $tmp_content .= "</span>";
  538. $tmp_content .= "<div>";
  539. // $tmp_content .= "$marked_pic";
  540. // $tmp_content .= "$published_pic";
  541. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_BUTTON) as $p) {
  542. $tmp_content .= $p->hook_article_button($line);
  543. }
  544. $tmp_content .= "</div>"; // buttons
  545. $tmp_content .= "</div>"; // cdmFooter
  546. $tmp_content .= "</div>"; // cdmContent
  547. $tmp_content .= "</div>"; // RROW.cdm
  548. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FORMAT_ARTICLE_CDM) as $p) {
  549. $tmp_content = $p->hook_format_article_cdm($tmp_content, $line);
  550. }
  551. $reply['content'] .= $tmp_content;
  552. }
  553. ++$lnum;
  554. }
  555. if ($_REQUEST["debug"]) $timing_info = print_checkpoint("PE", $timing_info);
  556. } else if (!is_numeric($result)) {
  557. $message = "";
  558. switch ($view_mode) {
  559. case "unread":
  560. $message = __("No unread articles found to display.");
  561. break;
  562. case "updated":
  563. $message = __("No updated articles found to display.");
  564. break;
  565. case "marked":
  566. $message = __("No starred articles found to display.");
  567. break;
  568. default:
  569. if ($feed < LABEL_BASE_INDEX) {
  570. $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.");
  571. } else {
  572. $message = __("No articles found to display.");
  573. }
  574. }
  575. if (!$offset && $message) {
  576. $reply['content'] = "<div class='whiteBox'>$message";
  577. $reply['content'] .= "<p><span class=\"insensitive\">";
  578. $result = $this->dbh->query("SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
  579. WHERE owner_uid = " . $_SESSION['uid']);
  580. $last_updated = $this->dbh->fetch_result($result, 0, "last_updated");
  581. $last_updated = make_local_datetime($last_updated, false);
  582. $reply['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
  583. $result = $this->dbh->query("SELECT COUNT(id) AS num_errors
  584. FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
  585. $num_errors = $this->dbh->fetch_result($result, 0, "num_errors");
  586. if ($num_errors > 0) {
  587. $reply['content'] .= "<br/>";
  588. $reply['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
  589. __('Some feeds have update errors (click for details)')."</a>";
  590. }
  591. $reply['content'] .= "</span></p></div>";
  592. }
  593. } else if (is_numeric($result) && $result == -1) {
  594. $reply['first_id_changed'] = true;
  595. }
  596. if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H2", $timing_info);
  597. return array($topmost_article_ids, $headlines_count, $feed, $disable_cache,
  598. $vgroup_last_feed, $reply);
  599. }
  600. function catchupAll() {
  601. $this->dbh->query("UPDATE ttrss_user_entries SET
  602. last_read = NOW(), unread = false WHERE unread = true AND owner_uid = " . $_SESSION["uid"]);
  603. CCache::zero_all($_SESSION["uid"]);
  604. }
  605. function view() {
  606. $timing_info = microtime(true);
  607. $reply = array();
  608. if ($_REQUEST["debug"]) $timing_info = print_checkpoint("0", $timing_info);
  609. $feed = $this->dbh->escape_string($_REQUEST["feed"]);
  610. $method = $this->dbh->escape_string($_REQUEST["m"]);
  611. $view_mode = $this->dbh->escape_string($_REQUEST["view_mode"]);
  612. $limit = 30;
  613. @$cat_view = $_REQUEST["cat"] == "true";
  614. @$next_unread_feed = $this->dbh->escape_string($_REQUEST["nuf"]);
  615. @$offset = $this->dbh->escape_string($_REQUEST["skip"]);
  616. @$vgroup_last_feed = $this->dbh->escape_string($_REQUEST["vgrlf"]);
  617. $order_by = $this->dbh->escape_string($_REQUEST["order_by"]);
  618. $check_first_id = $this->dbh->escape_string($_REQUEST["fid"]);
  619. if (is_numeric($feed)) $feed = (int) $feed;
  620. /* Feed -5 is a special case: it is used to display auxiliary information
  621. * when there's nothing to load - e.g. no stuff in fresh feed */
  622. if ($feed == -5) {
  623. print json_encode($this->generate_dashboard_feed());
  624. return;
  625. }
  626. $result = false;
  627. if ($feed < LABEL_BASE_INDEX) {
  628. $label_feed = Labels::feed_to_label_id($feed);
  629. $result = $this->dbh->query("SELECT id FROM ttrss_labels2 WHERE
  630. id = '$label_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_feeds WHERE
  633. id = '$feed' AND owner_uid = " . $_SESSION['uid']);
  634. } else if ($cat_view && is_numeric($feed) && $feed > 0) {
  635. $result = $this->dbh->query("SELECT id FROM ttrss_feed_categories WHERE
  636. id = '$feed' AND owner_uid = " . $_SESSION['uid']);
  637. }
  638. if ($result && $this->dbh->num_rows($result) == 0) {
  639. print json_encode($this->generate_error_feed(__("Feed not found.")));
  640. return;
  641. }
  642. /* Updating a label ccache means recalculating all of the caches
  643. * so for performance reasons we don't do that here */
  644. if ($feed >= 0) {
  645. CCache::update($feed, $_SESSION["uid"], $cat_view);
  646. }
  647. set_pref("_DEFAULT_VIEW_MODE", $view_mode);
  648. set_pref("_DEFAULT_VIEW_ORDER_BY", $order_by);
  649. /* bump login timestamp if needed */
  650. if (time() - $_SESSION["last_login_update"] > 3600) {
  651. $this->dbh->query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
  652. $_SESSION["uid"]);
  653. $_SESSION["last_login_update"] = time();
  654. }
  655. if (!$cat_view && is_numeric($feed) && $feed > 0) {
  656. $this->dbh->query("UPDATE ttrss_feeds SET last_viewed = NOW()
  657. WHERE id = '$feed' AND owner_uid = ".$_SESSION["uid"]);
  658. }
  659. $reply['headlines'] = array();
  660. $override_order = false;
  661. $skip_first_id_check = false;
  662. switch ($order_by) {
  663. case "title":
  664. $override_order = "ttrss_entries.title";
  665. break;
  666. case "date_reverse":
  667. $override_order = "score DESC, date_entered, updated";
  668. $skip_first_id_check = true;
  669. break;
  670. case "feed_dates":
  671. $override_order = "updated DESC";
  672. break;
  673. }
  674. if ($_REQUEST["debug"]) $timing_info = print_checkpoint("04", $timing_info);
  675. $ret = $this->format_headlines_list($feed, $method,
  676. $view_mode, $limit, $cat_view, $offset,
  677. $vgroup_last_feed, $override_order, true, $check_first_id, $skip_first_id_check);
  678. //$topmost_article_ids = $ret[0];
  679. $headlines_count = $ret[1];
  680. /* $returned_feed = $ret[2]; */
  681. $disable_cache = $ret[3];
  682. $vgroup_last_feed = $ret[4];
  683. //$reply['headlines']['content'] =& $ret[5]['content'];
  684. //$reply['headlines']['toolbar'] =& $ret[5]['toolbar'];
  685. $reply['headlines'] = $ret[5];
  686. if (!$next_unread_feed)
  687. $reply['headlines']['id'] = $feed;
  688. else
  689. $reply['headlines']['id'] = $next_unread_feed;
  690. $reply['headlines']['is_cat'] = (bool) $cat_view;
  691. if ($_REQUEST["debug"]) $timing_info = print_checkpoint("05", $timing_info);
  692. $reply['headlines-info'] = array("count" => (int) $headlines_count,
  693. "vgroup_last_feed" => $vgroup_last_feed,
  694. "disable_cache" => (bool) $disable_cache);
  695. if ($_REQUEST["debug"]) $timing_info = print_checkpoint("30", $timing_info);
  696. $reply['runtime-info'] = make_runtime_info();
  697. print json_encode($reply);
  698. }
  699. private function generate_dashboard_feed() {
  700. $reply = array();
  701. $reply['headlines']['id'] = -5;
  702. $reply['headlines']['is_cat'] = false;
  703. $reply['headlines']['toolbar'] = '';
  704. $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.');
  705. $reply['headlines']['content'] .= "<p><span class=\"insensitive\">";
  706. $result = $this->dbh->query("SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds
  707. WHERE owner_uid = " . $_SESSION['uid']);
  708. $last_updated = $this->dbh->fetch_result($result, 0, "last_updated");
  709. $last_updated = make_local_datetime($last_updated, false);
  710. $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated);
  711. $result = $this->dbh->query("SELECT COUNT(id) AS num_errors
  712. FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
  713. $num_errors = $this->dbh->fetch_result($result, 0, "num_errors");
  714. if ($num_errors > 0) {
  715. $reply['headlines']['content'] .= "<br/>";
  716. $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">".
  717. __('Some feeds have update errors (click for details)')."</a>";
  718. }
  719. $reply['headlines']['content'] .= "</span></p>";
  720. $reply['headlines-info'] = array("count" => 0,
  721. "vgroup_last_feed" => '',
  722. "unread" => 0,
  723. "disable_cache" => true);
  724. return $reply;
  725. }
  726. private function generate_error_feed($error) {
  727. $reply = array();
  728. $reply['headlines']['id'] = -7;
  729. $reply['headlines']['is_cat'] = false;
  730. $reply['headlines']['toolbar'] = '';
  731. $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>";
  732. $reply['headlines-info'] = array("count" => 0,
  733. "vgroup_last_feed" => '',
  734. "unread" => 0,
  735. "disable_cache" => true);
  736. return $reply;
  737. }
  738. function quickAddFeed() {
  739. print_hidden("op", "rpc");
  740. print_hidden("method", "addfeed");
  741. print "<div id='fadd_error_message' style='display : none' class='alert alert-danger'></div>";
  742. print "<div id='fadd_multiple_notify' style='display : none'>";
  743. print_notice("Provided URL is a HTML page referencing multiple feeds, please select required feed from the dropdown menu below.");
  744. print "<p></div>";
  745. print "<div class=\"dlgSec\">".__("Feed or site URL")."</div>";
  746. print "<div class=\"dlgSecCont\">";
  747. print "<div style='float : right'>
  748. <img style='display : none'
  749. id='feed_add_spinner' src='images/indicator_white.gif'></div>";
  750. print "<input style=\"font-size : 16px; width : 20em;\"
  751. placeHolder=\"".__("Feed or site URL")."\"
  752. dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"feed\" id=\"feedDlg_feedUrl\">";
  753. print "<hr/>";
  754. if (get_pref('ENABLE_FEED_CATS')) {
  755. print __('Place in category:') . " ";
  756. print_feed_cat_select("cat", false, 'dojoType="dijit.form.Select"');
  757. }
  758. print "</div>";
  759. print '<div id="feedDlg_feedsContainer" style="display : none">
  760. <div class="dlgSec">' . __('Available feeds') . '</div>
  761. <div class="dlgSecCont">'.
  762. '<select id="feedDlg_feedContainerSelect"
  763. dojoType="dijit.form.Select" size="3">
  764. <script type="dojo/method" event="onChange" args="value">
  765. dijit.byId("feedDlg_feedUrl").attr("value", value);
  766. </script>
  767. </select>'.
  768. '</div></div>';
  769. print "<div id='feedDlg_loginContainer' style='display : none'>
  770. <div class=\"dlgSec\">".__("Authentication")."</div>
  771. <div class=\"dlgSecCont\">".
  772. " <input dojoType=\"dijit.form.TextBox\" name='login'\"
  773. placeHolder=\"".__("Login")."\"
  774. autocomplete=\"new-password\"
  775. style=\"width : 10em;\"> ".
  776. " <input
  777. placeHolder=\"".__("Password")."\"
  778. dojoType=\"dijit.form.TextBox\" type='password'
  779. autocomplete=\"new-password\"
  780. style=\"width : 10em;\" name='pass'\">
  781. </div></div>";
  782. print "<div style=\"clear : both\">
  783. <input type=\"checkbox\" name=\"need_auth\" dojoType=\"dijit.form.CheckBox\" id=\"feedDlg_loginCheck\"
  784. onclick='checkboxToggleElement(this, \"feedDlg_loginContainer\")'>
  785. <label for=\"feedDlg_loginCheck\">".
  786. __('This feed requires authentication.')."</div>";
  787. print "</form>";
  788. print "<div class=\"dlgButtons\">
  789. <button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('feedAddDlg').execute()\">".__('Subscribe')."</button>";
  790. if (!(defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER)) {
  791. print "<button dojoType=\"dijit.form.Button\" onclick=\"return feedBrowser()\">".__('More feeds')."</button>";
  792. }
  793. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('feedAddDlg').hide()\">".__('Cancel')."</button>
  794. </div>";
  795. //return;
  796. }
  797. function feedBrowser() {
  798. if (defined('_DISABLE_FEED_BROWSER') && _DISABLE_FEED_BROWSER) return;
  799. $browser_search = $this->dbh->escape_string($_REQUEST["search"]);
  800. print_hidden("op", "rpc");
  801. print_hidden("method", "updateFeedBrowser");
  802. print "<div dojoType=\"dijit.Toolbar\">
  803. <div style='float : right'>
  804. <img style='display : none'
  805. id='feed_browser_spinner' src='images/indicator_white.gif'>
  806. <input name=\"search\" dojoType=\"dijit.form.TextBox\" size=\"20\" type=\"search\"
  807. onchange=\"dijit.byId('feedBrowserDlg').update()\" value=\"$browser_search\">
  808. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').update()\">".__('Search')."</button>
  809. </div>";
  810. print " <select name=\"mode\" dojoType=\"dijit.form.Select\" onchange=\"dijit.byId('feedBrowserDlg').update()\">
  811. <option value='1'>" . __('Popular feeds') . "</option>
  812. <option value='2'>" . __('Feed archive') . "</option>
  813. </select> ";
  814. print __("limit:");
  815. print " <select dojoType=\"dijit.form.Select\" name=\"limit\" onchange=\"dijit.byId('feedBrowserDlg').update()\">";
  816. foreach (array(25, 50, 100, 200) as $l) {
  817. //$issel = ($l == $limit) ? "selected=\"1\"" : "";
  818. print "<option value=\"$l\">$l</option>";
  819. }
  820. print "</select> ";
  821. print "</div>";
  822. require_once "feedbrowser.php";
  823. print "<ul class='browseFeedList' id='browseFeedList'>";
  824. print make_feed_browser("", 25);
  825. print "</ul>";
  826. print "<div align='center'>
  827. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').execute()\">".__('Subscribe')."</button>
  828. <button dojoType=\"dijit.form.Button\" style='display : none' id='feed_archive_remove' onclick=\"dijit.byId('feedBrowserDlg').removeFromArchive()\">".__('Remove')."</button>
  829. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('feedBrowserDlg').hide()\" >".__('Cancel')."</button></div>";
  830. }
  831. function search() {
  832. $this->params = explode(":", $this->dbh->escape_string($_REQUEST["param"]), 2);
  833. $active_feed_id = sprintf("%d", $this->params[0]);
  834. $is_cat = $this->params[1] != "false";
  835. print "<div class=\"dlgSec\">".__('Look for')."</div>";
  836. print "<div class=\"dlgSecCont\">";
  837. print "<input dojoType=\"dijit.form.ValidationTextBox\"
  838. style=\"font-size : 16px; width : 20em;\"
  839. required=\"1\" name=\"query\" type=\"search\" value=''>";
  840. print "<hr/><span style='float : right'>".T_sprintf('in %s', $this->getFeedTitle($active_feed_id, $is_cat))."</span>";
  841. if (DB_TYPE == "pgsql") {
  842. print "<hr/>";
  843. print_select("search_language", "", Pref_Feeds::$feed_languages,
  844. "dojoType='dijit.form.Select' title=\"".__('Used for word stemming')."\"");
  845. }
  846. print "</div>";
  847. print "<div class=\"dlgButtons\">";
  848. if (count(PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEARCH)) == 0) {
  849. print "<div style=\"float : left\">
  850. <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/SearchSyntax\">".__("Search syntax")."</a>
  851. </div>";
  852. }
  853. print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('searchDlg').execute()\">".__('Search')."</button>
  854. <button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('searchDlg').hide()\">".__('Cancel')."</button>
  855. </div>";
  856. }
  857. function update_debugger() {
  858. header("Content-type: text/html");
  859. $feed_id = (int)$_REQUEST["feed_id"];
  860. @$do_update = $_REQUEST["action"] == "do_update";
  861. $csrf_token = $_REQUEST["csrf_token"];
  862. $refetch_checked = isset($_REQUEST["force_refetch"]) ? "checked" : "";
  863. $rehash_checked = isset($_REQUEST["force_rehash"]) ? "checked" : "";
  864. ?>
  865. <html>
  866. <head>
  867. <link rel="stylesheet" type="text/css" href="css/utility.css">
  868. <title>Feed Debugger</title>
  869. </head>
  870. <body class="small_margins">
  871. <h1>Feed Debugger: <?php echo "$feed_id: " . $this->getFeedTitle($feed_id) ?></h1>
  872. <form method="GET" action="">
  873. <input type="hidden" name="op" value="feeds">
  874. <input type="hidden" name="method" value="update_debugger">
  875. <input type="hidden" name="xdebug" value="1">
  876. <input type="hidden" name="csrf_token" value="<?php echo $csrf_token ?>">
  877. <input type="hidden" name="action" value="do_update">
  878. <input type="hidden" name="feed_id" value="<?php echo $feed_id ?>">
  879. <input type="checkbox" name="force_refetch" value="1" <?php echo $refetch_checked ?>> Force refetch<br/>
  880. <input type="checkbox" name="force_rehash" value="1" <?php echo $rehash_checked ?>> Force rehash<br/>
  881. <p/><button type="submit">Continue</button>
  882. </form>
  883. <hr>
  884. <pre><?php
  885. if ($do_update) {
  886. RSSUtils::update_rss_feed($feed_id, true);
  887. }
  888. ?></pre>
  889. </body>
  890. </html>
  891. <?php
  892. }
  893. static function catchup_feed($feed, $cat_view, $owner_uid = false, $mode = 'all', $search = false) {
  894. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  895. // Todo: all this interval stuff needs some generic generator function
  896. $date_qpart = "false";
  897. $search_qpart = is_array($search) && $search[0] ? search_to_sql($search[0], $search[1])[0] : 'true';
  898. switch ($mode) {
  899. case "1day":
  900. if (DB_TYPE == "pgsql") {
  901. $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
  902. } else {
  903. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
  904. }
  905. break;
  906. case "1week":
  907. if (DB_TYPE == "pgsql") {
  908. $date_qpart = "date_entered < NOW() - INTERVAL '1 week' ";
  909. } else {
  910. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) ";
  911. }
  912. break;
  913. case "2week":
  914. if (DB_TYPE == "pgsql") {
  915. $date_qpart = "date_entered < NOW() - INTERVAL '2 week' ";
  916. } else {
  917. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) ";
  918. }
  919. break;
  920. default:
  921. $date_qpart = "true";
  922. }
  923. if (is_numeric($feed)) {
  924. if ($cat_view) {
  925. if ($feed >= 0) {
  926. if ($feed > 0) {
  927. $children = Feeds::getChildCategories($feed, $owner_uid);
  928. array_push($children, $feed);
  929. $children = join(",", $children);
  930. $cat_qpart = "cat_id IN ($children)";
  931. } else {
  932. $cat_qpart = "cat_id IS NULL";
  933. }
  934. db_query("UPDATE ttrss_user_entries
  935. SET unread = false, last_read = NOW() WHERE ref_id IN
  936. (SELECT id FROM
  937. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  938. AND owner_uid = $owner_uid AND unread = true AND feed_id IN
  939. (SELECT id FROM ttrss_feeds WHERE $cat_qpart) AND $date_qpart AND $search_qpart) as tmp)");
  940. } else if ($feed == -2) {
  941. db_query("UPDATE ttrss_user_entries
  942. SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
  943. FROM ttrss_user_labels2, ttrss_entries WHERE article_id = ref_id AND id = ref_id AND $date_qpart AND $search_qpart) > 0
  944. AND unread = true AND owner_uid = $owner_uid");
  945. }
  946. } else if ($feed > 0) {
  947. db_query("UPDATE ttrss_user_entries
  948. SET unread = false, last_read = NOW() WHERE ref_id IN
  949. (SELECT id FROM
  950. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  951. AND owner_uid = $owner_uid AND unread = true AND feed_id = $feed AND $date_qpart AND $search_qpart) as tmp)");
  952. } else if ($feed < 0 && $feed > LABEL_BASE_INDEX) { // special, like starred
  953. if ($feed == -1) {
  954. db_query("UPDATE ttrss_user_entries
  955. SET unread = false, last_read = NOW() WHERE ref_id IN
  956. (SELECT id FROM
  957. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  958. AND owner_uid = $owner_uid AND unread = true AND marked = true AND $date_qpart AND $search_qpart) as tmp)");
  959. }
  960. if ($feed == -2) {
  961. db_query("UPDATE ttrss_user_entries
  962. SET unread = false, last_read = NOW() WHERE ref_id IN
  963. (SELECT id FROM
  964. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  965. AND owner_uid = $owner_uid AND unread = true AND published = true AND $date_qpart AND $search_qpart) as tmp)");
  966. }
  967. if ($feed == -3) {
  968. $intl = get_pref("FRESH_ARTICLE_MAX_AGE");
  969. if (DB_TYPE == "pgsql") {
  970. $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
  971. } else {
  972. $match_part = "date_entered > DATE_SUB(NOW(),
  973. INTERVAL $intl HOUR) ";
  974. }
  975. db_query("UPDATE ttrss_user_entries
  976. SET unread = false, last_read = NOW() WHERE ref_id IN
  977. (SELECT id FROM
  978. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  979. AND owner_uid = $owner_uid AND score >= 0 AND unread = true AND $date_qpart AND $match_part AND $search_qpart) as tmp)");
  980. }
  981. if ($feed == -4) {
  982. db_query("UPDATE ttrss_user_entries
  983. SET unread = false, last_read = NOW() WHERE ref_id IN
  984. (SELECT id FROM
  985. (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  986. AND owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
  987. }
  988. } else if ($feed < LABEL_BASE_INDEX) { // label
  989. $label_id = Labels::feed_to_label_id($feed);
  990. db_query("UPDATE ttrss_user_entries
  991. SET unread = false, last_read = NOW() WHERE ref_id IN
  992. (SELECT id FROM
  993. (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_user_labels2 WHERE ref_id = id
  994. AND label_id = '$label_id' AND ref_id = article_id
  995. AND owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
  996. }
  997. CCache::update($feed, $owner_uid, $cat_view);
  998. } else { // tag
  999. db_query("UPDATE ttrss_user_entries
  1000. SET unread = false, last_read = NOW() WHERE ref_id IN
  1001. (SELECT id FROM
  1002. (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_tags WHERE ref_id = ttrss_entries.id
  1003. AND post_int_id = int_id AND tag_name = '$feed'
  1004. AND ttrss_user_entries.owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)");
  1005. }
  1006. }
  1007. static function getFeedArticles($feed, $is_cat = false, $unread_only = false,
  1008. $owner_uid = false) {
  1009. $n_feed = (int) $feed;
  1010. $need_entries = false;
  1011. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1012. if ($unread_only) {
  1013. $unread_qpart = "unread = true";
  1014. } else {
  1015. $unread_qpart = "true";
  1016. }
  1017. if ($is_cat) {
  1018. return Feeds::getCategoryUnread($n_feed, $owner_uid);
  1019. } else if ($n_feed == -6) {
  1020. return 0;
  1021. } else if ($feed != "0" && $n_feed == 0) {
  1022. $feed = db_escape_string($feed);
  1023. $result = db_query("SELECT SUM((SELECT COUNT(int_id)
  1024. FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
  1025. AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags
  1026. WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
  1027. return db_fetch_result($result, 0, "count");
  1028. } else if ($n_feed == -1) {
  1029. $match_part = "marked = true";
  1030. } else if ($n_feed == -2) {
  1031. $match_part = "published = true";
  1032. } else if ($n_feed == -3) {
  1033. $match_part = "unread = true AND score >= 0";
  1034. $intl = get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
  1035. if (DB_TYPE == "pgsql") {
  1036. $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
  1037. } else {
  1038. $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
  1039. }
  1040. $need_entries = true;
  1041. } else if ($n_feed == -4) {
  1042. $match_part = "true";
  1043. } else if ($n_feed >= 0) {
  1044. if ($n_feed != 0) {
  1045. $match_part = "feed_id = '$n_feed'";
  1046. } else {
  1047. $match_part = "feed_id IS NULL";
  1048. }
  1049. } else if ($feed < LABEL_BASE_INDEX) {
  1050. $label_id = Labels::feed_to_label_id($feed);
  1051. return Feeds::getLabelUnread($label_id, $owner_uid);
  1052. }
  1053. if ($match_part) {
  1054. if ($need_entries) {
  1055. $from_qpart = "ttrss_user_entries,ttrss_entries";
  1056. $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND";
  1057. } else {
  1058. $from_qpart = "ttrss_user_entries";
  1059. $from_where = "";
  1060. }
  1061. $query = "SELECT count(int_id) AS unread
  1062. FROM $from_qpart WHERE
  1063. $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
  1064. //echo "[$feed/$query]\n";
  1065. $result = db_query($query);
  1066. } else {
  1067. $result = db_query("SELECT COUNT(post_int_id) AS unread
  1068. FROM ttrss_tags,ttrss_user_entries,ttrss_entries
  1069. WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
  1070. AND $unread_qpart AND ttrss_tags.owner_uid = " . $owner_uid);
  1071. }
  1072. $unread = db_fetch_result($result, 0, "unread");
  1073. return $unread;
  1074. }
  1075. /**
  1076. * @return array (code => Status code, message => error message if available)
  1077. *
  1078. * 0 - OK, Feed already exists
  1079. * 1 - OK, Feed added
  1080. * 2 - Invalid URL
  1081. * 3 - URL content is HTML, no feeds available
  1082. * 4 - URL content is HTML which contains multiple feeds.
  1083. * Here you should call extractfeedurls in rpc-backend
  1084. * to get all possible feeds.
  1085. * 5 - Couldn't download the URL content.
  1086. * 6 - Content is an invalid XML.
  1087. */
  1088. static function subscribe_to_feed($url, $cat_id = 0,
  1089. $auth_login = '', $auth_pass = '') {
  1090. global $fetch_last_error;
  1091. global $fetch_last_error_content;
  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 = db_escape_string($auth_pass);
  1123. if (db_num_rows($result) == 0) {
  1124. $result = db_query(
  1125. "INSERT INTO ttrss_feeds
  1126. (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted)
  1127. VALUES ('".$_SESSION["uid"]."', '$url',
  1128. '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass', 0, false)");
  1129. $result = db_query(
  1130. "SELECT id FROM ttrss_feeds WHERE feed_url = '$url'
  1131. AND owner_uid = " . $_SESSION["uid"]);
  1132. $feed_id = db_fetch_result($result, 0, "id");
  1133. if ($feed_id) {
  1134. RSSUtils::set_basic_feed_info($feed_id);
  1135. }
  1136. return array("code" => 1, "feed_id" => (int) $feed_id);
  1137. } else {
  1138. return array("code" => 0, "feed_id" => (int) db_fetch_result($result, 0, "id"));
  1139. }
  1140. }
  1141. static function getFeedIcon($id) {
  1142. switch ($id) {
  1143. case 0:
  1144. return "images/archive.png";
  1145. break;
  1146. case -1:
  1147. return "images/star.png";
  1148. break;
  1149. case -2:
  1150. return "images/feed.png";
  1151. break;
  1152. case -3:
  1153. return "images/fresh.png";
  1154. break;
  1155. case -4:
  1156. return "images/folder.png";
  1157. break;
  1158. case -6:
  1159. return "images/time.png";
  1160. break;
  1161. default:
  1162. if ($id < LABEL_BASE_INDEX) {
  1163. return "images/label.png";
  1164. } else {
  1165. if (file_exists(ICONS_DIR . "/$id.ico"))
  1166. return ICONS_URL . "/$id.ico";
  1167. }
  1168. break;
  1169. }
  1170. return false;
  1171. }
  1172. static function getFeedTitle($id, $cat = false) {
  1173. if ($cat) {
  1174. return Feeds::getCategoryTitle($id);
  1175. } else if ($id == -1) {
  1176. return __("Starred articles");
  1177. } else if ($id == -2) {
  1178. return __("Published articles");
  1179. } else if ($id == -3) {
  1180. return __("Fresh articles");
  1181. } else if ($id == -4) {
  1182. return __("All articles");
  1183. } else if ($id === 0 || $id === "0") {
  1184. return __("Archived articles");
  1185. } else if ($id == -6) {
  1186. return __("Recently read");
  1187. } else if ($id < LABEL_BASE_INDEX) {
  1188. $label_id = Labels::feed_to_label_id($id);
  1189. $result = db_query("SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
  1190. if (db_num_rows($result) == 1) {
  1191. return db_fetch_result($result, 0, "caption");
  1192. } else {
  1193. return "Unknown label ($label_id)";
  1194. }
  1195. } else if (is_numeric($id) && $id > 0) {
  1196. $result = db_query("SELECT title FROM ttrss_feeds WHERE id = '$id'");
  1197. if (db_num_rows($result) == 1) {
  1198. return db_fetch_result($result, 0, "title");
  1199. } else {
  1200. return "Unknown feed ($id)";
  1201. }
  1202. } else {
  1203. return $id;
  1204. }
  1205. }
  1206. static function getCategoryUnread($cat, $owner_uid = false) {
  1207. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1208. if ($cat >= 0) {
  1209. if ($cat != 0) {
  1210. $cat_query = "cat_id = '$cat'";
  1211. } else {
  1212. $cat_query = "cat_id IS NULL";
  1213. }
  1214. $result = db_query("SELECT id FROM ttrss_feeds WHERE $cat_query
  1215. AND owner_uid = " . $owner_uid);
  1216. $cat_feeds = array();
  1217. while ($line = db_fetch_assoc($result)) {
  1218. array_push($cat_feeds, "feed_id = " . $line["id"]);
  1219. }
  1220. if (count($cat_feeds) == 0) return 0;
  1221. $match_part = implode(" OR ", $cat_feeds);
  1222. $result = db_query("SELECT COUNT(int_id) AS unread
  1223. FROM ttrss_user_entries
  1224. WHERE unread = true AND ($match_part)
  1225. AND owner_uid = " . $owner_uid);
  1226. $unread = 0;
  1227. # this needs to be rewritten
  1228. while ($line = db_fetch_assoc($result)) {
  1229. $unread += $line["unread"];
  1230. }
  1231. return $unread;
  1232. } else if ($cat == -1) {
  1233. return getFeedUnread(-1) + getFeedUnread(-2) + getFeedUnread(-3) + getFeedUnread(0);
  1234. } else if ($cat == -2) {
  1235. $result = db_query("
  1236. SELECT COUNT(unread) AS unread FROM
  1237. ttrss_user_entries, ttrss_user_labels2
  1238. WHERE article_id = ref_id AND unread = true
  1239. AND ttrss_user_entries.owner_uid = '$owner_uid'");
  1240. $unread = db_fetch_result($result, 0, "unread");
  1241. return $unread;
  1242. }
  1243. }
  1244. // only accepts real cats (>= 0)
  1245. static function getCategoryChildrenUnread($cat, $owner_uid = false) {
  1246. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1247. $result = db_query("SELECT id FROM ttrss_feed_categories WHERE parent_cat = '$cat'
  1248. AND owner_uid = $owner_uid");
  1249. $unread = 0;
  1250. while ($line = db_fetch_assoc($result)) {
  1251. $unread += Feeds::getCategoryUnread($line["id"], $owner_uid);
  1252. $unread += Feeds::getCategoryChildrenUnread($line["id"], $owner_uid);
  1253. }
  1254. return $unread;
  1255. }
  1256. static function getGlobalUnread($user_id = false) {
  1257. if (!$user_id) {
  1258. $user_id = $_SESSION["uid"];
  1259. }
  1260. $result = db_query("SELECT SUM(value) AS c_id FROM ttrss_counters_cache
  1261. WHERE owner_uid = '$user_id' AND feed_id > 0");
  1262. $c_id = db_fetch_result($result, 0, "c_id");
  1263. return $c_id;
  1264. }
  1265. static function getCategoryTitle($cat_id) {
  1266. if ($cat_id == -1) {
  1267. return __("Special");
  1268. } else if ($cat_id == -2) {
  1269. return __("Labels");
  1270. } else {
  1271. $result = db_query("SELECT title FROM ttrss_feed_categories WHERE
  1272. id = '$cat_id'");
  1273. if (db_num_rows($result) == 1) {
  1274. return db_fetch_result($result, 0, "title");
  1275. } else {
  1276. return __("Uncategorized");
  1277. }
  1278. }
  1279. }
  1280. static function getLabelUnread($label_id, $owner_uid = false) {
  1281. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1282. $result = db_query("SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2
  1283. WHERE owner_uid = '$owner_uid' AND unread = true AND label_id = '$label_id' AND article_id = ref_id");
  1284. if (db_num_rows($result) != 0) {
  1285. return db_fetch_result($result, 0, "unread");
  1286. } else {
  1287. return 0;
  1288. }
  1289. }
  1290. static function queryFeedHeadlines($params) {
  1291. $feed = $params["feed"];
  1292. $limit = isset($params["limit"]) ? $params["limit"] : 30;
  1293. $view_mode = $params["view_mode"];
  1294. $cat_view = isset($params["cat_view"]) ? $params["cat_view"] : false;
  1295. $search = isset($params["search"]) ? $params["search"] : false;
  1296. $search_language = isset($params["search_language"]) ? $params["search_language"] : "";
  1297. $override_order = isset($params["override_order"]) ? $params["override_order"] : false;
  1298. $offset = isset($params["offset"]) ? $params["offset"] : 0;
  1299. $owner_uid = isset($params["owner_uid"]) ? $params["owner_uid"] : $_SESSION["uid"];
  1300. $since_id = isset($params["since_id"]) ? $params["since_id"] : 0;
  1301. $include_children = isset($params["include_children"]) ? $params["include_children"] : false;
  1302. $ignore_vfeed_group = isset($params["ignore_vfeed_group"]) ? $params["ignore_vfeed_group"] : false;
  1303. $override_strategy = isset($params["override_strategy"]) ? $params["override_strategy"] : false;
  1304. $override_vfeed = isset($params["override_vfeed"]) ? $params["override_vfeed"] : false;
  1305. $start_ts = isset($params["start_ts"]) ? $params["start_ts"] : false;
  1306. $check_first_id = isset($params["check_first_id"]) ? $params["check_first_id"] : false;
  1307. $skip_first_id_check = isset($params["skip_first_id_check"]) ? $params["skip_first_id_check"] : false;
  1308. $ext_tables_part = "";
  1309. $query_strategy_part = "";
  1310. $search_words = array();
  1311. if ($search) {
  1312. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEARCH) as $plugin) {
  1313. list($search_query_part, $search_words) = $plugin->hook_search($search);
  1314. break;
  1315. }
  1316. // fall back in case of no plugins
  1317. if (!$search_query_part) {
  1318. list($search_query_part, $search_words) = search_to_sql($search, $search_language);
  1319. }
  1320. $search_query_part .= " AND ";
  1321. } else {
  1322. $search_query_part = "";
  1323. }
  1324. if ($since_id) {
  1325. $since_id_part = "ttrss_entries.id > $since_id AND ";
  1326. } else {
  1327. $since_id_part = "";
  1328. }
  1329. $view_query_part = "";
  1330. if ($view_mode == "adaptive") {
  1331. if ($search) {
  1332. $view_query_part = " ";
  1333. } else if ($feed != -1) {
  1334. $unread = getFeedUnread($feed, $cat_view);
  1335. if ($cat_view && $feed > 0 && $include_children)
  1336. $unread += Feeds::getCategoryChildrenUnread($feed);
  1337. if ($unread > 0) {
  1338. $view_query_part = " unread = true AND ";
  1339. }
  1340. }
  1341. }
  1342. if ($view_mode == "marked") {
  1343. $view_query_part = " marked = true AND ";
  1344. }
  1345. if ($view_mode == "has_note") {
  1346. $view_query_part = " (note IS NOT NULL AND note != '') AND ";
  1347. }
  1348. if ($view_mode == "published") {
  1349. $view_query_part = " published = true AND ";
  1350. }
  1351. if ($view_mode == "unread" && $feed != -6) {
  1352. $view_query_part = " unread = true AND ";
  1353. }
  1354. if ($limit > 0) {
  1355. $limit_query_part = "LIMIT " . $limit;
  1356. }
  1357. $allow_archived = false;
  1358. $vfeed_query_part = "";
  1359. /* tags */
  1360. if (!is_numeric($feed)) {
  1361. $query_strategy_part = "true";
  1362. $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
  1363. id = feed_id) as feed_title,";
  1364. } else if ($feed > 0) {
  1365. if ($cat_view) {
  1366. if ($feed > 0) {
  1367. if ($include_children) {
  1368. # sub-cats
  1369. $subcats = Feeds::getChildCategories($feed, $owner_uid);
  1370. array_push($subcats, $feed);
  1371. $query_strategy_part = "cat_id IN (".
  1372. implode(",", $subcats).")";
  1373. } else {
  1374. $query_strategy_part = "cat_id = '$feed'";
  1375. }
  1376. } else {
  1377. $query_strategy_part = "cat_id IS NULL";
  1378. }
  1379. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1380. } else {
  1381. $query_strategy_part = "feed_id = '$feed'";
  1382. }
  1383. } else if ($feed == 0 && !$cat_view) { // archive virtual feed
  1384. $query_strategy_part = "feed_id IS NULL";
  1385. $allow_archived = true;
  1386. } else if ($feed == 0 && $cat_view) { // uncategorized
  1387. $query_strategy_part = "cat_id IS NULL AND feed_id IS NOT NULL";
  1388. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1389. } else if ($feed == -1) { // starred virtual feed
  1390. $query_strategy_part = "marked = true";
  1391. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1392. $allow_archived = true;
  1393. if (!$override_order) {
  1394. $override_order = "last_marked DESC, date_entered DESC, updated DESC";
  1395. }
  1396. } else if ($feed == -2) { // published virtual feed OR labels category
  1397. if (!$cat_view) {
  1398. $query_strategy_part = "published = true";
  1399. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1400. $allow_archived = true;
  1401. if (!$override_order) {
  1402. $override_order = "last_published DESC, date_entered DESC, updated DESC";
  1403. }
  1404. } else {
  1405. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1406. $ext_tables_part = "ttrss_labels2,ttrss_user_labels2,";
  1407. $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
  1408. ttrss_user_labels2.article_id = ref_id";
  1409. }
  1410. } else if ($feed == -6) { // recently read
  1411. $query_strategy_part = "unread = false AND last_read IS NOT NULL";
  1412. if (DB_TYPE == "pgsql") {
  1413. $query_strategy_part .= " AND last_read > NOW() - INTERVAL '1 DAY' ";
  1414. } else {
  1415. $query_strategy_part .= " AND last_read > DATE_SUB(NOW(), INTERVAL 1 DAY) ";
  1416. }
  1417. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1418. $allow_archived = true;
  1419. $ignore_vfeed_group = true;
  1420. if (!$override_order) $override_order = "last_read DESC";
  1421. } else if ($feed == -3) { // fresh virtual feed
  1422. $query_strategy_part = "unread = true AND score >= 0";
  1423. $intl = get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
  1424. if (DB_TYPE == "pgsql") {
  1425. $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
  1426. } else {
  1427. $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
  1428. }
  1429. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1430. } else if ($feed == -4) { // all articles virtual feed
  1431. $allow_archived = true;
  1432. $query_strategy_part = "true";
  1433. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1434. } else if ($feed <= LABEL_BASE_INDEX) { // labels
  1435. $label_id = Labels::feed_to_label_id($feed);
  1436. $query_strategy_part = "label_id = '$label_id' AND
  1437. ttrss_labels2.id = ttrss_user_labels2.label_id AND
  1438. ttrss_user_labels2.article_id = ref_id";
  1439. $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
  1440. $ext_tables_part = "ttrss_labels2,ttrss_user_labels2,";
  1441. $allow_archived = true;
  1442. } else {
  1443. $query_strategy_part = "true";
  1444. }
  1445. $order_by = "score DESC, date_entered DESC, updated DESC";
  1446. if ($override_order) {
  1447. $order_by = $override_order;
  1448. }
  1449. if ($override_strategy) {
  1450. $query_strategy_part = $override_strategy;
  1451. }
  1452. if ($override_vfeed) {
  1453. $vfeed_query_part = $override_vfeed;
  1454. }
  1455. $feed_title = "";
  1456. if ($search) {
  1457. $feed_title = T_sprintf("Search results: %s", $search);
  1458. } else {
  1459. if ($cat_view) {
  1460. $feed_title = Feeds::getCategoryTitle($feed);
  1461. } else {
  1462. if (is_numeric($feed) && $feed > 0) {
  1463. $result = db_query("SELECT title,site_url,last_error,last_updated
  1464. FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = $owner_uid");
  1465. $feed_title = db_fetch_result($result, 0, "title");
  1466. $feed_site_url = db_fetch_result($result, 0, "site_url");
  1467. $last_error = db_fetch_result($result, 0, "last_error");
  1468. $last_updated = db_fetch_result($result, 0, "last_updated");
  1469. } else {
  1470. $feed_title = Feeds::getFeedTitle($feed);
  1471. }
  1472. }
  1473. }
  1474. $content_query_part = "content, ";
  1475. if ($limit_query_part) {
  1476. $offset_query_part = "OFFSET $offset";
  1477. } else {
  1478. $offset_query_part = "";
  1479. }
  1480. if (is_numeric($feed)) {
  1481. // proper override_order applied above
  1482. if ($vfeed_query_part && !$ignore_vfeed_group && get_pref('VFEED_GROUP_BY_FEED', $owner_uid)) {
  1483. if (!$override_order) {
  1484. $order_by = "ttrss_feeds.title, $order_by";
  1485. } else {
  1486. $order_by = "ttrss_feeds.title, $override_order";
  1487. }
  1488. }
  1489. if (!$allow_archived) {
  1490. $from_qpart = "${ext_tables_part}ttrss_entries LEFT JOIN ttrss_user_entries ON (ref_id = ttrss_entries.id),ttrss_feeds";
  1491. $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
  1492. } else {
  1493. $from_qpart = "${ext_tables_part}ttrss_entries LEFT JOIN ttrss_user_entries ON (ref_id = ttrss_entries.id)
  1494. LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
  1495. }
  1496. if ($vfeed_query_part) $vfeed_query_part .= "favicon_avg_color,";
  1497. if ($start_ts) {
  1498. $start_ts_formatted = date("Y/m/d H:i:s", strtotime($start_ts));
  1499. $start_ts_query_part = "date_entered >= '$start_ts_formatted' AND";
  1500. } else {
  1501. $start_ts_query_part = "";
  1502. }
  1503. $first_id = 0;
  1504. $first_id_query_strategy_part = $query_strategy_part;
  1505. if ($feed == -3)
  1506. $first_id_query_strategy_part = "true";
  1507. if (DB_TYPE == "pgsql") {
  1508. $sanity_interval_qpart = "date_entered >= NOW() - INTERVAL '1 hour' AND";
  1509. } else {
  1510. $sanity_interval_qpart = "date_entered >= DATE_SUB(NOW(), INTERVAL 1 hour) AND";
  1511. }
  1512. if (!$search && !$skip_first_id_check) {
  1513. // if previous topmost article id changed that means our current pagination is no longer valid
  1514. $query = "SELECT DISTINCT
  1515. ttrss_feeds.title,
  1516. date_entered,
  1517. guid,
  1518. ttrss_entries.id,
  1519. ttrss_entries.title,
  1520. updated,
  1521. score,
  1522. marked,
  1523. published,
  1524. last_marked,
  1525. last_published,
  1526. last_read
  1527. FROM
  1528. $from_qpart
  1529. WHERE
  1530. $feed_check_qpart
  1531. ttrss_user_entries.owner_uid = '$owner_uid' AND
  1532. $search_query_part
  1533. $start_ts_query_part
  1534. $since_id_part
  1535. $sanity_interval_qpart
  1536. $first_id_query_strategy_part ORDER BY $order_by LIMIT 1";
  1537. if ($_REQUEST["debug"]) {
  1538. print $query;
  1539. }
  1540. $result = db_query($query);
  1541. if ($result && db_num_rows($result) > 0) {
  1542. $first_id = (int)db_fetch_result($result, 0, "id");
  1543. if ($offset > 0 && $first_id && $check_first_id && $first_id != $check_first_id) {
  1544. return array(-1, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id);
  1545. }
  1546. }
  1547. }
  1548. $query = "SELECT DISTINCT
  1549. date_entered,
  1550. guid,
  1551. ttrss_entries.id,ttrss_entries.title,
  1552. updated,
  1553. label_cache,
  1554. tag_cache,
  1555. always_display_enclosures,
  1556. site_url,
  1557. note,
  1558. num_comments,
  1559. comments,
  1560. int_id,
  1561. uuid,
  1562. lang,
  1563. hide_images,
  1564. unread,feed_id,marked,published,link,last_read,orig_feed_id,
  1565. last_marked, last_published,
  1566. $vfeed_query_part
  1567. $content_query_part
  1568. author,score
  1569. FROM
  1570. $from_qpart
  1571. WHERE
  1572. $feed_check_qpart
  1573. ttrss_user_entries.owner_uid = '$owner_uid' AND
  1574. $search_query_part
  1575. $start_ts_query_part
  1576. $view_query_part
  1577. $since_id_part
  1578. $query_strategy_part ORDER BY $order_by
  1579. $limit_query_part $offset_query_part";
  1580. if ($_REQUEST["debug"]) print $query;
  1581. $result = db_query($query);
  1582. } else {
  1583. // browsing by tag
  1584. $query = "SELECT DISTINCT
  1585. date_entered,
  1586. guid,
  1587. note,
  1588. ttrss_entries.id as id,
  1589. title,
  1590. updated,
  1591. unread,
  1592. feed_id,
  1593. orig_feed_id,
  1594. marked,
  1595. num_comments,
  1596. comments,
  1597. int_id,
  1598. tag_cache,
  1599. label_cache,
  1600. link,
  1601. lang,
  1602. uuid,
  1603. last_read,
  1604. (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images,
  1605. last_marked, last_published,
  1606. $since_id_part
  1607. $vfeed_query_part
  1608. $content_query_part
  1609. author, score
  1610. FROM ttrss_entries, ttrss_user_entries, ttrss_tags
  1611. WHERE
  1612. ref_id = ttrss_entries.id AND
  1613. ttrss_user_entries.owner_uid = $owner_uid AND
  1614. post_int_id = int_id AND
  1615. tag_name = '$feed' AND
  1616. $view_query_part
  1617. $search_query_part
  1618. $query_strategy_part ORDER BY $order_by
  1619. $limit_query_part $offset_query_part";
  1620. if ($_REQUEST["debug"]) print $query;
  1621. $result = db_query($query);
  1622. }
  1623. return array($result, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words, $first_id);
  1624. }
  1625. static function getParentCategories($cat, $owner_uid) {
  1626. $rv = array();
  1627. $result = db_query("SELECT parent_cat FROM ttrss_feed_categories
  1628. WHERE id = '$cat' AND parent_cat IS NOT NULL AND owner_uid = $owner_uid");
  1629. while ($line = db_fetch_assoc($result)) {
  1630. array_push($rv, $line["parent_cat"]);
  1631. $rv = array_merge($rv, Feeds::getParentCategories($line["parent_cat"], $owner_uid));
  1632. }
  1633. return $rv;
  1634. }
  1635. static function getChildCategories($cat, $owner_uid) {
  1636. $rv = array();
  1637. $result = db_query("SELECT id FROM ttrss_feed_categories
  1638. WHERE parent_cat = '$cat' AND owner_uid = $owner_uid");
  1639. while ($line = db_fetch_assoc($result)) {
  1640. array_push($rv, $line["id"]);
  1641. $rv = array_merge($rv, Feeds::getChildCategories($line["id"], $owner_uid));
  1642. }
  1643. return $rv;
  1644. }
  1645. static function getFeedCategory($feed) {
  1646. $result = db_query("SELECT cat_id FROM ttrss_feeds
  1647. WHERE id = '$feed'");
  1648. if (db_num_rows($result) > 0) {
  1649. return db_fetch_result($result, 0, "cat_id");
  1650. } else {
  1651. return false;
  1652. }
  1653. }
  1654. }