article.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 ($_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);
  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. $content_hash = sha1($content);
  72. if ($labels_str != "") {
  73. $labels = explode(",", $labels_str);
  74. } else {
  75. $labels = array();
  76. }
  77. $rc = false;
  78. if (!$title) $title = $url;
  79. if (!$title && !$url) return false;
  80. if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false;
  81. db_query("BEGIN");
  82. // only check for our user data here, others might have shared this with different content etc
  83. $result = db_query("SELECT id FROM ttrss_entries, ttrss_user_entries WHERE
  84. guid = '$guid' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1");
  85. if (db_num_rows($result) != 0) {
  86. $ref_id = db_fetch_result($result, 0, "id");
  87. $result = db_query("SELECT int_id FROM ttrss_user_entries WHERE
  88. ref_id = '$ref_id' AND owner_uid = '$owner_uid' LIMIT 1");
  89. if (db_num_rows($result) != 0) {
  90. $int_id = db_fetch_result($result, 0, "int_id");
  91. db_query("UPDATE ttrss_entries SET
  92. content = '$content', content_hash = '$content_hash' WHERE id = '$ref_id'");
  93. db_query("UPDATE ttrss_user_entries SET published = true,
  94. last_published = NOW() WHERE
  95. int_id = '$int_id' AND owner_uid = '$owner_uid'");
  96. } else {
  97. db_query("INSERT INTO ttrss_user_entries
  98. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
  99. last_read, note, unread, last_published)
  100. VALUES
  101. ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())");
  102. }
  103. if (count($labels) != 0) {
  104. foreach ($labels as $label) {
  105. label_add_article($ref_id, trim($label), $owner_uid);
  106. }
  107. }
  108. $rc = true;
  109. } else {
  110. $result = db_query("INSERT INTO ttrss_entries
  111. (title, guid, link, updated, content, content_hash, date_entered, date_updated)
  112. VALUES
  113. ('$title', '$guid', '$url', NOW(), '$content', '$content_hash', NOW(), NOW())");
  114. $result = db_query("SELECT id FROM ttrss_entries WHERE guid = '$guid'");
  115. if (db_num_rows($result) != 0) {
  116. $ref_id = db_fetch_result($result, 0, "id");
  117. db_query("INSERT INTO ttrss_user_entries
  118. (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache,
  119. last_read, note, unread, last_published)
  120. VALUES
  121. ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())");
  122. if (count($labels) != 0) {
  123. foreach ($labels as $label) {
  124. label_add_article($ref_id, trim($label), $owner_uid);
  125. }
  126. }
  127. $rc = true;
  128. }
  129. }
  130. db_query("COMMIT");
  131. return $rc;
  132. }
  133. function editArticleTags() {
  134. print __("Tags for this article (separated by commas):")."<br>";
  135. $param = $this->dbh->escape_string($_REQUEST['param']);
  136. $tags = get_article_tags($this->dbh->escape_string($param));
  137. $tags_str = join(", ", $tags);
  138. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$param\">";
  139. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"article\">";
  140. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"setArticleTags\">";
  141. print "<table width='100%'><tr><td>";
  142. print "<textarea dojoType=\"dijit.form.SimpleTextarea\" rows='4'
  143. style='font-size : 12px; width : 100%' id=\"tags_str\"
  144. name='tags_str'>$tags_str</textarea>
  145. <div class=\"autocomplete\" id=\"tags_choices\"
  146. style=\"display:none\"></div>";
  147. print "</td></tr></table>";
  148. print "<div class='dlgButtons'>";
  149. print "<button dojoType=\"dijit.form.Button\"
  150. onclick=\"dijit.byId('editTagsDlg').execute()\">".__('Save')."</button> ";
  151. print "<button dojoType=\"dijit.form.Button\"
  152. onclick=\"dijit.byId('editTagsDlg').hide()\">".__('Cancel')."</button>";
  153. print "</div>";
  154. }
  155. function setScore() {
  156. $ids = $this->dbh->escape_string($_REQUEST['id']);
  157. $score = (int)$this->dbh->escape_string($_REQUEST['score']);
  158. $this->dbh->query("UPDATE ttrss_user_entries SET
  159. score = '$score' WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]);
  160. print json_encode(array("id" => $ids,
  161. "score" => (int)$score,
  162. "score_pic" => get_score_pic($score)));
  163. }
  164. function getScore() {
  165. $id = $this->dbh->escape_string($_REQUEST['id']);
  166. $result = $this->dbh->query("SELECT score FROM ttrss_user_entries WHERE ref_id = $id AND owner_uid = " . $_SESSION["uid"]);
  167. $score = $this->dbh->fetch_result($result, 0, "score");
  168. print json_encode(array("id" => $id,
  169. "score" => (int)$score,
  170. "score_pic" => get_score_pic($score)));
  171. }
  172. function setArticleTags() {
  173. $id = $this->dbh->escape_string($_REQUEST["id"]);
  174. $tags_str = $this->dbh->escape_string($_REQUEST["tags_str"]);
  175. $tags = array_unique(trim_array(explode(",", $tags_str)));
  176. $this->dbh->query("BEGIN");
  177. $result = $this->dbh->query("SELECT int_id FROM ttrss_user_entries WHERE
  178. ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1");
  179. if ($this->dbh->num_rows($result) == 1) {
  180. $tags_to_cache = array();
  181. $int_id = $this->dbh->fetch_result($result, 0, "int_id");
  182. $this->dbh->query("DELETE FROM ttrss_tags WHERE
  183. post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'");
  184. foreach ($tags as $tag) {
  185. $tag = sanitize_tag($tag);
  186. if (!tag_is_valid($tag)) {
  187. continue;
  188. }
  189. if (preg_match("/^[0-9]*$/", $tag)) {
  190. continue;
  191. }
  192. // print "<!-- $id : $int_id : $tag -->";
  193. if ($tag != '') {
  194. $this->dbh->query("INSERT INTO ttrss_tags
  195. (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')");
  196. }
  197. array_push($tags_to_cache, $tag);
  198. }
  199. /* update tag cache */
  200. sort($tags_to_cache);
  201. $tags_str = join(",", $tags_to_cache);
  202. $this->dbh->query("UPDATE ttrss_user_entries
  203. SET tag_cache = '$tags_str' WHERE ref_id = '$id'
  204. AND owner_uid = " . $_SESSION["uid"]);
  205. }
  206. $this->dbh->query("COMMIT");
  207. $tags = get_article_tags($id);
  208. $tags_str = format_tags_string($tags, $id);
  209. $tags_str_full = join(", ", $tags);
  210. if (!$tags_str_full) $tags_str_full = __("no tags");
  211. print json_encode(array("id" => (int)$id,
  212. "content" => $tags_str, "content_full" => $tags_str_full));
  213. }
  214. function completeTags() {
  215. $search = $this->dbh->escape_string($_REQUEST["search"]);
  216. $result = $this->dbh->query("SELECT DISTINCT tag_name FROM ttrss_tags
  217. WHERE owner_uid = '".$_SESSION["uid"]."' AND
  218. tag_name LIKE '$search%' ORDER BY tag_name
  219. LIMIT 10");
  220. print "<ul>";
  221. while ($line = $this->dbh->fetch_assoc($result)) {
  222. print "<li>" . $line["tag_name"] . "</li>";
  223. }
  224. print "</ul>";
  225. }
  226. function assigntolabel() {
  227. return $this->labelops(true);
  228. }
  229. function removefromlabel() {
  230. return $this->labelops(false);
  231. }
  232. private function labelops($assign) {
  233. $reply = array();
  234. $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
  235. $label_id = $this->dbh->escape_string($_REQUEST["lid"]);
  236. $label = $this->dbh->escape_string(label_find_caption($label_id,
  237. $_SESSION["uid"]));
  238. $reply["info-for-headlines"] = array();
  239. if ($label) {
  240. foreach ($ids as $id) {
  241. if ($assign)
  242. label_add_article($id, $label, $_SESSION["uid"]);
  243. else
  244. label_remove_article($id, $label, $_SESSION["uid"]);
  245. $labels = get_article_labels($id, $_SESSION["uid"]);
  246. array_push($reply["info-for-headlines"],
  247. array("id" => $id, "labels" => format_article_labels($labels, $id)));
  248. }
  249. }
  250. $reply["message"] = "UPDATE_COUNTERS";
  251. print json_encode($reply);
  252. }
  253. }