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