article.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. <?php
  2. class Article extends Handler_Protected {
  3. function csrf_ignore($method) {
  4. $csrf_ignored = array("redirect", "editarticletags");
  5. return array_search($method, $csrf_ignored) !== false;
  6. }
  7. function redirect() {
  8. $id = clean($_REQUEST['id']);
  9. $sth = $this->pdo->prepare("SELECT link FROM ttrss_entries, ttrss_user_entries
  10. WHERE id = ? AND id = ref_id AND owner_uid = ?
  11. LIMIT 1");
  12. $sth->execute([$id, $_SESSION['uid']]);
  13. if ($row = $sth->fetch()) {
  14. $article_url = $row['link'];
  15. $article_url = str_replace("\n", "", $article_url);
  16. header("Location: $article_url");
  17. return;
  18. } else {
  19. print_error(__("Article not found."));
  20. }
  21. }
  22. /*
  23. function view() {
  24. $id = clean($_REQUEST["id"]);
  25. $cids = explode(",", clean($_REQUEST["cids"]));
  26. $mode = clean($_REQUEST["mode"]);
  27. // in prefetch mode we only output requested cids, main article
  28. // just gets marked as read (it already exists in client cache)
  29. $articles = array();
  30. if ($mode == "") {
  31. array_push($articles, $this->format_article($id, false));
  32. } else if ($mode == "zoom") {
  33. array_push($articles, $this->format_article($id, true, true));
  34. } else if ($mode == "raw") {
  35. if (isset($_REQUEST['html'])) {
  36. header("Content-Type: text/html");
  37. print '<link rel="stylesheet" type="text/css" href="css/default.css"/>';
  38. }
  39. $article = $this->format_article($id, false, isset($_REQUEST["zoom"]));
  40. print $article['content'];
  41. return;
  42. }
  43. $this->catchupArticleById($id, 0);
  44. if (!$_SESSION["bw_limit"]) {
  45. foreach ($cids as $cid) {
  46. if ($cid) {
  47. array_push($articles, $this->format_article($cid, false, false));
  48. }
  49. }
  50. }
  51. print json_encode($articles);
  52. } */
  53. /*
  54. private function catchupArticleById($id, $cmode) {
  55. if ($cmode == 0) {
  56. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  57. unread = false,last_read = NOW()
  58. WHERE ref_id = ? AND owner_uid = ?");
  59. } else if ($cmode == 1) {
  60. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  61. unread = true
  62. WHERE ref_id = ? AND owner_uid = ?");
  63. } else {
  64. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  65. unread = NOT unread,last_read = NOW()
  66. WHERE ref_id = ? AND owner_uid = ?");
  67. }
  68. $sth->execute([$id, $_SESSION['uid']]);
  69. $feed_id = $this->getArticleFeed($id);
  70. CCache::update($feed_id, $_SESSION["uid"]);
  71. }
  72. */
  73. static function create_published_article($title, $url, $content, $labels_str,
  74. $owner_uid) {
  75. $guid = 'SHA1:' . sha1("ttshared:" . $url . $owner_uid); // include owner_uid to prevent global GUID clash
  76. if (!$content) {
  77. $pluginhost = new PluginHost();
  78. $pluginhost->load_all(PluginHost::KIND_ALL, $owner_uid);
  79. $pluginhost->load_data();
  80. foreach ($pluginhost->get_hooks(PluginHost::HOOK_GET_FULL_TEXT) as $p) {
  81. $extracted_content = $p->hook_get_full_text($url);
  82. if ($extracted_content) {
  83. $content = $extracted_content;
  84. break;
  85. }
  86. }
  87. }
  88. $content_hash = sha1($content);
  89. if ($labels_str != "") {
  90. $labels = explode(",", $labels_str);
  91. } else {
  92. $labels = array();
  93. }
  94. $rc = false;
  95. if (!$title) $title = $url;
  96. if (!$title && !$url) return false;
  97. if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false;
  98. $pdo = Db::pdo();
  99. $pdo->beginTransaction();
  100. // only check for our user data here, others might have shared this with different content etc
  101. $sth = $pdo->prepare("SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
  102. guid = ? AND ref_id = id AND owner_uid = ? LIMIT 1");
  103. $sth->execute([$guid, $owner_uid]);
  104. if ($row = $sth->fetch()) {
  105. $ref_id = $row['id'];
  106. $sth = $pdo->prepare("SELECT int_id FROM ttrss_user_entries WHERE
  107. ref_id = ? AND owner_uid = ? LIMIT 1");
  108. $sth->execute([$ref_id, $owner_uid]);
  109. if ($row = $sth->fetch()) {
  110. $int_id = $row['int_id'];
  111. $sth = $pdo->prepare("UPDATE ttrss_entries SET
  112. content = ?, content_hash = ? WHERE id = ?");
  113. $sth->execute([$content, $content_hash, $ref_id]);
  114. if (DB_TYPE == "pgsql"){
  115. $sth = $pdo->prepare("UPDATE ttrss_entries
  116. SET tsvector_combined = to_tsvector( :ts_content)
  117. WHERE id = :id");
  118. $params = [
  119. ":ts_content" => mb_substr(strip_tags($content ), 0, 900000),
  120. ":id" => $ref_id];
  121. $sth->execute($params);
  122. }
  123. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET published = true,
  124. last_published = NOW() WHERE
  125. int_id = ? AND owner_uid = ?");
  126. $sth->execute([$int_id, $owner_uid]);
  127. } else {
  128. $sth = $pdo->prepare("INSERT INTO ttrss_user_entries
  129. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
  130. last_read, note, unread, last_published)
  131. VALUES
  132. (?, '', NULL, NULL, ?, true, '', '', NOW(), '', false, NOW())");
  133. $sth->execute([$ref_id, $owner_uid]);
  134. }
  135. if (count($labels) != 0) {
  136. foreach ($labels as $label) {
  137. Labels::add_article($ref_id, trim($label), $owner_uid);
  138. }
  139. }
  140. $rc = true;
  141. } else {
  142. $sth = $pdo->prepare("INSERT INTO ttrss_entries
  143. (title, guid, link, updated, content, content_hash, date_entered, date_updated)
  144. VALUES
  145. (?, ?, ?, NOW(), ?, ?, NOW(), NOW())");
  146. $sth->execute([$title, $guid, $url, $content, $content_hash]);
  147. $sth = $pdo->prepare("SELECT id FROM ttrss_entries WHERE guid = ?");
  148. $sth->execute([$guid]);
  149. if ($row = $sth->fetch()) {
  150. $ref_id = $row["id"];
  151. if (DB_TYPE == "pgsql"){
  152. $sth = $pdo->prepare("UPDATE ttrss_entries
  153. SET tsvector_combined = to_tsvector( :ts_content)
  154. WHERE id = :id");
  155. $params = [
  156. ":ts_content" => mb_substr(strip_tags($content ), 0, 900000),
  157. ":id" => $ref_id];
  158. $sth->execute($params);
  159. }
  160. $sth = $pdo->prepare("INSERT INTO ttrss_user_entries
  161. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
  162. last_read, note, unread, last_published)
  163. VALUES
  164. (?, '', NULL, NULL, ?, true, '', '', NOW(), '', false, NOW())");
  165. $sth->execute([$ref_id, $owner_uid]);
  166. if (count($labels) != 0) {
  167. foreach ($labels as $label) {
  168. Labels::add_article($ref_id, trim($label), $owner_uid);
  169. }
  170. }
  171. $rc = true;
  172. }
  173. }
  174. $pdo->commit();
  175. return $rc;
  176. }
  177. function editArticleTags() {
  178. $param = clean($_REQUEST['param']);
  179. $tags = Article::get_article_tags($param);
  180. $tags_str = join(", ", $tags);
  181. print_hidden("id", "$param");
  182. print_hidden("op", "article");
  183. print_hidden("method", "setArticleTags");
  184. print "<header class='horizontal'>" . __("Tags for this article (separated by commas):")."</header>";
  185. print "<section>";
  186. print "<textarea dojoType='dijit.form.SimpleTextarea' rows='4'
  187. style='height : 100px; font-size : 12px; width : 98%' id='tags_str'
  188. name='tags_str'>$tags_str</textarea>
  189. <div class='autocomplete' id='tags_choices'
  190. style='display:none'></div>";
  191. print "</section>";
  192. print "<footer>";
  193. print "<button dojoType='dijit.form.Button'
  194. type='submit' class='alt-primary' onclick=\"dijit.byId('editTagsDlg').execute()\">".__('Save')."</button> ";
  195. print "<button dojoType='dijit.form.Button'
  196. onclick=\"dijit.byId('editTagsDlg').hide()\">".__('Cancel')."</button>";
  197. print "</footer>";
  198. }
  199. function setScore() {
  200. $ids = explode(",", clean($_REQUEST['id']));
  201. $score = (int)clean($_REQUEST['score']);
  202. $ids_qmarks = arr_qmarks($ids);
  203. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  204. score = ? WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  205. $sth->execute(array_merge([$score], $ids, [$_SESSION['uid']]));
  206. print json_encode(["id" => $ids, "score" => (int)$score]);
  207. }
  208. function getScore() {
  209. $id = clean($_REQUEST['id']);
  210. $sth = $this->pdo->prepare("SELECT score FROM ttrss_user_entries WHERE ref_id = ? AND owner_uid = ?");
  211. $sth->execute([$id, $_SESSION['uid']]);
  212. $row = $sth->fetch();
  213. $score = $row['score'];
  214. print json_encode(["id" => $id, "score" => (int)$score]);
  215. }
  216. function setArticleTags() {
  217. $id = clean($_REQUEST["id"]);
  218. $tags_str = clean($_REQUEST["tags_str"]);
  219. $tags = array_unique(trim_array(explode(",", $tags_str)));
  220. $this->pdo->beginTransaction();
  221. $sth = $this->pdo->prepare("SELECT int_id FROM ttrss_user_entries WHERE
  222. ref_id = ? AND owner_uid = ? LIMIT 1");
  223. $sth->execute([$id, $_SESSION['uid']]);
  224. if ($row = $sth->fetch()) {
  225. $tags_to_cache = array();
  226. $int_id = $row['int_id'];
  227. $sth = $this->pdo->prepare("DELETE FROM ttrss_tags WHERE
  228. post_int_id = ? AND owner_uid = ?");
  229. $sth->execute([$int_id, $_SESSION['uid']]);
  230. foreach ($tags as $tag) {
  231. $tag = Article::sanitize_tag($tag);
  232. if (!Article::tag_is_valid($tag)) {
  233. continue;
  234. }
  235. if (preg_match("/^[0-9]*$/", $tag)) {
  236. continue;
  237. }
  238. // print "<!-- $id : $int_id : $tag -->";
  239. if ($tag != '') {
  240. $sth = $this->pdo->prepare("INSERT INTO ttrss_tags
  241. (post_int_id, owner_uid, tag_name)
  242. VALUES (?, ?, ?)");
  243. $sth->execute([$int_id, $_SESSION['uid'], $tag]);
  244. }
  245. array_push($tags_to_cache, $tag);
  246. }
  247. /* update tag cache */
  248. sort($tags_to_cache);
  249. $tags_str = join(",", $tags_to_cache);
  250. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries
  251. SET tag_cache = ? WHERE ref_id = ? AND owner_uid = ?");
  252. $sth->execute([$tags_str, $id, $_SESSION['uid']]);
  253. }
  254. $this->pdo->commit();
  255. $tags = Article::get_article_tags($id);
  256. $tags_str = $this->format_tags_string($tags, $id);
  257. $tags_str_full = join(", ", $tags);
  258. if (!$tags_str_full) $tags_str_full = __("no tags");
  259. print json_encode(array("id" => (int)$id,
  260. "content" => $tags_str, "content_full" => $tags_str_full));
  261. }
  262. function completeTags() {
  263. $search = clean($_REQUEST["search"]);
  264. $sth = $this->pdo->prepare("SELECT DISTINCT tag_name FROM ttrss_tags
  265. WHERE owner_uid = ? AND
  266. tag_name LIKE ? ORDER BY tag_name
  267. LIMIT 10");
  268. $sth->execute([$_SESSION['uid'], "$search%"]);
  269. print "<ul>";
  270. while ($line = $sth->fetch()) {
  271. print "<li>" . $line["tag_name"] . "</li>";
  272. }
  273. print "</ul>";
  274. }
  275. function assigntolabel() {
  276. return $this->labelops(true);
  277. }
  278. function removefromlabel() {
  279. return $this->labelops(false);
  280. }
  281. private function labelops($assign) {
  282. $reply = array();
  283. $ids = explode(",", clean($_REQUEST["ids"]));
  284. $label_id = clean($_REQUEST["lid"]);
  285. $label = Labels::find_caption($label_id, $_SESSION["uid"]);
  286. $reply["info-for-headlines"] = array();
  287. if ($label) {
  288. foreach ($ids as $id) {
  289. if ($assign)
  290. Labels::add_article($id, $label, $_SESSION["uid"]);
  291. else
  292. Labels::remove_article($id, $label, $_SESSION["uid"]);
  293. $labels = $this->get_article_labels($id, $_SESSION["uid"]);
  294. array_push($reply["info-for-headlines"],
  295. array("id" => $id, "labels" => $this->format_article_labels($labels)));
  296. }
  297. }
  298. $reply["message"] = "UPDATE_COUNTERS";
  299. print json_encode($reply);
  300. }
  301. function getArticleFeed($id) {
  302. $sth = $this->pdo->prepare("SELECT feed_id FROM ttrss_user_entries
  303. WHERE ref_id = ? AND owner_uid = ?");
  304. $sth->execute([$id, $_SESSION['uid']]);
  305. if ($row = $sth->fetch()) {
  306. return $row["feed_id"];
  307. } else {
  308. return 0;
  309. }
  310. }
  311. static function format_article_enclosures($id, $always_display_enclosures,
  312. $article_content, $hide_images = false) {
  313. $result = Article::get_article_enclosures($id);
  314. $rv = '';
  315. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FORMAT_ENCLOSURES) as $plugin) {
  316. $retval = $plugin->hook_format_enclosures($rv, $result, $id, $always_display_enclosures, $article_content, $hide_images);
  317. if (is_array($retval)) {
  318. $rv = $retval[0];
  319. $result = $retval[1];
  320. } else {
  321. $rv = $retval;
  322. }
  323. }
  324. unset($retval); // Unset to prevent breaking render if there are no HOOK_RENDER_ENCLOSURE hooks below.
  325. if ($rv === '' && !empty($result)) {
  326. $entries_html = array();
  327. $entries = array();
  328. $entries_inline = array();
  329. foreach ($result as $line) {
  330. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ENCLOSURE_ENTRY) as $plugin) {
  331. $line = $plugin->hook_enclosure_entry($line);
  332. }
  333. $url = $line["content_url"];
  334. $ctype = $line["content_type"];
  335. $title = $line["title"];
  336. $width = $line["width"];
  337. $height = $line["height"];
  338. if (!$ctype) $ctype = __("unknown type");
  339. //$filename = substr($url, strrpos($url, "/")+1);
  340. $filename = basename($url);
  341. $player = format_inline_player($url, $ctype);
  342. if ($player) array_push($entries_inline, $player);
  343. # $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\" rel=\"noopener noreferrer\">" .
  344. # $filename . " (" . $ctype . ")" . "</a>";
  345. $entry = "<div onclick=\"popupOpenUrl('".htmlspecialchars($url)."')\"
  346. dojoType=\"dijit.MenuItem\">$filename ($ctype)</div>";
  347. array_push($entries_html, $entry);
  348. $entry = array();
  349. $entry["type"] = $ctype;
  350. $entry["filename"] = $filename;
  351. $entry["url"] = $url;
  352. $entry["title"] = $title;
  353. $entry["width"] = $width;
  354. $entry["height"] = $height;
  355. array_push($entries, $entry);
  356. }
  357. if ($_SESSION['uid'] && !get_pref("STRIP_IMAGES") && !$_SESSION["bw_limit"]) {
  358. if ($always_display_enclosures ||
  359. !preg_match("/<img/i", $article_content)) {
  360. foreach ($entries as $entry) {
  361. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ENCLOSURE) as $plugin)
  362. $retval = $plugin->hook_render_enclosure($entry, $hide_images);
  363. if ($retval) {
  364. $rv .= $retval;
  365. } else {
  366. if (preg_match("/image/", $entry["type"])) {
  367. if (!$hide_images) {
  368. $encsize = '';
  369. if ($entry['height'] > 0)
  370. $encsize .= ' height="' . intval($entry['height']) . '"';
  371. if ($entry['width'] > 0)
  372. $encsize .= ' width="' . intval($entry['width']) . '"';
  373. $rv .= "<p><img
  374. alt=\"".htmlspecialchars($entry["filename"])."\"
  375. src=\"" .htmlspecialchars($entry["url"]) . "\"
  376. " . $encsize . " /></p>";
  377. } else {
  378. $rv .= "<p><a target=\"_blank\" rel=\"noopener noreferrer\"
  379. href=\"".htmlspecialchars($entry["url"])."\"
  380. >" .htmlspecialchars($entry["url"]) . "</a></p>";
  381. }
  382. if ($entry['title']) {
  383. $rv.= "<div class=\"enclosure_title\">${entry['title']}</div>";
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. if (count($entries_inline) > 0) {
  391. //$rv .= "<hr clear='both'/>";
  392. foreach ($entries_inline as $entry) { $rv .= $entry; };
  393. $rv .= "<br clear='both'/>";
  394. }
  395. $rv .= "<div class=\"attachments\" dojoType=\"fox.form.DropDownButton\">".
  396. "<span>" . __('Attachments')."</span>";
  397. $rv .= "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  398. foreach ($entries as $entry) {
  399. if ($entry["title"])
  400. $title = " &mdash; " . truncate_string($entry["title"], 30);
  401. else
  402. $title = "";
  403. if ($entry["filename"])
  404. $filename = truncate_middle(htmlspecialchars($entry["filename"]), 60);
  405. else
  406. $filename = "";
  407. $rv .= "<div onclick='popupOpenUrl(\"".htmlspecialchars($entry["url"])."\")'
  408. dojoType=\"dijit.MenuItem\">".$filename . $title."</div>";
  409. };
  410. $rv .= "</div>";
  411. $rv .= "</div>";
  412. }
  413. return $rv;
  414. }
  415. static function get_article_tags($id, $owner_uid = 0, $tag_cache = false) {
  416. $a_id = $id;
  417. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  418. $pdo = Db::pdo();
  419. $sth = $pdo->prepare("SELECT DISTINCT tag_name,
  420. owner_uid as owner FROM ttrss_tags
  421. WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
  422. ref_id = ? AND owner_uid = ? LIMIT 1) ORDER BY tag_name");
  423. $tags = array();
  424. /* check cache first */
  425. if ($tag_cache === false) {
  426. $csth = $pdo->prepare("SELECT tag_cache FROM ttrss_user_entries
  427. WHERE ref_id = ? AND owner_uid = ?");
  428. $csth->execute([$id, $owner_uid]);
  429. if ($row = $csth->fetch()) $tag_cache = $row["tag_cache"];
  430. }
  431. if ($tag_cache) {
  432. $tags = explode(",", $tag_cache);
  433. } else {
  434. /* do it the hard way */
  435. $sth->execute([$a_id, $owner_uid]);
  436. while ($tmp_line = $sth->fetch()) {
  437. array_push($tags, $tmp_line["tag_name"]);
  438. }
  439. /* update the cache */
  440. $tags_str = join(",", $tags);
  441. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  442. SET tag_cache = ? WHERE ref_id = ?
  443. AND owner_uid = ?");
  444. $sth->execute([$tags_str, $id, $owner_uid]);
  445. }
  446. return $tags;
  447. }
  448. static function format_tags_string($tags) {
  449. if (!is_array($tags) || count($tags) == 0) {
  450. return __("no tags");
  451. } else {
  452. $maxtags = min(5, count($tags));
  453. $tags_str = "";
  454. for ($i = 0; $i < $maxtags; $i++) {
  455. $tags_str .= "<a class=\"tag\" href=\"#\" onclick=\"Feeds.open({feed:'".$tags[$i]."'})\">" . $tags[$i] . "</a>, ";
  456. }
  457. $tags_str = mb_substr($tags_str, 0, mb_strlen($tags_str)-2);
  458. if (count($tags) > $maxtags)
  459. $tags_str .= ", &hellip;";
  460. return $tags_str;
  461. }
  462. }
  463. static function format_article_labels($labels) {
  464. if (!is_array($labels)) return '';
  465. $labels_str = "";
  466. foreach ($labels as $l) {
  467. $labels_str .= sprintf("<div class='label'
  468. style='color : %s; background-color : %s'>%s</div>",
  469. $l[2], $l[3], $l[1]);
  470. }
  471. return $labels_str;
  472. }
  473. static function format_article_note($id, $note, $allow_edit = true) {
  474. if ($allow_edit) {
  475. $onclick = "onclick='Plugins.Note.edit($id)'";
  476. $note_class = 'editable';
  477. } else {
  478. $onclick = '';
  479. $note_class = '';
  480. }
  481. return "<div class='article-note $note_class'>
  482. <i class='material-icons'>note</i>
  483. <div $onclick class='body'>$note</div>
  484. </div>";
  485. return $str;
  486. }
  487. static function get_article_enclosures($id) {
  488. $pdo = Db::pdo();
  489. $sth = $pdo->prepare("SELECT * FROM ttrss_enclosures
  490. WHERE post_id = ? AND content_url != ''");
  491. $sth->execute([$id]);
  492. $rv = array();
  493. while ($line = $sth->fetch()) {
  494. if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) {
  495. $line["content_url"] = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($line["content_url"]);
  496. }
  497. array_push($rv, $line);
  498. }
  499. return $rv;
  500. }
  501. static function purge_orphans() {
  502. // purge orphaned posts in main content table
  503. if (DB_TYPE == "mysql")
  504. $limit_qpart = "LIMIT 5000";
  505. else
  506. $limit_qpart = "";
  507. $pdo = Db::pdo();
  508. $res = $pdo->query("DELETE FROM ttrss_entries WHERE
  509. NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id) $limit_qpart");
  510. if (Debug::enabled()) {
  511. $rows = $res->rowCount();
  512. Debug::log("Purged $rows orphaned posts.");
  513. }
  514. }
  515. static function catchupArticlesById($ids, $cmode, $owner_uid = false) {
  516. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  517. $pdo = Db::pdo();
  518. $ids_qmarks = arr_qmarks($ids);
  519. if ($cmode == 1) {
  520. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  521. unread = true
  522. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  523. } else if ($cmode == 2) {
  524. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  525. unread = NOT unread,last_read = NOW()
  526. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  527. } else {
  528. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  529. unread = false,last_read = NOW()
  530. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  531. }
  532. $sth->execute(array_merge($ids, [$owner_uid]));
  533. /* update ccache */
  534. $sth = $pdo->prepare("SELECT DISTINCT feed_id FROM ttrss_user_entries
  535. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  536. $sth->execute(array_merge($ids, [$owner_uid]));
  537. while ($line = $sth->fetch()) {
  538. CCache::update($line["feed_id"], $owner_uid);
  539. }
  540. }
  541. static function getLastArticleId() {
  542. $pdo = DB::pdo();
  543. $sth = $pdo->prepare("SELECT ref_id AS id FROM ttrss_user_entries
  544. WHERE owner_uid = ? ORDER BY ref_id DESC LIMIT 1");
  545. $sth->execute([$_SESSION['uid']]);
  546. if ($row = $sth->fetch()) {
  547. return $row['id'];
  548. } else {
  549. return -1;
  550. }
  551. }
  552. static function get_article_labels($id, $owner_uid = false) {
  553. $rv = array();
  554. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  555. $pdo = Db::pdo();
  556. $sth = $pdo->prepare("SELECT label_cache FROM
  557. ttrss_user_entries WHERE ref_id = ? AND owner_uid = ?");
  558. $sth->execute([$id, $owner_uid]);
  559. if ($row = $sth->fetch()) {
  560. $label_cache = $row["label_cache"];
  561. if ($label_cache) {
  562. $tmp = json_decode($label_cache, true);
  563. if (!$tmp || $tmp["no-labels"] == 1)
  564. return $rv;
  565. else
  566. return $tmp;
  567. }
  568. }
  569. $sth = $pdo->prepare("SELECT DISTINCT label_id,caption,fg_color,bg_color
  570. FROM ttrss_labels2, ttrss_user_labels2
  571. WHERE id = label_id
  572. AND article_id = ?
  573. AND owner_uid = ?
  574. ORDER BY caption");
  575. $sth->execute([$id, $owner_uid]);
  576. while ($line = $sth->fetch()) {
  577. $rk = array(Labels::label_to_feed_id($line["label_id"]),
  578. $line["caption"], $line["fg_color"],
  579. $line["bg_color"]);
  580. array_push($rv, $rk);
  581. }
  582. if (count($rv) > 0)
  583. Labels::update_cache($owner_uid, $id, $rv);
  584. else
  585. Labels::update_cache($owner_uid, $id, array("no-labels" => 1));
  586. return $rv;
  587. }
  588. static function sanitize_tag($tag) {
  589. $tag = trim($tag);
  590. $tag = mb_strtolower($tag, 'utf-8');
  591. $tag = preg_replace('/[,\'\"\+\>\<]/', "", $tag);
  592. if (DB_TYPE == "mysql") {
  593. $tag = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $tag);
  594. }
  595. return $tag;
  596. }
  597. static function tag_is_valid($tag) {
  598. if (!$tag || is_numeric($tag) || mb_strlen($tag) > 250)
  599. return false;
  600. return true;
  601. }
  602. }