4 * Functions for the API
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
12 * API function wrapper: Shorten a URL
15 * @return array Result of API call
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 );
28 * API function wrapper: Stats about links (XX top, bottom, last, rand)
31 * @return array Result of API call
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 ) );
41 * API function wrapper: Just the global counts of shorturls and clicks
44 * @return array Result of API call
46 function yourls_api_action_db_stats() {
47 return yourls_apply_filter( 'api_result_db_stats', yourls_api_db_stats() );
51 * API function wrapper: Stats for a shorturl
54 * @return array Result of API call
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 ) );
62 * API function wrapper: Expand a short link
65 * @return array Result of API call
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 ) );
73 * API function wrapper: return version numbers
76 * @return array Result of API call
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 );
86 * Return API result. Dies after this
89 function yourls_api_output( $mode, $return ) {
90 if( isset( $return['simple'] ) ) {
91 $simple = $return['simple'];
92 unset( $return['simple'] );
95 yourls_do_action( 'pre_api_output', $mode, $return );
97 if( isset( $return['statusCode'] ) ) {
98 $code = $return['statusCode'];
99 } elseif ( isset( $return['errorCode'] ) ) {
100 $code = $return['errorCode'];
104 yourls_status_header( $code );
108 header( 'Content-type: application/javascript' );
109 echo $return['callback'] . '(' . json_encode( $return ) . ')';
113 header( 'Content-type: application/json' );
114 echo json_encode( $return );
118 header( 'Content-type: application/xml' );
119 echo yourls_xml_encode( $return );
124 if( isset( $simple ) )
129 yourls_do_action( 'api_output', $mode, $return );
135 * Return array for API stat requests
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 );
146 * Return array for counts of shorturls and clicks
149 function yourls_api_db_stats() {
151 'db-stats' => yourls_get_db_stats(),
153 'simple' => 'Need either XML or JSON format for stats',
154 'message' => 'success',
157 return yourls_apply_filter( 'api_db_stats', $return );
161 * Return array for API stat requests
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 );
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 );
174 * Expand short url to long url
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 );
181 $longurl = yourls_get_keyword_longurl( $keyword );
185 'keyword' => $keyword,
186 'shorturl' => YOURLS_SITE . "/$keyword",
187 'longurl' => $longurl,
188 'simple' => $longurl,
189 'message' => 'success',
194 'keyword' => $keyword,
195 'simple' => 'not found',
196 'message' => 'Error: short URL not found',
201 return yourls_apply_filter( 'api_expand', $return, $shorturl );