| [ Index ] |
PHP Cross Reference of YOURLS |
[Summary view] [Print] [Text view]
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 * 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 if( isset( $output['simple'] ) ) { 103 $simple = $output['simple']; 104 unset( $output['simple'] ); 105 } 106 107 yourls_do_action( 'pre_api_output', $mode, $output, $send_headers, $echo ); 108 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 } 119 120 $result = ''; 121 122 switch ( $mode ) { 123 case 'jsonp': 124 if( $send_headers ) 125 yourls_content_type_header( 'application/javascript' ); 126 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 135 break; 136 137 case 'json': 138 if( $send_headers ) 139 yourls_content_type_header( 'application/json' ); 140 141 $result = json_encode( $output ); 142 break; 143 144 case 'xml': 145 if( $send_headers ) 146 yourls_content_type_header( 'application/xml' ); 147 148 $result = yourls_xml_encode( $output ); 149 break; 150 151 case 'simple': 152 default: 153 if( $send_headers ) 154 yourls_content_type_header( 'text/plain' ); 155 156 $result = isset( $simple ) ? $simple : ''; 157 break; 158 } 159 160 if( $echo ) { 161 echo $result; 162 } 163 164 yourls_do_action( 'api_output', $mode, $output, $send_headers, $echo ); 165 166 return $result; 167 } 168 169 /** 170 * Return array for API stat requests 171 * 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 176 */ 177 function yourls_api_stats($filter = 'top', $limit = 10, $start = 0 ) { 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 ); 182 } 183 184 /** 185 * Return array for counts of shorturls and clicks 186 * 187 * @return array 188 */ 189 function yourls_api_db_stats() { 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 ); 198 } 199 200 /** 201 * Return array for API stat requests 202 * 203 * @param string $shorturl Short URL to check 204 * @return array 205 */ 206 function yourls_api_url_stats( $shorturl ) { 207 $keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc' 208 $keyword = yourls_sanitize_keyword( $keyword ); 209 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 ); 213 } 214 215 /** 216 * Expand short url to long url 217 * 218 * @param string $shorturl Short URL to expand 219 * @return array 220 */ 221 function yourls_api_expand( $shorturl ) { 222 $keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc' 223 $keyword = yourls_sanitize_keyword( $keyword ); 224 225 $longurl = yourls_get_keyword_longurl( $keyword ); 226 227 if( $longurl ) { 228 $return = array( 229 'keyword' => $keyword, 230 'shorturl' => yourls_link($keyword), 231 'longurl' => $longurl, 232 'title' => yourls_get_keyword_title( $keyword ), 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 ); 247 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Jan 6 05:10:29 2026 | Cross-referenced by PHPXref 0.7.1 |