| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* |
| 5 |
* @link https://github.com/rosell-dk/webp-convert-cloud-service |
| 6 |
* @license MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WebPConvertCloudService; |
| 10 |
|
| 11 |
use WebPConvertCloudService\Serve; |
| 12 |
use WebPConvertCloudService\AccessCheck; |
| 13 |
|
| 14 |
class WebPConvertCloudService |
| 15 |
{ |
| 16 |
public $options; |
| 17 |
|
| 18 |
const ERROR_CONFIGURATION = 0; |
| 19 |
const ERROR_ACCESS_DENIED = 1; |
| 20 |
const ERROR_RUNTIME = 2; |
| 21 |
|
| 22 |
/* |
| 23 |
example yaml: |
| 24 |
|
| 25 |
destination-dir: '../conversions' |
| 26 |
access: |
| 27 |
allowed-hosts: |
| 28 |
- bitwise-it.dk |
| 29 |
allowed-ips: |
| 30 |
- 127.0.0.1 |
| 31 |
secret: 'my dog is white' |
| 32 |
|
| 33 |
whitelist: |
| 34 |
- |
| 35 |
label: 'rosell.dk' |
| 36 |
ip: 212.14.2.1 |
| 37 |
secret: 'aoeuth8aoeuh' |
| 38 |
- |
| 39 |
label: 'public' |
| 40 |
secret: '9tita8hoetua' |
| 41 |
|
| 42 |
webp-convert: |
| 43 |
quality: 80 |
| 44 |
... |
| 45 |
*/ |
| 46 |
/* |
| 47 |
public function loadConfig() |
| 48 |
{ |
| 49 |
$configDir = __DIR__; |
| 50 |
|
| 51 |
$parentFolders = explode('/', $configDir); |
| 52 |
$poppedFolders = []; |
| 53 |
|
| 54 |
while (!(file_exists(implode('/', $parentFolders) . '/wpc-config.yaml')) && count($parentFolders) > 0) { |
| 55 |
array_unshift($poppedFolders, array_pop($parentFolders)); |
| 56 |
} |
| 57 |
if (count($parentFolders) == 0) { |
| 58 |
self::exitWithError( |
| 59 |
WebPConvertCloudService::ERROR_SERVER_SETUP, |
| 60 |
'wpc-config.yaml not found in any parent folders.' |
| 61 |
); |
| 62 |
} |
| 63 |
$configFilePath = implode('/', $parentFolders) . '/wpc-config.yaml'; |
| 64 |
|
| 65 |
try { |
| 66 |
$options = \Spyc::YAMLLoad($configFilePath); |
| 67 |
} catch (\Exception $e) { |
| 68 |
self::exitWithError(WebPConvertCloudService::ERROR_SERVER_SETUP, 'Error parsing wpc-config.yaml.'); |
| 69 |
} |
| 70 |
}*/ |
| 71 |
|
| 72 |
public static function exitWithError($errorCode, $msg) |
| 73 |
{ |
| 74 |
$returnObject = [ |
| 75 |
'success' => 0, |
| 76 |
'errorCode' => $errorCode, |
| 77 |
'errorMessage' => $msg, |
| 78 |
]; |
| 79 |
echo json_encode($returnObject); |
| 80 |
exit; |
| 81 |
} |
| 82 |
|
| 83 |
public static function handleRequest($options) |
| 84 |
{ |
| 85 |
//$this->options = static::loadConfig(); |
| 86 |
if (!isset($options)) { |
| 87 |
self::exitWithError(self::ERROR_SERVER_SETUP, 'No options was supplied'); |
| 88 |
} |
| 89 |
|
| 90 |
$action = (isset($_POST['action']) ? $_POST['action'] : 'convert'); |
| 91 |
|
| 92 |
// Handle actions that does not require access check |
| 93 |
|
| 94 |
switch ($action) { |
| 95 |
case 'api-version': |
| 96 |
echo '2'; |
| 97 |
exit; |
| 98 |
} |
| 99 |
|
| 100 |
AccessCheck::runAccessChecks($options); |
| 101 |
|
| 102 |
// Handle actions that requires access check |
| 103 |
|
| 104 |
switch ($action) { |
| 105 |
case 'check-access': |
| 106 |
echo "You have access!\n"; |
| 107 |
break; |
| 108 |
case 'convert': |
| 109 |
Serve::serve($options); |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|