article.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. function view() {
  23. $id = clean($_REQUEST["id"]);
  24. $cids = explode(",", clean($_REQUEST["cids"]));
  25. $mode = clean($_REQUEST["mode"]);
  26. // in prefetch mode we only output requested cids, main article
  27. // just gets marked as read (it already exists in client cache)
  28. $articles = array();
  29. if ($mode == "") {
  30. array_push($articles, $this->format_article($id, false));
  31. } else if ($mode == "zoom") {
  32. array_push($articles, $this->format_article($id, true, true));
  33. } else if ($mode == "raw") {
  34. if (isset($_REQUEST['html'])) {
  35. header("Content-Type: text/html");
  36. print '<link rel="stylesheet" type="text/css" href="css/default.css"/>';
  37. }
  38. $article = $this->format_article($id, false, isset($_REQUEST["zoom"]));
  39. print $article['content'];
  40. return;
  41. }
  42. $this->catchupArticleById($id, 0);
  43. if (!$_SESSION["bw_limit"]) {
  44. foreach ($cids as $cid) {
  45. if ($cid) {
  46. array_push($articles, $this->format_article($cid, false, false));
  47. }
  48. }
  49. }
  50. print json_encode($articles);
  51. }
  52. private function catchupArticleById($id, $cmode) {
  53. if ($cmode == 0) {
  54. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  55. unread = false,last_read = NOW()
  56. WHERE ref_id = ? AND owner_uid = ?");
  57. } else if ($cmode == 1) {
  58. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  59. unread = true
  60. WHERE ref_id = ? AND owner_uid = ?");
  61. } else {
  62. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  63. unread = NOT unread,last_read = NOW()
  64. WHERE ref_id = ? AND owner_uid = ?");
  65. }
  66. $sth->execute([$id, $_SESSION['uid']]);
  67. $feed_id = $this->getArticleFeed($id);
  68. CCache::update($feed_id, $_SESSION["uid"]);
  69. }
  70. static function create_published_article($title, $url, $content, $labels_str,
  71. $owner_uid) {
  72. $guid = 'SHA1:' . sha1("ttshared:" . $url . $owner_uid); // include owner_uid to prevent global GUID clash
  73. if (!$content) {
  74. $pluginhost = new PluginHost();
  75. $pluginhost->load_all(PluginHost::KIND_ALL, $owner_uid);
  76. $pluginhost->load_data();
  77. $af_readability = $pluginhost->get_plugin("Af_Readability");
  78. if ($af_readability) {
  79. $enable_share_anything = $pluginhost->get($af_readability, "enable_share_anything");
  80. if ($enable_share_anything) {
  81. $extracted_content = $af_readability->extract_content($url);
  82. if ($extracted_content) $content = $extracted_content;
  83. }
  84. }
  85. }
  86. $content_hash = sha1($content);
  87. if ($labels_str != "") {
  88. $labels = explode(",", $labels_str);
  89. } else {
  90. $labels = array();
  91. }
  92. $rc = false;
  93. if (!$title) $title = $url;
  94. if (!$title && !$url) return false;
  95. if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false;
  96. $pdo = Db::pdo();
  97. $pdo->beginTransaction();
  98. // only check for our user data here, others might have shared this with different content etc
  99. $sth = $pdo->prepare("SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
  100. guid = ? AND ref_id = id AND owner_uid = ? LIMIT 1");
  101. $sth->execute([$guid, $owner_uid]);
  102. if ($row = $sth->fetch()) {
  103. $ref_id = $row['id'];
  104. $sth = $pdo->prepare("SELECT int_id FROM ttrss_user_entries WHERE
  105. ref_id = ? AND owner_uid = ? LIMIT 1");
  106. $sth->execute([$ref_id, $owner_uid]);
  107. if ($row = $sth->fetch()) {
  108. $int_id = $row['int_id'];
  109. $sth = $pdo->prepare("UPDATE ttrss_entries SET
  110. content = ?, content_hash = ? WHERE id = ?");
  111. $sth->execute([$content, $content_hash, $ref_id]);
  112. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET published = true,
  113. last_published = NOW() WHERE
  114. int_id = ? AND owner_uid = ?");
  115. $sth->execute([$int_id, $owner_uid]);
  116. } else {
  117. $sth = $pdo->prepare("INSERT INTO ttrss_user_entries
  118. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
  119. last_read, note, unread, last_published)
  120. VALUES
  121. (?, '', NULL, NULL, ?, true, '', '', NOW(), '', false, NOW())");
  122. $sth->execute([$ref_id, $owner_uid]);
  123. }
  124. if (count($labels) != 0) {
  125. foreach ($labels as $label) {
  126. Labels::add_article($ref_id, trim($label), $owner_uid);
  127. }
  128. }
  129. $rc = true;
  130. } else {
  131. $sth = $pdo->prepare("INSERT INTO ttrss_entries
  132. (title, guid, link, updated, content, content_hash, date_entered, date_updated)
  133. VALUES
  134. (?, ?, ?, NOW(), ?, ?, NOW(), NOW())");
  135. $sth->execute([$title, $guid, $url, $content, $content_hash]);
  136. $sth = $pdo->prepare("SELECT id FROM ttrss_entries WHERE guid = ?");
  137. $sth->execute([$guid]);
  138. if ($row = $sth->fetch()) {
  139. $ref_id = $row["id"];
  140. $sth = $pdo->prepare("INSERT INTO ttrss_user_entries
  141. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
  142. last_read, note, unread, last_published)
  143. VALUES
  144. (?, '', NULL, NULL, ?, true, '', '', NOW(), '', false, NOW())");
  145. $sth->execute([$ref_id, $owner_uid]);
  146. if (count($labels) != 0) {
  147. foreach ($labels as $label) {
  148. Labels::add_article($ref_id, trim($label), $owner_uid);
  149. }
  150. }
  151. $rc = true;
  152. }
  153. }
  154. $pdo->commit();
  155. return $rc;
  156. }
  157. function editArticleTags() {
  158. print __("Tags for this article (separated by commas):")."<br>";
  159. $param = clean($_REQUEST['param']);
  160. $tags = Article::get_article_tags($param);
  161. $tags_str = join(", ", $tags);
  162. print_hidden("id", "$param");
  163. print_hidden("op", "article");
  164. print_hidden("method", "setArticleTags");
  165. print "<table width='100%'><tr><td>";
  166. print "<textarea dojoType=\"dijit.form.SimpleTextarea\" rows='4'
  167. style='height : 100px; font-size : 12px; width : 98%' id=\"tags_str\"
  168. name='tags_str'>$tags_str</textarea>
  169. <div class=\"autocomplete\" id=\"tags_choices\"
  170. style=\"display:none\"></div>";
  171. print "</td></tr></table>";
  172. print "<div class='dlgButtons'>";
  173. print "<button dojoType=\"dijit.form.Button\"
  174. onclick=\"dijit.byId('editTagsDlg').execute()\">".__('Save')."</button> ";
  175. print "<button dojoType=\"dijit.form.Button\"
  176. onclick=\"dijit.byId('editTagsDlg').hide()\">".__('Cancel')."</button>";
  177. print "</div>";
  178. }
  179. function setScore() {
  180. $ids = explode(",", clean($_REQUEST['id']));
  181. $score = (int)clean($_REQUEST['score']);
  182. $ids_qmarks = arr_qmarks($ids);
  183. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET
  184. score = ? WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  185. $sth->execute(array_merge([$score], $ids, [$_SESSION['uid']]));
  186. print json_encode(array("id" => $ids,
  187. "score" => (int)$score,
  188. "score_pic" => get_score_pic($score)));
  189. }
  190. function getScore() {
  191. $id = clean($_REQUEST['id']);
  192. $sth = $this->pdo->prepare("SELECT score FROM ttrss_user_entries WHERE ref_id = ? AND owner_uid = ?");
  193. $sth->execute([$id, $_SESSION['uid']]);
  194. $row = $sth->fetch();
  195. $score = $row['score'];
  196. print json_encode(array("id" => $id,
  197. "score" => (int)$score,
  198. "score_pic" => get_score_pic($score)));
  199. }
  200. function setArticleTags() {
  201. $id = clean($_REQUEST["id"]);
  202. $tags_str = clean($_REQUEST["tags_str"]);
  203. $tags = array_unique(trim_array(explode(",", $tags_str)));
  204. $this->pdo->beginTransaction();
  205. $sth = $this->pdo->prepare("SELECT int_id FROM ttrss_user_entries WHERE
  206. ref_id = ? AND owner_uid = ? LIMIT 1");
  207. $sth->execute([$id, $_SESSION['uid']]);
  208. if ($row = $sth->fetch()) {
  209. $tags_to_cache = array();
  210. $int_id = $row['int_id'];
  211. $sth = $this->pdo->prepare("DELETE FROM ttrss_tags WHERE
  212. post_int_id = ? AND owner_uid = ?");
  213. $sth->execute([$int_id, $_SESSION['uid']]);
  214. foreach ($tags as $tag) {
  215. $tag = sanitize_tag($tag);
  216. if (!tag_is_valid($tag)) {
  217. continue;
  218. }
  219. if (preg_match("/^[0-9]*$/", $tag)) {
  220. continue;
  221. }
  222. // print "<!-- $id : $int_id : $tag -->";
  223. if ($tag != '') {
  224. $sth = $this->pdo->prepare("INSERT INTO ttrss_tags
  225. (post_int_id, owner_uid, tag_name)
  226. VALUES (?, ?, ?)");
  227. $sth->execute([$int_id, $_SESSION['uid'], $tag]);
  228. }
  229. array_push($tags_to_cache, $tag);
  230. }
  231. /* update tag cache */
  232. sort($tags_to_cache);
  233. $tags_str = join(",", $tags_to_cache);
  234. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries
  235. SET tag_cache = ? WHERE ref_id = ? AND owner_uid = ?");
  236. $sth->execute([$tags_str, $id, $_SESSION['uid']]);
  237. }
  238. $this->pdo->commit();
  239. $tags = Article::get_article_tags($id);
  240. $tags_str = $this->format_tags_string($tags, $id);
  241. $tags_str_full = join(", ", $tags);
  242. if (!$tags_str_full) $tags_str_full = __("no tags");
  243. print json_encode(array("id" => (int)$id,
  244. "content" => $tags_str, "content_full" => $tags_str_full));
  245. }
  246. function completeTags() {
  247. $search = clean($_REQUEST["search"]);
  248. $sth = $this->pdo->prepare("SELECT DISTINCT tag_name FROM ttrss_tags
  249. WHERE owner_uid = ? AND
  250. tag_name LIKE ? ORDER BY tag_name
  251. LIMIT 10");
  252. $sth->execute([$_SESSION['uid'], "$search%"]);
  253. print "<ul>";
  254. while ($line = $sth->fetch()) {
  255. print "<li>" . $line["tag_name"] . "</li>";
  256. }
  257. print "</ul>";
  258. }
  259. function assigntolabel() {
  260. return $this->labelops(true);
  261. }
  262. function removefromlabel() {
  263. return $this->labelops(false);
  264. }
  265. private function labelops($assign) {
  266. $reply = array();
  267. $ids = explode(",", clean($_REQUEST["ids"]));
  268. $label_id = clean($_REQUEST["lid"]);
  269. $label = db_escape_string(Labels::find_caption($label_id,
  270. $_SESSION["uid"]));
  271. $reply["info-for-headlines"] = array();
  272. if ($label) {
  273. foreach ($ids as $id) {
  274. if ($assign)
  275. Labels::add_article($id, $label, $_SESSION["uid"]);
  276. else
  277. Labels::remove_article($id, $label, $_SESSION["uid"]);
  278. $labels = $this->get_article_labels($id, $_SESSION["uid"]);
  279. array_push($reply["info-for-headlines"],
  280. array("id" => $id, "labels" => $this->format_article_labels($labels)));
  281. }
  282. }
  283. $reply["message"] = "UPDATE_COUNTERS";
  284. print json_encode($reply);
  285. }
  286. function getArticleFeed($id) {
  287. $sth = $this->pdo->prepare("SELECT feed_id FROM ttrss_user_entries
  288. WHERE ref_id = ? AND owner_uid = ?");
  289. $sth->execute([$id, $_SESSION['uid']]);
  290. if ($row = $sth->fetch()) {
  291. return $row["feed_id"];
  292. } else {
  293. return 0;
  294. }
  295. }
  296. static function format_article_enclosures($id, $always_display_enclosures,
  297. $article_content, $hide_images = false) {
  298. $result = Article::get_article_enclosures($id);
  299. $rv = '';
  300. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FORMAT_ENCLOSURES) as $plugin) {
  301. $retval = $plugin->hook_format_enclosures($rv, $result, $id, $always_display_enclosures, $article_content, $hide_images);
  302. if (is_array($retval)) {
  303. $rv = $retval[0];
  304. $result = $retval[1];
  305. } else {
  306. $rv = $retval;
  307. }
  308. }
  309. unset($retval); // Unset to prevent breaking render if there are no HOOK_RENDER_ENCLOSURE hooks below.
  310. if ($rv === '' && !empty($result)) {
  311. $entries_html = array();
  312. $entries = array();
  313. $entries_inline = array();
  314. foreach ($result as $line) {
  315. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ENCLOSURE_ENTRY) as $plugin) {
  316. $line = $plugin->hook_enclosure_entry($line);
  317. }
  318. $url = $line["content_url"];
  319. $ctype = $line["content_type"];
  320. $title = $line["title"];
  321. $width = $line["width"];
  322. $height = $line["height"];
  323. if (!$ctype) $ctype = __("unknown type");
  324. //$filename = substr($url, strrpos($url, "/")+1);
  325. $filename = basename($url);
  326. $player = format_inline_player($url, $ctype);
  327. if ($player) array_push($entries_inline, $player);
  328. # $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\" rel=\"noopener noreferrer\">" .
  329. # $filename . " (" . $ctype . ")" . "</a>";
  330. $entry = "<div onclick=\"openUrlPopup('".htmlspecialchars($url)."')\"
  331. dojoType=\"dijit.MenuItem\">$filename ($ctype)</div>";
  332. array_push($entries_html, $entry);
  333. $entry = array();
  334. $entry["type"] = $ctype;
  335. $entry["filename"] = $filename;
  336. $entry["url"] = $url;
  337. $entry["title"] = $title;
  338. $entry["width"] = $width;
  339. $entry["height"] = $height;
  340. array_push($entries, $entry);
  341. }
  342. if ($_SESSION['uid'] && !get_pref("STRIP_IMAGES") && !$_SESSION["bw_limit"]) {
  343. if ($always_display_enclosures ||
  344. !preg_match("/<img/i", $article_content)) {
  345. foreach ($entries as $entry) {
  346. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ENCLOSURE) as $plugin)
  347. $retval = $plugin->hook_render_enclosure($entry, $hide_images);
  348. if ($retval) {
  349. $rv .= $retval;
  350. } else {
  351. if (preg_match("/image/", $entry["type"])) {
  352. if (!$hide_images) {
  353. $encsize = '';
  354. if ($entry['height'] > 0)
  355. $encsize .= ' height="' . intval($entry['height']) . '"';
  356. if ($entry['width'] > 0)
  357. $encsize .= ' width="' . intval($entry['width']) . '"';
  358. $rv .= "<p><img
  359. alt=\"".htmlspecialchars($entry["filename"])."\"
  360. src=\"" .htmlspecialchars($entry["url"]) . "\"
  361. " . $encsize . " /></p>";
  362. } else {
  363. $rv .= "<p><a target=\"_blank\" rel=\"noopener noreferrer\"
  364. href=\"".htmlspecialchars($entry["url"])."\"
  365. >" .htmlspecialchars($entry["url"]) . "</a></p>";
  366. }
  367. if ($entry['title']) {
  368. $rv.= "<div class=\"enclosure_title\">${entry['title']}</div>";
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. if (count($entries_inline) > 0) {
  376. $rv .= "<hr clear='both'/>";
  377. foreach ($entries_inline as $entry) { $rv .= $entry; };
  378. $rv .= "<hr clear='both'/>";
  379. }
  380. $rv .= "<div class=\"attachments\" dojoType=\"dijit.form.DropDownButton\">".
  381. "<span>" . __('Attachments')."</span>";
  382. $rv .= "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  383. foreach ($entries as $entry) {
  384. if ($entry["title"])
  385. $title = " &mdash; " . truncate_string($entry["title"], 30);
  386. else
  387. $title = "";
  388. if ($entry["filename"])
  389. $filename = truncate_middle(htmlspecialchars($entry["filename"]), 60);
  390. else
  391. $filename = "";
  392. $rv .= "<div onclick='openUrlPopup(\"".htmlspecialchars($entry["url"])."\")'
  393. dojoType=\"dijit.MenuItem\">".$filename . $title."</div>";
  394. };
  395. $rv .= "</div>";
  396. $rv .= "</div>";
  397. }
  398. return $rv;
  399. }
  400. static function format_article($id, $mark_as_read = true, $zoom_mode = false, $owner_uid = false) {
  401. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  402. $rv = array();
  403. $rv['id'] = $id;
  404. /* we can figure out feed_id from article id anyway, why do we
  405. * pass feed_id here? let's ignore the argument :(*/
  406. $pdo = Db::pdo();
  407. $sth = $pdo->prepare("SELECT feed_id FROM ttrss_user_entries
  408. WHERE ref_id = ?");
  409. $sth->execute([$id]);
  410. $row = $sth->fetch();
  411. $feed_id = (int) $row["feed_id"];
  412. $rv['feed_id'] = $feed_id;
  413. //if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
  414. if ($mark_as_read) {
  415. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  416. SET unread = false,last_read = NOW()
  417. WHERE ref_id = ? AND owner_uid = ?");
  418. $sth->execute([$id, $owner_uid]);
  419. CCache::update($feed_id, $owner_uid);
  420. }
  421. $sth = $pdo->prepare("SELECT id,title,link,content,feed_id,comments,int_id,lang,
  422. ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
  423. (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) as site_url,
  424. (SELECT title FROM ttrss_feeds WHERE id = feed_id) as feed_title,
  425. (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) as hide_images,
  426. (SELECT always_display_enclosures FROM ttrss_feeds WHERE id = feed_id) as always_display_enclosures,
  427. num_comments,
  428. tag_cache,
  429. author,
  430. guid,
  431. orig_feed_id,
  432. note
  433. FROM ttrss_entries,ttrss_user_entries
  434. WHERE id = ? AND ref_id = id AND owner_uid = ?");
  435. $sth->execute([$id, $owner_uid]);
  436. if ($line = $sth->fetch()) {
  437. $line["tags"] = Article::get_article_tags($id, $owner_uid, $line["tag_cache"]);
  438. unset($line["tag_cache"]);
  439. $line["content"] = sanitize($line["content"],
  440. $line['hide_images'],
  441. $owner_uid, $line["site_url"], false, $line["id"]);
  442. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE) as $p) {
  443. $line = $p->hook_render_article($line);
  444. }
  445. $num_comments = (int) $line["num_comments"];
  446. $entry_comments = "";
  447. if ($num_comments > 0) {
  448. if ($line["comments"]) {
  449. $comments_url = htmlspecialchars($line["comments"]);
  450. } else {
  451. $comments_url = htmlspecialchars($line["link"]);
  452. }
  453. $entry_comments = "<a class=\"postComments\"
  454. target='_blank' rel=\"noopener noreferrer\" href=\"$comments_url\">$num_comments ".
  455. _ngettext("comment", "comments", $num_comments)."</a>";
  456. } else {
  457. if ($line["comments"] && $line["link"] != $line["comments"]) {
  458. $entry_comments = "<a class=\"postComments\" target='_blank' rel=\"noopener noreferrer\" href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
  459. }
  460. }
  461. $enclosures = self::get_article_enclosures($line["id"]);
  462. if ($zoom_mode) {
  463. header("Content-Type: text/html");
  464. $rv['content'] .= "<!DOCTYPE html>
  465. <html><head>
  466. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
  467. <title>".$line["title"]."</title>".
  468. stylesheet_tag("css/default.css")."
  469. <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
  470. <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">";
  471. $rv['content'] .= "<meta property=\"og:title\" content=\"".htmlspecialchars($line["title"])."\"/>\n";
  472. $rv['content'] .= "<meta property=\"og:site_name\" content=\"".htmlspecialchars($line["feed_title"])."\"/>\n";
  473. $rv['content'] .= "<meta property=\"og:description\" content=\"".
  474. htmlspecialchars(truncate_string(strip_tags($line["content"]), 500, "..."))."\"/>\n";
  475. $rv['content'] .= "</head>";
  476. $og_image = false;
  477. foreach ($enclosures as $enc) {
  478. if (strpos($enc["content_type"], "image/") !== FALSE) {
  479. $og_image = $enc["content_url"];
  480. break;
  481. }
  482. }
  483. if (!$og_image) {
  484. $tmpdoc = new DOMDocument();
  485. if (@$tmpdoc->loadHTML(mb_substr($line["content"], 0, 131070))) {
  486. $tmpxpath = new DOMXPath($tmpdoc);
  487. $first_img = $tmpxpath->query("//img")->item(0);
  488. if ($first_img) {
  489. $og_image = $first_img->getAttribute("src");
  490. }
  491. }
  492. }
  493. if ($og_image) {
  494. $rv['content'] .= "<meta property=\"og:image\" content=\"" . htmlspecialchars($og_image) . "\"/>";
  495. }
  496. $rv['content'] .= "<body class=\"claro ttrss_utility ttrss_zoom\">";
  497. }
  498. $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">";
  499. $rv['content'] .= "<div class=\"postHeader\" id=\"POSTHDR-$id\">";
  500. $entry_author = $line["author"];
  501. if ($entry_author) {
  502. $entry_author = __(" - ") . $entry_author;
  503. }
  504. $parsed_updated = make_local_datetime($line["updated"], true,
  505. $owner_uid, true);
  506. if (!$zoom_mode)
  507. $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>";
  508. if ($line["link"]) {
  509. $rv['content'] .= "<div class='postTitle'><a target='_blank' rel='noopener noreferrer'
  510. title=\"".htmlspecialchars($line['title'])."\"
  511. href=\"" .
  512. htmlspecialchars($line["link"]) . "\">" .
  513. $line["title"] . "</a>" .
  514. "<span class='author'>$entry_author</span></div>";
  515. } else {
  516. $rv['content'] .= "<div class='postTitle'>" . $line["title"] . "$entry_author</div>";
  517. }
  518. if ($zoom_mode) {
  519. $feed_title = htmlspecialchars($line["feed_title"]);
  520. $rv['content'] .= "<div class=\"postFeedTitle\">$feed_title</div>";
  521. $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>";
  522. }
  523. $tags_str = Article::format_tags_string($line["tags"], $id);
  524. $tags_str_full = join(", ", $line["tags"]);
  525. if (!$tags_str_full) $tags_str_full = __("no tags");
  526. if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
  527. $rv['content'] .= "<div class='postTags' style='float : right'>
  528. <img src='images/tag.png'
  529. class='tagsPic' alt='Tags' title='Tags'>&nbsp;";
  530. if (!$zoom_mode) {
  531. $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>
  532. <a title=\"".__('Edit tags for this article')."\"
  533. href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>";
  534. $rv['content'] .= "<div dojoType=\"dijit.Tooltip\"
  535. id=\"ATSTRTIP-$id\" connectId=\"ATSTR-$id\"
  536. position=\"below\">$tags_str_full</div>";
  537. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_BUTTON) as $p) {
  538. $rv['content'] .= $p->hook_article_button($line);
  539. }
  540. } else {
  541. $tags_str = strip_tags($tags_str);
  542. $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>";
  543. }
  544. $rv['content'] .= "</div>";
  545. $rv['content'] .= "<div clear='both'>";
  546. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_LEFT_BUTTON) as $p) {
  547. $rv['content'] .= $p->hook_article_left_button($line);
  548. }
  549. $rv['content'] .= "$entry_comments</div>";
  550. if ($line["orig_feed_id"]) {
  551. $of_sth = $pdo->prepare("SELECT * FROM ttrss_archived_feeds
  552. WHERE id = ? AND owner_uid = ?");
  553. $of_sth->execute([$line["orig_feed_id"], $owner_uid]);
  554. if ($tmp_line = $of_sth->fetch()) {
  555. $rv['content'] .= "<div clear='both'>";
  556. $rv['content'] .= __("Originally from:");
  557. $rv['content'] .= "&nbsp;";
  558. $rv['content'] .= "<a target='_blank' rel='noopener noreferrer'
  559. href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
  560. $tmp_line['title'] . "</a>";
  561. $rv['content'] .= "&nbsp;";
  562. $rv['content'] .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
  563. $rv['content'] .= "<img title='".__('Feed URL')."' class='tinyFeedIcon' src='images/pub_set.png'></a>";
  564. $rv['content'] .= "</div>";
  565. }
  566. }
  567. $rv['content'] .= "</div>";
  568. $rv['content'] .= "<div id=\"POSTNOTE-$id\">";
  569. if ($line['note']) {
  570. $rv['content'] .= Article::format_article_note($id, $line['note'], !$zoom_mode);
  571. }
  572. $rv['content'] .= "</div>";
  573. if (!$line['lang']) $line['lang'] = 'en';
  574. $rv['content'] .= "<div class=\"postContent\" lang=\"".$line['lang']."\">";
  575. $rv['content'] .= $line["content"];
  576. if (!$zoom_mode) {
  577. $rv['content'] .= Article::format_article_enclosures($id,
  578. $line["always_display_enclosures"],
  579. $line["content"],
  580. $line["hide_images"]);
  581. }
  582. $rv['content'] .= "</div>";
  583. $rv['content'] .= "</div>";
  584. }
  585. if ($zoom_mode) {
  586. $rv['content'] .= "
  587. <div class='footer'>
  588. <button onclick=\"return window.close()\">".
  589. __("Close this window")."</button></div>";
  590. $rv['content'] .= "</body></html>";
  591. }
  592. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FORMAT_ARTICLE) as $p) {
  593. $rv['content'] = $p->hook_format_article($rv['content'], $line, $zoom_mode);
  594. }
  595. return $rv;
  596. }
  597. static function get_article_tags($id, $owner_uid = 0, $tag_cache = false) {
  598. $a_id = $id;
  599. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  600. $pdo = Db::pdo();
  601. $sth = $pdo->prepare("SELECT DISTINCT tag_name,
  602. owner_uid as owner FROM ttrss_tags
  603. WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
  604. ref_id = ? AND owner_uid = ? LIMIT 1) ORDER BY tag_name");
  605. $tags = array();
  606. /* check cache first */
  607. if ($tag_cache === false) {
  608. $csth = $pdo->prepare("SELECT tag_cache FROM ttrss_user_entries
  609. WHERE ref_id = ? AND owner_uid = ?");
  610. $csth->execute([$id, $owner_uid]);
  611. if ($row = $csth->fetch()) $tag_cache = $row["tag_cache"];
  612. }
  613. if ($tag_cache) {
  614. $tags = explode(",", $tag_cache);
  615. } else {
  616. /* do it the hard way */
  617. $sth->execute([$a_id, $owner_uid]);
  618. while ($tmp_line = $sth->fetch()) {
  619. array_push($tags, $tmp_line["tag_name"]);
  620. }
  621. /* update the cache */
  622. $tags_str = join(",", $tags);
  623. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  624. SET tag_cache = ? WHERE ref_id = ?
  625. AND owner_uid = ?");
  626. $sth->execute([$tags_str, $id, $owner_uid]);
  627. }
  628. return $tags;
  629. }
  630. static function format_tags_string($tags) {
  631. if (!is_array($tags) || count($tags) == 0) {
  632. return __("no tags");
  633. } else {
  634. $maxtags = min(5, count($tags));
  635. $tags_str = "";
  636. for ($i = 0; $i < $maxtags; $i++) {
  637. $tags_str .= "<a class=\"tag\" href=\"#\" onclick=\"viewfeed({feed:'".$tags[$i]."'})\">" . $tags[$i] . "</a>, ";
  638. }
  639. $tags_str = mb_substr($tags_str, 0, mb_strlen($tags_str)-2);
  640. if (count($tags) > $maxtags)
  641. $tags_str .= ", &hellip;";
  642. return $tags_str;
  643. }
  644. }
  645. static function format_article_labels($labels) {
  646. if (!is_array($labels)) return '';
  647. $labels_str = "";
  648. foreach ($labels as $l) {
  649. $labels_str .= sprintf("<span class='hlLabelRef'
  650. style='color : %s; background-color : %s'>%s</span>",
  651. $l[2], $l[3], $l[1]);
  652. }
  653. return $labels_str;
  654. }
  655. static function format_article_note($id, $note, $allow_edit = true) {
  656. $str = "<div class='articleNote' onclick=\"editArticleNote($id)\">
  657. <div class='noteEdit' onclick=\"editArticleNote($id)\">".
  658. ($allow_edit ? __('(edit note)') : "")."</div>$note</div>";
  659. return $str;
  660. }
  661. static function get_article_enclosures($id) {
  662. $pdo = Db::pdo();
  663. $sth = $pdo->prepare("SELECT * FROM ttrss_enclosures
  664. WHERE post_id = ? AND content_url != ''");
  665. $sth->execute([$id]);
  666. $rv = array();
  667. while ($line = $sth->fetch()) {
  668. if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) {
  669. $line["content_url"] = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($line["content_url"]);
  670. }
  671. array_push($rv, $line);
  672. }
  673. return $rv;
  674. }
  675. static function purge_orphans($do_output = false) {
  676. // purge orphaned posts in main content table
  677. if (DB_TYPE == "mysql")
  678. $limit_qpart = "LIMIT 5000";
  679. else
  680. $limit_qpart = "";
  681. $pdo = Db::pdo();
  682. $res = $pdo->query("DELETE FROM ttrss_entries WHERE
  683. NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id) $limit_qpart");
  684. if ($do_output) {
  685. $rows = $res->rowCount();
  686. _debug("Purged $rows orphaned posts.");
  687. }
  688. }
  689. static function catchupArticlesById($ids, $cmode, $owner_uid = false) {
  690. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  691. $pdo = Db::pdo();
  692. $ids_qmarks = arr_qmarks($ids);
  693. if ($cmode == 0) {
  694. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  695. unread = false,last_read = NOW()
  696. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  697. } else if ($cmode == 1) {
  698. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  699. unread = true
  700. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  701. } else {
  702. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  703. unread = NOT unread,last_read = NOW()
  704. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  705. }
  706. $sth->execute(array_merge($ids, [$owner_uid]));
  707. /* update ccache */
  708. $sth = $pdo->prepare("SELECT DISTINCT feed_id FROM ttrss_user_entries
  709. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  710. $sth->execute(array_merge($ids, [$owner_uid]));
  711. while ($line = $sth->fetch()) {
  712. CCache::update($line["feed_id"], $owner_uid);
  713. }
  714. }
  715. static function getLastArticleId() {
  716. $pdo = DB::pdo();
  717. $sth = $pdo->prepare("SELECT ref_id AS id FROM ttrss_user_entries
  718. WHERE owner_uid = ? ORDER BY ref_id DESC LIMIT 1");
  719. $sth->execute([$_SESSION['uid']]);
  720. if ($row = $sth->fetch()) {
  721. return $row['id'];
  722. } else {
  723. return -1;
  724. }
  725. }
  726. static function get_article_labels($id, $owner_uid = false) {
  727. $rv = array();
  728. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  729. $pdo = Db::pdo();
  730. $sth = $pdo->prepare("SELECT label_cache FROM
  731. ttrss_user_entries WHERE ref_id = ? AND owner_uid = ?");
  732. $sth->execute([$id, $owner_uid]);
  733. if ($row = $sth->fetch()) {
  734. $label_cache = $row["label_cache"];
  735. if ($label_cache) {
  736. $tmp = json_decode($label_cache, true);
  737. if (!$tmp || $tmp["no-labels"] == 1)
  738. return $rv;
  739. else
  740. return $tmp;
  741. }
  742. }
  743. $sth = $pdo->prepare("SELECT DISTINCT label_id,caption,fg_color,bg_color
  744. FROM ttrss_labels2, ttrss_user_labels2
  745. WHERE id = label_id
  746. AND article_id = ?
  747. AND owner_uid = ?
  748. ORDER BY caption");
  749. $sth->execute([$id, $owner_uid]);
  750. while ($line = $sth->fetch()) {
  751. $rk = array(Labels::label_to_feed_id($line["label_id"]),
  752. $line["caption"], $line["fg_color"],
  753. $line["bg_color"]);
  754. array_push($rv, $rk);
  755. }
  756. if (count($rv) > 0)
  757. Labels::update_cache($owner_uid, $id, $rv);
  758. else
  759. Labels::update_cache($owner_uid, $id, array("no-labels" => 1));
  760. return $rv;
  761. }
  762. }