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