PHP Classes

File: examples/extended-admin.php

Recommend this page to a friend!
  Classes of Tony   twzAuth   examples/extended-admin.php   Download  
File: examples/extended-admin.php
Role: Example script
Content type: text/plain
Description: example script
Class: twzAuth
Authenticate users using HTTP Basic authentication
Author: By
Last change:
Date: 9 years ago
Size: 4,898 bytes
 

Contents

Class file image Download
<?php //>
/*
    Example - basic admin page for twzAuth "extended" mode
    ------------------------------------------------------

    This script allows admin users to add / delete users, and change their passwords.
   
    Enter a username (and optionally a password) in the text box, then select action:
        User info .... displays all settings for the specified username
        Add user ..... adds a new user with the specified username. If no password
                       is specified a random one will be generated.
        Edit user .... changes the password for the specified user. If no password
                       is specified a random one will be generated.
        Delete user .. deletes the specified user.
                       NOTE: nothing stops you from deleting your own username,
                       but ->setAdmin() in the include file will re-create it
                       with the original password
   
    To use custom fields, you will need to add the appropriate form fields and POST
    checks, and call addUser() / editUser() with all relevant fields.
*/


// clumsy "logout" (for testing only)..
if(isset($_POST['logout'])) { unset($_SERVER['PHP_AUTH_USER']); }


require
'extended-include.php';

// must be admin..
if(!$auth->isAdmin())
    {
header('location:./'); }

$Message=''; $Error='';

// check POST..
if($_POST)
    {
   
$TheUser=(isset($_POST['username'])) ? trim($_POST['username']) : '';
   
$ThePassword=(isset($_POST['password'])) ? trim($_POST['password']) : '';
   
    if(isset(
$_POST['edit']))
        {
        if(
$auth->editUser($TheUser, $ThePassword))
            {
$Message='Password for '.$TheUser.' changed to '.$auth->newPassword();}
        }
    elseif(isset(
$_POST['add']))
        {
        if(
$auth->addUser($TheUser, $ThePassword))
            {
$Message='User '.$TheUser.' added with password '.$auth->newPassword(); }
        }
    elseif(isset(
$_POST['delete']))
        {
        if(
$auth->deleteUser($TheUser))
            {
$Message='User '.$TheUser.' deleted'; }
        }
    elseif(isset(
$_POST['info']))
        {
       
$Info = $auth->getUserInfo($TheUser);
        if(
$Info)
            {
           
$Info['Added']=date('j M Y H:i:s', $Info['Added']);
           
$Info['Updated']=date('j M Y H:i:s', $Info['Updated']);
            echo
'<pre>'; print_r($Info); echo '</pre>';
            }
       
        }
    }

$Error=$auth->lastError();

?><!DOCTYPE HTML>
<html lang="en">
<head>
    <title>twzAuth (extended mode) admin</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <style>
    div#users ul { list-style:none; margin:0; }
    div#users ul li { float:left; margin:2px 10px; padding:1px 5px; background:#ddd; }
    form, div#users { float:left; clear:left; border:1px solid #ccc; padding:10px; margin-top:20px; width:450px; }
    form + * { clear:left; margin-top:50px; }
    div#buttons { text-align:right; padding-top:15px; }
    input[type=text] { width:130px; }
    div#msg { color:#084; font-weight:bold; } div#err { color:#d60; font-weight:bold; }
    </style>
</head>
<body>

<h2>Welcome <?php echo $Username; ?></h2>

<?php

if($Error) { echo '<div id="err">'.$Error.'</div>'; }
if(
$Message) { echo '<div id="msg">'.$Message.'</div>'; }

$UserList=$auth->getUsernames();
if(
$UserList)
    {
    echo
'<div id="users">Current usernames:';
    echo
'<ul>';
    foreach(
$UserList as $User)
        { echo
'<li>'.$User.'</li>'; }
    echo
'</ul></div>';
    }
?>

<form method="post">
    <label for="username">Username</label> <input type="text" id="username" name="username" value="" />
    <label for="password">Password</label> <input type="text" id="password" name="password" value="" />
    <div id="buttons">
        <input type="submit" name="logout" value="&quot;logout&quot;" style="float:left;" />
        <input type="submit" name="info" value="User info" />
        <input type="submit" name="add" value="Add user" />
        <input type="submit" name="edit" value="Edit user" />
        <input type="submit" name="delete" value="Delete user" onclick="return confirm('Are you really sure?');" />
    </div>
</form>
<p>&nbsp;</p>

<?php

if(isset($_GET['showlist']) and 'yes'==$_GET['showlist'])
    {
    echo
'<p><a href="?showlist=no">Hide user info</a></p>';
    echo
'<p>All user info:</p>';

   
$usr=$auth->getUsers();
    foreach(
$usr as $idx=>$info)
        {
       
$usr[$idx]['Added']=date('j M Y H:i:s', $info['Added']);
       
$usr[$idx]['Updated']=date('j M Y H:i:s', $info['Updated']);
        }
    echo
'<pre>'; print_r($usr); echo '</pre>';
    }
else
    { echo
'<p><a href="?showlist=yes">Show all user info</a></p>'; }

?>
<p><a href="./">home</a></p>

</body>
</html>