<?php
error_reporting(-1); // reports all errors
ini_set("display_errors", "1"); // shows all errors
ini_set("log_errors", 1);
ini_set("error_log", "register.log");
require "loadclasses.php";
$database = new Database;
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$password = $username = $email = $repeatPassword = "";
$username = $post['username'];
$password = $post['password'];
$repeatPassword = $post['repeatPassword'];
$email = $post['email'];
$activationCode = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") , 0, 1) . substr(md5(time()) , 1);
$filename = 'tmp_profile.jpg';
$filepath = 'uploads/images/' . 'tmp_profile.jpg';
$filetype = 'jpg';
$errors = array();
$fields = array(
'username' => array(
'validator' => 'validateUsername',
'message' => 'Username be between three and fourteen characters'
),
'email' => array(
'validator' => 'validateEmail',
'message' => 'invalid email',
),
'password' => array(
'validator' => 'validatePassword',
'message' => 'invalid password'
)
);
if(!Validation::validateRepeatPassword($password, $repeatPassword))
{
$errors[] = ["name" => "repeatPassword", "error" => "Passwords must match"];
}
foreach($_POST as $key => $value)
{
if(isset($fields[$key]))
{
if(!Validation::{$fields[$key]['validator']}($value))
{
$errors[] = ['name' => $key, 'error' => $fields[$key]['message']];
}
}
}
if(empty($errors))
{
try
{
$database->query('INSERT INTO username (username_id, password ,activecode, joinedDate, eMail, active, img_name,img_path, img_type)
VALUES
(:username, :password,:activationCode, NOW(), :email, 0, :filename, :filepath, :filetype)');
$database->bind(':username', $username);
$database->bind(':email', $email);
$database->bind(':password', password_hash($post['password'], PASSWORD_DEFAULT));
$database->bind(':activationCode', $activationCode);
$database->bind(':filename', $filename);
$database->bind(':filepath', $filepath);
$database->bind(':filetype', $filetype);
$database->execute();
return true;
}
catch(Exception $e)
{
$errors[] = ["name" => "username", "error" => "Username or E-mail may already be registered"];
}
}
header('Content-Type: application/json');
if (isset($success))
{
echo json_encode($success);
}
else
{
echo json_encode(["errors" => $errors]);
}
Reasonable? I think I should put
$activationCode = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") , 0, 1) . substr(md5(time()) , 1);
in the validation class or is it better there?