]>
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 | ||
47 | /* TODO: add pref key to disable/enable API | |
48 | if ($_SESSION["uid"] && !get_pref($link, 'API_ENABLED')) { | |
49 | print json_encode(array("error" => 'API_DISABLED')); | |
50 | return; | |
51 | } */ | |
52 | ||
53 | switch ($op) { | |
54 | case "getVersion": | |
55 | $rv = array("version" => VERSION); | |
56 | print json_encode($rv); | |
57 | break; | |
58 | case "login": | |
59 | $login = db_escape_string($_REQUEST["user"]); | |
60 | $password = db_escape_string($_REQUEST["password"]); | |
61 | ||
62 | if (authenticate_user($link, $login, $password)) { | |
63 | print json_encode(array("uid" => $_SESSION["uid"])); | |
64 | } else { | |
65 | print json_encode(array("uid" => 0)); | |
66 | } | |
67 | ||
68 | break; | |
69 | case "logout": | |
70 | logout_user(); | |
71 | print json_encode(array("uid" => 0)); | |
72 | break; | |
73 | case "isLoggedIn": | |
74 | print json_encode(array("status" => $_SESSION["uid"] != '')); | |
75 | break; | |
03e5f9eb AD |
76 | case "getUnread": |
77 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
78 | $is_cat = db_escape_string($_REQUEST["is_cat"]); | |
79 | ||
80 | if ($feed_id) { | |
81 | print json_encode(array("unread" => getFeedUnread($link, $feed_id, $is_cat))); | |
82 | } else { | |
83 | print json_encode(array("unread" => getGlobalUnread($link))); | |
84 | } | |
85 | break; | |
378a548c AD |
86 | case "getFeeds": |
87 | $cat_id = db_escape_string($_REQUEST["cat_id"]); | |
88 | $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]); | |
89 | ||
90 | if (!$cat_id) { | |
91 | $result = db_query($link, "SELECT | |
92 | id, feed_url, cat_id, title, ". | |
93 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
94 | FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
95 | } else { | |
96 | $result = db_query($link, "SELECT | |
97 | id, feed_url, cat_id, title, ". | |
98 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
99 | FROM ttrss_feeds WHERE | |
100 | cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"]); | |
101 | } | |
102 | ||
103 | $feeds = array(); | |
104 | ||
105 | while ($line = db_fetch_assoc($result)) { | |
106 | ||
107 | $unread = getFeedUnread($link, $line["id"]); | |
108 | ||
109 | if ($unread || !$unread_only) { | |
110 | ||
7e2b7e46 | 111 | $row = array( |
378a548c AD |
112 | "feed_url" => $line["feed_url"], |
113 | "title" => $line["title"], | |
114 | "id" => (int)$line["id"], | |
115 | "unread" => (int)$unread, | |
116 | "cat_id" => (int)$line["cat_id"], | |
117 | "last_updated" => strtotime($line["last_updated"]) | |
118 | ); | |
119 | ||
7e2b7e46 AD |
120 | array_push($feeds, $row); |
121 | } | |
122 | } | |
123 | ||
53aff642 AD |
124 | /* Labels */ |
125 | ||
126 | if (!$cat_id || $cat_id == -2) { | |
7e2b7e46 AD |
127 | $counters = getLabelCounters($link, false, true); |
128 | ||
129 | foreach (array_keys($counters) as $id) { | |
130 | ||
131 | $unread = $counters[$id]["counter"]; | |
132 | ||
133 | if ($unread || !$unread_only) { | |
134 | ||
135 | $row = array( | |
136 | "id" => $id, | |
137 | "title" => $counters[$id]["description"], | |
138 | "unread" => $counters[$id]["counter"], | |
53aff642 | 139 | "cat_id" => -2, |
7e2b7e46 AD |
140 | ); |
141 | ||
142 | array_push($feeds, $row); | |
143 | } | |
378a548c AD |
144 | } |
145 | } | |
146 | ||
53aff642 AD |
147 | /* Virtual feeds */ |
148 | ||
149 | if (!$cat_id || $cat_id == -1) { | |
150 | foreach (array(-1, -2, -3, -4) as $i) { | |
151 | $unread = getFeedUnread($link, $i); | |
152 | ||
153 | if ($unread || !$unread_only) { | |
154 | $title = getFeedTitle($link, $i); | |
155 | ||
156 | $row = array( | |
157 | "id" => $i, | |
158 | "title" => $title, | |
159 | "unread" => $unread, | |
160 | "cat_id" => -1, | |
161 | ); | |
162 | array_push($feeds, $row); | |
163 | } | |
164 | ||
165 | } | |
166 | } | |
167 | ||
378a548c AD |
168 | print json_encode($feeds); |
169 | ||
170 | break; | |
171 | case "getCategories": | |
730c97c7 AD |
172 | $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]); |
173 | ||
378a548c AD |
174 | $result = db_query($link, "SELECT |
175 | id, title FROM ttrss_feed_categories | |
176 | WHERE owner_uid = " . | |
177 | $_SESSION["uid"]); | |
178 | ||
179 | $cats = array(); | |
180 | ||
181 | while ($line = db_fetch_assoc($result)) { | |
182 | $unread = getFeedUnread($link, $line["id"], true); | |
730c97c7 AD |
183 | |
184 | if ($unread || !$unread_only) { | |
f1c2b672 AD |
185 | array_push($cats, array("id" => $line["id"], |
186 | "title" => $line["title"], | |
187 | "unread" => $unread)); | |
730c97c7 | 188 | } |
378a548c AD |
189 | } |
190 | ||
191 | print json_encode($cats); | |
192 | break; | |
193 | case "getHeadlines": | |
194 | $feed_id = db_escape_string($_REQUEST["feed_id"]); | |
195 | $limit = (int)db_escape_string($_REQUEST["limit"]); | |
196 | $filter = db_escape_string($_REQUEST["filter"]); | |
197 | $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]); | |
198 | $show_except = (bool)db_escape_string($_REQUEST["show_excerpt"]); | |
199 | ||
200 | /* do not rely on params below */ | |
201 | ||
202 | $search = db_escape_string($_REQUEST["search"]); | |
203 | $search_mode = db_escape_string($_REQUEST["search_mode"]); | |
204 | $match_on = db_escape_string($_REQUEST["match_on"]); | |
205 | ||
206 | $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, | |
207 | $view_mode, $is_cat, $search, $search_mode, $match_on); | |
208 | ||
209 | $result = $qfh_ret[0]; | |
210 | $feed_title = $qfh_ret[1]; | |
211 | ||
212 | $headlines = array(); | |
213 | ||
214 | while ($line = db_fetch_assoc($result)) { | |
215 | $is_updated = ($line["last_read"] == "" && | |
216 | ($line["unread"] != "t" && $line["unread"] != "1")); | |
217 | ||
218 | $headline_row = array( | |
219 | "id" => (int)$line["id"], | |
220 | "unread" => sql_bool_to_bool($line["unread"]), | |
221 | "marked" => sql_bool_to_bool($line["marked"]), | |
222 | "updated" => strtotime($line["updated"]), | |
223 | "is_updated" => $is_updated, | |
224 | "title" => $line["title"], | |
225 | "feed_id" => $line["feed_id"], | |
226 | ); | |
227 | ||
228 | if ($show_except) $headline_row["excerpt"] = $line["content_preview"]; | |
229 | ||
230 | array_push($headlines, $headline_row); | |
231 | } | |
232 | ||
233 | print json_encode($headlines); | |
234 | ||
730c97c7 | 235 | break; |
1c3fffbb AD |
236 | case "updateArticle": |
237 | $article_id = (int) db_escape_string($_GET["article_id"]); | |
238 | $mode = (int) db_escape_string($_REQUEST["mode"]); | |
239 | $field_raw = (int)db_escape_string($_REQUEST["field"]); | |
240 | ||
241 | $field = ""; | |
242 | $set_to = ""; | |
243 | ||
244 | switch ($field_raw) { | |
245 | case 0: | |
246 | $field = "marked"; | |
247 | break; | |
248 | case 1: | |
249 | $field = "published"; | |
250 | break; | |
251 | case 2: | |
252 | $field = "unread"; | |
253 | break; | |
254 | }; | |
255 | ||
256 | switch ($mode) { | |
257 | case 1: | |
258 | $set_to = "true"; | |
259 | break; | |
260 | case 0: | |
261 | $set_to = "false"; | |
262 | break; | |
263 | case 2: | |
264 | $set_to = "NOT $field"; | |
265 | break; | |
266 | } | |
267 | ||
268 | if ($field && $set_to) { | |
269 | if ($field == "unread") { | |
270 | $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to, | |
271 | last_read = NOW() | |
272 | WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]); | |
273 | } else { | |
274 | $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to | |
275 | WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]); | |
276 | } | |
277 | } | |
278 | ||
279 | break; | |
280 | ||
730c97c7 AD |
281 | case "getArticle": |
282 | ||
283 | $article_id = (int)db_escape_string($_REQUEST["article_id"]); | |
284 | ||
285 | $query = "SELECT title,link,content,feed_id,comments,int_id, | |
1c3fffbb | 286 | marked,unread,published, |
730c97c7 AD |
287 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, |
288 | author | |
289 | FROM ttrss_entries,ttrss_user_entries | |
290 | WHERE id = '$article_id' AND ref_id = id AND owner_uid = " . | |
291 | $_SESSION["uid"] ; | |
292 | ||
293 | $result = db_query($link, $query); | |
294 | ||
295 | $article = array(); | |
296 | ||
297 | if (db_num_rows($result) != 0) { | |
298 | $line = db_fetch_assoc($result); | |
299 | ||
300 | $article = array( | |
301 | "title" => $line["title"], | |
302 | "link" => $line["link"], | |
53aff642 | 303 | "labels" => get_article_labels($link, $article_id), |
730c97c7 AD |
304 | "unread" => sql_bool_to_bool($line["unread"]), |
305 | "marked" => sql_bool_to_bool($line["marked"]), | |
1c3fffbb | 306 | "published" => sql_bool_to_bool($line["published"]), |
730c97c7 AD |
307 | "comments" => $line["comments"], |
308 | "author" => $line["author"], | |
309 | "updated" => strtotime($line["updated"]), | |
310 | "content" => $line["content"], | |
311 | "feed_id" => $line["feed_id"], | |
312 | ); | |
313 | } | |
314 | ||
315 | print json_encode($article); | |
316 | ||
378a548c AD |
317 | break; |
318 | } | |
319 | ||
320 | db_close($link); | |
321 | ||
322 | ?> |