article.php 27 KB

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