article.php 27 KB

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