175 lines
5 KiB
PHP
175 lines
5 KiB
PHP
<?php
|
|
|
|
namespace Utils;
|
|
|
|
use Utils\SanitizeName;
|
|
|
|
use Utils\Debug;
|
|
|
|
class File {
|
|
|
|
private $path = __DIR__ . '/../../data/';
|
|
public $subPath;
|
|
|
|
/**
|
|
* Define a subpath for $path
|
|
* example for file
|
|
* - imilo
|
|
* - region
|
|
* - local
|
|
*
|
|
* @var string
|
|
*/
|
|
public $type;
|
|
|
|
public $filterType = '*';
|
|
|
|
private $mimePermit = [
|
|
'png' => 'png',
|
|
'jpg' => 'jpg',
|
|
'bmp' => 'bmp',
|
|
'gif' => 'gif',
|
|
'webp' => 'webp'
|
|
];
|
|
|
|
public function __construct() {
|
|
return $this;
|
|
}
|
|
|
|
/*
|
|
* @todo define list of authorized path
|
|
*/
|
|
public function setPath($path) {
|
|
$this->path = $path;
|
|
}
|
|
|
|
public function getFile($filename) {
|
|
ob_end_clean();
|
|
$path = realpath($this->path . $this->type);
|
|
$file = $path . '/' . $filename;
|
|
if (file_exists($file)) {
|
|
header('Content-Description: File Transfer');
|
|
header('Content-Type: ' . mime_content_type($file));
|
|
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
|
|
header('Expires: 0');
|
|
header('Cache-Control: must-revalidate');
|
|
header('Pragma: public');
|
|
header('Content-Length: ' . filesize($file));
|
|
readfile($file);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function getFileList(): array {
|
|
$path = realpath($this->path . $this->type);
|
|
|
|
foreach (glob($path . '/' . $this->filterType) as $filename) {
|
|
$path_parts = pathinfo($filename);
|
|
$fileList[] = [
|
|
'file' => basename($filename),
|
|
'path' => $this->subPath . '/' . $this->type . '/' . basename($filename),
|
|
'type' => mime_content_type($filename),
|
|
'ext' => $path_parts['extension'],
|
|
'icon' => $this->defineIcon($path_parts['extension']),
|
|
'size' => $this->human_filesize(filesize($filename)),
|
|
'addTime' => date("d-m-Y H:i", filectime($filename))
|
|
];
|
|
}
|
|
|
|
if (empty($fileList)) {
|
|
$fileList = [];
|
|
}
|
|
|
|
return $fileList;
|
|
}
|
|
|
|
private function defineIcon(string $mimeFile): string {
|
|
if (array_key_exists($mimeFile, $this->mimePermit)) {
|
|
return $this->mimePermit[$mimeFile];
|
|
} else {
|
|
return 'raw';
|
|
}
|
|
}
|
|
|
|
private function human_filesize(int $bytes, int $decimals = 2): string {
|
|
$factor = floor((strlen($bytes) - 1) / 3);
|
|
if ($factor > 0) $sz = 'KMGT';
|
|
|
|
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . @$sz[$factor - 1] . 'B';
|
|
}
|
|
|
|
public function saveFile($file, $replace = true): array {
|
|
$fileType = strtolower(pathinfo($_FILES["fileName"]["name"], PATHINFO_EXTENSION));
|
|
$fileName = strtolower(pathinfo($_FILES["fileName"]["name"], PATHINFO_FILENAME));
|
|
$file = SanitizeName::sanitizeName($fileName);
|
|
$cleanName = $file . '.' . $fileType;
|
|
|
|
$target_file = $this->path . '/' . $this->type . '/' . $cleanName;
|
|
|
|
|
|
/*
|
|
* @todo permit replace
|
|
*/
|
|
if (file_exists($target_file) && $replace === false) {
|
|
return [
|
|
'status' => 'danger',
|
|
'msg' => 'Le fichier existe déjà.'
|
|
];
|
|
}
|
|
|
|
$upload_max_size = ini_get('upload_max_filesize');
|
|
|
|
/*
|
|
* @Todo return max upload
|
|
*/
|
|
if ($_FILES["fileName"]["size"] > $upload_max_size) {
|
|
return [
|
|
'status' => 'danger',
|
|
'msg' => 'Le fichier soumis est trop volumineux.'
|
|
];
|
|
}
|
|
|
|
if (!array_key_exists($fileType, $this->mimePermit)) {
|
|
return [
|
|
'status' => 'danger',
|
|
'msg' => 'Les fichiers ' . $fileType . ' ne sont pas autorisés.'
|
|
];
|
|
}
|
|
|
|
if (move_uploaded_file($_FILES["fileName"]["tmp_name"], $target_file)) {
|
|
return [
|
|
'status' => 'success',
|
|
'msg' => 'Le fichier ' . htmlspecialchars($cleanName) . ' à bien été envoyé.'
|
|
];
|
|
} else {
|
|
return [
|
|
'status' => 'danger',
|
|
'msg' => 'Une erreur c\'est produite.'
|
|
];
|
|
}
|
|
}
|
|
|
|
public function deleteFile($filename) {
|
|
$path = realpath($this->path . $this->type);
|
|
$file = $path . '/' . $filename;
|
|
|
|
if (file_exists($file)) {
|
|
if (unlink($file)) {
|
|
return [
|
|
'status' => 'success',
|
|
'msg' => 'Le fichier a été supprimé avec succès.'
|
|
];
|
|
} else {
|
|
return [
|
|
'status' => 'danger',
|
|
'msg' => 'Une erreur c\'est produite lors de la suppression du fichier.'
|
|
];
|
|
}
|
|
} else {
|
|
return [
|
|
'status' => 'danger',
|
|
'msg' => 'Le fichier n\'existe pas.'
|
|
];
|
|
}
|
|
}
|
|
}
|