]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-api.php
Fix end of line
[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                         header( 'Content-type: application/javascript' );
109                         echo $return['callback'] . '(' . json_encode( $return ) . ')';
110                         break;
111     
112                 case 'json':
113                         header( 'Content-type: application/json' );
114                         echo json_encode( $return );
115                         break;
116                 
117                 case 'xml':
118                         header( 'Content-type: application/xml' );
119                         echo yourls_xml_encode( $return );
120                         break;
121                         
122                 case 'simple':
123                 default:
124                         if( isset( $simple ) )
125                                 echo $simple;
126                         break;
127         }
128
129         yourls_do_action( 'api_output', $mode, $return );
130         
131         die();
132 }
133
134 /**
135  * Return array for API stat requests
136  *
137  */
138 function yourls_api_stats( $filter = 'top', $limit = 10, $start = 0 ) {
139         $return = yourls_get_stats( $filter, $limit, $start );
140         $return['simple']  = 'Need either XML or JSON format for stats';
141         $return['message'] = 'success';
142         return yourls_apply_filter( 'api_stats', $return, $filter, $limit, $start );
143 }
144
145 /**
146  * Return array for counts of shorturls and clicks
147  *
148  */
149 function yourls_api_db_stats() {
150         $return = array(
151                 'db-stats'   => yourls_get_db_stats(),
152                 'statusCode' => 200,
153                 'simple'     => 'Need either XML or JSON format for stats',
154                 'message'    => 'success',
155         );
156                 
157         return yourls_apply_filter( 'api_db_stats', $return );
158 }
159
160 /**
161  * Return array for API stat requests
162  *
163  */
164 function yourls_api_url_stats( $shorturl ) {
165         $keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
166         $keyword = yourls_sanitize_string( $keyword );
167
168         $return = yourls_get_link_stats( $keyword );
169         $return['simple']  = 'Need either XML or JSON format for stats';
170         return yourls_apply_filter( 'api_url_stats', $return, $shorturl );
171 }
172
173 /**
174  * Expand short url to long url
175  *
176  */
177 function yourls_api_expand( $shorturl ) {
178         $keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
179         $keyword = yourls_sanitize_string( $keyword );
180         
181         $longurl = yourls_get_keyword_longurl( $keyword );
182         
183         if( $longurl ) {
184                 $return = array(
185                         'keyword'   => $keyword,
186                         'shorturl'  => YOURLS_SITE . "/$keyword",
187                         'longurl'   => $longurl,
188                         'simple'    => $longurl,
189                         'message'   => 'success',
190                         'statusCode' => 200,
191                 );
192         } else {
193                 $return = array(
194                         'keyword'   => $keyword,
195                         'simple'    => 'not found',
196                         'message'   => 'Error: short URL not found',
197                         'errorCode' => 404,
198                 );
199         }
200         
201         return yourls_apply_filter( 'api_expand', $return, $shorturl );
202 }