<?php

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

if(isset($_POST['submit']))
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$gender = $_POST['gender'];
$dob = $_POST['dob'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$postcode = $_POST['postcode'];
$country = $_POST['country'];
$idtype = $_POST['identification'];
$idno = $_POST['idno'];
$expiry = $_POST['expiry'];
$word = $_POST['pass'];
$pass = md5($_POST['pass']);
$pin = $_POST['pin'];
$code1 = $_POST['code1'];
$code2 = $_POST['code2'];
$code3 = $_POST['code3'];
$active = $_POST['status'];
$type = $_POST['type'];
$currency = $_POST['currency'];
$user_id = $_POST['user_id'];
$user = $_POST['user'];


dbconnect();
$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_pwd = :pass, user_word = :word, user_pin = :pin, user_code1 = :code1, user_code2 = :code2, user_code3 = :code3, user_active = :active, currency = :currency, acc_type = :type, first_name = :fname, last_name = :lname, user_email = :email, user_phone = :phone, user_gender = :gender, dob = :dob, user_add = :address, user_city = :city, user_state = :state, postcode = :postcode, country = :country, id_type = :idtype, id_number = :idno, id_expiry = :expiry WHERE user_id = :userid LIMIT 1');

$stmt->execute(array(':pass' => $pass, ':word' => $word, ':pin' => $pin, ':code1' => $code1, ':code2' => $code2, ':code3' => $code3, ':active' => $active, ':currency' => $currency, ':type' => $type, ':userid' => $user_id, ':fname' => $fname, ':lname' => $lname, ':email' => $email, ':phone' => $phone, ':gender' => $gender, ':dob' => $dob, ':address' => $address, ':city' => $city, ':state' => $state, ':postcode' => $postcode, ':country' => $country, ':idtype' => $idtype, ':idno' => $idno, ':expiry' => $expiry));


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))
{ $msg = "<span class='alert alert-danger'>Invalid photo File Format.</span>";}

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

if($size<(1024*1024)) // Image size max 1 MB
{ $msg = "<span class='alert alert-danger'>Photo Image Size Exceeded 1 MB.</span>";}

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;
$newname = "$photoid".".$ext";

imagejpeg($desired_gdim,$filename,100);

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

}
dbconnect();
$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_photo = :photo  WHERE user_id = :userid LIMIT 1');

$stmt->execute(array(':photo' => $newname, ':userid' => $user_id));
}
log_info($_SERVER['REMOTE_ADDR'], $_SESSION['admin_login'], $_POST['user'], 'Account Successfully Updated');
redirect('edit.php?user='.$user_id.'&id='.$user.'&msg=' . urlencode('Account Successfully Updated'));		

}

?>