| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebPConvertCloudService; |
| 4 |
|
| 5 |
use \WebPConvertCloudService\WebPConvertCloudService; |
| 6 |
use \WebPConvert\WebPConvert; |
| 7 |
|
| 8 |
class Serve |
| 9 |
{ |
| 10 |
|
| 11 |
private static function configurationError($msg) |
| 12 |
{ |
| 13 |
WebPConvertCloudService::exitWithError(WebPConvertCloudService::ERROR_CONFIGURATION, $msg); |
| 14 |
} |
| 15 |
|
| 16 |
private static function accessDenied($msg) |
| 17 |
{ |
| 18 |
WebPConvertCloudService::exitWithError(WebPConvertCloudService::ERROR_ACCESS_DENIED, $msg); |
| 19 |
} |
| 20 |
|
| 21 |
private static function runtimeError($msg) |
| 22 |
{ |
| 23 |
WebPConvertCloudService::exitWithError(WebPConvertCloudService::ERROR_RUNTIME, $msg); |
| 24 |
} |
| 25 |
|
| 26 |
public static function serve($options) |
| 27 |
{ |
| 28 |
$uploaddir = $options['destination-dir'] ; |
| 29 |
|
| 30 |
if (!is_dir($uploaddir)) { |
| 31 |
if (!@mkdir($uploaddir, 0775, true)) { |
| 32 |
self::configurationError('Could not create folder for converted files: ' . $uploaddir); |
| 33 |
} |
| 34 |
@chmod($uploaddir, 0775); |
| 35 |
} |
| 36 |
|
| 37 |
/* |
| 38 |
if (!isset($_POST['hash'])) { |
| 39 |
self::accessDenied('Restricted access. Hash required, but missing'); |
| 40 |
}*/ |
| 41 |
|
| 42 |
if (!isset($_FILES['file'])) { |
| 43 |
self::runtimeError('No file was supplied'); |
| 44 |
} |
| 45 |
if (!isset($_FILES['file']['error'])) { |
| 46 |
self::runtimeError('Invalid parameters'); |
| 47 |
} |
| 48 |
|
| 49 |
if (is_array($_FILES['file']['error'])) { |
| 50 |
self::runtimeError('Cannot convert multiple files'); |
| 51 |
} |
| 52 |
|
| 53 |
switch ($_FILES['file']['error']) { |
| 54 |
case UPLOAD_ERR_OK: |
| 55 |
break; |
| 56 |
case UPLOAD_ERR_NO_FILE: |
| 57 |
self::runtimeError('No file sent'); |
| 58 |
break; |
| 59 |
case UPLOAD_ERR_INI_SIZE: |
| 60 |
case UPLOAD_ERR_FORM_SIZE: |
| 61 |
self::runtimeError('Exceeded filesize limit.'); |
| 62 |
break; |
| 63 |
default: |
| 64 |
self::runtimeError('Unknown error.'); |
| 65 |
} |
| 66 |
|
| 67 |
if ($_FILES['file']['size'] == 0) { |
| 68 |
self::accessDenied('File size is zero. Perhaps exceeded filesize limit?'); |
| 69 |
} |
| 70 |
// Undefined | Multiple Files | $_FILES Corruption Attack |
| 71 |
// If this request falls under any of them, treat it invalid. |
| 72 |
/*if ($_FILES['file']['size'] > 1000000) { |
| 73 |
throw new RuntimeException('Exceeded filesize limit.'); |
| 74 |
}*/ |
| 75 |
|
| 76 |
// DO NOT TRUST $_FILES['upfile']['mime'] VALUE !! |
| 77 |
// Check MIME Type by yourself. |
| 78 |
if (function_exists('finfo_file') && (defined('FILEINFO_MIME_TYPE'))) { |
| 79 |
$r = finfo_open(FILEINFO_MIME_TYPE); |
| 80 |
if (false === $ext = array_search( |
| 81 |
finfo_file($r, $_FILES['file']['tmp_name']), |
| 82 |
array( |
| 83 |
'jpg' => 'image/jpeg', |
| 84 |
'png' => 'image/png', |
| 85 |
'gif' => 'image/gif', |
| 86 |
), |
| 87 |
true |
| 88 |
)) { |
| 89 |
self::accessDenied('Invalid file format.'); |
| 90 |
} |
| 91 |
} else { |
| 92 |
$ext = 'jpg'; // We set it to something, in case above fails. |
| 93 |
} |
| 94 |
|
| 95 |
$uploadfile = $uploaddir . '/' . sha1_file($_FILES['file']['tmp_name']) . '.' . $ext; |
| 96 |
//echo $uploadfile; |
| 97 |
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { |
| 98 |
// File is valid, and was successfully uploaded |
| 99 |
|
| 100 |
$source = $uploadfile; |
| 101 |
|
| 102 |
/* |
| 103 |
if (!empty($password)) { |
| 104 |
$hash = md5(md5_file($source) . $password); |
| 105 |
|
| 106 |
if ($hash != $_POST['hash']) { |
| 107 |
self::accessDenied('Wrong password.'); |
| 108 |
} |
| 109 |
} |
| 110 |
*/ |
| 111 |
$destination = $uploadfile . '.webp'; |
| 112 |
|
| 113 |
if (isset($_POST['options'])) { |
| 114 |
// Merge in options in $_POST, overwriting the webp-convert options in config |
| 115 |
$convertOptionsInPost = (array) json_decode($_POST['options'], true); |
| 116 |
$convertOptions = array_merge($options['webp-convert'], $convertOptionsInPost); |
| 117 |
} else { |
| 118 |
$convertOptions = $options['webp-convert']; |
| 119 |
} |
| 120 |
|
| 121 |
try { |
| 122 |
WebPConvert::convert($source, $destination, $convertOptions); |
| 123 |
header('Content-type: application/octet-stream'); |
| 124 |
echo file_get_contents($destination); |
| 125 |
|
| 126 |
unlink($source); |
| 127 |
unlink($destination); |
| 128 |
} catch (\Exception $e) { |
| 129 |
echo 'Conversion failed!'; |
| 130 |
echo $e->getMessage(); |
| 131 |
} |
| 132 |
} else { |
| 133 |
// Possible file upload attack! |
| 134 |
self::configurationError('Failed to move uploaded file'); |
| 135 |
|
| 136 |
//echo 'Failed to move uploaded file'; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|