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