]>
Commit | Line | Data |
---|---|---|
956c7629 AD |
1 | <? |
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 | } | |
25 | ||
a3c0ae7c AD |
26 | function getVersion() { |
27 | return new xmlrpcval(VERSION); | |
28 | } | |
29 | ||
7fb32341 AD |
30 | function getSubscribedFeeds($msg) { |
31 | global $link; | |
32 | ||
33 | $login_o = $msg->getParam(0); | |
34 | $pass_o = $msg->getParam(1); | |
35 | ||
36 | $login = $login_o->scalarval(); | |
37 | $pass = $pass_o->scalarval(); | |
38 | ||
39 | $user_id = authenticate_user($link, $login, $pass); | |
40 | ||
41 | if (authenticate_user($link, $login, $pass)) { | |
42 | ||
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 = " . | |
46 | $_SESSION["uid"]); | |
956c7629 | 47 | |
7fb32341 AD |
48 | $feeds = array(); |
49 | ||
50 | while ($line = db_fetch_assoc($result)) { | |
e359d44b AD |
51 | |
52 | $unread = getFeedUnread($link, $line["id"]); | |
7fb32341 AD |
53 | |
54 | $line_struct = new xmlrpcval( | |
55 | array( | |
56 | "feed_url" => new xmlrpcval($line["feed_url"]), | |
57 | "title" => new xmlrpcval($line["title"]), | |
68d3cf5a | 58 | "id" => new xmlrpcval($line["id"], "int"), |
e359d44b | 59 | "unread" => new xmlrpcval($unread, "int"), |
68d3cf5a | 60 | "last_updated" => new xmlrpcval(strtotime($line["last_updated"]), "int") |
7fb32341 AD |
61 | ), |
62 | "struct"); | |
63 | ||
64 | array_push($feeds, $line_struct); | |
65 | } | |
66 | ||
67 | $reply = new xmlrpcval($feeds, "array"); | |
68 | ||
69 | } else { | |
70 | $reply = new xmlrpcval("Login failed."); | |
71 | } | |
72 | ||
73 | return new xmlrpcresp($reply); | |
74 | } | |
75 | ||
76 | function subscribeToFeed($msg) { | |
956c7629 AD |
77 | global $link; |
78 | ||
68d3cf5a AD |
79 | $error_code = 0; |
80 | ||
956c7629 AD |
81 | $login_o = $msg->getParam(0); |
82 | $pass_o = $msg->getParam(1); | |
83 | $feed_url_o = $msg->getParam(2); | |
84 | ||
85 | $login = $login_o->scalarval(); | |
86 | $pass = $pass_o->scalarval(); | |
87 | $feed_url = $feed_url_o->scalarval(); | |
956c7629 AD |
88 | |
89 | if (authenticate_user($link, $login, $pass)) { | |
90 | if (subscribe_to_feed($link, $feed_url)) { | |
91 | $reply_msg = "Subscribed successfully."; | |
92 | } else { | |
93 | $reply_msg = "Feed already exists in the database."; | |
68d3cf5a | 94 | $error_code = 2; |
956c7629 AD |
95 | } |
96 | } else { | |
97 | $reply_msg = "Login failed."; | |
68d3cf5a AD |
98 | $error_code = 1; |
99 | } | |
100 | ||
101 | if ($error_code != 0) { | |
102 | return new xmlrpcresp(0, $error_code, $reply_msg); | |
103 | } else { | |
104 | return new xmlrpcresp(new xmlrpcval($reply_msg)); | |
105 | } | |
106 | } | |
107 | ||
108 | function getFeedHeadlines($msg) { | |
109 | global $link; | |
110 | ||
111 | $error_code = 0; | |
112 | ||
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); | |
118 | ||
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(); | |
124 | ||
125 | if (authenticate_user($link, $login, $pass)) { | |
126 | ||
127 | if ($limit > 0) { | |
128 | $limit_query_part = "LIMIT $limit"; | |
129 | } | |
130 | ||
131 | if ($filter == 1) { | |
132 | $query_strategy_part = "unread = true"; | |
133 | } else if ($filter == 2) { | |
134 | $query_strategy_part = "marked = true"; | |
135 | } else { | |
136 | $query_strategy_part = "ttrss_entries.id > 0"; | |
137 | } | |
138 | ||
139 | $query = "SELECT | |
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 | |
145 | FROM | |
146 | ttrss_entries,ttrss_user_entries,ttrss_feeds | |
147 | WHERE | |
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 | |
153 | $limit_query_part"; | |
154 | ||
155 | $result = db_query($link, $query); | |
156 | ||
157 | $articles = array(); | |
158 | ||
159 | while ($line = db_fetch_assoc($result)) { | |
160 | ||
161 | ||
162 | $line_struct = new xmlrpcval( | |
163 | array( | |
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"]) | |
169 | ), | |
170 | "struct"); | |
171 | ||
172 | array_push($articles, $line_struct); | |
173 | ||
174 | } | |
175 | ||
176 | $reply = new xmlrpcval($articles, "array"); | |
177 | ||
178 | } else { | |
179 | $reply_msg = "Login failed."; | |
180 | $error_code = 1; | |
181 | } | |
182 | ||
183 | if ($error_code != 0) { | |
184 | return new xmlrpcresp(0, $error_code, $reply_msg); | |
185 | } else { | |
186 | return new xmlrpcresp($reply); | |
956c7629 | 187 | } |
68d3cf5a AD |
188 | |
189 | } | |
190 | ||
191 | function getArticle($msg) { | |
192 | global $link; | |
193 | ||
194 | $error_code = 0; | |
195 | ||
196 | $login_o = $msg->getParam(0); | |
197 | $pass_o = $msg->getParam(1); | |
198 | $article_id_o = $msg->getParam(2); | |
199 | ||
200 | $login = $login_o->scalarval(); | |
201 | $pass = $pass_o->scalarval(); | |
202 | $article_id = $article_id_o->scalarval(); | |
203 | ||
204 | if (authenticate_user($link, $login, $pass)) { | |
205 | ||
206 | $query = "SELECT title,link,content,feed_id,comments,int_id, | |
207 | marked,unread, | |
208 | SUBSTRING(updated,1,16) as updated, | |
209 | author | |
210 | FROM ttrss_entries,ttrss_user_entries | |
211 | WHERE id = '$article_id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"] ; | |
212 | ||
213 | $result = db_query($link, $query); | |
214 | ||
215 | if (db_num_rows($result) == 1) { | |
216 | ||
217 | $line = db_fetch_assoc($result); | |
218 | ||
219 | $reply = new xmlrpcval( | |
220 | array( | |
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"]) | |
229 | ), | |
230 | "struct"); | |
231 | ||
232 | } else { | |
233 | $reply_msg = "Article not found."; | |
234 | $error_code = 2; | |
235 | } | |
956c7629 | 236 | |
68d3cf5a AD |
237 | } else { |
238 | $reply_msg = "Login failed."; | |
239 | $error_code = 1; | |
240 | } | |
241 | ||
242 | if ($error_code != 0) { | |
243 | return new xmlrpcresp(0, $error_code, $reply_msg); | |
244 | } else { | |
245 | return new xmlrpcresp($reply); | |
246 | } | |
247 | } | |
248 | ||
249 | function setArticleMarked($msg) { | |
250 | global $link; | |
251 | ||
252 | $error_code = 0; | |
253 | ||
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); | |
258 | ||
259 | $login = $login_o->scalarval(); | |
260 | $pass = $pass_o->scalarval(); | |
261 | $article_id = $article_id_o->scalarval(); | |
262 | $marked = $marked_o->scalarval(); | |
263 | ||
264 | if (authenticate_user($link, $login, $pass)) { | |
265 | ||
266 | if ($marked == 0) { | |
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"; | |
272 | } | |
273 | ||
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"]); | |
277 | ||
278 | if (db_affected_rows($link, $result) == 1) { | |
279 | $reply_msg = "OK"; | |
280 | } else { | |
281 | $error_code = 2; | |
282 | $reply_msg = "Failed to update article."; | |
283 | } | |
284 | ||
285 | } else { | |
286 | $reply_msg = "Login failed."; | |
287 | $error_code = 1; | |
288 | } | |
289 | ||
290 | if ($error_code != 0) { | |
291 | return new xmlrpcresp(0, $error_code, $reply_msg); | |
292 | } else { | |
293 | return new xmlrpcresp(new xmlrpcval($reply_msg)); | |
294 | } | |
295 | ||
296 | } | |
297 | ||
298 | function setArticleRead($msg) { | |
299 | global $link; | |
300 | ||
301 | $error_code = 0; | |
302 | ||
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); | |
307 | ||
308 | $login = $login_o->scalarval(); | |
309 | $pass = $pass_o->scalarval(); | |
310 | $article_id = $article_id_o->scalarval(); | |
311 | $read = $read_o->scalarval(); | |
312 | ||
313 | if (authenticate_user($link, $login, $pass)) { | |
314 | ||
315 | if ($read == 0) { | |
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"; | |
321 | } | |
322 | ||
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"]); | |
326 | ||
327 | if (db_affected_rows($link, $result) == 1) { | |
328 | $reply_msg = "OK"; | |
329 | } else { | |
330 | $error_code = 2; | |
331 | $reply_msg = "Failed to update article."; | |
332 | } | |
333 | ||
334 | } else { | |
335 | $reply_msg = "Login failed."; | |
336 | $error_code = 1; | |
337 | } | |
338 | ||
339 | if ($error_code != 0) { | |
340 | return new xmlrpcresp(0, $error_code, $reply_msg); | |
341 | } else { | |
342 | return new xmlrpcresp(new xmlrpcval($reply_msg)); | |
343 | } | |
344 | ||
956c7629 AD |
345 | } |
346 | ||
347 | $subscribeToFeed_sig = array(array($xmlrpcString, | |
348 | $xmlrpcString, $xmlrpcString, $xmlrpcString)); | |
7fb32341 AD |
349 | |
350 | $getSubscribedFeeds_sig = array(array($xmlrpcString, | |
351 | $xmlrpcString, $xmlrpcString)); | |
352 | ||
68d3cf5a AD |
353 | $getFeedHeadlines_sig = array(array($xmlrpcString, |
354 | $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt, $xmlrpcInt)); | |
355 | ||
356 | $getArticle_sig = array(array($xmlrpcString, | |
357 | $xmlrpcString, $xmlrpcString, $xmlrpcInt)); | |
358 | ||
359 | $setArticleMarked_sig = array(array($xmlrpcString, | |
360 | $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt)); | |
361 | ||
362 | $setArticleUnread_sig = array(array($xmlrpcString, | |
363 | $xmlrpcString, $xmlrpcString, $xmlrpcInt, $xmlrpcInt)); | |
364 | ||
a3c0ae7c AD |
365 | $getVersion_sig = array(array($xmlrpcString)); |
366 | ||
956c7629 AD |
367 | $s = new xmlrpc_server( |
368 | array( | |
a3c0ae7c AD |
369 | "rss.getVersion" => array("function" => "getVersion", |
370 | "signature" => $getVersion_sig), | |
68d3cf5a AD |
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), | |
7fb32341 AD |
379 | "rss.getSubscribedFeeds" => array("function" => "getSubscribedFeeds", |
380 | "signature" => $getSubscribedFeeds_sig), | |
956c7629 | 381 | "rss.subscribeToFeed" => array("function" => "subscribeToFeed", |
e359d44b | 382 | "signature" => $subscribeToFeed_sig)), 0 |
956c7629 | 383 | ); |
e359d44b AD |
384 | $s->response_charset_encoding = "UTF-8"; |
385 | $s->service(); | |
956c7629 | 386 | ?> |