function check($form) { $xml = new xml($form); $res = $xml->query('.//field[@check]', $form); $pswd = null; foreach ($res as $field) { $val = param($field->getAttribute('name')); switch ($field->getAttribute('type')) { case 'file': if (strstr($field->getAttribute('check'), 'empty') && count($_FILES) == 0) { $this->err('The file is not attached'); } if ($field->getAttribute('accept') && count($_FILES) > 0 && ($files = $_FILES[$field->getAttribute('name')])) { $arrAccept = explode(',', $field->getAttribute('accept')); switch (is_array($files['error'])) { case true: foreach ($files['error'] as $i => $code) { if ($code == 0 && !in_array($files['type'][$i], $arrAccept)) { $this->err('Invalid format of attached file. File name is "' . $files['name'][$i] . '"'); } if ($code == 0 && ($maxSize = $field->getAttribute('maxFileSize')) && $files['size'][$i] > $maxSize) { $this->err('Exceeded the allowable size of the file. File name is "' . $files['name'][$i] . '"'); } } break; case false: if ($files['error'] == 0 && !in_array($files['type'], $arrAccept)) { $this->err('Invalid format of attached file'); } if ($files['error'] == 0 && ($maxSize = $field->getAttribute('maxFileSize')) && $files['size'] > $maxSize) { $this->err('Exceeded the allowable size of the uploaded file'); } break; } } break; case 'password': if (!$pswd && $field->getAttribute('name') == 'password') { $pswd = $val; } if (isset($pswd) && $field->getAttribute('name') == 'password-check' && $pswd != $val) { $this->err('The entered passwords do not match'); } if (strstr($field->getAttribute('check'), 'empty') && !$val) { $this->err('Field "' . $field->getAttribute('label') . '" is not filled'); } break; case 'email': if ($val && !mymail::isEmail($val)) { $this->err('E-mail in field "' . $field->getAttribute('label') . '" incorrectly'); } break; case 'checkbox': case 'radio': if (!$val) { $this->err('Field "' . $field->getAttribute('label') . '" not observed'); } break; default: $mysql = new mysql(); if ($field->getAttribute('login') && ($res = $mysql->query("SELECT `login` FROM `" . $mysql->getTableName($form->getAttribute('dbTable')) . "` WHERE `login`='" . ($val ? $val : null) . "'", true))) { $this->err('A user with this login ' . $val . ' already exists.'); } if ($field->getAttribute('type') != 'captcha') { if (strstr($field->getAttribute('check'), 'empty') && !$val) { $this->err('Field "' . $field->getAttribute('label') . '" is not filled'); } } else { if ($field->getAttribute('show') && strstr($field->getAttribute('check'), 'empty') && !$val) { $this->err('Field "' . $field->getAttribute('label') . '" is not filled'); } } } } if ($this->hasCaptcha($form)) { $captcha = new captcha(); $captcha->setParamName('captcha'); if (!$captcha->check()) { $this->err('The result of the expression in the image incorrectly'); } } return $this->hasErrors(); }
function validateFieldValue($form, $field, $val) { $pswd = null; $error = null; switch ($field->getAttribute('type')) { case 'file': if (strstr($field->getAttribute('check'), 'empty') && count($_FILES) == 0) { $error = 'The file is not attached'; } if ($field->getAttribute('accept') && count($_FILES) > 0 && ($files = $_FILES[$field->getAttribute('name')])) { $arrAccept = explode(',', $field->getAttribute('accept')); switch (is_array($files['error'])) { case true: foreach ($files['error'] as $i => $code) { if ($code == 0 && !in_array($files['type'][$i], $arrAccept)) { $error = 'Invalid format of attached file. File name "' . $files['name'][$i] . '"'; } if ($code == 0 && ($maxSize = $field->getAttribute('maxFileSize')) && $files['size'][$i] > $maxSize) { $error = 'Exceeded the allowable size of the file. File name "' . $files['name'][$i] . '"'; } } break; case false: if ($files['error'] == 0 && !in_array($files['type'], $arrAccept)) { $error = 'Invalid format of attached file'; } if ($files['error'] == 0 && ($maxSize = $field->getAttribute('maxFileSize')) && $files['size'] > $maxSize) { $error = 'Exceeded the allowable size of the file'; } break; } } break; case 'password': if (!$pswd && $field->getAttribute('name') == 'password') { $pswd = $val; } if (isset($pswd) && $field->getAttribute('name') == 'password-check' && $pswd != $val) { $error = 'The entered passwords do not match'; } if (strstr($field->getAttribute('check'), 'empty') && !$val) { $error = 'The field "' . $field->getAttribute('label') . '" is not filled'; } break; case 'email': if ($val && !mymail::isEmail($val)) { $error = 'E-mail address in the field "' . $field->getAttribute('label') . '" is invalid'; } break; case 'checkbox': case 'radio': if (!$val) { $error = 'The field "' . $field->getAttribute('label') . '" unchecked'; } break; case 'recaptcha': if ($val) { $json = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, stream_context_create(array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query(array('secret' => $field->getAttribute('secret'), 'response' => $val)))))); if ($json && ($resp = json_decode($json))) { if (empty($resp->success)) { $this->err('Captcha entered incorrect'); } } else { $this->err('Error! Communicating with the server failed'); } } else { $this->err('Captcha entered incorrect'); } break; default: if ($field->getAttribute('login') && ($mysql = new mysql()) && $mysql->query("SELECT `login` FROM `" . $mysql->getTableName($form->getAttribute('dbTable')) . "` WHERE `login`='" . ($val ? $val : null) . "'", true)) { $error = 'Login ' . $val . ' already exists.'; } if ($field->getAttribute('type') != 'captcha') { if (strstr($field->getAttribute('check'), 'empty') && !$val) { $error = 'The field "' . $field->getAttribute('label') . '" required'; } } else { if ($field->getAttribute('show') && strstr($field->getAttribute('check'), 'empty') && !$val) { $error = 'The field "' . $field->getAttribute('label') . '" required'; } } } return $error; }
function emailCheck($emails) { if (is_array($emails)) { foreach ($emails as $email) { if (!mymail::isEmail($email)) { return false; } } return true; } return mymail::isEmail($emails); }