<?
include "header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
$result = "";
$is_error = "no";


if($task == "email") {
$new_email = $_POST['new_email'];
$new_email_id = randomcode(10);
$new_email_time = time();

  if(str_replace(" ", "", $new_email) == "") {
  $is_error = "yes";
  $error = "You must enter a new email address.";
  }
  if(!preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $new_email)) {
  $is_error = "yes";
  $error = "Please make sure you have provided a valid email address.";
  }
  $duplicates = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE email='$new_email'"));
  if($duplicates != 0) {
  $is_error = "yes";
    if($new_email == $user_info[email]) { 
    $error = "You are already using the email address you provided. Please enter a new email address.";
    } else { 
    $error = "A user with the email address \"$new_email\" already exists in our database.";
    }
  }

  if($is_error == "yes") {
  echo $header;
  echo "
  <table cellpadding='0' cellspacing='0' width='520'>
  <form action='account_info.php' method='POST'>
  <tr>
  <td>
  <b>An Error Has Occurred:</b><br>
  $error<br><br>
  <input type='submit' class='button' value='Try Again'>
  </td>
  </tr>
  </form>
  </table>
  ";
  echo $footer;
  exit;
  }

mysql_query("UPDATE ims SET new_email='$new_email',new_email_id='$new_email_id',new_email_time='$new_email_time' WHERE u_id='$user_info[u_id]'");

$server_array = explode("/", $_SERVER['PHP_SELF']);
$server_array_mod = array_pop($server_array);
$server_info = implode("/", $server_array);
$server = "http://".$_SERVER['HTTP_HOST'].$server_info;

$subject = "New Email Request Confirmation";
$message = "Dear $user_info[fname] $user_info[lname],
A request has been placed to change your email address. Follow the link below within 24 hours to confirm this request. If you do not wish to confirm this request, ignore this email.
$server/change_email.php?u=$new_email_id&u_id=$user_info[u_id]
Thank You.";

mail($new_email, $subject, $message, "From: $admin_info[site_name] <$admin_info[email]>");

$result = "<br>A confirmation email has been sent to the new address. Follow the link inside within 24 hours to confirm your new email address. If your new email address is not confirmed within 24 hours, the request will be ignored.<br>";
}



if($task == "name") {
$fname = str_replace("&#39;", "'", $_POST['fname']);
$lname = str_replace("&#39;", "'", $_POST['lname']);


  if(strpos($fname, "<") | strpos($fname, ">") | strpos($lname, "<") | strpos($lname, ">")) {
  $is_error = "yes";
  $error = "The name you have provided contains invalid characters.";
  }
  if(str_replace(" ", "", $fname) == "" | str_replace(" ", "", $lname) == "") {
  $is_error = "yes";
  $error = "You must enter your full name.";
  }

  if($is_error == "yes") {
  echo $header;
  echo "
  <table cellpadding='0' cellspacing='0' width='520'>
  <form action='account_info.php' method='POST'>
  <tr>
  <td>
  <b>An Error Has Occurred:</b><br>
  $error<br><br>
  <input type='submit' class='button' value='Try Again'>
  </td>
  </tr>
  </form>
  </table>
  ";
  echo $footer;
  exit;
  }

mysql_query("UPDATE ims SET fname='$fname', lname='$lname' WHERE u_id='$user_info[u_id]'");
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$user_info[u_id]'"));
$result = "<br>Your changes have been saved.<br>";
}




if($task == "password") {
$old_pass = $_POST['old_pass'];
$new_pass = $_POST['new_pass'];
$new_pass2 = $_POST['new_pass2'];


  if($old_pass != $user_info[password]) {
  $is_error = "yes";
  $error = "Your old password is incorrect.";
  }
  if(str_replace(" ", "", $new_pass) == "") {
  $is_error = "yes";
  $error = "You must enter a new password.";
  }
  if(preg_match("/[^a-z,A-Z,0-9]/", $new_pass)) {
  $is_error = "yes";
  $error = "Passwords may only contain alpha-numeric characters.";
  }
  if($new_pass != $new_pass2) {
  $is_error = "yes";
  $error = "Your password and password confirmation fields do not match.";
  }
  if(strlen($new_pass) < 4) { 
  $is_error = "yes";
  $error = "Your password must be 4 or more characters in length.";
  } 

  if($is_error == "yes") {
  echo $header;
  echo "
  <table cellpadding='0' cellspacing='0' width='520'>
  <form action='account_info.php' method='POST'>
  <tr>
  <td>
  <b>An Error Has Occurred:</b><br>
  $error<br><br>
  <input type='submit' class='button' value='Try Again'>
  </td>
  </tr>
  </form>
  </table>
  ";
  echo $footer;
  exit;
  }

$password = u_encrypt($new_pass);
setcookie("ims_password", "$password", 0, "/");
mysql_query("UPDATE ims SET password='$new_pass' WHERE u_id='$user_info[u_id]'");
$result = "<br>Your changes have been saved.<br>";
}


if(date("n", time()) == 2 | date("n", time()) == 4 | date("n", time()) == 6 | date("n", time()) == 8 | date("n", time()) == 9 | date("n", time()) == 11 | date("n", time()) == 1) {
$days = 31;
} elseif(date("n", time()) == 5 | date("n", time()) == 7 | date("n", time()) == 10 | date("n", time()) == 12) {
$days = 30;
} elseif(date("n", time()) == 3) {
$days = 28;
}

$reset = $user_info[reset];
$old_daylight_saving_time = date("I", $reset);
if(date("n", $reset) == 1 | date("n", $reset) == 3 | date("n", $reset) == 5 | date("n", $reset) == 7 | date("n", $reset) == 8 | date("n", $reset) == 10 | date("n", $reset) == 12) {
$actual_days = 31;
} elseif(date("n", $reset) == 4 | date("n", $reset) == 6 | date("n", $reset) == 9 | date("n", $reset) == 11) {
$actual_days = 30;
} elseif(date("n", $reset) == 2) {
$actual_days = 28;
}

$old_reset = $reset;
$reset = $reset + $actual_days*24*60*60;
if(date("I", $old_reset) != date("I", $reset)) {
  if(date("I", $reset) == 1) {
  $reset = $reset - 60*60;
  } elseif(date("I", $reset) == 0) {
  $reset = $reset + 60*60;
  }
}

$space_used = mysql_fetch_assoc(mysql_query("SELECT sum(size) AS size FROM ims_image WHERE u_id='$user_info[u_id]'"));
if($space_used[size] == NULL) { $space_used[size] = 0; }
$space_used = rounder(($space_used[size]) / (1024 * 1024));

if($user_info[bandwidth_used] == "") { $user_info[bandwidth_used] = 0; }
$bandwidth_used = rounder(($user_info[bandwidth_used]) / (1024 * 1024));

echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr><td>
<b>Space Usage</b><br>
You are currently using $space_used MB of space out of $user_info[space_avail] MB available.<br><br>

<b>Bandwidth Usage</b><br>
You have used $bandwidth_used MB of bandwidth since ".date("F j, Y", $user_info[reset]).". 
At this time, your bandwidth limit is $user_info[bandwidth_allotted] MB. Your bandwidth
usage amount will be reset to 0 MB on ".date("F j, Y", $reset).".<br><br>

<b>Modify Account Information</b><br>
Use this area to modify your account details, including
your Name, Email Address, and Password.<br>
</td></tr>
</table>

<table width='520' cellpadding='0' cellspacing='0'>
<tr><td>
<b>$result</b><br>
</td></tr>
</table>

<table width='520' cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<form action='account_info.php' method='POST'>
<tr>
<td valign='top' style='padding: 5px;'>
<table cellpadding='0' cellspacing='0'>
<tr><td>
First Name<br>
<input type='text' size='22' class='text' name='fname' maxlength='250' value='".str_replace("'", "&#39;", $user_info[fname])."'>&nbsp;
</td>
<td valign='top'>
Last Name<br>
<input type='text' size='22' class='text'  name='lname' maxlength='250' value='".str_replace("'", "&#39;", $user_info[lname])."'>&nbsp;
</td>
<td valign='bottom' style='padding-bottom: 3px;'>
<input class='button' type='submit' value='Update'"; if($admin_info[demo_feature] == "on" & $user_info[email] == "$admin_info[demo_email]") { echo " DISABLED"; } echo ">
<input type='hidden' name='task' value='name'>
</td></tr></table>
</td>
</tr>
</form>
</table>

<table width='520' cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<form action='account_info.php' method='POST'>
<tr>
<td valign='top' style='padding: 5px;'>
<table cellpadding='0' cellspacing='0'>
<tr><td>
Email Address (Username)<br>
<input type='text' class='text' size='30' maxlength='250' name='new_email' value='$user_info[email]'>&nbsp;
</td>
<td valign='bottom' style='padding-bottom: 3px;'>
<input class='button' type='submit' value='Update'"; if($admin_info[demo_feature] == "on" & $user_info[email] == "$admin_info[demo_email]") { echo " DISABLED"; } echo ">
<input type='hidden' name='task' value='email'>
</td></tr></table>
</td>
</tr>
</form>
</table>

<table width='520' cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF;'>
<form action='account_info.php' method='POST'>
<tr>
<td valign='top' style='padding: 5px;'>
<table cellpadding='0' cellspacing='0'>
<tr><td>
Old Password<br>
<input type='password' class='text' size='14' maxlength='25' name='old_pass'>&nbsp;
</td>
<td valign='top'>
New Password<br>
<input type='password' class='text' size='14' maxlength='25' name='new_pass'>&nbsp;
</td>
<td valign='top'>
Confirm New Password<br>
<input type='password' class='text' size='14' maxlength='25' name='new_pass2'>&nbsp;
</td>
<td valign='bottom' style='padding-bottom: 3px;'>
<input class='button' type='submit' value='Update'"; if($admin_info[demo_feature] == "on" & $user_info[email] == "$admin_info[demo_email]") { echo " DISABLED"; } echo ">
<input type='hidden' name='task' value='password'>
</td></tr></table>
</td>
</tr>
</form>
</table>

<br>

<table cellpadding='0' cellspacing='0' width='520'>
<tr><td align='right'>
If you want to cancel your account, please <a href='cancel.php'>click here</a>.
</td></tr>
</table>
";
echo $footer;




?>