]>
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 FROM ttrss_feed_categories
138 WHERE $nested_qpart AND owner_uid = " .
143 while ( $line = db_fetch_assoc ( $result )) {
144 if ( $line [ "num_feeds" ] > 0 ) {
145 $unread = getFeedUnread ( $this -> link
, $line [ "id" ], true );
148 $unread +
= getCategoryChildrenUnread ( $this -> link
, $line [ "id" ]);
150 if ( $unread ||
! $unread_only ) {
151 array_push ( $cats , array ( "id" => $line [ "id" ],
152 "title" => $line [ "title" ],
154 "order_id" => ( int ) $line [ "order_id" ],
160 foreach ( array (- 2 ,- 1 , 0 ) as $cat_id ) {
161 $unread = getFeedUnread ( $this -> link
, $cat_id , true );
163 if ( $unread ||
! $unread_only ) {
164 array_push ( $cats , array ( "id" => $cat_id ,
165 "title" => getCategoryTitle ( $this -> link
, $cat_id ),
166 "unread" => $unread ));
170 print $this -> wrap ( self
:: STATUS_OK
, $cats );
173 function getHeadlines () {
174 $feed_id = db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
175 if ( $feed_id != "" ) {
177 $limit = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "limit" ]);
179 if (! $limit ||
$limit >= 60 ) $limit = 60 ;
181 $offset = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "skip" ]);
182 $filter = db_escape_string ( $this -> link
, $_REQUEST [ "filter" ]);
183 $is_cat = sql_bool_to_bool ( $_REQUEST [ "is_cat" ]);
184 $show_excerpt = sql_bool_to_bool ( $_REQUEST [ "show_excerpt" ]);
185 $show_content = sql_bool_to_bool ( $_REQUEST [ "show_content" ]);
186 /* all_articles, unread, adaptive, marked, updated */
187 $view_mode = db_escape_string ( $this -> link
, $_REQUEST [ "view_mode" ]);
188 $include_attachments = sql_bool_to_bool ( $_REQUEST [ "include_attachments" ]);
189 $since_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "since_id" ]);
190 $include_nested = sql_bool_to_bool ( $_REQUEST [ "include_nested" ]);
191 $sanitize_content = true ;
193 /* do not rely on params below */
195 $search = db_escape_string ( $this -> link
, $_REQUEST [ "search" ]);
196 $search_mode = db_escape_string ( $this -> link
, $_REQUEST [ "search_mode" ]);
198 $headlines = $this -> api_get_headlines ( $this -> link
, $feed_id , $limit , $offset ,
199 $filter , $is_cat , $show_excerpt , $show_content , $view_mode , false ,
200 $include_attachments , $since_id , $search , $search_mode ,
201 $include_nested , $sanitize_content );
203 print $this -> wrap ( self
:: STATUS_OK
, $headlines );
205 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));
209 function updateArticle () {
210 $article_ids = array_filter ( explode ( "," , db_escape_string ( $this -> link
, $_REQUEST [ "article_ids" ])), is_numeric
);
211 $mode = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "mode" ]);
212 $data = db_escape_string ( $this -> link
, $_REQUEST [ "data" ]);
213 $field_raw = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "field" ]);
218 switch ( $field_raw ) {
221 $additional_fields = ",last_marked = NOW()" ;
224 $field = "published" ;
225 $additional_fields = ",last_published = NOW()" ;
229 $additional_fields = ",last_read = NOW()" ;
243 $set_to = "NOT $field " ;
247 if ( $field == "note" ) $set_to = "' $data '" ;
249 if ( $field && $set_to && count ( $article_ids ) > 0 ) {
251 $article_ids = join ( ", " , $article_ids );
253 $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" ]);
255 $num_updated = db_affected_rows ( $this -> link
, $result );
257 if ( $num_updated > 0 && $field == "unread" ) {
258 $result = db_query ( $this -> link
, "SELECT DISTINCT feed_id FROM ttrss_user_entries
259 WHERE ref_id IN ( $article_ids )" );
261 while ( $line = db_fetch_assoc ( $result )) {
262 ccache_update ( $this -> link
, $line [ "feed_id" ], $_SESSION [ "uid" ]);
266 if ( $num_updated > 0 && $field == "published" ) {
267 if ( PUBSUBHUBBUB_HUB
) {
268 $rss_link = get_self_url_prefix () .
269 "/public.php?op=rss&id=-2&key=" .
270 get_feed_access_key ( $this -> link
, - 2 , false );
272 $p = new Publisher ( PUBSUBHUBBUB_HUB
);
273 $pubsub_result = $p -> publish_update ( $rss_link );
277 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ,
278 "updated" => $num_updated ));
281 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));
286 function getArticle () {
288 $article_id = join ( "," , array_filter ( explode ( "," , db_escape_string ( $this -> link
, $_REQUEST [ "article_id" ])), is_numeric
));
290 $query = "SELECT id,title,link,content,cached_content,feed_id,comments,int_id,
291 marked,unread,published,
292 " . SUBSTRING_FOR_DATE
. "(updated,1,16) as updated,
294 FROM ttrss_entries,ttrss_user_entries
295 WHERE id IN ( $article_id ) AND ref_id = id AND owner_uid = " .
298 $result = db_query ( $this -> link
, $query );
302 if ( db_num_rows ( $result ) != 0 ) {
304 while ( $line = db_fetch_assoc ( $result )) {
306 $attachments = get_article_enclosures ( $this -> link
, $line [ 'id' ]);
310 "title" => $line [ "title" ],
311 "link" => $line [ "link" ],
312 "labels" => get_article_labels ( $this -> link
, $line [ 'id' ]),
313 "unread" => sql_bool_to_bool ( $line [ "unread" ]),
314 "marked" => sql_bool_to_bool ( $line [ "marked" ]),
315 "published" => sql_bool_to_bool ( $line [ "published" ]),
316 "comments" => $line [ "comments" ],
317 "author" => $line [ "author" ],
318 "updated" => ( int ) strtotime ( $line [ "updated" ]),
319 "content" => $line [ "cached_content" ] != "" ?
$line [ "cached_content" ] : $line [ "content" ],
320 "feed_id" => $line [ "feed_id" ],
321 "attachments" => $attachments
324 array_push ( $articles , $article );
329 print $this -> wrap ( self
:: STATUS_OK
, $articles );
333 function getConfig () {
335 "icons_dir" => ICONS_DIR
,
336 "icons_url" => ICONS_URL
);
338 $config [ "daemon_is_running" ] = file_is_locked ( "update_daemon.lock" );
340 $result = db_query ( $this -> link
, "SELECT COUNT(*) AS cf FROM
341 ttrss_feeds WHERE owner_uid = " . $_SESSION [ "uid" ]);
343 $num_feeds = db_fetch_result ( $result , 0 , "cf" );
345 $config [ "num_feeds" ] = ( int ) $num_feeds ;
347 print $this -> wrap ( self
:: STATUS_OK
, $config );
350 function updateFeed () {
351 $feed_id = db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
353 update_rss_feed ( $this -> link
, $feed_id , true );
355 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
358 function catchupFeed () {
359 $feed_id = db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
360 $is_cat = db_escape_string ( $this -> link
, $_REQUEST [ "is_cat" ]);
362 catchup_feed ( $this -> link
, $feed_id , $is_cat );
364 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
368 $pref_name = db_escape_string ( $this -> link
, $_REQUEST [ "pref_name" ]);
370 print $this -> wrap ( self
:: STATUS_OK
, array ( "value" => get_pref ( $this -> link
, $pref_name )));
373 function getLabels () {
374 //$article_ids = array_filter(explode(",", db_escape_string($this->link, $_REQUEST["article_ids"])), is_numeric);
376 $article_id = ( int ) $_REQUEST [ 'article_id' ];
380 $result = db_query ( $this -> link
, "SELECT id, caption, fg_color, bg_color
382 WHERE owner_uid = '" . $_SESSION [ 'uid' ]. "' ORDER BY caption" );
385 $article_labels = get_article_labels ( $this -> link
, $article_id );
387 $article_labels = array ();
389 while ( $line = db_fetch_assoc ( $result )) {
392 foreach ( $article_labels as $al ) {
393 if ( $al [ 0 ] == $line [ 'id' ]) {
399 array_push ( $rv , array (
400 "id" => ( int ) $line [ 'id' ],
401 "caption" => $line [ 'caption' ],
402 "fg_color" => $line [ 'fg_color' ],
403 "bg_color" => $line [ 'bg_color' ],
404 "checked" => $checked ));
407 print $this -> wrap ( self
:: STATUS_OK
, $rv );
410 function setArticleLabel () {
412 $article_ids = array_filter ( explode ( "," , db_escape_string ( $this -> link
, $_REQUEST [ "article_ids" ])), is_numeric
);
413 $label_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ 'label_id' ]);
414 $assign = ( bool ) db_escape_string ( $this -> link
, $_REQUEST [ 'assign' ]) == "true" ;
416 $label = db_escape_string ( $this -> link
, label_find_caption ( $this -> link
,
417 $label_id , $_SESSION [ "uid" ]));
423 foreach ( $article_ids as $id ) {
426 label_add_article ( $this -> link
, $id , $label , $_SESSION [ "uid" ]);
428 label_remove_article ( $this -> link
, $id , $label , $_SESSION [ "uid" ]);
435 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ,
436 "updated" => $num_updated ));
441 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'UNKNOWN_METHOD' ));
444 function shareToPublished () {
445 $title = db_escape_string ( $this -> link
, strip_tags ( $_REQUEST [ "title" ]));
446 $url = db_escape_string ( $this -> link
, strip_tags ( $_REQUEST [ "url" ]));
447 $content = db_escape_string ( $this -> link
, strip_tags ( $_REQUEST [ "content" ]));
449 if ( Article
:: create_published_article ( $this -> link
, $title , $url , $content , "" , $_SESSION [ "uid" ])) {
450 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => 'OK' ));
452 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'Publishing failed' ));
456 static function api_get_feeds ( $link , $cat_id , $unread_only , $limit , $offset , $include_nested = false ) {
462 if ( $cat_id == - 4 ||
$cat_id == - 2 ) {
463 $counters = getLabelCounters ( $link , true );
465 foreach ( array_values ( $counters ) as $cv ) {
467 $unread = $cv [ "counter" ];
469 if ( $unread ||
! $unread_only ) {
473 "title" => $cv [ "description" ],
474 "unread" => $cv [ "counter" ],
478 array_push ( $feeds , $row );
485 if ( $cat_id == - 4 ||
$cat_id == - 1 ) {
486 foreach ( array (- 1 , - 2 , - 3 , - 4 , - 6 , 0 ) as $i ) {
487 $unread = getFeedUnread ( $link , $i );
489 if ( $unread ||
! $unread_only ) {
490 $title = getFeedTitle ( $link , $i );
498 array_push ( $feeds , $row );
506 if ( $include_nested && $cat_id ) {
507 $result = db_query ( $link , "SELECT
508 id, title FROM ttrss_feed_categories
509 WHERE parent_cat = ' $cat_id ' AND owner_uid = " . $_SESSION [ "uid" ] .
510 " ORDER BY id, title" );
512 while ( $line = db_fetch_assoc ( $result )) {
513 $unread = getFeedUnread ( $link , $line [ "id" ], true ) +
514 getCategoryChildrenUnread ( $link , $line [ "id" ]);
516 if ( $unread ||
! $unread_only ) {
519 "title" => $line [ "title" ],
523 array_push ( $feeds , $row );
531 $limit_qpart = "LIMIT $limit OFFSET $offset " ;
536 if ( $cat_id == - 4 ||
$cat_id == - 3 ) {
537 $result = db_query ( $link , "SELECT
538 id, feed_url, cat_id, title, order_id, " .
539 SUBSTRING_FOR_DATE
. "(last_updated,1,19) AS last_updated
540 FROM ttrss_feeds WHERE owner_uid = " . $_SESSION [ "uid" ] .
541 " ORDER BY cat_id, title " . $limit_qpart );
545 $cat_qpart = "cat_id = ' $cat_id '" ;
547 $cat_qpart = "cat_id IS NULL" ;
549 $result = db_query ( $link , "SELECT
550 id, feed_url, cat_id, title, order_id, " .
551 SUBSTRING_FOR_DATE
. "(last_updated,1,19) AS last_updated
552 FROM ttrss_feeds WHERE
553 $cat_qpart AND owner_uid = " . $_SESSION [ "uid" ] .
554 " ORDER BY cat_id, title " . $limit_qpart );
557 while ( $line = db_fetch_assoc ( $result )) {
559 $unread = getFeedUnread ( $link , $line [ "id" ]);
561 $has_icon = feed_has_icon ( $line [ 'id' ]);
563 if ( $unread ||
! $unread_only ) {
566 "feed_url" => $line [ "feed_url" ],
567 "title" => $line [ "title" ],
568 "id" => ( int ) $line [ "id" ],
569 "unread" => ( int ) $unread ,
570 "has_icon" => $has_icon ,
571 "cat_id" => ( int ) $line [ "cat_id" ],
572 "last_updated" => ( int ) strtotime ( $line [ "last_updated" ]),
573 "order_id" => ( int ) $line [ "order_id" ],
576 array_push ( $feeds , $row );
583 static function api_get_headlines ( $link , $feed_id , $limit , $offset ,
584 $filter , $is_cat , $show_excerpt , $show_content , $view_mode , $order ,
585 $include_attachments , $since_id ,
586 $search = "" , $search_mode = "" ,
587 $include_nested = false , $sanitize_content = true ) {
589 $qfh_ret = queryFeedHeadlines ( $link , $feed_id , $limit ,
590 $view_mode , $is_cat , $search , $search_mode ,
591 $order , $offset , 0 , false , $since_id , $include_nested );
593 $result = $qfh_ret [ 0 ];
594 $feed_title = $qfh_ret [ 1 ];
596 $headlines = array ();
598 while ( $line = db_fetch_assoc ( $result )) {
599 $is_updated = ( $line [ "last_read" ] == "" &&
600 ( $line [ "unread" ] != "t" && $line [ "unread" ] != "1" ));
602 $tags = explode ( "," , $line [ "tag_cache" ]);
603 $labels = json_decode ( $line [ "label_cache" ], true );
605 //if (!$tags) $tags = get_article_tags($link, $line["id"]);
606 //if (!$labels) $labels = get_article_labels($link, $line["id"]);
608 $headline_row = array (
609 "id" => ( int ) $line [ "id" ],
610 "unread" => sql_bool_to_bool ( $line [ "unread" ]),
611 "marked" => sql_bool_to_bool ( $line [ "marked" ]),
612 "published" => sql_bool_to_bool ( $line [ "published" ]),
613 "updated" => ( int ) strtotime ( $line [ "updated" ]),
614 "is_updated" => $is_updated ,
615 "title" => $line [ "title" ],
616 "link" => $line [ "link" ],
617 "feed_id" => $line [ "feed_id" ],
621 if ( $include_attachments )
622 $headline_row [ 'attachments' ] = get_article_enclosures ( $link ,
626 $excerpt = truncate_string ( strip_tags ( $line [ "content_preview" ]), 100 );
627 $headline_row [ "excerpt" ] = $excerpt ;
632 if ( $line [ "cached_content" ] != "" ) {
633 $line [ "content_preview" ] =& $line [ "cached_content" ];
636 if ( $sanitize_content ) {
637 $headline_row [ "content" ] = sanitize ( $link ,
638 $line [ "content_preview" ],
639 sql_bool_to_bool ( $line [ 'hide_images' ]),
640 false , $line [ "site_url" ]);
642 $headline_row [ "content" ] = $line [ "content_preview" ];
646 // unify label output to ease parsing
647 if ( $labels [ "no-labels" ] == 1 ) $labels = array ();
649 $headline_row [ "labels" ] = $labels ;
651 $headline_row [ "feed_title" ] = $line [ "feed_title" ];
653 $headline_row [ "comments_count" ] = ( int ) $line [ "num_comments" ];
654 $headline_row [ "comments_link" ] = $line [ "comments" ];
656 $headline_row [ "always_display_attachments" ] = sql_bool_to_bool ( $line [ "always_display_enclosures" ]);
659 foreach ( $pluginhost -> get_hooks ( $pluginhost :: HOOK_RENDER_ARTICLE_API
) as $p ) {
660 $headline_row = $p -> hook_render_article_api ( $headline_row );
663 array_push ( $headlines , $headline_row );
669 function unsubscribeFeed () {
670 $feed_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "feed_id" ]);
672 $result = db_query ( $this -> link
, "SELECT id FROM ttrss_feeds WHERE
673 id = ' $feed_id ' AND owner_uid = " . $_SESSION [ "uid" ]);
675 if ( db_num_rows ( $result ) != 0 ) {
676 Pref_Feeds
:: remove_feed ( $this -> link
, $feed_id , $_SESSION [ "uid" ]);
677 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => "OK" ));
679 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => "FEED_NOT_FOUND" ));
683 function subscribeToFeed () {
684 $feed_url = db_escape_string ( $this -> link
, $_REQUEST [ "feed_url" ]);
685 $category_id = ( int ) db_escape_string ( $this -> link
, $_REQUEST [ "category_id" ]);
686 $login = db_escape_string ( $this -> link
, $_REQUEST [ "login" ]);
687 $password = db_escape_string ( $this -> link
, $_REQUEST [ "password" ]);
690 $rc = subscribe_to_feed ( $this -> link
, $feed_url , $category_id ,
691 $login , $password , false );
693 print $this -> wrap ( self
:: STATUS_OK
, array ( "status" => $rc ));
695 print $this -> wrap ( self
:: STATUS_ERR
, array ( "error" => 'INCORRECT_USAGE' ));