]> git.wh0rd.org Git - tt-rss.git/blob - xml-rpc.php
show tag cloud instead of flat tag list
[tt-rss.git] / xml-rpc.php
1 <?php
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                 pg_set_client_encoding("UNICODE");
25         }
26
27         function getVirtualFeeds($msg) {
28                 global $link;
29
30                 $error_code = 0;
31
32                 $login_o = $msg->getParam(0);
33                 $pass_o = $msg->getParam(1);
34         
35                 $login = $login_o->scalarval();
36                 $pass = $pass_o->scalarval();
37         
38                 $user_id = authenticate_user($link, $login, $pass);
39                 
40                 $counters_ret = array();
41                 
42                 if (authenticate_user($link, $login, $pass)) {
43
44                         $counters = getLabelCounters($link, false, true);
45
46                         foreach (array_keys($counters) as $id) {
47                                 $line_struct = new xmlrpcval(
48                                         array(
49                                                 "id" => new xmlrpcval($id, "int"),
50                                                 "title" => new xmlrpcval($counters[$id]["description"]),
51                                                 "unread" => new xmlrpcval($counters[$id]["counter"], "int")
52                                         ),
53                                         "struct");
54
55                                 array_push($counters_ret, $line_struct);
56                         }
57
58                         $reply = new xmlrpcval($counters_ret, "array");
59
60                 } else {
61                         $reply_msg = "Login failed.";
62                         $error_code = 1;
63                 }
64                 
65                 if ($error_code != 0) {
66                         return new xmlrpcresp(0, $error_code, $reply_msg);
67                 } else {                
68                         return new xmlrpcresp($reply);
69                 } 
70
71         }
72
73         function getCategories($msg) {
74                 global $link;
75
76                 $login_o = $msg->getParam(0);
77                 $pass_o = $msg->getParam(1);
78         
79                 $login = $login_o->scalarval();
80                 $pass = $pass_o->scalarval();
81         
82                 $user_id = authenticate_user($link, $login, $pass);
83
84                 $error_code = 0;
85
86                 if (authenticate_user($link, $login, $pass)) {
87
88                         $result = db_query($link, "SELECT 
89                                         id, title FROM ttrss_feed_categories 
90                                 WHERE owner_uid = " . 
91                                 $_SESSION["uid"]);
92
93                         $feeds = array();
94
95                         while ($line = db_fetch_assoc($result)) {
96
97                                 $unread = getFeedUnread($link, $line["id"]);
98                                 
99                                 $line_struct = new xmlrpcval(
100                                         array(
101                                                 "title" => new xmlrpcval($line["title"]),
102                                                 "id" => new xmlrpcval($line["id"], "int")
103                                         ),
104                                         "struct");
105
106                                 array_push($feeds, $line_struct);
107                         }
108
109                         $reply = new xmlrpcval($feeds, "array");
110                         
111                 } else {
112                         $reply = "Login failed.";
113                         $error_code = 1;
114                 }
115         
116                 if ($error_code != 0) {
117                         return new xmlrpcresp(0, $error_code, $reply_msg);
118                 } else {                
119                         return new xmlrpcresp($reply);
120                 }
121
122         }
123
124         function getTotalUnread($msg) {
125                 global $link;
126
127                 $error_code = 0;
128
129                 $login_o = $msg->getParam(0);
130                 $pass_o = $msg->getParam(1);
131         
132                 $login = $login_o->scalarval();
133                 $pass = $pass_o->scalarval();
134         
135                 $user_id = authenticate_user($link, $login, $pass);
136         
137                 
138                 if (authenticate_user($link, $login, $pass)) {
139
140                         $reply_msg = getGlobalUnread($link);
141
142                 } else {
143                         $reply_msg = "Login failed.";
144                         $error_code = 1;
145                 }
146                 
147                 if ($error_code != 0) {
148                         return new xmlrpcresp(0, $error_code, $reply_msg);
149                 } else {                
150                         return new xmlrpcresp(new xmlrpcval($reply_msg));
151                 }
152
153         }
154
155         function getVersion() {
156                 return new xmlrpcval(VERSION);
157         }
158
159         function getSubscribedFeeds($msg) {
160                 global $link;
161
162                 $login_o = $msg->getParam(0);
163                 $pass_o = $msg->getParam(1);
164         
165                 $login = $login_o->scalarval();
166                 $pass = $pass_o->scalarval();
167         
168                 $user_id = authenticate_user($link, $login, $pass);
169
170                 if (authenticate_user($link, $login, $pass)) {
171
172                         $result = db_query($link, "SELECT 
173                                 id, feed_url, cat_id, title, SUBSTRING(last_updated,1,19) AS last_updated
174                                         FROM ttrss_feeds WHERE owner_uid = " . 
175                                 $_SESSION["uid"]);
176
177                         $feeds = array();
178
179                         while ($line = db_fetch_assoc($result)) {
180
181                                 $unread = getFeedUnread($link, $line["id"]);
182                                 
183                                 $line_struct = new xmlrpcval(
184                                         array(
185                                                 "feed_url" => new xmlrpcval($line["feed_url"]),
186                                                 "title" => new xmlrpcval($line["title"]),
187                                                 "id" => new xmlrpcval($line["id"], "int"),
188                                                 "unread" => new xmlrpcval($unread, "int"),
189                                                 "cat_id" => new xmlrpcval($line["cat_id"], "int"),
190                                                 "last_updated" => new xmlrpcval(strtotime($line["last_updated"]), "int")
191                                         ),
192                                         "struct");
193
194                                 array_push($feeds, $line_struct);
195                         }
196
197                         $reply = new xmlrpcval($feeds, "array");
198                         
199                 } else {
200                         $reply = new xmlrpcval("Login failed.");
201                 }
202                 
203                 return new xmlrpcresp($reply);
204         }
205
206         function subscribeToFeed($msg) {
207                 global $link;
208
209                 $error_code = 0;
210
211                 $login_o = $msg->getParam(0);
212                 $pass_o = $msg->getParam(1);
213                 $feed_url_o = $msg->getParam(2);
214         
215                 $login = $login_o->scalarval();
216                 $pass = $pass_o->scalarval();
217                 $feed_url = $feed_url_o->scalarval();
218
219                 if (authenticate_user($link, $login, $pass)) {
220                         if (subscribe_to_feed($link, $feed_url)) {
221                                 $reply_msg = "Subscribed successfully.";
222                         } else {
223                                 $reply_msg = "Feed already exists in the database.";
224                                 $error_code = 2;
225                         }               
226                 } else {
227                         $reply_msg = "Login failed.";
228                         $error_code = 1;
229                 }
230         
231                 if ($error_code != 0) {
232                         return new xmlrpcresp(0, $error_code, $reply_msg);
233                 } else {                
234                         return new xmlrpcresp(new xmlrpcval($reply_msg));
235                 }
236         }
237
238         function getFeedHeadlines($msg) {
239                 global $link;
240
241                 $error_code = 0;
242
243                 $login_o = $msg->getParam(0);
244                 $pass_o = $msg->getParam(1);
245                 $feed_id_o = $msg->getParam(2);
246                 $limit_o = $msg->getParam(3);
247                 $filter_o = $msg->getParam(4);
248
249                 $login = $login_o->scalarval();
250                 $pass = $pass_o->scalarval();
251                 $feed_id = $feed_id_o->scalarval();
252                 $limit = $limit_o->scalarval();
253                 $filter = $filter_o->scalarval();
254
255                 if (authenticate_user($link, $login, $pass)) {
256
257                         if ($filter == 1) {
258                                 $view_mode = "unread";
259                         } else if ($filter == 2) {
260                                 $view_mode = "marked";
261                         } else if ($filter == 3) {
262                                 $view_mode = "adaptive";
263                         }
264                 
265                         $cat_view = false;
266                         $search = "";
267                         $search_mode = "";
268                         $match_on = "";
269                         
270                         $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, 
271                                 $view_mode, $cat_view, $search, $search_mode, $match_on);
272
273                         $result = $qfh_ret[0];
274                         $feed_title = $qfh_ret[1];
275                                 
276                         $articles = array();
277
278                         while ($line = db_fetch_assoc($result)) {
279
280                                 $is_updated = ($line["last_read"] == "" && ($line["unread"] != "t" && $line["unread"] != "1"));
281
282                                 $headline_items =       array(
283                                                 "id" => new xmlrpcval($line["id"], "int"),
284                                                 "unread" => new xmlrpcval(sql_bool_to_bool($line["unread"]), "boolean"),
285                                                 "marked" => new xmlrpcval(sql_bool_to_bool($line["marked"]), "boolean"),
286                                                 "updated" => new xmlrpcval(strtotime($line["updated"]), "int"),
287                                                 "is_updated" => new xmlrpcval($is_updated, "boolean"),
288
289                                                 "title" => new xmlrpcval($line["title"])
290                                         );
291
292                                 if ($feed_id < 0) {
293                                         $headline_items["feed_id"] = new xmlrpcval($line["feed_id"], "int");
294                                 }
295                         
296                                 $line_struct = new xmlrpcval($headline_items, 
297                                         "struct");
298
299                                 array_push($articles, $line_struct);
300                         }
301
302                         $reply = new xmlrpcval(
303                                 array(
304                                         "title" => new xmlrpcval($feed_title),
305                                         "headlines" => new xmlrpcval($articles, "array")
306                                 ),
307                                 "struct");
308
309                 } else {
310                         $reply_msg = "Login failed.";
311                         $error_code = 1;
312                 }
313
314                 if ($error_code != 0) {
315                         return new xmlrpcresp(0, $error_code, $reply_msg);
316                 } else {                
317                         return new xmlrpcresp($reply);
318                 }
319
320         }
321
322         function getArticle($msg) {
323                 global $link;
324
325                 $error_code = 0;
326
327                 $login_o = $msg->getParam(0);
328                 $pass_o = $msg->getParam(1);
329                 $article_id_o = $msg->getParam(2);
330         
331                 $login = $login_o->scalarval();
332                 $pass = $pass_o->scalarval();
333                 $article_id = $article_id_o->scalarval();
334
335                 if (authenticate_user($link, $login, $pass)) {
336
337                         $query = "SELECT title,link,content,feed_id,comments,int_id,
338                                 marked,unread,
339                                 SUBSTRING(updated,1,16) as updated,
340                                 author
341                                 FROM ttrss_entries,ttrss_user_entries
342                                 WHERE   id = '$article_id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"] ;
343
344                         $result = db_query($link, $query);
345
346                         if (db_num_rows($result) == 1) {
347
348                                 $line = db_fetch_assoc($result);
349
350                                 $reply = new xmlrpcval(
351                                         array(
352                                                 "title" => new xmlrpcval($line["title"]),
353                                                 "link" => new xmlrpcval($line["link"]),
354                                                 "unread" => new xmlrpcval(sql_bool_to_bool($line["unread"]), "boolean"),
355                                                 "marked" => new xmlrpcval(sql_bool_to_bool($line["marked"]), "boolean"),
356                                                 "comments" => new xmlrpcval($line["comments"]),
357                                                 "author" => new xmlrpcval($line["author"]),
358                                                 "updated" => new xmlrpcval(strtotime($line["updated"], "int")),
359                                                 "content" => new xmlrpcval($line["content"])
360                                         ),
361                                         "struct");
362                                 
363                         } else {
364                                 $reply_msg = "Article not found.";
365                                 $error_code = 2;
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($reply);
377                 }
378         }
379
380         function setArticleMarked($msg) {
381                 global $link;
382
383                 $error_code = 0;
384
385                 $login_o = $msg->getParam(0);
386                 $pass_o = $msg->getParam(1);
387                 $article_id_o = $msg->getParam(2);
388                 $marked_o = $msg->getParam(3);
389
390                 $login = $login_o->scalarval();
391                 $pass = $pass_o->scalarval();
392                 $article_id = $article_id_o->scalarval();
393                 $marked = $marked_o->scalarval();
394
395                 if (authenticate_user($link, $login, $pass)) {
396
397                         if ($marked == 0) {
398                                 $query_strategy_part = "marked = false";
399                         } else if ($marked == 1) {
400                                 $query_strategy_part = "marked = true";
401                         } else if ($marked == 2) {
402                                 $query_strategy_part = "marked = NOT marked";
403                         }
404
405                         $result = db_query($link, "UPDATE ttrss_user_entries SET
406                                 $query_strategy_part WHERE ref_id = '$article_id' AND
407                                 owner_uid = " . $_SESSION["uid"]);
408
409                         if (db_affected_rows($link, $result) == 1) {
410                                 $reply_msg = "OK";
411                         } else {
412                                 $error_code = 2;
413                                 $reply_msg = "Failed to update article.";
414                         }
415
416                 } else {
417                         $reply_msg = "Login failed.";
418                         $error_code = 1;
419                 }
420
421                 if ($error_code != 0) {
422                         return new xmlrpcresp(0, $error_code, $reply_msg);
423                 } else {                
424                         return new xmlrpcresp(new xmlrpcval($reply_msg));
425                 }
426
427         }
428
429         function setArticleRead($msg) {
430                 global $link;
431
432                 $error_code = 0;
433
434                 $login_o = $msg->getParam(0);
435                 $pass_o = $msg->getParam(1);
436                 $article_id_o = $msg->getParam(2);
437                 $read_o = $msg->getParam(3);
438
439                 $login = $login_o->scalarval();
440                 $pass = $pass_o->scalarval();
441                 $article_id = $article_id_o->scalarval();
442                 $read = $read_o->scalarval();
443
444                 if (authenticate_user($link, $login, $pass)) {
445
446                         if ($read == 0) {
447                                 $query_strategy_part = "unread = true";
448                         } else if ($read == 1) {
449                                 $query_strategy_part = "unread = false";
450                         } else if ($read == 2) {
451                                 $query_strategy_part = "unread = NOT unread";
452                         }
453
454                         $result = db_query($link, "UPDATE ttrss_user_entries SET
455                                 $query_strategy_part WHERE ref_id = '$article_id' AND
456                                 owner_uid = " . $_SESSION["uid"]);
457
458                         if (db_affected_rows($link, $result) == 1) {
459                                 $reply_msg = "OK";
460                         } else {
461                                 $error_code = 2;
462                                 $reply_msg = "Failed to update article.";
463                         }
464
465                 } else {
466                         $reply_msg = "Login failed.";
467                         $error_code = 1;
468                 }
469
470                 if ($error_code != 0) {
471                         return new xmlrpcresp(0, $error_code, $reply_msg);
472                 } else {                
473                         return new xmlrpcresp(new xmlrpcval($reply_msg));
474                 }
475
476         }
477
478         $subscribeToFeed_sig = array(array($xmlrpcString,
479                 $xmlrpcString, $xmlrpcString, $xmlrpcString));
480
481         $getSubscribedFeeds_sig = array(array($xmlrpcString,
482                 $xmlrpcString, $xmlrpcString));
483
484         $getFeedHeadlines_sig = array(array($xmlrpcString,
485                 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt, $xmlrpcInt));
486
487         $getArticle_sig = array(array($xmlrpcString,
488                 $xmlrpcString, $xmlrpcString, $xmlrpcInt));
489
490         $setArticleMarked_sig = array(array($xmlrpcString,
491                 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt));
492
493         $setArticleUnread_sig = array(array($xmlrpcString,
494                 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt));
495
496         $getVersion_sig = array(array($xmlrpcString));
497         
498         $getTotalUnread_sig = array(array($xmlrpcInt, $xmlrpcString,
499                 $xmlrpcString));
500
501         $getCategories_sig = array(array($xmlrpcString,
502                 $xmlrpcString, $xmlrpcString));
503
504         $getVirtualFeeds_sig = array(array($xmlrpcInt, $xmlrpcString,
505                 $xmlrpcString));
506
507         $s = new xmlrpc_server( 
508                         array(
509                           "rss.getVirtualFeeds" => array("function" => "getVirtualFeeds",
510                                         "signature" => $getVirtualFeeds_sig),
511                           "rss.getCategories" => array("function" => "getCategories",
512                                         "signature" => $getCategories_sig),
513                           "rss.getTotalUnread" => array("function" => "getTotalUnread",
514                                         "signature" => $getTotalUnread_sig),
515                           "rss.getVersion" => array("function" => "getVersion",
516                                         "signature" => $getVersion_sig),
517                           "rss.setArticleRead" => array("function" => "setArticleRead",
518                                         "signature" => $setArticleRead_sig),
519                           "rss.setArticleMarked" => array("function" => "setArticleMarked",
520                                         "signature" => $setArticleMarked_sig),
521                           "rss.getArticle" => array("function" => "getArticle",
522                                         "signature" => $getArticle_sig),
523                           "rss.getFeedHeadlines" => array("function" => "getFeedHeadlines",
524                                         "signature" => $getFeedHeadlines_sig),
525                           "rss.getSubscribedFeeds" => array("function" => "getSubscribedFeeds",
526                                         "signature" => $getSubscribedFeeds_sig),
527                           "rss.subscribeToFeed" => array("function" => "subscribeToFeed",
528                                         "signature" => $subscribeToFeed_sig)), 0
529                         );
530         $s->response_charset_encoding = "UTF-8";
531         $s->service();
532 ?>