]> git.wh0rd.org Git - tt-rss.git/blob - api/index.php
move virt feeds counter display to getVirtCounters()
[tt-rss.git] / api / index.php
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 "getCounters":
93
94                         /* TODO */
95
96                         break;
97                 case "getFeeds":
98                         $cat_id = db_escape_string($_REQUEST["cat_id"]);
99                         $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
100                         $limit = (int) db_escape_string($_REQUEST["limit"]);
101                         $offset = (int) db_escape_string($_REQUEST["offset"]);
102
103                         if ($limit) {
104                                 $limit_qpart = "LIMIT $limit OFFSET $offset";
105                         } else {
106                                 $limit_qpart = "";
107                         }
108
109                         if (!$cat_id) {
110                                 $result = db_query($link, "SELECT 
111                                         id, feed_url, cat_id, title, ".
112                                                 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
113                                                 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] . 
114                                                 "ORDER BY cat_id, title " . $limit_qpart);
115                         } else {
116                                 $result = db_query($link, "SELECT 
117                                         id, feed_url, cat_id, title, ".
118                                                 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
119                                                 FROM ttrss_feeds WHERE 
120                                                 cat_id = '$cat_id' AND owner_uid = " . $_SESSION["uid"] . 
121                                                 "ORDER BY cat_id, title " . $limit_qpart);
122                         }
123
124                         $feeds = array();
125
126                         while ($line = db_fetch_assoc($result)) {
127
128                                 $unread = getFeedUnread($link, $line["id"]);
129
130                                 $icon_path = "../" . ICONS_DIR . "/" . $line["id"] . ".ico";
131                                 $has_icon = file_exists($icon_path) && filesize($icon_path) > 0;
132
133                                 if ($unread || !$unread_only) {
134
135                                         $row = array(
136                                                         "feed_url" => $line["feed_url"],
137                                                         "title" => $line["title"],
138                                                         "id" => (int)$line["id"],
139                                                         "unread" => (int)$unread,
140                                                         "has_icon" => $has_icon,
141                                                         "cat_id" => (int)$line["cat_id"],
142                                                         "last_updated" => strtotime($line["last_updated"])
143                                                 );
144         
145                                         array_push($feeds, $row);
146                                 }
147                         }
148
149                         /* Labels */
150
151                         if (!$cat_id || $cat_id == -2) {
152                                 $counters = getLabelCounters($link, false, true);
153
154                                 foreach (array_keys($counters) as $id) {
155
156                                         $unread = $counters[$id]["counter"];
157         
158                                         if ($unread || !$unread_only) {
159         
160                                                 $row = array(
161                                                                 "id" => $id,
162                                                                 "title" => $counters[$id]["description"],
163                                                                 "unread" => $counters[$id]["counter"],
164                                                                 "cat_id" => -2,
165                                                         );
166         
167                                                 array_push($feeds, $row);
168                                         }
169                                 }
170                         }
171
172                         /* Virtual feeds */
173
174                         if (!$cat_id || $cat_id == -1) {
175                                 foreach (array(-1, -2, -3, -4, 0) as $i) {
176                                         $unread = getFeedUnread($link, $i);
177
178                                         if ($unread || !$unread_only) {
179                                                 $title = getFeedTitle($link, $i);
180
181                                                 $row = array(
182                                                                 "id" => $i,
183                                                                 "title" => $title,
184                                                                 "unread" => $unread,
185                                                                 "cat_id" => -1,
186                                                         );
187                                                 array_push($feeds, $row);
188                                         }
189
190                                 }
191                         }
192
193                         print json_encode($feeds);
194
195                         break;
196                 case "getCategories":
197                         $unread_only = (bool)db_escape_string($_REQUEST["unread_only"]);
198
199                         $result = db_query($link, "SELECT 
200                                         id, title FROM ttrss_feed_categories 
201                                 WHERE owner_uid = " . 
202                                 $_SESSION["uid"]);
203
204                         $cats = array();
205
206                         while ($line = db_fetch_assoc($result)) {
207                                 $unread = getFeedUnread($link, $line["id"], true);
208
209                                 if ($unread || !$unread_only) {
210                                         array_push($cats, array("id" => $line["id"],
211                                                 "title" => $line["title"], 
212                                                 "unread" => $unread));
213                                 }
214                         }
215
216                         print json_encode($cats);
217                         break;
218                 case "getHeadlines":
219                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
220                         $limit = (int)db_escape_string($_REQUEST["limit"]);
221                         $offset = (int)db_escape_string($_REQUEST["skip"]);
222                         $filter = db_escape_string($_REQUEST["filter"]);
223                         $is_cat = (bool)db_escape_string($_REQUEST["is_cat"]);
224                         $show_excerpt = (bool)db_escape_string($_REQUEST["show_excerpt"]);
225                         $show_content = (bool)db_escape_string($_REQUEST["show_content"]);
226
227                         /* do not rely on params below */
228
229                         $search = db_escape_string($_REQUEST["search"]);
230                         $search_mode = db_escape_string($_REQUEST["search_mode"]);
231                         $match_on = db_escape_string($_REQUEST["match_on"]);
232                         
233                         $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, 
234                                 $view_mode, $is_cat, $search, $search_mode, $match_on,
235                                 false, $offset);
236
237                         $result = $qfh_ret[0];
238                         $feed_title = $qfh_ret[1];
239
240                         $headlines = array();
241
242                         while ($line = db_fetch_assoc($result)) {
243                                 $is_updated = ($line["last_read"] == "" && 
244                                         ($line["unread"] != "t" && $line["unread"] != "1"));
245
246                                 $headline_row = array(
247                                                 "id" => (int)$line["id"],
248                                                 "unread" => sql_bool_to_bool($line["unread"]),
249                                                 "marked" => sql_bool_to_bool($line["marked"]),
250                                                 "updated" => strtotime($line["updated"]),
251                                                 "is_updated" => $is_updated,
252                                                 "title" => $line["title"],
253                                                 "feed_id" => $line["feed_id"],
254                                         );
255
256                                 if ($show_excerpt) {
257                                         $excerpt = truncate_string(strip_tags($line["content_preview"]), 100);
258                                         $headline_row["excerpt"] = $excerpt;
259                                 }
260
261                                 if ($show_content) {
262                                         $headline_row["content"] = $line["content_preview"];
263                                 }
264
265                                 array_push($headlines, $headline_row);
266                         }
267
268                         print json_encode($headlines);
269
270                         break;
271                 case "updateArticle":
272                         $article_id = (int) db_escape_string($_REQUEST["article_id"]);
273                         $mode = (int) db_escape_string($_REQUEST["mode"]);
274                         $field_raw = (int)db_escape_string($_REQUEST["field"]);
275
276                         $field = "";
277                         $set_to = "";
278
279                         switch ($field_raw) {
280                                 case 0:
281                                         $field = "marked";
282                                         break;
283                                 case 1:
284                                         $field = "published";
285                                         break;
286                                 case 2:
287                                         $field = "unread";
288                                         break;
289                         };
290
291                         switch ($mode) {
292                                 case 1:
293                                         $set_to = "true";
294                                         break;
295                                 case 0:
296                                         $set_to = "false";
297                                         break;
298                                 case 2:
299                                         $set_to = "NOT $field";
300                                         break;
301                         }
302
303                         if ($field && $set_to) {
304                                 if ($field == "unread") {
305                                         $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to,
306                                                 last_read = NOW()
307                                                 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
308                                 } else {
309                                         $result = db_query($link, "UPDATE ttrss_user_entries SET $field = $set_to
310                                                 WHERE ref_id = '$article_id' AND owner_uid = " . $_SESSION["uid"]);
311                                 }
312                         }
313
314                         break;
315
316                 case "getArticle":
317
318                         $article_id = (int)db_escape_string($_REQUEST["article_id"]);
319
320                         $query = "SELECT title,link,content,feed_id,comments,int_id,
321                                 marked,unread,published,
322                                 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
323                                 author
324                                 FROM ttrss_entries,ttrss_user_entries
325                                 WHERE   id = '$article_id' AND ref_id = id AND owner_uid = " . 
326                                         $_SESSION["uid"] ;
327
328                         $result = db_query($link, $query);
329
330                         $article = array();
331                         
332                         if (db_num_rows($result) != 0) {
333                                 $line = db_fetch_assoc($result);
334         
335                                 $article = array(
336                                         "title" => $line["title"],
337                                         "link" => $line["link"],
338                                         "labels" => get_article_labels($link, $article_id),
339                                         "unread" => sql_bool_to_bool($line["unread"]),
340                                         "marked" => sql_bool_to_bool($line["marked"]),
341                                         "published" => sql_bool_to_bool($line["published"]),
342                                         "comments" => $line["comments"],
343                                         "author" => $line["author"],
344                                         "updated" => strtotime($line["updated"]),
345                                         "content" => $line["content"],
346                                         "feed_id" => $line["feed_id"],                  
347                                 );
348                         }
349
350                         print json_encode($article);
351
352                         break;
353                 case "getConfig":
354                         $config = array(
355                                 "icons_dir" => ICONS_DIR,
356                                 "icons_url" => ICONS_URL);
357
358                         if (ENABLE_UPDATE_DAEMON) {
359                                 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
360                         }
361
362                         $result = db_query($link, "SELECT COUNT(*) AS cf FROM
363                                 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
364
365                         $num_feeds = db_fetch_result($result, 0, "cf");
366
367                         $config["num_feeds"] = (int)$num_feeds;
368         
369                         print json_encode($config);
370
371                         break;
372
373                 case "updateFeed":
374                         $feed_id = db_escape_string($_REQUEST["feed_id"]);
375
376                         $result = db_query($link, 
377                                 "SELECT feed_url FROM ttrss_feeds WHERE id = '$feed_id'
378                                         AND owner_uid = " . $_SESSION["uid"]);
379
380                         if (db_num_rows($result) > 0) {                 
381                                 $feed_url = db_fetch_result($result, 0, "feed_url");
382                                 update_rss_feed($link, $feed_url, $feed_id);
383                         }
384
385                         print json_encode(array("status" => "OK"));
386
387                         break;
388
389                 case "getPref":
390                         $pref_name = db_escape_string($_REQUEST["pref_name"]);
391                         print json_encode(array("value" => get_pref($link, $pref_name)));
392                         break;
393
394                 default:
395                         print json_encode(array("error" => 'UNKNOWN_METHOD'));
396                         break;
397
398         }
399
400         db_close($link);
401         
402 ?>