Skip to content

Commit 8bd5781

Browse files
committed
Add default proto option and error_msg field
1 parent b69ea76 commit 8bd5781

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ The following JSON fields can be returned:
8181
| `method` | `delete` & `rename` | String | Authentication method used to call the endpoint |
8282
| `old_name` | `rename` | String | Previous name of the file |
8383
| `error` | any | String | Static error code, only sent if anything fails |
84+
| `error_msg` | any | String | Human-readable error, sent with all error codes |
8485
| `debug` | any | String | Human-readable information, only for some errors |
8586

8687
The `info` endpoint implements several JSON fields, which can be returned or not depending on your access level, whether you specify a filename, and whether it exists. Here is the full specification:

ShareXen.sxcu

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"DestinationType": "ImageUploader",
3+
"RequestMethod": "POST",
34
"RequestURL": "https://your.domain/sharexen.php",
4-
"FileFormName": "image",
5+
"Body": "MultipartFormData",
56
"Arguments": {
67
"endpoint": "upload",
78
"token": "change-me"
89
},
10+
"FileFormName": "image",
911
"URL": "$json:url$",
10-
"DeletionURL": "$json:deletion_url$"
11-
}
12+
"DeletionURL": "$json:deletion_url$",
13+
"ErrorMessage": "$json:error_msg$"
14+
}

sharexen.php

+48-4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
// set this to false to embed logged image links
8585
define('DISCORD_PREVENT_EMBED', true);
8686

87+
// Default protocol to use in URLs
88+
// Accepted values: auto, http, https
89+
define('DEFAULT_PROTOCOL', 'auto');
90+
8791

8892
/* DANGEROUS CONSTANTS BELOW THIS LINE */
8993

@@ -224,6 +228,16 @@ function check_constants()
224228
'Invalid DISCORD_PREVENT_EMBED constant, must be a boolean.');
225229
}
226230

231+
if (!defined('DEFAULT_PROTOCOL'))
232+
{
233+
define('DEFAULT_PROTOCOL', 'auto');
234+
}
235+
if (!in_array(DEFAULT_PROTOCOL, ['auto', 'http', 'https']))
236+
{
237+
error_die($data, 500, 'invalid_server_configuration',
238+
'Invalid DEFAULT_PROTOCOL constant, must be one of: auto, http, https.');
239+
}
240+
227241
if (!defined('KEYSPACE'))
228242
{
229243
define('KEYSPACE', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
@@ -384,6 +398,7 @@ function end_request(&$data, $code = 200, $status = 'success')
384398
function error_die(&$data, $code, $reason = 'unknown_error', $debug = '')
385399
{
386400
$data['error'] = $reason;
401+
$data['error_msg'] = ERRORS[$reason];
387402

388403
if ($debug)
389404
{
@@ -462,10 +477,17 @@ function generate_all_urls(&$data, $deletion = true)
462477
{
463478
$protocol = get_parameter('protocol');
464479

465-
if (!$protocol)
480+
if (!in_array($protocol, ['http', 'https']))
466481
{
467-
$https = $_SERVER['HTTPS'];
468-
$protocol = 'http'.($https?'s':'');
482+
if (DEFAULT_PROTOCOL === 'auto')
483+
{
484+
$https = $_SERVER['HTTPS'];
485+
$protocol = 'http'.($https?'s':'');
486+
}
487+
else
488+
{
489+
$protocol = DEFAULT_PROTOCOL;
490+
}
469491
}
470492

471493
$protocol = $protocol.'://';
@@ -761,9 +783,31 @@ function info_endpoint(&$data)
761783
}
762784
}
763785

764-
define('VERSION', '2.3.1');
786+
define('VERSION', '2.4.0');
765787
define('SOURCE', 'https://github.com/Xenthys/ShareXen');
766788

789+
define('ERRORS', [
790+
'unknown_error' => 'An unknown error happened, you shouldn\'t ever see this error message.',
791+
'invalid_server_configuration' => 'Server configuration is invalid, please tell an administrator.',
792+
'invalid_credentials' => 'Your token is invalid, please check it out or ask an administrator.',
793+
'unauthenticated_request' => 'You must be authentified in order to use this API endpoint.',
794+
'file_already_exists' => 'The specified file name already exists on the server.',
795+
'forbidden_filename' => 'The specified file name is forbidden: invalid characters or extension.',
796+
'missing_filename' => 'A file name must be specified in order to use this API endpoint.',
797+
'file_not_found' => 'The given file name was not found on the server.',
798+
'invalid_key' => 'The given file deletion key is invalid.',
799+
'missing_permissions' => 'You do not have the permission to perform this operation.',
800+
'missing_file' => 'You must send a file in order to use the upload API endpoint.',
801+
'invalid_file_extension' => 'The specified file extension is invalid.',
802+
'invalid_file_mime_type' => 'The specified file MIME type is invalid.',
803+
'cannot_generate_unique_filename' => 'The API was unable to generate a unique file name.',
804+
'upload_failed' => 'The API was unable to save your file, please tell and administrator.',
805+
'delete_failed' => 'The API was unable to delete your file, please tell and administrator.',
806+
'missing_new_name' => 'You must specify the new file name (new_name) to rename a file.',
807+
'rename_failed' => 'The API was unable to rename your file, please tell and administrator.',
808+
'unknown_endpoint' => 'Unknown API endpoint, must be one of: upload, delete, rename, info.'
809+
]);
810+
767811
$data = [
768812
'api_version' => VERSION,
769813
'api_source' => SOURCE

0 commit comments

Comments
 (0)