<?
include "header.php";
if(isset($_POST['user_task'])) { $user_task = $_POST['user_task']; } elseif(isset($_GET['user_task'])) { $user_task = $_GET['user_task']; } else { $user_task = "main"; }
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
if(isset($_POST['page'])) { $page = $_POST['page']; } elseif(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = 1; }
if(isset($_POST['s'])) { $s = $_POST['s']; } elseif(isset($_GET['s'])) { $s = $_GET['s']; } else { $s = ""; }
if(isset($_POST['o'])) { $o = $_POST['o']; } elseif(isset($_GET['o'])) { $o = $_GET['o']; } else { $o = "u"; }
if(isset($_POST['t'])) { $t = $_POST['t']; } elseif(isset($_GET['t'])) { $t = $_GET['t']; } else { $t = "e"; }




$header = "
<html>
<head>
<title>Image Management System - Administration Panel</title>
<META HTTP-EQUIV='Pragma' CONTENT='no-cache'>
<link rel='STYLESHEET' type='text/css' href='../styles.css'>
<script language='javascript'>
<!--
function redirect() {
window.opener.location.href('users.php?task=main&page=1&s=&o=u&t=e');
window.close();
}
// -->
</script>
<style type='text/css'>
body { margin-left: 10px; margin-top: 10px; }
</style>
</head>
<body>

<font style='font-size: 13pt;'><b>Add New User</b></font><br>
";

$footer = "
</body>
</html>
";




if($user_task == "add_user") {
$email = $_POST['email'];
$password = $_POST['password'];
$fname = str_replace("'", "&#39;", $_POST['fname']);
$lname = str_replace("'", "&#39;", $_POST['lname']);
$account_active = $_POST['account_active'];
$notes = $_POST['notes'];
$plan_type = $_POST['plan_type'];
$plan = $_POST['plan'];
$price = str_replace("$", "", $_POST['price']);
$space_avail = $_POST['space_avail'];
$folders_avail = $_POST['folders_avail'];
$bandwidth_allotted = $_POST['bandwidth_allotted'];
$max_filesize = $_POST['max_filesize'];
$photo_album = $_POST['photo_album'];

$ext_list = "";
$lastext = "no";
if(isset($_POST['ext_jpg'])) { $ext_list = "jpg"; $lastext = "yes"; }
if($lastext == "yes") { $ext_list .= ","; $lastext = "no"; }
if(isset($_POST['ext_jpeg'])) { $ext_list .= "jpeg"; $lastext = "yes"; }
if($lastext == "yes") { $ext_list .= ","; $lastext = "no"; }
if(isset($_POST['ext_gif'])) { $ext_list .= "gif"; $lastext = "yes"; }
if($lastext == "yes") { $ext_list .= ","; $lastext = "no"; }
if(isset($_POST['ext_bmp'])) { $ext_list .= "bmp"; $lastext = "yes"; }
if($lastext == "yes") { $ext_list .= ","; $lastext = "no"; }
if(isset($_POST['ext_png'])) { $ext_list .= "png"; $lastext = "yes"; }
if($lastext == "yes") { $ext_list .= ","; $lastext = "no"; }
if(isset($_POST['ext_pdf'])) { $ext_list .= "pdf"; $lastext = "yes"; }

$is_error = "no";
$error = "";

if($email == "" | $password == "" | $fname == "" | $lname == "" | $plan_type == "" | ($plan_type == "custom" & ($price == "" | $space_avail == "" | $folders_avail == "" | $bandwidth_allotted == ""))) {
  $is_error = "yes";
  $error .= "You have omitted the following fields:<br>";
  if($email == "") { $error .= "- Email<br>"; }
  if($password == "") { $error .= "- Password<br>"; }
  if($fname == "") { $error .= "- First Name<br>"; }
  if($lname == "") { $error .= "- Last Name<br>"; }
  if($plan_type == "") { $error .= "- Plan Type<br>"; }
  if($plan_type == "custom") {
    if($price == "") { $error .= "- Price<br>"; }
    if($space_avail == "") { $error .= "- Space Available<br>"; }
    if($folders_avail == "") { $error .= "- Folders Available<br>"; }
    if($bandwidth_allotted == "") { $error .= "- Bandwidth Allotted<br>"; }
  }
  $error .= "<br>";
}
if(preg_match("/[^a-z,A-Z,0-9]/", $password)) {
$is_error = "yes";
$error .= "Passwords may only contain alpha-numeric characters.<br>";
}
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", $email)) {
$is_error = "yes";
$error .= "Please make sure you have provided a valid email address.<br>";
}
if($plan_type == "custom") {
  if(!is_numeric($price)) {
  $is_error = "yes";
  $error .= "Price must be a numeric value.<br>";
  }
  if(!is_numeric($space_avail) | $space_avail <= 0) {
  $is_error = "yes";
  $error .= "Space Available must be a numeric value greater than 0.<br>";
  }
  if((!is_numeric($folders_avail) | $folders_avail != (int)$folders_avail | $folders_avail <= 0) & $folders_avail != "") {
  $is_error = "yes";
  $error .= "Folders Available must be an integer value greater than 0.<br>";
  }
  if(!is_numeric($bandwidth_allotted) | $bandwidth_allotted <= 0) {
  $is_error = "yes";
  $error .= "Allotted Bandwidth must be a numeric value greater than 0.<br>";
  }
}
$duplicates = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE email='$email'"));
if($duplicates != 0) {
$is_error = "yes";
$error .= "A user with the email address \"$email\" already exists.<br>";
}

if($is_error == "yes") {
  echo $header;
echo "
<table cellpadding='0' cellspacing='0' style='margin-top: 5px;'>
<tr>
<td width='420' style='border: 1px solid #AAAAAA; background: #F5F5F5; padding: 10px;'>
<b>An Error Has Occurred:</b><br>
";
  echo $error;
  echo "<br><b><font face='verdana'>&#171;</font> <a href=\"javascript:history.go(-1);\">Click here to go back and try again.</a></b></td></tr></table>";
  echo $footer;
  exit;
}

if($plan_type == "plan") {
$plan_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_plans WHERE p_id='$plan'"));
$p_id = $plan_info[p_id];
$price = $plan_info[price];
$space_avail = $plan_info[allocated_space];
$folders_avail = $plan_info[allowed_folders];
$bandwidth_allotted = $plan_info[bandwidth_allotted];
$ext_list = $plan_info[ext_list];
$max_filesize = $plan_info[max_filesize];
$plan_name = $plan_info[plan];
$photo_album = $plan_info[photo_album];
} else {
$p_id = 0;
$plan_name = "Custom Plan";
}

if($price == 0) { $paypal_email = ""; } else { $paypal_email = $admin_info[paypal_email]; }
$signup_date = time();
$code = randomcode();
mysql_query("INSERT INTO ims (email,password,code,fname,lname,subscribed,account_active,reset,plan,price,signup_date,IP,last_login,ext_list,space_avail,folders_avail,max_filesize,bandwidth_allotted,photo_album,notes,paypal_email,is_approved,prefs_newwin,prefs_listby,prefs_images_per_page,albumprefs_on,albumprefs_privacy,albumprefs_password,albumprefs_logo,albumprefs_thumbs,albumprefs_align,albumprefs_width) VALUES ('$email','$password','$code','$fname','$lname','no','$account_active','$signup_date','$p_id','$price','$signup_date','','Never','$ext_list','$space_avail','$folders_avail','$max_filesize','$bandwidth_allotted','$photo_album','$notes','$paypal_email','yes','yes','oldest','5','no','public','','','no','left','450')");

$user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$email' AND password='$password' AND fname='$fname' AND lname='$lname' AND signup_date='$signup_date'"));
mkdir("../u/$user_info[u_id]", 0700);
chmod("../u/$user_info[u_id]", 0777);

if($price == 0) {
$text = "The new user has been successfully created.";
} else {
$text = "The new user has been successfully created. <br><br>Upon the user's first login, they will be required to create a new paypal subscription.";
}


echo $header;
echo "
<table cellpadding='0' cellspacing='0' style='margin-top: 5px;'>
<tr>
<td width='420' style='border: 1px solid #AAAAAA; background: #F5F5F5; padding: 10px;'>
<center>
$text<br><br>
<input class='button' type='submit' value='Close Window' onClick=\"redirect()\">
</center>
</td></tr></table>
";
echo $footer;
exit;

}




  
$plans = mysql_query("SELECT * FROM ims_plans WHERE status='on'");

echo $header;

echo "

<table cellpadding='0' cellspacing='0' style='margin-top: 5px;'>
<form action='users_add.php' method='POST'>
<tr>
<td width='420' style='border: 1px solid #AAAAAA; background: #F5F5F5; padding: 10px;'>
 <table cellpadding='0' cellspacing='0'>
 <tr><td width='80' align='right' NOWRAP>Email</td><td>&nbsp;&nbsp;<input type='text' name='email' maxlength='200' size='30'></td></tr>
 <tr><td align='right' NOWRAP>Password</td><td>&nbsp;&nbsp;<input type='text' name='password' maxlength='100' size='30'></td></tr>
 <tr><td align='right' NOWRAP>First Name</td><td>&nbsp;&nbsp;<input type='text' name='fname' maxlength='255' size='30'></td></tr>
 <tr><td align='right' NOWRAP>Last Name</td><td>&nbsp;&nbsp;<input type='text' name='lname' maxlength='255' size='30'></td></tr>
 <tr><td align='right' NOWRAP style='padding-top: 4px; padding-bottom: 4px;'>Account Status</td><td>&nbsp;&nbsp;<input type='radio' name='account_active' value='yes' CHECKED> Active&nbsp;<input type='radio' name='account_active' value='no'> Disabled</td></tr>
 <tr><td align='right' valign='top'>Notes</td><td>&nbsp;&nbsp;<textarea rows='3' cols='50' name='notes'></textarea></td></tr>
 </table>
</td>
</tr>
</table>

<br>


";
if(mysql_num_rows($plans) != 0) {
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td width='420' style='border: 1px solid #AAAAAA; background: #F5F5F5; padding: 10px;'>
<table cellpadding='0' cellspacing='0'>
<tr>
<td>
<input type='radio' name='plan_type' value='plan' CHECKED>
</td>
<td NOWRAP style='padding-bottom: 1px;'>
&nbsp;<b>Use Pricing Plan?</b>&nbsp;&nbsp;
</td>
<td>
<select name='plan'>";
      while($plan_info = mysql_fetch_assoc($plans)) {
      echo "<option value='$plan_info[p_id]'>$plan_info[plan]</option>";
      }
echo "
</select>
</td>
</tr>
</table>
<table cellpadding='0' cellspacing='0'>
<tr>
<td><input type='radio' name='plan_type' value='custom'></td>
<td NOWRAP style='padding-bottom: 1px;'>&nbsp;<b>Use Custom Settings?</b>
(if selected, please fill out details below)
</td>
</tr>
</table>
</td>
</tr>
</table>
";
} else {
echo "
<table cellpadding='0' cellspacing='0'>
<tr>
<td width='420' style='border: 1px solid #AAAAAA; background: #F5F5F5; padding: 10px;'>
<table cellpadding='0' cellspacing='0'>
<tr>
<td><input type='radio' name='disabled' DISABLED></td>
<td NOWRAP style='padding-bottom: 1px;'>&nbsp;You have no activated pricing plans. Please use custom settings.</td>
</tr>
</table>
<table cellpadding='0' cellspacing='0'>
<tr>
<td><input type='radio' name='plan_type' value='custom' CHECKED></td>
<td NOWRAP style='padding-bottom: 1px;'>&nbsp;<b>Use Custom Settings?</b>
(if selected, please fill out details below)
</td>
</tr>
</table>
</td>
</tr>
</table>
";
}


echo "

<br>

<table cellpadding='0' cellspacing='0'>
<tr>
<td width='420' style='border: 1px solid #AAAAAA; background: #F5F5F5; padding: 10px;'>
<table cellpadding='0' cellspacing='0' style='margin-bottom: 5px;'>
<tr><td width='80' align='right' NOWRAP>Monthly Price</td><td>&nbsp;&nbsp;<input type='text' name='price' size='7' maxlength='9' value='$0.00'>&nbsp; Price in dollars (e.g. $10.95)</td></tr>
<tr><td align='right' NOWRAP>Space</td><td>&nbsp;&nbsp;<input type='text' name='space_avail' size='7' maxlength='6' value='0'>&nbsp; Space in megabytes (e.g. 25)</td></tr>
<tr><td align='right' NOWRAP>Bandwidth</td><td>&nbsp;&nbsp;<input type='text' name='bandwidth_allotted' size='7' maxlength='7' value='0'>&nbsp; Bandwidth in megabytes (e.g. 800)</td></tr>
<tr><td align='right' NOWRAP>Max. Folders</td><td>&nbsp;&nbsp;<input type='text' name='folders_avail' maxlength='3' size='7' value='0'>&nbsp; Maximum number of image folders (e.g. 10)</td></tr>
<tr><td align='right' NOWRAP>Max. File Size</td><td>&nbsp;
<select name='max_filesize'>
<option value='10'>10 KB</option>
<option value='25'>25 KB</option>
<option value='50'>50 KB</option>
<option value='100'>100 KB</option>
<option value='200'>200 KB</option>
<option value='300' SELECTED>300 KB</option>
<option value='400'>400 KB</option>
<option value='500'>500 KB</option>
<option value='600'>600 KB</option>
<option value='700'>700 KB</option>
<option value='800'>800 KB</option>
<option value='900'>900 KB</option>
<option value='1024'>1 MB</option>
</select>
</td></tr>
<tr><td align='right' NOWRAP>File Extentions</td>
<td style='padding-left: 2px; padding-top: 2px;'>
<input type='checkbox' name='ext_jpg' value='yes' CHECKED>JPG &nbsp;
<input type='checkbox' name='ext_jpeg' value='yes' CHECKED>JPEG &nbsp;
<input type='checkbox' name='ext_gif' value='yes' CHECKED>GIF &nbsp;
<input type='checkbox' name='ext_bmp' value='yes' CHECKED>BMP &nbsp;
<input type='checkbox' name='ext_png' value='yes' CHECKED>PNG &nbsp;
<input type='checkbox' name='ext_pdf' value='yes' CHECKED>PDF &nbsp;
</td></tr>
<tr><td align='right' NOWRAP>Photo Album</td>
<td style='padding-left: 2px; padding-top: 2px;'>
<input type='checkbox' name='photo_album' value='yes'>
</td></tr>
</table>
</td>
</tr>
</table>

<br>

<table cellpadding='0' cellspacing='0'>
<tr>
<td>
<input class='button' type='submit' value='Create User'>&nbsp;
<input class='button' type='submit' value='Cancel' onClick=\"window.close()\">
</td>
</tr>
<input type='hidden' name='user_task' value='add_user'>
<input type='hidden' name='task' value='$task'>
<input type='hidden' name='page' value='$page'>
<input type='hidden' name='s' value='$s'>
<input type='hidden' name='o' value='$o'>
<input type='hidden' name='t' value='$t'>
</form>
</table>
";

echo $footer;


?>