<?
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "login"; }
if($task == "support_login_do") { $r = "support"; } else { $r = "login"; }
include "header.php";
if(isset($_GET['x'])) { $x = $_GET['x']; } else { $x = ""; }
if($x == "demo" & $admin_info[demo_feature] == "on" & $admin_info[demo_autofill] == "yes") { $email = "$admin_info[demo_email]"; $password = "$admin_info[demo_password]"; } else { $email = ""; $password = ""; }


if($task == "login_do") {
$email = $_POST['email'];
$password = $_POST['password'];
if(isset($_COOKIE['cookie_test'])) { $cookie_test = $_COOKIE['cookie_test']; } else { $cookie_test = ""; }
$is_error = "no";
$error = "";
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$email' AND password='$password'"));
$banned_emails = explode(",", $admin_info[ban_email]);
$banned_ip = explode(",", $admin_info[ban_ip]);

if(mysql_num_rows(mysql_query("SELECT * FROM ims WHERE email='$email' AND password='$password'")) == 0) {
$is_error = "yes";
$error = "
The login information you have provided is invalid. Please check your information and try again.<br>
<a href='forgot.php?r=login'>Forgot your password?</a></font>
<br><br>
<input type='submit' name='email' class='button' value='Try Again'>
";
} elseif($user_info[account_active] != "yes") {
$is_error = "yes";
$error = "The administrator has disabled your account.";
} elseif($user_info[is_approved] == "no") {
$is_error = "yes";
$error = "You cannot access your account because it has not yet been activated by the administrator. You will receive an email when your account has been activated.";
} elseif(($_POST['email'] != "" & in_array($_POST['email'], $banned_emails)) | ($_SERVER['REMOTE_ADDR'] != "" & in_array($_SERVER['REMOTE_ADDR'], $banned_ip))) {
$is_error = "yes";
$error = "You have been banned from using this system by the administrator.";
} elseif(isset($_POST['noscript'])) {
$is_error = "yes";
$error = "You must have javascript enabled to log in.";
} elseif($cookie_test != "cookies_enabled") {
$is_error = "yes";
$error = "You must have cookies enabled to log in.";
}

if($is_error == "yes") {
echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<form action='login.php' method='POST' name='login'>
<td>
<b>Login Failed</b><br><font class='csmall'>
$error
</td>
</tr>
</form>
</table>";
echo $footer;
exit;
}

$email = u_encrypt($user_info[email]);
$password = u_encrypt($user_info[password]);
setcookie("admk_u_id", "", 0, "/");
setcookie("ims_email", "$email", 0, "/");
setcookie("ims_password", "$password", 0, "/");
setcookie("ims_u_id", "$user_info[u_id]", 0, "/");
$current_time = time();
mysql_query("UPDATE ims SET last_login='$current_time' WHERE u_id='$user_info[u_id]'");

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;
}

if((time() - $days*24*60*60) >= $user_info[reset]) {
$reset = $user_info[reset];
$old_daylight_saving_time = date("I", $reset);
while((time() - $days*24*60*60) >= $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;
  }
}
}
mysql_query("UPDATE ims SET reset='$reset',bandwidth_used='0' WHERE u_id='$user_info[u_id]'");
$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE u_id='$user_info[u_id]'"));
}


if($user_info[first_login] == "no") {
header("Location: index.php");
} else {
header("Location: first_login.php");
}
exit;
}







setcookie("cookie_test", "cookies_enabled", 0, "/");
echo $header;
echo "
<table width='520' cellpadding='0' cellspacing='0'>
<tr>
<td>
";
if($x == "demo") { 
echo "
<b>Customer Login (Demonstration)</b><br><font class='csmall'>
Hello and welcome to the $admin_info[site_name] demonstration login page.<br>
Please click the \"Login to Account\" button to try our image hosting service.
";
} else {
echo "
<b>Account Login</b><br><font class='csmall'>
Hello and welcome to the $admin_info[site_name] account login page.<br>
Please provide your email address and password to access your image hosting account.
";
}

echo "
</font>
</td>
</tr>
</table>

<br>

<table width='520' cellpadding='0' cellspacing='0'>
<form action='login.php' method='POST' name='login'>
<tr><td valign='top'>
    <table cellpadding='0' cellspacing='0'><tr>
    <td class='nopad' valign='bottom'><b>Email Address<br><input type='text' class='text' name='email' maxlength='250' value='$email'>&nbsp;&nbsp;</td>
    <td class='nopad' valign='bottom'><b>Password</b><br><input type='password' class='text' name='password' maxlength='250' value='$password'>&nbsp;&nbsp;</td>
    <td class='nopad' valign='bottom' style='padding-bottom: 3px;'><input class='button' type='submit' value='Login to Account'></td>
    </table>
";

if($x != "demo") {
echo "
If you have lost or forgotten your password, <a href='forgot.php?r=login' title='Click here to have your password emailed to you.'>click here</a>.
";
}

echo "
</td></tr>
</tr>
<noscript><input type='hidden' name='noscript' value='yes'></noscript>
<input type='hidden' name='task' value='login_do'>
</form>
</table>";
echo $footer;

?>