]> git.wh0rd.org Git - tt-rss.git/blob - xml-rpc.php
add xml-rpc method rss.getCategories
[tt-rss.git] / xml-rpc.php
1 <?
2         require "xmlrpc/lib/xmlrpc.inc";
3         require "xmlrpc/lib/xmlrpcs.inc";
4
5         require_once "sanity_check.php";
6         require_once "config.php";
7         
8         require_once "db.php";
9         require_once "db-prefs.php";
10         require_once "functions.php";
11
12         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
13
14         if (!$link) {
15                 if (DB_TYPE == "mysql") {
16                         print mysql_error();
17                 }
18                 // PG seems to display its own errors just fine by default.             
19                 return;
20         }
21
22         if (DB_TYPE == "pgsql") {
23                 pg_query("set client_encoding = 'utf-8'");
24         }
25
26         function getCategories($msg) {
27                 global $link;
28
29                 $login_o = $msg->getParam(0);
30                 $pass_o = $msg->getParam(1);
31         
32                 $login = $login_o->scalarval();
33                 $pass = $pass_o->scalarval();
34         
35                 $user_id = authenticate_user($link, $login, $pass);
36
37                 $error_code = 0;
38
39                 if (authenticate_user($link, $login, $pass)) {
40
41                         $result = db_query($link, "SELECT 
42                                         id, title FROM ttrss_feed_categories 
43                                 WHERE owner_uid = " . 
44                                 $_SESSION["uid"]);
45
46                         $feeds = array();
47
48                         while ($line = db_fetch_assoc($result)) {
49
50                                 $unread = getFeedUnread($link, $line["id"]);
51                                 
52                                 $line_struct = new xmlrpcval(
53                                         array(
54                                                 "title" => new xmlrpcval($line["title"]),
55                                                 "id" => new xmlrpcval($line["id"], "int")
56                                         ),
57                                         "struct");
58
59                                 array_push($feeds, $line_struct);
60                         }
61
62                         $reply = new xmlrpcval($feeds, "array");
63                         
64                 } else {
65                         $reply = "Login failed.";
66                         $error_code = 1;
67                 }
68         
69                 if ($error_code != 0) {
70                         return new xmlrpcresp(0, $error_code, $reply_msg);
71                 } else {                
72                         return new xmlrpcresp($reply);
73                 }
74
75         }
76
77         function getTotalUnread($msg) {
78                 global $link;
79
80                 $error_code = 0;
81
82                 $login_o = $msg->getParam(0);
83                 $pass_o = $msg->getParam(1);
84         
85                 $login = $login_o->scalarval();
86                 $pass = $pass_o->scalarval();
87         
88                 $user_id = authenticate_user($link, $login, $pass);
89         
90                 
91                 if (authenticate_user($link, $login, $pass)) {
92
93                         $reply_msg = getGlobalUnread($link);
94
95                 } else {
96                         $reply_msg = "Login failed.";
97                         $error_code = 1;
98                 }
99                 
100                 if ($error_code != 0) {
101                         return new xmlrpcresp(0, $error_code, $reply_msg);
102                 } else {                
103                         return new xmlrpcresp(new xmlrpcval($reply_msg));
104                 }
105
106         }
107
108         function getVersion() {
109                 return new xmlrpcval(VERSION);
110         }
111
112         function getSubscribedFeeds($msg) {
113                 global $link;
114
115                 $login_o = $msg->getParam(0);
116                 $pass_o = $msg->getParam(1);
117         
118                 $login = $login_o->scalarval();
119                 $pass = $pass_o->scalarval();
120         
121                 $user_id = authenticate_user($link, $login, $pass);
122
123                 if (authenticate_user($link, $login, $pass)) {
124
125                         $result = db_query($link, "SELECT 
126                                 id, feed_url, cat_id, title, SUBSTRING(last_updated,1,19) AS last_updated
127                                         FROM ttrss_feeds WHERE owner_uid = " . 
128                                 $_SESSION["uid"]);
129
130                         $feeds = array();
131
132                         while ($line = db_fetch_assoc($result)) {
133
134                                 $unread = getFeedUnread($link, $line["id"]);
135                                 
136                                 $line_struct = new xmlrpcval(
137                                         array(
138                                                 "feed_url" => new xmlrpcval($line["feed_url"]),
139                                                 "title" => new xmlrpcval($line["title"]),
140                                                 "id" => new xmlrpcval($line["id"], "int"),
141                                                 "unread" => new xmlrpcval($unread, "int"),
142                                                 "category_id" => new xmlrpcval($line["cat_id"], "int"),
143                                                 "last_updated" => new xmlrpcval(strtotime($line["last_updated"]), "int")
144                                         ),
145                                         "struct");
146
147                                 array_push($feeds, $line_struct);
148                         }
149
150                         $reply = new xmlrpcval($feeds, "array");
151                         
152                 } else {
153                         $reply = new xmlrpcval("Login failed.");
154                 }
155                 
156                 return new xmlrpcresp($reply);
157         }
158
159         function subscribeToFeed($msg) {
160                 global $link;
161
162                 $error_code = 0;
163
164                 $login_o = $msg->getParam(0);
165                 $pass_o = $msg->getParam(1);
166                 $feed_url_o = $msg->getParam(2);
167         
168                 $login = $login_o->scalarval();
169                 $pass = $pass_o->scalarval();
170                 $feed_url = $feed_url_o->scalarval();
171
172                 if (authenticate_user($link, $login, $pass)) {
173                         if (subscribe_to_feed($link, $feed_url)) {
174                                 $reply_msg = "Subscribed successfully.";
175                         } else {
176                                 $reply_msg = "Feed already exists in the database.";
177                                 $error_code = 2;
178                         }               
179                 } else {
180                         $reply_msg = "Login failed.";
181                         $error_code = 1;
182                 }
183         
184                 if ($error_code != 0) {
185                         return new xmlrpcresp(0, $error_code, $reply_msg);
186                 } else {                
187                         return new xmlrpcresp(new xmlrpcval($reply_msg));
188                 }
189         }
190
191         function getFeedHeadlines($msg) {
192                 global $link;
193
194                 $error_code = 0;
195
196                 $login_o = $msg->getParam(0);
197                 $pass_o = $msg->getParam(1);
198                 $feed_id_o = $msg->getParam(2);
199                 $limit_o = $msg->getParam(3);
200                 $filter_o = $msg->getParam(4);
201
202                 $login = $login_o->scalarval();
203                 $pass = $pass_o->scalarval();
204                 $feed_id = $feed_id_o->scalarval();
205                 $limit = $limit_o->scalarval();
206                 $filter = $filter_o->scalarval();
207
208                 if (authenticate_user($link, $login, $pass)) {
209
210                         if ($limit > 0) {
211                                 $limit_query_part = "LIMIT $limit";
212                         }
213
214                         if ($filter == 1) {
215                                 $query_strategy_part = "unread = true";
216                         } else if ($filter == 2) {
217                                 $query_strategy_part = "marked = true";
218                         } else {
219                                 $query_strategy_part = "ttrss_entries.id > 0";
220                         }
221
222                         $query = "SELECT 
223                                         ttrss_entries.id,ttrss_entries.title,
224                                         SUBSTRING(updated,1,16) as updated,
225                                         unread,feed_id,marked,link,last_read,
226                                         SUBSTRING(last_read,1,19) as last_read_noms,
227                                         SUBSTRING(updated,1,19) as updated_noms
228                                 FROM
229                                         ttrss_entries,ttrss_user_entries,ttrss_feeds
230                                 WHERE
231                                 ttrss_feeds.id = '$feed_id' AND
232                                 ttrss_user_entries.feed_id = ttrss_feeds.id AND
233                                 ttrss_user_entries.ref_id = ttrss_entries.id AND
234                                 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
235                                 $query_strategy_part ORDER BY updated 
236                                 $limit_query_part";
237
238                         $result = db_query($link, $query);
239
240                         $articles = array();
241
242                         while ($line = db_fetch_assoc($result)) {
243
244
245                                 $line_struct = new xmlrpcval(
246                                         array(
247                                                 "id" => new xmlrpcval($line["id"], "int"),
248                                                 "unread" => new xmlrpcval(sql_bool_to_bool($line["unread"]), "boolean"),
249                                                 "marked" => new xmlrpcval(sql_bool_to_bool($line["marked"]), "boolean"),
250                                                 "updated" => new xmlrpcval(strtotime($line["updated"]), "int"),
251                                                 "title" => new xmlrpcval($line["title"])
252                                         ),
253                                         "struct");
254
255                                 array_push($articles, $line_struct);
256
257                         }
258
259                         $reply = new xmlrpcval($articles, "array");
260
261                 } else {
262                         $reply_msg = "Login failed.";
263                         $error_code = 1;
264                 }
265
266                 if ($error_code != 0) {
267                         return new xmlrpcresp(0, $error_code, $reply_msg);
268                 } else {                
269                         return new xmlrpcresp($reply);
270                 }
271
272         }
273
274         function getArticle($msg) {
275                 global $link;
276
277                 $error_code = 0;
278
279                 $login_o = $msg->getParam(0);
280                 $pass_o = $msg->getParam(1);
281                 $article_id_o = $msg->getParam(2);
282         
283                 $login = $login_o->scalarval();
284                 $pass = $pass_o->scalarval();
285                 $article_id = $article_id_o->scalarval();
286
287                 if (authenticate_user($link, $login, $pass)) {
288
289                         $query = "SELECT title,link,content,feed_id,comments,int_id,
290                                 marked,unread,
291                                 SUBSTRING(updated,1,16) as updated,
292                                 author
293                                 FROM ttrss_entries,ttrss_user_entries
294                                 WHERE   id = '$article_id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"] ;
295
296                         $result = db_query($link, $query);
297
298                         if (db_num_rows($result) == 1) {
299
300                                 $line = db_fetch_assoc($result);
301
302                                 $reply = new xmlrpcval(
303                                         array(
304                                                 "title" => new xmlrpcval($line["title"]),
305                                                 "link" => new xmlrpcval($line["link"]),
306                                                 "unread" => new xmlrpcval(sql_bool_to_bool($line["unread"]), "boolean"),
307                                                 "marked" => new xmlrpcval(sql_bool_to_bool($line["marked"]), "boolean"),
308                                                 "comments" => new xmlrpcval($line["comments"]),
309                                                 "author" => new xmlrpcval($line["author"]),
310                                                 "updated" => new xmlrpcval(strtotime($line["updated"], "int")),
311                                                 "content" => new xmlrpcval($line["content"])
312                                         ),
313                                         "struct");
314                                 
315                         } else {
316                                 $reply_msg = "Article not found.";
317                                 $error_code = 2;
318                         }
319                 
320                 } else {
321                         $reply_msg = "Login failed.";
322                         $error_code = 1;
323                 }
324         
325                 if ($error_code != 0) {
326                         return new xmlrpcresp(0, $error_code, $reply_msg);
327                 } else {                
328                         return new xmlrpcresp($reply);
329                 }
330         }
331
332         function setArticleMarked($msg) {
333                 global $link;
334
335                 $error_code = 0;
336
337                 $login_o = $msg->getParam(0);
338                 $pass_o = $msg->getParam(1);
339                 $article_id_o = $msg->getParam(2);
340                 $marked_o = $msg->getParam(3);
341
342                 $login = $login_o->scalarval();
343                 $pass = $pass_o->scalarval();
344                 $article_id = $article_id_o->scalarval();
345                 $marked = $marked_o->scalarval();
346
347                 if (authenticate_user($link, $login, $pass)) {
348
349                         if ($marked == 0) {
350                                 $query_strategy_part = "marked = false";
351                         } else if ($marked == 1) {
352                                 $query_strategy_part = "marked = true";
353                         } else if ($marked == 2) {
354                                 $query_strategy_part = "marked = NOT marked";
355                         }
356
357                         $result = db_query($link, "UPDATE ttrss_user_entries SET
358                                 $query_strategy_part WHERE ref_id = '$article_id' AND
359                                 owner_uid = " . $_SESSION["uid"]);
360
361                         if (db_affected_rows($link, $result) == 1) {
362                                 $reply_msg = "OK";
363                         } else {
364                                 $error_code = 2;
365                                 $reply_msg = "Failed to update article.";
366                         }
367
368                 } else {
369                         $reply_msg = "Login failed.";
370                         $error_code = 1;
371                 }
372
373                 if ($error_code != 0) {
374                         return new xmlrpcresp(0, $error_code, $reply_msg);
375                 } else {                
376                         return new xmlrpcresp(new xmlrpcval($reply_msg));
377                 }
378
379         }
380
381         function setArticleRead($msg) {
382                 global $link;
383
384                 $error_code = 0;
385
386                 $login_o = $msg->getParam(0);
387                 $pass_o = $msg->getParam(1);
388                 $article_id_o = $msg->getParam(2);
389                 $read_o = $msg->getParam(3);
390
391                 $login = $login_o->scalarval();
392                 $pass = $pass_o->scalarval();
393                 $article_id = $article_id_o->scalarval();
394                 $read = $read_o->scalarval();
395
396                 if (authenticate_user($link, $login, $pass)) {
397
398                         if ($read == 0) {
399                                 $query_strategy_part = "unread = true";
400                         } else if ($read == 1) {
401                                 $query_strategy_part = "unread = false";
402                         } else if ($read == 2) {
403                                 $query_strategy_part = "unread = NOT unread";
404                         }
405
406                         $result = db_query($link, "UPDATE ttrss_user_entries SET
407                                 $query_strategy_part WHERE ref_id = '$article_id' AND
408                                 owner_uid = " . $_SESSION["uid"]);
409
410                         if (db_affected_rows($link, $result) == 1) {
411                                 $reply_msg = "OK";
412                         } else {
413                                 $error_code = 2;
414                                 $reply_msg = "Failed to update article.";
415                         }
416
417                 } else {
418                         $reply_msg = "Login failed.";
419                         $error_code = 1;
420                 }
421
422                 if ($error_code != 0) {
423                         return new xmlrpcresp(0, $error_code, $reply_msg);
424                 } else {                
425                         return new xmlrpcresp(new xmlrpcval($reply_msg));
426                 }
427
428         }
429
430         $subscribeToFeed_sig = array(array($xmlrpcString,
431                 $xmlrpcString, $xmlrpcString, $xmlrpcString));
432
433         $getSubscribedFeeds_sig = array(array($xmlrpcString,
434                 $xmlrpcString, $xmlrpcString));
435
436         $getFeedHeadlines_sig = array(array($xmlrpcString,
437                 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt, $xmlrpcInt));
438
439         $getArticle_sig = array(array($xmlrpcString,
440                 $xmlrpcString, $xmlrpcString, $xmlrpcInt));
441
442         $setArticleMarked_sig = array(array($xmlrpcString,
443                 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt));
444
445         $setArticleUnread_sig = array(array($xmlrpcString,
446                 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt));
447
448         $getVersion_sig = array(array($xmlrpcString));
449         
450         $getTotalUnread_sig = array(array($xmlrpcInt, $xmlrpcString,
451                 $xmlrpcString));
452
453         $getCategories_sig = array(array($xmlrpcString,
454                 $xmlrpcString, $xmlrpcString));
455
456         $s = new xmlrpc_server( 
457                         array(
458                           "rss.getCategories" => array("function" => "getCategories",
459                                         "signature" => $getCategories_sig),
460                           "rss.getTotalUnread" => array("function" => "getTotalUnread",
461                                         "signature" => $getTotalUnread_sig),
462                           "rss.getVersion" => array("function" => "getVersion",
463                                         "signature" => $getVersion_sig),
464                           "rss.setArticleRead" => array("function" => "setArticleRead",
465                                         "signature" => $setArticleRead_sig),
466                           "rss.setArticleMarked" => array("function" => "setArticleMarked",
467                                         "signature" => $setArticleMarked_sig),
468                           "rss.getArticle" => array("function" => "getArticle",
469                                         "signature" => $getArticle_sig),
470                           "rss.getFeedHeadlines" => array("function" => "getFeedHeadlines",
471                                         "signature" => $getFeedHeadlines_sig),
472                           "rss.getSubscribedFeeds" => array("function" => "getSubscribedFeeds",
473                                         "signature" => $getSubscribedFeeds_sig),
474                           "rss.subscribeToFeed" => array("function" => "subscribeToFeed",
475                                         "signature" => $subscribeToFeed_sig)), 0
476                         );
477         $s->response_charset_encoding = "UTF-8";
478         $s->service();
479 ?>