PHP文本数据库类之管理(txtDB.manager.php)

类别:编程语言 点击:0 评论:0 推荐:

使用方法:结合txtDb.inc

初始用户和密码为:admin 123

CODE:

<?php
/*
TxtDb Manager
version: 0.1.0
mail: [email protected]
This script is the manager for PHP txtDb.
#############################
Suppose Modify structrual
*/
include('txtdb.inc');
$root="txtdb";
$s_admins=array("admin","admin2","admin3");
$s_password="123";
####################
if(isset($_GET))@extract($_GET);
if(isset($_POST))@extract($_POST);
if(isset($_SESSION))@extract($_SESSION);
if(isset($_COOKIE))@extract($_COOKIE);
#################
if(isset($u_admin))$admin=$u_admin;
if(isset($u_password))$password=md5($u_password);
$s_password=md5($s_password);
$islogin=false;
if(($password==$s_password)and in_array($admin,$s_admins)){
 setcookie('admin', $admin);
 setcookie('password',$password);
 $islogin=true; 
 if(isset($u_admin) and isset($u_password))echo"<script>top.location.reload();</script>";
}
if($action=="quit"){
 setcookie('admin');
 setcookie('password');
 $islogin=false;
 echo "<script>top.nav.location.reload();</script>";
}
if(!$islogin and $action!="leftbar" and $action!="")$action="main";
#################
?>
<html>
<head>
<title>Episome TxtDb Manager</title>
<meta http-equiv=pragma content=no-cache>
<meta http-equiv=no-cache>
<meta http-equiv=expires content=-1>
<meta http-equiv=cache-control content=no-cache>
<meta http-equiv=content-type content="text/html" charset=gb2312>
<style>
 body,td,input,textarea,select{font-family: verdana; font-size:10pt;}
 .btn{width:100px}
 hr{color:#AAAAAA;size:1}
 li{list-style:square;line-height: 24px;}
 h5{font-size:11pt;color:green;margin-top:0;margin-bottom:0}
 h6{font-size:10pt;color:red;margin-top:0;margin-bottom:0}
 td{height:24};
 a{color:blue;text-decoration:none;}
 a:hover{color:red;text-decoration:underline;}
 form{margin-top:0;margin-bottom:0}
 .tbl{color:red;}
</style>
<script>
var lastRow;
function select(curRow,curColor,lastColor){
if (lastRow!=null) lastRow.style.background=lastColor;
 curRow.style.background=curColor;
 lastRow = curRow;
}
</script>
<?
##########################
if(!is_dir($root))
 die('<h6>Current TxtDb root does not exists!</h6>');
$db=new txtDb($root);
$version=$db->version;
############################
switch($action){
 case "leftbar":
  leftbar();
  break;
 case "main":
  main();
  break;
 case "selecttable":
  selecttable($table);
  break;
 case "viewdata":
  viewdata($table,$start,$num,$sortby,$order,$keyword,$fields,$selectstring,$selectcolumn);
  break;
 case "newtable":
  newtable($table,$tblfields);
  break;
 case "insertrecord":
  insertrecord($table,$value,$id);
  break;
 case "delrecord":
  delrecord($table,$id,$start,$num,$order,$sortby);
  break;
 case "editrecord":
  editrecord($table,$id,$act,$value,$start,$num,$order,$sortby);
  break;
 case "droptable":
  droptable($table);
  break;
 case "cleartable":
  cleartable($table);
  break;
 default:
  index();
}
/*
edit a row from table
*/
function editrecord($table,$id,$act,$value,$start,$num,$order,$sortby){
 global $db;
 $order=($order=="asc")?"desc":"asc";
 $db->open($table,$id,1);
 if($act=="save" and is_array($value)){
  $db->change($value,$id);
  echo "<h5>The record of table <font class=tbl>[".$table."]</font> has been modifyed !</h5><hr>";
  echo "<li><a href='?action=viewdata&table=".$table."&start=".$start."&num=".$num."&order=".$order."&sortby=".$sortby."'>View data</a>";
  die();
 }
echo"
<h5>Modify record of table <font class=tbl>[".$table."]</font> !</h5><hr>
<table cellpadding=0 cellspacing=0>
<tr>
 <td bgcolor='#AAAAAA'>
  <table cellpadding=0 cellspacing=1>
  <form action='?action=editrecord&table=$table&start=$start&num=$num&order=$order&sortby=$sortby' method=post>";
 foreach($db->fieldNames as $field){
  $v=stripslashes($db->dbValues[$id][$field]);
  echo "
   <tr>
    <td bgcolor='#B3DBF9' width='100' align=right><nobr>$field :&nbsp;</nobr></td>
    <td bgcolor='white'><textarea name='value[$field]' rows=2 style='width:380'>$v</textarea></td>
   </tr>
  ";
 }
echo"
  </table>
 </td>
 <td valign=bottom>
  <input type=hidden name=act value='save'>
  <input type=hidden name=id value='$id'>
  &nbsp;<input type=submit value='Save >>' class=btn><br>&nbsp;<input type=submit value='Cancel >>' class=btn onclick='js:history.go(-1)'>
 </td>
</tr></form>
</table>
";
}
/*
delete a row from table
*/
function delrecord($table,$id,$start,$num,$order,$sortby){
 global $db;
 $order=($order=="asc")?"desc":"asc";
 $db->open($table,0,1);
 $db->delete($id);
 echo "<h5>The record of table <font class=tbl>[".$table."]</font> has been deleted !</h5><hr>";
 echo "<li><a href='?action=viewdata&table=".$table."&start=".$start."&num=".$num."&order=".$order."&sortby=".$sortby."'>View data</a>";
 die();
}
/*
Insert a row to table
*/
function insertrecord($table,$value,$id){
 global $db;
 $db->open($table,0,1);
 if(is_array($value)){
  $db->append($value,$id);
  echo "<h5>The record has been inserted to table <font class=tbl>[".$table."]</font>!</h5><hr>";
  echo "<li><a href='?action=viewdata&table=".$table."'>View data</a>";
  echo "<li><a href='?action=insertrecord&table=$table'>Insert record</a>";
  die();
 }
 echo"<h5>Insert record to <font class=tbl>[".$table."]</font></h5>";
 echo "<hr>";
 echo "
<table cellpadding=0 cellspacing=0>
<tr>
 <td bgcolor='#AAAAAA'>
  <table cellpadding=0 cellspacing=1 height='100%'>
  <form action='?action=insertrecord&table=$table' method=post name=iform>
";
 foreach($db->fieldNames as $field){
  echo "
   <tr>
    <td bgcolor='#B3DBF9' width='100' align=right><nobr>$field :&nbsp;</nobr></td>
    <td bgcolor='white'><textarea name='value[$field]' style='width:380'></textarea></td>
   </tr>
  ";
 }
 echo "
  </table>
 </td>
 <td valign=bottom>&nbsp;ID <input size=3 name=id value='$db->lines'><br>&nbsp;<input type=submit onclick='return !isNaN(iform.id.value);' value='Insert >>' class=btn><br>&nbsp;<input type=submit value='Cancel >>' class=btn onclick='js:history.go(-1)'></td>
</tr></form>
</table>
";
}
/*
view data
*/
function viewdata($table,$start,$num,$sortby,$order,$keyword,$fields,$selectstring,$selectcolumn){
 global $db;
 if(trim($start)=='')$start=0;
 if(trim($num)=='')$num=30;
  $db->open($table,$start,$num);
 if(trim($sortby)!='')
  $db->sortby($sortby,$order);
 $order=($order=="asc")?"desc":"asc";
 $dbValues=$db->dbValues;
 if(trim($keyword)!="")
  $dbValues=$db->query($keyword,$fields);
 if(trim($selectstring)!="")
  $dbValues=$db->select($selectcolumn,$selectstring);
echo"
<h5>Data for Table <font class=tbl>[".$table."]</font></h5>
<hr>
<table cellpadding=0 cellspacing=0>
<tr>
 <td bgcolor='#AAAAAA'>
  <table cellpadding=0 cellspacing=1>
   <tr bgcolor='#B3DBF9'>
    <td>&nbsp;<b>Actions</b>&nbsp;</td>
    <td>&nbsp;ID&nbsp;</td>
";
 foreach($db->fieldNames as $value){
  $select_string.="<option value=".$value.">".$value."</option>";
  echo"<td>&nbsp;<a href='?action=viewdata&table=$table&start=".$start."&num=".$num."&sortby=".$value."&order=".$order."'>".$value."</a>&nbsp;</td>\n";
 }
 echo "</tr>";
 if(is_array($dbValues))
 foreach($dbValues as $id=>$Values){
  echo "<tr style='background:#EEEEEE;cursor:hand' onclick=\"javascript:select(this,'#DCFAE8','#EEEEEE')\">\n";
  echo "<td><nobr>&nbsp;<a href='?action=editrecord&id=".$id."&table=".$table."&start=".$start."&num=".$num."&order=".$order."&sortby=".$sortby."'>Edit</a>&nbsp;<a href='?action=delrecord&table=".$table."&id=".$id."&start=".$start."&num=".$num."&order=".$order."&sortby=".$sortby."' onclick=\"return window.confirm('Are U sure of delete the record?');\">Del</a>&nbsp;</nobr></td>";
  echo "<td bgcolor='#B3DBF9'>&nbsp;$id&nbsp;</td>";
  if(is_array($Values))
  foreach($Values as $key=>$value){
   echo "<td><nobr>&nbsp;".htmlspecialchars($value)."&nbsp;</nobr></td>";
  }
  echo "</tr>\n";
 }
echo"
   </tr>
  </table>
 </td>
</tr>
</table>
<hr size=1>
<li>Query :<br>
<table cellpadding=0 cellspacing=0>
<tr><td bgcolor='#AAAAAA'>
<table cellpadding=0 cellspacing=1>
<tr>
 <td bgcolor='#B3DBF9' valign=bottom>
  <form method=post action='?action=viewdata&table=$table' name=iform>
   &nbsp;&nbsp;Show <br>
   &nbsp;&nbsp;<input size=5 value='$num' name='num'>&nbsp;rows [<a onclick='num.value=$db->lines' style='cursor:hand'>All</a>]&nbsp;<br>
   &nbsp;&nbsp;Starting from <br>
   &nbsp;&nbsp;<input size=5 name='start'><br>
   <input value='$order' name=order type=hidden>
   &nbsp;&nbsp;<input value='Display >>' onclick='return (!isNaN(iform.start.value) && !isNaN(iform.num.value));' type=submit>
  &nbsp;&nbsp;</form>
 </td>
 <td width=25 bgcolor='white'></td>
 <td bgcolor='#B3DBF9'>
  <form method=post action='?action=viewdata&table=$table'>
   &nbsp;&nbsp;For <input size=12 value='$keyword' name='keyword'> in&nbsp;&nbsp;<br>
   <select name='fields[]' id=fields size=4 multiple style='width:100%'>$select_string</select>
   <input value='Display >>' type=submit>&nbsp;[<a onclick='for(i=0;i<fields.length;i++)fields[i].selected=true' style='cursor:hand'>All</a>]
   <input value='$num' name=num type=hidden>
   <input value='$start' name=srart type=hidden>
   <input value='$order' name=order type=hidden>
  </form>
 </td>
 <td width=25 bgcolor='white'></td>
 <td bgcolor='#B3DBF9'>
  <form method=post action='?action=viewdata&table=$table'>
   &nbsp;&nbsp;For <input size=12 value='$selectstring' name='selectstring'> ==&nbsp;&nbsp;<br>
   <select name='selectcolumn' size=4 style='width:100%'>$select_string</select>
   <input value='Display >>' type=submit>
   <input value='$start' name=srart type=hidden>
   <input value='$num' name=num type=hidden>
   <input value='$order' name=order type=hidden>
  </form>
 </td>
</tr>
</table>
</td></tr></table>
<hr size=1>
<li>Records : $db->lines
<li><a href='?action=insertrecord&table=$table'>Insert record</a>
<li><a href='?action=cleartable&table=".$table."' onclick=\"return window.confirm('Are U sure of clear the data?');\">Clear</a> or <a href='?action=droptable&table=".$table."' onclick=\"return window.confirm('Are U sure of drop the table?');\">Drop</a> this table.
<li>";
$db->times();
}
/*
select table
*/
function selecttable($table){
 global $db;
 $db->open($table,0,1);
 echo "<h5>Select Table <font class=tbl>[".$table."]</font></h5><hr>";
 echo "<li>Path : ".$db->tableName;
 echo "<li>Size : ".number_format(filesize($db->tableName))." (bytes)";
 echo "<li>Records : ".$db->lines;
 echo "<li>Fields :".$db->fields."<br>";
 echo "<select size=8 name=field style='width:200'>";
 foreach($db->fieldNames as $value){
  echo "<option value=".$value.">".$value;
 }
 echo "</select>";
 echo "<input value='Add >>' type=submit>";
 echo "<input value='Drop >>' type=submit>";
 echo "<input value='Insert >>' type=submit>";
 echo "<li><a href='?action=viewdata&table=".$table."'>View data</a>";
 echo "<li><a href='?action=insertrecord&table=".$table."'>Insert record</a>";
 echo "<li><a href='?action=cleartable&table=".$table."' onclick=\"return window.confirm('Are U sure of clear the data?');\">Clear</a> or <a href='?action=droptable&table=".$table."' onclick=\"return window.confirm('Are U sure of drop the table?');\">Drop</a> this table.";
 echo "<li>Time of last modification : ".date ("F d Y H:i:s.", filemtime($db->tableName))."<br>";
 clearstatcache();
 $db->times();
}
/*
clear table
*/
function cleartable($table){
 global $db;
 $db->open($table,0,1);
 $db->clear();
 echo "<h5>The table <font class=tbl>[".$table."]</font> has been cleared !</h5><hr>";
}
/*
drop table
*/
function droptable($table){
 global $db;
 $db->open($table,0,1);
 $db->drop();
 echo "<h5>The table <font class=tbl>[".$table."]</font> has been droped !</h5><hr>";
 echo "<script>top.nav.location.reload();</script>";
}
/*
create table
*/
function newtable($table,$tblfields){
 global $db;
 if(isset($table) and isset($tblfields)){
  if(!ereg("^[-a-zA-Z0-9_\.]",$table))
   die('<font color=red>Table name <font class=tbl>['.$table.']</font> not allow!</font>');
  $tblfields=str_replace("\n","",$tblfields);
  $fields=explode("\r",$tblfields);
  foreach($fields as $key=>$value){
   if(trim($value)=="")unset($fields[$key]);
  }
  if($db->create($table,$fields)){
   echo "<br><h5>The table <font class=tbl>[".$table."]</font> has been created !</h5><br>";
   echo "<script>top.nav.location.reload();</script>";
   die();
  }
 }
 ?>
 <body>
 <h5>Create New Table</h5>
 <hr>
 <form action='?action=newtable' method=post>
 TableName:<br>
 <input name=table style="width:200"><br>
 Fields: (field per line)<br>
 <textarea name=tblfields rows=12 style="width:200"></textarea>&nbsp;<input type="submit" value="Create >>">
 </form>
 </body>
<?
}
/*
show DB information
*/
function main(){
 global $version,$islogin;
 $httphost=$_ENV["HTTP_HOST"];
 $server=$_SERVER["SERVER_SOFTWARE"];
 $php=PHP_VERSION;
 $os=PHP_OS;
echo"
 <body onselectstart='return false;'>
  <br><br><br>
  <nobr><h5>Welcome to TxtDb Manager </h5></nobr>
  <hr width='40%' align=left><br>
  <li>TxtDb $version running on $httphost
  <li>PHP $php running on $os
  <li>Server_SoftWare : $server";
 if(!$islogin){
  echo "
  <form action='?action=main' method=post>
   <input name=u_admin>
   <input name=u_password type=password>
   <input type=submit value='Landing >>'>
  </form>
  ";
 } 
echo "<br><hr width='60%' align=left></body>";
}
/*
navigation bar
*/
function leftbar(){
 global $db,$islogin;
?>
<style>
 body{background:#444444;color:white;}
 a{color:white;}
 a:hover{color:red;}
 hr{color:white;}
 td{height:20}
</style>
<body onselectstart="return false;">
 <a href='?action=main' target='manager'><b><?=$db->root?></b></a>
 <hr size=1>
<?echo"
 <table width='100%' cellpadding=0 cellspacing=0>
 ";
 showTables($db->root,$db->exten);
 echo"
 </table>
 <hr size=1><br>
 <b>Actions</b>
 <hr size=1>
 <table width='100%' cellpadding=0 cellspacing=0>
  <tr onclick=select(this,'#000000','')>
   <td nowrap><font face=Wingdings>w</font>&nbsp;<a href='?action=newtable' target=manager>Create Table</a></td>
  </tr>";
 if($islogin)echo"
  <tr onclick=select(this,'#000000','')>
   <td nowrap><font face=Wingdings>w</font>&nbsp;<a href='?action=quit' target=manager>Exit Manager</a></td>
  </tr>";
echo"
 </table>
 <hr size=1><br>
</body>";
}
/*
show All tables
*/
function showtables($path){
 global $db;
 $dirhandle=opendir($path);
 while($file=readdir($dirhandle)){
  if ($file!="." && $file!=".."){
   if (is_dir($path.'/'.$file)){
    showtables($path.'/'.$file);
   }else{
    $path_info=pathinfo($file);
    $exten=strtolower(".".$path_info["extension"]);
    if($exten==strtolower($db->exten)){
    if(strpos($path,'/')){
     $file=str_replace($db->root.'/','',$path.'/'.$file);
     $subdir="&nbsp;";
    }
    $file=str_replace($db->exten,'',$file);
    echo "
    <tr onclick=select(this,'#000000','')>
     <td nowrap>
      $subdir<font face=Wingdings>w</font>&nbsp;<a href='?action=selecttable&table=$file' target='manager'>$file</a>
     </td>
    </tr>
    ";
    }//
   }
  }
 }
 closedir($dirhandle);
}
/*
index of this manager
*/
function index(){
print'
<frameset name="frms" cols="150,*" rows="*" border="0" frameborder="0">
 <frame src="?action=leftbar" name="nav">
 <frame src="?action=main" name="manager">
</frameset>';
}

本文地址:http://com.8s8s.com/it/it29224.htm