feeds.php 66 KB

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