You are here

class ServletConfig in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

Implementation of ConfigInterface interface. Returnes values with validationing them.

Hierarchy

Expanded class hierarchy of ServletConfig

File

src/Flmngr/FileUploaderServer/servlet/ServletConfig.php, line 13

Namespace

Drupal\n1ed\Flmngr\FileUploaderServer\servlet
View source
class ServletConfig implements ConfigInterface {
  protected $conf;
  protected $testConf = [];

  /**
   * Creates an instance.
   */
  public function __construct($conf) {
    $this->conf = $conf;
  }

  /**
   * Gets a parameter from config.
   */
  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;
    }
  }

  /**
   * Adds a trainling slash for path.
   */
  protected function addTrailingSlash($value, $doAddTrailingSlash) {
    if ($value != NULL && $doAddTrailingSlash && (strlen($value) == 0 || !substr($value, strlen($value) - 1) == DIRECTORY_SEPARATOR)) {
      $value .= DIRECTORY_SEPARATOR;
    }
    return $value;
  }

  /**
   * Gets string parameter.
   */
  protected function getParameterStr($name, $defaultValue) {
    return $this
      ->getParameter($name, $defaultValue, FALSE);
  }

  /**
   * Gets integer parameter.
   */
  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;
    }
  }

  /**
   * Gets boolean parameter.
   */
  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;
    }
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function getMaxUploadFileSize() {
    return $this
      ->getParameterInt("maxUploadFileSize", 0);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getJpegQuality() {
    return $this
      ->getParameterInt("jpegQuality", 95);
  }

  /**
   * {@inheritdoc}
   */
  public function getMaxImageResizeWidth() {
    return $this
      ->getParameterInt("maxImageResizeWidth", 5000);
  }

  /**
   * {@inheritdoc}
   */
  public function getMaxImageResizeHeight() {
    return $this
      ->getParameterInt("maxImageResizeHeight", 5000);
  }

  /**
   * {@inheritdoc}
   */
  public function getCrossDomainUrl() {
    return $this
      ->getParameterStr("crossDomainUrl", NULL);
  }

  /**
   * {@inheritdoc}
   */
  public function doKeepUploads() {
    return $this
      ->getParameterBool("keepUploads", FALSE);
  }

  /**
   * {@inheritdoc}
   */
  public function setTestConfig($testConf) {
    $this->testConf = (array) $testConf;
  }

  /**
   * {@inheritdoc}
   */
  public function isTestAllowed() {
    return $this
      ->getParameterBool("isTestAllowed", FALSE);
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ServletConfig::$conf protected property
ServletConfig::$testConf protected property
ServletConfig::addTrailingSlash protected function Adds a trainling slash for path.
ServletConfig::doKeepUploads public function A flag to keep uplaods. Overrides ConfigInterface::doKeepUploads
ServletConfig::getAllowedExtensions public function Gets allowed extensions array. Overrides ConfigInterface::getAllowedExtensions
ServletConfig::getBaseDir public function Gets a path to base directory. Overrides ConfigInterface::getBaseDir
ServletConfig::getCrossDomainUrl public function Get crossdomain URL (for CORS). Overrides ConfigInterface::getCrossDomainUrl
ServletConfig::getJpegQuality public function Gets JPEG quality in percents as image optimization option. Overrides ConfigInterface::getJpegQuality
ServletConfig::getMaxImageResizeHeight public function Gets maximum image height for resizing. Overrides ConfigInterface::getMaxImageResizeHeight
ServletConfig::getMaxImageResizeWidth public function Gets maximum image width for resizing. Overrides ConfigInterface::getMaxImageResizeWidth
ServletConfig::getMaxUploadFileSize public function Gets maximum size of uploading file in bytes. Overrides ConfigInterface::getMaxUploadFileSize
ServletConfig::getParameter protected function Gets a parameter from config.
ServletConfig::getParameterBool protected function Gets boolean parameter.
ServletConfig::getParameterInt protected function Gets integer parameter.
ServletConfig::getParameterStr protected function Gets string parameter.
ServletConfig::getRelocateFromHosts public function Gets a list of hosts a relocation is allowed from. Overrides ConfigInterface::getRelocateFromHosts
ServletConfig::getTmpDir public function Gets a path to temporary directory. Overrides ConfigInterface::getTmpDir
ServletConfig::isTestAllowed public function Is testing allowed, do not turn on on production. Overrides ConfigInterface::isTestAllowed
ServletConfig::setTestConfig public function Sets a test config. Overrides ConfigInterface::setTestConfig
ServletConfig::__construct public function Creates an instance.