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 | 1 | $url = ( isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : '' ); |
|
19 | 1 | $keyword = ( isset( $_REQUEST['keyword'] ) ? $_REQUEST['keyword'] : '' ); |
|
20 | 1 | $title = ( isset( $_REQUEST['title'] ) ? $_REQUEST['title'] : '' ); |
|
21 | 1 | $return = yourls_add_new_link( $url, $keyword, $title ); |
|
22 | 1 | $return['simple'] = ( isset( $return['shorturl'] ) ? $return['shorturl'] : '' ); // This one will be used in case output mode is 'simple' |
|
23 | 1 | unset( $return['html'] ); // in API mode, no need for our internal HTML output |
|
24 | 1 | 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 | 1 | $filter = ( isset( $_REQUEST['filter'] ) ? $_REQUEST['filter'] : '' ); |
|
35 | 1 | $limit = ( isset( $_REQUEST['limit'] ) ? $_REQUEST['limit'] : '' ); |
|
36 | 1 | $start = ( isset( $_REQUEST['start'] ) ? $_REQUEST['start'] : '' ); |
|
37 | 1 | 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 | 1 | 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 | 1 | $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' ); |
|
58 | 1 | 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 | 1 | $shorturl = ( isset( $_REQUEST['shorturl'] ) ? $_REQUEST['shorturl'] : '' ); |
|
69 | 1 | 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 | 1 | $return['version'] = $return['simple'] = YOURLS_VERSION; |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
80 | 1 | if( isset( $_REQUEST['db'] ) && $_REQUEST['db'] == 1 ) |
|
81 | $return['db_version'] = YOURLS_DB_VERSION; |
||
82 | 1 | return yourls_apply_filter( 'api_result_version', $return ); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Output and return API result |
||
87 | * |
||
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 |
||
100 | */ |
||
101 | function yourls_api_output( $mode, $output, $send_headers = true, $echo = true ) { |
||
102 | 9 | if( isset( $output['simple'] ) ) { |
|
103 | 1 | $simple = $output['simple']; |
|
104 | 1 | unset( $output['simple'] ); |
|
105 | } |
||
106 | |||
107 | 9 | yourls_do_action( 'pre_api_output', $mode, $output, $send_headers, $echo ); |
|
108 | |||
109 | 9 | if( $send_headers ) { |
|
110 | 8 | if( isset( $output['statusCode'] ) ) { |
|
111 | 1 | $code = $output['statusCode']; |
|
112 | 7 | } elseif ( isset( $output['errorCode'] ) ) { |
|
113 | 1 | $code = $output['errorCode']; |
|
114 | } else { |
||
115 | 6 | $code = 200; |
|
116 | } |
||
117 | 8 | yourls_status_header( $code ); |
|
118 | } |
||
119 | |||
120 | 9 | $result = ''; |
|
121 | |||
122 | 9 | switch ( $mode ) { |
|
123 | 9 | case 'jsonp': |
|
124 | 1 | if( $send_headers ) |
|
125 | 1 | yourls_content_type_header( 'application/javascript' ); |
|
126 | |||
127 | 1 | $callback = isset( $output['callback'] ) ? $output['callback'] : ''; |
|
128 | 1 | $result = $callback . '(' . json_encode( $output ) . ')'; |
|
129 | 1 | break; |
|
130 | |||
131 | 8 | case 'json': |
|
132 | 1 | if( $send_headers ) |
|
133 | 1 | yourls_content_type_header( 'application/json' ); |
|
134 | |||
135 | 1 | $result = json_encode( $output ); |
|
136 | 1 | break; |
|
137 | |||
138 | 7 | case 'xml': |
|
139 | 1 | if( $send_headers ) |
|
140 | 1 | yourls_content_type_header( 'application/xml' ); |
|
141 | |||
142 | 1 | $result = yourls_xml_encode( $output ); |
|
143 | 1 | break; |
|
144 | |||
145 | 6 | case 'simple': |
|
146 | default: |
||
147 | 6 | if( $send_headers ) |
|
148 | 5 | yourls_content_type_header( 'text/plain' ); |
|
149 | |||
150 | 6 | $result = isset( $simple ) ? $simple : ''; |
|
151 | 6 | break; |
|
152 | } |
||
153 | |||
154 | 9 | if( $echo ) { |
|
155 | 1 | echo $result; |
|
156 | } |
||
157 | |||
158 | 9 | yourls_do_action( 'api_output', $mode, $output, $send_headers, $echo ); |
|
159 | |||
160 | 9 | return $result; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * Return array for API stat requests |
||
165 | * |
||
166 | */ |
||
167 | function yourls_api_stats( $filter = 'top', $limit = 10, $start = 0 ) { |
||
168 | 1 | $return = yourls_get_stats( $filter, $limit, $start ); |
|
169 | 1 | $return['simple'] = 'Need either XML or JSON format for stats'; |
|
170 | 1 | $return['message'] = 'success'; |
|
171 | 1 | return yourls_apply_filter( 'api_stats', $return, $filter, $limit, $start ); |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Return array for counts of shorturls and clicks |
||
176 | * |
||
177 | */ |
||
178 | function yourls_api_db_stats() { |
||
179 | $return = array( |
||
180 | 1 | 'db-stats' => yourls_get_db_stats(), |
|
181 | 1 | 'statusCode' => 200, |
|
182 | 1 | 'simple' => 'Need either XML or JSON format for stats', |
|
183 | 1 | 'message' => 'success', |
|
184 | ); |
||
185 | |||
186 | 1 | return yourls_apply_filter( 'api_db_stats', $return ); |
|
187 | } |
||
188 | |||
189 | /** |
||
190 | * Return array for API stat requests |
||
191 | * |
||
192 | */ |
||
193 | function yourls_api_url_stats( $shorturl ) { |
||
194 | 1 | $keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc' |
|
195 | 1 | $keyword = yourls_sanitize_keyword( $keyword ); |
|
196 | |||
197 | 1 | $return = yourls_get_keyword_stats( $keyword ); |
|
198 | 1 | $return['simple'] = 'Need either XML or JSON format for stats'; |
|
199 | 1 | return yourls_apply_filter( 'api_url_stats', $return, $shorturl ); |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Expand short url to long url |
||
204 | * |
||
205 | */ |
||
206 | function yourls_api_expand( $shorturl ) { |
||
207 | 1 | $keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc' |
|
208 | 1 | $keyword = yourls_sanitize_keyword( $keyword ); |
|
209 | |||
210 | 1 | $longurl = yourls_get_keyword_longurl( $keyword ); |
|
211 | |||
212 | 1 | if( $longurl ) { |
|
213 | $return = array( |
||
214 | 'keyword' => $keyword, |
||
215 | 'shorturl' => yourls_link($keyword), |
||
216 | 'longurl' => $longurl, |
||
217 | 'title' => yourls_get_keyword_title( $keyword ), |
||
218 | 'simple' => $longurl, |
||
219 | 'message' => 'success', |
||
220 | 'statusCode' => 200, |
||
221 | ); |
||
222 | } else { |
||
223 | $return = array( |
||
224 | 1 | 'keyword' => $keyword, |
|
225 | 1 | 'simple' => 'not found', |
|
226 | 1 | 'message' => 'Error: short URL not found', |
|
227 | 1 | 'errorCode' => 404, |
|
228 | ); |
||
229 | } |
||
230 | |||
231 | 1 | return yourls_apply_filter( 'api_expand', $return, $shorturl ); |
|
232 | } |
||
233 |