]>
Commit | Line | Data |
---|---|---|
de8260cb AD |
1 | <?php |
2 | ||
3 | class API extends Handler { | |
4 | ||
3e4af5b0 | 5 | const API_LEVEL = 2; |
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 | |
129 | id, title FROM ttrss_feed_categories | |
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"], | |
141 | "unread" => $unread)); | |
142 | } | |
143 | } | |
144 | ||
145 | foreach (array(-2,-1,0) as $cat_id) { | |
146 | $unread = getFeedUnread($this->link, $cat_id, true); | |
147 | ||
148 | if ($unread || !$unread_only) { | |
149 | array_push($cats, array("id" => $cat_id, | |
150 | "title" => getCategoryTitle($this->link, $cat_id), | |
151 | "unread" => $unread)); | |
152 | } | |
153 | } | |
154 | ||
155 | print $this->wrap(self::STATUS_OK, $cats); | |
156 | } | |
157 | ||
158 | function getHeadlines() { | |
159 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
8aa3becc | 160 | if ($feed_id != "") { |
de8260cb AD |
161 | |
162 | $limit = (int)db_escape_string($_REQUEST["limit"]); | |
163 | $offset = (int)db_escape_string($_REQUEST["skip"]); | |
164 | $filter = db_escape_string($_REQUEST["filter"]); | |
165 | $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]); | |
166 | $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]); | |
167 | $show_content = (bool)db_escape_string($_REQUEST["show_content"]); | |
168 | /* all_articles, unread, adaptive, marked, updated */ | |
169 | $view_mode = db_escape_string($_REQUEST["view_mode"]); | |
170 | $include_attachments = (bool)db_escape_string($_REQUEST["include_attachments"]); | |
171 | $since_id = (int)db_escape_string($_REQUEST["since_id"]); | |
172 | ||
3e4af5b0 AD |
173 | /* do not rely on params below */ |
174 | ||
175 | $search = db_escape_string($_REQUEST["search"]); | |
176 | $search_mode = db_escape_string($_REQUEST["search_mode"]); | |
177 | $match_on = db_escape_string($_REQUEST["match_on"]); | |
178 | ||
de8260cb AD |
179 | $headlines = api_get_headlines($this->link, $feed_id, $limit, $offset, |
180 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false, | |
3e4af5b0 | 181 | $include_attachments, $since_id, $search, $search_mode, $match_on); |
de8260cb AD |
182 | |
183 | print $this->wrap(self::STATUS_OK, $headlines); | |
184 | } else { | |
185 | print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
186 | } | |
187 | } | |
188 | ||
189 | function updateArticle() { | |
190 | $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric); | |
191 | $mode = (int) db_escape_string($_REQUEST["mode"]); | |
192 | $data = db_escape_string($_REQUEST["data"]); | |
193 | $field_raw = (int)db_escape_string($_REQUEST["field"]); | |
194 | ||
195 | $field = ""; | |
196 | $set_to = ""; | |
197 | ||
198 | switch ($field_raw) { | |
199 | case 0: | |
200 | $field = "marked"; | |
201 | break; | |
202 | case 1: | |
203 | $field = "published"; | |
204 | break; | |
205 | case 2: | |
206 | $field = "unread"; | |
207 | break; | |
208 | case 3: | |
209 | $field = "note"; | |
210 | }; | |
211 | ||
212 | switch ($mode) { | |
213 | case 1: | |
214 | $set_to = "true"; | |
215 | break; | |
216 | case 0: | |
217 | $set_to = "false"; | |
218 | break; | |
219 | case 2: | |
220 | $set_to = "NOT $field"; | |
221 | break; | |
222 | } | |
223 | ||
224 | if ($field == "note") $set_to = "'$data'"; | |
225 | ||
226 | if ($field && $set_to && count($article_ids) > 0) { | |
227 | ||
228 | $article_ids = join(", ", $article_ids); | |
229 | ||
230 | if ($field == "unread") { | |
231 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET $field = $set_to, | |
232 | last_read = NOW() | |
233 | WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); | |
234 | } else { | |
235 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET $field = $set_to | |
236 | WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); | |
237 | } | |
238 | ||
239 | $num_updated = db_affected_rows($this->link, $result); | |
240 | ||
241 | if ($num_updated > 0 && $field == "unread") { | |
242 | $result = db_query($this->link, "SELECT DISTINCT feed_id FROM ttrss_user_entries | |
243 | WHERE ref_id IN ($article_ids)"); | |
244 | ||
245 | while ($line = db_fetch_assoc($result)) { | |
246 | ccache_update($this->link, $line["feed_id"], $_SESSION["uid"]); | |
247 | } | |
248 | } | |
249 | ||
250 | print $this->wrap(self::STATUS_OK, array("status" => "OK", | |
251 | "updated" => $num_updated)); | |
252 | ||
253 | } else { | |
254 | print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
255 | } | |
256 | ||
257 | } | |
258 | ||
259 | function getArticle() { | |
260 | ||
261 | $article_id = join(",", array_filter(explode(",", db_escape_string($_REQUEST["article_id"])), is_numeric)); | |
262 | ||
263 | $query = "SELECT id,title,link,content,feed_id,comments,int_id, | |
264 | marked,unread,published, | |
265 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, | |
266 | author | |
267 | FROM ttrss_entries,ttrss_user_entries | |
268 | WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " . | |
269 | $_SESSION["uid"] ; | |
270 | ||
271 | $result = db_query($this->link, $query); | |
272 | ||
273 | $articles = array(); | |
274 | ||
275 | if (db_num_rows($result) != 0) { | |
276 | ||
277 | while ($line = db_fetch_assoc($result)) { | |
278 | ||
279 | $attachments = get_article_enclosures($this->link, $line['id']); | |
280 | ||
281 | $article = array( | |
282 | "id" => $line["id"], | |
283 | "title" => $line["title"], | |
284 | "link" => $line["link"], | |
285 | "labels" => get_article_labels($this->link, $line['id']), | |
286 | "unread" => sql_bool_to_bool($line["unread"]), | |
287 | "marked" => sql_bool_to_bool($line["marked"]), | |
288 | "published" => sql_bool_to_bool($line["published"]), | |
289 | "comments" => $line["comments"], | |
290 | "author" => $line["author"], | |
291 | "updated" => strtotime($line["updated"]), | |
292 | "content" => $line["content"], | |
293 | "feed_id" => $line["feed_id"], | |
294 | "attachments" => $attachments | |
295 | ); | |
296 | ||
297 | array_push($articles, $article); | |
298 | ||
299 | } | |
300 | } | |
301 | ||
302 | print $this->wrap(self::STATUS_OK, $articles); | |
303 | ||
304 | } | |
305 | ||
306 | function getConfig() { | |
307 | $config = array( | |
308 | "icons_dir" => ICONS_DIR, | |
309 | "icons_url" => ICONS_URL); | |
310 | ||
311 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); | |
312 | ||
313 | $result = db_query($this->link, "SELECT COUNT(*) AS cf FROM | |
314 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
315 | ||
316 | $num_feeds = db_fetch_result($result, 0, "cf"); | |
317 | ||
318 | $config["num_feeds"] = (int)$num_feeds; | |
319 | ||
320 | print $this->wrap(self::STATUS_OK, $config); | |
321 | } | |
322 | ||
323 | function updateFeed() { | |
324 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
325 | ||
326 | update_rss_feed($this->link, $feed_id, true); | |
327 | ||
328 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
329 | } | |
330 | ||
331 | function catchupFeed() { | |
332 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
333 | $is_cat = db_escape_string($_REQUEST["is_cat"]); | |
334 | ||
335 | catchup_feed($this->link, $feed_id, $is_cat); | |
336 | ||
337 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
338 | } | |
339 | ||
340 | function getPref() { | |
341 | $pref_name = db_escape_string($_REQUEST["pref_name"]); | |
342 | ||
343 | print $this->wrap(self::STATUS_OK, array("value" => get_pref($this->link, $pref_name))); | |
344 | } | |
345 | ||
ea1c2903 AD |
346 | function getLabels() { |
347 | //$article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric); | |
348 | ||
349 | $article_id = (int)$_REQUEST['article_id']; | |
350 | ||
351 | $rv = array(); | |
352 | ||
353 | $result = db_query($this->link, "SELECT id, caption, fg_color, bg_color | |
354 | FROM ttrss_labels2 | |
355 | WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption"); | |
356 | ||
357 | if ($article_id) | |
358 | $article_labels = get_article_labels($this->link, $article_id); | |
359 | else | |
360 | $article_labels = array(); | |
361 | ||
362 | while ($line = db_fetch_assoc($result)) { | |
363 | ||
364 | $checked = false; | |
365 | foreach ($article_labels as $al) { | |
366 | if ($al[0] == $line['id']) { | |
367 | $checked = true; | |
368 | break; | |
369 | } | |
370 | } | |
371 | ||
372 | array_push($rv, array( | |
373 | "id" => (int)$line['id'], | |
374 | "caption" => $line['caption'], | |
375 | "fg_color" => $line['fg_color'], | |
376 | "bg_color" => $line['bg_color'], | |
377 | "checked" => $checked)); | |
378 | } | |
379 | ||
380 | print $this->wrap(self::STATUS_OK, $rv); | |
381 | } | |
382 | ||
396bfdf9 AD |
383 | function setArticleLabel() { |
384 | ||
385 | $article_ids = array_filter(explode(",", db_escape_string($_REQUEST["article_ids"])), is_numeric); | |
386 | $label_id = (int) db_escape_string($_REQUEST['label_id']); | |
387 | $assign = (bool) db_escape_string($_REQUEST['assign']) == "true"; | |
388 | ||
389 | $label = db_escape_string(label_find_caption($this->link, | |
390 | $label_id, $_SESSION["uid"])); | |
391 | ||
392 | $num_updated = 0; | |
393 | ||
394 | if ($label) { | |
395 | ||
396 | foreach ($article_ids as $id) { | |
397 | ||
398 | if ($assign) | |
399 | label_add_article($this->link, $id, $label, $_SESSION["uid"]); | |
400 | else | |
401 | label_remove_article($this->link, $id, $label, $_SESSION["uid"]); | |
402 | ||
403 | ++$num_updated; | |
404 | ||
405 | } | |
406 | } | |
407 | ||
408 | print $this->wrap(self::STATUS_OK, array("status" => "OK", | |
409 | "updated" => $num_updated)); | |
410 | ||
411 | } | |
412 | ||
de8260cb AD |
413 | function index() { |
414 | print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD')); | |
415 | } | |
416 | ||
417 | } | |
418 | ||
419 | ?> |