]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | error_reporting(E_ERROR | E_PARSE); | |
3 | ||
4 | require_once "../config.php"; | |
5 | ||
6 | require_once "../db.php"; | |
7 | require_once "../db-prefs.php"; | |
8 | require_once "../functions.php"; | |
9 | ||
10 | define('API_STATUS_OK', 0); | |
11 | define('API_STATUS_ERR', 1); | |
12 | ||
13 | if (defined('ENABLE_GZIP_OUTPUT') && ENABLE_GZIP_OUTPUT) { | |
14 | ob_start("ob_gzhandler"); | |
15 | } | |
16 | ||
17 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
18 | ||
19 | $session_expire = SESSION_EXPIRE_TIME; //seconds | |
20 | $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api"; | |
21 | ||
22 | session_name($session_name); | |
23 | ||
24 | if ($_REQUEST["sid"]) { | |
25 | session_id($_REQUEST["sid"]); | |
26 | } | |
27 | ||
28 | session_start(); | |
29 | ||
30 | if (!$link) { | |
31 | if (DB_TYPE == "mysql") { | |
32 | print mysql_error(); | |
33 | } | |
34 | // PG seems to display its own errors just fine by default. | |
35 | return; | |
36 | } | |
37 | ||
38 | init_connection($link); | |
39 | ||
40 | $op = db_escape_string($_REQUEST["op"]); | |
41 | $seq = (int) $_REQUEST["seq"]; | |
42 | ||
43 | // header("Content-Type: application/json"); | |
44 | ||
45 | function api_wrap_reply($status, $seq, $reply) { | |
46 | print json_encode(array("seq" => $seq, | |
47 | "status" => $status, | |
48 | "content" => $reply)); | |
49 | } | |
50 | ||
51 | if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") { | |
52 | print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'NOT_LOGGED_IN')); | |
53 | return; | |
54 | } | |
55 | ||
56 | if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) { | |
57 | print api_wrap_reply(API_STATUS_ERR, $seq, array("error" => 'API_DISABLED')); | |
58 | return; | |
59 | } | |
60 | ||
61 | switch ($op) { | |
62 | ||
63 | case "getVersion": | |
64 | $rv = array("version" => VERSION); | |
65 | print api_wrap_reply(API_STATUS_OK, $seq, $rv); | |
66 | break; | |
67 | ||
68 | case "login": | |
69 | $login = db_escape_string($_REQUEST["user"]); | |
70 | $password = db_escape_string($_REQUEST["password"]); | |
71 | $password_base64 = db_escape_string(base64_decode($_REQUEST["password"])); | |
72 | ||
73 | if (SINGLE_USER_MODE) $login = "admin"; | |
74 | ||
75 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'"); | |
76 | ||
77 | if (db_num_rows($result) != 0) { | |
78 | $uid = db_fetch_result($result, 0, "id"); | |
79 | } else { | |
80 | $uid = 0; | |
81 | } | |
82 | ||
83 | if (!$uid) { | |
84 | print api_wrap_reply(API_STATUS_ERR, $seq, | |
85 | array("error" => "LOGIN_ERROR")); | |
86 | return; | |
87 | } | |
88 | ||
89 | if (get_pref($link, "ENABLE_API_ACCESS", $uid)) { | |
90 | if (authenticate_user($link, $login, $password)) { // try login with normal password | |
91 | print api_wrap_reply(API_STATUS_OK, $seq, | |
92 | array("session_id" => session_id())); | |
93 | } else if (authenticate_user($link, $login, $password_base64)) { // else try with base64_decoded password | |
94 | print api_wrap_reply(API_STATUS_OK, $seq, | |
95 | array("session_id" => session_id())); | |
96 | } else { // else we are not logged in | |
97 | print api_wrap_reply(API_STATUS_ERR, $seq, | |
98 | array("error" => "LOGIN_ERROR")); | |
99 | } | |
100 | } else { | |
101 | print api_wrap_reply(API_STATUS_ERR, $seq, | |
102 | array("error" => "API_DISABLED")); | |
103 | } | |
104 | ||
105 | break; | |
106 | ||
107 | case "logout": | |
108 | logout_user(); | |
109 | print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK")); | |
110 | break; | |
111 | ||
112 | case "isLoggedIn": | |
113 | print api_wrap_reply(API_STATUS_OK, $seq, | |
114 | array("status" => $_SESSION["uid"] != '')); | |
115 | break; | |
116 | ||
117 | case "getUnread": | |
118 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
119 | $is_cat = db_escape_string($_REQUEST["is_cat"]); | |
120 | ||
121 | if ($feed_id) { | |
122 | print api_wrap_reply(API_STATUS_OK, $seq, | |
123 | array("unread" => getFeedUnread($link, $feed_id, $is_cat))); | |
124 | } else { | |
125 | print api_wrap_reply(API_STATUS_OK, $seq, | |
126 | array("unread" => getGlobalUnread($link))); | |
127 | } | |
128 | break; | |
129 | ||
130 | /* Method added for ttrss-reader for Android */ | |
131 | case "getCounters": | |
132 | ||
133 | /* flct (flc is the default) FIXME: document */ | |
134 | $output_mode = db_escape_string($_REQUEST["output_mode"]); | |
135 | ||
136 | print api_wrap_reply(API_STATUS_OK, $seq, | |
137 | getAllCounters($link, $output_mode)); | |
138 | break; | |
139 | ||
140 | case "getFeeds": | |
141 | $cat_id = db_escape_string($_REQUEST["cat_id"]); | |
142 | $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]); | |
143 | $limit = (int) db_escape_string($_REQUEST["limit"]); | |
144 | $offset = (int) db_escape_string($_REQUEST["offset"]); | |
145 | ||
146 | chdir(".."); // so feed_has_icon() would work properly for relative ICONS_DIR | |
147 | ||
148 | $feeds = api_get_feeds($link, $cat_id, $unread_only, $limit, $offset); | |
149 | ||
150 | print api_wrap_reply(API_STATUS_OK, $seq, $feeds); | |
151 | ||
152 | break; | |
153 | ||
154 | case "getCategories": | |
155 | $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]); | |
156 | ||
157 | $result = db_query($link, "SELECT | |
158 | id, title FROM ttrss_feed_categories | |
159 | WHERE owner_uid = " . | |
160 | $_SESSION["uid"]); | |
161 | ||
162 | $cats = array(); | |
163 | ||
164 | while ($line = db_fetch_assoc($result)) { | |
165 | $unread = getFeedUnread($link, $line["id"], true); | |
166 | ||
167 | if ($unread || !$unread_only) { | |
168 | array_push($cats, array("id" => $line["id"], | |
169 | "title" => $line["title"], | |
170 | "unread" => $unread)); | |
171 | } | |
172 | } | |
173 | ||
174 | print api_wrap_reply(API_STATUS_OK, $seq, $cats); | |
175 | break; | |
176 | ||
177 | case "getHeadlines": | |
178 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
179 | $limit = (int)db_escape_string($_REQUEST["limit"]); | |
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 | ||
189 | $headlines = api_get_headlines($link, $feed_id, $limit, $offset, | |
190 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false, | |
191 | $include_attachments); | |
192 | ||
193 | print api_wrap_reply(API_STATUS_OK, $seq, $headlines); | |
194 | ||
195 | break; | |
196 | ||
197 | case "updateArticle": | |
198 | $article_ids = split(",", db_escape_string($_REQUEST["article_ids"])); | |
199 | $mode = (int) db_escape_string($_REQUEST["mode"]); | |
200 | $field_raw = (int)db_escape_string($_REQUEST["field"]); | |
201 | ||
202 | $field = ""; | |
203 | $set_to = ""; | |
204 | ||
205 | switch ($field_raw) { | |
206 | case 0: | |
207 | $field = "marked"; | |
208 | break; | |
209 | case 1: | |
210 | $field = "published"; | |
211 | break; | |
212 | case 2: | |
213 | $field = "unread"; | |
214 | break; | |
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 && $set_to && count($article_ids) > 0) { | |
230 | ||
231 | $article_ids = join(", ", $article_ids); | |
232 | ||
233 | if ($field == "unread") { | |
234 | $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to, | |
235 | last_read = NOW() | |
236 | WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); | |
237 | } else { | |
238 | $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to | |
239 | WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); | |
240 | } | |
241 | ||
242 | $num_updated = db_affected_rows($link, $result); | |
243 | ||
244 | if ($num_updated > 0 && $field == "unread") { | |
245 | $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries | |
246 | WHERE ref_id IN ($article_ids)"); | |
247 | ||
248 | while ($line = db_fetch_assoc($result)) { | |
249 | ccache_update($link, $line["feed_id"], $_SESSION["uid"]); | |
250 | } | |
251 | } | |
252 | ||
253 | print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK", | |
254 | "updated" => $num_updated)); | |
255 | ||
256 | } else { | |
257 | print api_wrap_reply(API_STATUS_ERR, $seq, | |
258 | array("error" => 'INCORRECT_USAGE')); | |
259 | } | |
260 | ||
261 | break; | |
262 | ||
263 | case "getArticle": | |
264 | ||
265 | $article_id = db_escape_string($_REQUEST["article_id"]); | |
266 | ||
267 | $query = "SELECT id,title,link,content,feed_id,comments,int_id, | |
268 | marked,unread,published, | |
269 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, | |
270 | author | |
271 | FROM ttrss_entries,ttrss_user_entries | |
272 | WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " . | |
273 | $_SESSION["uid"] ; | |
274 | ||
275 | $result = db_query($link, $query); | |
276 | ||
277 | $articles = array(); | |
278 | ||
279 | if (db_num_rows($result) != 0) { | |
280 | ||
281 | while ($line = db_fetch_assoc($result)) { | |
282 | ||
283 | $attachments = get_article_enclosures($link, $line['id']); | |
284 | ||
285 | $article = array( | |
286 | "id" => $line["id"], | |
287 | "title" => $line["title"], | |
288 | "link" => $line["link"], | |
289 | "labels" => get_article_labels($link, $line['id']), | |
290 | "unread" => sql_bool_to_bool($line["unread"]), | |
291 | "marked" => sql_bool_to_bool($line["marked"]), | |
292 | "published" => sql_bool_to_bool($line["published"]), | |
293 | "comments" => $line["comments"], | |
294 | "author" => $line["author"], | |
295 | "updated" => strtotime($line["updated"]), | |
296 | "content" => $line["content"], | |
297 | "feed_id" => $line["feed_id"], | |
298 | "attachments" => $attachments | |
299 | ); | |
300 | ||
301 | array_push($articles, $article); | |
302 | ||
303 | } | |
304 | } | |
305 | ||
306 | print api_wrap_reply(API_STATUS_OK, $seq, $articles); | |
307 | ||
308 | break; | |
309 | ||
310 | case "getConfig": | |
311 | $config = array( | |
312 | "icons_dir" => ICONS_DIR, | |
313 | "icons_url" => ICONS_URL); | |
314 | ||
315 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); | |
316 | ||
317 | $result = db_query($link, "SELECT COUNT(*) AS cf FROM | |
318 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
319 | ||
320 | $num_feeds = db_fetch_result($result, 0, "cf"); | |
321 | ||
322 | $config["num_feeds"] = (int)$num_feeds; | |
323 | ||
324 | print api_wrap_reply(API_STATUS_OK, $seq, $config); | |
325 | ||
326 | break; | |
327 | ||
328 | case "updateFeed": | |
329 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
330 | ||
331 | update_rss_feed($link, $feed_id, true); | |
332 | ||
333 | print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK")); | |
334 | ||
335 | break; | |
336 | ||
337 | case "catchupFeed": | |
338 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
339 | $is_cat = db_escape_string($_REQUEST["is_cat"]); | |
340 | ||
341 | catchup_feed($link, $feed_id, $is_cat); | |
342 | ||
343 | print api_wrap_reply(API_STATUS_OK, $seq, array("status" => "OK")); | |
344 | ||
345 | break; | |
346 | ||
347 | case "getPref": | |
348 | $pref_name = db_escape_string($_REQUEST["pref_name"]); | |
349 | ||
350 | print api_wrap_reply(API_STATUS_OK, $seq, | |
351 | array("value" => get_pref($link, $pref_name))); | |
352 | break; | |
353 | ||
354 | default: | |
355 | print api_wrap_reply(API_STATUS_ERR, $seq, | |
356 | array("error" => 'UNKNOWN_METHOD')); | |
357 | break; | |
358 | ||
359 | } | |
360 | ||
361 | db_close($link); | |
362 | ||
363 | ?> |