<?php

  // load viewer library
  $libraryPath = '../cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('','../','../../','../../../','../../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  //
  if (!$CURRENT_USER) { websiteLogin_redirectToLogin(); }

  // prepopulate form with current user values
  foreach ($CURRENT_USER as $name => $value) {
    if (array_key_exists($name, $_REQUEST)) { continue; }
    $_REQUEST[$name] = $value;
  }

  // process form
  if (@$_REQUEST['save']) {

    // error checking
    $errorsAndAlerts = "";
    if (!@$_REQUEST['firstname'])                { $errorsAndAlerts .= "You must enter your First name!<br/>\n"; }
    if (!@$_REQUEST['lastname'])                { $errorsAndAlerts .= "You must enter your Last name!<br/>\n"; }
    if (!@$_REQUEST['email'])                   { $errorsAndAlerts .= "You must enter your email!<br/>\n"; }
    else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }
	if (!@$_REQUEST['state'])                { $errorsAndAlerts .= "Please select a state!<br/>\n"; }
	if (!@$_REQUEST['address'])                { $errorsAndAlerts .= "Please enter your address!<br/>\n"; }
	if (!@$_REQUEST['url'])                { $errorsAndAlerts .= "Please enter your Website URL!<br/>\n"; }
	if (!@$_REQUEST['status'])                { $errorsAndAlerts .= "Please select your status!<br/>\n"; }
	if (!@$_REQUEST['camera'])                { $errorsAndAlerts .= "Please select a camera brand!<br/>\n"; }
	if (!@$_REQUEST['genres'])                { $errorsAndAlerts .= "Please select a genre!<br/>\n"; }
	if (!@$_REQUEST['birthday'])                { $errorsAndAlerts .= "Please enter your birthday!<br/>\n"; }

    // new password checking
    if (@$_REQUEST['oldPassword'] || $_REQUEST['newPassword1'] || $_REQUEST['newPassword2']) {
      $oldPasswordHash = (@$SETTINGS['advanced']['encryptPasswords']) ? getPasswordDigest(@$_REQUEST['oldPassword']) : @$_REQUEST['oldPassword'];
      if (!@$_REQUEST['oldPassword'])                                 { $errorsAndAlerts .= "Please enter a value for: Current Password<br/>\n"; }
      elseif ($oldPasswordHash != $CURRENT_USER['password'])          { $errorsAndAlerts .= "Current password isn't correct!<br/>\n"; }
      elseif (!@$_REQUEST['newPassword1'])                            { $errorsAndAlerts .= "Please enter a value for: New Password<br/>\n"; }
      elseif (!@$_REQUEST['newPassword2'])                            { $errorsAndAlerts .= "Please enter a value for: Confirm New Password<br/>\n"; }
      elseif ($_REQUEST['newPassword1'] != $_REQUEST['newPassword2']) { $errorsAndAlerts .= "New passwords don't match!<br/>\n"; }
    }

    // check for duplicate usernames and emails
    if (@$_REQUEST['username'] != $CURRENT_USER['username']) {
      $count = mysql_count($GLOBALS['WSM_ACCOUNTS_TABLE'], "`username` = '".mysql_escape(@$_REQUEST['username'])."'");
      if ($count > 0 && @$_REQUEST['username']) { $errorsAndAlerts .= "That username is already in use, please choose another!<br/>\n"; }
    }
    if (@$_REQUEST['email'] != $CURRENT_USER['email']) {
      $count = mysql_count($GLOBALS['WSM_ACCOUNTS_TABLE'], "'".mysql_escape($_REQUEST['email'])."' IN (email, username)");
      if ($count > 0) { $errorsAndAlerts .= "That email is already in use, please choose another!<br/>\n"; }
    }

	
    // update user
    if (!$errorsAndAlerts) {
      mysqlStrictMode(false);   // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)
      if (@$_REQUEST['newPassword2']) { $CURRENT_USER['password'] = $_REQUEST['newPassword2']; } // update password

      // update password if needed
      $password         = @$_REQUEST['newPassword2'] ? @$_REQUEST['newPassword2'] : $CURRENT_USER['password'];
      if (@$SETTINGS['advanced']['encryptPasswords']) { $passwordHash = getPasswordDigest($password); }
      else                                            { $passwordHash = $password; }
	  
	  // update checkbox 
	  if (is_array(@$_REQUEST['genres'])) { 
	$degital_or_film = "\t". join("\t", $_REQUEST['genres']). "\t"; 
      } 
      else { 
	$degital_or_film = "\t\t"; 
      }
	  if (is_array(@$_REQUEST['camera'])) { 
	$degital_or_film = "\t". join("\t", $_REQUEST['camera']). "\t"; 
      } 
      else { 
	$degital_or_film = "\t\t"; 
      }
	  
	  
	$birthday = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month'],@$_REQUEST['day'],@$_REQUEST['year'])); 

      //
      $query = "UPDATE `{$TABLE_PREFIX}" . @$GLOBALS['WSM_ACCOUNTS_TABLE'] . "` SET
                      firstname         = '".mysql_escape( $_REQUEST['firstname'] )."',
					  middle_name       = '".mysql_escape( $_REQUEST['middle_name'] )."',
                      lastname          = '".mysql_escape( $_REQUEST['lastname'] )."',
                      email             = '".mysql_escape( $_REQUEST['email'] )."',
                      username          = '".mysql_escape( $_REQUEST['email'] )."',
                      password          = '".mysql_escape( $passwordHash )."',
					  state             = '".mysql_escape( $_REQUEST['state'] )."',
					  address           = '".mysql_escape( $_REQUEST['address'] )."',
					  website_title     = '".mysql_escape( $_REQUEST['website_title'] )."',
					  url               = '".mysql_escape( $_REQUEST['url'] )."',
					  status            = '".mysql_escape( $_REQUEST['status'] )."',
					  occupation        = '".mysql_escape( $_REQUEST['occupation'] )."',
					  camera            = '".mysql_escape( $camera )."',
					  genres            = '".mysql_escape( $genres )."',
					  how_did_you_know  = '".mysql_escape( $_REQUEST['how_did_you_know'] )."',
					  favorite_artist   = '".mysql_escape( $_REQUEST['favorite_artist'] )."',
					  favorite_artist2  = '".mysql_escape( $_REQUEST['favorite_artist2'] )."',
					  favorite_artist3  = '".mysql_escape( $_REQUEST['favorite_artist3'] )."',
					  favorite_website  = '".mysql_escape( $_REQUEST['favorite_website'] )."',
					  favorite_website2 = '".mysql_escape( $_REQUEST['favorite_website2'] )."',
					  favorite_website3 = '".mysql_escape( $_REQUEST['favorite_website3'] )."',
					  favorite_magazine_book  = '".mysql_escape( $_REQUEST['favorite_magazine_book'] )."',
					  favorite_magazine_book2 = '".mysql_escape( $_REQUEST['favorite_magazine_book2'] )."',
					  favorite_magazine_book2 = '".mysql_escape( $_REQUEST['favorite_magazine_book2'] )."',
					  bio               = '".mysql_escape( $_REQUEST['bio'] )."',
					  type             = '".mysql_escape( $_REQUEST['type'] )."',
					  birthday       	= '".mysql_escape($birthday)."',
					  main_image_title             = '".mysql_escape( $_REQUEST['main_image_title'] )."'
					  

                      updatedByUserNum = '".mysql_escape( $CURRENT_USER['num'] )."',
                      updatedDate      = NOW()
                 WHERE num = '".mysql_escape( $CURRENT_USER['num'] )."'";
      mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
      $userNum = mysql_insert_id();


      // on success
      unset($_REQUEST['oldPassword'], $_REQUEST['newPassword1'], $_REQUEST['newPassword2']); // clear password fields
      $errorsAndAlerts = "Thanks, we've updated your profile!";
    }
  }


  // delete account
  if (@$_POST['deleteAccount']) {
    if ($CURRENT_USER['isAdmin']) { die("Error: Deleting admin accounts is not permitted!"); }

    // delete uploads
    $GLOBALS['tableName'] = $GLOBALS['WSM_ACCOUNTS_TABLE'];
    eraseRecordsUploads( $CURRENT_USER['num'] );

    // delete account
    $query = mysql_escapef("DELETE FROM `{$TABLE_PREFIX}accounts` WHERE num = ?", $CURRENT_USER['num']);
    mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");

    // redirect to login
    websiteLogin_redirectToLogin();
  }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>==LOTUS Photographer's Database== Top Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<link href="../css/layout.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="../css/superfish.css" media="all">
<link rel="stylesheet" type="text/css" href="../css/colorbox.css" media="screen">
<script type="text/javascript" src="../js/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="../js/jquery.colorbox-min.js"></script>
<script type="text/javascript" src="../js/hoverIntent.js"></script>
<script type="text/javascript" src="../js/superfish.js"></script>
<script type="text/javascript">$(function() {
	jQuery(function(){
		jQuery('ul.sf-menu').superfish();
	});
	</script>
<script>
			$(document).ready(function(){
				//Examples of how to assign the ColorBox event to elements
				$(".group1").colorbox({rel:'group1'});
				$(".iframe").colorbox({iframe:true, width:"50%", height:"50%"});
			});
		</script>
</head>
<body>
<div id="wrapper">
  <?php
include('../header.php'); 
?>
  <!-- /header -->
  <div id="content" class="clearFF">
    <div id="mainContent">
      <h1>My Portfolio and Profile</h1>
      <!-- EDIT PROFILE FORM -->
      <?php if (@$errorsAndAlerts): ?>
      <div style="color: #C00; font-weight: bold; font-size: 14px; font-family: arial;"><br/>
        <?php echo $errorsAndAlerts; ?><br/>
        <br/>
      </div>
      <?php endif ?>
      <p>You can always come back to edit or update your portfolio, but once the 'Update' button is pressed, the changes you've made will reflect your portfolio page. Be sure you have entered the correct information.<br />
      
      <p>Here is how your portfolio page looks so far.<br />
        <a href="<?php echo @$_REQUEST['_link'] ?>">My Portfolio Page</a><br />
      <form method="post" action="<?php echo $GLOBALS['WEBSITE_LOGIN_PROFILE_URL'];?>" enctype="multipart/form-data">
        <input type="hidden" name="save" value="1" />
        <table border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td colspan="2" valign="top"><h2>Portfolio page information</h2></td>
          </tr>
          <tr>
            <td width="181" valign="top">First name</td>
            <td width="593" valign="top"><input type="text" name="firstlname" value="<?php echo htmlspecialchars(@$_REQUEST['firstname']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">Middle name (option)</td>
            <td valign="top"><input type="text" name="lastlname" value="<?php echo htmlspecialchars(@$_REQUEST['middle_name']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">Last name</td>
            <td valign="top"><input type="text" name="lastlname" value="<?php echo htmlspecialchars(@$_REQUEST['lastname']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">State</td>
            <td valign="top"><?php $fieldname = 'state'; ?>
              <?php $idCounter = 0; ?>
              <?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
              <?php $id = "$fieldname." . ++$idCounter; ?>
              <div class="categoryLink"><input type="radio" name="<?php echo $fieldname ?>" id="<?php echo $id ?>"
    value="<?php echo htmlspecialchars($value) ?>" <?php checkedIf(@$_REQUEST[$fieldname], $value) ?> />
              <label for="<?php echo $id ?>"><?php echo htmlspecialchars($label) ?> </label></div>
              <?php endforeach ?>
            </td>
          </tr>
          <tr>
            <td valign="top">Your official website title</td>
            <td valign="top">
              <input type="text" name="website_title" value="<?php echo htmlspecialchars(@$_REQUEST['website_title']); ?>" size="50" /><br />
            (ex. My Works)<br /></td>
          </tr>
          <tr>
            <td valign="top">Your website URL</td>
            <td valign="top">
              <input type="text" name="url" value="<?php echo htmlspecialchars(@$_REQUEST['url']); ?>" size="50" /><br />
            (ex. http://www.example.com)</td>
          </tr>
          <tr>
            <td valign="top">Genres of your works<br />
            (Choose one or more)</td>
            <td valign="top">
              <?php $fieldname = 'genres'; ?>
              <?php 
			  if(is_array(@$_REQUEST[$fieldname])){ 
			  $fieldValues = $_REQUEST[$fieldname]; 
			  } 
			  else{ 
			  $fieldValues = explode("\t",trim(@$_REQUEST[$fieldname],"\t")); 
			  } 
			  ?>
              <?php $idCounter = 0; ?>
              <?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
              <?php $id = "$fieldname." . ++$idCounter; ?>
              <div class="categoryLink"><input type="checkbox" name="<?php echo $fieldname ?>"[] id="<?php echo $id ?>" 
             value="<?php echo htmlspecialchars($value) ?>" <?php if(in_array($value,$fieldValues)):?> checked="checked" <?php endif ?>/>
              <label for="<?php echo $id ?>"><?php echo htmlspecialchars($label) ?></label></div>
              <br/>
              <?php endforeach ?></td>
          </tr>
          <tr>
            <td valign="top">Enter your biography</td>
            <td valign="top"><textarea name="bio" COLS=80 ROWS=30> <?php echo htmlspecialchars(@$_REQUEST['bio']); ?></textarea></td>
          </tr>
          <tr>
            <td valign="top">Upload your main image</td>
            <td valign="top">
            </td>
          </tr>
          <tr>
            <td colspan="2" valign="top">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2" valign="top">My Profile</td>
          </tr>
          <tr>
            <td valign="top">Email</td>
            <td valign="top"><input type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">Address</td>
            <td valign="top"><input type="text" name="address" value="<?php echo htmlspecialchars(@$_REQUEST['address']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">Birthday</td>
            <td valign="top">
			<?php $birthday = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month'],@$_REQUEST['day'],@$_REQUEST['year']));?>
			<?php 
			$lowestYear  = 1920; 
			$highestYear = 2010; 
			?>
              Month:
              <select name="month">
                <?php foreach(range(1,12) as $month): ?>
                <option value="<?php echo $month;?>"><?php echo date("F",strtotime("0000-$month"));?></option>
                <?php endforeach ?>
              </select>
              Day:
              <select name="day">
                <?php foreach(range(1,31)as $day): ?>
                <option value="<?php echo $day;?>"><?php echo $day;?></option>
                <?php endforeach ?>
              </select>
              Year:
              <select name="year">
                <?php foreach (range($lowestYear,$highestYear) as $year):?>
                <option value="<?php echo $year;?>"><?php echo $year;?></option>
                <?php endforeach?>
              </select>
              
            </td>
          </tr>
          <tr>
            <td valign="top">Your Status as a photographer</td>
            <td valign="top">
			  <?php $fieldname = 'status'; ?>
              <?php $idCounter = 0; ?>
              <?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
              <?php $id = "$fieldname." . ++$idCounter; ?>
              <input type="radio" name="<?php echo $fieldname ?>" id="<?php echo $id ?>" value="<?php echo htmlspecialchars($value) ?>" <?php checkedIf(@$_REQUEST[$fieldname], $value) ?> />
              <label for="<?php echo $id ?>"><?php echo htmlspecialchars($value) ?></label>
              <?php endforeach ?></td>
          </tr>
          <tr>
            <td height="100" valign="top">Camera<br />
            (Choose one or more)</td>
            <td valign="top">
              <?php $fieldname = 'camera'; ?>
              <?php 
			  if(is_array(@$_REQUEST[$fieldname])){ 
			  $fieldValues = $_REQUEST[$fieldname]; 
			  } 
			  else{ 
			  $fieldValues = explode("\t",trim(@$_REQUEST[$fieldname],"\t")); 
			  } 
			  ?>
              <?php $idCounter = 0; ?>
              <?php foreach (getListOptions('accounts', $fieldname) as $value => $label): ?>
              <?php $id = "$fieldname." . ++$idCounter; ?>
              <input type="checkbox" name="<?php echo $fieldname ?>"[] id="<?php echo $id ?>" 
              value="<?php echo htmlspecialchars($value) ?>" <?php if(in_array($value,$fieldValues)):?> checked="checked" <?php endif ?>/>
              <label for="<?php echo $id ?>"><?php echo htmlspecialchars($label) ?></label>
              <br/>
              <?php endforeach ?></td>
          </tr>
          <tr>
            <td valign="top">Favorite artist</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_artist']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top"> (option)</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_artist2']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">(option)</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_artist3']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">Favorite website</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_website']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top"> (option)</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_website2']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top"> (option)</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_website3']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">Favorite Magazine / Book</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_magazine_book']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top"> (option)</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_magazine_book2']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">(option)</td>
            <td valign="top"><input type="text" name="phone" value="<?php echo htmlspecialchars(@$_REQUEST['favorite_magazine_book3']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top"><br />Membership</td>
            <td valign="top"><br /><b>I am a Free Member</b><br />
              <br />
              <a href="gold-signup">Become a Gold Member</a><br />
              <a href="platinum-signup">Become a Platinum Member</a><br />
              <a href="membership_info">More information about member types.</a><br /></td>
          </tr>
          <tr>
            <td colspan="2" valign="top">&nbsp;</td>
          </tr>
          <tr>
            <td valign="top">Current Password</td>
            <td valign="top"><input type="password" name="oldPassword" value="<?php echo htmlspecialchars(@$_REQUEST['oldPassword']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">New Password</td>
            <td valign="top"><input type="password" name="newPassword1" value="<?php echo htmlspecialchars(@$_REQUEST['newPassword1']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td valign="top">New Password (again)</td>
            <td valign="top"><input type="password" name="newPassword2" value="<?php echo htmlspecialchars(@$_REQUEST['newPassword2']); ?>" size="50" /></td>
          </tr>
          <tr>
            <td colspan="2" align="center"><br/>
              <input class="button" type="submit" name="submit" value="Update profile &gt;&gt;" />
            </td>
          </tr>
        </table>
        <tr>
          <td colspan="2" align="center"><br/>
          <a href="<?php echo @$_REQUEST['_link'] ?>">Your Portfolio Page</a> </td>
        </tr>
        </table>
      </form>
      <br/>
      <!-- /EDIT PROFILE FORM -->
      <h1>Update Record</h1>
      <?php if (@$errorsAndAlerts): ?>
      <div style="color: #C00; font-weight: bold; font-size: 14px;"><?php echo $errorsAndAlerts; ?></div>
      <?php endif ?>
      <hr/>
      <form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
        <input type="hidden" name="submitForm" value="1" />
        <input type="hidden" name="num" value="<?php echo $recordNum ?>" />
        <input type="hidden" name="preSaveTempId" value="<?php echo $preSaveTempId ?>" />
        <table border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td valign="top">Title</td>
            <td><input class="text-input medium-input" type="text" name="title" value="<?php echo htmlspecialchars(@$_REQUEST['title']) ?>" size="30" /></td>
          </tr>
          <tr>
            <td valign="top">Uploads</td>
            <td><?php /* TODO: Add security check in uploadForm2_iframe.php to limit access to only allowed uploads */ ?>
              <iframe src='uploadForm2_iframe.php?table=<?php echo $tableName ?>&amp;field=uploads&amp;num=<?php echo $recordNum ?>&amp;preSaveTempId=<?php echo $preSaveTempId ?>'
            height='100' width='600' frameborder='0' scrolling='no'> </iframe>
              <br/>
            </td>
          </tr>
        </table>
        <br/>
        <hr/>
        <input class="button" type="submit" name="submitForm" value="Update Record &gt;&gt;" />
      </form>
      <!-- REMOVE PROFILE FORM -->
      <div style="border: 1px solid #000; background-color: #EEE; padding: 20px; width: 500px"> <b>Delete Account</b>
        <p>If you want to delete your account you can do so here.<br/>
          Please note that all data will be lost and this is irreversible.</p>
        <form method="post" action="?" onsubmit="return confirm('Are you sure you want to delete your account?')">
          <input type="submit" name="deleteAccount" value="Delete Account" />
        </form>
      </div>
      <!-- /REMOVE PROFILE FORM -->
    </div>
  </div>
  <!-- /#content -->
  <!-- /#bannerBottom -->
  <?php
include('../footer.html'); 
?>
  <!-- /#footer -->
</div>
<!-- /#wrapper -->
</body>
</html>
