]>
Commit | Line | Data |
---|---|---|
de8260cb AD |
1 | <?php |
2 | ||
3 | class API extends Handler { | |
4 | ||
efc6553d | 5 | const API_LEVEL = 5; |
de8260cb AD |
6 | |
7 | const STATUS_OK = 0; | |
8 | const STATUS_ERR = 1; | |
9 | ||
10 | private $seq; | |
11 | ||
12 | function before($method) { | |
13 | if (parent::before($method)) { | |
12f31782 | 14 | header("Content-Type: text/json"); |
de8260cb AD |
15 | |
16 | if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") { | |
17 | print $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN')); | |
18 | return false; | |
19 | } | |
20 | ||
21 | if ($_SESSION["uid"] && $method != "logout" && !get_pref($this->link, 'ENABLE_API_ACCESS')) { | |
22 | print $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED')); | |
23 | return false; | |
24 | } | |
25 | ||
26 | $this->seq = (int) $_REQUEST['seq']; | |
27 | ||
de8260cb AD |
28 | return true; |
29 | } | |
30 | return false; | |
31 | } | |
32 | ||
33 | function wrap($status, $reply) { | |
34 | print json_encode(array("seq" => $this->seq, | |
35 | "status" => $status, | |
36 | "content" => $reply)); | |
37 | } | |
38 | ||
39 | function getVersion() { | |
40 | $rv = array("version" => VERSION); | |
41 | print $this->wrap(self::STATUS_OK, $rv); | |
42 | } | |
43 | ||
44 | function getApiLevel() { | |
a3b5394a | 45 | $rv = array("level" => self::API_LEVEL); |
de8260cb AD |
46 | print $this->wrap(self::STATUS_OK, $rv); |
47 | } | |
48 | ||
49 | function login() { | |
79bb5589 | 50 | @session_destroy(); |
5160620c AD |
51 | @session_start(); |
52 | ||
3972bf59 | 53 | $login = db_escape_string($this->link, $_REQUEST["user"]); |
de8260cb AD |
54 | $password = $_REQUEST["password"]; |
55 | $password_base64 = base64_decode($_REQUEST["password"]); | |
56 | ||
57 | if (SINGLE_USER_MODE) $login = "admin"; | |
58 | ||
59 | $result = db_query($this->link, "SELECT id FROM ttrss_users WHERE login = '$login'"); | |
60 | ||
61 | if (db_num_rows($result) != 0) { | |
62 | $uid = db_fetch_result($result, 0, "id"); | |
63 | } else { | |
64 | $uid = 0; | |
65 | } | |
66 | ||
67 | if (!$uid) { | |
68 | print $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); | |
69 | return; | |
70 | } | |
71 | ||
72 | if (get_pref($this->link, "ENABLE_API_ACCESS", $uid)) { | |
73 | if (authenticate_user($this->link, $login, $password)) { // try login with normal password | |
5ba4ebc6 AD |
74 | print $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
75 | "api_level" => self::API_LEVEL)); | |
de8260cb | 76 | } else if (authenticate_user($this->link, $login, $password_base64)) { // else try with base64_decoded password |
5ba4ebc6 AD |
77 | print $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
78 | "api_level" => self::API_LEVEL)); | |
de8260cb AD |
79 | } else { // else we are not logged in |
80 | print $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); | |
81 | } | |
82 | } else { | |
83 | print $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED")); | |
84 | } | |
85 | ||
86 | } | |
87 | ||
88 | function logout() { | |
89 | logout_user(); | |
90 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
91 | } | |
92 | ||
93 | function isLoggedIn() { | |
94 | print $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != '')); | |
95 | } | |
96 | ||
97 | function getUnread() { | |
3972bf59 AD |
98 | $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]); |
99 | $is_cat = db_escape_string($this->link, $_REQUEST["is_cat"]); | |
de8260cb AD |
100 | |
101 | if ($feed_id) { | |
102 | print $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($this->link, $feed_id, $is_cat))); | |
103 | } else { | |
104 | print $this->wrap(self::STATUS_OK, array("unread" => getGlobalUnread($this->link))); | |
105 | } | |
106 | } | |
107 | ||
108 | /* Method added for ttrss-reader for Android */ | |
109 | function getCounters() { | |
5b55e9e2 | 110 | print $this->wrap(self::STATUS_OK, getAllCounters($this->link)); |
de8260cb AD |
111 | } |
112 | ||
113 | function getFeeds() { | |
3972bf59 | 114 | $cat_id = db_escape_string($this->link, $_REQUEST["cat_id"]); |
9955a134 | 115 | $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]); |
3972bf59 AD |
116 | $limit = (int) db_escape_string($this->link, $_REQUEST["limit"]); |
117 | $offset = (int) db_escape_string($this->link, $_REQUEST["offset"]); | |
9955a134 | 118 | $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]); |
de8260cb | 119 | |
04f60eb7 | 120 | $feeds = $this->api_get_feeds($this->link, $cat_id, $unread_only, $limit, $offset, $include_nested); |
de8260cb AD |
121 | |
122 | print $this->wrap(self::STATUS_OK, $feeds); | |
123 | } | |
124 | ||
125 | function getCategories() { | |
9955a134 AD |
126 | $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]); |
127 | $enable_nested = sql_bool_to_bool($_REQUEST["enable_nested"]); | |
de8260cb AD |
128 | |
129 | // TODO do not return empty categories, return Uncategorized and standard virtual cats | |
130 | ||
48646336 AD |
131 | if ($enable_nested) |
132 | $nested_qpart = "parent_cat IS NULL"; | |
133 | else | |
134 | $nested_qpart = "true"; | |
135 | ||
de8260cb | 136 | $result = db_query($this->link, "SELECT |
d49dfa38 AD |
137 | id, title, order_id, (SELECT COUNT(id) FROM |
138 | ttrss_feeds WHERE | |
7be3fcd5 AD |
139 | ttrss_feed_categories.id IS NOT NULL AND cat_id = ttrss_feed_categories.id) AS num_feeds, |
140 | (SELECT COUNT(id) FROM | |
141 | ttrss_feed_categories AS c2 WHERE | |
142 | c2.parent_cat = ttrss_feed_categories.id) AS num_cats | |
d49dfa38 | 143 | FROM ttrss_feed_categories |
48646336 | 144 | WHERE $nested_qpart AND owner_uid = " . |
de8260cb AD |
145 | $_SESSION["uid"]); |
146 | ||
147 | $cats = array(); | |
148 | ||
149 | while ($line = db_fetch_assoc($result)) { | |
7be3fcd5 | 150 | if ($line["num_feeds"] > 0 || $line["num_cats"] > 0) { |
d49dfa38 AD |
151 | $unread = getFeedUnread($this->link, $line["id"], true); |
152 | ||
153 | if ($enable_nested) | |
154 | $unread += getCategoryChildrenUnread($this->link, $line["id"]); | |
155 | ||
156 | if ($unread || !$unread_only) { | |
157 | array_push($cats, array("id" => $line["id"], | |
158 | "title" => $line["title"], | |
159 | "unread" => $unread, | |
160 | "order_id" => (int) $line["order_id"], | |
161 | )); | |
162 | } | |
de8260cb AD |
163 | } |
164 | } | |
165 | ||
166 | foreach (array(-2,-1,0) as $cat_id) { | |
167 | $unread = getFeedUnread($this->link, $cat_id, true); | |
168 | ||
169 | if ($unread || !$unread_only) { | |
170 | array_push($cats, array("id" => $cat_id, | |
171 | "title" => getCategoryTitle($this->link, $cat_id), | |
172 | "unread" => $unread)); | |
173 | } | |
174 | } | |
175 | ||
176 | print $this->wrap(self::STATUS_OK, $cats); | |
177 | } | |
178 | ||
179 | function getHeadlines() { | |
3972bf59 | 180 | $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]); |
8aa3becc | 181 | if ($feed_id != "") { |
de8260cb | 182 | |
3972bf59 | 183 | $limit = (int)db_escape_string($this->link, $_REQUEST["limit"]); |
1a740cf6 AD |
184 | |
185 | if (!$limit || $limit >= 60) $limit = 60; | |
186 | ||
3972bf59 AD |
187 | $offset = (int)db_escape_string($this->link, $_REQUEST["skip"]); |
188 | $filter = db_escape_string($this->link, $_REQUEST["filter"]); | |
9955a134 AD |
189 | $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]); |
190 | $show_excerpt = sql_bool_to_bool($_REQUEST["show_excerpt"]); | |
191 | $show_content = sql_bool_to_bool($_REQUEST["show_content"]); | |
de8260cb | 192 | /* all_articles, unread, adaptive, marked, updated */ |
3972bf59 | 193 | $view_mode = db_escape_string($this->link, $_REQUEST["view_mode"]); |
9955a134 | 194 | $include_attachments = sql_bool_to_bool($_REQUEST["include_attachments"]); |
3972bf59 | 195 | $since_id = (int)db_escape_string($this->link, $_REQUEST["since_id"]); |
9955a134 | 196 | $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]); |
db9e00e3 | 197 | $sanitize_content = true; |
de8260cb | 198 | |
3e4af5b0 AD |
199 | /* do not rely on params below */ |
200 | ||
3972bf59 AD |
201 | $search = db_escape_string($this->link, $_REQUEST["search"]); |
202 | $search_mode = db_escape_string($this->link, $_REQUEST["search_mode"]); | |
3e4af5b0 | 203 | |
04f60eb7 | 204 | $headlines = $this->api_get_headlines($this->link, $feed_id, $limit, $offset, |
de8260cb | 205 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, false, |
ca5d9be4 | 206 | $include_attachments, $since_id, $search, $search_mode, |
db9e00e3 | 207 | $include_nested, $sanitize_content); |
de8260cb AD |
208 | |
209 | print $this->wrap(self::STATUS_OK, $headlines); | |
210 | } else { | |
211 | print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
212 | } | |
213 | } | |
214 | ||
215 | function updateArticle() { | |
3972bf59 AD |
216 | $article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric); |
217 | $mode = (int) db_escape_string($this->link, $_REQUEST["mode"]); | |
218 | $data = db_escape_string($this->link, $_REQUEST["data"]); | |
219 | $field_raw = (int)db_escape_string($this->link, $_REQUEST["field"]); | |
de8260cb AD |
220 | |
221 | $field = ""; | |
222 | $set_to = ""; | |
223 | ||
224 | switch ($field_raw) { | |
225 | case 0: | |
226 | $field = "marked"; | |
7873d588 | 227 | $additional_fields = ",last_marked = NOW()"; |
de8260cb AD |
228 | break; |
229 | case 1: | |
230 | $field = "published"; | |
7873d588 | 231 | $additional_fields = ",last_published = NOW()"; |
de8260cb AD |
232 | break; |
233 | case 2: | |
234 | $field = "unread"; | |
7873d588 | 235 | $additional_fields = ",last_read = NOW()"; |
de8260cb AD |
236 | break; |
237 | case 3: | |
238 | $field = "note"; | |
239 | }; | |
240 | ||
241 | switch ($mode) { | |
242 | case 1: | |
243 | $set_to = "true"; | |
244 | break; | |
245 | case 0: | |
246 | $set_to = "false"; | |
247 | break; | |
248 | case 2: | |
249 | $set_to = "NOT $field"; | |
250 | break; | |
251 | } | |
252 | ||
253 | if ($field == "note") $set_to = "'$data'"; | |
254 | ||
255 | if ($field && $set_to && count($article_ids) > 0) { | |
256 | ||
257 | $article_ids = join(", ", $article_ids); | |
258 | ||
7873d588 | 259 | $result = db_query($this->link, "UPDATE ttrss_user_entries SET $field = $set_to $additional_fields WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]); |
de8260cb AD |
260 | |
261 | $num_updated = db_affected_rows($this->link, $result); | |
262 | ||
263 | if ($num_updated > 0 && $field == "unread") { | |
264 | $result = db_query($this->link, "SELECT DISTINCT feed_id FROM ttrss_user_entries | |
265 | WHERE ref_id IN ($article_ids)"); | |
266 | ||
267 | while ($line = db_fetch_assoc($result)) { | |
268 | ccache_update($this->link, $line["feed_id"], $_SESSION["uid"]); | |
269 | } | |
270 | } | |
271 | ||
7873d588 AD |
272 | if ($num_updated > 0 && $field == "published") { |
273 | if (PUBSUBHUBBUB_HUB) { | |
274 | $rss_link = get_self_url_prefix() . | |
275 | "/public.php?op=rss&id=-2&key=" . | |
276 | get_feed_access_key($this->link, -2, false); | |
277 | ||
278 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
279 | $pubsub_result = $p->publish_update($rss_link); | |
280 | } | |
281 | } | |
282 | ||
de8260cb AD |
283 | print $this->wrap(self::STATUS_OK, array("status" => "OK", |
284 | "updated" => $num_updated)); | |
285 | ||
286 | } else { | |
287 | print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
288 | } | |
289 | ||
290 | } | |
291 | ||
292 | function getArticle() { | |
293 | ||
3972bf59 | 294 | $article_id = join(",", array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_id"])), is_numeric)); |
de8260cb | 295 | |
87764a50 | 296 | $query = "SELECT id,title,link,content,cached_content,feed_id,comments,int_id, |
de8260cb AD |
297 | marked,unread,published, |
298 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, | |
299 | author | |
300 | FROM ttrss_entries,ttrss_user_entries | |
301 | WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " . | |
302 | $_SESSION["uid"] ; | |
303 | ||
304 | $result = db_query($this->link, $query); | |
305 | ||
306 | $articles = array(); | |
307 | ||
308 | if (db_num_rows($result) != 0) { | |
309 | ||
310 | while ($line = db_fetch_assoc($result)) { | |
311 | ||
312 | $attachments = get_article_enclosures($this->link, $line['id']); | |
313 | ||
314 | $article = array( | |
315 | "id" => $line["id"], | |
316 | "title" => $line["title"], | |
317 | "link" => $line["link"], | |
318 | "labels" => get_article_labels($this->link, $line['id']), | |
319 | "unread" => sql_bool_to_bool($line["unread"]), | |
320 | "marked" => sql_bool_to_bool($line["marked"]), | |
321 | "published" => sql_bool_to_bool($line["published"]), | |
322 | "comments" => $line["comments"], | |
323 | "author" => $line["author"], | |
ef3da31c | 324 | "updated" => (int) strtotime($line["updated"]), |
87764a50 | 325 | "content" => $line["cached_content"] != "" ? $line["cached_content"] : $line["content"], |
de8260cb AD |
326 | "feed_id" => $line["feed_id"], |
327 | "attachments" => $attachments | |
328 | ); | |
329 | ||
330 | array_push($articles, $article); | |
331 | ||
332 | } | |
333 | } | |
334 | ||
335 | print $this->wrap(self::STATUS_OK, $articles); | |
336 | ||
337 | } | |
338 | ||
339 | function getConfig() { | |
340 | $config = array( | |
341 | "icons_dir" => ICONS_DIR, | |
342 | "icons_url" => ICONS_URL); | |
343 | ||
344 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); | |
345 | ||
346 | $result = db_query($this->link, "SELECT COUNT(*) AS cf FROM | |
347 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
348 | ||
349 | $num_feeds = db_fetch_result($result, 0, "cf"); | |
350 | ||
351 | $config["num_feeds"] = (int)$num_feeds; | |
352 | ||
353 | print $this->wrap(self::STATUS_OK, $config); | |
354 | } | |
355 | ||
356 | function updateFeed() { | |
c1f6e5f8 AD |
357 | require_once "include/rssfuncs.php"; |
358 | ||
359 | $feed_id = (int) db_escape_string($this->link, $_REQUEST["feed_id"]); | |
de8260cb AD |
360 | |
361 | update_rss_feed($this->link, $feed_id, true); | |
362 | ||
363 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
364 | } | |
365 | ||
366 | function catchupFeed() { | |
3972bf59 AD |
367 | $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]); |
368 | $is_cat = db_escape_string($this->link, $_REQUEST["is_cat"]); | |
de8260cb AD |
369 | |
370 | catchup_feed($this->link, $feed_id, $is_cat); | |
371 | ||
372 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
373 | } | |
374 | ||
375 | function getPref() { | |
3972bf59 | 376 | $pref_name = db_escape_string($this->link, $_REQUEST["pref_name"]); |
de8260cb AD |
377 | |
378 | print $this->wrap(self::STATUS_OK, array("value" => get_pref($this->link, $pref_name))); | |
379 | } | |
380 | ||
ea1c2903 | 381 | function getLabels() { |
3972bf59 | 382 | //$article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric); |
ea1c2903 AD |
383 | |
384 | $article_id = (int)$_REQUEST['article_id']; | |
385 | ||
386 | $rv = array(); | |
387 | ||
388 | $result = db_query($this->link, "SELECT id, caption, fg_color, bg_color | |
389 | FROM ttrss_labels2 | |
390 | WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption"); | |
391 | ||
392 | if ($article_id) | |
393 | $article_labels = get_article_labels($this->link, $article_id); | |
394 | else | |
395 | $article_labels = array(); | |
396 | ||
397 | while ($line = db_fetch_assoc($result)) { | |
398 | ||
399 | $checked = false; | |
400 | foreach ($article_labels as $al) { | |
401 | if ($al[0] == $line['id']) { | |
402 | $checked = true; | |
403 | break; | |
404 | } | |
405 | } | |
406 | ||
407 | array_push($rv, array( | |
408 | "id" => (int)$line['id'], | |
409 | "caption" => $line['caption'], | |
410 | "fg_color" => $line['fg_color'], | |
411 | "bg_color" => $line['bg_color'], | |
412 | "checked" => $checked)); | |
413 | } | |
414 | ||
415 | print $this->wrap(self::STATUS_OK, $rv); | |
416 | } | |
417 | ||
396bfdf9 AD |
418 | function setArticleLabel() { |
419 | ||
3972bf59 AD |
420 | $article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric); |
421 | $label_id = (int) db_escape_string($this->link, $_REQUEST['label_id']); | |
422 | $assign = (bool) db_escape_string($this->link, $_REQUEST['assign']) == "true"; | |
396bfdf9 | 423 | |
3972bf59 | 424 | $label = db_escape_string($this->link, label_find_caption($this->link, |
396bfdf9 AD |
425 | $label_id, $_SESSION["uid"])); |
426 | ||
427 | $num_updated = 0; | |
428 | ||
429 | if ($label) { | |
430 | ||
431 | foreach ($article_ids as $id) { | |
432 | ||
433 | if ($assign) | |
434 | label_add_article($this->link, $id, $label, $_SESSION["uid"]); | |
435 | else | |
436 | label_remove_article($this->link, $id, $label, $_SESSION["uid"]); | |
437 | ||
438 | ++$num_updated; | |
439 | ||
440 | } | |
441 | } | |
442 | ||
443 | print $this->wrap(self::STATUS_OK, array("status" => "OK", | |
444 | "updated" => $num_updated)); | |
445 | ||
446 | } | |
447 | ||
de8260cb AD |
448 | function index() { |
449 | print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD')); | |
450 | } | |
451 | ||
8361e724 | 452 | function shareToPublished() { |
3972bf59 AD |
453 | $title = db_escape_string($this->link, strip_tags($_REQUEST["title"])); |
454 | $url = db_escape_string($this->link, strip_tags($_REQUEST["url"])); | |
455 | $content = db_escape_string($this->link, strip_tags($_REQUEST["content"])); | |
8361e724 | 456 | |
50832719 | 457 | if (Article::create_published_article($this->link, $title, $url, $content, "", $_SESSION["uid"])) { |
8361e724 AD |
458 | print $this->wrap(self::STATUS_OK, array("status" => 'OK')); |
459 | } else { | |
460 | print $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed')); | |
461 | } | |
462 | } | |
04f60eb7 AD |
463 | |
464 | static function api_get_feeds($link, $cat_id, $unread_only, $limit, $offset, $include_nested = false) { | |
465 | ||
466 | $feeds = array(); | |
467 | ||
468 | /* Labels */ | |
469 | ||
470 | if ($cat_id == -4 || $cat_id == -2) { | |
471 | $counters = getLabelCounters($link, true); | |
472 | ||
473 | foreach (array_values($counters) as $cv) { | |
474 | ||
475 | $unread = $cv["counter"]; | |
476 | ||
477 | if ($unread || !$unread_only) { | |
478 | ||
479 | $row = array( | |
480 | "id" => $cv["id"], | |
481 | "title" => $cv["description"], | |
482 | "unread" => $cv["counter"], | |
483 | "cat_id" => -2, | |
484 | ); | |
485 | ||
486 | array_push($feeds, $row); | |
487 | } | |
488 | } | |
489 | } | |
490 | ||
491 | /* Virtual feeds */ | |
492 | ||
493 | if ($cat_id == -4 || $cat_id == -1) { | |
494 | foreach (array(-1, -2, -3, -4, -6, 0) as $i) { | |
495 | $unread = getFeedUnread($link, $i); | |
496 | ||
497 | if ($unread || !$unread_only) { | |
498 | $title = getFeedTitle($link, $i); | |
499 | ||
500 | $row = array( | |
501 | "id" => $i, | |
502 | "title" => $title, | |
503 | "unread" => $unread, | |
504 | "cat_id" => -1, | |
505 | ); | |
506 | array_push($feeds, $row); | |
507 | } | |
508 | ||
509 | } | |
510 | } | |
511 | ||
512 | /* Child cats */ | |
513 | ||
514 | if ($include_nested && $cat_id) { | |
515 | $result = db_query($link, "SELECT | |
516 | id, title FROM ttrss_feed_categories | |
517 | WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] . | |
518 | " ORDER BY id, title"); | |
519 | ||
520 | while ($line = db_fetch_assoc($result)) { | |
521 | $unread = getFeedUnread($link, $line["id"], true) + | |
522 | getCategoryChildrenUnread($link, $line["id"]); | |
523 | ||
524 | if ($unread || !$unread_only) { | |
525 | $row = array( | |
526 | "id" => $line["id"], | |
527 | "title" => $line["title"], | |
528 | "unread" => $unread, | |
529 | "is_cat" => true, | |
530 | ); | |
531 | array_push($feeds, $row); | |
532 | } | |
533 | } | |
534 | } | |
535 | ||
536 | /* Real feeds */ | |
537 | ||
538 | if ($limit) { | |
539 | $limit_qpart = "LIMIT $limit OFFSET $offset"; | |
540 | } else { | |
541 | $limit_qpart = ""; | |
542 | } | |
543 | ||
544 | if ($cat_id == -4 || $cat_id == -3) { | |
545 | $result = db_query($link, "SELECT | |
546 | id, feed_url, cat_id, title, order_id, ". | |
547 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
548 | FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] . | |
549 | " ORDER BY cat_id, title " . $limit_qpart); | |
550 | } else { | |
551 | ||
552 | if ($cat_id) | |
553 | $cat_qpart = "cat_id = '$cat_id'"; | |
554 | else | |
555 | $cat_qpart = "cat_id IS NULL"; | |
556 | ||
557 | $result = db_query($link, "SELECT | |
558 | id, feed_url, cat_id, title, order_id, ". | |
559 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
560 | FROM ttrss_feeds WHERE | |
561 | $cat_qpart AND owner_uid = " . $_SESSION["uid"] . | |
562 | " ORDER BY cat_id, title " . $limit_qpart); | |
563 | } | |
564 | ||
565 | while ($line = db_fetch_assoc($result)) { | |
566 | ||
567 | $unread = getFeedUnread($link, $line["id"]); | |
568 | ||
569 | $has_icon = feed_has_icon($line['id']); | |
570 | ||
571 | if ($unread || !$unread_only) { | |
572 | ||
573 | $row = array( | |
574 | "feed_url" => $line["feed_url"], | |
575 | "title" => $line["title"], | |
576 | "id" => (int)$line["id"], | |
577 | "unread" => (int)$unread, | |
578 | "has_icon" => $has_icon, | |
579 | "cat_id" => (int)$line["cat_id"], | |
ef3da31c | 580 | "last_updated" => (int) strtotime($line["last_updated"]), |
04f60eb7 AD |
581 | "order_id" => (int) $line["order_id"], |
582 | ); | |
583 | ||
584 | array_push($feeds, $row); | |
585 | } | |
586 | } | |
587 | ||
588 | return $feeds; | |
589 | } | |
590 | ||
591 | static function api_get_headlines($link, $feed_id, $limit, $offset, | |
592 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order, | |
593 | $include_attachments, $since_id, | |
ca5d9be4 | 594 | $search = "", $search_mode = "", |
04f60eb7 AD |
595 | $include_nested = false, $sanitize_content = true) { |
596 | ||
597 | $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, | |
ca5d9be4 | 598 | $view_mode, $is_cat, $search, $search_mode, |
04f60eb7 AD |
599 | $order, $offset, 0, false, $since_id, $include_nested); |
600 | ||
601 | $result = $qfh_ret[0]; | |
602 | $feed_title = $qfh_ret[1]; | |
603 | ||
604 | $headlines = array(); | |
605 | ||
606 | while ($line = db_fetch_assoc($result)) { | |
607 | $is_updated = ($line["last_read"] == "" && | |
608 | ($line["unread"] != "t" && $line["unread"] != "1")); | |
609 | ||
610 | $tags = explode(",", $line["tag_cache"]); | |
611 | $labels = json_decode($line["label_cache"], true); | |
612 | ||
613 | //if (!$tags) $tags = get_article_tags($link, $line["id"]); | |
614 | //if (!$labels) $labels = get_article_labels($link, $line["id"]); | |
615 | ||
616 | $headline_row = array( | |
617 | "id" => (int)$line["id"], | |
618 | "unread" => sql_bool_to_bool($line["unread"]), | |
619 | "marked" => sql_bool_to_bool($line["marked"]), | |
620 | "published" => sql_bool_to_bool($line["published"]), | |
ef3da31c | 621 | "updated" => (int) strtotime($line["updated"]), |
04f60eb7 AD |
622 | "is_updated" => $is_updated, |
623 | "title" => $line["title"], | |
624 | "link" => $line["link"], | |
625 | "feed_id" => $line["feed_id"], | |
626 | "tags" => $tags, | |
627 | ); | |
628 | ||
629 | if ($include_attachments) | |
630 | $headline_row['attachments'] = get_article_enclosures($link, | |
631 | $line['id']); | |
632 | ||
633 | if ($show_excerpt) { | |
634 | $excerpt = truncate_string(strip_tags($line["content_preview"]), 100); | |
635 | $headline_row["excerpt"] = $excerpt; | |
636 | } | |
637 | ||
638 | if ($show_content) { | |
639 | ||
640 | if ($line["cached_content"] != "") { | |
641 | $line["content_preview"] =& $line["cached_content"]; | |
642 | } | |
643 | ||
644 | if ($sanitize_content) { | |
645 | $headline_row["content"] = sanitize($link, | |
bfd61d3f AD |
646 | $line["content_preview"], |
647 | sql_bool_to_bool($line['hide_images']), | |
648 | false, $line["site_url"]); | |
04f60eb7 AD |
649 | } else { |
650 | $headline_row["content"] = $line["content_preview"]; | |
651 | } | |
652 | } | |
653 | ||
654 | // unify label output to ease parsing | |
655 | if ($labels["no-labels"] == 1) $labels = array(); | |
656 | ||
657 | $headline_row["labels"] = $labels; | |
658 | ||
659 | $headline_row["feed_title"] = $line["feed_title"]; | |
660 | ||
661 | $headline_row["comments_count"] = (int)$line["num_comments"]; | |
662 | $headline_row["comments_link"] = $line["comments"]; | |
663 | ||
664 | $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]); | |
665 | ||
b6604c96 AD |
666 | global $pluginhost; |
667 | foreach ($pluginhost->get_hooks($pluginhost::HOOK_RENDER_ARTICLE_API) as $p) { | |
668 | $headline_row = $p->hook_render_article_api($headline_row); | |
669 | } | |
670 | ||
04f60eb7 AD |
671 | array_push($headlines, $headline_row); |
672 | } | |
673 | ||
674 | return $headlines; | |
675 | } | |
676 | ||
efc6553d AD |
677 | function unsubscribeFeed() { |
678 | $feed_id = (int) db_escape_string($this->link, $_REQUEST["feed_id"]); | |
679 | ||
680 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
681 | id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]); | |
682 | ||
683 | if (db_num_rows($result) != 0) { | |
684 | Pref_Feeds::remove_feed($this->link, $feed_id, $_SESSION["uid"]); | |
685 | print $this->wrap(self::STATUS_OK, array("status" => "OK")); | |
686 | } else { | |
687 | print $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND")); | |
688 | } | |
689 | } | |
690 | ||
691 | function subscribeToFeed() { | |
692 | $feed_url = db_escape_string($this->link, $_REQUEST["feed_url"]); | |
693 | $category_id = (int) db_escape_string($this->link, $_REQUEST["category_id"]); | |
694 | $login = db_escape_string($this->link, $_REQUEST["login"]); | |
695 | $password = db_escape_string($this->link, $_REQUEST["password"]); | |
696 | ||
697 | if ($feed_url) { | |
698 | $rc = subscribe_to_feed($this->link, $feed_url, $category_id, | |
699 | $login, $password, false); | |
700 | ||
701 | print $this->wrap(self::STATUS_OK, array("status" => $rc)); | |
702 | } else { | |
703 | print $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
704 | } | |
705 | } | |
706 | ||
0bb5833b AD |
707 | function getFeedTree() { |
708 | $pf = new Pref_Feeds($this->link, $_REQUEST); | |
709 | ||
710 | $_REQUEST['mode'] = 2; | |
711 | ||
712 | if ($pf){ | |
713 | $data = $pf->makefeedtree(); | |
714 | print $this->wrap(self::STATUS_OK, array("categories" => $data)); | |
715 | } else { | |
716 | print $this->wrap(self::STATUS_ERR, array("error" => | |
717 | 'UNABLE_TO_INSTANTIATE_OBJECT')); | |
718 | } | |
719 | ||
720 | } | |
de8260cb AD |
721 | } |
722 | ||
723 | ?> |