示例#1
0
 /**
  * Validates absolute, relative and anchor URL through regular expression.
  *
  * @param array $record Record that contains value to be validated.
  *                           Errors are saved in this record, in the 'atkerror'
  *                           field.
  * @param string $mode Validation mode. Can be either "add" or "update"
  * @param bool $show_error fire a triggerError when validation fails
  */
 public function validateUrl(&$record, $mode, $show_error = false)
 {
     $result = false;
     $absolute_result = true;
     $anchor_result = true;
     $absolute_anchor_result = true;
     $relative_result = true;
     $base_url_regex = "(ft|htt)ps?:\\/\\/[a-zA-Z0-9\\.\\-\\_]+\\.[a-zA-Z]{2,4}";
     $relative_url_regex = "[a-zA-Z0-9\\.\\-\\_\\/?&=%]";
     $relative_url_regex_with_anchor = "[a-zA-Z0-9\\.\\-\\_\\/?&=%#]";
     /*
      * Validate URL, check if format is absolute (external URL's) and has no anchor
      *
      * Example: http://www2-dev.test_url.com
      * or:      ftp://www2-dev.test_url.com/index.php?/feeds/index.rss2
      */
     if (($this->m_accepts_url_flag & self::ABSOLUTE) == self::ABSOLUTE) {
         $absolute_result = preg_match('/^' . $base_url_regex . $relative_url_regex . '*$/Ui', $record[$this->fieldName()]) ? true : false;
         $result = $result || $absolute_result;
     }
     /*
      * Validate URL, check if format is a valid anchor
      *
      * Example: #internal_bookmark
      */
     if (($this->m_accepts_url_flag & self::ANCHOR) == self::ANCHOR) {
         $anchor_result = preg_match('/^#' . $relative_url_regex . '*$/Ui', $record[$this->fieldName()]) ? true : false;
         $result = $result || $anchor_result;
     }
     /*
      * Validate URL, check if format is absolute (external URL's) and has (optional) anchor
      *
      * Example: http://www2-dev.test_url.com
      * or:      ftp://www2-dev.test_url.com/index.php?/feeds/index.rss2
      * or:      https://www2-dev.test_url.com/index.php?/history.html#bookmark
      */
     if (($this->m_accepts_url_flag & self::ABSOLUTE) == self::ABSOLUTE && ($this->m_accepts_url_flag & self::ANCHOR) == self::ANCHOR) {
         $absolute_anchor_result = preg_match('/^' . $base_url_regex . $relative_url_regex_with_anchor . '*$/Ui', $record[$this->fieldName()]) ? true : false;
         $result = $result || $absolute_anchor_result;
     }
     /*
      * Validate URL, check if format is relative
      *
      * Example: /mysite/guestbook/index.html
      */
     if (($this->m_accepts_url_flag & self::RELATIVE) == self::RELATIVE) {
         $relative_result = preg_match('/^' . $relative_url_regex_with_anchor . '+$/Ui', $record[$this->fieldName()]) ? true : false;
         $result = $result || $relative_result;
     }
     /*
      * If an error occured, show applicable message(s)
      */
     if (!$result && $show_error) {
         // if result of all validations is false, display error-messages
         if ($absolute_result === false) {
             Tools::triggerError($record, $this->fieldName(), 'invalid_absolute_no_anchor_url', Tools::atktext('invalid_absolute_no_anchor_url'));
         }
         if ($anchor_result === false) {
             Tools::triggerError($record, $this->fieldName(), 'invalid_url_anchor', Tools::atktext('invalid_url_anchor'));
         }
         if ($absolute_anchor_result === false) {
             Tools::triggerError($record, $this->fieldName(), 'invalid_absolute_url', Tools::atktext('invalid_absolute_url'));
         }
         if ($relative_result === false) {
             Tools::triggerError($record, $this->fieldName(), 'invalid_relative_url', Tools::atktext('invalid_relative_url'));
         }
     }
     if (!$result) {
         parent::validate($record, $mode);
     }
 }
示例#2
0
 /**
  * Checks if the value is a valid IP address.
  *
  * @param array $record The record that holds the value for this
  *                       attribute. If an error occurs, the error will
  *                       be stored in the 'atkerror' field of the record.
  * @param string $mode The mode for which should be validated ("add" or
  *                       "update")
  */
 public function validate(&$record, $mode)
 {
     // Check for valid ip string
     $strvalue = Tools::atkArrayNvl($record, $this->fieldName(), '');
     if ($strvalue != '' && $strvalue != '...') {
         if ($this->hasFlag(self::AF_IP_ALLOW_WILDCARDS) && !$this->hasFlag(self::AF_IP_STORENUMERIC)) {
             $strvalue = str_replace('*', '0', $strvalue);
         }
         $num = '(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])';
         if (preg_match("/^{$num}\\.{$num}\\.{$num}\\.{$num}\$/", $strvalue, $matches) <= 0) {
             Tools::triggerError($record, $this->fieldName(), 'error_not_a_valid_ip');
         }
     }
     parent::validate($record, $mode);
 }
示例#3
0
 /**
  * Validate attribute value.
  *
  * @param Attribute $p_attrib pointer to the attribute
  * @param array $record record
  */
 public function validateAttributeValue($p_attrib, &$record)
 {
     if (!$p_attrib->isEmpty($record)) {
         $funcname = $p_attrib->m_name . '_validate';
         if (method_exists($this->m_nodeObj, $funcname)) {
             $this->m_nodeObj->{$funcname}($record, $this->m_mode);
         } else {
             $p_attrib->validate($record, $this->m_mode);
         }
     }
 }
示例#4
0
 /**
  * Checks if the file has a valid filetype.
  *
  * Note that obligatory and unique fields are checked by the
  * atkNodeValidator, and not by the validate() method itself.
  *
  * @param array $record The record that holds the value for this
  *                       attribute. If an error occurs, the error will
  *                       be stored in the 'atkerror' field of the record.
  * @param string $mode The mode for which should be validated ("add" or
  *                       "update")
  */
 public function validate(&$record, $mode)
 {
     parent::validate($record, $mode);
     $this->isAllowedFileType($record);
     $error = $record[$this->fieldName()]['error'];
     if ($error > 0) {
         $error_text = $this->fetchFileErrorType($error);
         Tools::atkTriggerError($record, $this, $error_text, Tools::atktext($error_text, 'atk'));
     }
 }