<?php
    $mcon 
mysql_connect ("localhost""root""passw0rd");
    if (
$mcon) {
         if (!
mysql_select_db ("asterisk"$mcon)) die ("Select database 'asterisk': ".mysql_error ());
    } else {
        die (
"<hr>We are very sorry but something went quite wrong.<p>\n");
    }

function 
checkTable($tablename){
    
$itexists false;
    if(
mysql_num_rows(mysql_query("SHOW TABLES LIKE '" $tablename "'"))){
        
$itexists true;
    }
    return 
$itexists;
}

function 
getmicrotime ()
{
    list (
$usec$sec) = explode (" "microtime ());
    return ((float)
$usec) + (float)$sec;
}

function 
selectBox ($var$sql)
{
    
$result mysql_query ($sql);
    if (!
$result) die (mysql_error ());
    
printf ("<select name=\"%s\">\n"$var);
    while (
$row mysql_fetch_row ($result)) {
        
printf ("<option value=\"%s\">%s\n"$row [0], $row [1]);
    }
    echo 
"<" "/select>\n";
    
mysql_free_result ($result);
}

function 
selectBoxSelected ($var$sql$sel)
{
    
$result mysql_query ($sql);
    if (!
$result) die (mysql_error ());
    
printf ("<select name=\"%s\">\n"$var);
    while (
$row mysql_fetch_row ($result)) {
        if (
$sel == $row [0]) {
            
printf ("<option value=\"%s\" selected>%s\n"$row [0], $row [1]);
        } else {
            
printf ("<option value=\"%s\">%s\n"$row [0], $row [1]);
        }
    }
    echo 
"<" "/select>\n";
    
mysql_free_result ($result);
}

class 
ListBoxSingle {
    var 
$boxName;
    
    function 
ListBoxSingle ($namVar$boxSize)
    {
        
$this->boxName $namVar;
        
printf ("<select name=\"%s\" size=\"$boxSize\">\n"$this->boxName);
    }
    function 
option ($optVal$optTxt)
    {
        global ${
$this->boxName};
        
printf ("<option value=\"%s\"%s>%s%s/option>\n"$optVal,
            (
$optVal == ${$this->boxName}) ? " selected" "",
            
$optTxt"<");
    }
    function 
optionSelected ($isSel$optVal$optTxt)
    {
        
printf ("<option value=\"%s\"%s>%s%s/option>\n"$optVal
            
$isSel " selected" ""$optTxt"<");
    }
    function 
close ()
    {
        echo 
"<" "/select>\n";
    }
}

class 
ListBoxMultiple {
    var 
$boxName;
    
    function 
ListBoxMultiple ($namVar$boxSize)
    {
        
$this->boxName $namVar;
        
printf ("<select multiple name=\"%s[]\" size=\"$boxSize\">\n"$this->boxName);
    }
    function 
option ($optVal$optTxt)
    {
        global ${
$this->boxName};
        
$isSel 0;
        foreach (${
$this->boxName} as $val) {
            if (
$optVal == $val) {
                
$isSel 1;
                
// echo "sel:$optVal:$val:\n";
                
break;
                
// } else {
                // echo "nsl:$optVal:$val:\n";
            
}
        }
        
printf ("<option value=\"%s\"%s>%s%s/option>\n"$optVal,
            (
$isSel) ? " selected" "",
            
$optTxt"<");
    }
    function 
optionSelected ($isSel$optVal$optTxt)
    {
        
printf ("<option value=\"%s\"%s>%s%s/option>\n"$optVal
            
$isSel " selected" ""$optTxt"<");
    }
    function 
close ()
    {
        echo 
"<" "/select>\n";
    }
}

class 
Table {
    var 
$tabOpen;
    var 
$rowOpen;
    var 
$colOpen;
    var 
$hedOpen;
    var 
$colSpan;
    var 
$rowColor1 "#BBCCDD";
    var 
$rowColor2 "#CCCCCC";
    var 
$rowCount 0;
    function 
Table ()
    {
        
$this->tabOpen 0;
        
$this->rowOpen 0;
        
$this->colOpen 0;
        
$this->hedOpen 0;
        
$this->colSpan 1;
    }
    function 
open ($border)
    {
        if (
$this->tabOpen) die ("Opening an open table");
        echo 
"<table border=\"$border\">\n";
        
$this->tabOpen 1;
    }
    function 
closeHed ()
    {
        if (!
$this->hedOpen) die ("Header cell not open");
        echo 
"<" "/th>\n";
        
$this->hedOpen 0;
    }
    function 
closeCol ()
    {
        if (!
$this->colOpen) die ("Cell not open");
        echo 
"<" "/td>\n";
        
$this->colOpen 0;
    }
    function 
closeRow ()
    {
        if (!
$this->rowOpen) die ("Row not open");
        if (
$this->hedOpen$this->closeHed ();
        if (
$this->colOpen$this->closeCol ();
        echo 
"<" "/tr>\n";
        
$this->rowOpen 0;
    }
    function 
row ()
    {
        if (!
$this->tabOpen) die ("Table not open");
        if (
$this->rowOpen$this->closeRow ();
        if (
$this->rowColor1) {
        if (
$this->rowCount 1) {
            echo (
"<tr bgcolor=\"$this->rowColor1\">\n");
        } else {
            echo (
"<tr bgcolor=\"$this->rowColor2\">\n");
        }
    } else {
        echo (
"<tr>\n");
    }
    
$this->rowCount ++;
        
$this->rowOpen 1;
    }
    function 
hcell ($s)
    {
        if (!
$this->rowOpen$this->row ();
        if (
$this->hedOpen$this->closeHed ();
        if (
$this->colOpen$this->closeCol ();
        if (
$this->colSpan 1) {
            echo 
"<th colSpan=\"$this->colSpan\" align=\"left\">$s";
        } else {
            echo 
"<th align=\"left\">$s";
        }
        
$this->hedOpen 1;
    }
    function 
cell ($s)
    {
        if (!
$this->rowOpen$this->row ();
        if (
$this->hedOpen$this->closeHed ();
        if (
$this->colOpen$this->closeCol ();
            if (
$this->colSpan 1) {
                echo 
"<td colSpan=\"$this->colSpan\">$s";
            } else {
                echo 
"<td>$s";
            }
        
$this->colOpen 1;
    }
    function 
preCell ($s)
    {
        
$this->cell ("<pre>" $s '<' '/' "pre>");
    }
    function 
input ($varNam$varVal$inpSiz$inpMax)
    {
        
$this->cell ("<input type=\"text\" name=\"$varNam\" value=\"$varVal\"" .
            
" size=\"$inpSiz\" maxlength=\"$inpMax\">");
    }
    function 
close ()
    {
        if (!
$this->tabOpen) die ("Closing a closed table");
        if (
$this->rowOpen$this->closeRow ();
        echo 
"<" "/table>\n";
        
$this->tabOpen 0;
    }
}

function 
showTable ($sql)
{
    
$result mysql_query ($sql);
    if (!
$result) {
        echo 
"showTable($sql): ";
        echo 
mysql_error ();
        echo 
"<br>";
        return;
    }
    
$tab = new Table;
    
$tab->open (1);
    
$i 0;
    while (
$i mysql_num_fields($result)) {
        
$meta mysql_fetch_field($result$i);
        if (!
$meta) {
            
$tab->hcell ("No information available");
        } else {
            
$tab->hcell ("$meta->name");
        }
        
$t [$i] = $meta->type;
        
$i++;
    }
    
$tab->closeRow ();
    while (
$row mysql_fetch_row ($result)) {
        
$i 0;
        while (
$i mysql_num_fields($result)) {
                if (
strcmp ($t[$i], "blob")) {
                
$tab->cell ($row[$i]);
            } else {
                
$tab->cell ("<textarea cols=\"40\" rows=\"3\" readonly>" $row[$i] . "</textarea>");
            }
            
$i ++;
        }
        
$tab->closeRow ();
    }
    
$tab->close ();
    
mysql_free_result ($result);
}

function 
showRecord ($sql)
{
    
$result mysql_query ($sql);
    if (!
$result) {
        echo 
"showRecord($sql): ";
        echo 
mysql_error ();
        echo 
"<br>";
        return;
    }
    
$tab = new Table;
    
$tab->open (1);
    
$i 0;
    
$row mysql_fetch_row ($result);
    while (
$i mysql_num_fields($result)) {
          
$meta mysql_fetch_field($result$i);
          if (!
$meta) {
           
$tab->hcell ("No information available");
          } else {
           
$tab->hcell ($meta->name);
          }
          if (
strcmp ($meta->type"blob")) {
            
$tab->cell ($row[$i]);
        } else {
            
$tab->preCell ($row[$i]);
        }
        
$i ++;
       
$tab->closeRow ();
    }
    
$tab->close ();
    
mysql_free_result ($result);
}

class 
Ctrl
{
    var 
$m_nam;
    function 
Ctrl ($nam)
    {
        
$this->m_nam $nam;
    }
    function 
readFld ()
    {
        if (
$_SERVER ["REQUEST_METHOD"] == "GET") {
            return 
$_GET [$this->m_nam];
        } else if (
$_SERVER ["REQUEST_METHOD"] == "POST") {
            return 
$_POST [$this->m_nam];
        }
        return 
"";
    }
}

class 
EditC extends Ctrl
{
    var 
$m_txt;
    var 
$m_cols;
    var 
$m_rows;
    
    function 
EditC ($nam)
    {
        
$this->Ctrl ($nam);
        
$this->m_rows 1;
        
$this->m_cols 20;
        
$this->m_txt stripslashes ($this->readFld ());
    }
    
    function 
disp ()
    {
        if (
$this->m_rows == 1) {
            
printf ("<input type=\"text\" name=\"%s\" size=\"%s\" value=\"%s\">\n",
                
$this->m_nam$this->m_colshtmlentities ($this->m_txt));
        } else {
            
printf ("<textarea name=\"%s\" rows=\"%d\" cols=\"%d\">%s%s\n"
                
$this->m_nam$this->m_rows$this->m_colshtmlentities ($this->m_txt), "</textarea>\n");
        }
    }
    
    function 
pass ()
    {
        
printf ("<input type=\"password\" name=\"%s\" size=\"%s\" value=\"%s\">\n",
            
$this->m_nam$this->m_colshtmlentities ($this->m_txt));
    }
}
?>