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;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function __construct() {}
|
|
|
|
|
2018-09-20 20:52:27 +02:00
|
|
|
public function isSizeValid($file) {
|
|
|
|
return $file['size'] <= (1048576 * $this->maxSize);
|
|
|
|
}
|
|
|
|
|
2017-01-12 20:45:01 +01:00
|
|
|
public function upload($file) {
|
2017-01-12 22:29:50 +01:00
|
|
|
$this->setNewName($file['name']);
|
2017-01-12 20:45:01 +01:00
|
|
|
|
2018-09-20 20:52:27 +02:00
|
|
|
if(!$this->isSizeValid($file)) {
|
2017-01-12 20:45:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-14 06:14:15 +02:00
|
|
|
|
2017-01-12 22:29:50 +01:00
|
|
|
move_uploaded_file($file['tmp_name'], $this->getLocalPath() . $this->getFileName());
|
2017-01-12 20:45:01 +01:00
|
|
|
|
|
|
|
return true;
|
2016-05-15 00:08:30 +02:00
|
|
|
}
|
2017-01-12 20:45:01 +01:00
|
|
|
|
2017-01-12 22:29:50 +01:00
|
|
|
private function setNewName($fileName) {
|
2017-01-12 20:45:01 +01:00
|
|
|
$newName = $fileName;
|
|
|
|
$newName = strtolower($newName);
|
2017-06-20 21:47:27 +02:00
|
|
|
$newName = preg_replace('/[^a-zA-Z0-9\d\.\-]/', '_', $newName);
|
2017-01-12 20:45:01 +01:00
|
|
|
|
|
|
|
if ($this->linearCongruentialGenerator instanceof LinearCongruentialGenerator) {
|
2018-09-14 06:14:15 +02:00
|
|
|
if($this->permission) $this->fileName = $this->permission . '_';
|
|
|
|
else $this->fileName = '';
|
|
|
|
|
|
|
|
$this->fileName .= $this->linearCongruentialGenerator->generate($this->linearCongruentialGeneratorOffset) . '_' . $newName;
|
2017-01-12 20:45:01 +01:00
|
|
|
}
|
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
|
|
|
|
2017-01-12 22:29:50 +01:00
|
|
|
public function getFileName() {
|
|
|
|
return $this->fileName;
|
|
|
|
}
|
2018-09-14 06:14:15 +02:00
|
|
|
}
|