Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once "../../templates/footer.php";
- require_once "../../configs/paths.php";
- include_once "../../templates/cookie-warning.php";
- include "../../templates/error.html";
- global $errors;
- $errors = array();
- function allParamsPresent() {
- // x & y are also params since the submit button is a image
- $mandatory_params = array('name', 'gender', 'username', 'cpf', 'phone', 'birth-date', 'password', 'confirm-password', 'institution-name', 'institution-uf', 'institution-city', 'g-recaptcha-response');
- for ($i=0; $i < count($mandatory_params); $i++) {
- if (!(isset($_POST[$mandatory_params[$i]]))) {
- //Param $expected_params[$i] is missing on the request
- $error = 'MISSING DATA \'' . $mandatory_params[$i] . '\'';
- $errors["params"] = $error;
- return false;
- }
- }
- //No params missing (params not validated yet, only present)
- return true;
- }
- function validateName($name = null) {
- if (empty($name)) {
- $errors["name"] = "EMPTY";
- return false;
- }
- $reg = '/[^\p{L}\s]+/u'; //Match all chars that are not letters (UTF-8 letters are ok) or whitespaces
- $verif = preg_replace($reg, '', $name);
- if ($name === $verif) {
- //Remove space if is the first or last char
- while (empty(explode(' ', $name)[0])) {
- $name = substr($name, 1);
- }
- while (empty(explode(' ', $name)[count(explode(' ', $name)) - 1])) {
- $name = substr($name, 0, strlen($name)-1);
- }
- if (count(explode(' ', $name)) < 2) {
- //Name is fine but it is only the first time
- $errors["name"] = "ONLY FIRST NAME";
- return false;
- } else {
- //Full name
- $_POST['full_name'] = $name;
- return true;
- }
- } else {
- //Name with not supported chars
- $errors["name"] = "NOT SUPPORTED LETTERS";
- return false;
- }
- }
- function validateGender($gender = null) {
- switch ($gender) {
- case 'M':
- return true;
- case 'F':
- return true;
- case 'NF':
- return true;
- default:
- $errors["gender"] = "NOT SUPPORTED GENDER";
- return false;
- }
- }
- function validateEmail($email = null) {
- if(empty($email)) {
- $errors["email"] = "EMPTY";
- return false;
- }
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $errors["email"] = "NOT VALID EMAIL";
- return false;
- } else {
- return true;
- }
- }
- function validateCpf($cpf = null) {
- if(empty($cpf)) {
- $errors["cpf"] = "EMPTY";
- return false;
- }
- // Elimina possivel mascara
- $cpf = preg_replace("/[^0-9]/", "", $cpf);
- $cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
- if (strlen($cpf) != 11) {
- $errors["cpf"] = "TOO SHORT";
- return false;
- }
- else if ($cpf == '00000000000' ||
- $cpf == '11111111111' ||
- $cpf == '22222222222' ||
- $cpf == '33333333333' ||
- $cpf == '44444444444' ||
- $cpf == '55555555555' ||
- $cpf == '66666666666' ||
- $cpf == '77777777777' ||
- $cpf == '88888888888' ||
- $cpf == '99999999999') {
- $errors["cpf"] = "INVALID";
- return false;
- } else {
- for ($t = 9; $t < 11; $t++) {
- for ($d = 0, $c = 0; $c < $t; $c++) {
- $d += $cpf{$c} * (($t + 1) - $c);
- }
- $d = ((10 * $d) % 11) % 10;
- if ($cpf{$c} != $d) {
- $errors["cpf"] = "INVALID";
- return false;
- }
- }
- return true;
- }
- }
- function validatePhone($phone = null) {
- if(empty($phone)) {
- $errors["phone"] = "EMPTY";
- return false;
- }
- $phone= trim(str_replace('/', '', str_replace(' ', '', str_replace('-', '', str_replace(')', '', str_replace('(', '', $phone))))));
- $phone_regex = "/^[0-9]{11}$/";
- if (preg_match($phone_regex, $phone) == 1) {
- return true;
- } else {
- $errors["phone"] = "WRONG FORMAT";
- return false;
- }
- }
- function validateBirth($date = null) {
- $min_age = 12;
- date_default_timezone_set('America/Sao_Paulo');
- if (empty($date) ||
- strlen($date) != 10 ||
- $date[2] != '/' ||
- $date[5] != '/') {
- $errors["birth"] = "WRONG FORMAT";
- return false;
- }
- $date = explode("/", $date);
- $year = $date[2];
- $month = $date[1];
- $day = $date[0];
- if (!(checkdate((int)$month, (int)$day, (int)$year))) {
- $errors["birth"] = "INVALID DATE";
- return false;
- }
- $today = new DateTime(date('m/d/Y', time()));
- $birth = new DateTime($month . '.' . $day . '.' . $year);
- $age = $today->diff($birth)->y;
- if ($age < $min_age) {
- $errors["birth"] = "AGE BELOW MINIMUM";
- return false;
- } else {
- //Change date on $_POST to SQL friendly
- $birth = $year . '-' . $month . '-' . $day;
- $_POST['birth-date'] = $birth;
- return true;
- }
- }
- function validatePassword($pwrd = null, $confirm_pwrd = null) {
- $pwrd_req = array('numbers' => 1, 'up_case' => 1, 'low_case' => 1, 'min_length' => 8, 'max_length' => 30);
- //Has to be different ifs because needs to set the right error
- if (empty($pwrd)) {
- $errors["password"] = "EMPTY";
- return false;
- }
- if (empty($confirm_pwrd)) {
- $errors["password"] = "CONFIRM EMPTY";
- return false;
- }
- if ($pwrd != $confirm_pwrd) {
- $errors["password"] = "NOT EQUAL TO CONFIRM";
- return false;
- }
- if (strlen($pwrd) > $pwrd_req['max_length']) {
- $errors["password"] = "TOO LONG";
- return false;
- }
- if (strlen($pwrd) < $pwrd_req['min_length']) {
- $errors["password"] = "TOO SHORT";
- return false;
- }
- //Check if uppercase if present
- if (preg_match("/[A-Z]/", $pwrd)===0) {
- return false;
- }
- //Check if lower case is present
- if (preg_match("/[a-z]/", $pwrd)===0) {
- return false;
- }
- //Check if digit is present
- if (preg_match("/[0-9]/", $pwrd)===0) {
- return false;
- }
- //Check if has white space
- if (!(preg_match("/\s/", $pwrd))===0) {
- return false;
- }
- return true;
- }
- function validateInstName($inst_name = null) {
- //Disable if inst not declared
- if ($_POST['institution-checkbox'] == 'on') {
- return true;
- }
- if (empty($inst_name)) {
- $errors["inst-name"] = "EMPTY";
- return false;
- }
- $reg = '/[^\p{L}\s]+/u'; //Match all chars that are not letters (UTF-8 letters are ok) or whitespaces
- $verif = preg_replace($reg, '', $inst_name);
- if ($inst_name === $verif) {
- //Remove space if is the first or last char
- while (empty(explode(' ', $inst_name)[0])) {
- $inst_name = substr($inst_name, 1);
- }
- while (empty(explode(' ', $inst_name)[count(explode(' ', $inst_name)) - 1])) {
- $inst_name = substr($inst_name, 0, strlen($inst_name)-1);
- }
- $_POST['institution-name'] = $inst_name;
- return true;
- } else {
- //Name with not supported chars
- $errors["inst-name"] = "NOT SUPPORTED CHARS";
- return false;
- }
- }
- function validateInstUf($uf = null, $data = null) {
- //Disable if inst not declared
- if ($_POST['institution-checkbox'] == 'on') {
- return true;
- }
- if (empty($uf) ||
- strlen($uf) != 2 ||
- empty($data)) {
- $errors["inst-uf"] = "EMPTY";
- return false;
- }
- $uf = strtoupper($uf);
- $siglas = array();
- for ($i=0; $i < count($data); $i++) {
- array_push($siglas, $data[$i]['sigla']);
- }
- if (in_array($uf, $siglas)) {
- return true;
- } else {
- $errors["inst-uf"] = "INVALID";
- return false;
- }
- }
- function validateInstCity($inst_uf = null, $inst_city = null, $data = null) {
- //Disable if inst not declared
- if ($_POST['institution-checkbox'] == 'on') {
- return true;
- }
- if (empty($inst_uf) ||
- empty($inst_city) ||
- empty($data)) {
- $errors["inst-city"] = "EMPTY";
- return false;
- }
- $idx;
- for ($i=0; $i < count($data); $i++) {
- if ($data[$i]['sigla'] == $inst_uf) {
- $idx = $i;
- break;
- }
- }
- if (in_array($inst_city, $data[$idx]['cidades'])) {
- return true;
- } else {
- $errors["inst-city"] = "CITY NOT IN GIVEN PROVINCE";
- return false;
- }
- }
- // Checks if everything provided is valid
- function dataIsValid() {
- include "../web/assets/estados-cidades.php";
- if (allParamsPresent() &&
- validateName($_POST['name']) &&
- validateGender($_POST['gender']) &&
- validateEmail($_POST['username']) &&
- validateCpf($_POST['cpf']) &&
- validatePhone($_POST['phone']) &&
- validateBirth($_POST['birth-date']) &&
- validatePassword($_POST['password'], $_POST['confirm-password']) &&
- validateInstName($_POST['institution-name']) &&
- validateInstUf($_POST['institution-uf'], $states_json) &&
- validateInstCity($_POST['institution-uf'], $_POST['institution-city'], $states_json)) {
- return true;
- } else {
- return false;
- }
- }
- //CAPTCHA HANDLING AND DB QUERY
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- function post_captcha($user_response) {
- $fields_string = '';
- $fields = array(
- 'secret' => "6Ld4aZkUAAAAAOkQjEEEDXz6bW6oj8Nb7CI_dtrI",
- 'response' => $user_response
- );
- foreach($fields as $key=>$value)
- $fields_string .= $key . '=' . $value . '&';
- $fields_string = rtrim($fields_string, '&');
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
- curl_setopt($ch, CURLOPT_POST, count($fields));
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
- $result = curl_exec($ch);
- curl_close($ch);
- return json_decode($result, true);
- }
- $res = post_captcha($_POST['g-recaptcha-response']);
- if (!$res['success']) {
- // FAILURE
- $_POST = array();
- echo '<script>showError(\'reCAPTCHA INVÁLIDO\')</script>';
- } else {
- // SUCCESS
- if (dataIsValid()) {
- //MAKE DB QUERY
- require_once "../../configs/db.php";
- $name = $_POST['name'];
- $gender = $_POST['gender'];
- $email = $_POST['username'];
- $cpf = $_POST['cpf'];
- $phone = $_POST['phone'];
- $birth = $_POST['birth-date'];
- $password = hash('sha256', $_POST['password']);
- $inst_name = $_POST['institution-name'];
- $inst_uf = $_POST['institution-uf'];
- $inst_city = $_POST['institution-city'];
- //Save Data
- $insert = "INSERT INTO users (id, name, gender, email, cpf, phone, birth, password, inst_name, inst_uf, inst_city) ";
- $values = "VALUES (NULL, \"$name\", \"$gender\", \"$email\", \"$cpf\", \"$phone\", \"$birth\", \"$password\", \"$inst_name\", \"$inst_uf\", \"$inst_city\")";
- $sql = $insert . $values;
- $result = $connection->query($sql);
- if ($result) {
- header("Location: entrar.php");
- exit;
- } else {
- $error = mysqli_error($connection);
- $msg;
- if(preg_match("/unique_email/i", $error)) {
- $msg = "EMAIL JÁ ESTÁ EM USO";
- }
- if(preg_match("/unique_phone/i", $error)) {
- $msg = "TELEFONE JÁ ESTÁ EM USO";
- }
- if(preg_match("/unique_cpf/i", $error)) {
- $msg = "CPF JÁ ESTÁ EM USO";
- }
- if(preg_match("/unique_id/i", $error)) {
- $msg = "ENTRE EM CONTATO CONOSCO! - ERROR UNIQUE_ID";
- }
- echo "<script>showError('$msg')</script>";
- }
- } else {
- //Captach is ok but data was not verified
- print_r($errors);
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="pt-br">
- <head>
- <meta charset="utf-8">
- <link rel="stylesheet" href="<?echo $sing_up_css;?>">
- <link href="https://fonts.googleapis.com/css?family=Work+Sans:300,400" rel="stylesheet">
- <script src="https://www.google.com/recaptcha/api.js?hl=pt-BR" async defer></script>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <!--<script src="<?//echo $sign_up_js;?>" defer></script>-->
- <script src="/js/sign-up.js" defer></script>
- <title>iStudiWeb | Cadastrar</title>
- </head>
- <body>
- <div id="wraper">
- <img id="logo" alt="iStudiWeb Logo" src="<?echo $web_logo_black;?>">
- <form id="form" action="cadastrar" method="POST">
- <ul class="left-list ul-list">
- <li><label class="list-title">Sobre Mim</label></li>
- <li><input class="text-input" type="text" name="name" id="name" placeholder="Nome" autocomplete="off" required></input></li>
- <li>
- <select class="text-input" id="gender" name="gender" placeholder="Sexo" required>
- <option id="gender-placeholder" value="" disabled selected style="">Sexo</option>
- <option value="M">Masculino</option>
- <option value="F">Feminino</option>
- <option value="NF">Prefiro não informar</option>
- </select>
- </li>
- <li><input class="text-input" type="email" name="username" id="email" placeholder="Email" autocomplete="on" required></input></li>
- <li><input class="text-input" type="text" name="cpf" id="cpf" placeholder="CPF" autocomplete="off" required></input></li>
- <li><input class="text-input" type="tel" name="phone" id="phone" placeholder="Celular" autocomplete="on" required></input></li>
- <li><input class="text-input" type="text" name="birth-date" id="birth-date" placeholder="Data de Nascimento" autocomplete="off" required></input></li>
- <li><input class="text-input" type="password" name="password" id="password" placeholder="Senha" autocomplete="off" required></input></li>
- <li><input class="text-input" type="password" name="confirm-password" id="confirm-password" placeholder="Confirme sua senha" autocomplete="off" required></input></li>
- </ul>
- <hr class="separator">
- <ul class="right-list ul-list">
- <li><label class="list-title">Minha Instituição</label></li>
- <li><label id="institution-label"><input class="check-input" type="checkbox" name="institution-checkbox" id="institution-checkbox" value="on"></input>Não estou em nenhuma Instituição</label></li>
- <li><input class="text-input" type="text" name="institution-name" id="institution-name" placeholder="Nome" autocomplete="off" required></input></li>
- <li>
- <input class="text-input" name="institution-uf" id="institution-uf" placeholder="UF" list="inst-uf-list" required>
- <datalist id="inst-uf-list"></datalist>
- </li>
- <li>
- <input class="text-input" name="institution-city" id="institution-city" list="inst-city-list" placeholder="Cidade" required readonly>
- <datalist id="inst-city-list"></datalist>
- </li>
- </ul>
- <hr class="separator">
- <div id="submit-wrap">
- <div class="g-recaptcha" id="reCaptcha" data-callback="recaptchaCallback" data-sitekey="6Ld4aZkUAAAAANscwRBTTGhaEIWvwj0gm3BY4BNQ"></div>
- <input id="cadastrar-btn" type="image" alt="Criar Conta" src=<?echo $sing_up_btn;?> disabled><br>
- <span class="legal-agreement">Ao se cadastrar, você concorda com os nossos</span><br>
- <span class="legal-agreement"><a href="#" class="legal-agreement">Termos de Uso</a> e <a href="#" class="legal-agreement">Política de Privacidade</a></span>
- </div>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement