|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\Upload; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\App\Config\ConfigManager; |
|
6
|
|
|
use Jaxon\App\I18n\Translator; |
|
7
|
|
|
use Jaxon\Request\Upload\UploadHandlerInterface; |
|
8
|
|
|
use Jaxon\Storage\StorageManager; |
|
9
|
|
|
use Jaxon\Upload\Manager\FileNameInterface; |
|
10
|
|
|
use Jaxon\Upload\Manager\UploadManager; |
|
11
|
|
|
use Jaxon\Upload\Manager\Validator; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
|
|
14
|
|
|
use function Jaxon\jaxon; |
|
15
|
|
|
use function bin2hex; |
|
16
|
|
|
use function dirname; |
|
17
|
|
|
use function php_sapi_name; |
|
18
|
|
|
use function random_bytes; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
function registerUpload(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$jaxon = jaxon(); |
|
26
|
|
|
if(!$jaxon->getAppOption('upload.enabled')) |
|
27
|
|
|
{ |
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$di = $jaxon->di(); |
|
32
|
|
|
if($di->h(UploadHandler::class)) |
|
33
|
|
|
{ |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// Upload file and dir name generator |
|
38
|
|
|
$di->set(FileNameInterface::class, function() { |
|
39
|
|
|
return new class implements FileNameInterface |
|
40
|
|
|
{ |
|
41
|
|
|
public function random(int $nLength): string |
|
42
|
|
|
{ |
|
43
|
|
|
return bin2hex(random_bytes((int)($nLength / 2))); |
|
44
|
|
|
} |
|
45
|
|
|
}; |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
// Upload validator |
|
49
|
|
|
$di->set(Validator::class, fn($c) => |
|
50
|
|
|
new Validator($c->g(ConfigManager::class), $c->g(Translator::class))); |
|
51
|
|
|
|
|
52
|
|
|
// File upload manager |
|
53
|
|
|
$di->set(UploadManager::class, function($c) { |
|
54
|
|
|
// Translation directory |
|
55
|
|
|
$sTranslationDir = dirname(__DIR__) . '/translations'; |
|
56
|
|
|
// Load the upload translations |
|
57
|
|
|
$xTranslator = $c->g(Translator::class); |
|
58
|
|
|
$xTranslator->loadTranslations("$sTranslationDir/en/upload.php", 'en'); |
|
59
|
|
|
$xTranslator->loadTranslations("$sTranslationDir/fr/upload.php", 'fr'); |
|
60
|
|
|
$xTranslator->loadTranslations("$sTranslationDir/es/upload.php", 'es'); |
|
61
|
|
|
|
|
62
|
|
|
return new UploadManager($c->g(Validator::class), $xTranslator, |
|
63
|
|
|
$c->g(LoggerInterface::class), $c->g(FileNameInterface::class), |
|
64
|
|
|
$c->g(StorageManager::class), $c->g(ConfigManager::class)); |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
// File upload plugin |
|
68
|
|
|
$di->set(UploadHandler::class, fn($c) => |
|
69
|
|
|
new UploadHandler($c->g(StorageManager::class), $c->g(UploadManager::class))); |
|
70
|
|
|
// Set alias on the interface |
|
71
|
|
|
$di->alias(UploadHandlerInterface::class, UploadHandler::class); |
|
72
|
|
|
|
|
73
|
|
|
// Set a callback to process uploaded files in the incoming requests. |
|
74
|
|
|
$jaxon->callback()->before(function() use($di) { |
|
75
|
|
|
/** @var UploadHandler */ |
|
76
|
|
|
$xUploadHandler = $di->g(UploadHandler::class); |
|
77
|
|
|
// The HTTP request |
|
78
|
|
|
$xRequest = $di->getRequest(); |
|
79
|
|
|
if($xUploadHandler->canProcessRequest($xRequest)) |
|
80
|
|
|
{ |
|
81
|
|
|
$xUploadHandler->processRequest($xRequest); |
|
82
|
|
|
} |
|
83
|
|
|
}); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Register the values into the container |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
function _register(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$jaxon = jaxon(); |
|
94
|
|
|
$jaxon->callback()->boot(fn() => registerUpload()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
function register() |
|
98
|
|
|
{ |
|
99
|
|
|
// Do nothing if running in cli. |
|
100
|
|
|
if(php_sapi_name() !== 'cli') |
|
101
|
|
|
{ |
|
102
|
|
|
_register(); |
|
103
|
|
|
}; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
register(); |
|
107
|
|
|
|