View source
<?php
namespace Drupal\n1ed\Flmngr\FileUploaderServer\servlet;
use Drupal\n1ed\Flmngr\FileUploaderServer\lib\config\ConfigInterface;
use Drupal\n1ed\Flmngr\FileUploaderServer\lib\file\UtilsPHP;
use Exception;
class ServletConfig implements ConfigInterface {
protected $conf;
protected $testConf = [];
public function __construct($conf) {
$this->conf = $conf;
}
protected function getParameter($name, $defaultValue, $doAddTrailingSlash) {
if (array_key_exists($name, $this->testConf)) {
return $this
->addTrailingSlash($this->testConf[$name], $doAddTrailingSlash);
}
else {
if (array_key_exists($name, $this->conf)) {
return $this
->addTrailingSlash($this->conf[$name], $doAddTrailingSlash);
}
return $defaultValue;
}
}
protected function addTrailingSlash($value, $doAddTrailingSlash) {
if ($value != NULL && $doAddTrailingSlash && (strlen($value) == 0 || !substr($value, strlen($value) - 1) == DIRECTORY_SEPARATOR)) {
$value .= DIRECTORY_SEPARATOR;
}
return $value;
}
protected function getParameterStr($name, $defaultValue) {
return $this
->getParameter($name, $defaultValue, FALSE);
}
protected function getParameterInt($name, $defaultValue) {
$value = $this
->getParameter($name, $defaultValue, FALSE);
if (is_int($value) !== FALSE) {
return $value;
}
else {
error_log("Incorrect '" . $name . "' parameter integer value");
return $defaultValue;
}
}
protected function getParameterBool($name, $defaultValue) {
$value = $this
->getParameter($name, $defaultValue, FALSE);
if (is_bool($value) !== FALSE) {
return $value;
}
else {
error_log("Incorrect '" . $name . "' parameter boolean value");
return $defaultValue;
}
}
public function getBaseDir() {
$dir = $this
->getParameter("dirFiles", NULL, TRUE);
if ($dir == NULL) {
throw new Exception("dirFiles not set");
}
if (!file_exists($dir)) {
if (!mkdir($dir, 0777, TRUE)) {
throw new Exception("Unable to create files directory '" . $dir . "''");
}
}
return UtilsPHP::normalizeNoEndSeparator($dir);
}
public function getTmpDir() {
$dir = $this
->getParameter("dirTmp", $this
->getBaseDir() . "/tmp", TRUE);
if (!file_exists($dir)) {
if (!mkdir($dir)) {
throw new Exception("Unable to create temporary files directory '" . $dir . "''");
}
}
return UtilsPHP::normalizeNoEndSeparator($dir);
}
public function getMaxUploadFileSize() {
return $this
->getParameterInt("maxUploadFileSize", 0);
}
public function getAllowedExtensions() {
$value = $this
->getParameterStr("allowedExtensions", NULL);
if ($value === NULL) {
return [];
}
$exts = explode(",", $value);
for ($i = 0; $i < count($exts); $i++) {
$exts[$i] = strtolower($exts[$i]);
}
return $exts;
}
public function getJpegQuality() {
return $this
->getParameterInt("jpegQuality", 95);
}
public function getMaxImageResizeWidth() {
return $this
->getParameterInt("maxImageResizeWidth", 5000);
}
public function getMaxImageResizeHeight() {
return $this
->getParameterInt("maxImageResizeHeight", 5000);
}
public function getCrossDomainUrl() {
return $this
->getParameterStr("crossDomainUrl", NULL);
}
public function doKeepUploads() {
return $this
->getParameterBool("keepUploads", FALSE);
}
public function setTestConfig($testConf) {
$this->testConf = (array) $testConf;
}
public function isTestAllowed() {
return $this
->getParameterBool("isTestAllowed", FALSE);
}
public function getRelocateFromHosts() {
$hostsStr = $this
->getParameterStr("relocateFromHosts", "");
$hostsFound = explode(",", $hostsStr);
$hosts = [];
for ($i = count($hostsFound) - 1; $i >= 0; $i--) {
$host = strtolower(trim($hostsFound[$i]));
if (strlen($host) > 0) {
$hosts[] = $host;
}
}
return $hosts;
}
}