2016-05-15 00:08:30 +02:00
|
|
|
<?php
|
2017-01-12 20:45:01 +01:00
|
|
|
|
|
|
|
class FileUploader extends FileManager {
|
2018-09-20 20:52:27 +02:00
|
|
|
private $maxSize = 1;
|
2017-01-12 20:45:01 +01:00
|
|
|
private $linearCongruentialGenerator;
|
|
|
|
private $linearCongruentialGeneratorOffset;
|
2017-01-12 22:29:50 +01:00
|
|
|
private $fileName;
|
2018-09-14 06:14:15 +02:00
|
|
|
private $permission;
|
2018-10-06 03:23:19 +02:00
|
|
|
private $storage;
|
2017-01-12 20:45:01 +01:00
|
|
|
|
2016-05-15 00:08:30 +02:00
|
|
|
private static $instance = null;
|
|
|
|
|
|
|
|
public static function getInstance() {
|
|
|
|
if (self::$instance === null) {
|
|
|
|
self::$instance = new FileUploader();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
2018-10-06 03:23:19 +02:00
|
|
|
private function __construct() {
|
|
|
|
$this->storage = new \Upload\Storage\FileSystem($this->getLocalPath());
|
|
|
|
}
|
2016-05-15 00:08:30 +02:00
|
|
|
|
2018-09-20 20:52:27 +02:00
|
|
|
public function isSizeValid($file) {
|
|
|
|
return $file['size'] <= (1048576 * $this->maxSize);
|
|
|
|
}
|
|
|
|
|
2018-10-06 03:23:19 +02:00
|
|
|
public function upload($fileKey) {
|
|
|
|
$file = new \Upload\File($fileKey, $this->storage);
|
|
|
|
$file->setName($this->generateFileName($_FILES[$fileKey]['name']));
|
|
|
|
|
|
|
|
$file->addValidations(array(
|
|
|
|
new \Upload\Validation\Mimetype([
|
|
|
|
'image/png',
|
|
|
|
'image/jpeg',
|
|
|
|
'image/bmp',
|
|
|
|
'image/tiff',
|
|
|
|
|
|
|
|
'application/gzip',
|
|
|
|
'application/x-gzip',
|
|
|
|
'application/zip',
|
|
|
|
'application/x-rar-compressed',
|
|
|
|
'application/x-7z-compressed',
|
|
|
|
'application/x-tar',
|
|
|
|
'application/x-bzip',
|
|
|
|
'application/x-bzip2',
|
|
|
|
|
|
|
|
'text/csv',
|
|
|
|
'text/rtf',
|
|
|
|
'application/msword',
|
|
|
|
'application/vnd.ms-excel',
|
|
|
|
'text/plain',
|
|
|
|
'application/pdf'
|
|
|
|
]),
|
|
|
|
new \Upload\Validation\Size($this->maxSize.'M')
|
|
|
|
));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$file->upload();
|
|
|
|
$this->setFileName($file->getNameWithExtension());
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
2017-01-12 20:45:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-15 00:08:30 +02:00
|
|
|
}
|
2017-01-12 20:45:01 +01:00
|
|
|
|
2018-10-06 03:23:19 +02:00
|
|
|
private function generateFileName($fileName) {
|
|
|
|
$newName = $this->removeFileExtension($fileName);
|
2017-01-12 20:45:01 +01:00
|
|
|
$newName = strtolower($newName);
|
2017-06-20 21:47:27 +02:00
|
|
|
$newName = preg_replace('/[^a-zA-Z0-9\d\.\-]/', '_', $newName);
|
2018-10-06 03:23:19 +02:00
|
|
|
$result = "";
|
2017-01-12 20:45:01 +01:00
|
|
|
|
|
|
|
if ($this->linearCongruentialGenerator instanceof LinearCongruentialGenerator) {
|
2018-10-06 03:23:19 +02:00
|
|
|
if($this->permission) $result = $this->permission . '_';
|
|
|
|
else $result = '';
|
2018-09-14 06:14:15 +02:00
|
|
|
|
2018-10-06 03:23:19 +02:00
|
|
|
$result .= $this->linearCongruentialGenerator->generate($this->linearCongruentialGeneratorOffset) . '_' . $newName;
|
2017-01-12 20:45:01 +01:00
|
|
|
}
|
2018-10-06 03:23:19 +02:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeFileExtension($fileName) {
|
|
|
|
return substr($fileName, 0, strrpos($fileName, "."));
|
2018-09-14 06:14:15 +02:00
|
|
|
}
|
2017-01-12 20:45:01 +01:00
|
|
|
|
2018-09-14 06:14:15 +02:00
|
|
|
public function setPermission($type = '', $extra = '') {
|
|
|
|
if($type === FileManager::PERMISSION_ARTICLE) $this->permission = 'a';
|
|
|
|
else if($type === FileManager::PERMISSION_TICKET) $this->permission = 't' . $extra;
|
|
|
|
else if($type === FileManager::PERMISSION_PROFILE) $this->permission = 'p';
|
|
|
|
else $this->permission = '';
|
2017-01-12 20:45:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setGeneratorValues($gap, $first, $offset) {
|
|
|
|
$this->linearCongruentialGenerator = new LinearCongruentialGenerator();
|
|
|
|
$this->linearCongruentialGeneratorOffset = $offset;
|
|
|
|
|
|
|
|
$this->linearCongruentialGenerator->setGap($gap);
|
|
|
|
$this->linearCongruentialGenerator->setFirst($first);
|
|
|
|
}
|
2018-09-14 06:14:15 +02:00
|
|
|
|
2017-01-12 20:45:01 +01:00
|
|
|
public function setMaxSize($maxSize) {
|
|
|
|
$this->maxSize = $maxSize;
|
|
|
|
}
|
2018-09-14 06:14:15 +02:00
|
|
|
|
2018-10-06 03:23:19 +02:00
|
|
|
public function setFileName($fileName) {
|
|
|
|
$this->fileName = $fileName;
|
|
|
|
}
|
|
|
|
|
2017-01-12 22:29:50 +01:00
|
|
|
public function getFileName() {
|
|
|
|
return $this->fileName;
|
|
|
|
}
|
2018-09-14 06:14:15 +02:00
|
|
|
}
|