Ensure all statusCode/errorCode API values are strings (#3756)

PHPDoc comments about the API response format say that both `statusCode`
and `errorCode` are expected to be strings, but a lot of cases were
returning ints instead. This could cause problems for JSON clients.
This commit is contained in:
dgw 2024-05-08 06:42:39 -05:00 committed by GitHub
parent aed6bb9a5a
commit 469763cd25
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 14 additions and 14 deletions

View File

@ -157,7 +157,7 @@ if ( isset( $_GET['u'] ) or isset( $_GET['up'] ) ) {
// Deal with the case when redirection failed: // Deal with the case when redirection failed:
$return['status'] = 'error'; $return['status'] = 'error';
$return['errorCode'] = 400; $return['errorCode'] = '400';
$return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Twitter' ); $return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Twitter' );
break; break;
@ -168,7 +168,7 @@ if ( isset( $_GET['u'] ) or isset( $_GET['up'] ) ) {
// Deal with the case when redirection failed: // Deal with the case when redirection failed:
$return['status'] = 'error'; $return['status'] = 'error';
$return['errorCode'] = 400; $return['errorCode'] = '400';
$return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Facebook' ); $return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Facebook' );
break; break;
@ -179,7 +179,7 @@ if ( isset( $_GET['u'] ) or isset( $_GET['up'] ) ) {
// Deal with the case when redirection failed: // Deal with the case when redirection failed:
$return['status'] = 'error'; $return['status'] = 'error';
$return['errorCode'] = 400; $return['errorCode'] = '400';
$return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Tumblr' ); $return['message'] = yourls_s( 'Short URL created, but could not redirect to %s !', 'Tumblr' );
break; break;
@ -189,7 +189,7 @@ if ( isset( $_GET['u'] ) or isset( $_GET['up'] ) ) {
// Still here? That was an unknown 'share' method, then. // Still here? That was an unknown 'share' method, then.
$return['status'] = 'error'; $return['status'] = 'error';
$return['errorCode'] = 400; $return['errorCode'] = '400';
$return['message'] = yourls__( 'Unknown "Share" bookmarklet' ); $return['message'] = yourls__( 'Unknown "Share" bookmarklet' );
break; break;
} }

View File

@ -13,7 +13,7 @@ if( $auth !== true ) {
yourls_api_output( $format, array( yourls_api_output( $format, array(
'simple' => $auth, 'simple' => $auth,
'message' => $auth, 'message' => $auth,
'errorCode' => 403, 'errorCode' => '403',
'callback' => $callback, 'callback' => $callback,
) ); ) );

View File

@ -183,7 +183,7 @@ function yourls_api_stats($filter = 'top', $limit = 10, $start = 0 ) {
function yourls_api_db_stats() { function yourls_api_db_stats() {
$return = array( $return = array(
'db-stats' => yourls_get_db_stats(), 'db-stats' => yourls_get_db_stats(),
'statusCode' => 200, 'statusCode' => '200',
'simple' => 'Need either XML or JSON format for stats', 'simple' => 'Need either XML or JSON format for stats',
'message' => 'success', 'message' => 'success',
); );
@ -226,14 +226,14 @@ function yourls_api_expand( $shorturl ) {
'title' => yourls_get_keyword_title( $keyword ), 'title' => yourls_get_keyword_title( $keyword ),
'simple' => $longurl, 'simple' => $longurl,
'message' => 'success', 'message' => 'success',
'statusCode' => 200, 'statusCode' => '200',
); );
} else { } else {
$return = array( $return = array(
'keyword' => $keyword, 'keyword' => $keyword,
'simple' => 'not found', 'simple' => 'not found',
'message' => 'Error: short URL not found', 'message' => 'Error: short URL not found',
'errorCode' => 404, 'errorCode' => '404',
); );
} }

View File

@ -143,7 +143,7 @@ function yourls_add_new_link( $url, $keyword = '', $title = '', $row_id = 1 ) {
$return['title'] = $title; $return['title'] = $title;
$return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time(), $row_id ); $return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time(), $row_id );
$return['shorturl'] = yourls_link($keyword); $return['shorturl'] = yourls_link($keyword);
$return['statusCode'] = 200; // 200 OK $return['statusCode'] = '200'; // 200 OK
} else { } else {
// unknown database error, couldn't store result // unknown database error, couldn't store result
$return['status'] = 'fail'; $return['status'] = 'fail';
@ -595,12 +595,12 @@ function yourls_get_keyword_stats( $shorturl ) {
if( !$res ) { if( !$res ) {
// non existent link // non existent link
$return = array( $return = array(
'statusCode' => 404, 'statusCode' => '404',
'message' => 'Error: short URL not found', 'message' => 'Error: short URL not found',
); );
} else { } else {
$return = array( $return = array(
'statusCode' => 200, 'statusCode' => '200',
'message' => 'success', 'message' => 'success',
'link' => array( 'link' => array(
'shorturl' => yourls_link($res->keyword), 'shorturl' => yourls_link($res->keyword),

View File

@ -172,7 +172,7 @@ function yourls_get_stats($filter = 'top', $limit = 10, $start = 0) {
$return['stats'] = yourls_get_db_stats(); $return['stats'] = yourls_get_db_stats();
$return['statusCode'] = 200; $return['statusCode'] = '200';
return yourls_apply_filter( 'get_stats', $return, $filter, $limit, $start ); return yourls_apply_filter( 'get_stats', $return, $filter, $limit, $start );
} }

View File

@ -504,7 +504,7 @@ echo $data->longurl;</tt></pre>
"message": "http:\/\/ozh.org added to database", "message": "http:\/\/ozh.org added to database",
"title": "Ozh RICHARD \u00ab ozh.org", "title": "Ozh RICHARD \u00ab ozh.org",
"shorturl": "http:\/\/sho.rt\/1f", "shorturl": "http:\/\/sho.rt\/1f",
"statusCode": 200 "statusCode": "200"
}</tt></pre> }</tt></pre>
<p>Sample return in XML format for the <code>expand</code> action</p> <p>Sample return in XML format for the <code>expand</code> action</p>

View File

@ -35,7 +35,7 @@ foreach( (array) $api_actions as $_action => $_callback ) {
$return = yourls_apply_filter( 'api_action_' . $action, false ); $return = yourls_apply_filter( 'api_action_' . $action, false );
if ( false === $return ) { if ( false === $return ) {
$return = array( $return = array(
'errorCode' => 400, 'errorCode' => '400',
'message' => 'Unknown or missing "action" parameter', 'message' => 'Unknown or missing "action" parameter',
'simple' => 'Unknown or missing "action" parameter', 'simple' => 'Unknown or missing "action" parameter',
); );