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