]> git.wh0rd.org - tt-rss.git/blob - classes/api.php
wrap rssfuncs into rssutils class
[tt-rss.git] / classes / api.php
1 <?php
2 class API extends Handler {
3
4 const API_LEVEL = 14;
5
6 const STATUS_OK = 0;
7 const STATUS_ERR = 1;
8
9 private $seq;
10
11 function before($method) {
12 if (parent::before($method)) {
13 header("Content-Type: text/json");
14
15 if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") {
16 $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN'));
17 return false;
18 }
19
20 if ($_SESSION["uid"] && $method != "logout" && !get_pref('ENABLE_API_ACCESS')) {
21 $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED'));
22 return false;
23 }
24
25 $this->seq = (int) $_REQUEST['seq'];
26
27 return true;
28 }
29 return false;
30 }
31
32 function wrap($status, $reply) {
33 print json_encode(array("seq" => $this->seq,
34 "status" => $status,
35 "content" => $reply));
36 }
37
38 function getVersion() {
39 $rv = array("version" => VERSION);
40 $this->wrap(self::STATUS_OK, $rv);
41 }
42
43 function getApiLevel() {
44 $rv = array("level" => self::API_LEVEL);
45 $this->wrap(self::STATUS_OK, $rv);
46 }
47
48 function login() {
49 @session_destroy();
50 @session_start();
51
52 $login = $this->dbh->escape_string($_REQUEST["user"]);
53 $password = $_REQUEST["password"];
54 $password_base64 = base64_decode($_REQUEST["password"]);
55
56 if (SINGLE_USER_MODE) $login = "admin";
57
58 $result = $this->dbh->query("SELECT id FROM ttrss_users WHERE login = '$login'");
59
60 if ($this->dbh->num_rows($result) != 0) {
61 $uid = $this->dbh->fetch_result($result, 0, "id");
62 } else {
63 $uid = 0;
64 }
65
66 if (!$uid) {
67 $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
68 return;
69 }
70
71 if (get_pref("ENABLE_API_ACCESS", $uid)) {
72 if (authenticate_user($login, $password)) { // try login with normal password
73 $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
74 "api_level" => self::API_LEVEL));
75 } else if (authenticate_user($login, $password_base64)) { // else try with base64_decoded password
76 $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
77 "api_level" => self::API_LEVEL));
78 } else { // else we are not logged in
79 user_error("Failed login attempt for $login from {$_SERVER['REMOTE_ADDR']}", E_USER_WARNING);
80 $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
81 }
82 } else {
83 $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
84 }
85
86 }
87
88 function logout() {
89 logout_user();
90 $this->wrap(self::STATUS_OK, array("status" => "OK"));
91 }
92
93 function isLoggedIn() {
94 $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != ''));
95 }
96
97 function getUnread() {
98 $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]);
99 $is_cat = $this->dbh->escape_string($_REQUEST["is_cat"]);
100
101 if ($feed_id) {
102 $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat)));
103 } else {
104 $this->wrap(self::STATUS_OK, array("unread" => Feeds::getGlobalUnread()));
105 }
106 }
107
108 /* Method added for ttrss-reader for Android */
109 function getCounters() {
110 $this->wrap(self::STATUS_OK, Counters::getAllCounters());
111 }
112
113 function getFeeds() {
114 $cat_id = $this->dbh->escape_string($_REQUEST["cat_id"]);
115 $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
116 $limit = (int) $this->dbh->escape_string($_REQUEST["limit"]);
117 $offset = (int) $this->dbh->escape_string($_REQUEST["offset"]);
118 $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
119
120 $feeds = $this->api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested);
121
122 $this->wrap(self::STATUS_OK, $feeds);
123 }
124
125 function getCategories() {
126 $unread_only = sql_bool_to_bool($_REQUEST["unread_only"]);
127 $enable_nested = sql_bool_to_bool($_REQUEST["enable_nested"]);
128 $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
129
130 // TODO do not return empty categories, return Uncategorized and standard virtual cats
131
132 if ($enable_nested)
133 $nested_qpart = "parent_cat IS NULL";
134 else
135 $nested_qpart = "true";
136
137 $result = $this->dbh->query("SELECT
138 id, title, order_id, (SELECT COUNT(id) FROM
139 ttrss_feeds WHERE
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
144 FROM ttrss_feed_categories
145 WHERE $nested_qpart AND owner_uid = " .
146 $_SESSION["uid"]);
147
148 $cats = array();
149
150 while ($line = $this->dbh->fetch_assoc($result)) {
151 if ($include_empty || $line["num_feeds"] > 0 || $line["num_cats"] > 0) {
152 $unread = getFeedUnread($line["id"], true);
153
154 if ($enable_nested)
155 $unread += Feeds::getCategoryChildrenUnread($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 }
164 }
165 }
166
167 foreach (array(-2,-1,0) as $cat_id) {
168 if ($include_empty || !$this->isCategoryEmpty($cat_id)) {
169 $unread = getFeedUnread($cat_id, true);
170
171 if ($unread || !$unread_only) {
172 array_push($cats, array("id" => $cat_id,
173 "title" => Feeds::getCategoryTitle($cat_id),
174 "unread" => $unread));
175 }
176 }
177 }
178
179 $this->wrap(self::STATUS_OK, $cats);
180 }
181
182 function getHeadlines() {
183 $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]);
184 if ($feed_id != "") {
185
186 if (is_numeric($feed_id)) $feed_id = (int) $feed_id;
187
188 $limit = (int)$this->dbh->escape_string($_REQUEST["limit"]);
189
190 if (!$limit || $limit >= 200) $limit = 200;
191
192 $offset = (int)$this->dbh->escape_string($_REQUEST["skip"]);
193 $filter = $this->dbh->escape_string($_REQUEST["filter"]);
194 $is_cat = sql_bool_to_bool($_REQUEST["is_cat"]);
195 $show_excerpt = sql_bool_to_bool($_REQUEST["show_excerpt"]);
196 $show_content = sql_bool_to_bool($_REQUEST["show_content"]);
197 /* all_articles, unread, adaptive, marked, updated */
198 $view_mode = $this->dbh->escape_string($_REQUEST["view_mode"]);
199 $include_attachments = sql_bool_to_bool($_REQUEST["include_attachments"]);
200 $since_id = (int)$this->dbh->escape_string($_REQUEST["since_id"]);
201 $include_nested = sql_bool_to_bool($_REQUEST["include_nested"]);
202 $sanitize_content = !isset($_REQUEST["sanitize"]) ||
203 sql_bool_to_bool($_REQUEST["sanitize"]);
204 $force_update = sql_bool_to_bool($_REQUEST["force_update"]);
205 $has_sandbox = sql_bool_to_bool($_REQUEST["has_sandbox"]);
206 $excerpt_length = (int)$this->dbh->escape_string($_REQUEST["excerpt_length"]);
207 $check_first_id = (int)$this->dbh->escape_string($_REQUEST["check_first_id"]);
208 $include_header = sql_bool_to_bool($_REQUEST["include_header"]);
209
210 $_SESSION['hasSandbox'] = $has_sandbox;
211
212 $skip_first_id_check = false;
213
214 $override_order = false;
215 switch ($_REQUEST["order_by"]) {
216 case "title":
217 $override_order = "ttrss_entries.title";
218 break;
219 case "date_reverse":
220 $override_order = "score DESC, date_entered, updated";
221 $skip_first_id_check = true;
222 break;
223 case "feed_dates":
224 $override_order = "updated DESC";
225 break;
226 }
227
228 /* do not rely on params below */
229
230 $search = $this->dbh->escape_string($_REQUEST["search"]);
231
232 list($headlines, $headlines_header) = $this->api_get_headlines($feed_id, $limit, $offset,
233 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order,
234 $include_attachments, $since_id, $search,
235 $include_nested, $sanitize_content, $force_update, $excerpt_length, $check_first_id, $skip_first_id_check);
236
237 if ($include_header) {
238 $this->wrap(self::STATUS_OK, array($headlines_header, $headlines));
239 } else {
240 $this->wrap(self::STATUS_OK, $headlines);
241 }
242 } else {
243 $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
244 }
245 }
246
247 function updateArticle() {
248 $article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
249 $mode = (int) $this->dbh->escape_string($_REQUEST["mode"]);
250 $data = $this->dbh->escape_string($_REQUEST["data"]);
251 $field_raw = (int)$this->dbh->escape_string($_REQUEST["field"]);
252
253 $field = "";
254 $set_to = "";
255
256 switch ($field_raw) {
257 case 0:
258 $field = "marked";
259 $additional_fields = ",last_marked = NOW()";
260 break;
261 case 1:
262 $field = "published";
263 $additional_fields = ",last_published = NOW()";
264 break;
265 case 2:
266 $field = "unread";
267 $additional_fields = ",last_read = NOW()";
268 break;
269 case 3:
270 $field = "note";
271 };
272
273 switch ($mode) {
274 case 1:
275 $set_to = "true";
276 break;
277 case 0:
278 $set_to = "false";
279 break;
280 case 2:
281 $set_to = "NOT $field";
282 break;
283 }
284
285 if ($field == "note") $set_to = "'$data'";
286
287 if ($field && $set_to && count($article_ids) > 0) {
288
289 $article_ids = join(", ", $article_ids);
290
291 $result = $this->dbh->query("UPDATE ttrss_user_entries SET $field = $set_to $additional_fields WHERE ref_id IN ($article_ids) AND owner_uid = " . $_SESSION["uid"]);
292
293 $num_updated = $this->dbh->affected_rows($result);
294
295 if ($num_updated > 0 && $field == "unread") {
296 $result = $this->dbh->query("SELECT DISTINCT feed_id FROM ttrss_user_entries
297 WHERE ref_id IN ($article_ids)");
298
299 while ($line = $this->dbh->fetch_assoc($result)) {
300 CCache::update($line["feed_id"], $_SESSION["uid"]);
301 }
302 }
303
304 if ($num_updated > 0 && $field == "published") {
305 if (PUBSUBHUBBUB_HUB) {
306 $rss_link = get_self_url_prefix() .
307 "/public.php?op=rss&id=-2&key=" .
308 get_feed_access_key(-2, false);
309
310 $p = new pubsubhubbub\publisher\Publisher(PUBSUBHUBBUB_HUB);
311 $p->publish_update($rss_link);
312 }
313 }
314
315 $this->wrap(self::STATUS_OK, array("status" => "OK",
316 "updated" => $num_updated));
317
318 } else {
319 $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
320 }
321
322 }
323
324 function getArticle() {
325
326 $article_id = join(",", array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_id"])), is_numeric));
327 $sanitize_content = !isset($_REQUEST["sanitize"]) ||
328 sql_bool_to_bool($_REQUEST["sanitize"]);
329
330 if ($article_id) {
331
332 $query = "SELECT id,guid,title,link,content,feed_id,comments,int_id,
333 marked,unread,published,score,note,lang,
334 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
335 author,(SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title,
336 (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) AS site_url,
337 (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images
338 FROM ttrss_entries,ttrss_user_entries
339 WHERE id IN ($article_id) AND ref_id = id AND owner_uid = " .
340 $_SESSION["uid"] ;
341
342 $result = $this->dbh->query($query);
343
344 $articles = array();
345
346 if ($this->dbh->num_rows($result) != 0) {
347
348 while ($line = $this->dbh->fetch_assoc($result)) {
349
350 $attachments = Article::get_article_enclosures($line['id']);
351
352 $article = array(
353 "id" => $line["id"],
354 "guid" => $line["guid"],
355 "title" => $line["title"],
356 "link" => $line["link"],
357 "labels" => Article::get_article_labels($line['id']),
358 "unread" => sql_bool_to_bool($line["unread"]),
359 "marked" => sql_bool_to_bool($line["marked"]),
360 "published" => sql_bool_to_bool($line["published"]),
361 "comments" => $line["comments"],
362 "author" => $line["author"],
363 "updated" => (int) strtotime($line["updated"]),
364 "feed_id" => $line["feed_id"],
365 "attachments" => $attachments,
366 "score" => (int)$line["score"],
367 "feed_title" => $line["feed_title"],
368 "note" => $line["note"],
369 "lang" => $line["lang"]
370 );
371
372 if ($sanitize_content) {
373 $article["content"] = sanitize(
374 $line["content"],
375 sql_bool_to_bool($line['hide_images']),
376 false, $line["site_url"], false, $line["id"]);
377 } else {
378 $article["content"] = $line["content"];
379 }
380
381 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) {
382 $article = $p->hook_render_article_api(array("article" => $article));
383 }
384
385
386 array_push($articles, $article);
387
388 }
389 }
390
391 $this->wrap(self::STATUS_OK, $articles);
392 } else {
393 $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
394 }
395 }
396
397 function getConfig() {
398 $config = array(
399 "icons_dir" => ICONS_DIR,
400 "icons_url" => ICONS_URL);
401
402 $config["daemon_is_running"] = file_is_locked("update_daemon.lock");
403
404 $result = $this->dbh->query("SELECT COUNT(*) AS cf FROM
405 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
406
407 $num_feeds = $this->dbh->fetch_result($result, 0, "cf");
408
409 $config["num_feeds"] = (int)$num_feeds;
410
411 $this->wrap(self::STATUS_OK, $config);
412 }
413
414 function updateFeed() {
415 $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]);
416
417 if (!ini_get("open_basedir")) {
418 RSSUtils::update_rss_feed($feed_id);
419 }
420
421 $this->wrap(self::STATUS_OK, array("status" => "OK"));
422 }
423
424 function catchupFeed() {
425 $feed_id = $this->dbh->escape_string($_REQUEST["feed_id"]);
426 $is_cat = $this->dbh->escape_string($_REQUEST["is_cat"]);
427
428 Feeds::catchup_feed($feed_id, $is_cat);
429
430 $this->wrap(self::STATUS_OK, array("status" => "OK"));
431 }
432
433 function getPref() {
434 $pref_name = $this->dbh->escape_string($_REQUEST["pref_name"]);
435
436 $this->wrap(self::STATUS_OK, array("value" => get_pref($pref_name)));
437 }
438
439 function getLabels() {
440 //$article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
441
442 $article_id = (int)$_REQUEST['article_id'];
443
444 $rv = array();
445
446 $result = $this->dbh->query("SELECT id, caption, fg_color, bg_color
447 FROM ttrss_labels2
448 WHERE owner_uid = '".$_SESSION['uid']."' ORDER BY caption");
449
450 if ($article_id)
451 $article_labels = Article::get_article_labels($article_id);
452 else
453 $article_labels = array();
454
455 while ($line = $this->dbh->fetch_assoc($result)) {
456
457 $checked = false;
458 foreach ($article_labels as $al) {
459 if (Labels::feed_to_label_id($al[0]) == $line['id']) {
460 $checked = true;
461 break;
462 }
463 }
464
465 array_push($rv, array(
466 "id" => (int)Labels::label_to_feed_id($line['id']),
467 "caption" => $line['caption'],
468 "fg_color" => $line['fg_color'],
469 "bg_color" => $line['bg_color'],
470 "checked" => $checked));
471 }
472
473 $this->wrap(self::STATUS_OK, $rv);
474 }
475
476 function setArticleLabel() {
477
478 $article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
479 $label_id = (int) $this->dbh->escape_string($_REQUEST['label_id']);
480 $assign = (bool) ($this->dbh->escape_string($_REQUEST['assign']) == "true");
481
482 $label = $this->dbh->escape_string(Labels::find_caption(
483 Labels::feed_to_label_id($label_id), $_SESSION["uid"]));
484
485 $num_updated = 0;
486
487 if ($label) {
488
489 foreach ($article_ids as $id) {
490
491 if ($assign)
492 Labels::add_article($id, $label, $_SESSION["uid"]);
493 else
494 Labels::remove_article($id, $label, $_SESSION["uid"]);
495
496 ++$num_updated;
497
498 }
499 }
500
501 $this->wrap(self::STATUS_OK, array("status" => "OK",
502 "updated" => $num_updated));
503
504 }
505
506 function index($method) {
507 $plugin = PluginHost::getInstance()->get_api_method(strtolower($method));
508
509 if ($plugin && method_exists($plugin, $method)) {
510 $reply = $plugin->$method();
511
512 $this->wrap($reply[0], $reply[1]);
513
514 } else {
515 $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method));
516 }
517 }
518
519 function shareToPublished() {
520 $title = $this->dbh->escape_string(strip_tags($_REQUEST["title"]));
521 $url = $this->dbh->escape_string(strip_tags($_REQUEST["url"]));
522 $content = $this->dbh->escape_string(strip_tags($_REQUEST["content"]));
523
524 if (Article::create_published_article($title, $url, $content, "", $_SESSION["uid"])) {
525 $this->wrap(self::STATUS_OK, array("status" => 'OK'));
526 } else {
527 $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed'));
528 }
529 }
530
531 static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) {
532
533 $feeds = array();
534
535 /* Labels */
536
537 if ($cat_id == -4 || $cat_id == -2) {
538 $counters = Counters::getLabelCounters(true);
539
540 foreach (array_values($counters) as $cv) {
541
542 $unread = $cv["counter"];
543
544 if ($unread || !$unread_only) {
545
546 $row = array(
547 "id" => (int) $cv["id"],
548 "title" => $cv["description"],
549 "unread" => $cv["counter"],
550 "cat_id" => -2,
551 );
552
553 array_push($feeds, $row);
554 }
555 }
556 }
557
558 /* Virtual feeds */
559
560 if ($cat_id == -4 || $cat_id == -1) {
561 foreach (array(-1, -2, -3, -4, -6, 0) as $i) {
562 $unread = getFeedUnread($i);
563
564 if ($unread || !$unread_only) {
565 $title = Feeds::getFeedTitle($i);
566
567 $row = array(
568 "id" => $i,
569 "title" => $title,
570 "unread" => $unread,
571 "cat_id" => -1,
572 );
573 array_push($feeds, $row);
574 }
575
576 }
577 }
578
579 /* Child cats */
580
581 if ($include_nested && $cat_id) {
582 $result = db_query("SELECT
583 id, title FROM ttrss_feed_categories
584 WHERE parent_cat = '$cat_id' AND owner_uid = " . $_SESSION["uid"] .
585 " ORDER BY id, title");
586
587 while ($line = db_fetch_assoc($result)) {
588 $unread = getFeedUnread($line["id"], true) +
589 Feeds::getCategoryChildrenUnread($line["id"]);
590
591 if ($unread || !$unread_only) {
592 $row = array(
593 "id" => (int) $line["id"],
594 "title" => $line["title"],
595 "unread" => $unread,
596 "is_cat" => true,
597 );
598 array_push($feeds, $row);
599 }
600 }
601 }
602
603 /* Real feeds */
604
605 if ($limit) {
606 $limit_qpart = "LIMIT $limit OFFSET $offset";
607 } else {
608 $limit_qpart = "";
609 }
610
611 if ($cat_id == -4 || $cat_id == -3) {
612 $result = db_query("SELECT
613 id, feed_url, cat_id, title, order_id, ".
614 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
615 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] .
616 " ORDER BY cat_id, title " . $limit_qpart);
617 } else {
618
619 if ($cat_id)
620 $cat_qpart = "cat_id = '$cat_id'";
621 else
622 $cat_qpart = "cat_id IS NULL";
623
624 $result = db_query("SELECT
625 id, feed_url, cat_id, title, order_id, ".
626 SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
627 FROM ttrss_feeds WHERE
628 $cat_qpart AND owner_uid = " . $_SESSION["uid"] .
629 " ORDER BY cat_id, title " . $limit_qpart);
630 }
631
632 while ($line = db_fetch_assoc($result)) {
633
634 $unread = getFeedUnread($line["id"]);
635
636 $has_icon = feed_has_icon($line['id']);
637
638 if ($unread || !$unread_only) {
639
640 $row = array(
641 "feed_url" => $line["feed_url"],
642 "title" => $line["title"],
643 "id" => (int)$line["id"],
644 "unread" => (int)$unread,
645 "has_icon" => $has_icon,
646 "cat_id" => (int)$line["cat_id"],
647 "last_updated" => (int) strtotime($line["last_updated"]),
648 "order_id" => (int) $line["order_id"],
649 );
650
651 array_push($feeds, $row);
652 }
653 }
654
655 return $feeds;
656 }
657
658 /**
659 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
660 */
661 static function api_get_headlines($feed_id, $limit, $offset,
662 $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order,
663 $include_attachments, $since_id,
664 $search = "", $include_nested = false, $sanitize_content = true,
665 $force_update = false, $excerpt_length = 100, $check_first_id = false, $skip_first_id_check = false) {
666
667 if ($force_update && $feed_id > 0 && is_numeric($feed_id)) {
668 // Update the feed if required with some basic flood control
669
670 $result = db_query(
671 "SELECT cache_images,".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
672 FROM ttrss_feeds WHERE id = '$feed_id'");
673
674 if (db_num_rows($result) != 0) {
675 $last_updated = strtotime(db_fetch_result($result, 0, "last_updated"));
676 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
677
678 if (!$cache_images && time() - $last_updated > 120) {
679 RSSUtils::update_rss_feed($feed_id, true);
680 } else {
681 db_query("UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
682 WHERE id = '$feed_id'");
683 }
684 }
685 }
686
687 $params = array(
688 "feed" => $feed_id,
689 "limit" => $limit,
690 "view_mode" => $view_mode,
691 "cat_view" => $is_cat,
692 "search" => $search,
693 "override_order" => $order,
694 "offset" => $offset,
695 "since_id" => $since_id,
696 "include_children" => $include_nested,
697 "check_first_id" => $check_first_id,
698 "skip_first_id_check" => $skip_first_id_check
699 );
700
701 $qfh_ret = Feeds::queryFeedHeadlines($params);
702
703 $result = $qfh_ret[0];
704 $feed_title = $qfh_ret[1];
705 $first_id = $qfh_ret[6];
706
707 $headlines = array();
708
709 $headlines_header = array(
710 'id' => $feed_id,
711 'first_id' => $first_id,
712 'is_cat' => $is_cat);
713
714 if (!is_numeric($result)) {
715 while ($line = db_fetch_assoc($result)) {
716 $line["content_preview"] = truncate_string(strip_tags($line["content"]), $excerpt_length);
717 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
718 $line = $p->hook_query_headlines($line, $excerpt_length, true);
719 }
720
721 $is_updated = ($line["last_read"] == "" &&
722 ($line["unread"] != "t" && $line["unread"] != "1"));
723
724 $tags = explode(",", $line["tag_cache"]);
725
726 $label_cache = $line["label_cache"];
727 $labels = false;
728
729 if ($label_cache) {
730 $label_cache = json_decode($label_cache, true);
731
732 if ($label_cache) {
733 if ($label_cache["no-labels"] == 1)
734 $labels = array();
735 else
736 $labels = $label_cache;
737 }
738 }
739
740 if (!is_array($labels)) $labels = Article::get_article_labels($line["id"]);
741
742 $headline_row = array(
743 "id" => (int)$line["id"],
744 "guid" => $line["guid"],
745 "unread" => sql_bool_to_bool($line["unread"]),
746 "marked" => sql_bool_to_bool($line["marked"]),
747 "published" => sql_bool_to_bool($line["published"]),
748 "updated" => (int)strtotime($line["updated"]),
749 "is_updated" => $is_updated,
750 "title" => $line["title"],
751 "link" => $line["link"],
752 "feed_id" => $line["feed_id"],
753 "tags" => $tags,
754 );
755
756 if ($include_attachments)
757 $headline_row['attachments'] = Article::get_article_enclosures(
758 $line['id']);
759
760 if ($show_excerpt)
761 $headline_row["excerpt"] = $line["content_preview"];
762
763 if ($show_content) {
764
765 if ($sanitize_content) {
766 $headline_row["content"] = sanitize(
767 $line["content"],
768 sql_bool_to_bool($line['hide_images']),
769 false, $line["site_url"], false, $line["id"]);
770 } else {
771 $headline_row["content"] = $line["content"];
772 }
773 }
774
775 // unify label output to ease parsing
776 if ($labels["no-labels"] == 1) $labels = array();
777
778 $headline_row["labels"] = $labels;
779
780 $headline_row["feed_title"] = $line["feed_title"] ? $line["feed_title"] :
781 $feed_title;
782
783 $headline_row["comments_count"] = (int)$line["num_comments"];
784 $headline_row["comments_link"] = $line["comments"];
785
786 $headline_row["always_display_attachments"] = sql_bool_to_bool($line["always_display_enclosures"]);
787
788 $headline_row["author"] = $line["author"];
789
790 $headline_row["score"] = (int)$line["score"];
791 $headline_row["note"] = $line["note"];
792 $headline_row["lang"] = $line["lang"];
793
794 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) {
795 $headline_row = $p->hook_render_article_api(array("headline" => $headline_row));
796 }
797
798 array_push($headlines, $headline_row);
799 }
800 } else if (is_numeric($result) && $result == -1) {
801 $headlines_header['first_id_changed'] = true;
802 }
803
804 return array($headlines, $headlines_header);
805 }
806
807 function unsubscribeFeed() {
808 $feed_id = (int) $this->dbh->escape_string($_REQUEST["feed_id"]);
809
810 $result = $this->dbh->query("SELECT id FROM ttrss_feeds WHERE
811 id = '$feed_id' AND owner_uid = ".$_SESSION["uid"]);
812
813 if ($this->dbh->num_rows($result) != 0) {
814 Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]);
815 $this->wrap(self::STATUS_OK, array("status" => "OK"));
816 } else {
817 $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND"));
818 }
819 }
820
821 function subscribeToFeed() {
822 $feed_url = $this->dbh->escape_string($_REQUEST["feed_url"]);
823 $category_id = (int) $this->dbh->escape_string($_REQUEST["category_id"]);
824 $login = $this->dbh->escape_string($_REQUEST["login"]);
825 $password = $this->dbh->escape_string($_REQUEST["password"]);
826
827 if ($feed_url) {
828 $rc = Feeds::subscribe_to_feed($feed_url, $category_id, $login, $password);
829
830 $this->wrap(self::STATUS_OK, array("status" => $rc));
831 } else {
832 $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE'));
833 }
834 }
835
836 function getFeedTree() {
837 $include_empty = sql_bool_to_bool($_REQUEST['include_empty']);
838
839 $pf = new Pref_Feeds($_REQUEST);
840
841 $_REQUEST['mode'] = 2;
842 $_REQUEST['force_show_empty'] = $include_empty;
843
844 if ($pf){
845 $data = $pf->makefeedtree();
846 $this->wrap(self::STATUS_OK, array("categories" => $data));
847 } else {
848 $this->wrap(self::STATUS_ERR, array("error" =>
849 'UNABLE_TO_INSTANTIATE_OBJECT'));
850 }
851
852 }
853
854 // only works for labels or uncategorized for the time being
855 private function isCategoryEmpty($id) {
856
857 if ($id == -2) {
858 $result = $this->dbh->query("SELECT COUNT(*) AS count FROM ttrss_labels2
859 WHERE owner_uid = " . $_SESSION["uid"]);
860
861 return $this->dbh->fetch_result($result, 0, "count") == 0;
862
863 } else if ($id == 0) {
864 $result = $this->dbh->query("SELECT COUNT(*) AS count FROM ttrss_feeds
865 WHERE cat_id IS NULL AND owner_uid = " . $_SESSION["uid"]);
866
867 return $this->dbh->fetch_result($result, 0, "count") == 0;
868
869 }
870
871 return false;
872 }
873
874
875 }