]>
Commit | Line | Data |
---|---|---|
6afcbcd1 AD |
1 | <?php |
2 | class Article extends Handler_Protected { | |
3 | ||
4 | function csrf_ignore($method) { | |
1c9bda91 | 5 | $csrf_ignored = array("redirect", "editarticletags"); |
6afcbcd1 AD |
6 | |
7 | return array_search($method, $csrf_ignored) !== false; | |
8 | } | |
9 | ||
10 | function redirect() { | |
d9c85e0f | 11 | $id = $this->dbh->escape_string($_REQUEST['id']); |
6afcbcd1 | 12 | |
d9c85e0f | 13 | $result = $this->dbh->query("SELECT link FROM ttrss_entries, ttrss_user_entries |
6afcbcd1 AD |
14 | WHERE id = '$id' AND id = ref_id AND owner_uid = '".$_SESSION['uid']."' |
15 | LIMIT 1"); | |
16 | ||
d9c85e0f AD |
17 | if ($this->dbh->num_rows($result) == 1) { |
18 | $article_url = $this->dbh->fetch_result($result, 0, 'link'); | |
6afcbcd1 AD |
19 | $article_url = str_replace("\n", "", $article_url); |
20 | ||
21 | header("Location: $article_url"); | |
22 | return; | |
23 | ||
24 | } else { | |
25 | print_error(__("Article not found.")); | |
26 | } | |
27 | } | |
28 | ||
29 | function view() { | |
d9c85e0f AD |
30 | $id = $this->dbh->escape_string($_REQUEST["id"]); |
31 | $cids = explode(",", $this->dbh->escape_string($_REQUEST["cids"])); | |
32 | $mode = $this->dbh->escape_string($_REQUEST["mode"]); | |
33 | $omode = $this->dbh->escape_string($_REQUEST["omode"]); | |
6afcbcd1 AD |
34 | |
35 | // in prefetch mode we only output requested cids, main article | |
36 | // just gets marked as read (it already exists in client cache) | |
37 | ||
38 | $articles = array(); | |
39 | ||
40 | if ($mode == "") { | |
a42c55f0 | 41 | array_push($articles, format_article($id, false)); |
6afcbcd1 | 42 | } else if ($mode == "zoom") { |
a42c55f0 | 43 | array_push($articles, format_article($id, true, true)); |
6afcbcd1 AD |
44 | } else if ($mode == "raw") { |
45 | if ($_REQUEST['html']) { | |
46 | header("Content-Type: text/html"); | |
5bbc4bb4 | 47 | print '<link rel="stylesheet" type="text/css" href="css/tt-rss.css"/>'; |
6afcbcd1 AD |
48 | } |
49 | ||
a42c55f0 | 50 | $article = format_article($id, false); |
6afcbcd1 AD |
51 | print $article['content']; |
52 | return; | |
53 | } | |
54 | ||
a42c55f0 | 55 | $this->catchupArticleById($id, 0); |
6afcbcd1 AD |
56 | |
57 | if (!$_SESSION["bw_limit"]) { | |
58 | foreach ($cids as $cid) { | |
59 | if ($cid) { | |
a42c55f0 | 60 | array_push($articles, format_article($cid, false, false)); |
6afcbcd1 AD |
61 | } |
62 | } | |
63 | } | |
64 | ||
65 | print json_encode($articles); | |
66 | } | |
67 | ||
a42c55f0 | 68 | private function catchupArticleById($id, $cmode) { |
6afcbcd1 AD |
69 | |
70 | if ($cmode == 0) { | |
d9c85e0f | 71 | $this->dbh->query("UPDATE ttrss_user_entries SET |
6afcbcd1 AD |
72 | unread = false,last_read = NOW() |
73 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
74 | } else if ($cmode == 1) { | |
d9c85e0f | 75 | $this->dbh->query("UPDATE ttrss_user_entries SET |
6afcbcd1 AD |
76 | unread = true |
77 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
78 | } else { | |
d9c85e0f | 79 | $this->dbh->query("UPDATE ttrss_user_entries SET |
6afcbcd1 AD |
80 | unread = NOT unread,last_read = NOW() |
81 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
82 | } | |
83 | ||
a42c55f0 AD |
84 | $feed_id = getArticleFeed($id); |
85 | ccache_update($feed_id, $_SESSION["uid"]); | |
6afcbcd1 AD |
86 | } |
87 | ||
a42c55f0 | 88 | static function create_published_article($title, $url, $content, $labels_str, |
6afcbcd1 AD |
89 | $owner_uid) { |
90 | ||
5e3d5480 | 91 | $guid = 'SHA1:' . sha1("ttshared:" . $url . $owner_uid); // include owner_uid to prevent global GUID clash |
6afcbcd1 AD |
92 | $content_hash = sha1($content); |
93 | ||
94 | if ($labels_str != "") { | |
95 | $labels = explode(",", $labels_str); | |
96 | } else { | |
97 | $labels = array(); | |
98 | } | |
99 | ||
100 | $rc = false; | |
101 | ||
102 | if (!$title) $title = $url; | |
103 | if (!$title && !$url) return false; | |
104 | ||
105 | if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) return false; | |
106 | ||
a42c55f0 | 107 | db_query("BEGIN"); |
6afcbcd1 AD |
108 | |
109 | // only check for our user data here, others might have shared this with different content etc | |
a42c55f0 | 110 | $result = db_query("SELECT id FROM ttrss_entries, ttrss_user_entries WHERE |
6afcbcd1 AD |
111 | link = '$url' AND ref_id = id AND owner_uid = '$owner_uid' LIMIT 1"); |
112 | ||
113 | if (db_num_rows($result) != 0) { | |
114 | $ref_id = db_fetch_result($result, 0, "id"); | |
115 | ||
a42c55f0 | 116 | $result = db_query("SELECT int_id FROM ttrss_user_entries WHERE |
6afcbcd1 AD |
117 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' LIMIT 1"); |
118 | ||
119 | if (db_num_rows($result) != 0) { | |
120 | $int_id = db_fetch_result($result, 0, "int_id"); | |
121 | ||
a42c55f0 | 122 | db_query("UPDATE ttrss_entries SET |
6afcbcd1 AD |
123 | content = '$content', content_hash = '$content_hash' WHERE id = '$ref_id'"); |
124 | ||
a42c55f0 | 125 | db_query("UPDATE ttrss_user_entries SET published = true, |
d2888e88 | 126 | last_published = NOW() WHERE |
6afcbcd1 AD |
127 | int_id = '$int_id' AND owner_uid = '$owner_uid'"); |
128 | } else { | |
129 | ||
a42c55f0 | 130 | db_query("INSERT INTO ttrss_user_entries |
d2888e88 AD |
131 | (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache, |
132 | last_read, note, unread, last_published) | |
6afcbcd1 | 133 | VALUES |
d2888e88 | 134 | ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())"); |
6afcbcd1 AD |
135 | } |
136 | ||
137 | if (count($labels) != 0) { | |
138 | foreach ($labels as $label) { | |
a42c55f0 | 139 | label_add_article($ref_id, trim($label), $owner_uid); |
6afcbcd1 AD |
140 | } |
141 | } | |
142 | ||
143 | $rc = true; | |
144 | ||
145 | } else { | |
a42c55f0 | 146 | $result = db_query("INSERT INTO ttrss_entries |
6afcbcd1 AD |
147 | (title, guid, link, updated, content, content_hash, date_entered, date_updated) |
148 | VALUES | |
149 | ('$title', '$guid', '$url', NOW(), '$content', '$content_hash', NOW(), NOW())"); | |
150 | ||
a42c55f0 | 151 | $result = db_query("SELECT id FROM ttrss_entries WHERE guid = '$guid'"); |
6afcbcd1 AD |
152 | |
153 | if (db_num_rows($result) != 0) { | |
154 | $ref_id = db_fetch_result($result, 0, "id"); | |
155 | ||
a42c55f0 | 156 | db_query("INSERT INTO ttrss_user_entries |
d2888e88 AD |
157 | (ref_id, uuid, feed_id, orig_feed_id, owner_uid, published, tag_cache, label_cache, |
158 | last_read, note, unread, last_published) | |
6afcbcd1 | 159 | VALUES |
d2888e88 | 160 | ('$ref_id', '', NULL, NULL, $owner_uid, true, '', '', NOW(), '', false, NOW())"); |
6afcbcd1 AD |
161 | |
162 | if (count($labels) != 0) { | |
163 | foreach ($labels as $label) { | |
a42c55f0 | 164 | label_add_article($ref_id, trim($label), $owner_uid); |
6afcbcd1 AD |
165 | } |
166 | } | |
167 | ||
168 | $rc = true; | |
169 | } | |
170 | } | |
171 | ||
a42c55f0 | 172 | db_query("COMMIT"); |
6afcbcd1 AD |
173 | |
174 | return $rc; | |
175 | } | |
176 | ||
1c9bda91 AD |
177 | function editArticleTags() { |
178 | ||
179 | print __("Tags for this article (separated by commas):")."<br>"; | |
180 | ||
d9c85e0f | 181 | $param = $this->dbh->escape_string($_REQUEST['param']); |
1c9bda91 | 182 | |
d9c85e0f | 183 | $tags = get_article_tags($this->dbh->escape_string($param)); |
1c9bda91 AD |
184 | |
185 | $tags_str = join(", ", $tags); | |
186 | ||
187 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$param\">"; | |
5df8be5c | 188 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"article\">"; |
1c9bda91 AD |
189 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"setArticleTags\">"; |
190 | ||
191 | print "<table width='100%'><tr><td>"; | |
192 | ||
193 | print "<textarea dojoType=\"dijit.form.SimpleTextarea\" rows='4' | |
194 | style='font-size : 12px; width : 100%' id=\"tags_str\" | |
195 | name='tags_str'>$tags_str</textarea> | |
196 | <div class=\"autocomplete\" id=\"tags_choices\" | |
197 | style=\"display:none\"></div>"; | |
198 | ||
199 | print "</td></tr></table>"; | |
200 | ||
201 | print "<div class='dlgButtons'>"; | |
202 | ||
203 | print "<button dojoType=\"dijit.form.Button\" | |
204 | onclick=\"dijit.byId('editTagsDlg').execute()\">".__('Save')."</button> "; | |
205 | print "<button dojoType=\"dijit.form.Button\" | |
206 | onclick=\"dijit.byId('editTagsDlg').hide()\">".__('Cancel')."</button>"; | |
207 | print "</div>"; | |
208 | ||
209 | } | |
6afcbcd1 | 210 | |
d719b062 | 211 | function setScore() { |
d9c85e0f AD |
212 | $ids = $this->dbh->escape_string($_REQUEST['id']); |
213 | $score = (int)$this->dbh->escape_string($_REQUEST['score']); | |
d719b062 | 214 | |
d9c85e0f | 215 | $this->dbh->query("UPDATE ttrss_user_entries SET |
d719b062 AD |
216 | score = '$score' WHERE ref_id IN ($ids) AND owner_uid = " . $_SESSION["uid"]); |
217 | ||
6f7798b6 | 218 | print json_encode(array("id" => $ids, |
d719b062 AD |
219 | "score_pic" => get_score_pic($score))); |
220 | } | |
221 | ||
6afcbcd1 | 222 | |
5df8be5c AD |
223 | function setArticleTags() { |
224 | ||
d9c85e0f | 225 | $id = $this->dbh->escape_string($_REQUEST["id"]); |
5df8be5c | 226 | |
d9c85e0f | 227 | $tags_str = $this->dbh->escape_string($_REQUEST["tags_str"]); |
5df8be5c AD |
228 | $tags = array_unique(trim_array(explode(",", $tags_str))); |
229 | ||
d9c85e0f | 230 | $this->dbh->query("BEGIN"); |
5df8be5c | 231 | |
d9c85e0f | 232 | $result = $this->dbh->query("SELECT int_id FROM ttrss_user_entries WHERE |
5df8be5c AD |
233 | ref_id = '$id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1"); |
234 | ||
d9c85e0f | 235 | if ($this->dbh->num_rows($result) == 1) { |
5df8be5c AD |
236 | |
237 | $tags_to_cache = array(); | |
238 | ||
d9c85e0f | 239 | $int_id = $this->dbh->fetch_result($result, 0, "int_id"); |
5df8be5c | 240 | |
d9c85e0f | 241 | $this->dbh->query("DELETE FROM ttrss_tags WHERE |
5df8be5c AD |
242 | post_int_id = $int_id AND owner_uid = '".$_SESSION["uid"]."'"); |
243 | ||
244 | foreach ($tags as $tag) { | |
245 | $tag = sanitize_tag($tag); | |
246 | ||
247 | if (!tag_is_valid($tag)) { | |
248 | continue; | |
249 | } | |
250 | ||
251 | if (preg_match("/^[0-9]*$/", $tag)) { | |
252 | continue; | |
253 | } | |
254 | ||
255 | // print "<!-- $id : $int_id : $tag -->"; | |
256 | ||
257 | if ($tag != '') { | |
d9c85e0f | 258 | $this->dbh->query("INSERT INTO ttrss_tags |
5df8be5c AD |
259 | (post_int_id, owner_uid, tag_name) VALUES ('$int_id', '".$_SESSION["uid"]."', '$tag')"); |
260 | } | |
261 | ||
262 | array_push($tags_to_cache, $tag); | |
263 | } | |
264 | ||
265 | /* update tag cache */ | |
266 | ||
267 | sort($tags_to_cache); | |
268 | $tags_str = join(",", $tags_to_cache); | |
269 | ||
d9c85e0f | 270 | $this->dbh->query("UPDATE ttrss_user_entries |
5df8be5c AD |
271 | SET tag_cache = '$tags_str' WHERE ref_id = '$id' |
272 | AND owner_uid = " . $_SESSION["uid"]); | |
273 | } | |
274 | ||
d9c85e0f | 275 | $this->dbh->query("COMMIT"); |
5df8be5c | 276 | |
a42c55f0 | 277 | $tags = get_article_tags($id); |
5df8be5c AD |
278 | $tags_str = format_tags_string($tags, $id); |
279 | $tags_str_full = join(", ", $tags); | |
280 | ||
281 | if (!$tags_str_full) $tags_str_full = __("no tags"); | |
282 | ||
283 | print json_encode(array("id" => (int)$id, | |
284 | "content" => $tags_str, "content_full" => $tags_str_full)); | |
285 | } | |
286 | ||
c83554bd AD |
287 | |
288 | function completeTags() { | |
d9c85e0f | 289 | $search = $this->dbh->escape_string($_REQUEST["search"]); |
c83554bd | 290 | |
d9c85e0f | 291 | $result = $this->dbh->query("SELECT DISTINCT tag_name FROM ttrss_tags |
c83554bd AD |
292 | WHERE owner_uid = '".$_SESSION["uid"]."' AND |
293 | tag_name LIKE '$search%' ORDER BY tag_name | |
294 | LIMIT 10"); | |
295 | ||
296 | print "<ul>"; | |
d9c85e0f | 297 | while ($line = $this->dbh->fetch_assoc($result)) { |
c83554bd AD |
298 | print "<li>" . $line["tag_name"] . "</li>"; |
299 | } | |
300 | print "</ul>"; | |
301 | } | |
302 | ||
4b7726f0 AD |
303 | function assigntolabel() { |
304 | return $this->labelops(true); | |
305 | } | |
306 | ||
307 | function removefromlabel() { | |
308 | return $this->labelops(false); | |
309 | } | |
310 | ||
311 | private function labelops($assign) { | |
312 | $reply = array(); | |
313 | ||
d9c85e0f AD |
314 | $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"])); |
315 | $label_id = $this->dbh->escape_string($_REQUEST["lid"]); | |
4b7726f0 | 316 | |
d9c85e0f | 317 | $label = $this->dbh->escape_string(label_find_caption($label_id, |
4b7726f0 AD |
318 | $_SESSION["uid"])); |
319 | ||
320 | $reply["info-for-headlines"] = array(); | |
321 | ||
322 | if ($label) { | |
323 | ||
324 | foreach ($ids as $id) { | |
325 | ||
326 | if ($assign) | |
a42c55f0 | 327 | label_add_article($id, $label, $_SESSION["uid"]); |
4b7726f0 | 328 | else |
a42c55f0 | 329 | label_remove_article($id, $label, $_SESSION["uid"]); |
4b7726f0 | 330 | |
a42c55f0 | 331 | $labels = get_article_labels($id, $_SESSION["uid"]); |
4b7726f0 AD |
332 | |
333 | array_push($reply["info-for-headlines"], | |
334 | array("id" => $id, "labels" => format_article_labels($labels, $id))); | |
335 | ||
336 | } | |
337 | } | |
338 | ||
339 | $reply["message"] = "UPDATE_COUNTERS"; | |
340 | ||
341 | print json_encode($reply); | |
342 | } | |
343 | ||
344 | ||
c83554bd | 345 | |
6afcbcd1 | 346 | } |