Skip to content
Ignoring revisions in .git-blame-ignore-revs.

Latest commit

 

History

History
247 lines (216 loc) · 7.76 KB

File metadata and controls

247 lines (216 loc) · 7.76 KB
 
1
<?php
2
/*
3
* YOURLS
4
* Functions for the API
Feb 10, 2013
Feb 10, 2013
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
*
Apr 7, 2013
Apr 7, 2013
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() {
Sep 26, 2025
Sep 26, 2025
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 );
Apr 7, 2013
Apr 7, 2013
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() {
Sep 26, 2025
Sep 26, 2025
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 ) );
Apr 7, 2013
Apr 7, 2013
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() {
Sep 26, 2025
Sep 26, 2025
47
return yourls_apply_filter( 'api_result_db_stats', yourls_api_db_stats() );
Apr 7, 2013
Apr 7, 2013
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() {
Sep 26, 2025
Sep 26, 2025
57
$shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );
58
return yourls_apply_filter( 'api_result_url_stats', yourls_api_url_stats( $shorturl ) );
Apr 7, 2013
Apr 7, 2013
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() {
Sep 26, 2025
Sep 26, 2025
68
$shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' );
69
return yourls_apply_filter( 'api_result_expand', yourls_api_expand( $shorturl ) );
Apr 7, 2013
Apr 7, 2013
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() {
Sep 26, 2025
Sep 26, 2025
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 );
May 27, 2015
May 27, 2015
84
Feb 13, 2013
Feb 13, 2013
85
/**
May 27, 2015
May 27, 2015
86
* Output and return API result
Feb 13, 2013
Feb 13, 2013
87
*
May 27, 2015
May 27, 2015
88
* This function will echo (or only return if asked) an array as JSON, JSONP or XML. If the array has a
89
* 'simple' key, it can also output that key as unformatted text if expected output mode is 'simple'
90
*
91
* Most likely, script should not do anything after outputting this
92
*
93
* @since 1.6
94
*
95
* @param string $mode Expected output mode ('json', 'jsonp', 'xml', 'simple')
96
* @param array $output Array of things to output
97
* @param bool $send_headers Optional, default true: Whether a headers (status, content type) should be sent or not
98
* @param bool $echo Optional, default true: Whether the output should be outputted or just returned
99
* @return string API output, as an XML / JSON / JSONP / raw text string
Sep 2, 2017
Sep 2, 2017
100
*/
May 27, 2015
May 27, 2015
101
function yourls_api_output( $mode, $output, $send_headers = true, $echo = true ) {
Sep 26, 2025
Sep 26, 2025
102
if( isset( $output['simple'] ) ) {
103
$simple = $output['simple'];
104
unset( $output['simple'] );
105
}
Sep 2, 2017
Sep 2, 2017
106
Sep 26, 2025
Sep 26, 2025
107
yourls_do_action( 'pre_api_output', $mode, $output, $send_headers, $echo );
Sep 2, 2017
Sep 2, 2017
108
May 27, 2015
May 27, 2015
109
if( $send_headers ) {
110
if( isset( $output['statusCode'] ) ) {
111
$code = $output['statusCode'];
112
} elseif ( isset( $output['errorCode'] ) ) {
113
$code = $output['errorCode'];
114
} else {
115
$code = 200;
116
}
117
yourls_status_header( $code );
118
}
Sep 2, 2017
Sep 2, 2017
119
May 27, 2015
May 27, 2015
120
$result = '';
Sep 2, 2017
Sep 2, 2017
121
Sep 26, 2025
Sep 26, 2025
122
switch ( $mode ) {
123
case 'jsonp':
May 27, 2015
May 27, 2015
124
if( $send_headers )
125
yourls_content_type_header( 'application/javascript' );
Sep 2, 2017
Sep 2, 2017
126
Dec 22, 2025
Dec 22, 2025
127
$callback = isset( $output['callback'] ) ? yourls_validate_jsonp_callback($output['callback'] ) : '';
128
if( $callback === false ) {
129
yourls_status_header( 400 );
130
$result = json_encode( ['errorCode' => '400', 'error' => 'Invalid callback parameter'] );
131
} else {
132
$result = $callback . '(' . json_encode( $output ) . ')';
133
}
134
Sep 26, 2025
Sep 26, 2025
135
break;
Sep 2, 2017
Sep 2, 2017
136
Sep 26, 2025
Sep 26, 2025
137
case 'json':
May 27, 2015
May 27, 2015
138
if( $send_headers )
139
yourls_content_type_header( 'application/json' );
Sep 2, 2017
Sep 2, 2017
140
Sep 26, 2025
Sep 26, 2025
141
$result = json_encode( $output );
142
break;
Sep 2, 2017
Sep 2, 2017
143
Sep 26, 2025
Sep 26, 2025
144
case 'xml':
May 27, 2015
May 27, 2015
145
if( $send_headers )
146
yourls_content_type_header( 'application/xml' );
Sep 2, 2017
Sep 2, 2017
147
Sep 26, 2025
Sep 26, 2025
148
$result = yourls_xml_encode( $output );
149
break;
Sep 2, 2017
Sep 2, 2017
150
Sep 26, 2025
Sep 26, 2025
151
case 'simple':
152
default:
May 27, 2015
May 27, 2015
153
if( $send_headers )
154
yourls_content_type_header( 'text/plain' );
Sep 2, 2017
Sep 2, 2017
155
Sep 26, 2025
Sep 26, 2025
156
$result = isset( $simple ) ? $simple : '';
157
break;
158
}
Apr 7, 2013
Apr 7, 2013
159
May 27, 2015
May 27, 2015
160
if( $echo ) {
161
echo $result;
162
}
Sep 2, 2017
Sep 2, 2017
163
Sep 26, 2025
Sep 26, 2025
164
yourls_do_action( 'api_output', $mode, $output, $send_headers, $echo );
Sep 2, 2017
Sep 2, 2017
165
May 27, 2015
May 27, 2015
166
return $result;
Apr 7, 2013
Apr 7, 2013
168
Feb 13, 2013
Feb 13, 2013
169
/**
170
* Return array for API stat requests
171
*
May 5, 2022
May 5, 2022
172
* @param string $filter either "top", "bottom" , "rand" or "last"
173
* @param int $limit maximum number of links to return
174
* @param int $start offset
175
* @return array
Feb 13, 2013
Feb 13, 2013
176
*/
May 5, 2022
May 5, 2022
177
function yourls_api_stats($filter = 'top', $limit = 10, $start = 0 ) {
Sep 26, 2025
Sep 26, 2025
178
$return = yourls_get_stats( $filter, $limit, $start );
179
$return['simple'] = 'Need either XML or JSON format for stats';
180
$return['message'] = 'success';
181
return yourls_apply_filter( 'api_stats', $return, $filter, $limit, $start );
Apr 7, 2013
Apr 7, 2013
183
Feb 13, 2013
Feb 13, 2013
184
/**
185
* Return array for counts of shorturls and clicks
186
*
May 5, 2022
May 5, 2022
187
* @return array
Feb 13, 2013
Feb 13, 2013
188
*/
189
function yourls_api_db_stats() {
Sep 26, 2025
Sep 26, 2025
190
$return = array(
191
'db-stats' => yourls_get_db_stats(),
192
'statusCode' => '200',
193
'simple' => 'Need either XML or JSON format for stats',
194
'message' => 'success',
195
);
196
197
return yourls_apply_filter( 'api_db_stats', $return );
Apr 7, 2013
Apr 7, 2013
199
Feb 13, 2013
Feb 13, 2013
200
/**
201
* Return array for API stat requests
202
*
May 5, 2022
May 5, 2022
203
* @param string $shorturl Short URL to check
204
* @return array
Feb 13, 2013
Feb 13, 2013
205
*/
206
function yourls_api_url_stats( $shorturl ) {
Sep 26, 2025
Sep 26, 2025
207
$keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
208
$keyword = yourls_sanitize_keyword( $keyword );
Apr 7, 2013
Apr 7, 2013
209
Sep 26, 2025
Sep 26, 2025
210
$return = yourls_get_keyword_stats( $keyword );
211
$return['simple'] = 'Need either XML or JSON format for stats';
212
return yourls_apply_filter( 'api_url_stats', $return, $shorturl );
Apr 7, 2013
Apr 7, 2013
214
Feb 13, 2013
Feb 13, 2013
215
/**
216
* Expand short url to long url
217
*
May 5, 2022
May 5, 2022
218
* @param string $shorturl Short URL to expand
219
* @return array
Feb 13, 2013
Feb 13, 2013
220
*/
221
function yourls_api_expand( $shorturl ) {
Sep 26, 2025
Sep 26, 2025
222
$keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
223
$keyword = yourls_sanitize_keyword( $keyword );
Sep 2, 2017
Sep 2, 2017
224
Sep 26, 2025
Sep 26, 2025
225
$longurl = yourls_get_keyword_longurl( $keyword );
Sep 2, 2017
Sep 2, 2017
226
Sep 26, 2025
Sep 26, 2025
227
if( $longurl ) {
228
$return = array(
229
'keyword' => $keyword,
230
'shorturl' => yourls_link($keyword),
231
'longurl' => $longurl,
Oct 25, 2014
Oct 25, 2014
232
'title' => yourls_get_keyword_title( $keyword ),
Sep 26, 2025
Sep 26, 2025
233
'simple' => $longurl,
234
'message' => 'success',
235
'statusCode' => '200',
236
);
237
} else {
238
$return = array(
239
'keyword' => $keyword,
240
'simple' => 'not found',
241
'message' => 'Error: short URL not found',
242
'errorCode' => '404',
243
);
244
}
245
246
return yourls_apply_filter( 'api_expand', $return, $shorturl );