Wikka : ExpandedAdminBackupTool

HomePage :: Categories :: Index :: Changes :: Comments :: Documentation :: Blog :: Login/Register

Expanded Administration and Backup Tool


This tool was designed to meet some local needs but has features useful to most admin (the backup of an image directory won't be useful until I upload the tools that are to generate indivdual user directories). This tool allows deleting or "hiding"/restoring individual revisions, hiding/restoring pages, resetting passwords, "reserving" user names, inactivating users, deleting users, backing up database tables (to both an external ftp server and locally), backing up image uploads (I have an upload tool that uploads to a user directory, not to a page directory, and it will back up these to an external ftp server), and, other than the graphic and exports directory, allows backing up the files in the wikka directory (both to an external ftp server and locally as a compressed file) thereby allowing backups of php code development easily.

The other "needs" for this script are listed in the file header (note that page.inc.php is available at the WikkaBlog2 page).

Install the script below in actions (I call it wikkamanagement.php). It will work without an ftp server (although you should put the variable references into the wikka.config.php, just leave the variable names blank) and local backups can be downloaded. [note that you have to replace "percentpercent" in the code with two percent signs]
<?
    /*     VERSION 1.0b (management of pages (hide, unhide, delete, delete history), users (inactivate, change upper
        and lower case), & backups (of tables (selectively), images (uploaded to graphics directory), & other files (both locally and to ftp server))

        @filename:        wikimanagement.php
        @author:         Andrew Somerville & GmBowen for a SSHRC research project
        @email:                      gmbowen@yahoo.com
        @date:            31 January 2006
        @license:        GPL
        @restrictions:     usage available to designated Administrator(s) only
        @description:     this file **REQUIRES** a pagination class file (page.inc.php) in the wikka root directory "scripts"
                To use the backup feature (to an external FTP server) one must modify the wikka.config.php with ftp login information
        @usage:         insert {{wikimanagement}} anywhere in a wikka wiki page.
        @note:         requires modifications of the pages table ("latest" field has to be changed to enum('Y','N','H','V') )
        @note:         requires added variables in wikka.config.php....

                    "db_backup_ftp_host" => "000.000.000.000",   //no http: or slashes (e.g. "put.the.file.here")
                    "db_backup_ftp_user" => "username",
                    "db_backup_ftp_pass" => "password",
   
                    "graphic_backup_ftp_host" => "000.000.000.000",  //no http: or slashes (e.g. "put.the.file.here")
                    "graphic_backup_ftp_user" => "username",
                    "graphic_backup_ftp_pass" => "password",
                    "graphic_directory" => "graphicdirectory", //directory in wikka root, permissions set to allow upload
                   
        @note:         graphic directory is for individual user upload directory (requires modified upload & insertimage code)
       
        @note:         a directory in wikka root called "exports" is needed (and should be writeable). It needs two writeable
                    subdirectories called "codebackup" and "dbbackup". An htaccess file with the content

                    Options -Indexes
                    order allow,deny
                    allow from all
                   
                    is necessary in the "exports" directory to stop browsing of those files (the SQL files can contain sensitive user info).

    */


# Include functions for doing pagination
include_once("scripts/page.inc.php");
define ('RECORDS_PER_PAGE','20');

$t_pages             = $this->config["table_prefix"]."pages";
$t_users             = $this->config["table_prefix"]."users";
$t_resetpasswords    = $this->config["table_prefix"]."resetpasswords";

$link    = $this->Href();
$can_use$this->IsAdmin();

if (!$can_use) {
    echo "Only the designated admin can use Wiki Management.";
} else {   
    $menu1 = $_REQUEST['menu1']?$_REQUEST['menu1']:'';
   
    echo "<h3>Wiki Management</h3>";
    $pre_link = $link;
    $this_link = $pre_link.'&menu1=pages';
    $page_name = 'Pages';
    echo ($menu1 != 'pages')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
            :'<font color="red"><b>'.$page_name.'</b></font>';
    echo ' | ';
    $this_link = $pre_link.'&menu1=users';
    $page_name = 'Users';
    echo ($menu1 != 'users')?'<a href="'.$this_link.'name.'</b></a>'
            :'
<font color="red"><b>'.$page_name.'</b></font>';
    echo '
| ';
    $this_link = $pre_link.'
&menu1=backup';
    $page_name = '
Backup';
    echo ($menu1 != '
backup')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
            :'
<font color="red"><b>'.$page_name.'</b></font>';
   
    ################################################################
    ################################################################
    # Pages
    ################################################################
    ################################################################
    if ($menu1 == '
pages') {
        $menu2     = $_REQUEST['
menu2']?$_REQUEST['menu2']:'';
       
        $pre_link  = $link.'
&menu1='.$menu1;
        echo '
<br />';
        $this_link = $pre_link.'
&menu2=manage';
        $page_name = '
Manage Pages';
        echo ($menu2 != '
manage')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'
<font color="red"><b>'.$page_name.'</b></font>';
        echo '
| ';
        $this_link = $pre_link.'
&menu2=hidden';
        $page_name = '
Hidden Pages';
        echo ($menu2 != '
hidden')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'
<font color="red"><b>'.$page_name.'</b></font>';
        echo '
| ';
        $this_link = $pre_link.'
&menu2=revisions';
        $page_name = '
Manage Revisions';
        echo ($menu2 != '
revisions')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'
<a href="'.$this_link.'"><font color="red"><b>'.$page_name.'</b></font></a>';
       
        /*
        echo '
| ';
        $this_link = $pre_link.'
&menu2=info';
        $page_name = '
Page Info';
        echo ($menu2 != '
info')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'
<font color="red"><b>'.$page_name.'</b></font>';
        */
        $this_link = $link.'
&menu1='.$menu1.'&menu2='.$menu2;
           
        ################################################################
        # Manage Pages
        ################################################################
        if ($menu2 == '
manage') {
            if ($_REQUEST['
set']) {
                $tag         = $_REQUEST['
tag'];
                $action      = $_REQUEST['
action'];
       
                $n = count($tag);
       
                for ($i = 0; $i < $n; $i++) {
                    $this_tag         = $tag[$i];
                    $this_action      = $action[$i];
                   
                    if ($this_action == '
delete_page_history') {
                        # Erase all records concerning page name.
                        $this->Query("DELETE FROM `$t_pages` WHERE `tag`='
".mysql_real_escape_string($this_tag)."'");
                    }
                    else if ($this_action == '
delete_history') {
                        # Remove all records from DB concerning that page where mostrecent is N.
                        $this->Query("DELETE FROM `$t_pages` WHERE `tag`='
".mysql_real_escape_string($this_tag)."' AND `latest`='N'");
                    }
                    else if ($this_action == '
hide_page') {
                        # Set most-recent to H on the current record
                        $this->Query("UPDATE `$t_pages` SET `latest`='
H' WHERE `tag`='".mysql_real_escape_string($this_tag)."' AND `latest`='Y'");
                    }
                   
                }
            }
           
            $cur_page       = $_REQUEST['
page'];
            $sort           = $_REQUEST['
sort'];
            $sort_order     = $_REQUEST['
sortorder'];
            $t_temp_pages   = $this->config["table_prefix"]."pages".rand(1,10000);
           
            $this->Query("CREATE TEMPORARY TABLE `$t_temp_pages` (`tag` varchar(75),`num_revisions` int) TYPE=MyISAM;");
           
            $pages = $this->LoadAll("SELECT `tag` FROM `$t_pages` WHERE `latest`='
Y'");
            foreach ($pages as $page) {
                $tag   = &$page['
tag'];
               
                $num_revisions = $this->LoadSingle("SELECT count(*) as `num_revisions` FROM `$t_pages` WHERE `tag`='
$tag'");
                $num_revisions = $num_revisions ['
num_revisions'];
               
                $this->Query("INSERT INTO `$t_temp_pages` (`tag`,`num_revisions`) VALUES ('
$tag','$num_revisions');");               
            }
           
            if      ($sort == '
tag')           $order = 'ORDER BY `tag`';
            else if ($sort == '
num_revisions') $order = 'ORDER BY `num_revisions`';
            else                               $order = '
ORDER BY `tag`';
           
            if      ($sort_order == '
DESC')  { $order .= ' DESC'; $sort_order = 'ASC' ; $sort_order2 = 'DESC'; }
            else                             { $order .= '
ASC' ; $sort_order = 'DESC'; $sort_order2 = 'ASC' ; }
           
            $query = "SELECT * FROM `$t_temp_pages` $order";
           
            $page = new Page();
            $total_records = count($this->LoadAll($query));
            $page->set_page_data($this_link.'
&sort='.$sort.'&sortorder='.$sort_order2,$total_records,RECORDS_PER_PAGE,5,true,true,true);
            ob_start();
            $page->get_page_nav();
            $page_nav = ob_get_contents();
            ob_end_clean();
           
            $pages = $this->LoadAll($page->get_limit_query($query));
           
            $this->Query("DROP TABLE `$t_temp_pages`;");
           
            $this_link2 = $this_link.'
&sort='.$sort.'&sortorder='.$sort_order2.'&page='.$cur_page;
       
            echo '
<H4>Page Management And Deletion</H4>';
            echo
                '
<form action="'.$this_link2.'" method="POST">'.
                '
<table border="0" callspacing="0" cellpadding="0">'.
                '
<tr>'.
                    '
<td colspan="5" >'.
                        '
<center>'.$page_nav.'</center>'.
                    '
</td>'.
                '
</tr>'.
                '
<tr bgcolor="#DDDDDD">'.
                    '
<th width="200">&nbsp;<a href="'.$this_link.'&sort=tag&sortorder='.$sort_order.'&page='.$cur_page.'">Page Name:</a>&nbsp;</th>'.
                    '
<th nowrap>&nbsp;Do Nothing&nbsp;</th>'.
                    '
<th nowrap>&nbsp;Hide Page:&nbsp;</th>'.
                    '
<th nowrap>&nbsp;<a href="'.$this_link.'&sort=num_revisions&sortorder='.$sort_order.'&page='.$cur_page.'">Erase History:</a>&nbsp;</th>'.
                    '
<th nowrap>&nbsp;Erase Page and History:&nbsp;</th>'.
       
                '
</tr>';
           
            $i = 0;
            foreach ($pages as $page) {
                $tag = &$page['
tag'];
                $num_revisions = &$page['
num_revisions'];
                $color = ($i%2)?"#ffffff":"#f5f6f7";
                echo
                    '
<tr bgcolor="'.$color.'">'.
                        '
<td>&nbsp;'.$this->Link($tag).'&nbsp;</td>'.
                        '
<td align="center">'.
                            '
<input type="hidden" name="tag['.$i.']"    value="'.$tag.'" />'.
                            '
<input type="radio"  name="action['.$i.']" value="no_action" checked />'.
                        '
</td>'.
                        '
<td align="center">'.
                            '
<input type="radio" name="action['.$i.']" value="hide_page" />'.
                        '
</td>'.
                        '
<td>&nbsp;&nbsp;'.
                            '
<input type="radio" name="action['.$i.']" value="delete_history" />'.
                            '
['.$num_revisions.' Rev'.(($num_revisions > 1)?'\'s':'').']'.
                        '</td>'.
                        '<td align="center">'.
                            '<input type="radio" name="action['.$i.']" value="delete_page_history" />'.
                        '</td>'.
       
                    '</tr>';
                $i++;
            }
               
            echo
                '</table>'.
                '<input type="submit" value="Carry Out Selected Actions" onclick="return confirm(\'Are you sure you want to carry out the selected actions?\')">'.
                '<input type="hidden" name="set" value="1">'.
                '</form>';
       
        }
       
           
        ################################################################
        # Hidden
        ################################################################
        else if ($menu2 == 'hidden') {
            $unhide     = $_REQUEST['unhide'];
           
            if ($unhide) {
                $unhidename = $_REQUEST['unhidename'];
                $this->query("UPDATE ".$this->config['table_prefix']."pages SET `latest`='Y' WHERE tag='".mysql_real_escape_string($unhidename)."' AND latest = 'H' ");
            }
           
            $count = 0;
            $query = "SELECT tag,owner,latest,time FROM `$t_pages` WHERE `latest`='H' ORDER BY id asc";
            $result = mysql_query($query);
            echo '<h4>Hidden Pages</h4>';
            echo "<TABLE width='650' border='0' cellpadding='0' cellspacing='2'>";
            ?>
            <TR bgcolor="#dddddd">
                <Th>&nbsp;#&nbsp;</Th>
                <Th>&nbsp;Page Name&nbsp;</Th>
                <Th>&nbsp;Page Owner&nbsp;</Th>
                <Th>&nbsp;Time Hidden&nbsp;</Th>
                <Th>&nbsp;Un-Hide&nbsp;</Th>
            </TR>
            <?
            while($row = mysql_fetch_array($result)) {
                $count++;
                $unhidepagelnk = $this_link.'&unhide=1'.'&unhidename='.$row['tag'];
                echo
                    "<TR>".
                        "<TD>&nbsp;".$count."&nbsp;</TD>".
                        "<TD>&nbsp;".$row['tag']."&nbsp;</TD>".
                        "<TD>&nbsp;".$row['owner']."&nbsp;</TD>".
                        "<TD>&nbsp;".$row['time']."&nbsp;</TD>".
                        "<TD><a href=\"$unhidepagelnk\">&nbsp Restore</a></TD>".
                    "</TR>";
            }
            echo "</table>";   
        }
        ################################################################
        # Manage Revisions
        ################################################################
        else if ($menu2 == 'revisions') {
            $cur_page       = $_REQUEST['page'];
            $sort           = $_REQUEST['sort'];
            $sort_order     = $_REQUEST['sortorder'];
            $show_owner     = $_REQUEST['show_owner'];
            $show_page    = $_REQUEST['show_page'];

            if      ($sort == 'tag')           $order = 'ORDER BY `tag`';
            else if ($sort == 'time')          $order = 'ORDER BY `time`';
            else if ($sort == 'owner')         $order = 'ORDER BY `owner`';
            else if ($sort == 'num_revisions') $order = 'ORDER BY `num_revisions`';
            else if ($sort == 'num_editors')   $order = 'ORDER BY `num_editors`';
            else                               $order = 'ORDER BY `tag`';
           
            if      ($sort_order == 'DESC')  { $order .= ' DESC'; $sort_order = 'ASC' ; $sort_order2 = 'DESC'; }
            else                             { $order .= ' ASC' ; $sort_order = 'DESC'; $sort_order2 = 'ASC' ; }
           
           
            $show_owner = $this->LoadUser($show_owner);
            $show_owner = $show_owner['name'];
           
            $this_link2 = $this_link.'&sort='.$sort.'&sortorder='.$sort_order2.'&page='.$cur_page;
           
            if (!$show_page) {
                $t_temp_pages   = $this->config["table_prefix"]."pages".rand(1,10000);
   
                $this->Query("CREATE TEMPORARY TABLE `$t_temp_pages` (`tag` varchar(75),`time` datetime,`user` varchar(75),`owner` varchar(75),`num_revisions` int,`num_editors` int) TYPE=MyISAM;");
               
                $pages = $this->LoadAll("SELECT `tag`,`time`,`user`,`owner` FROM `$t_pages` WHERE ".($show_owner?"`owner`='$show_owner' AND ":'')."`latest`='Y'");
                foreach ($pages as $page) {
                    $tag   = &$page['tag'];
                    $time  = &$page['time'];
                    $user  = &$page['user'];
                    $owner = &$page['owner'];
                    $num_revisions = $this->LoadSingle("SELECT count(*) as `num_revisions` FROM `$t_pages` WHERE `tag`='$tag'");
                    $num_revisions = $num_revisions ['num_revisions'];
                    $num_editors   = $this->LoadSingle("SELECT count(DISTINCT `user`) as `num_editors` FROM `$t_pages` WHERE `tag`='$tag'");
                    $num_editors   = $num_editors['num_editors'];
                    $this->Query("INSERT INTO `$t_temp_pages` (`tag`,`time`,`user`,`owner`,`num_revisions`,`num_editors`) VALUES ('$tag','$time','$user','$owner','$num_revisions','$num_editors');");               
                }
               
                $this_link3 = $this_link.'&show_owner='.$show_owner;
                $this_link4 = $this_link2.'&show_owner='.$show_owner;
   
                $query = "SELECT * FROM `$t_temp_pages` $order";
                $page = new Page();
                $total_records = count($this->LoadAll($query));
                $page->set_page_data($this_link3.'&sort='.$sort.'&sortorder='.$sort_order2,$total_records,RECORDS_PER_PAGE,5,true,true,true);
               
                ob_start();
                $page->get_page_nav();
                $page_nav = ob_get_contents();
                ob_end_clean();
               
                $pages = $this->LoadAll($page->get_limit_query($query));
               
                $this->Query("DROP TABLE `$t_temp_pages`;");
               
                echo
                    '<table border="0" callspacing="0" cellpadding="0">'.
                   
                    '<tr>'.
                        '<td colspan="2" >'.
                            '<H4>Page Info</H4>'.
                        '</td>'.
                        '<td align="right" valign="bottom" colspan="3" >'.
                            '<form action="'.$this_link.'" method="POST">'.
                            '<b>Owner: </b>'.
                            '<input type="text" name="show_owner" value="'.($show_owner?$show_owner:'Enter a Username').'"/>'.
                            '<input type="submit" value="Search" style="height: 23px; font-size: 10px; "/>'.
                            '</form>'.
                        '</td>'.
                    '</tr>'.
                    '<tr>'.
                        '<td colspan="5" >'.
                            '<small>Click on the page name to manage revisions.</small>'.
                        '</td>'.
                    '</tr>'.
                    '<tr>'.
                        '<td colspan="5" >'.
                            '<center>'.$page_nav.'</center>'.
                        '</td>'.
                    '</tr>'.
                    '<tr bgcolor="#DDDDDD">'.
                        '<th width="200">&nbsp;<a href="'.$this_link3.'&sort=tag&sortorder='.$sort_order.'&page='.$cur_page.'">Page Name:</a>&nbsp;</th>'.
                        '<th nowrap>&nbsp;<a href="'.$this_link3.'&sort=num_revisions&sortorder='.$sort_order.'&page='.$cur_page.'"># Edits:</a>&nbsp;</th>'.
                        '<th nowrap>&nbsp;<a href="'.$this_link3.'&sort=num_editors&sortorder='.$sort_order.'&page='.$cur_page.'"># Editors:</a>&nbsp;</th>'.
                        '<th nowrap>&nbsp;<a href="'.$this_link3.'&sort=owner&sortorder='.$sort_order.'&page='.$cur_page.'">Page Owner:</a>&nbsp;</th>'.
                        '<th nowrap>&nbsp;Last Editor:&nbsp;</th>'.
                        '<th>&nbsp;<a href="'.$this_link3.'&sort=time&sortorder='.$sort_order.'&page='.$cur_page.'">Last Edited:</a>&nbsp;</th>'.
                    '</tr>';
               
                $i = 0;
                foreach ($pages as $page) {
                    $tag           = &$page['tag'];
                    $time          = &$page['time'];
                    $owner         = &$page['owner'];
                    $last_editor   = &$page['user'];
                    $num_revisions = &$page['num_revisions'];
                    $num_editors   = &$page['num_editors'];
                   
                    $color = ($i%2)?"#ffffff":"#f5f6f7";
                    echo
                        '<tr bgcolor="'.$color.'">'.
                            '<td nowrap>&nbsp;<a href="'.$this_link4.'&show_page='.$tag.'">'.$tag.'</a>&nbsp;</td>'.
                            '<td nowrap>&nbsp;'.$num_revisions.'&nbsp;</td>'.
                            '<td nowrap>&nbsp;'.$num_editors.'&nbsp;</td>'.
                            '<td nowrap>&nbsp;'.$owner.'&nbsp;</td>'.
                            '<td nowrap>&nbsp;'.$last_editor.'&nbsp;</td>'.
                            '<td nowrap>&nbsp;'.$time.'&nbsp;</td>'.
                        '</tr>';
                    $i++;
                }
                   
                echo
                    '</table>';
            }
            # Show Page
            else {
                if ($_REQUEST['set']) {
                    $id          = $_REQUEST['id'];
                    $action      = $_REQUEST['action'];
           
                    $n = count($id);
           
                    for ($i = 0; $i < $n; $i++) {
                        $this_id         = $id[$i];
                        $this_action     = $action[$i];
                       
                        if      ($this_action == 'delete_revision') {
                            $this->Query("DELETE FROM `$t_pages` WHERE `id`='".mysql_real_escape_string($this_id)."'");
                        }
                        else if ($this_action == 'hide_revision') {
                            $this->Query("UPDATE `$t_pages` SET `latest`='V' WHERE `id`='".mysql_real_escape_string($this_id)."' AND `latest`='N'");
                        }
                        else if ($this_action == 'restore_revision') {
                            $this->Query("UPDATE `$t_pages` SET `latest`='N' WHERE `id`='".mysql_real_escape_string($this_id)."' AND `latest`='V'");
                        }
                    }
                }
               
                $show_page = $this->LoadPage($show_page);
                $show_page = $show_page['tag'];
               
                $this_link3 = $this_link2.'&show_page='.$show_page.'&show_owner='.$show_owner;
                $this_link4 = $this_link2.'&show_page=&show_owner='.$show_owner;
               
                $revisions = $this->LoadAll("SELECT `id`,`tag`,`time`,`user`,`latest` FROM `$t_pages` WHERE `tag`='$show_page' ORDER BY `time` DESC");
                echo
                    '<H4>Manage Revisions: '.$show_page.'</H4>'.
                    '<a href="'.$this_link4.'"><=Back</a>'.
                    '<form action="'.$this_link3.'" method="POST">'.
                    '<table border="0" callspacing="0" cellpadding="0">'.
                    '<tr bgcolor="#DDDDDD">'.
                        '<th nowrap>&nbsp;Time:&nbsp;</th>'.
                        '<th nowrap>&nbsp;User:&nbsp;</th>'.
                        '<th nowrap>&nbsp;&nbsp;</th>'.
                        '<th nowrap>&nbsp;Delete:&nbsp;</th>'.
                        '<th nowrap>&nbsp;Hide:&nbsp;</th>'.
                    '</tr>';
               
                $i = 0;
                foreach ($revisions as $revision) {
                    $id            = &$revision['id'];
                    $tag           = &$revision['tag'];
                    $time          = &$revision['time'];
                    $user          = &$revision['user'];
                    $latest        = &$revision['latest'];
                    $current_rev   = ($latest == 'Y');
                    $hidden_rev    = ($latest == 'V');
                   
                    $color = ($i%2)?"#ffffff":"#f5f6f7";
                    echo
                        '<tr bgcolor="'.$color.'">';
                       
                    if ($current_rev) {
                        echo    '<td nowrap>&nbsp;<a href="'.$this->Href('show',$tag).'"><b>'.$time.'</b></a>&nbsp;</td>'.
                            '<td nowrap>&nbsp;<b>'.$user.'</b>&nbsp;</td>';
                    } else if ($hidden_rev) {
                        echo    '<td nowrap>&nbsp;<font color="gray">'.$time.'</font>&nbsp;</td>'.
                            '<td nowrap>&nbsp;<font color="gray">'.$user.'</font>&nbsp;</td>';
                    } else {
                        echo    '<td nowrap>&nbsp;<a href="'.$this->Href('show',$tag,'time=').urlencode($time).'">'.$time.'</a>&nbsp;</td>'.
                            '<td nowrap>&nbsp;'.$user.'&nbsp;</td>';
                    }
                    echo
                            '<td align="center">'.
                                '<input type="hidden" name="id['.$i.']"    value="'.$id.'" />'.
                                '<input type="radio"  name="action['.$i.']" value="restore_revision" '.($current_rev?'disabled':'').' '.(!$hidden_rev?'checked':'').'/>'.
                            '</td>'.
                            '<td align="center">'.
                                '<input type="radio"  name="action['.$i.']" value="delete_revision" '.($current_rev?'disabled':'').'/>'.
                            '</td>'.
                            '<td align="center">'.
                                '<input type="radio"  name="action['.$i.']" value="hide_revision"    '.($current_rev?'disabled':'').'  '.($hidden_rev?'checked':'').'/>'.
                            '</td>'.
                        '</tr>';
                    $i++;
                }
                   
                echo
                    '</table>'.
                    '<input type="submit" value="Carry Out Selected Actions" onclick="return confirm(\'Are you sure you want to carry out the selected actions?\')">'.
                    '<input type="hidden" name="set" value="1">'.
                    '</form>';
               
            }
        }
    }
    ################################################################
    ################################################################
    # Users
    ################################################################
    ################################################################
    else if ($menu1 == 'users') {
        $menu2     = $_REQUEST['menu2']?$_REQUEST['menu2']:'';
        $pre_link  = $link.'&menu1='.$menu1;
        echo '<br />';
        $this_link = $pre_link.'&menu2=delete';
        $page_name = 'Delete User';
        echo ($menu2 != 'delete')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'<font color="red"><b>'.$page_name.'</b></font>';
        echo ' | ';
        $this_link = $pre_link.'&menu2=reset_password';
        $page_name = 'Reset Password';
        echo ($menu2 != 'reset_password')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'<font color="red"><b>'.$page_name.'</b></font>';
        echo ' | ';
        $this_link = $pre_link.'&menu2=reset_wikiname';
        $page_name = 'Reset Wikiname';
        echo ($menu2 != 'reset_wikiname')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'<font color="red"><b>'.$page_name.'</b></font>';
        echo ' | ';
        $this_link = $pre_link.'&menu2=inactivate';
        $page_name = 'Inactivate User';
        echo ($menu2 != 'inactivate')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'<font color="red"><b>'.$page_name.'</b></font>';
        echo ' | ';
        $this_link = $pre_link.'&menu2=reserve';
        $page_name = 'Reserve Account';
        echo ($menu2 != 'reserve')?'<a href="'.$this_link.'"><b>'.$page_name.'</b></a>'
                :'<font color="red"><b>'.$page_name.'</b></font>';
               
        $this_link = $link.'&menu1='.$menu1.'&menu2='.$menu2;
   
        ################################################################
        # Delete Users
        ################################################################
        if ($menu2 == 'delete') {
            if ($_REQUEST['set']) {
                $delete = $_REQUEST['delete'];
                $n = count($delete);
               
                for ($i = 0; $i < $n; $i++) {
                    if ($this_delete = $delete[$i]) {
                        $this->Query("DELETE FROM `$t_users` WHERE `name`='".mysql_real_escape_string($this_delete)."'");
                    }
                }
            }
       
            $sort           = stripslashes($_REQUEST['sort']);
            $sort_order     = stripslashes($_REQUEST['sortorder']);
            $cur_page       = stripslashes($_REQUEST['page']);
           
            if      ($sort == 'name')       $order = 'ORDER BY `name`';
            else                            $order = 'ORDER BY `signuptime`';
           
            if      ($sort_order == 'ASC')  { $order .= ' ASC' ; $sort_order = 'DESC'; $sort_order2 = 'ASC'}
            else                            { $order .= ' DESC'; $sort_order = 'ASC'$sort_order2 = 'DESC'; }
           
            $query = "SELECT `name`,`signuptime`,`login_count`,`email` FROM `$t_users` WHERE `password`!='Reserved Account' $order ";
           
            $page = new Page();
            $total_records = count($this->LoadAll($query));
            $page->set_page_data($this_link.'&sort='.$sort.'&sortorder='.$sort_order2,$total_records,RECORDS_PER_PAGE,5,true,true,true);
            ob_start();
            $page->get_page_nav();
            $page_nav = ob_get_contents();
            ob_end_clean();
           
            $users = $this->LoadAll($page->get_limit_query($query));
           
            $this_link2 = $this_link.'&sort='.$sort.'&sortorder='.$sort_order2.'&page='.$cur_page;
           
            echo
                '<form action="'.$this_link2.'" method="POST">'.
                '<h4>Wiki User Delete</h4>'.
                '<table border="0" cellspacing="2" cellpadding="0">'.
                '<tr>'.
                    '<td colspan="6" >'.
                        '<center>'.$page_nav.'</center>'.
                    '</td>'.
                '</tr>'.
                '<tr bgcolor="#DDDDDD">'.
                    "<th>&nbsp;Del.&nbsp;</th>".
                    '<th>&nbsp;<a href="'.$this_link.'&sort=name&sortorder='.$sort_order.'&page='.$cur_page.'">Name</a>&nbsp;</th>'.
                    "<th>&nbsp;# logins&nbsp;</th>".
                    "<th>&nbsp;Owned Pages&nbsp;</th>".
                    "<th>&nbsp;Email&nbsp;</th>".
                    '<th>&nbsp;<a href="'.$this_link.'&sort=signuptime&sortorder='.$sort_order.'&page='.$cur_page.'">Signup Date/Time</a>&nbsp;</th>'.
                "</tr>";
           
            $i = 0;
            foreach($users as $user)  {
                if ($stat!=="0") $num = $this->LoadSingle("SELECT count(*) as n from ".$this->config["table_prefix"]."pages where owner='".$user["name"]."' AND latest = 'Y'");   
                echo
                    '<tr>'.
                    '<td>'.
                        '<input type="hidden"   name="delete['.$i.']" value="0" />'.
                        '<input type="checkbox" name="delete['.$i.']" value="'.$user['name'].'" />'.
                    '</td>'
                    "<td>".$this->Link($user["name"])."</td>".
                    "<td>".$user["login_count"]."</td>".
                    "<td>".($stat!=="0"?"".$num["n"]."":"")."</td>".
                    "<td>".$user["email"]."</td>".
                    "<td>(".$user["signuptime"].")</td>".
                    '</tr>';
                $i ++;
            }
           
            echo
                '</table>'.
                '<input type="submit" value="Delete Users" onclick="return confirm(\'Are you sure you want to delete the selected user?\')">'.
                '<input type="hidden" name="set" value="1">'.
                '</form>';       
        }
        ################################################################
        # Reset Passwords
        ################################################################

        else if ($menu2 == 'reset_password') {
            $user_action = $_REQUEST['user_action'];
           
            if ($user_action == 'set_password') {
                $username  = strtolower($_REQUEST['username']);
                $password1 = $_REQUEST['password1'];
                $password2 = $_REQUEST['password2'];
   
                if ($password1 != $password2) {
                    echo '<em>Passwords entered do not match.</em>';    
                } else {
                    $password = md5($password1);
                    $wikiname = $this->LoadUser(mysql_real_escape_string($username));
                    $wikiname = $wikiname['name'];
                    if ($wikiname) {
                        $this->Query("UPDATE `$t_users` SET `password`='$password' WHERE `name`='$wikiname'");
                    }               
                }
            }
           
            echo
                '<br>'.
                '<form action="'.$this_link.'" method="POST">'.
                '<h4>Reset Password</h4>'.
                '<table border="0" cellpadding="0" cellspacing="0">'.
                '<tr>'.
                    '<td>'.
                        '<b>Username:</b>&nbsp;'.
                    '</td>'.
                    '<td>'.
                        '<input type="text"   name ="username" />&nbsp;'.
                    '</td>'.
                '</tr>'.
                '<tr>'.
                    '<td>'.
                        '<b>New Password:</b>&nbsp;'.
                    '</td>'.
                    '<td>'.
                        '<input type="password"   name ="password1" />&nbsp;'.
                    '</td>'.
                '</tr>'.
                '<tr>'.
                    '<td>'.
                        '<b>Confirm Password:</b>&nbsp;'.
                    '</td>'.
                    '<td>'.
                        '<input type="password"   name ="password2" />&nbsp;'.
                    '</td>'.
                '</tr>'.
                '</table>'.
                '<input type="submit" value="Set Password" />'.
                '<input type="hidden" name ="user_action" value="set_password" />'.
                '</form>';
        }
       
        ################################################################
        # Reset Wikiname
        ################################################################
        else if ($menu2 == 'reset_wikiname') {
            $old_username = preg_replace("/[^A-Za-z0-9]/i","",$_REQUEST['old_username']);
            $old_username = $this->LoadUser($old_username);
            $old_username = $old_username['name'];
            $new_username = preg_replace("/[^A-Za-z0-9]/i","",$_REQUEST['new_username']);
           
            if ($new_username) {
                if (strtolower($old_username) != strtolower($new_username)) {
                    echo
                        '<div style="border: 2px solid #800; background-color: #ff0; color: #800; ">'.
                            'The new username you have specified is incompatable with your old username. The new username must be the same as the old name in all ways but case.'.
                        '</div>';
                } else {
                    if (!$this->isWikiName($new_username)) {
                        echo
                            '<div style="border: 2px solid #800; background-color: #ff0; color: #800; ">'.
                                "The entered username is <b>not</b> an acceptable wiki name. Wiki names are \"humped\" like this: <b>P</b>age<b>N</b>ame, <b>P</b>age<b>N</b>ame2, <b>H</b>umped<b>N</b>ame.".
                            '</div>';
                    } else {
                        $this->Query("UPDATE `$t_users` SET `name`='$new_username' WHERE `name`='$old_username'");
                        $this->SetUser($this->LoadUser($new_username));
                        header("Location: $this_link");
                    }
                }
   
            }
   
            echo
                "<h4>Reset Upper/Lower Case In Wikiname</h4>".
                '<form action="'.$this_link.'" method="POST">'.
                '<table border="0" cellpadding="0" cellspacing="0">'.
                '<tr>'.
                    '<th>Old Username: &nbsp;</th>'.
                    '<td><input type="text" name="old_username" value="" />&nbsp;<td>'.
                '</tr>'.
                '<tr>'.
                    '<th>New Username: &nbsp;</th>'.
                    '<td><input type="text" name="new_username" value="" />&nbsp;<td>'.
                '</tr>'.
                '<tr>'.
                    '<td colspan="2"><input type="submit" value="Change" /><td>'.
                '</tr>'.
                '</table>'.
                '</form>';
           
        }
        ################################################################
        # Inactivate account
        ################################################################
        else if ($menu2 == 'inactivate') {
            $reset_action = $_REQUEST['reset_action'];
   
            if ($reset_action == 'reset_password') {
                $username = $_REQUEST['username'];
                if (!$user = $this->LoadUser($username)) {