/** * UploadField::isValid() * * Check if the value is valid * * @return bool: check if the value is valid * @access public * @author Teye Heimans */ function isValid() { // make the files array global if they are not if (!_global) { global $_FILES; } /** * Removed this part in order to get the required parameter working. * @since 02-04-2008 * @author Johan Wiegel * * reactivated this part seems to work why was this removed?? 29-04-2009 JW */ // when no uploadfield was submitted (on multi-paged forms) if (!isset($_FILES[$this->_sName])) { return true; } // check if we have validated this field before if (isset($this->_isValid)) { return $this->_isValid; } // is a own error handler used? if (isset($this->_sValidator) && !empty($this->_sValidator)) { // check the field with the users validator $this->_isValid = Field::isValid(); return $this->_isValid; } // easy name to work with (this is the $_FILES['xxx'] array ) $aFile = $this->_mValue; // alert when file exists ? (and is a file uploaded ?) if (strtolower($this->_aConfig['exists']) == 'alert' && $aFile['error'] == 0 && file_exists($this->_aConfig['path'] . $this->_getFilename(true))) { $this->_sError = $this->_oForm->_text(21); } // check if the field is required and if it is uploaded. if ((is_bool($this->_aConfig['required']) && $this->_aConfig['required'] || strtolower(trim($this->_aConfig['required'])) == 'true') && $aFile['error'] == 4 && empty($this->_sFilename)) { // no file uploaded $this->_sError = $this->_oForm->_text(22); } elseif ($aFile['error'] != 4) { // file size to big? if (isset($aFile['error']) && ($aFile['error'] == 1 || $aFile['error'] == 2 || $aFile['size'] > $this->_aConfig['size'])) { $this->_sError = sprintf($this->_oForm->_text(23), round($this->_aConfig['size'] / 1024, 2)); $this->_isValid = false; return false; } // is the extension correct ? $sExt = $this->_getExtension($this->_mValue['name']); if (!$sExt) { // no extension found! $this->_sError = $this->_oForm->_text(37); } else { // check if the extension is allowed if ($this->_aConfig['type'] != '*' && !in_array($sExt, explode(' ', strtolower($this->_aConfig['type'])))) { $this->_sError = sprintf($this->_oForm->_text(20), $this->_aConfig['type']); } // does build in function exists for retrieving the mime type of the file? // if so, get the type of the mime from that function (more secure) // otherwise, use the one from the browser // as of php 5.3 mime_content_type is deprecated, use finfo_open instead if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME); $sTypeRaw = finfo_file($finfo, $aFile['tmp_name']); finfo_close($finfo); list($sType) = preg_split('/;/', $sTypeRaw); } else { $sType = function_exists('mime_content_type') ? mime_content_type($aFile['tmp_name']) : $aFile['type']; } // get the mime data $aMimeData = unserialize(FH_MIME_DATA); // the allowed mime types given by the user $aUsrMimeData = $this->_aConfig['mime']; # Debug message /* echo "<fieldset><legend><b>Debug Mime data</b></legend>\n". "Extension of the uploaded file: ".$sExt ."<br '. FH_XHTML_CLOSE .'>\n". "Mime type of uploaded file: ". $sType ."<br '. FH_XHTML_CLOSE .'>\n". "Extension known: ".(isset( $aMimeData[$sExt] ) ? "true":"false")."<br '. FH_XHTML_CLOSE .'>\n". (isset( $aMimeData[$sExt] ) ? "Mime type(s) expected: " . implode(", ", $aMimeData[$sExt]) : '')."<br '. FH_XHTML_CLOSE .'>\n". "User allowed mime types:<br'. FH_XHTML_CLOSE .'>\n<pre>"; print_r( $aUsrMimeData ); echo "</pre>\n". "</fieldset>\n"; */ // check if the mime type is allowed // wo 8 feb 2006: added "!isset( $aUsrMimeData['*'] ) || " if ((!isset($aUsrMimeData[$sExt]) || !in_array($sType, $aUsrMimeData[$sExt])) && (!isset($aUsrMimeData['*']) || !in_array($sType, $aUsrMimeData['*'])) && (!isset($aMimeData[$sExt]) || !in_array($sType, $aMimeData[$sExt]))) { // mime type is not allowed! $this->_sError = $this->_oForm->_text(31); } else { // is it an image and is a max width/height given? Get the proportions if (preg_match('/^image\\//', $sType) && !empty($this->_aConfig['height']) || !empty($this->_aConfig['width'])) { // size is incorrect.. give a size error message list($iWidth, $iHeight) = getimagesize($aFile['tmp_name']); if ((int) $this->_aConfig['height'] > 0 && $iHeight > (int) $this->_aConfig['height'] || (int) $this->_aConfig['width'] > 0 && $iWidth > (int) $this->_aConfig['width']) { $this->_sError = sprintf($this->_oForm->_text(32), (int) $this->_aConfig['width'], (int) $this->_aConfig['height'], $iWidth, $iHeight); } } } // if an error occured.. if ($aFile['error'] == 3) { $this->_sError = $this->_oForm->_text(24); } } } // when no error ocoured, the file is valid $this->_isValid = empty($this->_sError); return $this->_isValid; }
/** * TextArea::isValid() * * Check if the field's value is valid * * @return boolean * @access public * @author Teye Heimans */ function isValid() { // is a max length set ? if (isset($this->_iMaxLength) && $this->_iMaxLength > 0) { // is there to many data submitted ? $iLen = strlen($this->_mValue); if ($iLen > $this->_iMaxLength) { // set the error message $this->_sError = sprintf($this->_oForm->_text(40), $this->_iMaxLength, $iLen, abs($iLen - $this->_iMaxLength)); // return false because the value is not valid return false; } } // everything ok untill here, use the default validator return parent::isValid(); }
/** * DateField::isValid() * * Check if the date is valid (eg not 31-02-2003) * * @return boolean: true if the field is correct, false if not * @access public * @author Teye Heimans */ function isValid() { // the result has been requested before.. if (isset($this->_isValid)) { return $this->_isValid; } // check if the year field is valid if (isset($this->_oYear) && is_object($this->_oYear)) { if (!$this->_oYear->isValid()) { // get the error $this->_sError = $this->_oYear->getError(); return false; } } // check if the month field is valid if (isset($this->_oMonth) && is_object($this->_oMonth)) { if (!$this->_oMonth->isValid()) { // get the error $this->_sError = $this->_oMonth->getError(); return false; } } // check if the day field is valid if (isset($this->_oDay) && is_object($this->_oDay)) { if (!$this->_oDay->isValid()) { // get the error $this->_sError = $this->_oDay->getError(); return false; } } $d = $this->getValue('d'); $m = $this->getValue('m'); $y = $this->getValue('y'); $mask = strtolower($this->_sMask); if ($y != '' && strlen($y) != 4) { $this->_sError = $this->_oForm->_text(13); return false; } // first of al check if the date is right when a valid date is submitted // (but only when all fields are displayed (d m and y or t in the display string!) if (strpos($mask, 'd') !== false && strpos($mask, 'm') !== false && strpos($mask, 'y') !== false && ($d != '00' && $d != '') && ($m != '00' && $m != '') && ($y != '0000' && $y != '') && !checkdate($m, $d, $y)) { $this->_sError = $this->_oForm->_text(13); $this->_isValid = false; return $this->_isValid; } // if validator given, check the value with the validator if (isset($this->_sValidator) && !empty($this->_sValidator)) { $this->_isValid = parent::isValid(); } else { $this->_isValid = true; } return $this->_isValid; }