]>
Commit | Line | Data |
---|---|---|
378a548c AD |
1 | <?php |
2 | ||
3 | /* This is experimental JSON-based API. It has to be manually enabled: | |
4 | * | |
5 | * Add define('_JSON_API_ENABLED', true) to config.php | |
6 | */ | |
7 | ||
8 | error_reporting(E_ERROR | E_PARSE); | |
9 | ||
10 | require_once "../config.php"; | |
11 | ||
12 | require_once "../db.php"; | |
13 | require_once "../db-prefs.php"; | |
14 | require_once "../functions.php"; | |
15 | ||
16 | if (!defined('_JSON_API_ENABLED')) { | |
17 | print json_encode(array("error" => "API_DISABLED")); | |
18 | return; | |
19 | } | |
20 | ||
21 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
22 | ||
23 | $session_expire = SESSION_EXPIRE_TIME; //seconds | |
24 | $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid_api" : TTRSS_SESSION_NAME . "_api"; | |
25 | ||
26 | session_start(); | |
27 | ||
28 | if (!$link) { | |
29 | if (DB_TYPE == "mysql") { | |
30 | print mysql_error(); | |
31 | } | |
32 | // PG seems to display its own errors just fine by default. | |
33 | return; | |
34 | } | |
35 | ||
36 | init_connection($link); | |
37 | ||
38 | $op = db_escape_string($_REQUEST["op"]); | |
39 | ||
40 | // header("Content-Type: application/json"); | |
41 | ||
42 | if (!$_SESSION["uid"] && $op != "login" && $op != "isLoggedIn") { | |
43 | print json_encode(array("error" => 'NOT_LOGGED_IN')); | |
44 | return; | |
45 | } | |
46 | ||
3a216db4 | 47 | if ($_SESSION["uid"] && $op != "logout" && !get_pref($link, 'ENABLE_API_ACCESS')) { |
378a548c AD |
48 | print json_encode(array("error" => 'API_DISABLED')); |
49 | return; | |
3a216db4 | 50 | } |
378a548c AD |
51 | |
52 | switch ($op) { | |
53 | case "getVersion": | |
54 | $rv = array("version" => VERSION); | |
55 | print json_encode($rv); | |
56 | break; | |
57 | case "login": | |
58 | $login = db_escape_string($_REQUEST["user"]); | |
59 | $password = db_escape_string($_REQUEST["password"]); | |
60 | ||
2bebdd34 AD |
61 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE login = '$login'"); |
62 | ||
63 | if (db_num_rows($result) != 0) { | |
64 | $uid = db_fetch_result($result, 0, "id"); | |
65 | } else { | |
66 | $uid = 0; | |
67 | } | |
68 | ||
69 | if (get_pref($link, "ENABLE_API_ACCESS", $uid)) { | |
4cdd0d7c AD |
70 | if (authenticate_user($link, $login, $password)) { |
71 | print json_encode(array("uid" => $_SESSION["uid"])); | |
72 | } else { | |
73 | print json_encode(array("error" => "LOGIN_ERROR")); | |
74 | } | |
378a548c | 75 | } else { |
4cdd0d7c | 76 | print json_encode(array("error" => "API_DISABLED")); |
378a548c AD |
77 | } |
78 | ||
79 | break; | |
80 | case "logout": | |
81 | logout_user(); | |
82 | print json_encode(array("uid" => 0)); | |
83 | break; | |
84 | case "isLoggedIn": | |
85 | print json_encode(array("status" => $_SESSION["uid"] != '')); | |
86 | break; | |
03e5f9eb AD |
87 | case "getUnread": |
88 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
89 | $is_cat = db_escape_string($_REQUEST["is_cat"]); | |
90 | ||
91 | if ($feed_id) { | |
92 | print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat))); | |
93 | } else { | |
94 | print json_encode(array("unread" => getGlobalUnread($link))); | |
95 | } | |
96 | break; | |
378a548c AD |
97 | case "getFeeds": |
98 | $cat_id = db_escape_string($_REQUEST["cat_id"]); | |
99 | $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]); | |
100 | ||
101 | if (!$cat_id) { | |
102 | $result = db_query($link, "SELECT | |
103 | id, feed_url, cat_id, title, ". | |
104 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
105 | FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
106 | } else { | |
107 | $result = db_query($link, "SELECT | |
108 | id, feed_url, cat_id, title, ". | |
109 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
110 | FROM ttrss_feeds WHERE | |
111 | cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]); | |
112 | } | |
113 | ||
114 | $feeds = array(); | |
115 | ||
116 | while ($line = db_fetch_assoc($result)) { | |
117 | ||
118 | $unread = getFeedUnread($link, $line["id"]); | |
119 | ||
120 | if ($unread || !$unread_only) { | |
121 | ||
7e2b7e46 | 122 | $row = array( |
378a548c AD |
123 | "feed_url" => $line["feed_url"], |
124 | "title" => $line["title"], | |
125 | "id" => (int)$line["id"], | |
126 | "unread" => (int)$unread, | |
127 | "cat_id" => (int)$line["cat_id"], | |
128 | "last_updated" => strtotime($line["last_updated"]) | |
129 | ); | |
130 | ||
7e2b7e46 AD |
131 | array_push($feeds, $row); |
132 | } | |
133 | } | |
134 | ||
53aff642 AD |
135 | /* Labels */ |
136 | ||
137 | if (!$cat_id || $cat_id == -2) { | |
7e2b7e46 AD |
138 | $counters = getLabelCounters($link, false, true); |
139 | ||
140 | foreach (array_keys($counters) as $id) { | |
141 | ||
142 | $unread = $counters[$id]["counter"]; | |
143 | ||
144 | if ($unread || !$unread_only) { | |
145 | ||
146 | $row = array( | |
147 | "id" => $id, | |
148 | "title" => $counters[$id]["description"], | |
149 | "unread" => $counters[$id]["counter"], | |
53aff642 | 150 | "cat_id" => -2, |
7e2b7e46 AD |
151 | ); |
152 | ||
153 | array_push($feeds, $row); | |
154 | } | |
378a548c AD |
155 | } |
156 | } | |
157 | ||
53aff642 AD |
158 | /* Virtual feeds */ |
159 | ||
160 | if (!$cat_id || $cat_id == -1) { | |
161 | foreach (array(-1, -2, -3, -4) as $i) { | |
162 | $unread = getFeedUnread($link, $i); | |
163 | ||
164 | if ($unread || !$unread_only) { | |
165 | $title = getFeedTitle($link, $i); | |
166 | ||
167 | $row = array( | |
168 | "id" => $i, | |
169 | "title" => $title, | |
170 | "unread" => $unread, | |
171 | "cat_id" => -1, | |
172 | ); | |
173 | array_push($feeds, $row); | |
174 | } | |
175 | ||
176 | } | |
177 | } | |
178 | ||
378a548c AD |
179 | print json_encode($feeds); |
180 | ||
181 | break; | |
182 | case "getCategories": | |
730c97c7 AD |
183 | $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]); |
184 | ||
378a548c AD |
185 | $result = db_query($link, "SELECT |
186 | id, title FROM ttrss_feed_categories | |
187 | WHERE owner_uid = " . | |
188 | $_SESSION["uid"]); | |
189 | ||
190 | $cats = array(); | |
191 | ||
192 | while ($line = db_fetch_assoc($result)) { | |
193 | $unread = getFeedUnread($link, $line["id"], true); | |
730c97c7 AD |
194 | |
195 | if ($unread || !$unread_only) { | |
f1c2b672 AD |
196 | array_push($cats, array("id" => $line["id"], |
197 | "title" => $line["title"], | |
198 | "unread" => $unread)); | |
730c97c7 | 199 | } |
378a548c AD |
200 | } |
201 | ||
202 | print json_encode($cats); | |
203 | break; | |
204 | case "getHeadlines": | |
205 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
206 | $limit = (int)db_escape_string($_REQUEST["limit"]); | |
207 | $filter = db_escape_string($_REQUEST["filter"]); | |
208 | $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]); | |
209 | $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]); | |
210 | ||
211 | /* do not rely on params below */ | |
212 | ||
213 | $search = db_escape_string($_REQUEST["search"]); | |
214 | $search_mode = db_escape_string($_REQUEST["search_mode"]); | |
215 | $match_on = db_escape_string($_REQUEST["match_on"]); | |
216 | ||
217 | $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, | |
218 | $view_mode, $is_cat, $search, $search_mode, $match_on); | |
219 | ||
220 | $result = $qfh_ret[0]; | |
221 | $feed_title = $qfh_ret[1]; | |
222 | ||
223 | $headlines = array(); | |
224 | ||
225 | while ($line = db_fetch_assoc($result)) { | |
226 | $is_updated = ($line["last_read"] == "" && | |
227 | ($line["unread"] != "t" && $line["unread"] != "1")); | |
228 | ||
229 | $headline_row = array( | |
230 | "id" => (int)$line["id"], | |
231 | "unread" => sql_bool_to_bool($line["unread"]), | |
232 | "marked" => sql_bool_to_bool($line["marked"]), | |
233 | "updated" => strtotime($line["updated"]), | |
234 | "is_updated" => $is_updated, | |
235 | "title" => $line["title"], | |
236 | "feed_id" => $line["feed_id"], | |
237 | ); | |
238 | ||
239 | if ($show_except) $headline_row["excerpt"] = $line["content_preview"]; | |
240 | ||
241 | array_push($headlines, $headline_row); | |
242 | } | |
243 | ||
244 | print json_encode($headlines); | |
245 | ||
730c97c7 | 246 | break; |
1c3fffbb AD |
247 | case "updateArticle": |
248 | $article_id = (int) db_escape_string($_GET["article_id"]); | |
249 | $mode = (int) db_escape_string($_REQUEST["mode"]); | |
250 | $field_raw = (int)db_escape_string($_REQUEST["field"]); | |
251 | ||
252 | $field = ""; | |
253 | $set_to = ""; | |
254 | ||
255 | switch ($field_raw) { | |
256 | case 0: | |
257 | $field = "marked"; | |
258 | break; | |
259 | case 1: | |
260 | $field = "published"; | |
261 | break; | |
262 | case 2: | |
263 | $field = "unread"; | |
264 | break; | |
265 | }; | |
266 | ||
267 | switch ($mode) { | |
268 | case 1: | |
269 | $set_to = "true"; | |
270 | break; | |
271 | case 0: | |
272 | $set_to = "false"; | |
273 | break; | |
274 | case 2: | |
275 | $set_to = "NOT $field"; | |
276 | break; | |
277 | } | |
278 | ||
279 | if ($field && $set_to) { | |
280 | if ($field == "unread") { | |
281 | $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to, | |
282 | last_read = NOW() | |
283 | WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]); | |
284 | } else { | |
285 | $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to | |
286 | WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]); | |
287 | } | |
288 | } | |
289 | ||
290 | break; | |
291 | ||
730c97c7 AD |
292 | case "getArticle": |
293 | ||
294 | $article_id = (int)db_escape_string($_REQUEST["article_id"]); | |
295 | ||
296 | $query = "SELECT title,link,content,feed_id,comments,int_id, | |
1c3fffbb | 297 | marked,unread,published, |
730c97c7 AD |
298 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, |
299 | author | |
300 | FROM ttrss_entries,ttrss_user_entries | |
301 | WHERE id = '$article_id' AND ref_id = id AND owner_uid = " . | |
302 | $_SESSION["uid"] ; | |
303 | ||
304 | $result = db_query($link, $query); | |
305 | ||
306 | $article = array(); | |
307 | ||
308 | if (db_num_rows($result) != 0) { | |
309 | $line = db_fetch_assoc($result); | |
310 | ||
311 | $article = array( | |
312 | "title" => $line["title"], | |
313 | "link" => $line["link"], | |
53aff642 | 314 | "labels" => get_article_labels($link, $article_id), |
730c97c7 AD |
315 | "unread" => sql_bool_to_bool($line["unread"]), |
316 | "marked" => sql_bool_to_bool($line["marked"]), | |
1c3fffbb | 317 | "published" => sql_bool_to_bool($line["published"]), |
730c97c7 AD |
318 | "comments" => $line["comments"], |
319 | "author" => $line["author"], | |
320 | "updated" => strtotime($line["updated"]), | |
321 | "content" => $line["content"], | |
322 | "feed_id" => $line["feed_id"], | |
323 | ); | |
324 | } | |
325 | ||
326 | print json_encode($article); | |
327 | ||
7c6d05cd AD |
328 | break; |
329 | case "getConfig": | |
330 | $config = array( | |
331 | "icons_dir" => ICONS_DIR, | |
332 | "icons_url" => ICONS_URL); | |
333 | ||
334 | if (ENABLE_UPDATE_DAEMON) { | |
335 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); | |
336 | } | |
337 | ||
338 | $result = db_query($link, "SELECT COUNT(*) AS cf FROM | |
339 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
340 | ||
341 | $num_feeds = db_fetch_result($result, 0, "cf"); | |
342 | ||
343 | $config["num_feeds"] = (int)$num_feeds; | |
344 | ||
345 | print json_encode($config); | |
346 | ||
4d557a13 | 347 | break; |
7c6d05cd | 348 | |
4d557a13 AD |
349 | case "getPref": |
350 | $pref_name = db_escape_string($_REQUEST["pref_name"]); | |
351 | print json_encode(array("value" => get_pref($link, $pref_name))); | |
378a548c AD |
352 | break; |
353 | } | |
354 | ||
355 | db_close($link); | |
356 | ||
357 | ?> |