<?php

/**
 * Copyright (C) DairyWindow 2019
 * admin@dairywindow.nz
 */

/* (C) DairyWindow 2012-2017 */

require "inc/connect.php";

$change = false;

$auth_link = new mysqli($server, $authusername, $authpassword, $authdb);
if (!$auth_link)
	die($error_msg);

if (isset($_POST['save'])) {

	$userFirst = clean($_POST['userFirst']);
	$userLast = clean($_POST['userLast']);
	$userEmail = clean($_POST['userEmail']);
	$userLevel = clean($_POST['userLevel']);
	$userTitle = clean($_POST['userTitle']);

	if (isset($_POST['userCOA']) && $_POST['userCOA'] == 1)
		$userCOA = 1;
	else
		$userCOA = 0;

	if (isset($_POST['userRelease']) && $_POST['userRelease'] == 1)
		$userRelease = 1;
	else
		$userRelease = 0;

	if (isset($_POST['userID']) && $_POST['userID'] != "" && $_POST['userID'] != null && $_POST['userID'] != 0) {
		//edit existing user

		$userID = clean($_POST['userID']);
		if ($debug)
			echo 'userID: ' . $userID;

		if (isset($_POST['userActive']) && $_POST['userActive'] == 1)
			$userActive = 1;
		else
			$userActive = 0;

		$change = false;

		if (isset($_POST['userPassword']) && $_POST['userPassword'] != '') {
			$userPassword = password_hash($_POST['userPassword'], PASSWORD_DEFAULT);
			if ($debug)
				echo 'userPassword is set';
			$sql = "
					# Update user, password is set
					Update dw_auth set
						userFirst    = ?,
						userEmail    = ?,
						userPassword = ?,
						userActive   = ?
					Where userID   = ?
				";
			$change = true;
			$types = 'sssii';
			$array = array(
				$userFirst,
				$userEmail,
				$userPassword,
				$userActive,
				$userID
			);
			if (!savesql($auth_link, $sql, $types, $array, __FILE__, __LINE__))
				reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			if ($debug)
				echo $sql . '<br>';

			$sql = "
					# Update user, password is set
					Update dw_user set
						userLevel    = ?,
						userFirst    = ?,
						userLast     = ?,
						userTitle    = ?,
						userCOA      = ?,
						userRelease  = ?
					Where userID     = ?
				";
			$change = true;
			$types = 'isssiii';
			$array = array(
				$userLevel,
				$userFirst,
				$userLast,
				$userTitle,
				$userCOA,
				$userRelease,
				$userID
			);
			if (!savesql($link, $sql, $types, $array, __FILE__, __LINE__))
				reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			if ($debug)
				echo $sql . '<br>';
		} else {
			//need to check for duplicates
			//check email, count should be 0 or 1
			$sql = "
					Select userID
					From dw_auth
					Where userEmail = ?
				";
			if (!$qry = mysqli_prepared_query($auth_link, $sql, "s", array($userEmail), __FILE__, __LINE__))
				reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			if ($debug)
				echo $sql . '<br>' . mysqli_prepared_num_rows($qry) . ' rows<br>';

			switch (mysqli_prepared_num_rows($qry)) {
				case 0:
					//user is changing email to a new one
					$sql = "
							# Update auth, no password
							Update dw_auth set
								userFirst   = ?,
								userEmail   = ?,
								userActive  = ?
							Where userID  = ?
						";
					$types = 'ssii';
					$values = array(
						$userFirst,
						$userEmail,
						$userActive,
						$userID
					);
					if (!savesql($auth_link, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					if ($debug)
						echo $sql . '<br>';

					$sql = "
							# Update user, no password
							Update dw_user set
								userLevel   = ?,
								userFirst   = ?,
								userLast    = ?,
								userTitle   = ?,
								userCOA     = ?,
								userDate    = ?,
								userRelease = ?
							Where userID    = ?
						";
					$types = 'isssisii';
					$values = array(
						$userLevel,
						$userFirst,
						$userLast,
						$userTitle,
						$userCOA,
						date('Y-m-d'),
						$userRelease,
						$userID
					);
					if (!savesql($link, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					if ($debug)
						echo $sql . '<br>';
					$change = true;
					break;
				case 1:
					//normal situation, email already exists, check if userid is ok
					if (!isset($_SESSION['userID']))
						reporterror("userID is not set", 'none', $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					$sql = "
							# Update auth, no password
							Update dw_auth set
								userFirst  = ?,
								userEmail  = ?,
								userActive = ?
							Where userID   = ?
						";
					$types = 'ssii';
					$values = array(
						$userFirst,
						$userEmail,
						$userActive,
						$userID
					);
					if (!savesql($auth_link, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					if ($debug)
						echo $sql . '<br>';
					$sql = "
							# Update user, no password
							Update dw_user set
								userLevel   = ?,
								userFirst   = ?,
								userLast    = ?,
								userTitle   = ?,
								userCOA     = ?,
								userDate    = ?,
								userRelease = ?
							Where userID    = ?
						";
					$types = 'isssisii';
					$values = array(
						$userLevel,
						$userFirst,
						$userLast,
						$userTitle,
						$userCOA,
						date('Y-m-d'),
						$userRelease,
						$userID
					);
					if (!savesql($link, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					if ($debug)
						echo $sql . '<br>';
					$change = true;
					break;
				default:
					$message['type'] = "error";
					$message['text'] = $userEmail . " already exists. " . $userFirst . " " . $userLast . "'s details were not updated";
			}
		}
		if ($change) {
			$message['type'] = "ok";
			$message['text'] = $userFirst . " " . $userLast . "'s details have been updated";

			if (!$debug) {
				//inform user
				$body = 'Your profile was changed in DairyWindow by ' . $_SESSION['userFirst'] . ' ' . $_SESSION['userLast'] . ' at ' . date('D d M y H:m') . ' by IP address ' . $_SERVER['REMOTE_ADDR'];
				if (isset($_POST['userPassword'])) {
					$body .= '<br>Your password is now <strong>' . $_POST['userPassword'] . '</strong><br>
					Don\'t forget that passwords are cAsE sEnSiTiVe. Ensure you copy and paste <u>the whole thing</u>.<br>
					<br>If you didn\'t make this change, <a href="mailto:admin@dairywindow.nz" target="_blank">please let us know</a>.';
				}
				if (email($userEmail, 'DW profile change', $body))
					$message['text'] .= ", and an email was sent";

				if (isset($phone) && $phone != "" && $phone != null && isset($_POST['userPassword'])) {
					require "inc/sms.php";
					$text = substr("Your DW profile has changed. Your password is now '" . $_POST['userPassword'] . "'. If you didnt make this change, please contact admin@dairywindow.nz immediately", 0, 159);
					sms($text, $phone);
				}
			}
		}
	} else {
		//add new user

		$sql = "
				# need to check that the user doesnt already exist
				Select userID
				From dw_auth
				Where userEmail = ?
			";
		if (!$qry = mysqli_prepared_query($auth_link, $sql, "s", array($userEmail), __FILE__, __LINE__))
			reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
		if (mysqli_prepared_num_rows($qry) > 0) {
			$message['type'] = "error";
			$message['text'] = "Sorry but user with email '" . $userEmail . "' already exists";
		} else {
			$userPassword = password_hash($_POST['userPassword'], PASSWORD_DEFAULT);

			$sql = "#insert into auth table
					Insert Into dw_auth ( userFirst,  userEmail,  userPassword, userActive,            userSite  )
					Values              (     ?    ,      ?    ,      ?       ,     ?     ,                ?     )";
			$types = '     s           s           s             i                      i      ';
			$values = array($userFirst, $userEmail, $userPassword, 1, $_SESSION['userSite']);
			if (!$userID = savesql($auth_link, $sql, $types, $values, __FILE__, __LINE__))
				reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			if ($debug)
				echo '<br><br>';

			$sql = "#insert into normal table
							Insert Into dw_user ( userID,  userLevel,  userLast,  userTitle,  userCOA, userDate,  userOOS,  userStyle,  userRelease)
							Values              (     ? ,      ?    ,      ?   ,      ?    ,      ?  ,     ?   ,      ?  ,      ?    ,      ?      )";
			$types = '                                i        i           s          s           i        s          s         i           i       ';
			$values = array($userID, $userLevel, $userLast, $userTitle, $userCOA, date('Y-m-d'), 1, 1, $userRelease);

			$userodl = false;
			switch ($_SESSION['userSite']) {
				case 4: //odl
					$userodl = true;
					$link7 = new mysqli($server, $dbusername, $dbpassword, 'dw_7');
					if (!savesql($link7, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);

					$link8 = new mysqli($server, $dbusername, $dbpassword, 'dw_8');
					if (!savesql($link8, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					break;

					$link13 = new mysqli($server, $dbusername, $dbpassword, 'dw_13');
					if (!savesql($link13, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					break;

				case 7: //uht
					$userodl = true;
					$link4 = new mysqli($server, $dbusername, $dbpassword, 'dw_4');
					if (!savesql($link4, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);

					$link8 = new mysqli($server, $dbusername, $dbpassword, 'dw_8');
					if (!savesql($link8, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					break;

					$link13 = new mysqli($server, $dbusername, $dbpassword, 'dw_13');
					if (!savesql($link13, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					break;

				case 8: //c&b
					$userodl = true;
					$link4 = new mysqli($server, $dbusername, $dbpassword, 'dw_4');
					if (!savesql($link4, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);

					$link7 = new mysqli($server, $dbusername, $dbpassword, 'dw_7');
					if (!savesql($link7, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					break;

					$link13 = new mysqli($server, $dbusername, $dbpassword, 'dw_13');
					if (!savesql($link13, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					break;

				case 13: //m&s
					$userodl = true;
					$link4 = new mysqli($server, $dbusername, $dbpassword, 'dw_4');
					if (!savesql($link4, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);

					$link7 = new mysqli($server, $dbusername, $dbpassword, 'dw_7');
					if (!savesql($link7, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					break;

					$link8 = new mysqli($server, $dbusername, $dbpassword, 'dw_8');
					if (!savesql($link8, $sql, $types, $values, __FILE__, __LINE__))
						reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
					break;
			}

			if (!savesql($link, $sql, $types, $values, __FILE__, __LINE__))
				reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);

			$message['type'] = "ok";
			$message['text'] = $userFirst . " " . $userLast . "'s profile was created, you will need to send an email to inform them";

			//inform user
			$body = "Your profile has been created in DairyWindow.<br><br>
					Your username is <strong>" . $userEmail . "</strong><br>
					Your password is <strong>" . $_POST['userPassword'] . "</strong><br>
					The address for DairyWindow is <a href='https://www.dairywindow.nz/home.php'>https://www.dairywindow.nz/home.php</a><br>
					If you need any help, please email <a href='mailto:admin@dairywindow.nz'>admin@dairywindow.nz</a>";

			if (email($userEmail, 'DairyWindow profile', $body)) {
				if ($userodl) {
					$message['text'] = $userFirst . " " . $userLast . "'s profile was created in all 3 plants, and an email was sent to inform them";
				} else {
					$message['text'] = $userFirst . " " . $userLast . "'s profile was created, and an email was sent to inform them";
				}
			} else {
				$message['text'] = $userFirst . " " . $userLast . "'s profile was created, but an email could NOT be sent to inform them. Is the email address correct?";
			}
		}
	}
}

$title = "Users";
require "inc/head.php";
$menu = "user";
require "inc/menu.php";

if ($debug)
	showdebug();

?>

<div id="topDIV" class="page-header">
	<h1 class="text-center">
		<?php
		echo $title;
		if ($_SESSION['userLevel'] == 1) {
			echo "&nbsp;<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#edituser' onclick='newuser();' ><span class='glyphicon glyphicon-plus' aria-hidden='true'></span>&nbsp;Create new</button>";
		}
		?>
	</h1>
</div>

<div id="main" class="container" role="main">
	<table class="table table-hover">
		<thead>
			<tr id='table-head'>
				<th id='th_userName'>Name</th>
				<th id='th_userTitle'>Title/Position</th>
				<th id='th_userLevel'>Level</th>
				<th id='th_userCOA'>Sign COA's</th>
				<th style="display:none;">ID</th>
				<th style="display:none;">First name</th>
				<th style="display:none;">Last name</th>
				<th style="display:none;">Title</th>
				<th style="display:none;">Email</th>
				<th style="display:none;">Phone</th>
				<th style="display:none;">Level</th>
				<th style="display:none;">COA</th>
				<th style="display:none;">Active</th>
				<th style="display:none;">Release</th>
			</tr>
		</thead>
		<tbody>
			<?php
			$sql = "
				Select *
				From dw_auth
				Where userID > 1 and (userSite = ? or userSite = ? or userSite = ? or userSite = ?)
				Order by userActive desc, userFirst
			";
			$site1 = $_SESSION['userSite'];
			$site2 = $_SESSION['userSite'];
			$site3 = $_SESSION['userSite'];
			$site4 = $_SESSION['userSite'];
			if ($_SESSION['userSite'] == 4 || $_SESSION['userSite'] == 7 || $_SESSION['userSite'] == 8 || $_SESSION['userSite'] == 13) {
				$site1 = 4;
				$site2 = 7;
				$site3 = 8;
				$site4 = 13;
			}
			if (!$qry = mysqli_prepared_query($auth_link, $sql, "iiii", array($site1, $site2, $site3, $site4), __FILE__, __LINE__))
				reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
			foreach ($qry as $array) {
				if ($array['userActive'] == 0 || $array['userActive'] == '0') {
					//false
					$active = "line-through";
				} else {
					//true
					$active = "none";
				}
				echo "<tr ";
				if ($array['userID'] == $_SESSION['userID']) {
					echo "onclick=\"window.location='profile.php';\"";
				} else {
					if ($_SESSION['userLevel'] <= $_SESSION['admin_user_edit'])
						echo "data-toggle='modal' data-target='#edituser' onclick='updateuser(" . $array['userID'] . ",";
					if ($debug)
						echo "true";
					else
						echo "false";
					echo ");'";
				}
				echo ">
						<td id='userName" . $array['userID'] . "' style='text-decoration:" . $active . "'>" . $array['userFirst'] . "</td>
						<td id='userTitle" . $array['userID'] . "' style='text-decoration:" . $active . "'></td>
						<td id='userLevel" . $array['userID'] . "' style='text-decoration:" . $active . "'></td>
						<td id='userCOA" . $array['userID'] . "' style='text-decoration:" . $active . "'></td>
						<td ";
				if (!$debug)
					echo "style='display:none;'";
				echo " id='userID_" . $array['userID'] . "' style='text-decoration:" . $active . "'>" . $array['userID'] . "</td>
						<td style='display:none;' id='userFirst_" . $array['userID'] . "' style='text-decoration:" . $active . "'>" . $array['userFirst'] . "</td>
						<td style='display:none;' id='userLast_" . $array['userID'] . "' style='text-decoration:" . $active . "'></td>
						<td style='display:none;' id='userTitle_" . $array['userID'] . "' style='text-decoration:" . $active . "'></td>
						<td style='display:none;' id='userEmail_" . $array['userID'] . "' style='text-decoration:" . $active . "'>" . $array['userEmail'] . "</td>
						<td style='display:none;' id='userLevel_" . $array['userID'] . "' style='text-decoration:" . $active . "'></td>
						<td style='display:none;' id='userActive_" . $array['userID'] . "' style='text-decoration:" . $active . "'>" . $array['userActive'] . "</td>
						<td style='display:none;' id='userRelease_" . $array['userID'] . "' style='text-decoration:" . $active . "'></td>
					</tr>
					<script>
						$.get('ajax/get_user_info.php', { id: " . $array['userID'] . " } )
					.done(function( user ) {
						$(function() {
							//user is json format
							var json = JSON.parse(user);
							var thisuser = json[0];
							";
				if ($debug)
					echo "console.log(thisuser);";
				echo "
							$('#userName" . $array['userID'] . "').html($('#userName" . $array['userID'] . "').html()+' '+thisuser.userLast);
							$('#userTitle" . $array['userID'] . "').html(thisuser.userTitle);
							var userLevel = 'Unknown';
							switch(thisuser.userLevel){
							case 1: case '1':
								userLevel = 'Administrator';
								break;
							case 2: case '2':
								userLevel = 'Manager';
								break;
							case 3: case '3':
								userLevel = 'Supervisor';
								break;
							case 4: case '4':
								userLevel = 'Operator';
								break;
							case 5: case '5':
								userLevel = 'View only';
								break;
							}
							$('#userLevel" . $array['userID'] . "').html(userLevel);
							var userCOA = '';
							if(thisuser.userCOA == 1) userCOA = '<span class=\"glyphicon glyphicon-ok\" aria-hidden=\"true\"></span>';
							$('#userCOA" . $array['userID'] . "').html(userCOA);
							$('#userID_" . $array['userID'] . "').html(thisuser.userID);
							$('#userLast_" . $array['userID'] . "').html(thisuser.userLast);
							$('#userTitle_" . $array['userID'] . "').html(thisuser.userTitle);
							$('#userEmail_" . $array['userID'] . "').html(thisuser.userEmail);
							$('#userLevel_" . $array['userID'] . "').html(thisuser.userLevel);
							$('#userCOA_" . $array['userID'] . "').html(thisuser.userCOA);
							$('#userActive_" . $array['userID'] . "').html(thisuser.userActive);
							$('#userRelease_" . $array['userID'] . "').html(thisuser.userRelease);
						});
					})
					.fail(function(){
						alert('Error getting user details for user ID " . $array['userID'] . "');
					});
					</script>
				";
			}
			?>
		</tbody>
	</table>
</div>

<div class="modal fade" id="edituser" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<form method="post">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
							aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="myModalLabel">Edit existing user</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="userFirst" class="control-label">Name</label>
						<div class="row">
							<div class="col-sm-6">
								<input type="text" class="form-control" name="userFirst" id="userFirst" maxlength="45"
									placeholder="Given name" autocomplete="new-password" required>
							</div>
							<div class="col-sm-6">
								<input type="text" class="form-control" name="userLast" id="userLast" maxlength="45"
									placeholder="Family name" autocomplete="new-password" required>
							</div>
						</div>
					</div>
					<div class="form-group">
						<label for="userTitle">Title/Position</label>
						<input type="text" class="form-control" id="userTitle" name="userTitle"
							placeholder="Title or position" autocomplete="new-password" required>
					</div>
					<div class="form-group">
						<label for="userEmail">Email address</label>
						<div class="input-group">
							<div class="input-group-addon"><span class="glyphicon glyphicon-envelope"
									aria-hidden="true"></span></div>
							<input type="email" class="form-control" id="userEmail" name="userEmail"
								placeholder="Email address" autocomplete="new-password" required>
							<input type="hidden" class="form-control" id="userEmailOld" name="userEmailOld" required>
						</div>
					</div>
					<div class="form-group">
						<label for="userLevel">Level</label>
						<select name="userLevel" id="userLevel" class="form-control">
							<option value="1">Administrator</option>
							<option value="2">Manager</option>
							<option value="3">Supervisor</option>
							<option value="4">Operator</option>
							<option value="5" selected>View only</option>
						</select>
					</div>
					<div class="form-group">
						<label for="userCOA">Generate COA</label>
						<input type="checkbox" data-toggle="toggle" data-on="Yes" data-off="No" data-width="50"
							name="userCOA" id="userCOA" value="1">
					</div>
					<div class="form-group" id='status'>
						<label for="userActive">Status</label>
						<input type="checkbox" data-toggle="toggle" data-on="Active" data-off="Disabled"
							data-width="100" name="userActive" id="userActive" value="1">
					</div>
					<div class="form-group" id='release'>
						<label for="userRelease">Release results</label>
						<input type="checkbox" data-toggle="toggle" data-on="Yes" data-off="No" data-width="50"
							name="userRelease" id="userRelease" value="1">
					</div>
					<div class="form-group">
						<label for="userPassword" id='pwdlabel'>NEW password</label>
						<input type="password" class="form-control" pattern=".{8,}" id="userPassword"
							name="userPassword" placeholder="Only required if changing password" value=""
							autocomplete="new-password">
					</div>
					<!--
					<div class="form-group">
						<label >Are you human?</label>
						<div class="g-recaptcha" data-sitekey="6LfBrhETAAAAACUcVEml8nJ7O8a_zn3bvoGruNz9"></div>
					</div>
					-->
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal"><span
							class='glyphicon glyphicon-remove' aria-hidden='true'></span>&nbsp;Cancel</button>
					<input type="hidden" id="userID" name="userID">
					<button type="submit" name="save" id="save" class="btn btn-primary"><span
							class='glyphicon glyphicon-save' aria-hidden='true'></span>&nbsp;Save</button>
				</div>
			</form>
		</div>
	</div>
</div>

<script src="js/user.js?v=2.2"></script>

<?php
if (isset($_GET['u']) && $_GET['u'] != "" && $_GET['u'] != null && !isset($_POST['userID'])) {
	$sql = "# see if the user actually exists
			Select dw_user.userID
			From dw_user
			Where dw_user.userID = ?
		";
	if (!$qryuserid = mysqli_prepared_query($link, $sql, 'i', array($_GET['u']), __FILE__, __LINE__))
		reporterror(get_error(), $sql, $_SERVER['REQUEST_URI'], __FILE__, __LINE__);
	$count_users = mysqli_prepared_num_rows($qryuserid);
	if ($count_users == 1) {
		echo "
				<script>
					$('#edituser').modal('show');
					updateuser(" . clean($_GET['u']) . ", " . ($debug ? "1" : "0") . ");
				</script>
			";
	}
}
require "inc/footer.php";
