]>
git.wh0rd.org - tt-rss.git/blob - classes/api.php
3 class API
extends Handler
{
12 function before ( $method ) {
13 if ( parent
:: before ( $method )) {
14 header ( "Content-Type: text/json" );
16 if (! $_SESSION [ "uid" ] && $method != "login" && $method != "isloggedin" ) {
17 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'NOT_LOGGED_IN' ));
21 if ( $_SESSION [ "uid" ] && $method != "logout" && ! get_pref ( $this -> link
, 'ENABLE_API_ACCESS' )) {
22 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'API_DISABLED' ));
26 $this -> seq
= ( int ) $_REQUEST [ 'seq' ];
33 function wrap ( $status , $reply ) {
34 print json_encode ( array ( "seq" => $this -> seq
,
36 "content" => $reply ));
39 function getVersion () {
40 $rv = array ( "version" => VERSION
);
41 print $this -> wrap ( self
:: STATUS_OK
, $rv );
44 function getApiLevel () {
45 $rv = array ( "level" => self
:: API_LEVEL
);
46 print $this -> wrap ( self
:: STATUS_OK
, $rv );
50 $login = db_escape_string ( $this -> link
, $_REQUEST [ "user" ]);
51 $password = $_REQUEST [ "password" ];
52 $password_base64 = base64_decode ( $_REQUEST [ "password" ]);
54 if ( SINGLE_USER_MODE
) $login = "admin" ;
56 $result = db_query ( $this -> link
, "SELECT id FROM ttrss_users WHERE login = ' $login '" );
58 if ( db_num_rows ( $result ) != 0 ) {
59 $uid = db_fetch_result ( $result , 0 , "id" );
65 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => "LOGIN_ERROR" ));
69 if ( get_pref ( $this -> link
, "ENABLE_API_ACCESS" , $uid )) {
70 if ( authenticate_user ( $this -> link
, $login , $password )) { // try login with normal password
71 print $this -> wrap ( self
:: STATUS_OK
, array ( "session_id" => session_id (),
72 "api_level" => self
:: API_LEVEL
));
73 } else if ( authenticate_user ( $this -> link
, $login , $password_base64 )) { // else try with base64_decoded password
74 print $this -> wrap ( self
:: STATUS_OK
, array ( "session_id" => session_id (),
75 "api_level" => self
:: API_LEVEL
));
76 } else { // else we are not logged in
77 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => "LOGIN_ERROR" ));
80 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => "API_DISABLED" ));
87 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
90 function isLoggedIn () {
91 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => $_SESSION [ "uid" ] != '' ));
94 function getUnread () {
95 $feed_id = db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
96 $is_cat = db_escape_string ( $this -> link
, $_REQUEST [ "is_cat" ]);
99 print $this -> wrap ( self
:: STATUS_OK
, array ( "unread" => getFeedUnread ( $this -> link
, $feed_id , $is_cat )));
101 print $this -> wrap ( self
:: STATUS_OK
, array ( "unread" => getGlobalUnread ( $this -> link
)));
105 /* Method added for ttrss-reader for Android */
106 function getCounters () {
107 print $this -> wrap ( self
:: STATUS_OK
, getAllCounters ( $this -> link
));
110 function getFeeds () {
111 $cat_id = db_escape_string ( $this -> link
, $_REQUEST [ "cat_id" ]);
112 $unread_only = sql_bool_to_bool ( $_REQUEST [ "unread_only" ]);
113 $limit = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "limit" ]);
114 $offset = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "offset" ]);
115 $include_nested = sql_bool_to_bool ( $_REQUEST [ "include_nested" ]);
117 $feeds = $this -> api_get_feeds ( $this -> link
, $cat_id , $unread_only , $limit , $offset , $include_nested );
119 print $this -> wrap ( self
:: STATUS_OK
, $feeds );
122 function getCategories () {
123 $unread_only = sql_bool_to_bool ( $_REQUEST [ "unread_only" ]);
124 $enable_nested = sql_bool_to_bool ( $_REQUEST [ "enable_nested" ]);
126 // TODO do not return empty categories, return Uncategorized and standard virtual cats
129 $nested_qpart = "parent_cat IS NULL" ;
131 $nested_qpart = "true" ;
133 $result = db_query ( $this -> link
, "SELECT
134 id, title, order_id, (SELECT COUNT(id) FROM
136 ttrss_feed_categories.id IS NOT NULL AND cat_id = ttrss_feed_categories.id) AS num_feeds,
137 (SELECT COUNT(id) FROM
138 ttrss_feed_categories AS c2 WHERE
139 c2.parent_cat = ttrss_feed_categories.id) AS num_cats
140 FROM ttrss_feed_categories
141 WHERE $nested_qpart AND owner_uid = " .
146 while ( $line = db_fetch_assoc ( $result )) {
147 if ( $line [ "num_feeds" ] > 0 ||
$line [ "num_cats" ] > 0 ) {
148 $unread = getFeedUnread ( $this -> link
, $line [ "id" ], true );
151 $unread +
= getCategoryChildrenUnread ( $this -> link
, $line [ "id" ]);
153 if ( $unread ||
! $unread_only ) {
154 array_push ( $cats , array ( "id" => $line [ "id" ],
155 "title" => $line [ "title" ],
157 "order_id" => ( int ) $line [ "order_id" ],
163 foreach ( array (- 2 ,- 1 , 0 ) as $cat_id ) {
164 $unread = getFeedUnread ( $this -> link
, $cat_id , true );
166 if ( $unread ||
! $unread_only ) {
167 array_push ( $cats , array ( "id" => $cat_id ,
168 "title" => getCategoryTitle ( $this -> link
, $cat_id ),
169 "unread" => $unread ));
173 print $this -> wrap ( self
:: STATUS_OK
, $cats );
176 function getHeadlines () {
177 $feed_id = db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
178 if ( $feed_id != "" ) {
180 $limit = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "limit" ]);
182 if (! $limit ||
$limit >= 60 ) $limit = 60 ;
184 $offset = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "skip" ]);
185 $filter = db_escape_string ( $this -> link
, $_REQUEST [ "filter" ]);
186 $is_cat = sql_bool_to_bool ( $_REQUEST [ "is_cat" ]);
187 $show_excerpt = sql_bool_to_bool ( $_REQUEST [ "show_excerpt" ]);
188 $show_content = sql_bool_to_bool ( $_REQUEST [ "show_content" ]);
189 /* all_articles, unread, adaptive, marked, updated */
190 $view_mode = db_escape_string ( $this -> link
, $_REQUEST [ "view_mode" ]);
191 $include_attachments = sql_bool_to_bool ( $_REQUEST [ "include_attachments" ]);
192 $since_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "since_id" ]);
193 $include_nested = sql_bool_to_bool ( $_REQUEST [ "include_nested" ]);
194 $sanitize_content = true ;
196 /* do not rely on params below */
198 $search = db_escape_string ( $this -> link
, $_REQUEST [ "search" ]);
199 $search_mode = db_escape_string ( $this -> link
, $_REQUEST [ "search_mode" ]);
201 $headlines = $this -> api_get_headlines ( $this -> link
, $feed_id , $limit , $offset ,
202 $filter , $is_cat , $show_excerpt , $show_content , $view_mode , false ,
203 $include_attachments , $since_id , $search , $search_mode ,
204 $include_nested , $sanitize_content );
206 print $this -> wrap ( self
:: STATUS_OK
, $headlines );
208 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));
212 function updateArticle () {
213 $article_ids = array_filter ( explode ( "," , db_escape_string ( $this -> link
, $_REQUEST [ "article_ids" ])), is_numeric
);
214 $mode = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "mode" ]);
215 $data = db_escape_string ( $this -> link
, $_REQUEST [ "data" ]);
216 $field_raw = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "field" ]);
221 switch ( $field_raw ) {
224 $additional_fields = ",last_marked = NOW()" ;
227 $field = "published" ;
228 $additional_fields = ",last_published = NOW()" ;
232 $additional_fields = ",last_read = NOW()" ;
246 $set_to = "NOT $field " ;
250 if ( $field == "note" ) $set_to = "' $data '" ;
252 if ( $field && $set_to && count ( $article_ids ) > 0 ) {
254 $article_ids = join ( ", " , $article_ids );
256 $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" ]);
258 $num_updated = db_affected_rows ( $this -> link
, $result );
260 if ( $num_updated > 0 && $field == "unread" ) {
261 $result = db_query ( $this -> link
, "SELECT DISTINCT feed_id FROM ttrss_user_entries
262 WHERE ref_id IN ( $article_ids )" );
264 while ( $line = db_fetch_assoc ( $result )) {
265 ccache_update ( $this -> link
, $line [ "feed_id" ], $_SESSION [ "uid" ]);
269 if ( $num_updated > 0 && $field == "published" ) {
270 if ( PUBSUBHUBBUB_HUB
) {
271 $rss_link = get_self_url_prefix () .
272 "/public.php?op=rss&id=-2&key=" .
273 get_feed_access_key ( $this -> link
, - 2 , false );
275 $p = new Publisher ( PUBSUBHUBBUB_HUB
);
276 $pubsub_result = $p -> publish_update ( $rss_link );
280 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ,
281 "updated" => $num_updated ));
284 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));
289 function getArticle () {
291 $article_id = join ( "," , array_filter ( explode ( "," , db_escape_string ( $this -> link
, $_REQUEST [ "article_id" ])), is_numeric
));
293 $query = "SELECT id,title,link,content,cached_content,feed_id,comments,int_id,
294 marked,unread,published,
295 " . SUBSTRING_FOR_DATE
. "(updated,1,16) as updated,
297 FROM ttrss_entries,ttrss_user_entries
298 WHERE id IN ( $article_id ) AND ref_id = id AND owner_uid = " .
301 $result = db_query ( $this -> link
, $query );
305 if ( db_num_rows ( $result ) != 0 ) {
307 while ( $line = db_fetch_assoc ( $result )) {
309 $attachments = get_article_enclosures ( $this -> link
, $line [ 'id' ]);
313 "title" => $line [ "title" ],
314 "link" => $line [ "link" ],
315 "labels" => get_article_labels ( $this -> link
, $line [ 'id' ]),
316 "unread" => sql_bool_to_bool ( $line [ "unread" ]),
317 "marked" => sql_bool_to_bool ( $line [ "marked" ]),
318 "published" => sql_bool_to_bool ( $line [ "published" ]),
319 "comments" => $line [ "comments" ],
320 "author" => $line [ "author" ],
321 "updated" => ( int ) strtotime ( $line [ "updated" ]),
322 "content" => $line [ "cached_content" ] != "" ?
$line [ "cached_content" ] : $line [ "content" ],
323 "feed_id" => $line [ "feed_id" ],
324 "attachments" => $attachments
327 array_push ( $articles , $article );
332 print $this -> wrap ( self
:: STATUS_OK
, $articles );
336 function getConfig () {
338 "icons_dir" => ICONS_DIR
,
339 "icons_url" => ICONS_URL
);
341 $config [ "daemon_is_running" ] = file_is_locked ( "update_daemon.lock" );
343 $result = db_query ( $this -> link
, "SELECT COUNT(*) AS cf FROM
344 ttrss_feeds WHERE owner_uid = " . $_SESSION [ "uid" ]);
346 $num_feeds = db_fetch_result ( $result , 0 , "cf" );
348 $config [ "num_feeds" ] = ( int ) $num_feeds ;
350 print $this -> wrap ( self
:: STATUS_OK
, $config );
353 function updateFeed () {
354 require_once "include/rssfuncs.php" ;
356 $feed_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
358 update_rss_feed ( $this -> link
, $feed_id , true );
360 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
363 function catchupFeed () {
364 $feed_id = db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
365 $is_cat = db_escape_string ( $this -> link
, $_REQUEST [ "is_cat" ]);
367 catchup_feed ( $this -> link
, $feed_id , $is_cat );
369 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
373 $pref_name = db_escape_string ( $this -> link
, $_REQUEST [ "pref_name" ]);
375 print $this -> wrap ( self
:: STATUS_OK
, array ( "value" => get_pref ( $this -> link
, $pref_name )));
378 function getLabels () {
379 //$article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric);
381 $article_id = ( int ) $_REQUEST [ 'article_id' ];
385 $result = db_query ( $this -> link
, "SELECT id, caption, fg_color, bg_color
387 WHERE owner_uid = '" . $_SESSION [ 'uid' ]. "' ORDER BY caption" );
390 $article_labels = get_article_labels ( $this -> link
, $article_id );
392 $article_labels = array ();
394 while ( $line = db_fetch_assoc ( $result )) {
397 foreach ( $article_labels as $al ) {
398 if ( $al [ 0 ] == $line [ 'id' ]) {
404 array_push ( $rv , array (
405 "id" => ( int ) $line [ 'id' ],
406 "caption" => $line [ 'caption' ],
407 "fg_color" => $line [ 'fg_color' ],
408 "bg_color" => $line [ 'bg_color' ],
409 "checked" => $checked ));
412 print $this -> wrap ( self
:: STATUS_OK
, $rv );
415 function setArticleLabel () {
417 $article_ids = array_filter ( explode ( "," , db_escape_string ( $this -> link
, $_REQUEST [ "article_ids" ])), is_numeric
);
418 $label_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ 'label_id' ]);
419 $assign = ( bool ) db_escape_string ( $this -> link
, $_REQUEST [ 'assign' ]) == "true" ;
421 $label = db_escape_string ( $this -> link
, label_find_caption ( $this -> link
,
422 $label_id , $_SESSION [ "uid" ]));
428 foreach ( $article_ids as $id ) {
431 label_add_article ( $this -> link
, $id , $label , $_SESSION [ "uid" ]);
433 label_remove_article ( $this -> link
, $id , $label , $_SESSION [ "uid" ]);
440 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ,
441 "updated" => $num_updated ));
446 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'UNKNOWN_METHOD' ));
449 function shareToPublished () {
450 $title = db_escape_string ( $this -> link
, strip_tags ( $_REQUEST [ "title" ]));
451 $url = db_escape_string ( $this -> link
, strip_tags ( $_REQUEST [ "url" ]));
452 $content = db_escape_string ( $this -> link
, strip_tags ( $_REQUEST [ "content" ]));
454 if ( Article
:: create_published_article ( $this -> link
, $title , $url , $content , "" , $_SESSION [ "uid" ])) {
455 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => 'OK' ));
457 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'Publishing failed' ));
461 static function api_get_feeds ( $link , $cat_id , $unread_only , $limit , $offset , $include_nested = false ) {
467 if ( $cat_id == - 4 ||
$cat_id == - 2 ) {
468 $counters = getLabelCounters ( $link , true );
470 foreach ( array_values ( $counters ) as $cv ) {
472 $unread = $cv [ "counter" ];
474 if ( $unread ||
! $unread_only ) {
478 "title" => $cv [ "description" ],
479 "unread" => $cv [ "counter" ],
483 array_push ( $feeds , $row );
490 if ( $cat_id == - 4 ||
$cat_id == - 1 ) {
491 foreach ( array (- 1 , - 2 , - 3 , - 4 , - 6 , 0 ) as $i ) {
492 $unread = getFeedUnread ( $link , $i );
494 if ( $unread ||
! $unread_only ) {
495 $title = getFeedTitle ( $link , $i );
503 array_push ( $feeds , $row );
511 if ( $include_nested && $cat_id ) {
512 $result = db_query ( $link , "SELECT
513 id, title FROM ttrss_feed_categories
514 WHERE parent_cat = ' $cat_id ' AND owner_uid = " . $_SESSION [ "uid" ] .
515 " ORDER BY id, title" );
517 while ( $line = db_fetch_assoc ( $result )) {
518 $unread = getFeedUnread ( $link , $line [ "id" ], true ) +
519 getCategoryChildrenUnread ( $link , $line [ "id" ]);
521 if ( $unread ||
! $unread_only ) {
524 "title" => $line [ "title" ],
528 array_push ( $feeds , $row );
536 $limit_qpart = "LIMIT $limit OFFSET $offset " ;
541 if ( $cat_id == - 4 ||
$cat_id == - 3 ) {
542 $result = db_query ( $link , "SELECT
543 id, feed_url, cat_id, title, order_id, " .
544 SUBSTRING_FOR_DATE
. "(last_updated,1,19) AS last_updated
545 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION [ "uid" ] .
546 " ORDER BY cat_id, title " . $limit_qpart );
550 $cat_qpart = "cat_id = ' $cat_id '" ;
552 $cat_qpart = "cat_id IS NULL" ;
554 $result = db_query ( $link , "SELECT
555 id, feed_url, cat_id, title, order_id, " .
556 SUBSTRING_FOR_DATE
. "(last_updated,1,19) AS last_updated
557 FROM ttrss_feeds WHERE
558 $cat_qpart AND owner_uid = " . $_SESSION [ "uid" ] .
559 " ORDER BY cat_id, title " . $limit_qpart );
562 while ( $line = db_fetch_assoc ( $result )) {
564 $unread = getFeedUnread ( $link , $line [ "id" ]);
566 $has_icon = feed_has_icon ( $line [ 'id' ]);
568 if ( $unread ||
! $unread_only ) {
571 "feed_url" => $line [ "feed_url" ],
572 "title" => $line [ "title" ],
573 "id" => ( int ) $line [ "id" ],
574 "unread" => ( int ) $unread ,
575 "has_icon" => $has_icon ,
576 "cat_id" => ( int ) $line [ "cat_id" ],
577 "last_updated" => ( int ) strtotime ( $line [ "last_updated" ]),
578 "order_id" => ( int ) $line [ "order_id" ],
581 array_push ( $feeds , $row );
588 static function api_get_headlines ( $link , $feed_id , $limit , $offset ,
589 $filter , $is_cat , $show_excerpt , $show_content , $view_mode , $order ,
590 $include_attachments , $since_id ,
591 $search = "" , $search_mode = "" ,
592 $include_nested = false , $sanitize_content = true ) {
594 $qfh_ret = queryFeedHeadlines ( $link , $feed_id , $limit ,
595 $view_mode , $is_cat , $search , $search_mode ,
596 $order , $offset , 0 , false , $since_id , $include_nested );
598 $result = $qfh_ret [ 0 ];
599 $feed_title = $qfh_ret [ 1 ];
601 $headlines = array ();
603 while ( $line = db_fetch_assoc ( $result )) {
604 $is_updated = ( $line [ "last_read" ] == "" &&
605 ( $line [ "unread" ] != "t" && $line [ "unread" ] != "1" ));
607 $tags = explode ( "," , $line [ "tag_cache" ]);
608 $labels = json_decode ( $line [ "label_cache" ], true );
610 //if (!$tags) $tags = get_article_tags($link, $line["id"]);
611 //if (!$labels) $labels = get_article_labels($link, $line["id"]);
613 $headline_row = array (
614 "id" => ( int ) $line [ "id" ],
615 "unread" => sql_bool_to_bool ( $line [ "unread" ]),
616 "marked" => sql_bool_to_bool ( $line [ "marked" ]),
617 "published" => sql_bool_to_bool ( $line [ "published" ]),
618 "updated" => ( int ) strtotime ( $line [ "updated" ]),
619 "is_updated" => $is_updated ,
620 "title" => $line [ "title" ],
621 "link" => $line [ "link" ],
622 "feed_id" => $line [ "feed_id" ],
626 if ( $include_attachments )
627 $headline_row [ 'attachments' ] = get_article_enclosures ( $link ,
631 $excerpt = truncate_string ( strip_tags ( $line [ "content_preview" ]), 100 );
632 $headline_row [ "excerpt" ] = $excerpt ;
637 if ( $line [ "cached_content" ] != "" ) {
638 $line [ "content_preview" ] =& $line [ "cached_content" ];
641 if ( $sanitize_content ) {
642 $headline_row [ "content" ] = sanitize ( $link ,
643 $line [ "content_preview" ],
644 sql_bool_to_bool ( $line [ 'hide_images' ]),
645 false , $line [ "site_url" ]);
647 $headline_row [ "content" ] = $line [ "content_preview" ];
651 // unify label output to ease parsing
652 if ( $labels [ "no-labels" ] == 1 ) $labels = array ();
654 $headline_row [ "labels" ] = $labels ;
656 $headline_row [ "feed_title" ] = $line [ "feed_title" ];
658 $headline_row [ "comments_count" ] = ( int ) $line [ "num_comments" ];
659 $headline_row [ "comments_link" ] = $line [ "comments" ];
661 $headline_row [ "always_display_attachments" ] = sql_bool_to_bool ( $line [ "always_display_enclosures" ]);
664 foreach ( $pluginhost -> get_hooks ( $pluginhost :: HOOK_RENDER_ARTICLE_API
) as $p ) {
665 $headline_row = $p -> hook_render_article_api ( $headline_row );
668 array_push ( $headlines , $headline_row );
674 function unsubscribeFeed () {
675 $feed_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
677 $result = db_query ( $this -> link
, "SELECT id FROM ttrss_feeds WHERE
678 id = ' $feed_id ' AND owner_uid = " . $_SESSION [ "uid" ]);
680 if ( db_num_rows ( $result ) != 0 ) {
681 Pref_Feeds
:: remove_feed ( $this -> link
, $feed_id , $_SESSION [ "uid" ]);
682 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
684 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => "FEED_NOT_FOUND" ));
688 function subscribeToFeed () {
689 $feed_url = db_escape_string ( $this -> link
, $_REQUEST [ "feed_url" ]);
690 $category_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "category_id" ]);
691 $login = db_escape_string ( $this -> link
, $_REQUEST [ "login" ]);
692 $password = db_escape_string ( $this -> link
, $_REQUEST [ "password" ]);
695 $rc = subscribe_to_feed ( $this -> link
, $feed_url , $category_id ,
696 $login , $password , false );
698 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => $rc ));
700 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));