article.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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, format_article($id, false));
  30. } else if ($mode == "zoom") {
  31. array_push($articles, 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 = 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, 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 = 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. label_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. label_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 = 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 = get_article_tags($id);
  221. $tags_str = 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(label_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. label_add_article($id, $label, $_SESSION["uid"]);
  256. else
  257. label_remove_article($id, $label, $_SESSION["uid"]);
  258. $labels = get_article_labels($id, $_SESSION["uid"]);
  259. array_push($reply["info-for-headlines"],
  260. array("id" => $id, "labels" => format_article_labels($labels, $id)));
  261. }
  262. }
  263. $reply["message"] = "UPDATE_COUNTERS";
  264. print json_encode($reply);
  265. }
  266. }