Barnaby Norman

MySQL Genie - Source Code

Barnaby Norman Home Page | Site Map | Family Norman Genealogy Site | Contact Me

Genie PHP

This PHP file captures the AJAX posted data at the server end and separates it into commands and arguments. The script then branches depending on if the cookies have been set (if the user has logged in).

Part of genie.php showing the processing of arguments posted

$posted = urldecode ( implode ( file ('php://input')));

// Get the data posted as command and argument
list($command, $argument) = explode(";", $posted, 2);

The first branch then branches again depending on the command passed to it and in each case the data is requested from an instance of GenieInterface object and further formatted before being returned to the client’s browser.

Part of genie.php showing the case listTables:

case listTables:
        
    
// Add titles
    
echo "<h1>Database: $argument</h1>";
            
    
// Check tere are tables to display
    
if($data $genie->listTables($argument))
    {
        
// Add border
        
echo "<p style=\"border-top-style: solid; border-bottom-style: solid;\"><br />";
            
        
// Display first element
        
echo $data[0];
            
        
// Remove first element
        
array_shift($data);
            
        
// Loop through any other data and remove when displayed
        
while($data)
        {
            echo 
" | " $data[0];
                
            array_shift(
$data);
        }
            
        echo 
"<br /><br /></p>";
    }
            
    
// Display link back to previous database list
    
echo "<p><a href=\"\" onClick=\"listDatabases(); return false;\">Back to database list</a></p>";
            
    break;

The final branch, which is followed if no cookies have been set, presents the user with the log-in form for them to complete.

Full source code for genie.php

<?php

require_once "GenieInterface.class";
    
$posted = urldecode ( implode ( file ('php://input')));

// Get the data posted as command and argument
list($command, $argument) = explode(";", $posted, 2);

// Test for cookies
if(($_COOKIE['hostname']) && ($_COOKIE['username']) && ($_COOKIE['password']))
{
    
// Found - create instance of GenieInterface class
        
    // Get info from cookies
    
$host $_COOKIE['hostname'];
    
$user $_COOKIE['username'];
    
$pass $_COOKIE['password'];
        
    
$genie new GenieInterface($host, $user, $pass);
        
    
// Branch depending on the command passed
    
switch($command)
    {
        case 
listDatabases:
            
            
// Add titles
            
echo "<h1>All Database Instances:</h1>";
            echo 
"<p style=\"border-top-style: solid; border-bottom-style: solid;\"><br />";

            
// Get and display first data element
            
$data $genie->listDatabases();
            echo 
$data[0];
            
            
// Remove first element from array
            
array_shift($data);
            
            
// Loop through any other elements listing and chopping
            
while($data)
            {
                echo 
" | " $data[0];
                
                array_shift(
$data);
            }
            
            echo 
"<br /><br /></p>";
            
            break;
            
        case 
listTables:
        
            
// Add titles
            
echo "<h1>Database: $argument</h1>";
            
            
// Check tere are tables to display
            
if($data $genie->listTables($argument))
            {
                
// Add border
                
echo "<p style=\"border-top-style: solid; border-bottom-style: solid;\"><br />";
            
                
// Display first element
                
echo $data[0];
            
                
// Remove first element
                
array_shift($data);
            
                
// Loop through any other data and remove when displayed
                
while($data)
                {
                    echo 
" | " $data[0];
                
                    array_shift(
$data);
                }
            
                echo 
"<br /><br /></p>";
            }
            
            
// Display link back to previous database list
            
echo "<p><a href=\"\" onClick=\"listDatabases(); return false;\">"
                    
"Back to database list</a></p>";
            
            break;
            
        case 
listData:
                    
            list(
$database, $table) = explode(".", $argument, 2);
            
            echo 
"<p><a href=\"\" onClick=\"listTables('$database'); return false;\">"
                    
"Back to database $database</a> | ";

            echo 
"<a href=\"\" onClick=\"listDatabases(); return false;\">"
                    
"Back to database list</a></p>";
            
            
$data $genie->listData($argument);
            
            echo 
"<p style=\"border-style-top: solid; border-style-bottom: solid;\">" 
                
$data "</p>";

            echo 
"<p><a href=\"\" onClick=\"listTables('$database'); return false;\">"
                    
"Back to database $database</a> | ";

            echo 
"<a href=\"\" onClick=\"listDatabases(); return false;\">"
                    
"Back to database list</a></p>";
            
            break;
                        
    }
        
}
else
{
    
// No cookies so display login form
            
    
echo "<h1>MySQL Visual Manager</h1>";
    echo 
"<h3>Please enter your username and password for this instance of MySQL</h3>";
    echo 
"<form action=\"#\">";
    echo 
"<table>";
    echo 
"<tr>";
    echo 
"<td><label>Hostname:</label></td>";
    echo 
"<td><input type=\"text\" id=\"hostname\" value=\"localhost\"></td>";
    echo 
"<td><span id=\"hostStar\" style=\"color: red; visibility: hidden; \">*</span></td>";
    echo 
"</tr>";
    echo 
"<tr>";
    echo 
"<td><label>Username:</label></td>";
    echo 
"<td><input type=\"text\" id=\"username\"></td>";
    echo 
"<td><span id=\"nameStar\" style=\"color: red; visibility: hidden; \">*</span></td>";
    echo 
"</tr>";
    echo 
"<tr>";
    echo 
"<td><label>Password:</label></td>";
    echo 
"<td><input type=\"password\" id=\"password\"></td>";
    echo 
"<td><span id=\"passStar\" style=\"color: red; visibility: hidden; \">*</span></td>";
    echo 
"</tr>";
    echo 
"</table>";
    echo 
"<input type=\"submit\" onclick=\"set_cookie(); return false; \">";
    echo 
"<span id=\"errorStar\" style=\"color: red; visibility: hidden; \">";
    echo 
"* Please Complete ALL Fields Before Submitting</span>";
}
    
?>