Skip to content

Instantly share code, notes, and snippets.

@flashwave
Created October 31, 2019 15:05
Show Gist options
  • Save flashwave/e0a0d55d09df41da06968b9b444f8408 to your computer and use it in GitHub Desktop.
Save flashwave/e0a0d55d09df41da06968b9b444f8408 to your computer and use it in GitHub Desktop.
old
<?php
define('SHAREX_KEY', '>:(');
define('SHAREX_URL', 'https://mikoto.misaka.nl/i/%s');
define('SHAREX_DIR', __DIR__ . '/i/');
function die_better($text, $code = 418) {
http_response_code($code);
die($text);
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST')
die_better('Invalid request method.', 405);
if (empty($_REQUEST['SHAREX_KEY']) || !hash_equals(SHAREX_KEY, $_REQUEST['SHAREX_KEY']))
die_better('Invalid key.', 401);
if (!isset($_FILES['SHAREX_FILE']))
die_better('No file supplied.', 400);
switch ($_FILES['SHAREX_FILE']['error']) {
case UPLOAD_ERR_INI_SIZE:
die_better('Filesize was too large (INI).', 400);
case UPLOAD_ERR_FORM_SIZE:
die_better('Filesize was too large (FORM).', 400);
case UPLOAD_ERR_PARTIAL:
die_better('Only partially uploaded.', 400);
case UPLOAD_ERR_NO_FILE:
die_better('No file supplied?', 400);
case UPLOAD_ERR_NO_TMP_DIR:
die_better('No temp folder.', 500);
case UPLOAD_ERR_CANT_WRITE:
die_better('Unable to save temporary file.', 500);
case UPLOAD_ERR_EXTENSION:
die_better('(((failed)))', 418);
}
$pathinfo = pathinfo($_FILES['SHAREX_FILE']['name']);
$file_ext = strtolower($pathinfo['extension']) === 'php' ? 'txt' : strtolower($pathinfo['extension']);
$filename = $pathinfo['filename'] . '.' . $file_ext;
if (file_exists(SHAREX_DIR . $filename))
$filename = $pathinfo['filename'] . '-' . str_replace('.', '', microtime(true)) . '.' . $file_ext;
if (!move_uploaded_file($_FILES['SHAREX_FILE']['tmp_name'], SHAREX_DIR . $filename))
die_better('Upload failed.', 500);
printf(SHAREX_URL, rawurlencode($filename));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment