<?php
require_once('../config/config.php');
require_once('../foo/functions.php');
session_start();
dbconnect();

if(isset($_POST['submit']))
{

if ($_POST['surname'] == "") { redirect('index.php?error=' . urlencode('Enter your Surname'));}
elseif ($_POST['firstname'] == "") { redirect('index.php?error=' . urlencode('Enter your Firstname'));} 
elseif ($_POST['gender'] == "") { redirect('index.php?error=' . urlencode('Select your Gender'));}
elseif ($_POST['dob'] == "") { redirect('index.php?error=' . urlencode('Select your date of birth'));}
elseif ($_POST['email'] == "") { redirect('index.php?error=' . urlencode('Enter your email address'));}
elseif ($_POST['phone'] == "") { redirect('index.php?error=' . urlencode('Enter your mobile number'));}
elseif ($_POST['address'] == "") { redirect('index.php?error=' . urlencode('Enter your address'));}
elseif ($_POST['city'] == "") { redirect('index.php?error=' . urlencode('Enter your city'));}
elseif ($_POST['state'] == "") { redirect('index.php?error=' . urlencode('Enter your state'));}
elseif ($_POST['zip'] == "") { redirect('index.php?error=' . urlencode('Enter your zip code'));}
elseif ($_POST['country'] == "") { redirect('index.php?error=' . urlencode('Enter your country'));}
elseif ($_POST['identification'] == "") { redirect('index.php?error=' . urlencode('Select your ID type'));}
elseif ($_POST['idno'] == "") { redirect('index.php?error=' . urlencode('Enter your ID number'));}
elseif ($_POST['expiry'] == "") { redirect('index.php?error=' . urlencode('Select your ID expiry date'));}
elseif (empty($_SESSION['captcha']) || strtolower(trim($_REQUEST['captcha'])) != $_SESSION['captcha']) {redirect('index.php?error=' . urlencode('Invalid Word Verification'));}

else{
$sname = $_POST['surname'];
$fname = $_POST['firstname'];
$gender = $_POST['gender'];
$dob = $_POST['dob'];
$email = $_POST['email'];
//$phone = str_replace(' ', '', $_POST['phone']);
$phone = $_POST['phone'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$idtype = $_POST['identification'];
$idno = $_POST['idno'];
$expiry = $_POST['expiry'];
$status = "0";


dbconnect();
$stmt = $pdo->prepare('INSERT INTO '.USERS_TABLE_NAME.' (first_name, last_name, user_email, user_phone, user_gender, dob, user_add, user_city, user_state, postcode, country, id_type, id_number, id_expiry, user_status, user_registered) VALUES (:fname, :lname, :email, :phone, :gender, :dob, :address, :city, :state, :postcode, :country, :idtype, :idno, :expiry, :status, NOW())');

$stmt->execute(array(':fname' => $fname, ':lname' => $sname, ':email' => $email, ':phone' => $phone, ':gender' => $gender, ':dob' => $dob, ':address' => $address, ':city' => $city, ':state' => $state, ':postcode' => $zip, ':country' => $country, ':idtype' => $idtype, ':idno' => $idno, ':expiry' => $expiry, ':status' => $status));

if ($pdo->lastInsertId()) {

// Send EMAIL
//$email = get_email($_SESSION['user_acc']);
$message = "
<html>
<head>
<title>".WEB_NAME."</title>
</head>
<body>
<img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
<p>ONLINE APPLICATION SUBMITTED:<br><br>
FIRST NAME: ".$fname." <br>		
SURNAME: ".$sname." <br>
EMAIL: ".$email." <br>
PHONE NUMBER: ".$phone." <br>
GENDER: ".$gender." <br> 
DATE OF BIRTH ".$dob." <br> 
ADDRESS ".$address." <br>
CITY ".$city." <br>
STATE ".$state." <br>
POST CODE ".$zip." <br>
COUNTRY ".$country." <br>
ID TYPE ".$idtype." <br>
ID NUMBER ".$idno." <br>
ID EXPIRY ".$expiry." <br>
</p><br><br><br>
===========
<p>".WEB_NAME."</p>
</body>
</html>
";

// Send the email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$fname." ".$sname." <".$email.">" . "\r\n";
$subject = "ONLINE APPLICATION SUBMITTED";
mail(WEB_EMAIL,$subject,$message,$headers);


	

if (!empty($_FILES['photo']['tmp_name'])) {

// upload photo

$digits = rand(pow(10, 12-1), pow(10, 12)-1);
$photoid = time().$digits;
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
$name = $_FILES['photo']['name'];
$size = $_FILES['photo']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{ $error = "Invalid photo File Format.";}

$filename = stripslashes($_FILES['photo']['name']);
$extension = strtolower($ext);

if($size<(1024*1024)) // Image size max 1 MB
{ $error = "Photo Image Size Exceeded 1 MB.";}

define( 'DESIRED_IMAGE_WIDTH', 200 );
define( 'DESIRED_IMAGE_HEIGHT', 220 );

  $source_path = $_FILES[ 'photo' ][ 'tmp_name' ];

  list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );

  switch ( $source_type )
  {
    case IMAGETYPE_GIF:
      $source_gdim = imagecreatefromgif( $source_path );
      break;

    case IMAGETYPE_JPEG:
      $source_gdim = imagecreatefromjpeg( $source_path );
      break;

    case IMAGETYPE_PNG:
      $source_gdim = imagecreatefrompng( $source_path );
      break;
  }

  $source_aspect_ratio = $source_width / $source_height;
  $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;

  if ( $source_aspect_ratio > $desired_aspect_ratio )
  {
    // Triggered when source image is wider
    $temp_height = DESIRED_IMAGE_HEIGHT;
    $temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio );
  }
  else
  {
    // Triggered otherwise (i.e. source image is similar or taller)
    $temp_width = DESIRED_IMAGE_WIDTH;
    $temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio );
  }

  // Resize the image into a temporary GD image

  $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
  imagecopyresampled($temp_gdim, $source_gdim, 0, 0, 0, 0, $temp_width, $temp_height, $source_width, $source_height);

  // Copy cropped region from temporary image into the desired GD image

  $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2;
  $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2;

  $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT );
  imagecopy($desired_gdim, $temp_gdim, 0, 0, $x0, $y0, DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
  
$filename = "../users/". $photoid .".".$ext;
$cphoto = "$photoid".".$ext";

imagejpeg($desired_gdim,$filename,100);

imagedestroy($source_gdim);
imagedestroy($desired_gdim);

}
$sql = "UPDATE `".USERS_TABLE_NAME."` SET user_photo = '{$cphoto}' WHERE user_email = '{$email}' LIMIT 1";
$stmt = $pdo->query($sql);
}

redirect('index.php?msg='. urlencode('Account Successfully Created! Your Account details will be emailed upon verfication and activation. <a href='.HOME_URL.'>Click here to continue</a>'));}
} 

}

?>