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'");
24 pg_set_client_encoding("UNICODE");
26 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
27 db_query($link, "SET NAMES " . MYSQL_CHARSET);
28 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
32 function getVirtualFeeds($msg) {
37 $login_o = $msg->getParam(0);
38 $pass_o = $msg->getParam(1);
40 $login = $login_o->scalarval();
41 $pass = $pass_o->scalarval();
43 $user_id = authenticate_user($link, $login, $pass);
45 $counters_ret = array();
47 if (authenticate_user($link, $login, $pass)) {
49 $counters = getLabelCounters($link, false, true);
51 foreach (array_keys($counters) as $id) {
52 $line_struct = new xmlrpcval(
54 "id" => new xmlrpcval($id, "int"),
55 "title" => new xmlrpcval($counters[$id]["description"]),
56 "unread" => new xmlrpcval($counters[$id]["counter"], "int")
60 array_push($counters_ret, $line_struct);
63 $reply = new xmlrpcval($counters_ret, "array");
66 $reply_msg = "Login failed.";
70 if ($error_code != 0) {
71 return new xmlrpcresp(0, $error_code, $reply_msg);
73 return new xmlrpcresp($reply);
78 function getCategories($msg) {
81 $login_o = $msg->getParam(0);
82 $pass_o = $msg->getParam(1);
84 $login = $login_o->scalarval();
85 $pass = $pass_o->scalarval();
87 $user_id = authenticate_user($link, $login, $pass);
91 if (authenticate_user($link, $login, $pass)) {
93 $result = db_query($link, "SELECT
94 id, title FROM ttrss_feed_categories
100 while ($line = db_fetch_assoc($result)) {
102 $unread = getFeedUnread($link, $line["id"]);
104 $line_struct = new xmlrpcval(
106 "title" => new xmlrpcval($line["title"]),
107 "id" => new xmlrpcval($line["id"], "int")
111 array_push($feeds, $line_struct);
114 $reply = new xmlrpcval($feeds, "array");
117 $reply = "Login failed.";
121 if ($error_code != 0) {
122 return new xmlrpcresp(0, $error_code, $reply_msg);
124 return new xmlrpcresp($reply);
129 function getTotalUnread($msg) {
134 $login_o = $msg->getParam(0);
135 $pass_o = $msg->getParam(1);
137 $login = $login_o->scalarval();
138 $pass = $pass_o->scalarval();
140 $user_id = authenticate_user($link, $login, $pass);
143 if (authenticate_user($link, $login, $pass)) {
145 $reply_msg = getGlobalUnread($link);
148 $reply_msg = "Login failed.";
152 if ($error_code != 0) {
153 return new xmlrpcresp(0, $error_code, $reply_msg);
155 return new xmlrpcresp(new xmlrpcval($reply_msg));
160 function getVersion() {
161 return new xmlrpcval(VERSION);
164 function getSubscribedFeeds($msg) {
167 $login_o = $msg->getParam(0);
168 $pass_o = $msg->getParam(1);
170 $login = $login_o->scalarval();
171 $pass = $pass_o->scalarval();
173 $user_id = authenticate_user($link, $login, $pass);
175 if (authenticate_user($link, $login, $pass)) {
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 = " .
184 while ($line = db_fetch_assoc($result)) {
186 $unread = getFeedUnread($link, $line["id"]);
188 $line_struct = new xmlrpcval(
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")
199 array_push($feeds, $line_struct);
202 $reply = new xmlrpcval($feeds, "array");
205 $reply = new xmlrpcval("Login failed.");
208 return new xmlrpcresp($reply);
211 function subscribeToFeed($msg) {
216 $login_o = $msg->getParam(0);
217 $pass_o = $msg->getParam(1);
218 $feed_url_o = $msg->getParam(2);
220 $login = $login_o->scalarval();
221 $pass = $pass_o->scalarval();
222 $feed_url = $feed_url_o->scalarval();
224 if (authenticate_user($link, $login, $pass)) {
225 if (subscribe_to_feed($link, $feed_url)) {
226 $reply_msg = "Subscribed successfully.";
228 $reply_msg = "Feed already exists in the database.";
232 $reply_msg = "Login failed.";
236 if ($error_code != 0) {
237 return new xmlrpcresp(0, $error_code, $reply_msg);
239 return new xmlrpcresp(new xmlrpcval($reply_msg));
243 function getFeedHeadlines($msg) {
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);
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();
260 if (authenticate_user($link, $login, $pass)) {
263 $view_mode = "unread";
264 } else if ($filter == 2) {
265 $view_mode = "marked";
266 } else if ($filter == 3) {
267 $view_mode = "adaptive";
275 $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit,
276 $view_mode, $cat_view, $search, $search_mode, $match_on);
278 $result = $qfh_ret[0];
279 $feed_title = $qfh_ret[1];
283 while ($line = db_fetch_assoc($result)) {
285 $is_updated = ($line["last_read"] == "" && ($line["unread"] != "t" && $line["unread"] != "1"));
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"),
294 "title" => new xmlrpcval($line["title"])
298 $headline_items["feed_id"] = new xmlrpcval($line["feed_id"], "int");
301 $line_struct = new xmlrpcval($headline_items,
304 array_push($articles, $line_struct);
307 $reply = new xmlrpcval(
309 "title" => new xmlrpcval($feed_title),
310 "headlines" => new xmlrpcval($articles, "array")
315 $reply_msg = "Login failed.";
319 if ($error_code != 0) {
320 return new xmlrpcresp(0, $error_code, $reply_msg);
322 return new xmlrpcresp($reply);
327 function getArticle($msg) {
332 $login_o = $msg->getParam(0);
333 $pass_o = $msg->getParam(1);
334 $article_id_o = $msg->getParam(2);
336 $login = $login_o->scalarval();
337 $pass = $pass_o->scalarval();
338 $article_id = $article_id_o->scalarval();
340 if (authenticate_user($link, $login, $pass)) {
342 $query = "SELECT title,link,content,feed_id,comments,int_id,
344 SUBSTRING(updated,1,16) as updated,
346 FROM ttrss_entries,ttrss_user_entries
347 WHERE id = '$article_id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"] ;
349 $result = db_query($link, $query);
351 if (db_num_rows($result) == 1) {
353 $line = db_fetch_assoc($result);
355 $reply = new xmlrpcval(
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"])
369 $reply_msg = "Article not found.";
374 $reply_msg = "Login failed.";
378 if ($error_code != 0) {
379 return new xmlrpcresp(0, $error_code, $reply_msg);
381 return new xmlrpcresp($reply);
385 function setArticleMarked($msg) {
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);
395 $login = $login_o->scalarval();
396 $pass = $pass_o->scalarval();
397 $article_id = $article_id_o->scalarval();
398 $marked = $marked_o->scalarval();
400 if (authenticate_user($link, $login, $pass)) {
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";
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"]);
414 if (db_affected_rows($link, $result) == 1) {
418 $reply_msg = "Failed to update article.";
422 $reply_msg = "Login failed.";
426 if ($error_code != 0) {
427 return new xmlrpcresp(0, $error_code, $reply_msg);
429 return new xmlrpcresp(new xmlrpcval($reply_msg));
434 function setArticleRead($msg) {
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);
444 $login = $login_o->scalarval();
445 $pass = $pass_o->scalarval();
446 $article_id = $article_id_o->scalarval();
447 $read = $read_o->scalarval();
449 if (authenticate_user($link, $login, $pass)) {
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";
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"]);
463 if (db_affected_rows($link, $result) == 1) {
467 $reply_msg = "Failed to update article.";
471 $reply_msg = "Login failed.";
475 if ($error_code != 0) {
476 return new xmlrpcresp(0, $error_code, $reply_msg);
478 return new xmlrpcresp(new xmlrpcval($reply_msg));
483 $subscribeToFeed_sig = array(array($xmlrpcString,
484 $xmlrpcString, $xmlrpcString, $xmlrpcString));
486 $getSubscribedFeeds_sig = array(array($xmlrpcString,
487 $xmlrpcString, $xmlrpcString));
489 $getFeedHeadlines_sig = array(array($xmlrpcString,
490 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt, $xmlrpcInt));
492 $getArticle_sig = array(array($xmlrpcString,
493 $xmlrpcString, $xmlrpcString, $xmlrpcInt));
495 $setArticleMarked_sig = array(array($xmlrpcString,
496 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt));
498 $setArticleUnread_sig = array(array($xmlrpcString,
499 $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt));
501 $getVersion_sig = array(array($xmlrpcString));
503 $getTotalUnread_sig = array(array($xmlrpcInt, $xmlrpcString,
506 $getCategories_sig = array(array($xmlrpcString,
507 $xmlrpcString, $xmlrpcString));
509 $getVirtualFeeds_sig = array(array($xmlrpcInt, $xmlrpcString,
512 $s = new xmlrpc_server(
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
535 $s->response_charset_encoding = "UTF-8";