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