<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

  // DEV NOTE: this script is available at X:\support\test_scripts\show_user_accounts.php, but lives in CVS at C:\wamp\www\sb\CMS Builder\cmsb\show_user_accounts.php

  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  require_once "lib/viewer_functions.php";

  if (!function_exists('getPasswordDigest')) {
    require_once "lib/login_functions.php";
  }

  if (!function_exists('_mysql_getMysqlSetValues')) {
    function _mysql_getMysqlSetValues($columnsToValues) {
      $mysqlSet = '';

      if (is_array($columnsToValues)) {
        foreach ($columnsToValues as $column => $value) {
          list($column, $dontEscapeValue) = extractSuffixChar($column, '=');

          if (!preg_match('/^(\w+)$/', $column)) { die(__FUNCTION__. ": Invalid column name '" .htmlspecialchars($column, ENT_QUOTES, 'UTF-8'). "'!"); } // error checking: whitelist column chars to prevent sql injection

          if ($dontEscapeValue) { $mysqlSet .= "`$column` = $value, "; }
          else                  { $mysqlSet .= "`$column` = '" . mysql_escape($value) . "', "; }
        }
      }

      //
      $mysqlSet = chop($mysqlSet, ', ');

      return $mysqlSet;
    }
  }
  if (!function_exists('extractSuffixChar')) {
    function extractSuffixChar($string, $acceptableSuffixChars) {
      if (!strlen($string)) { return array($string, null); }
      $suffixChar = substr($string, -1);
      if (strpos($acceptableSuffixChars, $suffixChar) === FALSE) {
        return array($string, null);
      }
      $newString = substr($string, 0, -1);
      return array($newString, $suffixChar);
    }
  }
  // limit by IP
  $allowedIPs = array('0.0.0.0', '184.71.180.122', '127.0.0.1', '192.168.1.99');
  if (!in_array($_SERVER['REMOTE_ADDR'], $allowedIPs)) {
    die("Sorry, you are not allowed to access this program from {$_SERVER['REMOTE_ADDR']}");
  }

  //
  if (@$_REQUEST['createAccount']) {
    $username = @$_REQUEST['newUsername'];
    $password = @$_REQUEST['newPassword'];
    if ($isEncrypted) { $password = getPasswordDigest($password); }

    if (mysql_count('accounts', "username = '".mysql_escape($username)."'")) { die("username must be unique"); }

    mysqlStrictMode(FALSE);
    mysql_insert('accounts', array(
      'username'         => $username,
      'password'         => $password,
      'fullname'         => $username,
      'email'            => $username . '@example.com',
      'isAdmin'          => '1',
      'disabled'         => '0',
      'expiresDate'      => '0000-00-00 00:00:00',
      'neverExpires'     => '1',
      'createdDate'      => mysql_datetime(),
      'updatedDate'      => mysql_datetime(),
      'createdByUserNum' => '0',
      'updatedByUserNum' => '0',
    ));
    $userNum = mysql_insert_id();
    mysql_insert('_accesslist', array(
      'userNum'      => $userNum,
      'tableName'    => 'all',
      'accessLevel'  => '9',
      'maxRecords='  => 'NULL',
      'randomSaveId' => '1234567890',
    ));
    mysqlStrictMode(TRUE);
  }

  //
  list($accountsRecords, $accountsMetaData) = getRecords(array(
    'tableName'   => 'accounts',
    'where'       => 'isAdmin = 1',
    'allowSearch' => FALSE,
  ));

?>
<!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></title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <style type="text/css">
    body          { font-family: arial; }
    .instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
  </style>
 </head>
<body>

<form action="?" method="post">
<input type="hidden" name="createAccount" value="1" />
<table border="1" cellspacing="1" cellpadding="1">
<tr>
    <td><b>Username</b></td>
    <td><b>Password</b></td>
    <td><b>Admin</b></td>
    <td><b>Action</b></td>
</tr>

<tr>
    <td><input type="text" name="newUsername" /></td>
    <td><input type="text" name="newPassword" /></td>
    <td>Admin</td>
    <td><input type="submit" value="Add" /></td>
</tr>

    <?php foreach ($accountsRecords as $record): ?>
<tr>
    <td><?php echo $record['username'] ?></td>
    <td><?php echo $record['password'] ?></td>
    <td><?php echo $record['isAdmin'] ? 'Admin' : '' ?></td>
    <td align="center">
      <?php if (true): // replace this with an encrypted password check if logging in with encrypted password as plaintext doesn't work in future ?>
        <a href="#" onclick="doLogin(<?php echo htmlspecialchars(json_encode(array($record['username'], $record['password'])), ENT_QUOTES, 'UTF-8') ?>); return false;">login</a>
      <?php endif ?>
    </td>
</tr>
    <?php endforeach; ?>
</table>
</form>

<form id="loginForm" action="admin.php" method="post">
<input type="hidden" name="action" value="loginSubmit" />
<input type="hidden" name="login" value="Login" />
<input type="hidden" name="username" value="" />
<input type="hidden" name="password" value="" />
</form>

<script type='text/javascript'>
function doLogin(args) {
  (document.getElementsByName('username'))[0].setAttribute('value', args[0]);
  (document.getElementsByName('password'))[0].setAttribute('value', args[1]);
  document.getElementById('loginForm').submit();
}
</script>

</body>
</html>
