<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

$showm = '6';
include('header.php');

$error = '';
$the_user = array();
$user_id = '';
$errors = array();

$user_id = (int) $_GET['uid'];

$action = (int) $_GET['action'];

if( empty($user_id) || $user_id === '' || !is_numeric($user_id) )
{
	$error = "The User ID in your URL is not valid or missing";
}
else
{
	$the_user = fetch_user_advanced($user_id);
	
	$the_user['comment_count'] = count_entries('pm_comments', 'user_id', $the_user['id']);
}

if($action == 1 && is_array($the_user))	//	activate user account
{
	if($the_user['power'] == U_INACTIVE)
	{
		$sql = "UPDATE pm_users SET power = '".U_ACTIVE."' WHERE id='".$user_id."'";
		$result = @mysql_query($sql);
		if(!$result)
		{
			$info_msg = '<div class="info_msg_errimg">Activation failed.<br />MySQL returned: '.mysql_error().'</div>';
		}
		else
		{
			$info_msg = '<div class="info_msg_ok">This user\'s account is now active.</div>';
		}
	}
	else
	{
		$info_msg = '<div class="info_msg_ok">This user\'s account is already active.</div>';
	}
}
else if ($action == 9 && is_array($the_user))	//	delete all comments posted by this user
{
	if (is_moderator() && mod_cannot('manage_comments'))
	{
		echo '<div id="content">';
		restricted_access();
	}
	
	$sql = "DELETE FROM 
			pm_comments 
			WHERE user_id = '". $the_user['id']."'";
	
	$result = @mysql_query($sql);
	if ( ! $result)
	{
		$error = 'An error occurred while attempting to delete the user\'s comments.<br />MySQL returned: '.mysql_error();
	}
	else
	{
		$info_msg = '<div class="info_msg_ok">Comments made by this user have been deleted.</div>';
		$the_user['comment_count'] = count_entries('pm_comments', 'user_id', $user_id);
	}
}

if( isset($_POST['save']))
{

	$no_errors = 0;

	if( check_username($_POST['username']) == 3 && $_POST['username'] != $the_user['username'] )
	{
		$error = "Username is already in use";
		$no_errors++;
	}

	if( validate_email($_POST['email']) == 2 && $_POST['email'] != $the_user['email'] )
	{
		$error = "Email is already in use";
		$no_errors++;
	}

	if( $_POST['delete_avatar'] == 1 && $the_user['avatar'] != "no_avatar.gif" )
	{
		// delete avatar;
		if( unlink(ABSPATH."uploads/avatars/".$the_user['avatar']) === FALSE )
			$error = "Could not delete the user's avatar";
	}

	if( $no_errors == 0 )
	{	
		$sql = "UPDATE pm_users SET ";
		
		if( $_POST['new_pass'] != '' )
			$sql .= " password = '".md5($_POST['new_pass'])."', ";
		
		if( $_POST['delete_avatar'] == 1 )
			$sql .= " avatar = 'no_avatar.gif', ";
		
		$sql .= " username = '".secure_sql($_POST['username'])."', name = '".secure_sql($_POST['name'])."', ";
		$sql .= " gender = '".$_POST['gender']."', country = '".$_POST['country']."', email = '".$_POST['email']."', ";
		$sql .= " about = '".secure_sql($_POST['aboutme'])."', favorite = '".$_POST['favorite']."', ";
		$sql .= " website = '". secure_sql($_POST['website']) ."', facebook = '". secure_sql($_POST['facebook']) ."', ";
		$sql .= " twitter = '". secure_sql($_POST['twitter']) ."', lastfm = '". secure_sql($_POST['lastfm']) ."' "; 
	
		if (is_admin() && isset($_POST['user_power']))
		{
			$sql .= ", power = '".$_POST['user_power']."'";
		}
		
		$sql .= " WHERE id = ".$the_user['id']."";

		$result = @mysql_query($sql);
		if( !$result )
			$error = "There was a problem while updating this user. <br /> Mysql returned this error : ".mysql_error();
		
		// Was the username changed? Update the pm_comments table with the new username too;
		if($_POST['username'] != $the_user['username'])
		{
			$new_username = secure_sql($_POST['username']);
			$all_ids = '';

			// update pm_comments
			$sql = "SELECT id FROM pm_comments WHERE username = '".$the_user['username']."' AND user_id='".$the_user['id']."'";
			$result = mysql_query($sql);
			$total = mysql_num_rows($result);
			
			if($total > 0)
			{
				while($row = mysql_fetch_assoc($result))
				{
					$all_ids .= $row['id'] . ", ";
				}
				$all_ids = substr($all_ids, 0, -2);
				
				mysql_free_result($result);
				
				$sql = "UPDATE pm_comments SET username = '".$new_username."' WHERE id IN(".$all_ids.")";
				$result = @mysql_query($sql);
			}
			
			unset($all_ids, $total, $result);
			$all_ids = '';
			
			// update pm_videos			
			$sql = "SELECT id FROM pm_videos WHERE submitted = '".$the_user['username']."'";
			$result = mysql_query($sql);
			$total = mysql_num_rows($result);
			
			if($total > 0)
			{
				while($row = mysql_fetch_assoc($result))
				{
					$all_ids .= $row['id'] . ", ";
				}
				
				$all_ids = substr($all_ids, 0, -2);
				
				mysql_free_result($result);
				
				$sql = "UPDATE pm_videos SET submitted = '".$new_username."' WHERE id IN(".$all_ids.")";
				$result = @mysql_query($sql);
			}
		}
	}
}

?>
<div id="sub_menu">

</div>
<div id="content">
<h2>Edit User Profile: <?php echo $the_user['username']; ?></h2>

<?php echo $info_msg; ?>
	<?php

		if( !isset($_POST['save']) && $error != '' )
			echo "<div class=\"info_msg_err\">".$error."</div>";
			
		else {
			if( isset($_POST['save']) && $no_errors > 0 )
				echo "<div class=\"info_msg_err\">".$error."</div>";
			elseif( isset($_POST['save']) && $no_errors == 0 )
			{
				echo "<div class=\"info_msg_ok\">The user account has been updated.</div>";
				echo '<input name="continue" type="button" value="&laquo; Manage users" onClick="location.href=\'members.php\'" class="inputbutton" />';
			}
			else
			{
			$sql = "SELECT * FROM pm_countries";
			$result = mysql_query($sql);
			if(!$result) { 
				echo "<div class=\"info_msg_err\">Could not generate countries list</div>";
			}
			while($row = mysql_fetch_assoc($result))
			{
				$countries_list[$row['countryid']] = $row['country'];
			}
			@mysql_free_result($result);
	?>

	<a href="comments.php?keywords=<?php echo urlencode($the_user['username']);?>&search_type=username&submit=Search" class="b_edit">Read all comments (<?php echo $the_user['comment_count'];?>)</a>
	 <?php 
	 if (is_admin() || (is_moderator() && mod_can('manage_comments')))
	 {
	 	?>
	 	| <a href="edit_user_profile.php?action=9&uid=<?php echo $user_id;?>" onclick="return confirm_delete_all();" class="b_delete">Delete all comments (<?php echo $the_user['comment_count'];?>)</a>
		<?php
	 }
	 ?>
	
	<?php
	
	// check if banned.
	$sql = "SELECT COUNT(*) AS total, reason FROM pm_banlist WHERE user_id = '". $the_user['id'] ."'";
	if ($result = @mysql_query($sql))
	{
		$ban = mysql_fetch_assoc($result);
		mysql_free_result($result);
	}
	
	?>
	
	<form name="edit_profile_form" method="POST" action="<?php echo "edit_user_profile.php?uid=".$user_id; ?>">
	<table width="60%" border="0" cellspacing="1" cellpadding="3" align="center">
	<?php
		if ($ban['total'] > 0)
		{
			?>
			<tr>
				<td colspan="2">
				<div class="info_msg_err">
					This user account is banned. 
					<br /> 
					<?php if ($ban['reason'] != '') : ?>
					<b>Reason:</b> <?php echo $ban['reason'];?>
					<?php endif; ?>				
				</div>
				</td>
			</tr>
			<?php
		}
	?>
	  <tr>
	  	<td colspan="2"><h3>Avatar</h3></td>
	  </tr>
	  <tr>
	  	<td>
			<span class="avatar_border"><img src="<?php echo _URL."/uploads/avatars/".$the_user['avatar']; ?>" border="0" alt="" /></span>		</td>
		<td>
			<input type="checkbox" class="checkbox" name="delete_avatar" value="1" /> Delete this avatar?		</td>
	  </tr>
	  <tr>
		<td colspan="2"><h3>Change Password?</h3></td>
	  </tr>
	  <tr>
		<td class="fieldtitle">New Password:</td>
		<td valign="absmiddle"><input name="new_pass" type="password" maxlength="32" class="inputtext" /><br />
		<small>Leave blank if you don't want to change the password</small>		</td>
	  </tr>
	   <tr>
		<td valign="top"><h3>User level</h3></td>
		<td valign="absmiddle">
		  <select name="user_power">
		  	<?php
			
			if( $the_user['power'] == U_INACTIVE)
			{
				?>
				<option value="<?php echo U_INACTIVE; ?>" <?php if($the_user['power'] == U_INACTIVE) echo 'selected="selected"';?>>Inactive User</option>';
				<?php
			}
			
			if (is_admin())
			{
			 	?>
				
				<option value="<?php echo U_ACTIVE;?>"  <?php if($the_user['power'] == U_ACTIVE) echo 'selected="selected"';?> >Regular User</option>
				<option value="<?php echo U_EDITOR;?>"  <?php if($the_user['power'] == U_EDITOR) echo 'selected="selected"';?> >Editor</option>
				<option value="<?php echo U_MODERATOR;?>"  <?php if($the_user['power'] == U_MODERATOR) echo 'selected="selected"';?> >Moderator</option>
				<option value="<?php echo U_ADMIN;?>"  <?php if($the_user['power'] == U_ADMIN) echo 'selected="selected"';?> >Administrator</option>
				
			 	<?php
			}
			else 
			{
				?>
				
				<option value="<?php echo $the_user['power'];?>"  selected="selected">
					<?php
					switch ($the_user['power'])
					{
						default:
						case U_ACTIVE: 		echo 'Regular User';	break;
						case U_EDITOR: 		echo 'Editor'; 			break;
						case U_MODERATOR:	echo 'Moderator'; 		break;
						case U_ADMIN:		echo 'Administrator';	break;
					} 
					?>
				</option>
				
				<?php
			}
			?>
		  </select>		</td>
	  </tr>
	  <tr>
		<td colspan="2"><h3>Other information</h3></td>
	  </tr>
	  <tr>
		<td class="fieldtitle"><strong>Username:</strong></td>
		<td><input type="text" name="username" class="inputtext" value="<?php echo $the_user['username']; ?>" /><br />		</td>
	  </tr>		  
	  <tr>
		<td class="fieldtitle">Email address:</td>
		<td><input type="text" name="email" class="inputtext" value="<?php echo $the_user['email']; ?>" /><br />		</td>
	  </tr>		  
	  <tr>
		<td width="36%" class="fieldtitle">Full Name:</td>
	  <td width="64%"><input name="name" type="text" class="inputtext" value="<?php echo $the_user['name']; ?>" /><br />		  </tr>
	  <tr>
		<td class="fieldtitle">Gender:</td>
		<td> <input name="gender" type="radio" value="male" <?php if($the_user['gender'] == "male") echo "checked"; ?> /> Male 
			<input name="gender" type="radio" value="female" <?php if($the_user['gender'] == "female") echo "checked"; ?> /> Female		</td>
	  </tr>
	  <tr>
		<td class="fieldtitle">Favorite Videos List</td>
		<td> <input name="favorite" type="radio" value="1" <?php if($the_user['favorite'] == 1) echo "checked"; ?> /> Public 
			<input name="favorite" type="radio" value="0" <?php if($the_user['favorite'] == 0) echo "checked"; ?> /> Private		</td>
	  </tr>	  	  
	  <tr>
		<td class="fieldtitle">Country:</td>
		<td>
			<select name="country" size="1" >
			<option value="-1">Select one</option>
				<?php
					$opt = '';
					foreach($countries_list as $k => $v)
					{
						$opt = "<option value=\"".$k."\"";
						if( $the_user['country'] == $k )
							$opt .= " selected ";
						$opt .= ">".$v."</option>";
						echo $opt;
					}
					?>
			</select>		 </td>
	  </tr>
	  <tr>
		<td class="fieldtitle">Website</td>
		<td><input type="text" name="website" class="inputtext" size="45" value="<?php echo $the_user['website']; ?>" /><br />		</td>
	  </tr>
	  <tr>
		<td class="fieldtitle">Facebook</td>
		<td><input type="text" name="facebook" class="inputtext" size="45" value="<?php echo $the_user['facebook']; ?>" /><br />		</td>
	  </tr>	
	  <tr>
		<td class="fieldtitle">Twitter</td>
		<td><input type="text" name="twitter" class="inputtext" size="45" value="<?php echo $the_user['twitter']; ?>" /><br />		</td>
	  </tr>	
	   <tr>
		<td class="fieldtitle">Last.fm</td>
		<td><input type="text" name="lastfm" class="inputtext" size="45" value="<?php echo $the_user['lastfm']; ?>" /><br />		</td>
	  </tr>	
	  <tr>
		 <td colspan="2" valign="top"><h3>About you</h3></td>
	  </tr>
	  <tr>
		<td valign="top">
		 </td>
	    <td valign="top">
		<textarea name="aboutme" cols="60" rows="10" class="inputtext"><?php echo $the_user['about']; ?></textarea>
		</td>
	  </tr>	  
	  <tr>
		<td>&nbsp;</td>
		<td><input type="submit" name="save" value="Save" class="inputbutton" /></td>
	  </tr>
	</table>
	</form>
	<?php
			}
	 	} // end else
	?>
</div>
<?php
include('footer.php');
?>