article.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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_class" => get_score_class($score),
  189. "score_pic" => get_score_pic($score)));
  190. }
  191. function getScore() {
  192. $id = clean($_REQUEST['id']);
  193. $sth = $this->pdo->prepare("SELECT score FROM ttrss_user_entries WHERE ref_id = ? AND owner_uid = ?");
  194. $sth->execute([$id, $_SESSION['uid']]);
  195. $row = $sth->fetch();
  196. $score = $row['score'];
  197. print json_encode(array("id" => $id,
  198. "score" => (int)$score,
  199. "score_pic" => get_score_pic($score)));
  200. }
  201. function setArticleTags() {
  202. $id = clean($_REQUEST["id"]);
  203. $tags_str = clean($_REQUEST["tags_str"]);
  204. $tags = array_unique(trim_array(explode(",", $tags_str)));
  205. $this->pdo->beginTransaction();
  206. $sth = $this->pdo->prepare("SELECT int_id FROM ttrss_user_entries WHERE
  207. ref_id = ? AND owner_uid = ? LIMIT 1");
  208. $sth->execute([$id, $_SESSION['uid']]);
  209. if ($row = $sth->fetch()) {
  210. $tags_to_cache = array();
  211. $int_id = $row['int_id'];
  212. $sth = $this->pdo->prepare("DELETE FROM ttrss_tags WHERE
  213. post_int_id = ? AND owner_uid = ?");
  214. $sth->execute([$int_id, $_SESSION['uid']]);
  215. foreach ($tags as $tag) {
  216. $tag = sanitize_tag($tag);
  217. if (!tag_is_valid($tag)) {
  218. continue;
  219. }
  220. if (preg_match("/^[0-9]*$/", $tag)) {
  221. continue;
  222. }
  223. // print "<!-- $id : $int_id : $tag -->";
  224. if ($tag != '') {
  225. $sth = $this->pdo->prepare("INSERT INTO ttrss_tags
  226. (post_int_id, owner_uid, tag_name)
  227. VALUES (?, ?, ?)");
  228. $sth->execute([$int_id, $_SESSION['uid'], $tag]);
  229. }
  230. array_push($tags_to_cache, $tag);
  231. }
  232. /* update tag cache */
  233. sort($tags_to_cache);
  234. $tags_str = join(",", $tags_to_cache);
  235. $sth = $this->pdo->prepare("UPDATE ttrss_user_entries
  236. SET tag_cache = ? WHERE ref_id = ? AND owner_uid = ?");
  237. $sth->execute([$tags_str, $id, $_SESSION['uid']]);
  238. }
  239. $this->pdo->commit();
  240. $tags = Article::get_article_tags($id);
  241. $tags_str = $this->format_tags_string($tags, $id);
  242. $tags_str_full = join(", ", $tags);
  243. if (!$tags_str_full) $tags_str_full = __("no tags");
  244. print json_encode(array("id" => (int)$id,
  245. "content" => $tags_str, "content_full" => $tags_str_full));
  246. }
  247. function completeTags() {
  248. $search = clean($_REQUEST["search"]);
  249. $sth = $this->pdo->prepare("SELECT DISTINCT tag_name FROM ttrss_tags
  250. WHERE owner_uid = ? AND
  251. tag_name LIKE ? ORDER BY tag_name
  252. LIMIT 10");
  253. $sth->execute([$_SESSION['uid'], "$search%"]);
  254. print "<ul>";
  255. while ($line = $sth->fetch()) {
  256. print "<li>" . $line["tag_name"] . "</li>";
  257. }
  258. print "</ul>";
  259. }
  260. function assigntolabel() {
  261. return $this->labelops(true);
  262. }
  263. function removefromlabel() {
  264. return $this->labelops(false);
  265. }
  266. private function labelops($assign) {
  267. $reply = array();
  268. $ids = explode(",", clean($_REQUEST["ids"]));
  269. $label_id = clean($_REQUEST["lid"]);
  270. $label = Labels::find_caption($label_id, $_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=\"popupOpenUrl('".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='popupOpenUrl(\"".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. $line['content'] = rewrite_cached_urls($line['content']);
  446. $num_comments = (int) $line["num_comments"];
  447. $entry_comments = "";
  448. if ($num_comments > 0) {
  449. if ($line["comments"]) {
  450. $comments_url = htmlspecialchars($line["comments"]);
  451. } else {
  452. $comments_url = htmlspecialchars($line["link"]);
  453. }
  454. $entry_comments = "<a class=\"comments\"
  455. target='_blank' rel=\"noopener noreferrer\" href=\"$comments_url\">$num_comments ".
  456. _ngettext("comment", "comments", $num_comments)."</a>";
  457. } else {
  458. if ($line["comments"] && $line["link"] != $line["comments"]) {
  459. $entry_comments = "<a class=\"comments\" target='_blank' rel=\"noopener noreferrer\" href=\"".
  460. htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
  461. }
  462. }
  463. $enclosures = self::get_article_enclosures($line["id"]);
  464. if ($zoom_mode) {
  465. header("Content-Type: text/html");
  466. $rv['content'] .= "<!DOCTYPE html>
  467. <html><head>
  468. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
  469. <title>".$line["title"]."</title>".
  470. stylesheet_tag("css/default.css")."
  471. <link rel=\"shortcut icon\" type=\"image/png\" href=\"images/favicon.png\">
  472. <link rel=\"icon\" type=\"image/png\" sizes=\"72x72\" href=\"images/favicon-72px.png\">";
  473. $rv['content'] .= "<meta property=\"og:title\" content=\"".htmlspecialchars($line["title"])."\"/>\n";
  474. $rv['content'] .= "<meta property=\"og:site_name\" content=\"".htmlspecialchars($line["feed_title"])."\"/>\n";
  475. $rv['content'] .= "<meta property=\"og:description\" content=\"".
  476. htmlspecialchars(truncate_string(strip_tags($line["content"]), 500, "..."))."\"/>\n";
  477. $rv['content'] .= "</head>";
  478. $og_image = false;
  479. foreach ($enclosures as $enc) {
  480. if (strpos($enc["content_type"], "image/") !== FALSE) {
  481. $og_image = $enc["content_url"];
  482. break;
  483. }
  484. }
  485. if (!$og_image) {
  486. $tmpdoc = new DOMDocument();
  487. if (@$tmpdoc->loadHTML(mb_substr($line["content"], 0, 131070))) {
  488. $tmpxpath = new DOMXPath($tmpdoc);
  489. $first_img = $tmpxpath->query("//img")->item(0);
  490. if ($first_img) {
  491. $og_image = $first_img->getAttribute("src");
  492. }
  493. }
  494. }
  495. if ($og_image) {
  496. $rv['content'] .= "<meta property=\"og:image\" content=\"" . htmlspecialchars($og_image) . "\"/>";
  497. }
  498. $rv['content'] .= "<body class='flat ttrss_utility ttrss_zoom'>";
  499. }
  500. $rv['content'] .= "<div class='post post-$id'>";
  501. /* header */
  502. $rv['content'] .= "<div class='header'>";
  503. $rv['content'] .= "<div class='row'>"; # row
  504. //$entry_author = $line["author"] ? " - " . $line["author"] : "";
  505. $parsed_updated = make_local_datetime($line["updated"], true,
  506. $owner_uid, true);
  507. if ($line["link"]) {
  508. $rv['content'] .= "<div class='title'><a target='_blank' rel='noopener noreferrer'
  509. title=\"".htmlspecialchars($line['title'])."\"
  510. href=\"" .htmlspecialchars($line["link"]) . "\">" . $line["title"] . "</a></div>";
  511. } else {
  512. $rv['content'] .= "<div class='title'>" . $line["title"] . "</div>";
  513. }
  514. if (!$zoom_mode)
  515. $rv['content'] .= "<div class='date'>$parsed_updated<br/></div>";
  516. $rv['content'] .= "</div>"; # row
  517. $rv['content'] .= "<div class='row'>"; # row
  518. /* left buttons */
  519. $rv['content'] .= "<div class='buttons left'>";
  520. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_LEFT_BUTTON) as $p) {
  521. $rv['content'] .= $p->hook_article_left_button($line);
  522. }
  523. $rv['content'] .= "</div>";
  524. /* comments */
  525. $rv['content'] .= "<div class='comments'>$entry_comments</div>";
  526. $rv['content'] .= "<div class='author'>".$line['author']."</div>";
  527. /* tags */
  528. $tags_str = Article::format_tags_string($line["tags"], $id);
  529. $tags_str_full = join(", ", $line["tags"]);
  530. if (!$tags_str_full) $tags_str_full = __("no tags");
  531. $rv['content'] .= "<i class='material-icons'>label_outline</i><div>";
  532. if (!$zoom_mode) {
  533. $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>
  534. <a title=\"".__('Edit tags for this article')."\"
  535. href=\"#\" onclick=\"Article.editTags($id)\">(+)</a>";
  536. $rv['content'] .= "<div dojoType=\"dijit.Tooltip\"
  537. id=\"ATSTRTIP-$id\" connectId=\"ATSTR-$id\"
  538. position=\"below\">$tags_str_full</div>";
  539. } else {
  540. $tags_str = strip_tags($tags_str);
  541. $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>";
  542. }
  543. $rv['content'] .= "</div>";
  544. /* buttons */
  545. $rv['content'] .= "<div class='buttons right'>";
  546. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_BUTTON) as $p) {
  547. $rv['content'] .= $p->hook_article_button($line);
  548. }
  549. $rv['content'] .= "</div>";
  550. $rv['content'] .= "</div>"; # row
  551. $rv['content'] .= "</div>"; # header
  552. /* note */
  553. $rv['content'] .= "<div id=\"POSTNOTE-$id\">";
  554. if ($line['note']) {
  555. $rv['content'] .= Article::format_article_note($id, $line['note'], !$zoom_mode);
  556. }
  557. $rv['content'] .= "</div>";
  558. /* content */
  559. $lang = $line['lang'] ? $line['lang'] : "en";
  560. $rv['content'] .= "<div class=\"content\" lang=\"$lang\">";
  561. /* originally from */
  562. if (!$zoom_mode && $line["orig_feed_id"]) {
  563. $of_sth = $pdo->prepare("SELECT * FROM ttrss_archived_feeds
  564. WHERE id = ? AND owner_uid = ?");
  565. $of_sth->execute([$line["orig_feed_id"], $owner_uid]);
  566. if ($tmp_line = $of_sth->fetch()) {
  567. $rv['content'] .= "<div clear='both'>";
  568. $rv['content'] .= __("Originally from:");
  569. $rv['content'] .= "&nbsp;";
  570. $rv['content'] .= "<a target='_blank' rel='noopener noreferrer'
  571. href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
  572. $tmp_line['title'] . "</a>";
  573. $rv['content'] .= "&nbsp;";
  574. $rv['content'] .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
  575. $rv['content'] .= "</div>";
  576. }
  577. }
  578. /* content body */
  579. $rv['content'] .= $line["content"];
  580. if (!$zoom_mode) {
  581. $rv['content'] .= Article::format_article_enclosures($id,
  582. $line["always_display_enclosures"],
  583. $line["content"],
  584. $line["hide_images"]);
  585. }
  586. $rv['content'] .= "</div>"; # content
  587. $rv['content'] .= "</div>"; # post
  588. }
  589. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_FORMAT_ARTICLE) as $p) {
  590. $rv['content'] = $p->hook_format_article($rv['content'], $line, $zoom_mode);
  591. }
  592. return $rv;
  593. }
  594. static function get_article_tags($id, $owner_uid = 0, $tag_cache = false) {
  595. $a_id = $id;
  596. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  597. $pdo = Db::pdo();
  598. $sth = $pdo->prepare("SELECT DISTINCT tag_name,
  599. owner_uid as owner FROM ttrss_tags
  600. WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
  601. ref_id = ? AND owner_uid = ? LIMIT 1) ORDER BY tag_name");
  602. $tags = array();
  603. /* check cache first */
  604. if ($tag_cache === false) {
  605. $csth = $pdo->prepare("SELECT tag_cache FROM ttrss_user_entries
  606. WHERE ref_id = ? AND owner_uid = ?");
  607. $csth->execute([$id, $owner_uid]);
  608. if ($row = $csth->fetch()) $tag_cache = $row["tag_cache"];
  609. }
  610. if ($tag_cache) {
  611. $tags = explode(",", $tag_cache);
  612. } else {
  613. /* do it the hard way */
  614. $sth->execute([$a_id, $owner_uid]);
  615. while ($tmp_line = $sth->fetch()) {
  616. array_push($tags, $tmp_line["tag_name"]);
  617. }
  618. /* update the cache */
  619. $tags_str = join(",", $tags);
  620. $sth = $pdo->prepare("UPDATE ttrss_user_entries
  621. SET tag_cache = ? WHERE ref_id = ?
  622. AND owner_uid = ?");
  623. $sth->execute([$tags_str, $id, $owner_uid]);
  624. }
  625. return $tags;
  626. }
  627. static function format_tags_string($tags) {
  628. if (!is_array($tags) || count($tags) == 0) {
  629. return __("no tags");
  630. } else {
  631. $maxtags = min(5, count($tags));
  632. $tags_str = "";
  633. for ($i = 0; $i < $maxtags; $i++) {
  634. $tags_str .= "<a class=\"tag\" href=\"#\" onclick=\"Feeds.open({feed:'".$tags[$i]."'})\">" . $tags[$i] . "</a>, ";
  635. }
  636. $tags_str = mb_substr($tags_str, 0, mb_strlen($tags_str)-2);
  637. if (count($tags) > $maxtags)
  638. $tags_str .= ", &hellip;";
  639. return $tags_str;
  640. }
  641. }
  642. static function format_article_labels($labels) {
  643. if (!is_array($labels)) return '';
  644. $labels_str = "";
  645. foreach ($labels as $l) {
  646. $labels_str .= sprintf("<span class='hlLabelRef'
  647. style='color : %s; background-color : %s'>%s</span>",
  648. $l[2], $l[3], $l[1]);
  649. }
  650. return $labels_str;
  651. }
  652. static function format_article_note($id, $note, $allow_edit = true) {
  653. $str = "<div class='articleNote' onclick=\"Plugins.Note.edit($id)\">
  654. <div class='noteEdit' onclick=\"Plugins.Note.edit($id)\">".
  655. ($allow_edit ? __('(edit note)') : "")."</div>$note</div>";
  656. return $str;
  657. }
  658. static function get_article_enclosures($id) {
  659. $pdo = Db::pdo();
  660. $sth = $pdo->prepare("SELECT * FROM ttrss_enclosures
  661. WHERE post_id = ? AND content_url != ''");
  662. $sth->execute([$id]);
  663. $rv = array();
  664. while ($line = $sth->fetch()) {
  665. if (file_exists(CACHE_DIR . '/images/' . sha1($line["content_url"]))) {
  666. $line["content_url"] = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($line["content_url"]);
  667. }
  668. array_push($rv, $line);
  669. }
  670. return $rv;
  671. }
  672. static function purge_orphans() {
  673. // purge orphaned posts in main content table
  674. if (DB_TYPE == "mysql")
  675. $limit_qpart = "LIMIT 5000";
  676. else
  677. $limit_qpart = "";
  678. $pdo = Db::pdo();
  679. $res = $pdo->query("DELETE FROM ttrss_entries WHERE
  680. NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id) $limit_qpart");
  681. if (Debug::enabled()) {
  682. $rows = $res->rowCount();
  683. Debug::log("Purged $rows orphaned posts.");
  684. }
  685. }
  686. static function catchupArticlesById($ids, $cmode, $owner_uid = false) {
  687. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  688. $pdo = Db::pdo();
  689. $ids_qmarks = arr_qmarks($ids);
  690. if ($cmode == 0) {
  691. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  692. unread = false,last_read = NOW()
  693. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  694. } else if ($cmode == 1) {
  695. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  696. unread = true
  697. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  698. } else {
  699. $sth = $pdo->prepare("UPDATE ttrss_user_entries SET
  700. unread = NOT unread,last_read = NOW()
  701. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  702. }
  703. $sth->execute(array_merge($ids, [$owner_uid]));
  704. /* update ccache */
  705. $sth = $pdo->prepare("SELECT DISTINCT feed_id FROM ttrss_user_entries
  706. WHERE ref_id IN ($ids_qmarks) AND owner_uid = ?");
  707. $sth->execute(array_merge($ids, [$owner_uid]));
  708. while ($line = $sth->fetch()) {
  709. CCache::update($line["feed_id"], $owner_uid);
  710. }
  711. }
  712. static function getLastArticleId() {
  713. $pdo = DB::pdo();
  714. $sth = $pdo->prepare("SELECT ref_id AS id FROM ttrss_user_entries
  715. WHERE owner_uid = ? ORDER BY ref_id DESC LIMIT 1");
  716. $sth->execute([$_SESSION['uid']]);
  717. if ($row = $sth->fetch()) {
  718. return $row['id'];
  719. } else {
  720. return -1;
  721. }
  722. }
  723. static function get_article_labels($id, $owner_uid = false) {
  724. $rv = array();
  725. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  726. $pdo = Db::pdo();
  727. $sth = $pdo->prepare("SELECT label_cache FROM
  728. ttrss_user_entries WHERE ref_id = ? AND owner_uid = ?");
  729. $sth->execute([$id, $owner_uid]);
  730. if ($row = $sth->fetch()) {
  731. $label_cache = $row["label_cache"];
  732. if ($label_cache) {
  733. $tmp = json_decode($label_cache, true);
  734. if (!$tmp || $tmp["no-labels"] == 1)
  735. return $rv;
  736. else
  737. return $tmp;
  738. }
  739. }
  740. $sth = $pdo->prepare("SELECT DISTINCT label_id,caption,fg_color,bg_color
  741. FROM ttrss_labels2, ttrss_user_labels2
  742. WHERE id = label_id
  743. AND article_id = ?
  744. AND owner_uid = ?
  745. ORDER BY caption");
  746. $sth->execute([$id, $owner_uid]);
  747. while ($line = $sth->fetch()) {
  748. $rk = array(Labels::label_to_feed_id($line["label_id"]),
  749. $line["caption"], $line["fg_color"],
  750. $line["bg_color"]);
  751. array_push($rv, $rk);
  752. }
  753. if (count($rv) > 0)
  754. Labels::update_cache($owner_uid, $id, $rv);
  755. else
  756. Labels::update_cache($owner_uid, $id, array("no-labels" => 1));
  757. return $rv;
  758. }
  759. }