<?php
/*
copyright: copyright 2004 Php Image Host. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE

*/

require_once(dirname(__FILE__).'/includes/config.inc.php');
require_once(dirname(__FILE__).'/../classes/users.class.php');
$users =& new users($ace);

$newstatus = 0;

$femail = $ace->getstr('femail', $_POST);
$fusername = $ace->getstr('fusername', $_POST);
$fname = $ace->getstr('fname', $_POST);
$fstatus = $ace->getint('fstatus', $_POST);
$fpassword = $ace->getstr('fpassword', $_POST);

$user = 0;
$edit = $ace->getint('edit', $_REQUEST);
$userid = $ace->getint('userid', $_REQUEST);
if( $userid != 0 ){
	$user = $users->getuser(array('id'=>$userid));
	if( !$user ){
		$userid = 0;
	}else{
		$edit = 1;
		if( $ace->getstr('update',$_POST) == ''){
			$femail = $user->email;
			$fusername = $user->username;
			$fname = $user->name;
			$fstatus = $user->status;
			$fpassword = $user->pass;
		}
	}
}

$message = '';
$errors = array();
$criteria = array();
$statustexts = array('Unconfirmed', 'Confirmed', 'Suspended');
$ids = isset($_POST['ids']) ? $ace->getids($_POST['ids']) : array();
if( count($ids) > 0 ){
	if( $ace->getstr('delete', $_POST) != '' ){
		$message = $users->deleteusers($ids)." user(s) deleted. ";
	}elseif( $ace->getstr('upgrade', $_POST) != '' ){
		$extradays = $ace->getint('extradays', $_POST);
		if( $extradays > 0 ){
			$message = $users->upgradeusers($ids, $extradays).' user(s) upgraded to paid account for '.$extradays.' additional days.';
		}
	}elseif( $ace->getstr('downgrade', $_POST) != '' ){
		$message = $users->updateusers($ids, array('account_type'=>'free', 'paid_until'=>date('Y-m-d'))).' users account type set to "free".';
	}elseif( $ace->getstr('changestatus', $_POST) != '' ){
		$newstatus = $ace->getint('newstatus', $_POST);
		if( $newstatus >= 0 && $newstatus < 3){
			$message = $users->updateusers($ids, array('status'=>$newstatus)).' users accounts set to "'.$statustexts[$newstatus].'".';
		}
	}elseif( $ace->getstr('resetbandwidth', $_POST) != '' ){
		if( count($ids) ){
			$sql = "UPDATE users SET deleted_images_bandwidth=0, bandwidth_exceeded=0 ";
			if( $ace->getint('resetday', $_POST) != 0 ) $sql .= ", bandwidth_reset=now() ";
			$sql .="WHERE user_id IN (".join(",",$ids).") ";
			$ace->query($sql, 'Reset User Bandwidth');
			$message = 'Bandwidth usage reset for selected users.';
			$sql = "UPDATE images SET bandwidth=0 WHERE user_id IN (".join(",",$ids).") ";
			$ace->query($sql, 'Reset Image Bandwidth');
		}
	}
}elseif( $ace->getstr('update', $_POST) != '' ){
	if( $user == 0 ){
		$userid = $users->adduser($fusername, $fpassword, $femail, $fname, $fstatus);
		if( $userid ){
			$message = 'User "'.$fusername.'" Added.';
			$user = $users->getuser(array('id'=>$userid));
		}else{
			$errors = $users->errors;
		}
	}else{
		if( $user->username != $fusername ){
			if( !preg_match('/^([a-z0-9]{2,20})$/i', $fusername) ){
				$users->errors[] = 'The username must be between 2 and 20 alphanumeric characters long.';
			}else{
				if( $users->getuser(array('username'=>$fusername) ) ) $users->errors[] = 'The username "'.$fusername.'" is already taken by another user.';
			}
		}
		if( !preg_match('/^([a-z0-9]{6,20})$/i', $fpassword) ) $users->errors[] = 'The password must be between 6 and 12 alphanumeric characters long.';
		if( $user->email != $femail ){
			if( !$ace->validateemail($femail) ){
				$users->errors[] = 'You must enter a valid email address.';
			}else{
				if( $users->getuser(array('email'=>$femail)) ) $users->errors[] = 'The email address "'.$femail.'" is already in use in another account.';
			}
		}
		if( count($users->errors) == 0 ){
			if( $users->updateusers(array($userid), array('username'=>$fusername, 'email'=>$femail,
																'pass'=>$fpassword, 'name'=>$fname, 'status'=>$fstatus)) ){
				$message = 'User "'.$fusername.'" Updated.';
				$user->username = $fusername;
			}
		}
		$errors = $users->errors;
	}
	$edit = 1;
}

$perpage = $ace->getint('perpage', $_REQUEST, 30);

$fromdate = $todate = '';
$from = $ace->getstr('fromdate', $_REQUEST);
if( $from != '' ){
	if( ($tm = @strtotime($from)) !== -1 ){
		$fromdate = date('Y-m-d', $tm);
	}
}
$to = $ace->getstr('todate', $_REQUEST);
if( $to != '' ){
	if( ($tm = @strtotime($to)) !== -1 ){
		$todate = date('Y-m-d', $tm);
	}
}

$name = $ace->getstr('name', $_REQUEST);
$email = $ace->getstr('email', $_REQUEST);
$username = $ace->getstr('uname', $_REQUEST);
$totalimages = $ace->getint('images', $_REQUEST);
$datetype = $ace->getstr('datetype', $_REQUEST);
$datetypes = array('joined'=>'Joined', 'loggedin'=>'Last Loggedin');
if( !isset($datetypes[$datetype]) ) $datetype = 'joined';
$status = $ace->getint('status', $_REQUEST,-1);

$bwidth = $ace->getint('bandwidth', $_REQUEST);

$account = $ace->getstr('account', $_REQUEST);

$orderby = $ace->getstr('orderby', $_REQUEST);
$orderdir = $ace->getstr('orderdir', $_REQUEST);

if( !isset($users->orderbys[$orderby]) ) $orderby = 'username';
$orderdir = $ace->getstr('orderdir', $_REQUEST, 'asc');
if( $orderdir != 'asc' && $orderdir != 'desc' ) $orderdir = 'asc';

if( $bwidth ) $criteria['bandwidth'] = true;
if( $username != '' ) $criteria['username'] = $username;
if( $name != '' ) $criteria['name'] = $name;
if( $email != '' ) $criteria['email'] = $email;
if( $account != '' ) $criteria['account'] = $account;
if( $todate != '' ) $criteria[$datetype.'before'] = $todate;
if( $fromdate != '' ) $criteria[$datetype.'after'] = $fromdate;
if( $status != -1 ) $criteria['status'] = $status;

$criteria['count'] = true;

$numusers = $users->getusers($criteria);

$numpages = ceil($numusers / $perpage);

$page = $ace->getint('page', $_REQUEST, 1);
if( $page < 1 ) $page = 1;
if( $page > $numpages ) $page =$numpages;

$first = ($page-1) * $perpage;
$last = min($numusers,(($page) * $perpage));

$criteria['count'] = false;

if( $numusers > 0 ) $usrs = $users->getusers($criteria, $orderby, $orderdir, $first, $perpage);

$pageurl = $_SERVER['SCRIPT_NAME']."?datetype=$datetype&status=$status&account=$account&email=$email&fromdate=$fromdate&todate=$todate&name=$name&username=$username&perpage=$perpage&page={page}&orderby=$orderby&orderdir=$orderdir";

require_once(dirname(__FILE__).'/includes/header.inc.php');

?>
<div style="text-align: center;">
<b>Manage Users</b><br /><br />
This page lets you view, add, edit, delete, and change the status of the registered users of your site.
You can also downgrade free accounts to paid accounts, and upgrade free accounts to paid accounts, as
well as increase the number of days remaining on any paid accounts.
Use the form on the left to filter which users are displayed.
<br /><br />
[<a href="users.php?edit=1">Add A New User</a>]
[<a href="users.php?bandwidth=1&status=-1">Exceeding Bandwidth</a>]
[<a href="users.php?status=-1&account=free">Free Members</a>]
[<a href="users.php?status=-1&account=paid">Paid Members</a>]
[<a href="users.php?status=0">Unconfirmed Members</a>]
[<a href="users.php?status=1">Confirmed Members</a>]
[<a href="users.php?status=2">Suspended Members</a>]
[<a href="users.php?status=-1&datetype=loggedin&todate=<?php echo date('Y-m-d',time()-(30*24*60*60));?>">Loggedin over 30 days ago.</a>]
<br /><br />
<table border="0" cellspacing="0" cellpadding="10" width="95%">
<tr>
<td style="text-align: center; vertical-align: top; width: 150px; color: #555555; font-weight: bold;">
<div style="padding: 5px; border: 2px solid #555555; ">
<div style="border-bottom: 2px solid #555555; font-weight: bold;text-align: center;padding: 3px;margin-bottom: 5px;">Search Users</div>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="get">
Username Like<br />
<input type="text" name="uname" value="<?php echo htmlspecialchars($username);?>" />
<br />(Use * as wildcard)
<br /><br />Name Like<br />
<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />
<br />(Use * as wildcard)
<br /><br />Email Like<br />
<input type="text" name="email" value="<?php echo htmlspecialchars($email);?>" />
<br />(Use * as wildcard)
<br /><br />
<select name="datetype">
<?php
foreach($datetypes as $d=>$v ){
	$sel = $d == $datetype ? ' SELECTED ' : '';
	echo "<option value=\"$d\"$sel>$v</option>\n";
}
?>
</select>
<br />
between
<br />
<input type="text" name="fromdate" value="<?php echo htmlspecialchars($fromdate);?>" />
<br />and<br />
<input type="text" name="todate" value="<?php echo htmlspecialchars($todate);?>" />
<br />(enter dates in format yyyy-mm-dd)
<br /><br />Account Status<br />
<select name="status">
<option value="-1">Any</option>
<?php 
foreach( $statustexts as $s=>$v){ 
	$sel = $s == $status ? ' SELECTED ' : '';
	echo "<option value=\"$s\"$sel>$v</option>\n";
}
?>
</select>
<br /><br />Account Type<br />
<select name="account">
<option value="">Any</option>
<option value="free"<?php if( $account == 'free' ) echo ' SELECTED ';?>>Free</option>
<option value="paid"<?php if( $account == 'paid' ) echo ' SELECTED ';?>>Paid</option>
</select>
<br /><br />Order By<br />
<select name="orderby">
<?php
foreach( $users->orderbys as $n=>$v ){
	$sel = $n == $orderby ? ' SELECTED ' : '';
	echo "<option value=\"$n\"$sel>$v</option>\n";
}
?>
</select>
<br />
<select name="orderdir">
	<option value="asc">ascending</option>
	<option value="desc"<?php if( $orderdir == 'desc' ) echo ' SELECTED ';?>>descending</option>
</select>
<br /><br />Display<br />
<input style="width: 40px;" type="text" name="perpage" value="<?php echo $perpage;?>" />
<br />Per Page
<br /><br /><input type="submit" value="List Users" />
</form>
</td>
<td style="text-align: center; vertical-align: top;">

<?php if( $message != '' ){?>
<div class="errors"><?php echo $message;?></div>
<?php } ?>
<?php if( count($errors) > 0 ){ ?>
<div class="errors"><?php echo join("<br />", $errors);?></div>
<?php }

if( !$edit ){
?>
<div class="errors"><?php	echo $numusers;?> user(s) matched your criteria.</div>
<?php
if( $numusers > 0 ){
?>
Listing users <?php echo ($first+1).' to '.$last.' of '.$numusers.'.';?><br /><br />
Page <?php echo $page.' of '.$numpages.'.';?><br /><br />
View Page:
<?php
	for( $i = 1; $i <= $numpages; $i++){
?>
<a href="<?php echo str_replace('{page}', $i,$pageurl);?>"><?php echo $i;?></a>
<?php
	}
?>
<br /><br />
<form name="iform" id="iform" action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<input type="hidden" name="name" value="<?php echo htmlspecialchars($name);?>" />
<input type="hidden" name="status" value="<?php echo $status;?>" />
<input type="hidden" name="orderby" value="<?php echo $orderby;?>" />
<input type="hidden" name="orderdir" value="<?php echo $orderdir;?>" />
<table class="formtable" cellspacing="1" cellpadding="0" border="0" align="center" style="background-color: #555555;">
<tr>
	<th>&nbsp;</th>
	<th>Username</th>
	<th>Name</th>
	<th>Email</th>
	<th>Joined</th>
	<th>Last Login</th>
	<th>Images</th>
	<th>Bandwidth</th>
	<th>Storage</th>
	<th>Status</th>
	<th>Type</th>
</tr>
<?php
	$tr = 0;
	foreach( $usrs as $u ){
		if( $u->max_images > 0 ){
			$imgs = $u->images.' / '.$u->max_images;
			if( $u->images > $u->max_images ) $imgs = '<span style="color: red;">'.$imgs.'</span>';
		}else{
			$imgs = $u->images.' / unlimited.';
		}
		if( $u->bandwidth > 0 ){
			$bwidth = $u->bandwidth_used.' / '.$u->bandwidth;
			if( $u->bandwidth_used > $u->bandwidth ) $bwidth = '<span style="color: red;">'.$bwidth.'</span>'; 
		}else{
			$bwidth = $u->bandwidth_used.' / unlimited';
		}
		if( $u->storage > 0 ){
			$storage = $u->storage_used.' / '.$u->storage;
			if( $u->storage_used > $u->storage ) $storage = '<span style="color: red;">'.$storage.'</span>';
		}else{
			$storage = $u->storage_used.' / unlimited';
		}
?>
<tr class="tr<?php echo ($tr+1);?>">
	<td>
		<input type="checkbox" name="ids[]" value="<?php echo $u->user_id;?>" />
	</td>
	<td><a href="users.php?userid=<?php echo $u->user_id;?>"><?php echo htmlspecialchars($u->username);?></a></td>
	<td><?php echo htmlspecialchars($u->name);?></td>
	<td><?php echo htmlspecialchars($u->email);?></td>
	<td><?php echo @date("Y-m-d", @strtotime($u->joined)).' ('.$u->ip.')';?></td>
	<td><?php echo @date("Y-m-d", @strtotime($u->lastlogin)).' ('.$u->loginip.')';?></td>
	<td style="text-align: center;"><a href="images.php?checked=-1&username=<?php echo $u->username;?>"><?php echo $imgs;?></a></td>
	<td><?php echo $bwidth;?></td>
	<td><?php echo $storage;?></td>
	<td><?php echo $statustexts[$u->status];?></td>
	<td><?php echo ucfirst($u->account_type).($u->account_type == 'paid' ? ' (to '.$u->paid_until.')' : '');?></td>
</tr>
<?php
		$tr = 1 - $tr;
	}
?>
</table>
<br clear="all" />
<input type="button" onClick="check(this.form,true);" value="Select All" />
<input type="button" onClick="check(this.form,false);" value="Select None" />
<input type="button" onClick="invert(this.form);" value="Invert Selection" />
<noscript>
<br />The buttons above require javascript to be enabled in order to function.
</noscript>
<br /><br />
<input type="submit" name="upgrade" value="Upgrade Selected Users For" onClick="return confirm('Are you sure you want to upgrade selected users accounts to paid for the additional number of days selected?');" />
<input type="text" name="extradays" style="width: 40px;" /> Days.
<br /><br />
<input type="submit" name="downgrade" value="Downgrade Selected Users" onClick="return confirm('Are you sure you want to downgrade all the selected users to a free account?');" />
<br /><br />
<input type="submit" name="resetbandwidth" value="Reset bandwidth usage for the selected users" onClick="return confirm('Are you sure you want to reset bandwidth usage for all the selected users?');" />
<input type="checkbox" name="resetday" value="1" /> Reset time period?
<br /><i>Resetting the time period means that the user's month (for bandwidth limits) begins again from today.
<br />eg. A user signs up on the 5th of the month. That user's bandwidth usage is normally reset on the 5th of each month.
<br />If you "Reset Bandwidth" on the 20th of the month, and check the "Reset time period" box, that user will now have their
bandwidth usage reset on the 20th of each month, instead of the 5th.</i>
<br /><br />
<input type="submit" name="changestatus" value="Change Selected Users Status To" onClick="return confirm('Are you sure you want to change the status of the selected users?');" />
<select name="newstatus">
<?php
foreach( $statustexts as $s=>$v ){
	$sel = $s == $newstatus ? ' SELECTED ' : '';
	echo "<option value=\"$s\"$sel>$v</option>\n";
}
?>
</select>
<br /><br />
<input type="submit" name="delete" value="Delete Selected Users" onClick="return confirm('Are you sure you want to delete all the selected users and their images?');" />
</form>
<br />
View Page:
<?php
	for( $i = 1; $i <= $numpages; $i++){
?>
<a href="<?php echo str_replace('{page}', $i,$pageurl);?>"><?php echo $i;?></a>
<?php
	}
?>
<br /><br />
Page <?php echo $page.' of '.$numpages.'.';?><br /><br />
Listing users <?php echo ($first+1).' to '.$last.' of '.$numusers.'.';?>
<?php
}

}else{	// edit user
?>
<form action="users.php" method="post">
<input type="hidden" name="userid" value="<?php echo $userid;?>" />
<table align="center" class="formtable" cellspacing="1" cellpadding="0">
	<tr>
		<td colspan="2" class="formheading"><?php echo $user == 0 ? 'Add New User' : 'Edit User "'.htmlspecialchars($user->username).'"';?></td>
	</tr>
	<tr>
		<td class="formlabel">Username:</td>
		<td class="formfield"><input type="text" class="forminput" name="fusername" value="<?php echo htmlspecialchars($fusername);?>" /></td>
	</tr>
	<tr>
		<td class="formlabel">Name:</td>
		<td class="formfield"><input type="text" class="forminput" name="fname" value="<?php echo htmlspecialchars($fname);?>" /></td>
	</tr>
	<tr>
		<td class="formlabel">Email:</td>	
		<td class="formfield"><input type="text" class="forminput" name="femail" value="<?php echo htmlspecialchars($femail);?>" /></td>
	</tr>
	<tr>
		<td class="formlabel">Password:</td>	
		<td class="formfield"><input type="text" class="forminput" name="fpassword" value="<?php echo htmlspecialchars($fpassword);?>" /></td>
	</tr>
	<tr>
		<td class="formlabel">Status:</td>
		<td class="formfield">
			<select name="fstatus">
<?php 
	foreach( $statustexts as $s => $v ){ 
		$sel = $s == $fstatus ? ' SELECTED ' : '';
		echo "<option value=\"$s\"$sel>$v</option>\n";
	}
?>
			</select>
		</td>
	</tr>
	<tr>
		<td class="formlabel">&nbsp;</td>
		<td class="formfield"><input type="submit" name="update" value="<?php echo $user == 0 ? 'Add New User' : 'Update User';?>" /></td>
	</tr>
<?php	if( $user != 0 ){?>
	<tr>
		<td class="formlabel">Joined On:</td>
		<td class="formfield"><?php echo date('l jS F Y @ H:i', strtotime($user->joined));?></td>
	</tr>
	<tr>
		<td class="formlabel">IP Address:</td>
		<td class="formfield"><?php echo $user->ip;?></td>
	</tr>
	<tr>
		<td class="formlabel">Last Logged In:</td>
		<td class="formfield"><?php echo date('l jS F Y @ H:i', strtotime($user->lastlogin));?></td>
	</tr>
	<tr>
		<td class="formlabel">IP Address:</td>
		<td class="formfield"><?php echo $user->loginip;?></td>
	</tr>
	<tr>
		<td class="formlabel">Uploaded Images:</td>
		<td class="formfield"><a href="images.php?checked=-1&username=<?php echo $user->username;?>"><?php echo $user->images;?></a></td>
	</tr>
	<tr>
		<td class="formlabel">Galleries:</td>
		<td class="formfield"><a href="galleries.php?username=<?php echo $user->username;?>"><?php echo count($user->galleries);?></a></td>
	</tr>
	<tr>
		<td class="formlabel">Account Type:</td>
		<td class="formfield"><?php echo ucfirst($user->account_type);?></td>
	</tr>
	<tr>
		<td class="formlabel">Bandwidth Used:</td>
		<td class="formfield"><?php echo $user->bandwidth_used.'mb of '.($user->bandwidth == 0 ? 'unlimited' : $user->bandwidth.'mb');?>.</td>
	</tr>
	<tr>
		<td class="formlabel">Storage Used:</td>
		<td class="formfield"><?php echo $user->storage_used.'mb of '.($user->storage == 0 ? 'unlimited' : $user->storage.'mb');?>.</td>
	</tr>
<?php }?>
</table>
</form>
<?php
}

?>
</td>
</tr>
</table>
</div>
<?php


require_once(dirname(__FILE__).'/includes/footer.inc.php');

?>
