2 require "xmlrpc/lib/xmlrpc.inc";
3 require "xmlrpc/lib/xmlrpcs.inc";
5 require_once "sanity_check.php";
6 require_once "config.php";
9 require_once "db-prefs.php";
10 require_once "functions.php";
12 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
15 if (DB_TYPE
== "mysql") {
18 // PG seems to display its own errors just fine by default.
22 if (DB_TYPE
== "pgsql") {
23 pg_query("set client_encoding = 'utf-8'");
26 function getVersion() {
27 return new xmlrpcval(VERSION
);
30 function getSubscribedFeeds($msg) {
33 $login_o = $msg->getParam(0);
34 $pass_o = $msg->getParam(1);
36 $login = $login_o->scalarval();
37 $pass = $pass_o->scalarval();
39 $user_id = authenticate_user($link, $login, $pass);
41 if (authenticate_user($link, $login, $pass)) {
43 $result = db_query($link, "SELECT
44 id, feed_url, title, SUBSTRING(last_updated,1,19) AS last_updated
45 FROM ttrss_feeds WHERE owner_uid = " .
50 while ($line = db_fetch_assoc($result)) {
52 $unread = getFeedUnread($link, $line["id"]);
54 $line_struct = new xmlrpcval(
56 "feed_url" => new xmlrpcval($line["feed_url"]),
57 "title" => new xmlrpcval($line["title"]),
58 "id" => new xmlrpcval($line["id"], "int"),
59 "unread" => new xmlrpcval($unread, "int"),
60 "last_updated" => new xmlrpcval(strtotime($line["last_updated"]), "int")
64 array_push($feeds, $line_struct);
67 $reply = new xmlrpcval($feeds, "array");
70 $reply = new xmlrpcval("Login failed.");
73 return new xmlrpcresp($reply);
76 function subscribeToFeed($msg) {
81 $login_o = $msg->getParam(0);
82 $pass_o = $msg->getParam(1);
83 $feed_url_o = $msg->getParam(2);
85 $login = $login_o->scalarval();
86 $pass = $pass_o->scalarval();
87 $feed_url = $feed_url_o->scalarval();
89 if (authenticate_user($link, $login, $pass)) {
90 if (subscribe_to_feed($link, $feed_url)) {
91 $reply_msg = "Subscribed successfully.";
93 $reply_msg = "Feed already exists in the database.";
97 $reply_msg = "Login failed.";
101 if ($error_code != 0) {
102 return new xmlrpcresp(0, $error_code, $reply_msg);
104 return new xmlrpcresp(new xmlrpcval($reply_msg));
108 function getFeedHeadlines($msg) {
113 $login_o = $msg->getParam(0);
114 $pass_o = $msg->getParam(1);
115 $feed_id_o = $msg->getParam(2);
116 $limit_o = $msg->getParam(3);
117 $filter_o = $msg->getParam(4);
119 $login = $login_o->scalarval();
120 $pass = $pass_o->scalarval();
121 $feed_id = $feed_id_o->scalarval();
122 $limit = $limit_o->scalarval();
123 $filter = $filter_o->scalarval();
125 if (authenticate_user($link, $login, $pass)) {
128 $limit_query_part = "LIMIT $limit";
132 $query_strategy_part = "unread = true";
133 } else if ($filter == 2) {
134 $query_strategy_part = "marked = true";
136 $query_strategy_part = "ttrss_entries.id > 0";
140 ttrss_entries.id,ttrss_entries.title,
141 SUBSTRING(updated,1,16) as updated,
142 unread,feed_id,marked,link,last_read,
143 SUBSTRING(last_read,1,19) as last_read_noms,
144 SUBSTRING(updated,1,19) as updated_noms
146 ttrss_entries,ttrss_user_entries,ttrss_feeds
148 ttrss_feeds.id = '$feed_id' AND
149 ttrss_user_entries.feed_id = ttrss_feeds.id AND
150 ttrss_user_entries.ref_id = ttrss_entries.id AND
151 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
152 $query_strategy_part ORDER BY updated
155 $result = db_query($link, $query);
159 while ($line = db_fetch_assoc($result)) {
162 $line_struct = new xmlrpcval(
164 "id" => new xmlrpcval($line["id"], "int"),
165 "unread" => new xmlrpcval(sql_bool_to_bool($line["unread"]), "boolean"),
166 "marked" => new xmlrpcval(sql_bool_to_bool($line["marked"]), "boolean"),
167 "updated" => new xmlrpcval(strtotime($line["updated"]), "int"),
168 "title" => new xmlrpcval($line["title"])
172 array_push($articles, $line_struct);
176 $reply = new xmlrpcval($articles, "array");
179 $reply_msg = "Login failed.";
183 if ($error_code != 0) {
184 return new xmlrpcresp(0, $error_code, $reply_msg);
186 return new xmlrpcresp($reply);
191 function getArticle($msg) {
196 $login_o = $msg->getParam(0);
197 $pass_o = $msg->getParam(1);
198 $article_id_o = $msg->getParam(2);
200 $login = $login_o->scalarval();
201 $pass = $pass_o->scalarval();
202 $article_id = $article_id_o->scalarval();
204 if (authenticate_user($link, $login, $pass)) {
206 $query = "SELECT title,link,content,feed_id,comments,int_id,
208 SUBSTRING(updated,1,16) as updated,
210 FROM ttrss_entries,ttrss_user_entries
211 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"] ;
213 $result = db_query($link, $query);
215 if (db_num_rows($result) == 1) {
217 $line = db_fetch_assoc($result);
219 $reply = new xmlrpcval(
221 "title" => new xmlrpcval($line["title"]),
222 "link" => new xmlrpcval($line["link"]),
223 "unread" => new xmlrpcval(sql_bool_to_bool($line["unread"]), "boolean"),
224 "marked" => new xmlrpcval(sql_bool_to_bool($line["marked"]), "boolean"),
225 "comments" => new xmlrpcval($line["comments"]),
226 "author" => new xmlrpcval($line["author"]),
227 "updated" => new xmlrpcval(strtotime($line["updated"], "int")),
228 "content" => new xmlrpcval($line["content"])
233 $reply_msg = "Article not found.";
238 $reply_msg = "Login failed.";
242 if ($error_code != 0) {
243 return new xmlrpcresp(0, $error_code, $reply_msg);
245 return new xmlrpcresp($reply);
249 function setArticleMarked($msg) {
254 $login_o = $msg->getParam(0);
255 $pass_o = $msg->getParam(1);
256 $article_id_o = $msg->getParam(2);
257 $marked_o = $msg->getParam(3);
259 $login = $login_o->scalarval();
260 $pass = $pass_o->scalarval();
261 $article_id = $article_id_o->scalarval();
262 $marked = $marked_o->scalarval();
264 if (authenticate_user($link, $login, $pass)) {
267 $query_strategy_part = "marked = false";
268 } else if ($marked == 1) {
269 $query_strategy_part = "marked = true";
270 } else if ($marked == 2) {
271 $query_strategy_part = "marked = NOT marked";
274 $result = db_query($link, "UPDATE ttrss_user_entries SET
275 $query_strategy_part WHERE ref_id = '$article_id' AND
276 owner_uid = " . $_SESSION["uid"]);
278 if (db_affected_rows($link, $result) == 1) {
282 $reply_msg = "Failed to update article.";
286 $reply_msg = "Login failed.";
290 if ($error_code != 0) {
291 return new xmlrpcresp(0, $error_code, $reply_msg);
293 return new xmlrpcresp(new xmlrpcval($reply_msg));
298 function setArticleRead($msg) {
303 $login_o = $msg->getParam(0);
304 $pass_o = $msg->getParam(1);
305 $article_id_o = $msg->getParam(2);
306 $read_o = $msg->getParam(3);
308 $login = $login_o->scalarval();
309 $pass = $pass_o->scalarval();
310 $article_id = $article_id_o->scalarval();
311 $read = $read_o->scalarval();
313 if (authenticate_user($link, $login, $pass)) {
316 $query_strategy_part = "unread = true";
317 } else if ($read == 1) {
318 $query_strategy_part = "unread = false";
319 } else if ($read == 2) {
320 $query_strategy_part = "unread = NOT unread";
323 $result = db_query($link, "UPDATE ttrss_user_entries SET
324 $query_strategy_part WHERE ref_id = '$article_id' AND
325 owner_uid = " . $_SESSION["uid"]);
327 if (db_affected_rows($link, $result) == 1) {
331 $reply_msg = "Failed to update article.";
335 $reply_msg = "Login failed.";
339 if ($error_code != 0) {
340 return new xmlrpcresp(0, $error_code, $reply_msg);
342 return new xmlrpcresp(new xmlrpcval($reply_msg));
347 $subscribeToFeed_sig = array(array($xmlrpcString,
348 $xmlrpcString, $xmlrpcString, $xmlrpcString));
350 $getSubscribedFeeds_sig = array(array($xmlrpcString,
351 $xmlrpcString, $xmlrpcString));
353 $getFeedHeadlines_sig = array(array($xmlrpcString,
354 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt, $xmlrpcInt));
356 $getArticle_sig = array(array($xmlrpcString,
357 $xmlrpcString, $xmlrpcString, $xmlrpcInt));
359 $setArticleMarked_sig = array(array($xmlrpcString,
360 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt));
362 $setArticleUnread_sig = array(array($xmlrpcString,
363 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt));
365 $getVersion_sig = array(array($xmlrpcString));
367 $s = new xmlrpc_server(
369 "rss.getVersion" => array("function" => "getVersion",
370 "signature" => $getVersion_sig),
371 "rss.setArticleRead" => array("function" => "setArticleRead",
372 "signature" => $setArticleRead_sig),
373 "rss.setArticleMarked" => array("function" => "setArticleMarked",
374 "signature" => $setArticleMarked_sig),
375 "rss.getArticle" => array("function" => "getArticle",
376 "signature" => $getArticle_sig),
377 "rss.getFeedHeadlines" => array("function" => "getFeedHeadlines",
378 "signature" => $getFeedHeadlines_sig),
379 "rss.getSubscribedFeeds" => array("function" => "getSubscribedFeeds",
380 "signature" => $getSubscribedFeeds_sig),
381 "rss.subscribeToFeed" => array("function" => "subscribeToFeed",
382 "signature" => $subscribeToFeed_sig)), 0
384 $s->response_charset_encoding
= "UTF-8";