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

if (!is_user()) {
	redirect('index.php');

}

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

if ($_POST['pass'] == ""){ redirect('splash.php?error=' . urlencode('Please Enter New Password'));}
elseif ($_POST['pin'] == ""){ redirect('splash.php?error=' . urlencode('Please Enter New PIN'));}
elseif ($_POST['secq'] == ""){ redirect('splash.php?error=' . urlencode('Please Enter Security Question'));}
elseif ($_POST['seca'] == ""){ redirect('splash.php?error=' . urlencode('Please Enter your answer'));}
//elseif (empty($_FILES['photo']['tmp_name'])) { redirect('splash.php?error=' . urlencode('Please select the post image'));}

else{
$pass = md5($_POST['pass']);
$pin = $_POST['pin'];
$word = $_POST['pass'];
$secq = $_POST['secq'];
$seca = $_POST['seca'];
$splash = "1";
$userid = $_SESSION['user_login'];


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_pwd = :pass, user_pin = :pin, user_photo = :photo, user_word = :word, secret_qs = :secq, secret_ans = :seca, splash = :splash WHERE user_name = :userid LIMIT 1');

$stmt->execute(array(':pass' => $pass, ':pin' => $pin, ':photo' => $newname, ':word' => $word, ':secq' => $secq, ':seca' => $seca, ':splash' => $splash, ':userid' => $userid));

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $_POST['pass'], 'Successfully updated security settings');

}else {
dbconnect();
$stmt = $pdo->prepare('UPDATE '.USERS_TABLE_NAME.' SET user_pwd = :pass, user_pin = :pin, user_word = :word, secret_qs = :secq, secret_ans = :seca, splash = :splash WHERE user_name = :userid LIMIT 1');

$stmt->execute(array(':pass' => $pass, ':pin' => $pin, ':word' => $word, ':secq' => $secq, ':seca' => $seca, ':splash' => $splash, ':userid' => $userid));

log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $_POST['pass'], 'Successfully updated security settings');	
}
redirect('dashboard.php'); 
}
}

?>