]>
git.wh0rd.org - tt-rss.git/blob - classes/api.php
2 class API
extends Handler
{
11 function before ( $method ) {
12 if ( parent
:: before ( $method )) {
13 header ( "Content-Type: text/json" );
15 if (! $_SESSION [ "uid" ] && $method != "login" && $method != "isloggedin" ) {
16 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'NOT_LOGGED_IN' ));
20 if ( $_SESSION [ "uid" ] && $method != "logout" && ! get_pref ( 'ENABLE_API_ACCESS' )) {
21 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'API_DISABLED' ));
25 $this -> seq
= ( int ) $_REQUEST [ 'seq' ];
32 function wrap ( $status , $reply ) {
33 print json_encode ( array ( "seq" => $this -> seq
,
35 "content" => $reply ));
38 function getVersion () {
39 $rv = array ( "version" => VERSION
);
40 $this -> wrap ( self
:: STATUS_OK
, $rv );
43 function getApiLevel () {
44 $rv = array ( "level" => self
:: API_LEVEL
);
45 $this -> wrap ( self
:: STATUS_OK
, $rv );
52 $login = $this -> dbh
-> escape_string ( $_REQUEST [ "user" ]);
53 $password = $_REQUEST [ "password" ];
54 $password_base64 = base64_decode ( $_REQUEST [ "password" ]);
56 if ( SINGLE_USER_MODE
) $login = "admin" ;
58 $result = $this -> dbh
-> query ( "SELECT id FROM ttrss_users WHERE login = ' $login '" );
60 if ( $this -> dbh
-> num_rows ( $result ) != 0 ) {
61 $uid = $this -> dbh
-> fetch_result ( $result , 0 , "id" );
67 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => "LOGIN_ERROR" ));
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" ));
83 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => "API_DISABLED" ));
90 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
93 function isLoggedIn () {
94 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => $_SESSION [ "uid" ] != '' ));
97 function getUnread () {
98 $feed_id = $this -> dbh
-> escape_string ( $_REQUEST [ "feed_id" ]);
99 $is_cat = $this -> dbh
-> escape_string ( $_REQUEST [ "is_cat" ]);
102 $this -> wrap ( self
:: STATUS_OK
, array ( "unread" => getFeedUnread ( $feed_id , $is_cat )));
104 $this -> wrap ( self
:: STATUS_OK
, array ( "unread" => Feeds
:: getGlobalUnread ()));
108 /* Method added for ttrss-reader for Android */
109 function getCounters () {
110 $this -> wrap ( self
:: STATUS_OK
, getAllCounters ());
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" ]);
120 $feeds = $this -> api_get_feeds ( $cat_id , $unread_only , $limit , $offset , $include_nested );
122 $this -> wrap ( self
:: STATUS_OK
, $feeds );
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' ]);
130 // TODO do not return empty categories, return Uncategorized and standard virtual cats
133 $nested_qpart = "parent_cat IS NULL" ;
135 $nested_qpart = "true" ;
137 $result = $this -> dbh
-> query ( "SELECT
138 id, title, order_id, (SELECT COUNT(id) FROM
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 = " .
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 );
155 $unread +
= Feeds
:: getCategoryChildrenUnread ( $line [ "id" ]);
157 if ( $unread ||
! $unread_only ) {
158 array_push ( $cats , array ( "id" => $line [ "id" ],
159 "title" => $line [ "title" ],
161 "order_id" => ( int ) $line [ "order_id" ],
167 foreach ( array (- 2 ,- 1 , 0 ) as $cat_id ) {
168 if ( $include_empty ||
! $this -> isCategoryEmpty ( $cat_id )) {
169 $unread = getFeedUnread ( $cat_id , true );
171 if ( $unread ||
! $unread_only ) {
172 array_push ( $cats , array ( "id" => $cat_id ,
173 "title" => Feeds
:: getCategoryTitle ( $cat_id ),
174 "unread" => $unread ));
179 $this -> wrap ( self
:: STATUS_OK
, $cats );
182 function getHeadlines () {
183 $feed_id = $this -> dbh
-> escape_string ( $_REQUEST [ "feed_id" ]);
184 if ( $feed_id != "" ) {
186 if ( is_numeric ( $feed_id )) $feed_id = ( int ) $feed_id ;
188 $limit = ( int ) $this -> dbh
-> escape_string ( $_REQUEST [ "limit" ]);
190 if (! $limit ||
$limit >= 200 ) $limit = 200 ;
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" ]);
210 $_SESSION [ 'hasSandbox' ] = $has_sandbox ;
212 $skip_first_id_check = false ;
214 $override_order = false ;
215 switch ( $_REQUEST [ "order_by" ]) {
217 $override_order = "ttrss_entries.title" ;
220 $override_order = "score DESC, date_entered, updated" ;
221 $skip_first_id_check = true ;
224 $override_order = "updated DESC" ;
228 /* do not rely on params below */
230 $search = $this -> dbh
-> escape_string ( $_REQUEST [ "search" ]);
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 );
237 if ( $include_header ) {
238 $this -> wrap ( self
:: STATUS_OK
, array ( $headlines_header , $headlines ));
240 $this -> wrap ( self
:: STATUS_OK
, $headlines );
243 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));
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" ]);
256 switch ( $field_raw ) {
259 $additional_fields = ",last_marked = NOW()" ;
262 $field = "published" ;
263 $additional_fields = ",last_published = NOW()" ;
267 $additional_fields = ",last_read = NOW()" ;
281 $set_to = "NOT $field " ;
285 if ( $field == "note" ) $set_to = "' $data '" ;
287 if ( $field && $set_to && count ( $article_ids ) > 0 ) {
289 $article_ids = join ( ", " , $article_ids );
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" ]);
293 $num_updated = $this -> dbh
-> affected_rows ( $result );
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 )" );
299 while ( $line = $this -> dbh
-> fetch_assoc ( $result )) {
300 CCache
:: update ( $line [ "feed_id" ], $_SESSION [ "uid" ]);
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 );
310 $p = new pubsubhubbub\publisher\
Publisher ( PUBSUBHUBBUB_HUB
);
311 $p -> publish_update ( $rss_link );
315 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ,
316 "updated" => $num_updated ));
319 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));
324 function getArticle () {
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" ]);
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 = " .
342 $result = $this -> dbh
-> query ( $query );
346 if ( $this -> dbh
-> num_rows ( $result ) != 0 ) {
348 while ( $line = $this -> dbh
-> fetch_assoc ( $result )) {
350 $attachments = Article
:: get_article_enclosures ( $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" ]
372 if ( $sanitize_content ) {
373 $article [ "content" ] = sanitize (
375 sql_bool_to_bool ( $line [ 'hide_images' ]),
376 false , $line [ "site_url" ], false , $line [ "id" ]);
378 $article [ "content" ] = $line [ "content" ];
381 foreach ( PluginHost
:: getInstance ()-> get_hooks ( PluginHost
:: HOOK_RENDER_ARTICLE_API
) as $p ) {
382 $article = $p -> hook_render_article_api ( array ( "article" => $article ));
386 array_push ( $articles , $article );
391 $this -> wrap ( self
:: STATUS_OK
, $articles );
393 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));
397 function getConfig () {
399 "icons_dir" => ICONS_DIR
,
400 "icons_url" => ICONS_URL
);
402 $config [ "daemon_is_running" ] = file_is_locked ( "update_daemon.lock" );
404 $result = $this -> dbh
-> query ( "SELECT COUNT(*) AS cf FROM
405 ttrss_feeds WHERE owner_uid = " . $_SESSION [ "uid" ]);
407 $num_feeds = $this -> dbh
-> fetch_result ( $result , 0 , "cf" );
409 $config [ "num_feeds" ] = ( int ) $num_feeds ;
411 $this -> wrap ( self
:: STATUS_OK
, $config );
414 function updateFeed () {
415 require_once "include/rssfuncs.php" ;
417 $feed_id = ( int ) $this -> dbh
-> escape_string ( $_REQUEST [ "feed_id" ]);
419 if (! ini_get ( "open_basedir" )) {
420 update_rss_feed ( $feed_id );
423 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
426 function catchupFeed () {
427 $feed_id = $this -> dbh
-> escape_string ( $_REQUEST [ "feed_id" ]);
428 $is_cat = $this -> dbh
-> escape_string ( $_REQUEST [ "is_cat" ]);
430 Feeds
:: catchup_feed ( $feed_id , $is_cat );
432 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
436 $pref_name = $this -> dbh
-> escape_string ( $_REQUEST [ "pref_name" ]);
438 $this -> wrap ( self
:: STATUS_OK
, array ( "value" => get_pref ( $pref_name )));
441 function getLabels () {
442 //$article_ids = array_filter(explode(",", $this->dbh->escape_string($_REQUEST["article_ids"])), is_numeric);
444 $article_id = ( int ) $_REQUEST [ 'article_id' ];
448 $result = $this -> dbh
-> query ( "SELECT id, caption, fg_color, bg_color
450 WHERE owner_uid = '" . $_SESSION [ 'uid' ]. "' ORDER BY caption" );
453 $article_labels = Article
:: get_article_labels ( $article_id );
455 $article_labels = array ();
457 while ( $line = $this -> dbh
-> fetch_assoc ( $result )) {
460 foreach ( $article_labels as $al ) {
461 if ( feed_to_label_id ( $al [ 0 ]) == $line [ 'id' ]) {
467 array_push ( $rv , array (
468 "id" => ( int ) label_to_feed_id ( $line [ 'id' ]),
469 "caption" => $line [ 'caption' ],
470 "fg_color" => $line [ 'fg_color' ],
471 "bg_color" => $line [ 'bg_color' ],
472 "checked" => $checked ));
475 $this -> wrap ( self
:: STATUS_OK
, $rv );
478 function setArticleLabel () {
480 $article_ids = array_filter ( explode ( "," , $this -> dbh
-> escape_string ( $_REQUEST [ "article_ids" ])), is_numeric
);
481 $label_id = ( int ) $this -> dbh
-> escape_string ( $_REQUEST [ 'label_id' ]);
482 $assign = ( bool ) ( $this -> dbh
-> escape_string ( $_REQUEST [ 'assign' ]) == "true" );
484 $label = $this -> dbh
-> escape_string ( label_find_caption (
485 feed_to_label_id ( $label_id ), $_SESSION [ "uid" ]));
491 foreach ( $article_ids as $id ) {
494 label_add_article ( $id , $label , $_SESSION [ "uid" ]);
496 label_remove_article ( $id , $label , $_SESSION [ "uid" ]);
503 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ,
504 "updated" => $num_updated ));
508 function index ( $method ) {
509 $plugin = PluginHost
:: getInstance ()-> get_api_method ( strtolower ( $method ));
511 if ( $plugin && method_exists ( $plugin , $method )) {
512 $reply = $plugin -> $method ();
514 $this -> wrap ( $reply [ 0 ], $reply [ 1 ]);
517 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'UNKNOWN_METHOD' , "method" => $method ));
521 function shareToPublished () {
522 $title = $this -> dbh
-> escape_string ( strip_tags ( $_REQUEST [ "title" ]));
523 $url = $this -> dbh
-> escape_string ( strip_tags ( $_REQUEST [ "url" ]));
524 $content = $this -> dbh
-> escape_string ( strip_tags ( $_REQUEST [ "content" ]));
526 if ( Article
:: create_published_article ( $title , $url , $content , "" , $_SESSION [ "uid" ])) {
527 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => 'OK' ));
529 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'Publishing failed' ));
533 static function api_get_feeds ( $cat_id , $unread_only , $limit , $offset , $include_nested = false ) {
539 if ( $cat_id == - 4 ||
$cat_id == - 2 ) {
540 $counters = getLabelCounters ( true );
542 foreach ( array_values ( $counters ) as $cv ) {
544 $unread = $cv [ "counter" ];
546 if ( $unread ||
! $unread_only ) {
549 "id" => ( int ) $cv [ "id" ],
550 "title" => $cv [ "description" ],
551 "unread" => $cv [ "counter" ],
555 array_push ( $feeds , $row );
562 if ( $cat_id == - 4 ||
$cat_id == - 1 ) {
563 foreach ( array (- 1 , - 2 , - 3 , - 4 , - 6 , 0 ) as $i ) {
564 $unread = getFeedUnread ( $i );
566 if ( $unread ||
! $unread_only ) {
567 $title = Feeds
:: getFeedTitle ( $i );
575 array_push ( $feeds , $row );
583 if ( $include_nested && $cat_id ) {
584 $result = db_query ( "SELECT
585 id, title FROM ttrss_feed_categories
586 WHERE parent_cat = ' $cat_id ' AND owner_uid = " . $_SESSION [ "uid" ] .
587 " ORDER BY id, title" );
589 while ( $line = db_fetch_assoc ( $result )) {
590 $unread = getFeedUnread ( $line [ "id" ], true ) +
591 Feeds
:: getCategoryChildrenUnread ( $line [ "id" ]);
593 if ( $unread ||
! $unread_only ) {
595 "id" => ( int ) $line [ "id" ],
596 "title" => $line [ "title" ],
600 array_push ( $feeds , $row );
608 $limit_qpart = "LIMIT $limit OFFSET $offset " ;
613 if ( $cat_id == - 4 ||
$cat_id == - 3 ) {
614 $result = db_query ( "SELECT
615 id, feed_url, cat_id, title, order_id, " .
616 SUBSTRING_FOR_DATE
. "(last_updated,1,19) AS last_updated
617 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION [ "uid" ] .
618 " ORDER BY cat_id, title " . $limit_qpart );
622 $cat_qpart = "cat_id = ' $cat_id '" ;
624 $cat_qpart = "cat_id IS NULL" ;
626 $result = db_query ( "SELECT
627 id, feed_url, cat_id, title, order_id, " .
628 SUBSTRING_FOR_DATE
. "(last_updated,1,19) AS last_updated
629 FROM ttrss_feeds WHERE
630 $cat_qpart AND owner_uid = " . $_SESSION [ "uid" ] .
631 " ORDER BY cat_id, title " . $limit_qpart );
634 while ( $line = db_fetch_assoc ( $result )) {
636 $unread = getFeedUnread ( $line [ "id" ]);
638 $has_icon = feed_has_icon ( $line [ 'id' ]);
640 if ( $unread ||
! $unread_only ) {
643 "feed_url" => $line [ "feed_url" ],
644 "title" => $line [ "title" ],
645 "id" => ( int ) $line [ "id" ],
646 "unread" => ( int ) $unread ,
647 "has_icon" => $has_icon ,
648 "cat_id" => ( int ) $line [ "cat_id" ],
649 "last_updated" => ( int ) strtotime ( $line [ "last_updated" ]),
650 "order_id" => ( int ) $line [ "order_id" ],
653 array_push ( $feeds , $row );
661 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
663 static function api_get_headlines ( $feed_id , $limit , $offset ,
664 $filter , $is_cat , $show_excerpt , $show_content , $view_mode , $order ,
665 $include_attachments , $since_id ,
666 $search = "" , $include_nested = false , $sanitize_content = true ,
667 $force_update = false , $excerpt_length = 100 , $check_first_id = false , $skip_first_id_check = false ) {
669 if ( $force_update && $feed_id > 0 && is_numeric ( $feed_id )) {
670 // Update the feed if required with some basic flood control
673 "SELECT cache_images," . SUBSTRING_FOR_DATE
. "(last_updated,1,19) AS last_updated
674 FROM ttrss_feeds WHERE id = ' $feed_id '" );
676 if ( db_num_rows ( $result ) != 0 ) {
677 $last_updated = strtotime ( db_fetch_result ( $result , 0 , "last_updated" ));
678 $cache_images = sql_bool_to_bool ( db_fetch_result ( $result , 0 , "cache_images" ));
680 if (! $cache_images && time () - $last_updated > 120 ) {
681 include "rssfuncs.php" ;
682 update_rss_feed ( $feed_id , true );
684 db_query ( "UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01'
685 WHERE id = ' $feed_id '" );
693 "view_mode" => $view_mode ,
694 "cat_view" => $is_cat ,
696 "override_order" => $order ,
698 "since_id" => $since_id ,
699 "include_children" => $include_nested ,
700 "check_first_id" => $check_first_id ,
701 "skip_first_id_check" => $skip_first_id_check
704 $qfh_ret = Feeds
:: queryFeedHeadlines ( $params );
706 $result = $qfh_ret [ 0 ];
707 $feed_title = $qfh_ret [ 1 ];
708 $first_id = $qfh_ret [ 6 ];
710 $headlines = array ();
712 $headlines_header = array (
714 'first_id' => $first_id ,
715 'is_cat' => $is_cat );
717 if (! is_numeric ( $result )) {
718 while ( $line = db_fetch_assoc ( $result )) {
719 $line [ "content_preview" ] = truncate_string ( strip_tags ( $line [ "content" ]), $excerpt_length );
720 foreach ( PluginHost
:: getInstance ()-> get_hooks ( PluginHost
:: HOOK_QUERY_HEADLINES
) as $p ) {
721 $line = $p -> hook_query_headlines ( $line , $excerpt_length , true );
724 $is_updated = ( $line [ "last_read" ] == "" &&
725 ( $line [ "unread" ] != "t" && $line [ "unread" ] != "1" ));
727 $tags = explode ( "," , $line [ "tag_cache" ]);
729 $label_cache = $line [ "label_cache" ];
733 $label_cache = json_decode ( $label_cache , true );
736 if ( $label_cache [ "no-labels" ] == 1 )
739 $labels = $label_cache ;
743 if (! is_array ( $labels )) $labels = Article
:: get_article_labels ( $line [ "id" ]);
745 $headline_row = array (
746 "id" => ( int ) $line [ "id" ],
747 "guid" => $line [ "guid" ],
748 "unread" => sql_bool_to_bool ( $line [ "unread" ]),
749 "marked" => sql_bool_to_bool ( $line [ "marked" ]),
750 "published" => sql_bool_to_bool ( $line [ "published" ]),
751 "updated" => ( int ) strtotime ( $line [ "updated" ]),
752 "is_updated" => $is_updated ,
753 "title" => $line [ "title" ],
754 "link" => $line [ "link" ],
755 "feed_id" => $line [ "feed_id" ],
759 if ( $include_attachments )
760 $headline_row [ 'attachments' ] = Article
:: get_article_enclosures (
764 $headline_row [ "excerpt" ] = $line [ "content_preview" ];
768 if ( $sanitize_content ) {
769 $headline_row [ "content" ] = sanitize (
771 sql_bool_to_bool ( $line [ 'hide_images' ]),
772 false , $line [ "site_url" ], false , $line [ "id" ]);
774 $headline_row [ "content" ] = $line [ "content" ];
778 // unify label output to ease parsing
779 if ( $labels [ "no-labels" ] == 1 ) $labels = array ();
781 $headline_row [ "labels" ] = $labels ;
783 $headline_row [ "feed_title" ] = $line [ "feed_title" ] ?
$line [ "feed_title" ] :
786 $headline_row [ "comments_count" ] = ( int ) $line [ "num_comments" ];
787 $headline_row [ "comments_link" ] = $line [ "comments" ];
789 $headline_row [ "always_display_attachments" ] = sql_bool_to_bool ( $line [ "always_display_enclosures" ]);
791 $headline_row [ "author" ] = $line [ "author" ];
793 $headline_row [ "score" ] = ( int ) $line [ "score" ];
794 $headline_row [ "note" ] = $line [ "note" ];
795 $headline_row [ "lang" ] = $line [ "lang" ];
797 foreach ( PluginHost
:: getInstance ()-> get_hooks ( PluginHost
:: HOOK_RENDER_ARTICLE_API
) as $p ) {
798 $headline_row = $p -> hook_render_article_api ( array ( "headline" => $headline_row ));
801 array_push ( $headlines , $headline_row );
803 } else if ( is_numeric ( $result ) && $result == - 1 ) {
804 $headlines_header [ 'first_id_changed' ] = true ;
807 return array ( $headlines , $headlines_header );
810 function unsubscribeFeed () {
811 $feed_id = ( int ) $this -> dbh
-> escape_string ( $_REQUEST [ "feed_id" ]);
813 $result = $this -> dbh
-> query ( "SELECT id FROM ttrss_feeds WHERE
814 id = ' $feed_id ' AND owner_uid = " . $_SESSION [ "uid" ]);
816 if ( $this -> dbh
-> num_rows ( $result ) != 0 ) {
817 Pref_Feeds
:: remove_feed ( $feed_id , $_SESSION [ "uid" ]);
818 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
820 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => "FEED_NOT_FOUND" ));
824 function subscribeToFeed () {
825 $feed_url = $this -> dbh
-> escape_string ( $_REQUEST [ "feed_url" ]);
826 $category_id = ( int ) $this -> dbh
-> escape_string ( $_REQUEST [ "category_id" ]);
827 $login = $this -> dbh
-> escape_string ( $_REQUEST [ "login" ]);
828 $password = $this -> dbh
-> escape_string ( $_REQUEST [ "password" ]);
831 $rc = Feeds
:: subscribe_to_feed ( $feed_url , $category_id , $login , $password );
833 $this -> wrap ( self
:: STATUS_OK
, array ( "status" => $rc ));
835 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));
839 function getFeedTree () {
840 $include_empty = sql_bool_to_bool ( $_REQUEST [ 'include_empty' ]);
842 $pf = new Pref_Feeds ( $_REQUEST );
844 $_REQUEST [ 'mode' ] = 2 ;
845 $_REQUEST [ 'force_show_empty' ] = $include_empty ;
848 $data = $pf -> makefeedtree ();
849 $this -> wrap ( self
:: STATUS_OK
, array ( "categories" => $data ));
851 $this -> wrap ( self
:: STATUS_ERR
, array ( "error" =>
852 'UNABLE_TO_INSTANTIATE_OBJECT' ));
857 // only works for labels or uncategorized for the time being
858 private function isCategoryEmpty ( $id ) {
861 $result = $this -> dbh
-> query ( "SELECT COUNT(*) AS count FROM ttrss_labels2
862 WHERE owner_uid = " . $_SESSION [ "uid" ]);
864 return $this -> dbh
-> fetch_result ( $result , 0 , "count" ) == 0 ;
866 } else if ( $id == 0 ) {
867 $result = $this -> dbh
-> query ( "SELECT COUNT(*) AS count FROM ttrss_feeds
868 WHERE cat_id IS NULL AND owner_uid = " . $_SESSION [ "uid" ]);
870 return $this -> dbh
-> fetch_result ( $result , 0 , "count" ) == 0 ;