Esempio n. 1
0
 /**
  * Validates the input and returns an error code
  *
  * @return int
  */
 private function _validate()
 {
     if (isset($this->_validator)) {
         $call_params = array(&$this->_value, $this->_range);
         return call_user_func_array($this->_validator, $call_params) ?? self::ERROR_NONE;
     }
     switch ($this->_type) {
         case "bool":
             if (!in_array($this->_value, array('1', '0', 'true', 'false'))) {
                 return self::ERROR_INVALID;
             }
             $this->_value = $this->_value == '1' || $this->_value == 'true';
             break;
         case "int":
         case "float":
             if (!is_numeric($this->_value)) {
                 return self::ERROR_INVALID;
             }
             $this->_value = $this->_type === 'int' ? intval($this->_value, 10) : floatval($this->_value, 10);
             if (self::checkNumberRange($this->_value, $this->_range, $code)) {
                 return $code;
             }
             break;
         case "text":
         case "string":
             if (!is_string($this->_value)) {
                 return self::ERROR_INVALID;
             }
             if (self::checkStringLength($this->_value, $this->_range, $code)) {
                 return $code;
             }
             break;
         case "uuid":
             if (!is_string($this->_value) || !preg_match(new RegExp('^[a-f0-9]{8}\\-[a-f0-9]{4}\\-4[a-f0-9]{3}\\-[89ab][a-f0-9]{3}\\-[a-f0-9]{12}$', 'i'), $this->_value)) {
                 return self::ERROR_INVALID;
             }
             $this->_value = strtolower($this->_value);
             break;
         case "username":
             global $USERNAME_REGEX;
             if (!is_string($this->_value) || !$USERNAME_REGEX->match($this->_value)) {
                 return self::ERROR_INVALID;
             }
             break;
         case "url":
             if (!is_string($this->_value)) {
                 return self::ERROR_INVALID;
             }
             global $REWRITE_REGEX;
             if (stripos($this->_value, ABSPATH) === 0) {
                 $this->_value = CoreUtils::substring($this->_value, CoreUtils::length(ABSPATH) - 1);
             }
             if (!preg_match($REWRITE_REGEX, $this->_value) && !preg_match(new RegExp('^#[a-z\\-]+$'), $this->_value)) {
                 if (self::checkStringLength($this->_value, $this->_range, $code)) {
                     return $code;
                 }
                 if (!preg_match(new RegExp('^https?://[a-z\\d/.-]+/[ -~]+$', 'i'), $this->_value)) {
                     Response::fail('Link URL does not appear to be a valid link');
                 }
             }
             break;
         case "int[]":
             if (!is_string($this->_value) || !preg_match(new RegExp('^\\d{1,12}(?:,\\d{1,12})*$'), $this->_value)) {
                 return self::ERROR_INVALID;
             }
             $this->_value = explode(',', $this->_value);
             break;
         case "json":
             try {
                 $this->_value = JSON::decode($this->_value);
                 if (empty($this->_value)) {
                     throw new \Exception(rtrim('Could not decode JSON; ' . json_last_error(), '; '));
                 }
             } catch (\Exception $e) {
                 error_log($e->getMessage() . "\n" . $e->getTraceAsString());
                 return self::ERROR_INVALID;
             }
             break;
         case "timestamp":
             $this->_value = strtotime($this->_value);
             if ($this->_value === false) {
                 return self::ERROR_INVALID;
             }
             if (self::checkNumberRange($this->_value, $this->_range, $code)) {
                 return $code;
             }
             break;
         case "epid":
             $this->_value = Episodes::parseID($this->_value);
             if (empty($this->_value)) {
                 return self::ERROR_INVALID;
             }
             break;
     }
     return self::ERROR_NONE;
 }