article.php 27 KB

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