article.php 9.5 KB

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