feeds.php 71 KB

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