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