<?
include "header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
  if($admin_info[demo_feature] == "on") {
  $demo_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$admin_info[demo_email]' AND password='$admin_info[demo_password]'"));
  $ext_list = explode(",", trim($demo_info[ext_list]));
  } else {
  $demo_info[space_avail] = "";
  $demo_info[bandwidth_allotted] = "";
  $demo_info[folders_avail] = "";
  $demo_info[max_filesize] = "";
  $ext_list = Array();
  }

if($task == "update_settings") {
$demo_feature = $_POST['demo_feature'];
$demo_email = $_POST['demo_email'];
$demo_password = $_POST['demo_password'];
$demo_autofill = $_POST['demo_autofill'];

if($demo_feature == "off") {
  if($admin_info[demo_feature] == "on") {
  if(str_replace(" ", "", $demo_info[u_id]) != "") { @deleteuse("../u/$demo_info[u_id]"); }
  mysql_query("DELETE FROM ims WHERE u_id='$demo_info[u_id]'");
  mysql_query("DELETE FROM ims_folders WHERE u_id='$demo_info[u_id]'");
  mysql_query("DELETE FROM ims_image WHERE u_id='$demo_info[u_id]'");
  mysql_query("UPDATE ims_admin SET demo_feature='off', demo_email='', demo_password='', demo_autofill=''");
  }
}

if($demo_feature == "on") {
  $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($demo_email == "" | $demo_password == "" | $space_avail == "" | $folders_avail == "" | $bandwidth_allotted == "" | $max_filesize == 0) {
    $is_error = "yes";
    $error .= "You have omitted the following fields:<br>";
    if($demo_email == "") { $error .= "- Email<br>"; }
    if($demo_password == "") { $error .= "- Password<br>"; }
    if($space_avail == "") { $error .= "- Space Available<br>"; }
    if($folders_avail == "") { $error .= "- Folders Available<br>"; }
    if($bandwidth_allotted == "") { $error .= "- Bandwidth Allotted<br>"; }
    if($max_filesize == 0) { $error .= "- Max Filesize<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", $demo_email)) {
  $is_error = "yes";
  $error .= "Please make sure you have provided a valid email address.<br>";
  }
  if($admin_info[demo_feature] == "off") {
    $duplicates = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE email='$demo_email'"));
    if($duplicates != 0) {
    $is_error = "yes";
    $error .= "A user with the email address \"$demo_email\" already exists.<br>";
    }
  }
  if(preg_match("/[^a-z,A-Z,0-9]/", $demo_password)) {
  $is_error = "yes";
  $error .= "Passwords may only contain alpha-numeric characters.<br>";
  }
  if(!is_numeric($space_avail) | $space_avail <= 0) {
  $is_error = "yes";
  $error .= "Space 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 .= "Max. Folders must be an integer value greater than 0.<br>";
  }
  if(!is_numeric($bandwidth_allotted) | $bandwidth_allotted <= 0) {
  $is_error = "yes";
  $error .= "Bandwidth must be a numeric value greater than 0.<br>";
  }



  if($is_error == "yes") {
    echo "
    $header
    <table cellpadding='0' cellspacing='0' width='500'>
    <tr>
    <td><b>An Error Has Occurred:</b></td>
    </tr><form action='settings_demo.php' method='POST'><tr>
    <td>
    $error<br>
    <input type='submit' class='button' value='Try Again'>
    </td>
    </form>
    </tr>
    </table>
    $footer";
    exit;
  } else {
  if($admin_info[demo_feature] == "off") {
    $signup_date = time();
    $code = randomcode();
    mysql_query("UPDATE ims_admin SET demo_feature='on', demo_email='$demo_email', demo_password='$demo_password', demo_autofill='$demo_autofill'");
    mysql_query("INSERT INTO ims (email,password,code,fname,lname,account_active,reset,plan,price,signup_date,IP,last_login,ext_list,space_avail,folders_avail,max_filesize,bandwidth_allotted,photo_album,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 ('$demo_email','$demo_password','$code','Demo Customer','','yes','$signup_date','0','0.00','$signup_date','','Never','$ext_list','$space_avail','$folders_avail','$max_filesize','$bandwidth_allotted','$photo_album','yes','yes','oldest','5','yes','public','','','yes','left','500')");
    $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$demo_email' AND password='$demo_password' AND fname='Demo Customer' AND lname='' AND signup_date='$signup_date'"));
    mkdir("../u/$user_info[u_id]", 0700);
    chmod("../u/$user_info[u_id]", 0777);
  } else {
    $user_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$admin_info[demo_email]' AND password='$admin_info[demo_password]' AND fname='Demo Customer' AND lname=''"));
    mysql_query("UPDATE ims SET email='$demo_email', password='$demo_password', space_avail='$space_avail', folders_avail='$folders_avail', ext_list='$ext_list', bandwidth_allotted='$bandwidth_allotted', max_filesize='$max_filesize', photo_album='$photo_album' WHERE u_id='$user_info[u_id]'");
    mysql_query("UPDATE ims_admin SET demo_email='$demo_email', demo_password='$demo_password', demo_autofill='$demo_autofill'");
  }}
}

$r = "Your changes have been saved.";
$admin_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_admin"));
}

  if($admin_info[demo_feature] == "on") {
  $demo_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims WHERE email='$admin_info[demo_email]' AND password='$admin_info[demo_password]'"));
  $ext_list = explode(",", trim($demo_info[ext_list]));
  } else {
  $demo_info[space_avail] = "";
  $demo_info[bandwidth_allotted] = "";
  $demo_info[folders_avail] = "";
  $demo_info[max_filesize] = "";
  $ext_list = Array();
  }

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

echo $header;
echo "
<table cellpadding='0' cellspacing='0' width='518'>
<tr><td>
<b>Demonstration Settings</b><br>
Here you can choose to feature a \"demo\" mode for your IMS.
To begin, specify an email and password to be reserved for the demo user.
Logging in as the demo user will allow guests to explore the system before signing up.
When logged in as the demo user, guests will not be able to upgrade/cancel the account,
email tech support, change the account information, etc. They will, however, be able
to upload/delete images, create/delete folders, etc.
</td></tr></table>

<br>

If the feature is turned on, the following URL will allow guests to login as the demo user:<br>
<a href='$url' target='_blank'>$url</a><br><br>

";
if(isset($r)) {
echo "<b>$r</b><br><br>";
}



if($admin_info[demo_feature] == "on") {

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

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

$total_folders = mysql_num_rows(mysql_query("SELECT * FROM ims_folders WHERE u_id='$demo_info[u_id]'"));
$total_images = mysql_num_rows(mysql_query("SELECT * FROM ims_image WHERE u_id='$demo_info[u_id]'"));

echo "
<b>Demo Specs</b><br>
Space Used: $space_used MB of $demo_info[space_avail] MB<br>
Bandwidth Used: $bandwidth_used MB of $demo_info[bandwidth_allotted] MB<br>
Total Folders: $total_folders<br>
Total Images: $total_images<br><br>
";
}

echo "

<script language='JavaScript'>
<!--
function enable() {
document.demo_settings.demo_email.disabled = false;
document.demo_settings.demo_email.style.background = '#FFFFFF'
document.demo_settings.demo_password.disabled = false;
document.demo_settings.demo_password.style.background = '#FFFFFF'
document.demo_settings.space_avail.disabled = false;
document.demo_settings.space_avail.style.background = '#FFFFFF'
document.demo_settings.bandwidth_allotted.disabled = false;
document.demo_settings.bandwidth_allotted.style.background = '#FFFFFF'
document.demo_settings.folders_avail.disabled = false;
document.demo_settings.folders_avail.style.background = '#FFFFFF'
document.demo_settings.max_filesize.disabled = false;
document.demo_settings.max_filesize.style.background = '#FFFFFF'
document.demo_settings.ext_jpg.disabled = false;
document.demo_settings.ext_jpg.style.background = '#FFFFFF'
document.demo_settings.ext_jpeg.disabled = false;
document.demo_settings.ext_jpeg.style.background = '#FFFFFF'
document.demo_settings.ext_gif.disabled = false;
document.demo_settings.ext_gif.style.background = '#FFFFFF'
document.demo_settings.ext_bmp.disabled = false;
document.demo_settings.ext_bmp.style.background = '#FFFFFF'
document.demo_settings.ext_png.disabled = false;
document.demo_settings.ext_png.style.background = '#FFFFFF'
document.demo_settings.ext_pdf.disabled = false;
document.demo_settings.ext_pdf.style.background = '#FFFFFF'
document.demo_settings.demo_autofill.disabled = false;
document.demo_settings.demo_autofill.style.background = '#FFFFFF'
document.demo_settings.photo_album.disabled = false;
document.demo_settings.photo_album.style.background = '#FFFFFF'
}
function disable() {
document.demo_settings.demo_email.disabled = true;
document.demo_settings.demo_email.style.background = '#EEEEEE'
document.demo_settings.demo_password.disabled = true;
document.demo_settings.demo_password.style.background = '#EEEEEE'
document.demo_settings.space_avail.disabled = true;
document.demo_settings.space_avail.style.background = '#EEEEEE'
document.demo_settings.bandwidth_allotted.disabled = true;
document.demo_settings.bandwidth_allotted.style.background = '#EEEEEE'
document.demo_settings.folders_avail.disabled = true;
document.demo_settings.folders_avail.style.background = '#EEEEEE'
document.demo_settings.max_filesize.disabled = true;
document.demo_settings.max_filesize.style.background = '#EEEEEE'
document.demo_settings.ext_jpg.disabled = true;
document.demo_settings.ext_jpg.style.background = '#EEEEEE'
document.demo_settings.ext_jpeg.disabled = true;
document.demo_settings.ext_jpeg.style.background = '#EEEEEE'
document.demo_settings.ext_gif.disabled = true;
document.demo_settings.ext_gif.style.background = '#EEEEEE'
document.demo_settings.ext_bmp.disabled = true;
document.demo_settings.ext_bmp.style.background = '#EEEEEE'
document.demo_settings.ext_png.disabled = true;
document.demo_settings.ext_png.style.background = '#EEEEEE'
document.demo_settings.ext_pdf.disabled = true;
document.demo_settings.ext_pdf.style.background = '#EEEEEE'
document.demo_settings.demo_autofill.disabled = true;
document.demo_settings.demo_autofill.style.background = '#EEEEEE'
document.demo_settings.photo_album.disabled = true;
document.demo_settings.photo_album.style.background = '#EEEEEE'
}
// -->
</script>


<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<form action='settings_demo.php' method='POST' name='demo_settings'>
<tr><td width='502' style='padding: 5px; padding-left: 7px;'>
Turn the demonstration feature on or off?
<table cellpadding='0' cellspacing='0' style='margin-top: 2px;'>
<tr><td>
<input type='radio' name='demo_feature' value='on' onClick=\"enable()\""; if($admin_info[demo_feature] == "on") { echo " CHECKED"; } echo "></td><td style='padding-bottom: 2px;'>&nbsp;<b>ON</b></td><td>&nbsp;&nbsp;
<input type='radio' name='demo_feature' value='off' onClick=\"disable()\""; if($admin_info[demo_feature] == "off") { echo " CHECKED"; } echo "></td><td style='padding-bottom: 2px;'>&nbsp;<b>OFF</b></td>
</tr></table>
</td>
</tr>
</table>

<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<tr><td width='502' style='padding: 5px; padding-left: 7px;'>
<table cellpadding='0' cellspacing='0'><tr><td valign='top'>
Demonstration Email (username)<br>
<input type='text' size='30' name='demo_email' value='$admin_info[demo_email]' maxlength='150'"; if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">&nbsp;&nbsp;
</td>
<td valign='top'>
Password<br>
<input type='text' name='demo_password' value='$admin_info[demo_password]' maxlength='150'"; if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">
</td></tr></table>
<table cellpadding='0' cellspacing='0' style='margin-top: 2px;'>
<tr>
<td><input type='checkbox' name='demo_autofill' value='yes'"; if($admin_info[demo_autofill] == "yes") { echo " CHECKED"; } if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">&nbsp;</td>
<td>Autofill email/password on demo login page?</td>
</tr></table>
</td>
</tr>
</table>


<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<tr><td width='502' style='padding: 5px; padding-left: 7px;'>
Space<br>
<input type='text' name='space_avail' size='7' maxlength='6' value='$demo_info[space_avail]'"; if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">&nbsp; Space in megabytes (e.g. 25)
</td></tr></table>

<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<tr><td width='502' style='padding: 5px; padding-left: 7px;'>
Bandwidth<br>
<input type='text' name='bandwidth_allotted' size='7' maxlength='7' value='$demo_info[bandwidth_allotted]'"; if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">&nbsp; Bandwidth in megabytes (e.g. 800)
</td></tr></table>

<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<tr><td width='502' style='padding: 5px; padding-left: 7px;'>
Max. Folders<br>
<input type='text' name='folders_avail' maxlength='3' size='7' value='$demo_info[folders_avail]'"; if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">&nbsp; Maximum number of image folders (e.g. 10)
</td></tr></table>

<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<tr><td width='502' style='padding: 5px; padding-left: 7px;'>
Max. File Size<br>
<select name='max_filesize'"; if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">
<option value='0'"; if($demo_info[max_filesize] == '0') { echo " SELECTED"; } echo "></option>
<option value='10'"; if($demo_info[max_filesize] == '10') { echo " SELECTED"; } echo ">10 KB</option>
<option value='25'"; if($demo_info[max_filesize] == '25') { echo " SELECTED"; } echo ">25 KB</option>
<option value='50'"; if($demo_info[max_filesize] == '50') { echo " SELECTED"; } echo ">50 KB</option>
<option value='100'"; if($demo_info[max_filesize] == '100') { echo " SELECTED"; } echo ">100 KB</option>
<option value='200'"; if($demo_info[max_filesize] == '200') { echo " SELECTED"; } echo ">200 KB</option>
<option value='300'"; if($demo_info[max_filesize] == '300') { echo " SELECTED"; } echo ">300 KB</option>
<option value='400'"; if($demo_info[max_filesize] == '400') { echo " SELECTED"; } echo ">400 KB</option>
<option value='500'"; if($demo_info[max_filesize] == '500') { echo " SELECTED"; } echo ">500 KB</option>
<option value='600'"; if($demo_info[max_filesize] == '600') { echo " SELECTED"; } echo ">600 KB</option>
<option value='700'"; if($demo_info[max_filesize] == '700') { echo " SELECTED"; } echo ">700 KB</option>
<option value='800'"; if($demo_info[max_filesize] == '800') { echo " SELECTED"; } echo ">800 KB</option>
<option value='900'"; if($demo_info[max_filesize] == '900') { echo " SELECTED"; } echo ">900 KB</option>
<option value='1024'"; if($demo_info[max_filesize] == '1024') { echo " SELECTED"; } echo ">1 MB</option>
</select> &nbsp;Maximum file size for each uploaded image. Larger sizes = slower uploads.
</td></tr></table>

<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<tr><td width='502' style='padding: 5px; padding-left: 7px;'>
File Extentions<br>
<input type='checkbox' name='ext_jpg' value='yes'"; if(in_array("jpg", $ext_list)) { echo " CHECKED"; } if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">JPG &nbsp;
<input type='checkbox' name='ext_jpeg' value='yes'"; if(in_array("jpeg", $ext_list)) { echo " CHECKED"; } if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">JPEG &nbsp;
<input type='checkbox' name='ext_gif' value='yes'"; if(in_array("gif", $ext_list)) { echo " CHECKED"; } if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">GIF &nbsp;
<input type='checkbox' name='ext_bmp' value='yes'"; if(in_array("bmp", $ext_list)) { echo " CHECKED"; } if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">BMP &nbsp;
<input type='checkbox' name='ext_png' value='yes'"; if(in_array("png", $ext_list)) { echo " CHECKED"; } if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">PNG &nbsp;
<input type='checkbox' name='ext_pdf' value='yes'"; if(in_array("pdf", $ext_list)) { echo " CHECKED"; } if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo ">PDF &nbsp;
</td></tr></table>

<table cellpadding='0' cellspacing='0' style='border: 1px solid #AAAAAA; background: #FFFFFF; margin-bottom: 10px;'>
<tr><td width='502' style='padding: 5px; padding-left: 7px;'>
Photo Album<br>
<input type='checkbox' name='photo_album' value='yes'"; if($demo_info[photo_album] == "yes") { echo " CHECKED"; } if($admin_info[demo_feature] == "off") { echo " DISABLED style='background: #EEEEEE;'"; } echo "> Turn on demo photo album?
</td></tr></table>

<table cellpadding='0' cellspacing='0'>
<tr><td>
<input type='submit' class='button' value='Save Changes'>
<input type='hidden' name='task' value='update_settings'>
</td>
</form>
</tr>
</table>
";
echo $footer;
?>