]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-api.php
Filterable charset in content-type headers.
[Github/YOURLS.git] / includes / functions-api.php
1 <?php
2 /*
3  * YOURLS
4  * Functions for the API
5  *
6  * Note about translation : this file should NOT be translation ready
7  * API messages and returns are supposed to be programmatically tested, so default English is expected
8  *
9  */
10
11 /**
12  * API function wrapper: Shorten a URL
13  *
14  * @since 1.6
15  * @return array Result of API call
16  */
17 function yourls_api_action_shorturl() {
18         $url = ( isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : '' );
19         $keyword = ( isset( $_REQUEST['keyword'] ) ? $_REQUEST['keyword'] : '' );
20         $title = ( isset( $_REQUEST['title'] ) ? $_REQUEST['title'] : '' );
21         $return = yourls_add_new_link( $url, $keyword, $title );
22         $return['simple'] = ( isset( $return['shorturl'] ) ? $return['shorturl'] : '' ); // This one will be used in case output mode is 'simple'
23         unset( $return['html'] ); // in API mode, no need for our internal HTML output
24         return yourls_apply_filter( 'api_result_shorturl', $return );
25 }
26
27 /**
28  * API function wrapper: Stats about links (XX top, bottom, last, rand)
29  *
30  * @since 1.6
31  * @return array Result of API call
32  */
33 function yourls_api_action_stats() {
34         $filter = ( isset( $_REQUEST['filter'] ) ? $_REQUEST['filter'] : '' );
35         $limit = ( isset( $_REQUEST['limit'] ) ? $_REQUEST['limit'] : '' );
36         $start = ( isset( $_REQUEST['start'] ) ? $_REQUEST['start'] : '' );
37         return yourls_apply_filter( 'api_result_stats', yourls_api_stats( $filter, $limit, $start ) );
38 }
39
40 /**
41  * API function wrapper: Just the global counts of shorturls and clicks
42  *
43  * @since 1.6
44  * @return array Result of API call
45  */
46 function yourls_api_action_db_stats() {
47         return yourls_apply_filter( 'api_result_db_stats', yourls_api_db_stats() );
48 }
49
50 /**
51  * API function wrapper: Stats for a shorturl
52  *
53  * @since 1.6
54  * @return array Result of API call
55  */
56 function yourls_api_action_url_stats() {
57         $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );
58         return yourls_apply_filter( 'api_result_url_stats', yourls_api_url_stats( $shorturl ) );
59 }
60
61 /**
62  * API function wrapper: Expand a short link
63  *
64  * @since 1.6
65  * @return array Result of API call
66  */
67 function yourls_api_action_expand() {
68         $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );
69         return yourls_apply_filter( 'api_result_expand', yourls_api_expand( $shorturl ) );
70 }
71
72 /**
73  * API function wrapper: return version numbers
74  *
75  * @since 1.6
76  * @return array Result of API call
77  */
78 function yourls_api_action_version() {
79         $return['version'] = $return['simple'] = YOURLS_VERSION;
80         if( isset( $_REQUEST['db'] ) && $_REQUEST['db'] == 1 )
81                 $return['db_version'] = YOURLS_DB_VERSION;
82         return yourls_apply_filter( 'api_result_version', $return );
83 }
84  
85 /**
86  * Return API result. Dies after this
87  *
88  */
89 function yourls_api_output( $mode, $return ) {
90         if( isset( $return['simple'] ) ) {
91                 $simple = $return['simple'];
92                 unset( $return['simple'] );
93         }
94         
95         yourls_do_action( 'pre_api_output', $mode, $return );
96         
97         if( isset( $return['statusCode'] ) ) {
98                 $code = $return['statusCode'];
99         } elseif ( isset( $return['errorCode'] ) ) {
100                 $code = $return['errorCode'];
101         } else {
102                 $code = 200;
103         }
104         yourls_status_header( $code );
105         
106         switch ( $mode ) {
107                 case 'jsonp':
108                         yourls_content_type_header( 'application/javascript' );
109                         echo $return['callback'] . '(' . json_encode( $return ) . ')';
110                         break;
111         
112                 case 'json':
113                         yourls_content_type_header( 'application/json' );
114                         echo json_encode( $return );
115                         break;
116                 
117                 case 'xml':
118                         yourls_content_type_header( 'application/xml' );
119                         echo yourls_xml_encode( $return );
120                         break;
121                         
122                 case 'simple':
123                 default:
124                         yourls_content_type_header( 'text/plain' );
125                         if( isset( $simple ) )
126                                 echo $simple;
127                         break;
128         }
129
130         yourls_do_action( 'api_output', $mode, $return );
131         
132         die();
133 }
134
135 /**
136  * Return array for API stat requests
137  *
138  */
139 function yourls_api_stats( $filter = 'top', $limit = 10, $start = 0 ) {
140         $return = yourls_get_stats( $filter, $limit, $start );
141         $return['simple']  = 'Need either XML or JSON format for stats';
142         $return['message'] = 'success';
143         return yourls_apply_filter( 'api_stats', $return, $filter, $limit, $start );
144 }
145
146 /**
147  * Return array for counts of shorturls and clicks
148  *
149  */
150 function yourls_api_db_stats() {
151         $return = array(
152                 'db-stats'   => yourls_get_db_stats(),
153                 'statusCode' => 200,
154                 'simple'     => 'Need either XML or JSON format for stats',
155                 'message'    => 'success',
156         );
157                 
158         return yourls_apply_filter( 'api_db_stats', $return );
159 }
160
161 /**
162  * Return array for API stat requests
163  *
164  */
165 function yourls_api_url_stats( $shorturl ) {
166         $keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
167         $keyword = yourls_sanitize_string( $keyword );
168
169         $return = yourls_get_link_stats( $keyword );
170         $return['simple']  = 'Need either XML or JSON format for stats';
171         return yourls_apply_filter( 'api_url_stats', $return, $shorturl );
172 }
173
174 /**
175  * Expand short url to long url
176  *
177  */
178 function yourls_api_expand( $shorturl ) {
179         $keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
180         $keyword = yourls_sanitize_string( $keyword );
181         
182         $longurl = yourls_get_keyword_longurl( $keyword );
183         
184         if( $longurl ) {
185                 $return = array(
186                         'keyword'   => $keyword,
187                         'shorturl'  => YOURLS_SITE . "/$keyword",
188                         'longurl'   => $longurl,
189                         'simple'    => $longurl,
190                         'message'   => 'success',
191                         'statusCode' => 200,
192                 );
193         } else {
194                 $return = array(
195                         'keyword'   => $keyword,
196                         'simple'    => 'not found',
197                         'message'   => 'Error: short URL not found',
198                         'errorCode' => 404,
199                 );
200         }
201         
202         return yourls_apply_filter( 'api_expand', $return, $shorturl );
203 }