]>
Commit | Line | Data |
---|---|---|
de8260cb AD |
1 | <?php |
2 | ||
3 | class API extends Handler { | |
4 | ||
8361e724 | 5 | const API_LEVEL = 4; |
de8260cb AD |
6 | |
7 | const STATUS_OK = 0; | |
8 | const STATUS_ERR = 1; | |
9 | ||
10 | private $seq; | |
11 | ||
12 | function before($method) { | |
13 | if (parent::before($method)) { | |
839b0658 | 14 | header("Content-Type: text/plain"); |
de8260cb AD |
15 | |
16 | if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") { | |
17 | print $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN')); | |
18 | return false; | |
19 | } | |
20 | ||
21 | if ($_SESSION["uid"] && $method != "logout" && !get_pref($this->link, 'ENABLE_API_ACCESS')) { | |
22 | print $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED')); | |
23 | return false; | |
24 | } | |
25 | ||
26 | $this->seq = (int) $_REQUEST['seq']; | |
27 | ||
de8260cb AD |
28 | return true; |
29 | } | |
30 | return false; | |
31 | } | |
32 | ||
33 | function wrap($status, $reply) { | |
34 | print json_encode(array("seq" => $this->seq, | |
35 | "status" => $status, | |
36 | "content" => $reply)); | |
37 | } | |
38 | ||
39 | function getVersion() { | |
40 | $rv = array("version" => VERSION); | |
41 | print $this->wrap(self::STATUS_OK, $rv); | |
42 | } | |
43 | ||
44 | function getApiLevel() { | |
a3b5394a | 45 | $rv = array("level" => self::API_LEVEL); |
de8260cb AD |
46 | print $this->wrap(self::STATUS_OK, $rv); |
47 | } | |
48 | ||
49 | function login() { | |
50 | $login = db_escape_string($_REQUEST["user"]); | |
51 | $password = $_REQUEST["password"]; | |
52 | $password_base64 = base64_decode($_REQUEST["password"]); | |
53 | ||
54 | if (SINGLE_USER_MODE) $login = "admin"; | |
55 | ||
56 | $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$login'"); | |
57 | ||
58 | if (db_num_rows($result) != 0) { | |
59 | $uid = db_fetch_result($result, 0, "id"); | |
60 | } else { | |
61 | $uid = 0; | |
62 | } | |
63 | ||
64 | if (!$uid) { | |
65 | print $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); | |
66 | return; | |
67 | } | |
68 | ||
69 | if (get_pref($this->link, "ENABLE_API_ACCESS", $uid)) { | |
70 | if (authenticate_user($this->link, $login, $password)) { // try login with normal password | |
5ba4ebc6 AD |
71 | print $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
72 | "api_level" => self::API_LEVEL)); | |
de8260cb | 73 | } else if (authenticate_user($this->link, $login, $password_base64)) { // else try with base64_decoded password |
5ba4ebc6 AD |
74 | print $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
75 | "api_level" => self::API_LEVEL)); | |
de8260cb AD |
76 | } else { // else we are not logged in |
77 | print $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); | |
78 | } | |
79 | } else { | |
80 | print $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED")); | |
81 | } | |
82 | ||
83 | } | |
84 | ||
85 | function logout() { | |
86 | logout_user(); | |
87 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
88 | } | |
89 | ||
90 | function isLoggedIn() { | |
91 | print $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != '')); | |
92 | } | |
93 | ||
94 | function getUnread() { | |
95 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
96 | $is_cat = db_escape_string($_REQUEST["is_cat"]); | |
97 | ||
98 | if ($feed_id) { | |
99 | print $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($this->link, $feed_id, $is_cat))); | |
100 | } else { | |
101 | print $this->wrap(self::STATUS_OK, array("unread" => getGlobalUnread($this->link))); | |
102 | } | |
103 | } | |
104 | ||
105 | /* Method added for ttrss-reader for Android */ | |
106 | function getCounters() { | |
107 | ||
108 | /* flct (flc is the default) FIXME: document */ | |
109 | $output_mode = db_escape_string($_REQUEST["output_mode"]); | |
110 | ||
111 | print $this->wrap(self::STATUS_OK, getAllCounters($this->link, $output_mode)); | |
112 | } | |
113 | ||
114 | function getFeeds() { | |
115 | $cat_id = db_escape_string($_REQUEST["cat_id"]); | |
116 | $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]); | |
117 | $limit = (int) db_escape_string($_REQUEST["limit"]); | |
118 | $offset = (int) db_escape_string($_REQUEST["offset"]); | |
48646336 | 119 | $include_nested = (bool)db_escape_string($_REQUEST["include_nested"]); |
de8260cb | 120 | |
48646336 | 121 | $feeds = api_get_feeds($this->link, $cat_id, $unread_only, $limit, $offset, $include_nested); |
de8260cb AD |
122 | |
123 | print $this->wrap(self::STATUS_OK, $feeds); | |
124 | } | |
125 | ||
126 | function getCategories() { | |
127 | $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]); | |
48646336 | 128 | $enable_nested = (bool)db_escape_string($_REQUEST["enable_nested"]); |
de8260cb AD |
129 | |
130 | // TODO do not return empty categories, return Uncategorized and standard virtual cats | |
131 | ||
48646336 AD |
132 | if ($enable_nested) |
133 | $nested_qpart = "parent_cat IS NULL"; | |
134 | else | |
135 | $nested_qpart = "true"; | |
136 | ||
de8260cb | 137 | $result = db_query($this->link, "SELECT |
b11e9943 | 138 | id, title, order_id FROM ttrss_feed_categories |
48646336 | 139 | WHERE $nested_qpart AND owner_uid = " . |
de8260cb AD |
140 | $_SESSION["uid"]); |
141 | ||
142 | $cats = array(); | |
143 | ||
144 | while ($line = db_fetch_assoc($result)) { | |
145 | $unread = getFeedUnread($this->link, $line["id"], true); | |
146 | ||
48646336 AD |
147 | if ($enable_nested) |
148 | $unread += getCategoryChildrenUnread($this->link, $line["id"]); | |
149 | ||
de8260cb AD |
150 | if ($unread || !$unread_only) { |
151 | array_push($cats, array("id" => $line["id"], | |
152 | "title" => $line["title"], | |
b11e9943 AD |
153 | "unread" => $unread, |
154 | "order_id" => (int) $line["order_id"], | |
155 | )); | |
de8260cb AD |
156 | } |
157 | } | |
158 | ||
159 | foreach (array(-2,-1,0) as $cat_id) { | |
160 | $unread = getFeedUnread($this->link, $cat_id, true); | |
161 | ||
162 | if ($unread || !$unread_only) { | |
163 | array_push($cats, array("id" => $cat_id, | |
164 | "title" => getCategoryTitle($this->link, $cat_id), | |
165 | "unread" => $unread)); | |
166 | } | |
167 | } | |
168 | ||
169 | print $this->wrap(self::STATUS_OK, $cats); | |
170 | } | |
171 | ||
172 | function getHeadlines() { | |
173 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
8aa3becc | 174 | if ($feed_id != "") { |
de8260cb AD |
175 | |
176 | $limit = (int)db_escape_string($_REQUEST["limit"]); | |
1a740cf6 AD |
177 | |
178 | if (!$limit || $limit >= 60) $limit = 60; | |
179 | ||
de8260cb AD |
180 | $offset = (int)db_escape_string($_REQUEST["skip"]); |
181 | $filter = db_escape_string($_REQUEST["filter"]); | |
182 | $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]); | |
183 | $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]); | |
184 | $show_content = (bool)db_escape_string($_REQUEST["show_content"]); | |
185 | /* all_articles, unread, adaptive, marked, updated */ | |
186 | $view_mode = db_escape_string($_REQUEST["view_mode"]); | |
187 | $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]); | |
188 | $since_id = (int)db_escape_string($_REQUEST["since_id"]); | |
48646336 | 189 | $include_nested = (bool)db_escape_string($_REQUEST["include_nested"]); |
de8260cb | 190 | |
3e4af5b0 AD |
191 | /* do not rely on params below */ |
192 | ||
193 | $search = db_escape_string($_REQUEST["search"]); | |
194 | $search_mode = db_escape_string($_REQUEST["search_mode"]); | |
195 | $match_on = db_escape_string($_REQUEST["match_on"]); | |
196 | ||
de8260cb AD |
197 | $headlines = api_get_headlines($this->link, $feed_id, $limit, $offset, |
198 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false, | |
48646336 AD |
199 | $include_attachments, $since_id, $search, $search_mode, $match_on, |
200 | $include_nested); | |
de8260cb AD |
201 | |
202 | print $this->wrap(self::STATUS_OK, $headlines); | |
203 | } else { | |
204 | print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
205 | } | |
206 | } | |
207 | ||
208 | function updateArticle() { | |
209 | $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric); | |
210 | $mode = (int) db_escape_string($_REQUEST["mode"]); | |
211 | $data = db_escape_string($_REQUEST["data"]); | |
212 | $field_raw = (int)db_escape_string($_REQUEST["field"]); | |
213 | ||
214 | $field = ""; | |
215 | $set_to = ""; | |
216 | ||
217 | switch ($field_raw) { | |
218 | case 0: | |
219 | $field = "marked"; | |
220 | break; | |
221 | case 1: | |
222 | $field = "published"; | |
223 | break; | |
224 | case 2: | |
225 | $field = "unread"; | |
226 | break; | |
227 | case 3: | |
228 | $field = "note"; | |
229 | }; | |
230 | ||
231 | switch ($mode) { | |
232 | case 1: | |
233 | $set_to = "true"; | |
234 | break; | |
235 | case 0: | |
236 | $set_to = "false"; | |
237 | break; | |
238 | case 2: | |
239 | $set_to = "NOT $field"; | |
240 | break; | |
241 | } | |
242 | ||
243 | if ($field == "note") $set_to = "'$data'"; | |
244 | ||
245 | if ($field && $set_to && count($article_ids) > 0) { | |
246 | ||
247 | $article_ids = join(", ", $article_ids); | |
248 | ||
249 | if ($field == "unread") { | |
250 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET $field = $set_to, | |
251 | last_read = NOW() | |
252 | WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); | |
253 | } else { | |
254 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET $field = $set_to | |
255 | WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); | |
256 | } | |
257 | ||
258 | $num_updated = db_affected_rows($this->link, $result); | |
259 | ||
260 | if ($num_updated > 0 && $field == "unread") { | |
261 | $result = db_query($this->link, "SELECT DISTINCT feed_id FROM ttrss_user_entries | |
262 | WHERE ref_id IN ($article_ids)"); | |
263 | ||
264 | while ($line = db_fetch_assoc($result)) { | |
265 | ccache_update($this->link, $line["feed_id"], $_SESSION["uid"]); | |
266 | } | |
267 | } | |
268 | ||
269 | print $this->wrap(self::STATUS_OK, array("status" => "OK", | |
270 | "updated" => $num_updated)); | |
271 | ||
272 | } else { | |
273 | print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
274 | } | |
275 | ||
276 | } | |
277 | ||
278 | function getArticle() { | |
279 | ||
280 | $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric)); | |
281 | ||
282 | $query = "SELECT id,title,link,content,feed_id,comments,int_id, | |
283 | marked,unread,published, | |
284 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, | |
285 | author | |
286 | FROM ttrss_entries,ttrss_user_entries | |
287 | WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " . | |
288 | $_SESSION["uid"] ; | |
289 | ||
290 | $result = db_query($this->link, $query); | |
291 | ||
292 | $articles = array(); | |
293 | ||
294 | if (db_num_rows($result) != 0) { | |
295 | ||
296 | while ($line = db_fetch_assoc($result)) { | |
297 | ||
298 | $attachments = get_article_enclosures($this->link, $line['id']); | |
299 | ||
300 | $article = array( | |
301 | "id" => $line["id"], | |
302 | "title" => $line["title"], | |
303 | "link" => $line["link"], | |
304 | "labels" => get_article_labels($this->link, $line['id']), | |
305 | "unread" => sql_bool_to_bool($line["unread"]), | |
306 | "marked" => sql_bool_to_bool($line["marked"]), | |
307 | "published" => sql_bool_to_bool($line["published"]), | |
308 | "comments" => $line["comments"], | |
309 | "author" => $line["author"], | |
310 | "updated" => strtotime($line["updated"]), | |
311 | "content" => $line["content"], | |
312 | "feed_id" => $line["feed_id"], | |
313 | "attachments" => $attachments | |
314 | ); | |
315 | ||
316 | array_push($articles, $article); | |
317 | ||
318 | } | |
319 | } | |
320 | ||
321 | print $this->wrap(self::STATUS_OK, $articles); | |
322 | ||
323 | } | |
324 | ||
325 | function getConfig() { | |
326 | $config = array( | |
327 | "icons_dir" => ICONS_DIR, | |
328 | "icons_url" => ICONS_URL); | |
329 | ||
330 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); | |
331 | ||
332 | $result = db_query($this->link, "SELECT COUNT(*) AS cf FROM | |
333 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
334 | ||
335 | $num_feeds = db_fetch_result($result, 0, "cf"); | |
336 | ||
337 | $config["num_feeds"] = (int)$num_feeds; | |
338 | ||
339 | print $this->wrap(self::STATUS_OK, $config); | |
340 | } | |
341 | ||
342 | function updateFeed() { | |
343 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
344 | ||
345 | update_rss_feed($this->link, $feed_id, true); | |
346 | ||
347 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
348 | } | |
349 | ||
350 | function catchupFeed() { | |
351 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
352 | $is_cat = db_escape_string($_REQUEST["is_cat"]); | |
353 | ||
354 | catchup_feed($this->link, $feed_id, $is_cat); | |
355 | ||
356 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
357 | } | |
358 | ||
359 | function getPref() { | |
360 | $pref_name = db_escape_string($_REQUEST["pref_name"]); | |
361 | ||
362 | print $this->wrap(self::STATUS_OK, array("value" => get_pref($this->link, $pref_name))); | |
363 | } | |
364 | ||
ea1c2903 AD |
365 | function getLabels() { |
366 | //$article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric); | |
367 | ||
368 | $article_id = (int)$_REQUEST['article_id']; | |
369 | ||
370 | $rv = array(); | |
371 | ||
372 | $result = db_query($this->link, "SELECT id, caption, fg_color, bg_color | |
373 | FROM ttrss_labels2 | |
374 | WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption"); | |
375 | ||
376 | if ($article_id) | |
377 | $article_labels = get_article_labels($this->link, $article_id); | |
378 | else | |
379 | $article_labels = array(); | |
380 | ||
381 | while ($line = db_fetch_assoc($result)) { | |
382 | ||
383 | $checked = false; | |
384 | foreach ($article_labels as $al) { | |
385 | if ($al[0] == $line['id']) { | |
386 | $checked = true; | |
387 | break; | |
388 | } | |
389 | } | |
390 | ||
391 | array_push($rv, array( | |
392 | "id" => (int)$line['id'], | |
393 | "caption" => $line['caption'], | |
394 | "fg_color" => $line['fg_color'], | |
395 | "bg_color" => $line['bg_color'], | |
396 | "checked" => $checked)); | |
397 | } | |
398 | ||
399 | print $this->wrap(self::STATUS_OK, $rv); | |
400 | } | |
401 | ||
396bfdf9 AD |
402 | function setArticleLabel() { |
403 | ||
404 | $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric); | |
405 | $label_id = (int) db_escape_string($_REQUEST['label_id']); | |
406 | $assign = (bool) db_escape_string($_REQUEST['assign']) == "true"; | |
407 | ||
408 | $label = db_escape_string(label_find_caption($this->link, | |
409 | $label_id, $_SESSION["uid"])); | |
410 | ||
411 | $num_updated = 0; | |
412 | ||
413 | if ($label) { | |
414 | ||
415 | foreach ($article_ids as $id) { | |
416 | ||
417 | if ($assign) | |
418 | label_add_article($this->link, $id, $label, $_SESSION["uid"]); | |
419 | else | |
420 | label_remove_article($this->link, $id, $label, $_SESSION["uid"]); | |
421 | ||
422 | ++$num_updated; | |
423 | ||
424 | } | |
425 | } | |
426 | ||
427 | print $this->wrap(self::STATUS_OK, array("status" => "OK", | |
428 | "updated" => $num_updated)); | |
429 | ||
430 | } | |
431 | ||
de8260cb AD |
432 | function index() { |
433 | print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD')); | |
434 | } | |
435 | ||
8361e724 AD |
436 | function shareToPublished() { |
437 | $title = db_escape_string(strip_tags($_REQUEST["title"])); | |
438 | $url = db_escape_string(strip_tags($_REQUEST["url"])); | |
439 | $content = db_escape_string(strip_tags($_REQUEST["content"])); | |
440 | ||
441 | if (create_published_article($this->link, $title, $url, $content, $_SESSION["uid"])) { | |
442 | print $this->wrap(self::STATUS_OK, array("status" => 'OK')); | |
443 | } else { | |
444 | print $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed')); | |
445 | } | |
446 | } | |
de8260cb AD |
447 | } |
448 | ||
449 | ?> |