[ 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'] ) ? $output['callback'] : ''; 128 $result = $callback . '(' . json_encode( $output ) . ')'; 129 break; 130 131 case 'json': 132 if( $send_headers ) 133 yourls_content_type_header( 'application/json' ); 134 135 $result = json_encode( $output ); 136 break; 137 138 case 'xml': 139 if( $send_headers ) 140 yourls_content_type_header( 'application/xml' ); 141 142 $result = yourls_xml_encode( $output ); 143 break; 144 145 case 'simple': 146 default: 147 if( $send_headers ) 148 yourls_content_type_header( 'text/plain' ); 149 150 $result = isset( $simple ) ? $simple : ''; 151 break; 152 } 153 154 if( $echo ) { 155 echo $result; 156 } 157 158 yourls_do_action( 'api_output', $mode, $output, $send_headers, $echo ); 159 160 return $result; 161 } 162 163 /** 164 * Return array for API stat requests 165 * 166 * @param string $filter either "top", "bottom" , "rand" or "last" 167 * @param int $limit maximum number of links to return 168 * @param int $start offset 169 * @return array 170 */ 171 function yourls_api_stats($filter = 'top', $limit = 10, $start = 0 ) { 172 $return = yourls_get_stats( $filter, $limit, $start ); 173 $return['simple'] = 'Need either XML or JSON format for stats'; 174 $return['message'] = 'success'; 175 return yourls_apply_filter( 'api_stats', $return, $filter, $limit, $start ); 176 } 177 178 /** 179 * Return array for counts of shorturls and clicks 180 * 181 * @return array 182 */ 183 function yourls_api_db_stats() { 184 $return = array( 185 'db-stats' => yourls_get_db_stats(), 186 'statusCode' => '200', 187 'simple' => 'Need either XML or JSON format for stats', 188 'message' => 'success', 189 ); 190 191 return yourls_apply_filter( 'api_db_stats', $return ); 192 } 193 194 /** 195 * Return array for API stat requests 196 * 197 * @param string $shorturl Short URL to check 198 * @return array 199 */ 200 function yourls_api_url_stats( $shorturl ) { 201 $keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc' 202 $keyword = yourls_sanitize_keyword( $keyword ); 203 204 $return = yourls_get_keyword_stats( $keyword ); 205 $return['simple'] = 'Need either XML or JSON format for stats'; 206 return yourls_apply_filter( 'api_url_stats', $return, $shorturl ); 207 } 208 209 /** 210 * Expand short url to long url 211 * 212 * @param string $shorturl Short URL to expand 213 * @return array 214 */ 215 function yourls_api_expand( $shorturl ) { 216 $keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc' 217 $keyword = yourls_sanitize_keyword( $keyword ); 218 219 $longurl = yourls_get_keyword_longurl( $keyword ); 220 221 if( $longurl ) { 222 $return = array( 223 'keyword' => $keyword, 224 'shorturl' => yourls_link($keyword), 225 'longurl' => $longurl, 226 'title' => yourls_get_keyword_title( $keyword ), 227 'simple' => $longurl, 228 'message' => 'success', 229 'statusCode' => '200', 230 ); 231 } else { 232 $return = array( 233 'keyword' => $keyword, 234 'simple' => 'not found', 235 'message' => 'Error: short URL not found', 236 'errorCode' => '404', 237 ); 238 } 239 240 return yourls_apply_filter( 'api_expand', $return, $shorturl ); 241 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Feb 22 05:10:06 2025 | Cross-referenced by PHPXref 0.7.1 |