24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
/**
* This is a virtual entity, it cannot be saved to a SQL table
*/
class File extends Entity
{
const TABLE = 'files';
protected $id;
/**
* Parent directory of file
*/
protected $parent;
/**
* File name
*/
protected $name;
/**
* Complete file path (parent + '/' + name)
*/
protected $path;
/**
* Type of file: file or directory
*/
protected $type = self::TYPE_FILE;
protected $mime;
protected $size;
protected $modified;
protected $image;
protected $_types = [
'id' => '?int',
'path' => 'string',
'parent' => '?string',
'name' => 'string',
'type' => 'int',
'mime' => '?string',
'size' => '?int',
'modified' => 'DateTime',
'image' => 'int',
];
const TYPE_FILE = 1;
const TYPE_DIRECTORY = 2;
const TYPE_LINK = 3;
/**
* Tailles de miniatures autorisées, pour ne pas avoir 500 fichiers générés avec 500 tailles différentes
|
|
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/**
* This is a virtual entity, it cannot be saved to a SQL table
*/
class File extends Entity
{
const TABLE = 'files';
protected ?int $id;
/**
* Parent directory of file
*/
protected ?string $parent;
/**
* File name
*/
protected string $name;
/**
* Complete file path (parent + '/' + name)
*/
protected string $path;
/**
* Type of file: file or directory
*/
protected int $type = self::TYPE_FILE;
protected ?string $mime;
protected ?int $size;
protected \DateTime $modified;
protected bool $image;
const TYPE_FILE = 1;
const TYPE_DIRECTORY = 2;
const TYPE_LINK = 3;
/**
* Tailles de miniatures autorisées, pour ne pas avoir 500 fichiers générés avec 500 tailles différentes
|
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
if (0 === strpos($new_path . '/', $this->path . '/')) {
if ($this->type != self::TYPE_DIRECTORY) {
throw new UserException(sprintf('Impossible de renommer "%s" vers "%s"', $this->path, $new_path));
}
}
self::ensureDirectoryExists(Utils::dirname($new_path));
$return = Files::callStorage('move', $this, $new_path);
Plugin::fireSignal('files.move', ['file' => $this, 'new_path' => $new_path]);
return $return;
}
|
|
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
if (0 === strpos($new_path . '/', $this->path . '/')) {
if ($this->type != self::TYPE_DIRECTORY) {
throw new UserException(sprintf('Impossible de renommer "%s" vers "%s"', $this->path, $new_path));
}
}
Files::ensureDirectoryExists(Utils::dirname($new_path));
$return = Files::callStorage('move', $this, $new_path);
Plugin::fireSignal('files.move', ['file' => $this, 'new_path' => $new_path]);
return $return;
}
|
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
public function removeFromSearch(): void
{
$db = DB::getInstance();
$db->preparedQuery('DELETE FROM files_search WHERE path = ?;', $this->path);
}
/**
* Create and store a file
* If one parameter is supplied, the other must be NULL (you cannot omit one)
* @param string $path Target path
* @param string $name Target name
* @param string $source_path Source file path
* @param string $source_content OR source file content (binary string)
* @return self
*/
static public function createAndStore(string $path, string $name, ?string $source_path, ?string $source_content): self
{
$file = self::create($path, $name, $source_path, $source_content);
$file->store($source_path, $source_content);
return $file;
}
/**
* Create a new directory
* @param string $path Target parent path
* @param string $name Target name
* @param bool $create_parent Create parent directories if they don't exist
* @return self
*/
static public function createDirectory(string $path, string $name, bool $create_parent = true): self
{
$name = self::filterName($name);
$fullpath = trim($path . '/' . $name, '/');
self::validatePath($fullpath);
Files::checkQuota();
if (Files::callStorage('exists', $fullpath)) {
throw new ValidationException('Le nom de répertoire choisi existe déjà: ' . $fullpath);
}
if ($path !== '' && $create_parent) {
self::ensureDirectoryExists($path);
}
$file = new self;
$file->set('path', $fullpath);
$file->set('name', $name);
$file->set('parent', $path);
$file->set('type', self::TYPE_DIRECTORY);
$file->set('image', 0);
$file->set('modified', new \DateTime);
Files::callStorage('mkdir', $file);
Plugin::fireSignal('files.mkdir', ['file' => $file]);
return $file;
}
static public function ensureDirectoryExists(string $path): void
{
$db = DB::getInstance();
$parts = explode('/', $path);
$tree = '';
foreach ($parts as $part) {
$tree = trim($tree . '/' . $part, '/');
$exists = $db->test(File::TABLE, 'type = ? AND path = ?', self::TYPE_DIRECTORY, $tree);
if (!$exists) {
try {
self::createDirectory(Utils::dirname($tree), Utils::basename($tree), false);
}
catch (ValidationException $e) {
// Ignore when directory already exists
}
}
}
}
static public function create(string $path, string $name, ?string $source_path, ?string $source_content): self
{
if (!isset($source_path) && !isset($source_content)) {
throw new \InvalidArgumentException('Either source path or source content should be set but not both');
}
self::validateFileName($name);
self::validatePath($path);
self::ensureDirectoryExists($path);
$finfo = \finfo_open(\FILEINFO_MIME_TYPE);
$fullpath = $path . '/' . $name;
$file = Files::callStorage('get', $fullpath) ?: new self;
$file->set('path', $fullpath);
$file->set('parent', $path);
$file->set('name', $name);
if ($source_path && !$source_content) {
$file->set('mime', finfo_file($finfo, $source_path));
$file->set('size', filesize($source_path));
$file->set('modified', new \DateTime('@' . filemtime($source_path)));
}
else {
$file->set('mime', finfo_buffer($finfo, $source_content));
$file->set('size', strlen($source_content));
}
$file->set('image', (int) in_array($file->mime, self::IMAGE_TYPES));
// Force empty files as text/plain
if ($file->mime == 'application/x-empty' && !$file->size) {
$file->set('mime', 'text/plain');
}
return $file;
}
/**
* Upload multiple files
* @param string $path Target parent directory (eg. 'documents/Logos')
* @param string $key The name of the file input in the HTML form (this MUST have a '[]' at the end of the name)
* @return array list of File objects created
*/
static public function uploadMultiple(string $path, string $key): array
{
if (!isset($_FILES[$key]['name'][0])) {
throw new UserException('Aucun fichier reçu');
}
// Transpose array
// see https://www.php.net/manual/en/features.file-upload.multiple.php#53240
$files = Utils::array_transpose($_FILES[$key]);
$out = [];
// First check all files
foreach ($files as $file) {
if (!empty($file['error'])) {
throw new UserException(self::getErrorMessage($file['error']));
}
if (empty($file['size']) || empty($file['name'])) {
throw new UserException('Fichier reçu invalide : vide ou sans nom de fichier.');
}
if (!is_uploaded_file($file['tmp_name'])) {
throw new \RuntimeException('Le fichier n\'a pas été envoyé de manière conventionnelle.');
}
}
// Then create files
foreach ($files as $file) {
$name = self::filterName($file['name']);
$out[] = self::createAndStore($path, $name, $file['tmp_name'], null);
}
return $out;
}
/**
* Upload a file using POST from a HTML form
* @param string $path Target parent directory (eg. 'documents/Logos')
* @param string $key The name of the file input in the HTML form
* @return self Created file object
*/
static public function upload(string $path, string $key, ?string $name = null): self
{
if (!isset($_FILES[$key]) || !is_array($_FILES[$key])) {
throw new UserException('Aucun fichier reçu');
}
$file = $_FILES[$key];
if (!empty($file['error'])) {
throw new UserException(self::getErrorMessage($file['error']));
}
if (empty($file['size']) || empty($file['name'])) {
throw new UserException('Fichier reçu invalide : vide ou sans nom de fichier.');
}
if (!is_uploaded_file($file['tmp_name'])) {
throw new \RuntimeException('Le fichier n\'a pas été envoyé de manière conventionnelle.');
}
$name = self::filterName($name ?? $file['name']);
return self::createAndStore($path, $name, $file['tmp_name'], null);
}
/**
* Récupération du message d'erreur
* @param integer $error Code erreur du $_FILE
* @return string Message d'erreur
*/
static public function getErrorMessage($error)
{
switch ($error)
{
case UPLOAD_ERR_INI_SIZE:
return 'Le fichier excède la taille permise par la configuration.';
case UPLOAD_ERR_FORM_SIZE:
return 'Le fichier excède la taille permise par le formulaire.';
case UPLOAD_ERR_PARTIAL:
return 'L\'envoi du fichier a été interrompu.';
case UPLOAD_ERR_NO_FILE:
return 'Aucun fichier n\'a été reçu.';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Pas de répertoire temporaire pour stocker le fichier.';
case UPLOAD_ERR_CANT_WRITE:
return 'Impossible d\'écrire le fichier sur le disque du serveur.';
case UPLOAD_ERR_EXTENSION:
return 'Une extension du serveur a interrompu l\'envoi du fichier.';
default:
return 'Erreur inconnue: ' . $error;
}
}
/**
* Full URL with https://...
*/
public function url(bool $download = false): string
{
$base = in_array($this->context(), [self::CONTEXT_WEB, self::CONTEXT_SKELETON, self::CONTEXT_CONFIG]) ? WWW_URL : BASE_URL;
$url = $base . $this->uri();
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
public function removeFromSearch(): void
{
$db = DB::getInstance();
$db->preparedQuery('DELETE FROM files_search WHERE path = ?;', $this->path);
}
/**
* Full URL with https://...
*/
public function url(bool $download = false): string
{
$base = in_array($this->context(), [self::CONTEXT_WEB, self::CONTEXT_SKELETON, self::CONTEXT_CONFIG]) ? WWW_URL : BASE_URL;
$url = $base . $this->uri();
|