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