Advertisement
Guest User

phpliteadmin for FusionPBX

a guest
Apr 30th, 2011
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 101.22 KB | None | 0 0
  1. <?php
  2.  
  3. require_once "includes/checkauth.php";
  4.  
  5. if (ifgroup("superadmin")) {
  6.         //access granted
  7. }
  8. else {
  9.         echo "access denied";
  10.         exit;
  11. }
  12.  
  13. //
  14. //  Project: phpLiteAdmin (http://phpliteadmin.googlecode.com)
  15. //  Version: 1.8.3
  16. //  Summary: PHP-based admin tool to manage SQLite2 and SQLite3 databases on the web
  17. //  Last updated: 4/21/11
  18. //  Developers:
  19. //     Dane Iracleous (daneiracleous@gmail.com)
  20. //     Ian Aldrighetti (ian.aldrighetti@gmail.com)
  21. //     George Flanagin & Digital Gaslight, Inc (george@digitalgaslight.com)
  22. //
  23. //
  24. //  Copyright (C) 2011  Dane Iracleous
  25. //
  26. //  This program is free software: you can redistribute it and/or modify
  27. //  it under the terms of the GNU General Public License as published by
  28. //  the Free Software Foundation, either version 3 of the License, or
  29. //  (at your option) any later version.
  30. //
  31. //  This program is distributed in the hope that it will be useful,
  32. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34. //  GNU General Public License for more details.
  35. //
  36. //  You should have received a copy of the GNU General Public License
  37. //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  38. //
  39. ///////////////////////////////////////////////////////////////////////////
  40.  
  41.  
  42. //please report any bugs you encounter to http://code.google.com/p/phpliteadmin/issues/list
  43.  
  44. //password to gain access (change this to something more secure than 'admin')
  45. // $password = "admin"; //commented out to make authing from fusion
  46.  
  47. //an array of databases that will appear in the application
  48. //if any of the databases do not exist as they are referenced by their path, they will be created automatically if possible
  49. //the SQLite version of each database is determined automatically
  50. $databases = array
  51. (
  52.     array
  53.     (
  54.         "path"=> "secure/fusionpbx.db", //path to database file on server relative to phpliteadmin.php (this file you are editing)
  55.         "name"=> "FusionPBX" //name of database to appear in application
  56.     ),
  57.     array
  58.     (
  59.         "path"=> "secure/nibblebill.db",
  60.         "name"=> "NibbleDB"
  61.     )
  62. );
  63.  
  64. // What should the name of the cookie be which contains the current password?
  65. // Changing this allows multiple phpLiteAdmin installs to work under the same domain.
  66. // $cookie_name = 'pla3412'; //commented out for fusion auth
  67.  
  68.  
  69. //end of the variables you may need to edit
  70.  
  71. //session_start(); //don't mess with this - required for the login session
  72. date_default_timezone_set(date_default_timezone_get()); //needed to fix STRICT warnings about timezone issues
  73.  
  74. //toggle error reporting
  75. //ini_set("display_errors", 1);
  76. //error_reporting(E_STRICT | E_ALL);
  77.  
  78. $startTimeTot = microtime(true); //start the timer to record page load time
  79.  
  80. //the salt and password encrypting is probably unnecessary protection but is done just for the sake of being very secure
  81. //create a random salt for this session if a cookie doesn't already exist for it
  82. //if(!isset($_SESSION['salt']) && !isset($_COOKIE['salt']))
  83. //{
  84. //  $n = rand(10e16, 10e20);
  85. //  $_SESSION['salt'] = base_convert($n, 10, 36);
  86. //}
  87. //else if(!isset($_SESSION['salt']) && isset($_COOKIE['salt'])) //session doesn't exist, but cookie does so grab it
  88. //{
  89. //  $_SESSION['salt'] = $_COOKIE['salt'];
  90. //}
  91.  
  92. //build the basename of this file for later reference
  93. $info = pathinfo($_SERVER['PHP_SELF']);
  94. $thisName = $info['basename'];
  95.  
  96. //constants
  97. define("PROJECT", "phpLiteAdmin");
  98. define("VERSION", "1.8.3");
  99. define("PAGE", $thisName);
  100. //define("COOKIENAME", $cookie_name);
  101. //define("SYSTEMPASSWORD", $password); // Makes things easier.
  102. //define("SYSTEMPASSWORDENCRYPTED", md5($password."_".$_SESSION['salt'])); //extra security - salted and encrypted password used for checking
  103.  
  104. //data types array
  105. $types = array("INTEGER", "REAL", "TEXT", "BLOB");
  106. define("DATATYPES", serialize($types));
  107. //
  108. // Authorization class
  109. // Maintains user's logged-in state and security of application
  110. //
  111. //class Authorization
  112. //{
  113. //  public function grant($remember)
  114. //  {
  115. //      if($remember) //user wants to be remembered, so set a cookie
  116. //      {
  117. //          $expire = time()+60*60*24*30; //set expiration to 1 month from now
  118. //          setcookie(COOKIENAME, SYSTEMPASSWORD, $expire);
  119. //          setcookie(COOKIENAME."_salt", $_SESSION['salt'], $expire);
  120. //      }
  121. //      else
  122. //      {
  123. //          //user does not want to be remembered, so destroy any potential cookies
  124. //          setcookie(COOKIENAME, "", time()-86400);
  125. //          setcookie(COOKIENAME."_salt", "", time()-86400);
  126. //          unset($_COOKIE[COOKIENAME]);
  127. //          unset($_COOKIE[COOKIENAME.'_salt']);
  128. //      }
  129. //
  130. //      $_SESSION['password'] = SYSTEMPASSWORDENCRYPTED;
  131. //  }
  132. //  public function revoke()
  133. //  {
  134. //      //destroy everything - cookies and session vars
  135. //      setcookie(COOKIENAME, "", time()-86400);
  136. //      setcookie("salt", "", time()-86400);
  137. //      unset($_COOKIE[COOKIENAME]);
  138. //      unset($_COOKIE[COOKIENAME.'_salt']);
  139. //      session_unset();
  140. //      session_destroy();
  141. //  }
  142. //  public function isAuthorized()
  143. //  {
  144.         // Is this just session long?
  145. //      if((isset($_SESSION['password']) && $_SESSION['password'] == SYSTEMPASSWORDENCRYPTED) || (isset($_COOKIE[COOKIENAME]) && isset($_COOKIE[COOKIENAME.'_salt']) && md5($_COOKIE[COOKIENAME]."_".$_COOKIE[COOKIENAME.'_salt']) == SYSTEMPASSWORDENCRYPTED))
  146. //          return true;
  147. //      else
  148. //      {
  149. //          return false;
  150. //      }
  151. //  }
  152. //}
  153.  
  154. //
  155. // Database class
  156. // Generic database abstraction class to manage interaction with database without worrying about SQLite vs. PHP versions
  157. //
  158. class Database
  159. {
  160.     protected $db; //reference to the DB object
  161.     protected $type; //the extension for PHP that handles SQLite
  162.     protected $data;
  163.     protected $lastResult;
  164.  
  165.     public function __construct($data)
  166.     {
  167.         $this->data = $data;
  168.         try
  169.         {
  170.             if(file_exists($this->data["path"]) && !is_writable($this->data["path"])) //make sure the actual database file is writable
  171.             {
  172.                 echo "<div class='confirm' style='margin:20px;'>";
  173.                 echo "The database, '".$this->data["path"]."', is not writable. The application is unusable until you make it writable.";
  174.                 echo "</div><br/>";
  175.                 exit();
  176.             }
  177.  
  178.             if(!file_exists($this->data["path"]) && !is_writable(dirname($this->data["path"]))) //make sure the containing directory is writable if the database does not exist
  179.             {
  180.                 echo "<div class='confirm' style='margin:20px;'>";
  181.                 echo "The database, '".$this->data["path"]."', does not exist and cannot be created because the containing directory, '".dirname($this->data["path"])."', is not writable. The application is unusable until you make it writable.";
  182.                 echo "</div><br/>";
  183.                 exit();
  184.             }
  185.  
  186.             $ver = $this->getVersion();
  187.  
  188.             switch(true)
  189.             {
  190.                 case class_exists("PDO") && ($ver==-1 || $ver==3):
  191.                     $this->db = new PDO("sqlite:".$this->data['path']);
  192.                     if($this->db!=NULL)
  193.                     {
  194.                         $this->type = "PDO";
  195.                         break;
  196.                     }
  197.                 case class_exists("SQLite3") && ($ver==-1 || $ver==3):
  198.                     $this->db = new SQLite3($this->data['path']);
  199.                     if($this->db!=NULL)
  200.                     {
  201.                         $this->type = "SQLite3";
  202.                         break;
  203.                     }
  204.                 case class_exists("SQLiteDatabase") && ($ver==-1 || $ver==2):
  205.                     $this->db = new SQLiteDatabase($this->data['path']);
  206.                     if($this->db!=NULL)
  207.                     {
  208.                         $this->type = "SQLiteDatabase";
  209.                         break;
  210.                     }
  211.                 default:
  212.                     $this->showError();
  213.                     exit();
  214.             }
  215.         }
  216.         catch(Exception $e)
  217.         {
  218.             $this->showError();
  219.             exit();
  220.         }
  221.     }
  222.  
  223.     public function showError()
  224.     {
  225.         $classPDO = class_exists("PDO");
  226.         $classSQLite3 = class_exists("SQLite3");
  227.         $classSQLiteDatabase = class_exists("SQLiteDatabase");
  228.         if($classPDO)
  229.             $strPDO = "installed";
  230.         else
  231.             $strPDO = "not installed";
  232.         if($classSQLite3)
  233.             $strSQLite3 = "installed";
  234.         else
  235.             $strSQLite3 = "not installed";
  236.         if($classSQLiteDatabase)
  237.             $strSQLiteDatabase = "installed";
  238.         else
  239.             $strSQLiteDatabase = "not installed";
  240.         echo "<div class='confirm' style='margin:20px;'>";
  241.         echo "There was a problem setting up your database, ".$this->getPath().". An attempt will be made to find out what's going on so you can fix the problem more easily.<br/><br/>";
  242.         echo "<i>Checking supported SQLite PHP extensions...<br/><br/>";
  243.         echo "<b>PDO</b>: ".$strPDO."<br/>";
  244.         echo "<b>SQLite3</b>: ".$strSQLite3."<br/>";
  245.         echo "<b>SQLiteDatabase</b>: ".$strSQLiteDatabase."<br/><br/>...done.</i><br/><br/>";
  246.         if(!$classPDO && !$classSQLite3 && !$classSQLiteDatabase)
  247.             echo "It appears that none of the supported SQLite library extensions are available in your installation of PHP. You may not use ".PROJECT." until you install at least one of them.";
  248.         else
  249.         {
  250.             if(!$classPDO && !$classSQLite3 && $this->getVersion()==3)
  251.                 echo "It appears that your database is of SQLite version 3 but your installation of PHP does not contain the necessary extensions to handle this version. To fix the problem, either delete the database and allow ".PROJECT." to create it automatically or recreate it manually as SQLite version 2.";
  252.             else if(!$classSQLiteDatabase && $this->getVersion()==2)
  253.                 echo "It appears that your database is of SQLite version 2 but your installation of PHP does not contain the necessary extensions to handle this version. To fix the problem, either delete the database and allow ".PROJECT." to create it automatically or recreate it manually as SQLite version 3.";
  254.             else
  255.                 echo "The problem cannot be diagnosed properly. Please email me at daneiracleous@gmail.com with your database as an attachment and the contents of this error message. It may be that your database is simply not a valid SQLite database, but this is not certain.";
  256.         }
  257.         echo "</div><br/>";
  258.     }
  259.  
  260.     public function __destruct()
  261.     {
  262.         if($this->db)
  263.             $this->close();
  264.     }
  265.  
  266.     //get the exact PHP extension being used for SQLite
  267.     public function getType()
  268.     {
  269.         return $this->type;
  270.     }
  271.  
  272.     //get the name of the database
  273.     public function getName()
  274.     {
  275.         return $this->data["name"];
  276.     }
  277.  
  278.     //get the filename of the database
  279.     public function getPath()
  280.     {
  281.         return $this->data["path"];
  282.     }
  283.  
  284.     //get the version of the database
  285.     public function getVersion()
  286.     {
  287.         if(file_exists($this->data['path'])) //make sure file exists before getting its contents
  288.         {
  289.             $content = strtolower(file_get_contents($this->data['path'], NULL, NULL, 0, 40)); //get the first 40 characters of the database file
  290.             $p = strpos($content, "** this file contains an sqlite 2"); //this text is at the beginning of every SQLite2 database
  291.             if($p!==false) //the text is found - this is version 2
  292.                 return 2;
  293.             else
  294.                 return 3;
  295.         }
  296.         else //return -1 to indicate that it does not exist and needs to be created
  297.         {
  298.             return -1;
  299.         }
  300.     }
  301.  
  302.     //get the size of the database
  303.     public function getSize()
  304.     {
  305.         return round(filesize($this->data["path"])*0.0009765625, 1)." Kb";
  306.     }
  307.  
  308.     //get the last modified time of database
  309.     public function getDate()
  310.     {
  311.         return date("g:ia \o\\n F j, Y", filemtime($this->data["path"]));
  312.     }
  313.  
  314.     //get number of affected rows from last query
  315.     public function getAffectedRows()
  316.     {
  317.         if($this->type=="PDO")
  318.             return $this->lastResult->rowCount();
  319.         else if($this->type=="SQLite3")
  320.             return $this->db->changes();
  321.         else if($this->type=="SQLiteDatabase")
  322.             return $this->db->changes();
  323.     }
  324.  
  325.     public function close()
  326.     {
  327.         if($this->type=="PDO")
  328.             $this->db = NULL;
  329.         else if($this->type=="SQLite3")
  330.             $this->db->close();
  331.         else if($this->type=="SQLiteDatabase")
  332.             $this->db = NULL;
  333.     }
  334.  
  335.     public function beginTransaction()
  336.     {
  337.         $this->query("BEGIN");
  338.     }
  339.  
  340.     public function commitTransaction()
  341.     {
  342.         $this->query("COMMIT");
  343.     }
  344.  
  345.     public function rollbackTransaction()
  346.     {
  347.         $this->query("ROLLBACK");
  348.     }
  349.  
  350.     //generic query wrapper
  351.     public function query($query, $ignoreAlterCase=false)
  352.     {
  353.         if(strtolower(substr(ltrim($query),0,5))=='alter' && $ignoreAlterCase==false) //this query is an ALTER query - call the necessary function
  354.         {
  355.             $queryparts = preg_split("/[\s]+/", $query, 4, PREG_SPLIT_NO_EMPTY);
  356.             $tablename = $queryparts[2];
  357.             $alterdefs = $queryparts[3];
  358.             //echo $query;
  359.             $result = $this->alterTable($tablename, $alterdefs);
  360.         }
  361.         else //this query is normal - proceed as normal
  362.             $result = $this->db->query($query);
  363.         $this->lastResult = $result;
  364.         return $result;
  365.     }
  366.  
  367.     //wrapper for an INSERT and returns the ID of the inserted row
  368.     public function insert($query)
  369.     {
  370.         $result = $this->query($query);
  371.         if($this->type=="PDO")
  372.             return $this->db->lastInsertId();
  373.         else if($this->type=="SQLite3")
  374.             return $this->db->lastInsertRowID();
  375.         else if($this->type=="SQLiteDatabase")
  376.             return $this->db->lastInsertRowid();
  377.     }
  378.  
  379.     //returns an array for SELECT
  380.     public function select($query, $mode="both")
  381.     {
  382.         $result = $this->query($query);
  383.         if($this->type=="PDO")
  384.         {
  385.             if($mode=="assoc")
  386.                 $mode = PDO::FETCH_ASSOC;
  387.             else if($mode=="num")
  388.                 $mode = PDO::FETCH_NUM;
  389.             else
  390.                 $mode = PDO::FETCH_BOTH;
  391.             return $result->fetch($mode);
  392.         }
  393.         else if($this->type=="SQLite3")
  394.         {
  395.             if($mode=="assoc")
  396.                 $mode = SQLITE3_ASSOC;
  397.             else if($mode=="num")
  398.                 $mode = SQLITE3_NUM;
  399.             else
  400.                 $mode = SQLITE3_BOTH;
  401.             return $result->fetchArray($mode);
  402.         }
  403.         else if($this->type=="SQLiteDatabase")
  404.         {
  405.             if($mode=="assoc")
  406.                 $mode = SQLITE_ASSOC;
  407.             else if($mode=="num")
  408.                 $mode = SQLITE_NUM;
  409.             else
  410.                 $mode = SQLITE_BOTH;
  411.             return $result->fetch($mode);
  412.         }
  413.     }
  414.  
  415.     //returns an array of arrays after doing a SELECT
  416.     public function selectArray($query, $mode="both")
  417.     {
  418.         $result = $this->query($query);
  419.         if($this->type=="PDO")
  420.         {
  421.             if($mode=="assoc")
  422.                 $mode = PDO::FETCH_ASSOC;
  423.             else if($mode=="num")
  424.                 $mode = PDO::FETCH_NUM;
  425.             else
  426.                 $mode = PDO::FETCH_BOTH;
  427.             return $result->fetchAll($mode);
  428.         }
  429.         else if($this->type=="SQLite3")
  430.         {
  431.             if($mode=="assoc")
  432.                 $mode = SQLITE3_ASSOC;
  433.             else if($mode=="num")
  434.                 $mode = SQLITE3_NUM;
  435.             else
  436.                 $mode = SQLITE3_BOTH;
  437.             $arr = array();
  438.             $i = 0;
  439.             while($res = $result->fetchArray($mode))
  440.             {
  441.                 $arr[$i] = $res;
  442.                 $i++;
  443.             }
  444.             return $arr;
  445.         }
  446.         else if($this->type=="SQLiteDatabase")
  447.         {
  448.             if($mode=="assoc")
  449.                 $mode = SQLITE_ASSOC;
  450.             else if($mode=="num")
  451.                 $mode = SQLITE_NUM;
  452.             else
  453.                 $mode = SQLITE_BOTH;
  454.             return $result->fetchAll($mode);
  455.         }
  456.     }
  457.  
  458.     //function that is called for an alter table statement in a query
  459.     //code borrowed with permission from http://code.jenseng.com/db/
  460.     public function alterTable($table, $alterdefs)
  461.     {
  462.         if($alterdefs != '')
  463.         {
  464.             $tempQuery = "SELECT sql,name,type FROM sqlite_master WHERE tbl_name = '".$table."' ORDER BY type DESC";
  465.             $result = $this->query($tempQuery);
  466.             $resultArr = $this->selectArray($tempQuery);
  467.  
  468.             if(sizeof($resultArr)>0)
  469.             {
  470.                 $row = $this->select($tempQuery); //table sql
  471.                 $tmpname = 't'.time();
  472.                 $origsql = trim(preg_replace("/[\s]+/", " ", str_replace(",", ", ",preg_replace("/[\(]/", "( ", $row['sql'], 1))));
  473.                 $createtemptableSQL = 'CREATE TEMPORARY '.substr(trim(preg_replace("'".$table."'", $tmpname, $origsql, 1)), 6);
  474.                 $createindexsql = array();
  475.                 $i = 0;
  476.                 $defs = preg_split("/[,]+/",$alterdefs, -1, PREG_SPLIT_NO_EMPTY);
  477.                 $prevword = $table;
  478.                 $oldcols = preg_split("/[,]+/", substr(trim($createtemptableSQL), strpos(trim($createtemptableSQL), '(')+1), -1, PREG_SPLIT_NO_EMPTY);
  479.                 $newcols = array();
  480.                 for($i=0; $i<sizeof($oldcols); $i++)
  481.                 {
  482.                     $colparts = preg_split("/[\s]+/", $oldcols[$i], -1, PREG_SPLIT_NO_EMPTY);
  483.                     $oldcols[$i] = $colparts[0];
  484.                     $newcols[$colparts[0]] = $colparts[0];
  485.                 }
  486.                 $newcolumns = '';
  487.                 $oldcolumns = '';
  488.                 reset($newcols);
  489.                 while(list($key, $val) = each($newcols))
  490.                 {
  491.                     $newcolumns .= ($newcolumns?', ':'').$val;
  492.                     $oldcolumns .= ($oldcolumns?', ':'').$key;
  493.                 }
  494.                 $copytotempsql = 'INSERT INTO '.$tmpname.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$table;
  495.                 $dropoldsql = 'DROP TABLE '.$table;
  496.                 $createtesttableSQL = $createtemptableSQL;
  497.                 foreach($defs as $def)
  498.                 {
  499.                     $defparts = preg_split("/[\s]+/", $def,-1, PREG_SPLIT_NO_EMPTY);
  500.                     $action = strtolower($defparts[0]);
  501.                     switch($action)
  502.                     {
  503.                         case 'add':
  504.                             if(sizeof($defparts) <= 2)
  505.                                 return false;
  506.                             $createtesttableSQL = substr($createtesttableSQL, 0, strlen($createtesttableSQL)-1).',';
  507.                             for($i=1;$i<sizeof($defparts);$i++)
  508.                                 $createtesttableSQL.=' '.$defparts[$i];
  509.                             $createtesttableSQL.=')';
  510.                             break;
  511.                         case 'change':
  512.                             if(sizeof($defparts) <= 3)
  513.                             {
  514.                                 return false;
  515.                             }
  516.                             if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' '))
  517.                             {
  518.                                 if($newcols[$defparts[1]] != $defparts[1])
  519.                                     return false;
  520.                                 $newcols[$defparts[1]] = $defparts[2];
  521.                                 $nextcommapos = strpos($createtesttableSQL,',',$severpos);
  522.                                 $insertval = '';
  523.                                 for($i=2;$i<sizeof($defparts);$i++)
  524.                                     $insertval.=' '.$defparts[$i];
  525.                                 if($nextcommapos)
  526.                                     $createtesttableSQL = substr($createtesttableSQL,0,$severpos).$insertval.substr($createtesttableSQL,$nextcommapos);
  527.                                 else
  528.                                     $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1)).$insertval.')';
  529.                             }
  530.                             else
  531.                                 return false;
  532.                             break;
  533.                         case 'drop':
  534.                             if(sizeof($defparts) < 2)
  535.                                 return false;
  536.                             if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' '))
  537.                             {
  538.                                 $nextcommapos = strpos($createtesttableSQL,',',$severpos);
  539.                                 if($nextcommapos)
  540.                                     $createtesttableSQL = substr($createtesttableSQL,0,$severpos).substr($createtesttableSQL,$nextcommapos + 1);
  541.                                 else
  542.                                     $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1) - 1).')';
  543.                                 unset($newcols[$defparts[1]]);
  544.                             }
  545.                             else
  546.                                 return false;
  547.                             break;
  548.                         default:
  549.                             return false;
  550.                     }
  551.                     $prevword = $defparts[sizeof($defparts)-1];
  552.                 }
  553.                 //this block of code generates a test table simply to verify that the columns specifed are valid in an sql statement
  554.                 //this ensures that no reserved words are used as columns, for example
  555.                 $tempResult = $this->query($createtesttableSQL);
  556.                 if(!$tempResult)
  557.                     return false;
  558.                 $droptempsql = 'DROP TABLE '.$tmpname;
  559.                 $tempResult = $this->query($droptempsql);
  560.                 //end block
  561.  
  562.                 $createnewtableSQL = 'CREATE '.substr(trim(preg_replace("'".$tmpname."'", $table, $createtesttableSQL, 1)), 17);
  563.                 $newcolumns = '';
  564.                 $oldcolumns = '';
  565.                 reset($newcols);
  566.                 while(list($key,$val) = each($newcols))
  567.                 {
  568.                     $newcolumns .= ($newcolumns?', ':'').$val;
  569.                     $oldcolumns .= ($oldcolumns?', ':'').$key;
  570.                 }
  571.                 $copytonewsql = 'INSERT INTO '.$table.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$tmpname;
  572.  
  573.                 $this->query($createtemptableSQL); //create temp table
  574.                 $this->query($copytotempsql); //copy to table
  575.                 $this->query($dropoldsql); //drop old table
  576.  
  577.                 $this->query($createnewtableSQL); //recreate original table
  578.                 $this->query($copytonewsql); //copy back to original table
  579.                 $this->query($droptempsql); //drop temp table
  580.             }
  581.             else
  582.             {
  583.                 return false;
  584.             }
  585.             return true;
  586.         }
  587.     }
  588.  
  589.     //multiple query execution
  590.     public function multiQuery($query)
  591.     {
  592.         if($this->type=="PDO")
  593.         {
  594.             $this->db->exec($query);
  595.         }
  596.         else if($this->type=="SQLite3")
  597.         {
  598.             $this->db->exec($query);
  599.         }
  600.         else
  601.         {
  602.             $this->db->queryExec($query);
  603.         }
  604.     }
  605.  
  606.     //get number of rows in table
  607.     public function numRows($table)
  608.     {
  609.         $result = $this->select("SELECT Count(*) FROM ".$table);
  610.         return $result[0];
  611.     }
  612.  
  613.     //correctly escape a string to be injected into an SQL query
  614.     public function quote($value)
  615.     {
  616.         if($this->type=="PDO")
  617.         {
  618.             return $this->db->quote($value);
  619.         }
  620.         else if($this->type=="SQLite3")
  621.         {
  622.             return $this->db->escapeString($value);
  623.         }
  624.         else
  625.         {
  626.             return "'".$value."'";
  627.         }
  628.     }
  629.  
  630.     //correctly format a string value from a table before showing it
  631.     public function formatString($value)
  632.     {
  633.         return htmlspecialchars(stripslashes($value));
  634.     }
  635.  
  636.     //import
  637.     public function import($query)
  638.     {
  639.         $this->multiQuery($query);
  640.     }
  641.  
  642.     //export
  643.     public function export($tables, $drop, $structure, $data, $transaction, $comments)
  644.     {
  645.         if($comments)
  646.         {
  647.             echo "----\r\n";
  648.             echo "-- phpLiteAdmin database dump (http://phpliteadmin.googlecode.com)\r\n";
  649.             echo "-- phpLiteAdmin version: ".VERSION."\r\n";
  650.             echo "-- Exported on ".date('M jS, Y, h:i:sA')."\r\n";
  651.             echo "-- Database file: ".$this->getPath()."\r\n";
  652.             echo "----\r\n";
  653.         }
  654.         $query = "SELECT * FROM sqlite_master WHERE type='table' OR type='index' ORDER BY type DESC";
  655.         $result = $this->selectArray($query);
  656.  
  657.         //iterate through each table
  658.         for($i=0; $i<sizeof($result); $i++)
  659.         {
  660.             $valid = false;
  661.             for($j=0; $j<sizeof($tables); $j++)
  662.             {
  663.                 if($result[$i]['tbl_name']==$tables[$j])
  664.                     $valid = true;
  665.             }
  666.             if($valid)
  667.             {
  668.                 if($drop)
  669.                 {
  670.                     if($comments)
  671.                     {
  672.                         echo "\r\n----\r\n";
  673.                         if($result[$i]['type']=="table")
  674.                             echo "-- Drop table for ".$result[$i]['tbl_name']."\r\n";
  675.                         else
  676.                             echo "-- Drop index for ".$result[$i]['name']."\r\n";
  677.                         echo "----\r\n";
  678.                     }
  679.                     if($result[$i]['type']=="table")
  680.                         echo "DROP TABLE '".$result[$i]['tbl_name']."';\r\n";
  681.                     else
  682.                         echo "DROP INDEX '".$result[$i]['name']."';\r\n";
  683.                 }
  684.                 if($structure)
  685.                 {
  686.                     if($comments)
  687.                     {
  688.                         echo "\r\n----\r\n";
  689.                         if($result[$i]['type']=="table")
  690.                             echo "-- Table structure for ".$result[$i]['tbl_name']."\r\n";
  691.                         else
  692.                             echo "-- Structure for index ".$result[$i]['name']." on table ".$result[$i]['tbl_name']."\r\n";
  693.                         echo "----\r\n";
  694.                     }
  695.                     echo $result[$i]['sql'].";\r\n";
  696.                 }
  697.                 if($data && $result[$i]['type']=="table")
  698.                 {
  699.                     $query = "SELECT * FROM ".$result[$i]['tbl_name'];
  700.                     $arr = $this->selectArray($query, "assoc");
  701.  
  702.                     if($comments)
  703.                     {
  704.                         echo "\r\n----\r\n";
  705.                         echo "-- Data dump for ".$result[$i]['tbl_name'].", a total of ".sizeof($arr)." rows\r\n";
  706.                         echo "----\r\n";
  707.                     }
  708.                     $query = "PRAGMA table_info('".$result[$i]['tbl_name']."')";
  709.                     $temp = $this->selectArray($query);
  710.                     $cols = array();
  711.                     $vals = array();
  712.                     for($z=0; $z<sizeof($temp); $z++)
  713.                         $cols[$z] = $temp[$z][1];
  714.                     for($z=0; $z<sizeof($arr); $z++)
  715.                     {
  716.                         for($y=0; $y<sizeof($cols); $y++)
  717.                         {
  718.                             if(!isset($vals[$z]))
  719.                                 $vals[$z] = array();
  720.                             $vals[$z][$cols[$y]] = $this->quote($arr[$z][$cols[$y]]);
  721.                         }
  722.                     }
  723.                     if($transaction)
  724.                         echo "BEGIN TRANSACTION;\r\n";
  725.                     for($j=0; $j<sizeof($vals); $j++)
  726.                         echo "INSERT INTO ".$result[$i]['tbl_name']." (".implode(",", $cols).") VALUES (".implode(",", $vals[$j]).");\r\n";
  727.                     if($transaction)
  728.                         echo "COMMIT;\r\n";
  729.                 }
  730.             }
  731.         }
  732.     }
  733. }
  734.  
  735. //$auth = new Authorization(); //create authorization object
  736. //if(isset($_POST['logout'])) //user has attempted to log out
  737. //  $auth->revoke();
  738. //else if(isset($_POST['login']) || isset($_POST['proc_login'])) //user has attempted to log in
  739. //{
  740. //  $_POST['login'] = true;
  741.  
  742. //  if($_POST['password']==SYSTEMPASSWORD) //make sure passwords match before granting authorization
  743. //  {
  744. //      if(isset($_POST['remember']))
  745. //          $auth->grant(true);
  746. //      else
  747.             //$auth->grant(false);
  748. //  }
  749. //}
  750.  
  751. //user is downloading the exported database file
  752. if(isset($_POST['export']))
  753. {
  754.     header('Content-Type: text/sql');
  755.     header('Content-Disposition: attachment; filename="'.$_POST['filename'].'.'.$_POST['export_type'].'";');
  756.     if(isset($_POST['tables']))
  757.         $tables = $_POST['tables'];
  758.     else
  759.     {
  760.         $tables = array();
  761.         $tables[0] = $_POST['single_table'];
  762.     }
  763.     $drop = isset($_POST['drop']);
  764.     $structure = isset($_POST['structure']);
  765.     $data = isset($_POST['data']);
  766.     $transaction = isset($_POST['transaction']);
  767.     $comments = isset($_POST['comments']);
  768.     $db = new Database($databases[$_SESSION['currentDB']]);
  769.     echo $db->export($tables, $drop, $structure, $data, $transaction, $comments);
  770.     exit();
  771. }
  772.  
  773. //user is importing a file
  774. if(isset($_POST['import']))
  775. {
  776.     $data = file_get_contents($_FILES["file"]["tmp_name"]);
  777.     $db = new Database($databases[$_SESSION['currentDB']]);
  778.     $db->import($data);
  779. }
  780.  
  781. // here begins the HTML.
  782. ?>
  783. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  784. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  785. <head>
  786. <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  787. <title><?php echo PROJECT ?></title>
  788.  
  789. <!-- begin the customizable stylesheet/theme -->
  790. <style type="text/css">
  791. /* overall styles for entire page */
  792. body
  793. {
  794.     margin: 0px;
  795.     padding: 0px;
  796.     font-family: Arial, Helvetica, sans-serif;
  797.     font-size: 14px;
  798.     color: #000000;
  799.     background-color: #e0ebf6;
  800. }
  801. /* general styles for hyperlink */
  802. a
  803. {
  804.     color: #03F;
  805.     text-decoration: none;
  806.     cursor :pointer;
  807. }
  808. a:hover
  809. {
  810.     color: #06F;
  811. }
  812. /* logo text containing name of project */
  813. h1
  814. {
  815.     margin: 0px;
  816.     padding: 5px;
  817.     font-size: 24px;
  818.     background-color: #f3cece;
  819.     text-align: center;
  820.     margin-bottom: 10px;
  821.     color: #000;
  822. }
  823. /* version text within the logo */
  824. h1 #version
  825. {
  826.     color: #000000;
  827.     font-size: 16px;
  828. }
  829. /* general header for various views */
  830. h2
  831. {
  832.     margin:0px;
  833.     padding:0px;
  834.     font-size:14px;
  835.     margin-bottom:20px;
  836. }
  837. /* input buttons and areas for entering text */
  838. input, select, textarea
  839. {
  840.     font-family:Arial, Helvetica, sans-serif;
  841.     background-color:#eaeaea;
  842.     color:#03F;
  843.     border-color:#03F;
  844.     border-style:solid;
  845.     border-width:1px;
  846.     margin:5px;
  847. }
  848. /* general styles for hyperlink */
  849. fieldset
  850. {
  851.     padding:15px;
  852.     border-color:#03F;
  853.     border-width:1px;
  854.     border-style:solid;
  855. }
  856. /* outer div that holds everything */
  857. #container
  858. {
  859.     padding:10px;
  860. }
  861. /* div of left box with log, list of databases, etc. */
  862. #leftNav
  863. {
  864.     float:left;
  865.     width:250px;
  866.     padding:0px;
  867.     border-color:#03F;
  868.     border-width:1px;
  869.     border-style:solid;
  870.     background-color:#FFF;
  871.     padding-bottom:15px;
  872. }
  873. /* div holding the content to the right of the leftNav */
  874. #content
  875. {
  876.     overflow:hidden;
  877.     padding-left:10px;
  878. }
  879. /* div holding the login fields */
  880. #loginBox
  881. {
  882.     width:500px;
  883.     margin-left:auto;
  884.     margin-right:auto;
  885.     margin-top:50px;
  886.     border-color:#03F;
  887.     border-width:1px;
  888.     border-style:solid;
  889.     background-color:#FFF;
  890. }
  891. /* div under tabs with tab-specific content */
  892. #main
  893. {
  894.     border-color:#03F;
  895.     border-width:1px;
  896.     border-style:solid;
  897.     padding:15px;
  898.     overflow:auto;
  899.     background-color:#FFF;
  900. }
  901. /* odd-numbered table rows */
  902. .td1
  903. {
  904.     background-color:#f9e3e3;
  905.     text-align:right;
  906.     font-size:12px;
  907.     padding-left:10px;
  908.     padding-right:10px;
  909. }
  910. /* even-numbered table rows */
  911. .td2
  912. {
  913.     background-color:#f3cece;
  914.     text-align:right;
  915.     font-size:12px;
  916.     padding-left:10px;
  917.     padding-right:10px;
  918. }
  919. /* table column headers */
  920. .tdheader
  921. {
  922.     border-color:#03F;
  923.     border-width:1px;
  924.     border-style:solid;
  925.     font-weight:bold;
  926.     font-size:12px;
  927.     padding-left:10px;
  928.     padding-right:10px;
  929.     background-color:#e0ebf6;
  930. }
  931. /* div holding the confirmation text of certain actions */
  932. .confirm
  933. {
  934.     border-color:#03F;
  935.     border-width:1px;
  936.     border-style:dashed;
  937.     padding:15px;
  938.     background-color:#e0ebf6;
  939. }
  940. /* tab navigation for each table */
  941. .tab
  942. {
  943.     display:block;
  944.     width:70px;
  945.     padding:5px;
  946.     border-color:#03F;
  947.     border-width:1px;
  948.     border-style:solid;
  949.     margin-right:5px;
  950.     float:left;
  951.     border-bottom-style:none;
  952.     position:relative;
  953.     top:1px;
  954.     padding-bottom:4px;
  955.     background-color:#eaeaea;
  956. }
  957. /* pressed state of tab */
  958. .tab_pressed
  959. {
  960.     display:block;
  961.     width:70px;
  962.     padding:5px;
  963.     border-color:#03F;
  964.     border-width:1px;
  965.     border-style:solid;
  966.     margin-right:5px;
  967.     float:left;
  968.     border-bottom-style:none;
  969.     position:relative;
  970.     top:1px;
  971.     background-color:#FFF;
  972.     cursor:default;
  973. }
  974. /* tooltip styles */
  975. #tt {position:absolute; display:block;}
  976. #tttop {display:block; height:5px; margin-left:5px; overflow:hidden}
  977. #ttcont {display:block; padding:2px 12px 3px 7px; margin-left:5px; background:#f3cece; color:#333}
  978. #ttbot {display:block; height:5px; margin-left:5px; overflow:hidden}
  979. </style>
  980. <!-- end the customizable stylesheet/theme -->
  981.  
  982. <!-- JavaScript Support -->
  983. <script type="text/javascript">
  984. //makes sure autoincrement can only be selected when integer type is selected
  985. function toggleAutoincrement(i)
  986. {
  987.     var type = document.getElementById(i+'_type');
  988.     var autoincrement = document.getElementById(i+'_autoincrement');
  989.     if(type.value=="INTEGER")
  990.         autoincrement.disabled = false;
  991.     else
  992.     {
  993.         autoincrement.disabled = true;
  994.         autoincrement.checked = false;
  995.     }
  996. }
  997. //finds and checks all checkboxes for all rows on the Browse or Structure tab for a table
  998. function checkAll(field)
  999. {
  1000.     var i=0;
  1001.     while(document.getElementById('check_'+i)!=undefined)
  1002.     {
  1003.         document.getElementById('check_'+i).checked = true;
  1004.         i++;
  1005.     }
  1006. }
  1007. //finds and unchecks all checkboxes for all rows on the Browse or Structure tab for a table
  1008. function uncheckAll(field)
  1009. {
  1010.     var i=0;
  1011.     while(document.getElementById('check_'+i)!=undefined)
  1012.     {
  1013.         document.getElementById('check_'+i).checked = false;
  1014.         i++;
  1015.     }
  1016. }
  1017. //unchecks the ignore checkbox if user has typed something into one of the fields for adding new rows
  1018. function changeIgnore(area, e)
  1019. {
  1020.     if(area.value!="")
  1021.         document.getElementById(e).checked = false;
  1022. }
  1023. //moves fields from select menu into query textarea for SQL tab
  1024. function moveFields()
  1025. {
  1026.     var fields = document.getElementById("fieldcontainer");
  1027.     var selected = new Array();
  1028.     for(var i=0; i<fields.options.length; i++)
  1029.         if(fields.options[i].selected)
  1030.             selected.push(fields.options[i].value);
  1031.     for(var i=0; i<selected.length; i++)
  1032.         insertAtCaret("queryval", "`"+selected[i]+"`");
  1033. }
  1034. //helper function for moveFields
  1035. function insertAtCaret(areaId,text)
  1036. {
  1037.     var txtarea = document.getElementById(areaId);
  1038.     var scrollPos = txtarea.scrollTop;
  1039.     var strPos = 0;
  1040.     var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ));
  1041.     if(br=="ie")
  1042.     {
  1043.         txtarea.focus();
  1044.         var range = document.selection.createRange();
  1045.         range.moveStart ('character', -txtarea.value.length);
  1046.         strPos = range.text.length;
  1047.     }
  1048.     else if(br=="ff")
  1049.         strPos = txtarea.selectionStart;
  1050.  
  1051.     var front = (txtarea.value).substring(0,strPos);
  1052.     var back = (txtarea.value).substring(strPos,txtarea.value.length);
  1053.     txtarea.value=front+text+back;
  1054.     strPos = strPos + text.length;
  1055.     if(br=="ie")
  1056.     {
  1057.         txtarea.focus();
  1058.         var range = document.selection.createRange();
  1059.         range.moveStart ('character', -txtarea.value.length);
  1060.         range.moveStart ('character', strPos);
  1061.         range.moveEnd ('character', 0);
  1062.         range.select();
  1063.     }
  1064.     else if(br=="ff")
  1065.     {
  1066.         txtarea.selectionStart = strPos;
  1067.         txtarea.selectionEnd = strPos;
  1068.         txtarea.focus();
  1069.     }
  1070.     txtarea.scrollTop = scrollPos;
  1071. }
  1072. //tooltip help feature
  1073. var tooltip=function()
  1074. {
  1075.     var id = 'tt';
  1076.     var top = 3;
  1077.     var left = 3;
  1078.     var maxw = 300;
  1079.     var speed = 10;
  1080.     var timer = 20;
  1081.     var endalpha = 95;
  1082.     var alpha = 0;
  1083.     var tt,t,c,b,h;
  1084.     var ie = document.all ? true : false;
  1085.     return{
  1086.         show:function(v,w)
  1087.         {
  1088.             if(tt == null)
  1089.             {
  1090.                 tt = document.createElement('div');
  1091.                 tt.setAttribute('id',id);
  1092.                 t = document.createElement('div');
  1093.                 t.setAttribute('id',id + 'top');
  1094.                 c = document.createElement('div');
  1095.                 c.setAttribute('id',id + 'cont');
  1096.                 b = document.createElement('div');
  1097.                 b.setAttribute('id',id + 'bot');
  1098.                 tt.appendChild(t);
  1099.                 tt.appendChild(c);
  1100.                 tt.appendChild(b);
  1101.                 document.body.appendChild(tt);
  1102.                 tt.style.opacity = 0;
  1103.                 tt.style.filter = 'alpha(opacity=0)';
  1104.                 document.onmousemove = this.pos;
  1105.             }
  1106.             tt.style.display = 'block';
  1107.             c.innerHTML = v;
  1108.             tt.style.width = w ? w + 'px' : 'auto';
  1109.             if(!w && ie)
  1110.             {
  1111.                 t.style.display = 'none';
  1112.                 b.style.display = 'none';
  1113.                 tt.style.width = tt.offsetWidth;
  1114.                 t.style.display = 'block';
  1115.                 b.style.display = 'block';
  1116.             }
  1117.             if(tt.offsetWidth > maxw)
  1118.                 tt.style.width = maxw + 'px'
  1119.             h = parseInt(tt.offsetHeight) + top;
  1120.             clearInterval(tt.timer);
  1121.             tt.timer = setInterval(function(){tooltip.fade(1)},timer);
  1122.         },
  1123.         pos:function(e)
  1124.         {
  1125.             var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
  1126.             var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
  1127.             tt.style.top = (u - h) + 'px';
  1128.             tt.style.left = (l + left) + 'px';
  1129.         },
  1130.         fade:function(d)
  1131.         {
  1132.             var a = alpha;
  1133.             if((a != endalpha && d == 1) || (a != 0 && d == -1))
  1134.             {
  1135.                 var i = speed;
  1136.                 if(endalpha - a < speed && d == 1)
  1137.                     i = endalpha - a;
  1138.                 else if(alpha < speed && d == -1)
  1139.                     i = a;
  1140.                 alpha = a + (i * d);
  1141.                 tt.style.opacity = alpha * .01;
  1142.                 tt.style.filter = 'alpha(opacity=' + alpha + ')';
  1143.             }
  1144.             else
  1145.             {
  1146.                 clearInterval(tt.timer);
  1147.                 if(d == -1)
  1148.                     tt.style.display = 'none';
  1149.             }
  1150.         },
  1151.         hide:function()
  1152.         {
  1153.             clearInterval(tt.timer);
  1154.             tt.timer = setInterval(function()
  1155.             {
  1156.                 tooltip.fade(-1)
  1157.             },timer);
  1158.         }
  1159.     };
  1160. }();
  1161. </script>
  1162. </head>
  1163. <body>
  1164. <?php
  1165. //if(!$auth->isAuthorized()) //user is not authorized - display the login screen
  1166. //{
  1167. //  echo "<div id='loginBox'>";
  1168. //  echo "<h1>".PROJECT." <span id='version'>v".VERSION."</span></h1>";
  1169. //  echo "<div style='padding:15px; text-align:center;'>";
  1170. //  if(isset($_POST['login']))
  1171. //      echo "<span style='color:red;'>Incorrect password.</span><br/><br/>";
  1172. //  echo "<form action='".PAGE."' method='post'>";
  1173. //  echo "Password: <input type='password' name='password'/><br/>";
  1174. //  echo "<input type='checkbox' name='remember' value='yes' checked='checked'/> Remember me<br/><br/>";
  1175. //  echo "<input type='submit' value='Log In' name='login'/>";
  1176. //  echo "<input type='hidden' name='proc_login' value='true' />";
  1177. //  echo "</form>";
  1178. //  echo "</div>";
  1179. //  echo "</div>";
  1180. //  echo "<br/>";
  1181. //  echo "<div style='text-align:center;'>";
  1182. //  $endTimeTot = microtime(true);
  1183. //  $timeTot = round(($endTimeTot - $startTimeTot), 4);
  1184. //  echo "<span style='font-size:11px;'>Powered by <a href='http://code.google.com/p/phpliteadmin/' target='_blank' style='font-size:11px;'>".PROJECT."</a> and <a href='http://www.danedesigns.com' target='_blank' style='font-size:11px;'>Dane Designs</a> | Page generated in ".$timeTot." seconds.</span>";
  1185. //  echo "</div>";
  1186. //}
  1187. //else //user is authorized - display the main application
  1188. {
  1189.     if(!isset($_SESSION['currentDB']))
  1190.         $_SESSION['currentDB'] = 0;
  1191.     //set the current database to the first in the array (default)
  1192.     if(sizeof($databases)>0)
  1193.         $currentDB = $databases[0];
  1194.     else //the database array is empty - show error and halt execution
  1195.     {
  1196.         echo "<div class='confirm' style='margin:20px;'>";
  1197.         echo "Error: you have not specified any databases to manage.";
  1198.         echo "</div><br/>";
  1199.         exit();
  1200.     }
  1201.  
  1202.     if(isset($_POST['database_switch'])) //user is switching database with drop-down menu
  1203.     {
  1204.         $_SESSION["currentDB"] = $_POST['database_switch'];
  1205.         $currentDB = $databases[$_SESSION['currentDB']];
  1206.     }
  1207.     if(isset($_SESSION['currentDB']))
  1208.         $currentDB = $databases[$_SESSION['currentDB']];
  1209.  
  1210.     //create the objects
  1211.     $db = new Database($currentDB); //create the Database object
  1212.  
  1213.     //switch board for various operations a user could have requested - these actions are invisible and produce no output
  1214.     if(isset($_GET['action']) && isset($_GET['confirm']))
  1215.     {
  1216.         switch($_GET['action'])
  1217.         {
  1218.             //table actions
  1219.             /////////////////////////////////////////////// create table
  1220.             case "table_create":
  1221.                 $num = intval($_POST['rows']);
  1222.                 $name = $_POST['tablename'];
  1223.                 $query = "CREATE TABLE ".$name."(";
  1224.                 for($i=0; $i<$num; $i++)
  1225.                 {
  1226.                     if($_POST[$i.'_field']!="")
  1227.                     {
  1228.                         $query .= $_POST[$i.'_field']." ";
  1229.                         $query .= $_POST[$i.'_type']." ";
  1230.                         if(isset($_POST[$i.'_primarykey']))
  1231.                             $query .= "PRIMARY KEY ";
  1232.                         if(isset($_POST[$i.'_notnull']))
  1233.                             $query .= "NOT NULL ";
  1234.                         if($_POST[$i.'_defaultvalue']!="")
  1235.                         {
  1236.                             if($_POST[$i.'_type']=="INTEGER")
  1237.                                 $query .= "default ".$_POST[$i.'_defaultvalue']."  ";
  1238.                             else
  1239.                                 $query .= "default '".$_POST[$i.'_defaultvalue']."' ";
  1240.                         }
  1241.                         $query = substr($query, 0, sizeof($query)-2);
  1242.                         $query .= ", ";
  1243.                     }
  1244.                 }
  1245.                 $query = substr($query, 0, sizeof($query)-3);
  1246.                 $query .= ")";
  1247.                 $result = $db->query($query);
  1248.                 if(!$result)
  1249.                     $error = true;
  1250.                 $completed = "Table '".$_POST['tablename']."' has been created.<br/><span style='font-size:11px;'>".$query."</span>";
  1251.                 break;
  1252.             /////////////////////////////////////////////// empty table
  1253.             case "table_empty":
  1254.                 $query = "DELETE FROM ".$_POST['tablename'];
  1255.                 $result = $db->query($query);
  1256.                 if(!$result)
  1257.                     $error = true;
  1258.                 $query = "VACUUM";
  1259.                 $result = $db->query($query);
  1260.                 if(!$result)
  1261.                     $error = true;
  1262.                 $completed = "Table '".$_POST['tablename']."' has been emptied.<br/><span style='font-size:11px;'>".$query."</span>";
  1263.                 break;
  1264.             /////////////////////////////////////////////// drop table
  1265.             case "table_drop":
  1266.                 $query = "DROP TABLE ".$_POST['tablename'];
  1267.                 $db->query($query);
  1268.                 $completed = "Table '".$_POST['tablename']."' has been dropped.";
  1269.                 break;
  1270.             /////////////////////////////////////////////// rename table
  1271.             case "table_rename":
  1272.                 $query = "ALTER TABLE ".$_POST['oldname']." RENAME TO ".$_POST['newname'];
  1273.                 if($db->getVersion()==3)
  1274.                     $result = $db->query($query, true);
  1275.                 else
  1276.                     $result = $db->query($query, false);
  1277.                 if(!$result)
  1278.                     $error = true;
  1279.                 $completed = "Table '".$_POST['oldname']."' has been renamed to '".$_POST['newname']."'.<br/><span style='font-size:11px;'>".$query."</span>";
  1280.                 break;
  1281.             //row actions
  1282.             /////////////////////////////////////////////// create row
  1283.             case "row_create":
  1284.                 $completed = "";
  1285.                 $num = $_POST['numRows'];
  1286.                 $fields = explode(":", $_POST['fields']);
  1287.                 $z = 0;
  1288.                 for($i=0; $i<$num; $i++)
  1289.                 {
  1290.                     if(!isset($_POST[$i.":ignore"]))
  1291.                     {
  1292.                         $query = "INSERT INTO ".$_GET['table']." (";
  1293.                         for($j=0; $j<sizeof($fields); $j++)
  1294.                         {
  1295.                             $query .= $fields[$j].",";
  1296.                         }
  1297.                         $query = substr($query, 0, sizeof($query)-2);
  1298.                         $query .= ") VALUES (";
  1299.                         for($j=0; $j<sizeof($fields); $j++)
  1300.                         {
  1301.                             $value = $_POST[$i.":".$fields[$j]];
  1302.                             if($value=="")
  1303.                                 $query .= "NULL,";
  1304.                             else
  1305.                                 $query .= $db->quote($value).",";
  1306.                         }
  1307.                         $query = substr($query, 0, sizeof($query)-2);
  1308.                         $query .= ")";
  1309.                         $result = $db->query($query);
  1310.                         if(!$result)
  1311.                             $error = true;
  1312.                         $completed .= "<span style='font-size:11px;'>".$query."</span><br/>";
  1313.                         $z++;
  1314.                     }
  1315.                 }
  1316.                 $completed = $z." row(s) inserted.<br/><br/>".$completed;
  1317.                 break;
  1318.             /////////////////////////////////////////////// delete row
  1319.             case "row_delete":
  1320.                 $pks = explode(":", $_GET['pk']);
  1321.                 $str = $pks[0];
  1322.                 $query = "DELETE FROM ".$_GET['table']." WHERE ROWID = ".$pks[0];
  1323.                 for($i=1; $i<sizeof($pks); $i++)
  1324.                 {
  1325.                     $str .= ", ".$pks[$i];
  1326.                     $query .= " OR ROWID = ".$pks[$i];
  1327.                 }
  1328.                 $result = $db->query($query);
  1329.                 if(!$result)
  1330.                     $error = true;
  1331.                 $completed = sizeof($pks)." row(s) deleted.<br/><span style='font-size:11px;'>".$query."</span>";
  1332.                 break;
  1333.             /////////////////////////////////////////////// edit row
  1334.             case "row_edit":
  1335.                 $pks = explode(":", $_GET['pk']);
  1336.                 $fields = explode(":", $_POST['fieldArray']);
  1337.  
  1338.                 $completed = sizeof($pks)." row(s) affected.<br/><br/>";
  1339.  
  1340.                 for($i=0; $i<sizeof($pks); $i++)
  1341.                 {
  1342.                     $query = "UPDATE ".$_GET['table']." SET ";
  1343.                     for($j=0; $j<sizeof($fields); $j++)
  1344.                     {
  1345.                         $query .= $fields[$j]."=".$db->quote($_POST[$pks[$i].":".$fields[$j]]).", ";
  1346.                     }
  1347.                     $query = substr($query, 0, sizeof($query)-3);
  1348.                     $query .= " WHERE ROWID = ".$pks[$i];
  1349.                     $result = $db->query($query);
  1350.                     if(!$result)
  1351.                     {
  1352.                         $error = true;
  1353.                     }
  1354.                     $completed .= "<span style='font-size:11px;'>".$query."</span><br/>";
  1355.                 }
  1356.                 break;
  1357.             //column actions
  1358.             /////////////////////////////////////////////// create column
  1359.             case "column_create":
  1360.                 $num = intval($_POST['rows']);
  1361.                 for($i=0; $i<$num; $i++)
  1362.                 {
  1363.                     if($_POST[$i.'_field']!="")
  1364.                     {
  1365.                         $query = "ALTER TABLE ".$_GET['table']." ADD ".$_POST[$i.'_field']." ";
  1366.                         $query .= $_POST[$i.'_type']." ";
  1367.                         if(isset($_POST[$i.'_primarykey']))
  1368.                             $query .= "PRIMARY KEY ";
  1369.                         if(isset($_POST[$i.'_notnull']))
  1370.                             $query .= "NOT NULL ";
  1371.                         if($_POST[$i.'_defaultvalue']!="")
  1372.                         {
  1373.                             if($_POST[$i.'_type']=="INTEGER")
  1374.                                 $query .= "DEFAULT ".$_POST[$i.'_defaultvalue']."  ";
  1375.                             else
  1376.                                 $query .= "DEFAULT '".$_POST[$i.'_defaultvalue']."' ";
  1377.                         }
  1378.                         if($db->getVersion()==3)
  1379.                             $result = $db->query($query, true);
  1380.                         else
  1381.                             $result = $db->query($query, false);
  1382.                         if(!$result)
  1383.                             $error = true;
  1384.                     }
  1385.                 }
  1386.                 $completed = "Table '".$_GET['table']."' has been altered successfully.";
  1387.                 break;
  1388.             /////////////////////////////////////////////// delete column
  1389.             case "column_delete":
  1390.                 $pks = explode(":", $_GET['pk']);
  1391.                 $str = $pks[0];
  1392.                 $query = "ALTER TABLE ".$_GET['table']." DROP ".$pks[0];
  1393.                 for($i=1; $i<sizeof($pks); $i++)
  1394.                 {
  1395.                     $str .= ", ".$pks[$i];
  1396.                     $query .= ", DROP ".$pks[$i];
  1397.                 }
  1398.                 $result = $db->query($query);
  1399.                 if(!$result)
  1400.                     $error = true;
  1401.                 $completed = "Table '".$_GET['table']."' has been altered successfully.";
  1402.                 break;
  1403.             /////////////////////////////////////////////// edit column
  1404.             case "column_edit":
  1405.                 $query = "ALTER TABLE ".$_GET['table']." CHANGE ".$_POST['field_old']." ".$_POST['field']." ".$_POST['type'];
  1406.                 $result = $db->query($query);
  1407.                 if(!$result)
  1408.                     $error = true;
  1409.                 $completed = "Table '".$_GET['table']."' has been altered successfully.";
  1410.                 break;
  1411.             /////////////////////////////////////////////// delete index
  1412.             case "index_delete":
  1413.                 $query = "DROP INDEX ".$_GET['pk'];
  1414.                 $result = $db->query($query);
  1415.                 if(!$result)
  1416.                     $error = true;
  1417.                 $completed = "Index '".$_GET['pk']."' deleted.<br/><span style='font-size:11px;'>".$query."</span>";
  1418.                 break;
  1419.             /////////////////////////////////////////////// create index
  1420.             case "index_create":
  1421.                 $num = $_POST['num'];
  1422.  
  1423.                 $str = "CREATE ";
  1424.                 if($_POST['duplicate']=="no")
  1425.                     $str .= "UNIQUE ";
  1426.                 $str .= "INDEX ".$_POST['name']." ON ".$_GET['table']." (";
  1427.                 $str .= $_POST['0_field'].$_POST['0_order'];
  1428.                 for($i=1; $i<$num; $i++)
  1429.                 {
  1430.                     if($_POST[$i.'_field']!="--Ignore--")
  1431.                         $str .= ", ".$_POST[$i.'_field'].$_POST[$i.'_order'];
  1432.                 }
  1433.                 $str .= ")";
  1434.                 $query = $str;
  1435.                 $result = $db->query($query);
  1436.                 if(!$result)
  1437.                     $error = true;
  1438.                 $completed = "Index created.<br/><span style='font-size:11px;'>".$query."</span>";
  1439.                 break;
  1440.         }
  1441.     }
  1442.  
  1443.     echo "<div id='container'>";
  1444.     echo "<div id='leftNav'>";
  1445.     echo "<h1>";
  1446.     echo "<a href='".PAGE."' style='color:#000;'>";
  1447.     echo PROJECT." <span id='version'>v".VERSION."</span>";
  1448.     echo "</a>";
  1449.     echo "</h1>";
  1450.     echo "<fieldset style='margin:15px;'><legend><b>Change Database</b></legend>";
  1451.     echo "<form action='".PAGE."' method='post'>";
  1452.     echo "<select name='database_switch'>";
  1453.     for($i=0; $i<sizeof($databases); $i++)
  1454.     {
  1455.         if($i==$_SESSION['currentDB'])
  1456.             echo "<option value='".$i."' selected='selected'>".$databases[$i]['name']."</option>";
  1457.         else
  1458.             echo "<option value='".$i."'>".$databases[$i]['name']."</option>";
  1459.     }
  1460.     echo "</select> ";
  1461.     echo "<input type='submit' value='Go'>";
  1462.     echo "</form>";
  1463.     echo "</fieldset>";
  1464.     echo "<fieldset style='margin:15px;'><legend>";
  1465.     echo "<a href='".PAGE."'";
  1466.     if(!isset($_GET['table']))
  1467.         echo " style='text-decoration:underline;'";
  1468.     echo ">".$currentDB['name']."</a>";
  1469.     echo "</legend>";
  1470.     //Display list of tables
  1471.     $query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
  1472.     $result = $db->selectArray($query);
  1473.     $j=0;
  1474.     for($i=0; $i<sizeof($result); $i++)
  1475.     {
  1476.         if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  1477.         {
  1478.             echo "<a href='".PAGE."?action=row_view&table=".$result[$i]['name']."'";
  1479.             if(isset($_GET['table']) && $_GET['table']==$result[$i]['name'])
  1480.                 echo " style='text-decoration:underline;'";
  1481.             echo ">".$result[$i]['name']."</a><br/>";
  1482.             $j++;
  1483.         }
  1484.     }
  1485.     if($j==0)
  1486.         echo "No tables in database.";
  1487.     echo "</fieldset>";
  1488.     echo "<div style='text-align:center;'>";
  1489.     echo "<form action='".PAGE."' method='post'/>";
  1490.     echo "<input type='submit' value='Log Out' name='logout'/>";
  1491.     echo "</form>";
  1492.     echo "</div>";
  1493.     echo "</div>";
  1494.     echo "<div id='content'>";
  1495.  
  1496.     //breadcrumb navigation
  1497.     echo "<a href='".PAGE."'>".$currentDB['name']."</a>";
  1498.     if(isset($_GET['table']))
  1499.         echo " &rarr; <a href='".PAGE."?table=".$_GET['table']."&action=row_view'>".$_GET['table']."</a>";
  1500.     echo "<br/><br/>";
  1501.  
  1502.     //user has performed some action so show the resulting message
  1503.     if(isset($_GET['confirm']))
  1504.     {
  1505.         echo "<div id='main'>";
  1506.         echo "<div class='confirm'>";
  1507.         if(isset($error) && $error) //an error occured during the action, so show an error message
  1508.             echo "An error occured. This may be a bug that needs to be reported at <a href='http://code.google.com/p/phpliteadmin/issues/list' target='_blank'>code.google.com/p/phpliteadmin/issues/list</a>";
  1509.         else //action was performed successfully - show success message
  1510.             echo $completed;
  1511.         echo "</div>";
  1512.         if($_GET['action']=="row_delete" || $_GET['action']=="row_create" || $_GET['action']=="row_edit")
  1513.             echo "<br/><br/><a href='".PAGE."?table=".$_GET['table']."&action=row_view'>Return</a>";
  1514.         else if($_GET['action']=="column_create" || $_GET['action']=="column_delete" || $_GET['action']=="column_edit" || $_GET['action']=="index_create" || $_GET['action']=="index_delete")
  1515.             echo "<br/><br/><a href='".PAGE."?table=".$_GET['table']."&action=column_view'>Return</a>";
  1516.         else
  1517.             echo "<br/><br/><a href='".PAGE."'>Return</a>";
  1518.         echo "</div>";
  1519.     }
  1520.  
  1521.     //show the various tab views for a table
  1522.     if(!isset($_GET['confirm']) && isset($_GET['table']) && isset($_GET['action']) && ($_GET['action']=="table_export" || $_GET['action']=="table_import" || $_GET['action']=="table_sql" || $_GET['action']=="row_view" || $_GET['action']=="row_create" || $_GET['action']=="column_view" || $_GET['action']=="table_rename" || $_GET['action']=="table_search"))
  1523.     {
  1524.         echo "<a href='".PAGE."?table=".$_GET['table']."&action=row_view' ";
  1525.         if($_GET['action']=="row_view")
  1526.             echo "class='tab_pressed'";
  1527.         else
  1528.             echo "class='tab'";
  1529.         echo ">Browse</a>";
  1530.         echo "<a href='".PAGE."?table=".$_GET['table']."&action=column_view' ";
  1531.         if($_GET['action']=="column_view")
  1532.             echo "class='tab_pressed'";
  1533.         else
  1534.             echo "class='tab'";
  1535.         echo ">Structure</a>";
  1536.         echo "<a href='".PAGE."?table=".$_GET['table']."&action=table_sql' ";
  1537.         if($_GET['action']=="table_sql")
  1538.             echo "class='tab_pressed'";
  1539.         else
  1540.             echo "class='tab'";
  1541.         echo ">SQL</a>";
  1542.         echo "<a href='".PAGE."?table=".$_GET['table']."&action=table_search' ";
  1543.         if($_GET['action']=="table_search")
  1544.             echo "class='tab_pressed'";
  1545.         else
  1546.             echo "class='tab'";
  1547.         echo ">Search</a>";
  1548.         echo "<a href='".PAGE."?table=".$_GET['table']."&action=row_create' ";
  1549.         if($_GET['action']=="row_create")
  1550.             echo "class='tab_pressed'";
  1551.         else
  1552.             echo "class='tab'";
  1553.         echo ">Insert</a>";
  1554.         echo "<a href='".PAGE."?table=".$_GET['table']."&action=table_export' ";
  1555.         if($_GET['action']=="table_export")
  1556.             echo "class='tab_pressed'";
  1557.         else
  1558.             echo "class='tab'";
  1559.         echo ">Export</a>";
  1560.         echo "<a href='".PAGE."?table=".$_GET['table']."&action=table_import' ";
  1561.         if($_GET['action']=="table_import")
  1562.             echo "class='tab_pressed'";
  1563.         else
  1564.             echo "class='tab'";
  1565.         echo ">Import</a>";
  1566.         echo "<a href='".PAGE."?table=".$_GET['table']."&action=table_rename' ";
  1567.         if($_GET['action']=="table_rename")
  1568.             echo "class='tab_pressed'";
  1569.         else
  1570.             echo "class='tab'";
  1571.         echo ">Rename</a>";
  1572.         echo "<a href='".PAGE."?action=table_empty&table=".$_GET['table']."' ";
  1573.         echo "class='tab' style='color:red;'";
  1574.         echo ">Empty</a>";
  1575.         echo "<a href='".PAGE."?action=table_drop&table=".$_GET['table']."' ";
  1576.         echo "class='tab' style='color:red;'";
  1577.         echo ">Drop</a>";
  1578.         echo "<div style='clear:both;'></div>";
  1579.     }
  1580.  
  1581.     //switch board for the page display
  1582.     if(isset($_GET['action']) && !isset($_GET['confirm']))
  1583.     {
  1584.         echo "<div id='main'>";
  1585.         switch($_GET['action'])
  1586.         {
  1587.             //table actions
  1588.             /////////////////////////////////////////////// create table
  1589.             case "table_create":
  1590.                 echo "<h2>Creating new table: '".$_POST['tablename']."'</h2>";
  1591.                 if($_POST['tablefields']=="" || intval($_POST['tablefields'])<=0)
  1592.                     echo "You must specify the number of table fields.";
  1593.                 else if($_POST['tablename']=="")
  1594.                     echo "You must specify a table name.";
  1595.                 else
  1596.                 {
  1597.                     $num = intval($_POST['tablefields']);
  1598.                     $name = $_POST['tablename'];
  1599.                     echo "<form action='".PAGE."?action=table_create&confirm=1' method='post'>";
  1600.                     echo "<input type='hidden' name='tablename' value='".$name."'/>";
  1601.                     echo "<input type='hidden' name='rows' value='".$num."'/>";
  1602.                     echo "<table border='0' cellpadding='2' cellspacing='1'>";
  1603.                     echo "<tr>";
  1604.                     $headings = array("Field", "Type", "Primary Key", "Autoincrement", "Not NULL", "Default Value");
  1605.                 for($k=0; $k<count($headings); $k++)
  1606.                         echo "<td class='tdheader'>" . $headings[$k] . "</td>";
  1607.                     echo "</tr>";
  1608.  
  1609.                     for($i=0; $i<$num; $i++)
  1610.                     {
  1611.                         $tdWithClass = "<td class='td" . ($i%2 ? "1" : "2") . "'>";
  1612.                         echo "<tr>";
  1613.                         echo $tdWithClass;
  1614.                         echo "<input type='text' name='".$i."_field' style='width:200px;'/>";
  1615.                         echo "</td>";
  1616.                         echo $tdWithClass;
  1617.                         echo "<select name='".$i."_type' id='".$i."_type' onchange='toggleAutoincrement(".$i.");'>";
  1618.                         $types = unserialize(DATATYPES);
  1619.                         for($z=0; $z<sizeof($types); $z++)
  1620.                             echo "<option value='".$types[$z]."'>".$types[$z]."</option>";
  1621.                         echo "</select>";
  1622.                         echo "</td>";
  1623.                         echo $tdWithClass;
  1624.                         echo "<input type='checkbox' name='".$i."_primarykey'/> Yes";
  1625.                         echo "</td>";
  1626.                         echo $tdWithClass;
  1627.                         echo "<input type='checkbox' name='".$i."_autoincrement' id='".$i."_autoincrement'/> Yes";
  1628.                         echo "</td>";
  1629.                         echo $tdWithClass;
  1630.                         echo "<input type='checkbox' name='".$i."_notnull'/> Yes";
  1631.                         echo "</td>";
  1632.                         echo $tdWithClass;
  1633.                         echo "<input type='text' name='".$i."_defaultvalue' style='width:100px;'/>";
  1634.                         echo "</td>";
  1635.                         echo "</tr>";
  1636.                     }
  1637.                     echo "<tr>";
  1638.                     echo "<td class='tdheader' style='text-align:right;' colspan='6'>";
  1639.                     echo "<input type='submit' value='Create'/> ";
  1640.                     echo "<a href='".PAGE."'>Cancel</a>";
  1641.                     echo "</td>";
  1642.                     echo "</tr>";
  1643.                     echo "</table>";
  1644.                     echo "</form>";
  1645.                 }
  1646.                 break;
  1647.             /////////////////////////////////////////////// perform SQL query on table
  1648.             case "table_sql":
  1649.                 $isSelect = false;
  1650.                 if(isset($_POST['query']) && $_POST['query']!="")
  1651.                 {
  1652.                     $delimiter = $_POST['delimiter'];
  1653.                     $queryStr = stripslashes($_POST['queryval']);
  1654.                     $query = explode($delimiter, $queryStr); //explode the query string into individual queries based on the delimiter
  1655.  
  1656.                     for($i=0; $i<sizeof($query); $i++) //iterate through the queries exploded by the delimiter
  1657.                     {
  1658.                         if(str_replace(" ", "", str_replace("\n", "", str_replace("\r", "", $query[$i])))!="") //make sure this query is not an empty string
  1659.                         {
  1660.                             $startTime = microtime(true);
  1661.                             if(strpos(strtolower($query[$i]), "select ")!==false)
  1662.                             {
  1663.                                 $isSelect = true;
  1664.                                 $result = $db->selectArray($query[$i], "assoc");
  1665.                             }
  1666.                             else
  1667.                             {
  1668.                                 $isSelect = false;
  1669.                                 $result = $db->query($query[$i]);
  1670.                             }
  1671.                             $endTime = microtime(true);
  1672.                             $time = round(($endTime - $startTime), 4);
  1673.  
  1674.                             echo "<div class='confirm'>";
  1675.                             echo "<b>";
  1676.                             if($isSelect || $result)
  1677.                             {
  1678.                                 if($isSelect)
  1679.                                 {
  1680.                                     $affected = sizeof($result);
  1681.                                     echo "Showing ".$affected." row(s). ";
  1682.                                 }
  1683.                                 else
  1684.                                 {
  1685.                                     $affected = $db->getAffectedRows();
  1686.                                     echo $affected." row(s) affected. ";
  1687.                                 }
  1688.                                 echo "(Query took ".$time." sec)</b><br/>";
  1689.                             }
  1690.                             else
  1691.                             {
  1692.                                 echo "There is a problem with the syntax of your query ";
  1693.                                 echo "(Query was not executed)</b><br/>";
  1694.                             }
  1695.                             echo "<span style='font-size:11px;'>".$query[$i]."</span>";
  1696.                             echo "</div><br/>";
  1697.                             if($isSelect)
  1698.                             {
  1699.                                 if(sizeof($result)>0)
  1700.                                 {
  1701.                                     $headers = array_keys($result[0]);
  1702.  
  1703.                                     echo "<table border='0' cellpadding='2' cellspacing='1'>";
  1704.                                     echo "<tr>";
  1705.                                     for($j=0; $j<sizeof($headers); $j++)
  1706.                                     {
  1707.                                         echo "<td class='tdheader'>";
  1708.                                         echo $headers[$j];
  1709.                                         echo "</td>";
  1710.                                     }
  1711.                                     echo "</tr>";
  1712.                                     for($j=0; $j<sizeof($result); $j++)
  1713.                                     {
  1714.                                         $tdWithClass = "<td class='td".($j%2 ? "1" : "2")."'>";
  1715.                                         echo "<tr>";
  1716.                                         for($z=0; $z<sizeof($headers); $z++)
  1717.                                         {
  1718.                                             echo $tdWithClass;
  1719.                                             echo $result[$j][$headers[$z]];
  1720.                                             echo "</td>";
  1721.                                         }
  1722.                                         echo "</tr>";
  1723.                                     }
  1724.                                     echo "</table><br/><br/>";
  1725.                                 }
  1726.                             }
  1727.                         }
  1728.                     }
  1729.                 }
  1730.                 else
  1731.                 {
  1732.                     $delimiter = ";";
  1733.                     $queryStr = "SELECT * FROM `".$_GET['table']."` WHERE 1";
  1734.                 }
  1735.  
  1736.                 echo "<fieldset>";
  1737.                 echo "<legend><b>Run SQL query/queries on database '".$db->getName()."'</b></legend>";
  1738.                 echo "<form action='".PAGE."?table=".$_GET['table']."&action=table_sql' method='post'>";
  1739.                 echo "<div style='float:left; width:70%;'>";
  1740.                 echo "<textarea style='width:97%; height:300px;' name='queryval' id='queryval'>".$queryStr."</textarea>";
  1741.                 echo "</div>";
  1742.                 echo "<div style='float:left; width:28%; padding-left:10px;'>";
  1743.                 echo "Fields<br/>";
  1744.                 echo "<select multiple='multiple' style='width:100%;' id='fieldcontainer'>";
  1745.                 $query = "PRAGMA table_info('".$_GET['table']."')";
  1746.                 $result = $db->selectArray($query);
  1747.                 for($i=0; $i<sizeof($result); $i++)
  1748.                 {
  1749.                     echo "<option value='".$result[$i][1]."'>".$result[$i][1]."</option>";
  1750.                 }
  1751.                 echo "</select>";
  1752.                 echo "<input type='button' value='<<' onclick='moveFields();'/>";
  1753.                 echo "</div>";
  1754.                 echo "<div style='clear:both;'></div>";
  1755.                 echo "Delimiter <input type='text' name='delimiter' value='".$delimiter."' style='width:50px;'/> ";
  1756.                 echo "<input type='submit' name='query' value='Go'/>";
  1757.                 echo "</form>";
  1758.                 break;
  1759.             /////////////////////////////////////////////// empty table
  1760.             case "table_empty":
  1761.                 echo "<form action='".PAGE."?action=table_empty&confirm=1' method='post'>";
  1762.                 echo "<input type='hidden' name='tablename' value='".$_GET['table']."'/>";
  1763.                 echo "<div class='confirm'>";
  1764.                 echo "Are you sure you want to empty the table '".$_GET['table']."'?<br/><br/>";
  1765.                 echo "<input type='submit' value='Confirm'/> ";
  1766.                 echo "<a href='".PAGE."'>Cancel</a>";
  1767.                 echo "</div>";
  1768.                 break;
  1769.             /////////////////////////////////////////////// drop table
  1770.             case "table_drop":
  1771.                 echo "<form action='".PAGE."?action=table_drop&confirm=1' method='post'>";
  1772.                 echo "<input type='hidden' name='tablename' value='".$_GET['table']."'/>";
  1773.                 echo "<div class='confirm'>";
  1774.                 echo "Are you sure you want to drop the table '".$_GET['table']."'?<br/><br/>";
  1775.                 echo "<input type='submit' value='Confirm'/> ";
  1776.                 echo "<a href='".PAGE."'>Cancel</a>";
  1777.                 echo "</div>";
  1778.                 break;
  1779.             /////////////////////////////////////////////// export table
  1780.             case "table_export":
  1781.                 echo "<form method='post' action='".PAGE."'>";
  1782.                 echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>Export</b></legend>";
  1783.                 echo "<input type='hidden' value='".$_GET['table']."' name='single_table'/>";
  1784.                 echo "<input type='radio' name='export_type' checked='checked' value='sql'/> SQL";
  1785.                 echo "</fieldset>";
  1786.                 echo "<fieldset style='float:left;'><legend><b>Options</b></legend>";
  1787.                 echo "<input type='checkbox' checked='checked' name='structure'/> Export with structure [<a onmouseover='tooltip.show(\"Creates the queries to add the tables and their columns\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  1788.                 echo "<input type='checkbox' checked='checked' name='data'/> Export with data [<a onmouseover='tooltip.show(\"Creates the queries to insert the table rows\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  1789.                 echo "<input type='checkbox' name='drop'/> Add DROP TABLE [<a onmouseover='tooltip.show(\"Creates the queries to remove the tables before potentially adding them so that errors do not occur if they already exist\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  1790.                 echo "<input type='checkbox' checked='checked' name='transaction'/> Add TRANSACTION [<a onmouseover='tooltip.show(\"Performs queries within transactions so that if an error occurs, the table is not returned to a partially incomplete and unusable state\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  1791.                 echo "<input type='checkbox' checked='checked' name='comments'/> Comments [<a onmouseover='tooltip.show(\"Adds comments to the file to explain what is happening in each part of it\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  1792.                 echo "</fieldset>";
  1793.                 echo "<div style='clear:both;'></div>";
  1794.                 echo "<br/><br/>";
  1795.                 echo "<fieldset style='float:left;'><legend><b>Save As</b></legend>";
  1796.                 echo "<input type='hidden' name='database_num' value='".$_SESSION['currentDB']."'/>";
  1797.                 echo "<input type='text' name='filename' value='".str_replace(" ", "_", $db->getName())."_".date("n-j-y")."' style='width:400px;'/> <input type='submit' name='export' value='Export'/>";
  1798.                 echo "</fieldset>";
  1799.                 echo "</form>";
  1800.                 break;
  1801.             /////////////////////////////////////////////// import table
  1802.             case "table_import":
  1803.                 if(isset($_POST['import']))
  1804.                 {
  1805.                     echo "<div class='confirm'>";
  1806.                     echo "Import was successful.";
  1807.                     echo "</div><br/>";
  1808.                 }
  1809.                 echo "<form method='post' action='".PAGE."?table=".$_GET['action']."&action=table_import' enctype='multipart/form-data'>";
  1810.                 echo "<fieldset><legend><b>File to import</b></legend>";
  1811.                 echo "<input type='radio' name='export_type' checked='checked' value='sql'/> SQL";
  1812.                 echo "<br/><br/>";
  1813.                 echo "<input type='file' value='Choose File' name='file' style='background-color:transparent; border-style:none;'/> <input type='submit' value='Import' name='import'/>";
  1814.                 echo "</fieldset>";
  1815.                 break;
  1816.             /////////////////////////////////////////////// rename table
  1817.             case "table_rename":
  1818.                 echo "<form action='".PAGE."?action=table_rename&confirm=1' method='post'>";
  1819.                 echo "<input type='hidden' name='oldname' value='".$_GET['table']."'/>";
  1820.                 echo "Rename table '".$_GET['table']."' to <input type='text' name='newname' style='width:200px;'/> <input type='submit' value='Rename' name='rename'/>";
  1821.                 echo "</form>";
  1822.                 break;
  1823.             /////////////////////////////////////////////// search table
  1824.             case "table_search":
  1825.                 if(isset($_GET['done']))
  1826.                 {
  1827.                     $query = "PRAGMA table_info('".$_GET['table']."')";
  1828.                     $result = $db->selectArray($query);
  1829.                     $str = "";
  1830.                     $j = 0;
  1831.                     $arr = array();
  1832.                     for($i=0; $i<sizeof($result); $i++)
  1833.                     {
  1834.                         $field = $result[$i][1];
  1835.                         $operator = $_POST[$field.":operator"];
  1836.                         $value = $_POST[$field];
  1837.                         if($value!="" || $operator=="!= ''" || $operator=="= ''")
  1838.                         {
  1839.                             if($operator=="= ''" || $operator=="!= ''")
  1840.                                 $arr[$j] .= $field." ".$operator;
  1841.                             else
  1842.                                 $arr[$j] .= $field." ".$operator." ".$db->quote($value);
  1843.                             $j++;
  1844.                         }
  1845.                     }
  1846.                     $query = "SELECT * FROM ".$_GET['table'];
  1847.                     if(sizeof($arr)>0)
  1848.                     {
  1849.                         $query .= " WHERE ".$arr[0];
  1850.                         for($i=1; $i<sizeof($arr); $i++)
  1851.                         {
  1852.                             $query .= " AND ".$arr[$i];
  1853.                         }
  1854.                     }
  1855.                     $startTime = microtime(true);
  1856.                     $result = $db->selectArray($query, "assoc");
  1857.                     $endTime = microtime(true);
  1858.                     $time = round(($endTime - $startTime), 4);
  1859.  
  1860.                     echo "<div class='confirm'>";
  1861.                     echo "<b>";
  1862.                     if($result)
  1863.                     {
  1864.                         $affected = sizeof($result);
  1865.                         echo "Showing ".$affected." row(s). ";
  1866.                         echo "(Query took ".$time." sec)</b><br/>";
  1867.                     }
  1868.                     else
  1869.                     {
  1870.                         echo "There is a problem with the syntax of your query ";
  1871.                         echo "(Query was not executed)</b><br/>";
  1872.                     }
  1873.                     echo "<span style='font-size:11px;'>".$query."</span>";
  1874.                     echo "</div><br/>";
  1875.  
  1876.                     if(sizeof($result)>0)
  1877.                     {
  1878.                         $headers = array_keys($result[0]);
  1879.  
  1880.                         echo "<table border='0' cellpadding='2' cellspacing='1'>";
  1881.                         echo "<tr>";
  1882.                         for($j=0; $j<sizeof($headers); $j++)
  1883.                         {
  1884.                             echo "<td class='tdheader'>";
  1885.                             echo $headers[$j];
  1886.                             echo "</td>";
  1887.                         }
  1888.                         echo "</tr>";
  1889.                         for($j=0; $j<sizeof($result); $j++)
  1890.                         {
  1891.                             $tdWithClass = "<td class='td".($j%2 ? "1" : "2")."'>";
  1892.                             echo "<tr>";
  1893.                             for($z=0; $z<sizeof($headers); $z++)
  1894.                             {
  1895.                                 echo $tdWithClass;
  1896.                                 echo $result[$j][$headers[$z]];
  1897.                                 echo "</td>";
  1898.                             }
  1899.                             echo "</tr>";
  1900.                         }
  1901.                         echo "</table><br/><br/>";
  1902.                         echo "<a href='".PAGE."?table=".$_GET['table']."&action=table_search'>Do Another Search</a>";
  1903.                     }
  1904.                 }
  1905.                 else
  1906.                 {
  1907.                     $query = "PRAGMA table_info('".$_GET['table']."')";
  1908.                     $result = $db->selectArray($query);
  1909.  
  1910.                     echo "<form action='".PAGE."?table=".$_GET['table']."&action=table_search&done=1' method='post'>";
  1911.                     echo "<table border='0' cellpadding='2' cellspacing='1'>";
  1912.                     echo "<tr>";
  1913.                     echo "<td class='tdheader'>Field</td>";
  1914.                     echo "<td class='tdheader'>Type</td>";
  1915.                     echo "<td class='tdheader'>Operator</td>";
  1916.                     echo "<td class='tdheader'>Value</td>";
  1917.                     echo "</tr>";
  1918.  
  1919.                     for($i=0; $i<sizeof($result); $i++)
  1920.                     {
  1921.                       $field = $result[$i][1];
  1922.                       $type = $result[$i][2];
  1923.                       $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  1924.                       $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  1925.                       echo "<tr>";
  1926.                       echo $tdWithClassLeft;
  1927.                       echo $field;
  1928.                       echo "</td>";
  1929.                       echo $tdWithClassLeft;
  1930.                       echo $type;
  1931.                       echo "</td>";
  1932.                       echo $tdWithClassLeft;
  1933.                       echo "<select name='".$field.":operator'>";
  1934.                       echo "<option value='='>=</option>";
  1935.                       if($type=="INTEGER" || $type=="REAL")
  1936.                       {
  1937.                           echo "<option value='>'>></option>";
  1938.                           echo "<option value='>='>>=</option>";
  1939.                           echo "<option value='<'><</option>";
  1940.                           echo "<option value='<='><=</option>";
  1941.                       }
  1942.                       else if($type=="TEXT" || $type=="BLOB")
  1943.                       {
  1944.                           echo "<option value='= '''>= ''</option>";
  1945.                           echo "<option value='!= '''>!= ''</option>";
  1946.                       }
  1947.                       echo "<option value='!='>!=</option>";
  1948.                       if($type=="TEXT" || $type=="BLOB")
  1949.                           echo "<option value='LIKE' selected='selected'>LIKE</option>";
  1950.                       else
  1951.                           echo "<option value='LIKE'>LIKE</option>";
  1952.                       echo "<option value='NOT LIKE'>NOT LIKE</option>";
  1953.                       echo "</select>";
  1954.                       echo "</td>";
  1955.                       echo $tdWithClassLeft;
  1956.                       if($type=="INTEGER" || $type=="REAL" || $type=="NULL")
  1957.                           echo "<input type='text' name='".$field."'/>";
  1958.                       else
  1959.                           echo "<textarea name='".$field."' wrap='hard' rows='1' cols='60'></textarea>";
  1960.                       echo "</td>";
  1961.                       echo "</tr>";
  1962.                     }
  1963.                     echo "<tr>";
  1964.                     echo "<td class='tdheader' style='text-align:right;' colspan='4'>";
  1965.                     echo "<input type='submit' value='Search'/>";
  1966.                     echo "</td>";
  1967.                     echo "</tr>";
  1968.                     echo "</table>";
  1969.                     echo "</form>";
  1970.                 }
  1971.                 break;
  1972.             //row actions
  1973.             /////////////////////////////////////////////// view row
  1974.             case "row_view":
  1975.                 if(!isset($_POST['startRow']))
  1976.                     $_POST['startRow'] = 0;
  1977.  
  1978.                 if(isset($_POST['numRows']))
  1979.                     $_SESSION['numRows'] = $_POST['numRows'];
  1980.  
  1981.                 if(!isset($_SESSION['numRows']))
  1982.                     $_SESSION['numRows'] = 30;
  1983.                
  1984.                 if(isset($_SESSION['currentTable']) && $_SESSION['currentTable']!=$_GET['table'])
  1985.                 {
  1986.                     unset($_SESSION['sort']);
  1987.                     unset($_SESSION['order']); 
  1988.                 }
  1989.                
  1990.                 $query = "SELECT Count(*) FROM ".$_GET['table'];
  1991.                 $rowCount = $db->select($query);
  1992.                 $rowCount = intval($rowCount[0]);
  1993.                 $lastPage = intval($rowCount / $_SESSION['numRows']);
  1994.                 $remainder = intval($rowCount % $_SESSION['numRows']);
  1995.                 if($remainder==0)
  1996.                     $remainder = $_SESSION['numRows'];
  1997.                
  1998.                 echo "<div style='overflow:hidden;'>";
  1999.                 //previous button
  2000.                 if($_POST['startRow']>0)
  2001.                 {
  2002.                     echo "<div style='float:left; overflow:hidden;'>";
  2003.                     echo "<form action='".PAGE."?action=row_view&table=".$_GET['table']."' method='post'>";
  2004.                     echo "<input type='hidden' name='startRow' value='0'/>";
  2005.                     echo "<input type='hidden' name='numRows' value='".$_SESSION['numRows']."'/> ";
  2006.                     echo "<input type='submit' value='&larr;&larr;' name='previous'/> ";
  2007.                     echo "</form>";
  2008.                     echo "</div>";
  2009.                     echo "<div style='float:left; overflow:hidden; margin-right:20px;'>";
  2010.                     echo "<form action='".PAGE."?action=row_view&table=".$_GET['table']."' method='post'>";
  2011.                     echo "<input type='hidden' name='startRow' value='".intval($_POST['startRow']-$_SESSION['numRows'])."'/>";
  2012.                     echo "<input type='hidden' name='numRows' value='".$_SESSION['numRows']."'/> ";
  2013.                     echo "<input type='submit' value='&larr;' name='previous_full'/> ";
  2014.                     echo "</form>";
  2015.                     echo "</div>";
  2016.                 }
  2017.                
  2018.                 //show certain number buttons
  2019.                 echo "<div style='float:left; overflow:hidden;'>";
  2020.                 echo "<form action='".PAGE."?action=row_view&table=".$_GET['table']."' method='post'>";
  2021.                 echo "<input type='submit' value='Show : ' name='show'/> ";
  2022.                 echo "<input type='text' name='numRows' style='width:50px;' value='".$_SESSION['numRows']."'/> ";
  2023.                 echo "row(s) starting from record # ";
  2024.                 if(intval($_POST['startRow']+$_SESSION['numRows']) < $rowCount)
  2025.                     echo "<input type='text' name='startRow' style='width:90px;' value='".intval($_POST['startRow']+$_SESSION['numRows'])."'/>";
  2026.                 else
  2027.                     echo "<input type='text' name='startRow' style='width:90px;' value='0'/>";
  2028.                 echo "</form>";
  2029.                 echo "</div>";
  2030.                
  2031.                 //next button
  2032.                 if(intval($_POST['startRow']+$_SESSION['numRows'])<$rowCount)
  2033.                 {
  2034.                     echo "<div style='float:left; overflow:hidden; margin-left:20px; '>";
  2035.                     echo "<form action='".PAGE."?action=row_view&table=".$_GET['table']."' method='post'>";
  2036.                     echo "<input type='hidden' name='startRow' value='".intval($_POST['startRow']+$_SESSION['numRows'])."'/>";
  2037.                     echo "<input type='hidden' name='numRows' value='".$_SESSION['numRows']."'/> ";
  2038.                     echo "<input type='submit' value='&rarr;' name='next'/> ";
  2039.                     echo "</form>";
  2040.                     echo "</div>";
  2041.                     echo "<div style='float:left; overflow:hidden;'>";
  2042.                     echo "<form action='".PAGE."?action=row_view&table=".$_GET['table']."' method='post'>";
  2043.                     echo "<input type='hidden' name='startRow' value='".intval($rowCount-$remainder)."'/>";
  2044.                     echo "<input type='hidden' name='numRows' value='".$_SESSION['numRows']."'/> ";
  2045.                     echo "<input type='submit' value='&rarr;&rarr;' name='next_full'/> ";
  2046.                     echo "</form>";
  2047.                     echo "</div>";
  2048.                 }
  2049.                 echo "<div style='clear:both;'></div>";
  2050.                 echo "</div>";
  2051.                
  2052.                 if(!isset($_GET['sort']))
  2053.                     $_GET['sort'] = NULL;
  2054.                 if(!isset($_GET['order']))
  2055.                     $_GET['order'] = NULL;
  2056.  
  2057.                 $table = $_GET['table'];
  2058.                 $numRows = $_SESSION['numRows'];
  2059.                 $startRow = $_POST['startRow'];
  2060.                 if(isset($_GET['sort']))
  2061.                 {
  2062.                     $_SESSION['sort'] = $_GET['sort'];
  2063.                     $_SESSION['currentTable'] = $_GET['table'];
  2064.                 }
  2065.                 if(isset($_GET['order']))
  2066.                 {
  2067.                     $_SESSION['order'] = $_GET['order'];
  2068.                     $_SESSION['currentTable'] = $_GET['table'];
  2069.                 }
  2070.                 $_SESSION['numRows'] = $numRows;
  2071.                 $query = "SELECT *, ROWID FROM ".$table;
  2072.                 $queryDisp = "SELECT * FROM ".$table;
  2073.                 $queryAdd = "";
  2074.                 if(isset($_SESSION['sort']))
  2075.                     $queryAdd .= " ORDER BY ".$_SESSION['sort'];
  2076.                 if(isset($_SESSION['order']))
  2077.                     $queryAdd .= " ".$_SESSION['order'];
  2078.                 $queryAdd .= " LIMIT ".$startRow.", ".$numRows;
  2079.                 $query .= $queryAdd;
  2080.                 $queryDisp .= $queryAdd;
  2081.                 $startTime = microtime(true);
  2082.                 $arr = $db->selectArray($query);
  2083.                 $endTime = microtime(true);
  2084.                 $time = round(($endTime - $startTime), 4);
  2085.                 $total = $db->numRows($table);
  2086.  
  2087.                 if(sizeof($arr)>0)
  2088.                 {
  2089.                     echo "<br/><div class='confirm'>";
  2090.                     echo "<b>Showing rows ".$startRow." - ".($startRow + sizeof($arr)-1)." (".$total." total, Query took ".$time." sec)</b><br/>";
  2091.                     echo "<span style='font-size:11px;'>".$queryDisp."</span>";
  2092.                     echo "</div><br/>";
  2093.  
  2094.                     echo "<form action='".PAGE."?action=row_editordelete&table=".$table."' method='post' name='checkForm'>";
  2095.                     echo "<table border='0' cellpadding='2' cellspacing='1'>";
  2096.                     $query = "PRAGMA table_info('".$table."')";
  2097.                     $result = $db->selectArray($query);
  2098.                     $rowidColumn = sizeof($result);
  2099.  
  2100.                     echo "<tr>";
  2101.                     echo "<td colspan='3'>";
  2102.                     echo "</td>";
  2103.  
  2104.                     for($i=0; $i<sizeof($result); $i++)
  2105.                     {
  2106.                         echo "<td class='tdheader'>";
  2107.                         echo "<a href='".PAGE."?action=row_view&table=".$table."&sort=".$result[$i][1];
  2108.                         $orderTag = ($_SESSION['sort']==$result[$i][1] && $_SESSION['order']=="ASC") ? "DESC" : "ASC";
  2109.                         echo "&order=".$orderTag;
  2110.                         echo "'>".$result[$i][1]."</a>";
  2111.                         if(isset($_SESSION['sort']) && $_SESSION['sort']==$result[$i][1])
  2112.                             echo (($_SESSION['order']=="ASC") ? " <b>&uarr;</b>" : " <b>&darr;</b>");
  2113.                         echo "</td>";
  2114.                     }
  2115.                     echo "</tr>";
  2116.  
  2117.                     for($i=0; $i<sizeof($arr); $i++)
  2118.                     {
  2119.                         // -g-> $pk will always be the last column in each row of the array because we are doing a "SELECT *, ROWID FROM ..."
  2120.                         $pk = $arr[$i][$rowidColumn];
  2121.                         $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  2122.                         $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  2123.                         echo "<tr>";
  2124.                         echo $tdWithClass;
  2125.                         echo "<input type='checkbox' name='check[]' value='".$pk."' id='check_".$i."'/>";
  2126.                         echo "</td>";
  2127.                         echo $tdWithClass;
  2128.                         // -g-> Here, we need to put the ROWID in as the link for both the edit and delete.
  2129.                         echo "<a href='".PAGE."?table=".$table."&action=row_editordelete&pk=".$pk."&type=edit'>edit</a>";
  2130.                         echo "</td>";
  2131.                         echo $tdWithClass;
  2132.                         echo "<a href='".PAGE."?table=".$table."&action=row_editordelete&pk=".$pk."&type=delete' style='color:red;'>delete</a>";
  2133.                         echo "</td>";
  2134.                         for($j=0; $j<sizeof($result); $j++)
  2135.                         {
  2136.                             if($result[$j][2]=="TEXT")
  2137.                                 echo $tdWithClassLeft;
  2138.                             else
  2139.                                 echo $tdWithClass;
  2140.                             // -g-> although the inputs do not interpret HTML on the way "in", when we print the contents of the database the interpretation cannot be avoided.
  2141.                             echo $db->formatString($arr[$i][$j]);
  2142.                             echo "</td>";
  2143.                         }
  2144.                         echo "</tr>";
  2145.                     }
  2146.                     echo "</table>";
  2147.                     echo "<a onclick='checkAll()'>Check All</a> / <a onclick='uncheckAll()'>Uncheck All</a> <i>With selected:</i> ";
  2148.                     echo "<select name='type'>";
  2149.                     echo "<option value='edit'>Edit</option>";
  2150.                     echo "<option value='delete'>Delete</option>";
  2151.                     echo "</select> ";
  2152.                     echo "<input type='submit' value='Go' name='massGo'/>";
  2153.                     echo "</form>";
  2154.                 }
  2155.                 else if($rowCount>0)//no rows - do nothing
  2156.                 {
  2157.                     echo "<br/><br/>There are no rows in the table for the range you selected.";
  2158.                 }
  2159.                 else
  2160.                 {
  2161.                     echo "<br/><br/>This table is empty. <a href='".PAGE."?table=".$_GET['table']."&action=row_create'>Click here</a> to insert rows.";
  2162.                 }
  2163.  
  2164.                 break;
  2165.             /////////////////////////////////////////////// create row
  2166.             case "row_create":
  2167.                 echo "<form action='".PAGE."?table=".$_GET['table']."&action=row_create' method='post'>";
  2168.                 echo "Restart insertion with ";
  2169.                 echo "<select name='num'>";
  2170.                 for($i=1; $i<=40; $i++)
  2171.                 {
  2172.                     if(isset($_POST['num']) && $_POST['num']==$i)
  2173.                         echo "<option value='".$i."' selected='selected'>".$i."</option>";
  2174.                     else
  2175.                         echo "<option value='".$i."'>".$i."</option>";
  2176.                 }
  2177.                 echo "</select>";
  2178.                 echo " rows ";
  2179.                 echo "<input type='submit' value='Go'/>";
  2180.                 echo "</form>";
  2181.                 echo "<br/>";
  2182.                 $query = "PRAGMA table_info('".$_GET['table']."')";
  2183.                 $result = $db->selectArray($query);
  2184.                 echo "<form action='".PAGE."?table=".$_GET['table']."&action=row_create&confirm=1' method='post'>";
  2185.                 if(isset($_POST['num']))
  2186.                     $num = $_POST['num'];
  2187.                 else
  2188.                     $num = 1;
  2189.                 echo "<input type='hidden' name='numRows' value='".$num."'/>";
  2190.                 for($j=0; $j<$num; $j++)
  2191.                 {
  2192.                     if($j>0)
  2193.                         echo "<input type='checkbox' value='ignore' name='".$j.":ignore' id='".$j."_ignore' checked='checked'/> Ignore<br/>";
  2194.                     echo "<table border='0' cellpadding='2' cellspacing='1'>";
  2195.                     echo "<tr>";
  2196.                     echo "<td class='tdheader'>Field</td>";
  2197.                     echo "<td class='tdheader'>Type</td>";
  2198.                     echo "<td class='tdheader'>Value</td>";
  2199.                     echo "</tr>";
  2200.  
  2201.                     for($i=0; $i<sizeof($result); $i++)
  2202.                     {
  2203.                         $field = $result[$i][1];
  2204.                         if($j==0)
  2205.                             $fieldStr .= ":".$field;
  2206.                         $type = $result[$i][2];
  2207.                         $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  2208.                         $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  2209.                         echo "<tr>";
  2210.                         echo $tdWithClassLeft;
  2211.                         echo $field;
  2212.                         echo "</td>";
  2213.                         echo $tdWithClassLeft;
  2214.                         echo $type;
  2215.                         echo "</td>";
  2216.                         echo $tdWithClassLeft;
  2217.                         if($type=="INTEGER" || $type=="REAL" || $type=="NULL")
  2218.                             echo "<input type='text' name='".$j.":".$field."' onblur='changeIgnore(this, \"".$j."_ignore\")'/>";
  2219.                         else
  2220.                             echo "<textarea name='".$j.":".$field."' wrap='hard' rows='1' cols='60' onblur='changeIgnore(this, \"".$j."_ignore\")'></textarea>";
  2221.                         echo "</td>";
  2222.                         echo "</tr>";
  2223.                     }
  2224.                     echo "<tr>";
  2225.                     echo "<td class='tdheader' style='text-align:right;' colspan='3'>";
  2226.                     echo "<input type='submit' value='Insert'/>";
  2227.                     echo "</td>";
  2228.                     echo "</tr>";
  2229.                     echo "</table><br/>";
  2230.                 }
  2231.                 $fieldStr = substr($fieldStr, 1);
  2232.                 echo "<input type='hidden' name='fields' value='".$fieldStr."'/>";
  2233.                 echo "</form>";
  2234.                 break;
  2235.             /////////////////////////////////////////////// edit or delete row
  2236.             case "row_editordelete":
  2237.                 if(isset($_POST['check']))
  2238.                     $pks = $_POST['check'];
  2239.                 else if(isset($_GET['pk']))
  2240.                     $pks = array($_GET['pk']);
  2241.                 $str = $pks[0];
  2242.                 $pkVal = $pks[0];
  2243.                 for($i=1; $i<sizeof($pks); $i++)
  2244.                 {
  2245.                     $str .= ", ".$pks[$i];
  2246.                     $pkVal .= ":".$pks[$i];
  2247.                 }
  2248.                 if($str=="") //nothing was selected so show an error
  2249.                 {
  2250.                     echo "<div class='confirm'>";
  2251.                     echo "Error: You did not select anything.";
  2252.                     echo "</div>";
  2253.                     echo "<br/><br/><a href='".PAGE."?table=".$_GET['table']."&action=row_view'>Return</a>";
  2254.                 }
  2255.                 else
  2256.                 {
  2257.                     if((isset($_POST['type']) && $_POST['type']=="edit") || (isset($_GET['type']) && $_GET['type']=="edit")) //edit
  2258.                     {
  2259.                         echo "<form action='".PAGE."?table=".$_GET['table']."&action=row_edit&confirm=1&pk=".$pkVal."' method='post'>";
  2260.                         $query = "PRAGMA table_info('".$_GET['table']."')";
  2261.                         $result = $db->selectArray($query);
  2262.  
  2263.                         //build the POST array of fields
  2264.                         $fieldStr = $result[0][1];
  2265.                         for($j=1; $j<sizeof($result); $j++)
  2266.                             $fieldStr .= ":".$result[$j][1];
  2267.  
  2268.                         echo "<input type='hidden' name='fieldArray' value='".$fieldStr."'/>";
  2269.  
  2270.                         for($j=0; $j<sizeof($pks); $j++)
  2271.                         {
  2272.                             $query = "SELECT * FROM ".$_GET['table']." WHERE ROWID = ".$pks[$j];
  2273.                             $result1 = $db->select($query);
  2274.  
  2275.                             echo "<table border='0' cellpadding='2' cellspacing='1'>";
  2276.                             echo "<tr>";
  2277.                             echo "<td class='tdheader'>Field</td>";
  2278.                             echo "<td class='tdheader'>Type</td>";
  2279.                             echo "<td class='tdheader'>Value</td>";
  2280.                             echo "</tr>";
  2281.  
  2282.                             for($i=0; $i<sizeof($result); $i++)
  2283.                             {
  2284.                                 $field = $result[$i][1];
  2285.                                 $type = $result[$i][2];
  2286.                                 $value = $result1[$i];
  2287.                                 $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  2288.                                 $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  2289.                                 echo "<tr>";
  2290.                                 echo $tdWithClass;
  2291.                                 echo $field;
  2292.                                 echo "</td>";
  2293.                                 echo $tdWithClass;
  2294.                                 echo $type;
  2295.                                 echo "</td>";
  2296.                                 echo $tdWithClassLeft;
  2297.                                 if($type=="INTEGER" || $type=="REAL" || $type=="NULL")
  2298.                                     echo "<input type='text' name='".$pks[$j].":".$field."' value='".$db->formatString($value)."'/>";
  2299.                                 else
  2300.                                     echo "<textarea name='".$pks[$j].":".$field."' wrap='hard' rows='1' cols='60'>".$db->formatString($value)."</textarea>";
  2301.                                 echo "</td>";
  2302.                                 echo "</tr>";
  2303.                             }
  2304.                             echo "<tr>";
  2305.                             echo "<td class='tdheader' style='text-align:right;' colspan='3'>";
  2306.                             echo "<input type='submit' value='Save Changes'/> ";
  2307.                             echo "<a href='".PAGE."?table=".$_GET['table']."&action=row_view'>Cancel</a>";
  2308.                             echo "</td>";
  2309.                             echo "</tr>";
  2310.                             echo "</table>";
  2311.                             echo "<br/>";
  2312.                         }
  2313.                         echo "</form>";
  2314.                     }
  2315.                     else //delete
  2316.                     {
  2317.                         echo "<form action='".PAGE."?table=".$_GET['table']."&action=row_delete&confirm=1&pk=".$pkVal."' method='post'>";
  2318.                         echo "<div class='confirm'>";
  2319.                         echo "Are you sure you want to delete row(s) ".$str." from table '".$_GET['table']."'?<br/><br/>";
  2320.                         echo "<input type='submit' value='Confirm'/> ";
  2321.                         echo "<a href='".PAGE."?table=".$_GET['table']."&action=row_view'>Cancel</a>";
  2322.                         echo "</div>";
  2323.                     }
  2324.                 }
  2325.                 break;
  2326.             //column actions
  2327.             /////////////////////////////////////////////// view column
  2328.             case "column_view":
  2329.                 $query = "PRAGMA table_info('".$_GET['table']."')";
  2330.                 $result = $db->selectArray($query);
  2331.  
  2332.                 echo "<form action='".PAGE."?table=".$_GET['table']."&action=column_delete' method='post' name='checkForm'>";
  2333.                 echo "<table border='0' cellpadding='2' cellspacing='1'>";
  2334.                 echo "<tr>";
  2335.                 echo "<td colspan='2'>";
  2336.                 echo "</td>";
  2337.                 echo "<td class='tdheader'>Column #</td>";
  2338.                 echo "<td class='tdheader'>Field</td>";
  2339.                 echo "<td class='tdheader'>Type</td>";
  2340.                 echo "<td class='tdheader'>Not Null</td>";
  2341.                 echo "<td class='tdheader'>Default Value</td>";
  2342.                 echo "<td class='tdheader'>Primary Key</td>";
  2343.                 echo "</tr>";
  2344.  
  2345.                 for($i=0; $i<sizeof($result); $i++)
  2346.                 {
  2347.                     $colVal = $result[$i][0];
  2348.                     $fieldVal = $result[$i][1];
  2349.                     $typeVal = $result[$i][2];
  2350.                     $notnullVal = $result[$i][3];
  2351.                     $defaultVal = $result[$i][4];
  2352.                     $primarykeyVal = $result[$i][5];
  2353.  
  2354.                     if(intval($notnullVal)!=0)
  2355.                         $notnullVal = "yes";
  2356.                     else
  2357.                         $notnullVal = "no";
  2358.                     if(intval($primarykeyVal)!=0)
  2359.                         $primarykeyVal = "yes";
  2360.                     else
  2361.                         $primarykeyVal = "no";
  2362.  
  2363.                     $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  2364.                     $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  2365.                     echo "<tr>";
  2366.                     echo $tdWithClass;
  2367.                     echo "<input type='checkbox' name='check[]' value='".$fieldVal."' id='check_".$i."'/>";
  2368.                     echo "</td>";
  2369.                     echo $tdWithClass;
  2370.                     echo "<a href='".PAGE."?table=".$_GET['table']."&action=column_delete&pk=".$fieldVal."' style='color:red;'>delete</a>";
  2371.                     echo "</td>";
  2372.                     echo $tdWithClass;
  2373.                     echo $colVal;
  2374.                     echo "</td>";
  2375.                     echo $tdWithClassLeft;
  2376.                     echo $fieldVal;
  2377.                     echo "</td>";
  2378.                     echo $tdWithClassLeft;
  2379.                     echo $typeVal;
  2380.                     echo "</td>";
  2381.                     echo $tdWithClassLeft;
  2382.                     echo $notnullVal;
  2383.                     echo "</td>";
  2384.                     echo $tdWithClassLeft;
  2385.                     echo $defaultVal;
  2386.                     echo "</td>";
  2387.                     echo $tdWithClassLeft;
  2388.                     echo $primarykeyVal;
  2389.                     echo "</td>";
  2390.                     echo "</tr>";
  2391.                 }
  2392.  
  2393.                 echo "</table>";
  2394.  
  2395.                 echo "<a onclick='checkAll()'>Check All</a> / <a onclick='uncheckAll()'>Uncheck All</a> <i>With selected:</i> ";
  2396.                 echo "<select name='massType'>";
  2397.                 //echo "<option value='edit'>Edit</option>";
  2398.                 echo "<option value='delete'>Delete</option>";
  2399.                 echo "</select> ";
  2400.                 echo "<input type='hidden' name='structureDel' value='true'/>";
  2401.                 echo "<input type='submit' value='Go' name='massGo'/>";
  2402.                 echo "</form>";
  2403.  
  2404.                 echo "<br/>";
  2405.                 echo "<form action='".PAGE."?table=".$_GET['table']."&action=column_create' method='post'>";
  2406.                 echo "<input type='hidden' name='tablename' value='".$_GET['table']."'/>";
  2407.                 echo "Add <input type='text' name='tablefields' style='width:30px;' value='1'/> field(s) at end of table <input type='submit' value='Go' name='addfields'/>";
  2408.                 echo "</form>";
  2409.                 echo "<br/><hr/><br/>";
  2410.                 //$query = "SELECT * FROM sqlite_master WHERE type='index' AND tbl_name='".$_GET['table']."'";
  2411.                 $query = "PRAGMA index_list(".$_GET['table'].")";
  2412.                 $result = $db->selectArray($query);
  2413.                 if(sizeof($result)>0)
  2414.                 {
  2415.                     echo "<h2>Indexes:</h2>";
  2416.                     echo "<table border='0' cellpadding='2' cellspacing='1'>";
  2417.                     echo "<tr>";
  2418.                     echo "<td colspan='1'>";
  2419.                     echo "</td>";
  2420.                     echo "<td class='tdheader'>Name</td>";
  2421.                     echo "<td class='tdheader'>Unique</td>";
  2422.                     echo "<td class='tdheader'>Seq. No.</td>";
  2423.                     echo "<td class='tdheader'>Column #</td>";
  2424.                     echo "<td class='tdheader'>Field</td>";
  2425.                     echo "</tr>";
  2426.                     for($i=0; $i<sizeof($result); $i++)
  2427.                     {
  2428.                         if($result[$i]['unique']==0)
  2429.                             $unique = "no";
  2430.                         else
  2431.                             $unique = "yes";
  2432.  
  2433.                         $query = "PRAGMA index_info(".$result[$i]['name'].")";
  2434.                         $info = $db->selectArray($query);
  2435.                         $span = sizeof($info);
  2436.  
  2437.                         $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  2438.                         $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  2439.                         $tdWithClassSpan = "<td class='td".($i%2 ? "1" : "2")."' rowspan='".$span."'>";
  2440.                         $tdWithClassLeftSpan = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;' rowspan='".$span."'>";
  2441.                         echo "<tr>";
  2442.                         echo $tdWithClassSpan;
  2443.                         echo "<a href='".PAGE."?table=".$_GET['table']."&action=index_delete&pk=".$result[$i]['name']."' style='color:red;'>delete</a>";
  2444.                         echo "</td>";
  2445.                         echo $tdWithClassLeftSpan;
  2446.                         echo $result[$i]['name'];
  2447.                         echo "</td>";
  2448.                         echo $tdWithClassLeftSpan;
  2449.                         echo $unique;
  2450.                         echo "</td>";
  2451.                         for($j=0; $j<$span; $j++)
  2452.                         {
  2453.                             if($j!=0)
  2454.                                 echo "<tr>";
  2455.                             echo $tdWithClassLeft;
  2456.                             echo $info[$j]['seqno'];
  2457.                             echo "</td>";
  2458.                             echo $tdWithClassLeft;
  2459.                             echo $info[$j]['cid'];
  2460.                             echo "</td>";
  2461.                             echo $tdWithClassLeft;
  2462.                             echo $info[$j]['name'];
  2463.                             echo "</td>";
  2464.                             echo "</tr>";
  2465.                         }
  2466.                     }
  2467.                     echo "</table>";
  2468.                 }
  2469.                 echo "<form action='".PAGE."?table=".$_GET['table']."&action=index_create' method='post'>";
  2470.                 echo "<input type='hidden' name='tablename' value='".$_GET['table']."'/>";
  2471.                 echo "<br/><div class='tdheader'>";
  2472.                 echo "Create an index on <input type='text' name='numcolumns' style='width:30px;' value='1'/> columns <input type='submit' value='Go' name='addindex'/>";
  2473.                 echo "</div>";
  2474.                 echo "</form>";
  2475.                 break;
  2476.             /////////////////////////////////////////////// create column
  2477.             case "column_create":
  2478.                 echo "<h2>Adding new field(s) to table '".$_POST['tablename']."'</h2>";
  2479.                 if($_POST['tablefields']=="" || intval($_POST['tablefields'])<=0)
  2480.                     echo "You must specify the number of table fields.";
  2481.                 else if($_POST['tablename']=="")
  2482.                     echo "You must specify a table name.";
  2483.                 else
  2484.                 {
  2485.                     $num = intval($_POST['tablefields']);
  2486.                     $name = $_POST['tablename'];
  2487.                     echo "<form action='".PAGE."?table=".$_POST['tablename']."&action=column_create&confirm=1' method='post'>";
  2488.                     echo "<input type='hidden' name='tablename' value='".$name."'/>";
  2489.                     echo "<input type='hidden' name='rows' value='".$num."'/>";
  2490.                     echo "<table border='0' cellpadding='2' cellspacing='1'>";
  2491.                     echo "<tr>";
  2492.                     $headings = array("Field", "Type", "Primary Key", "Autoincrement", "Not NULL", "Default Value");
  2493.                 for($k=0; $k<count($headings); $k++)
  2494.                         echo "<td class='tdheader'>" . $headings[$k] . "</td>";
  2495.                     echo "</tr>";
  2496.  
  2497.                     for($i=0; $i<$num; $i++)
  2498.                     {
  2499.                         $tdWithClass = "<td class='td" . ($i%2 ? "1" : "2") . "'>";
  2500.                         echo "<tr>";
  2501.                         echo $tdWithClass;
  2502.                         echo "<input type='text' name='".$i."_field' style='width:200px;'/>";
  2503.                         echo "</td>";
  2504.                         echo $tdWithClass;
  2505.                         echo "<select name='".$i."_type' id='".$i."_type' onchange='toggleAutoincrement(".$i.");'>";
  2506.                         echo "<option value='INTEGER' selected='selected'>INTEGER</option>";
  2507.                         $types = unserialize(DATATYPES);
  2508.                         for($z=0; $z<sizeof($types); $z++)
  2509.                             echo "<option value='".$types[$z]."'>".$types[$z]."</option>";
  2510.                         echo "</select>";
  2511.                         echo "</td>";
  2512.                         echo $tdWithClass;
  2513.                         echo "<input type='checkbox' name='".$i."_primarykey'/> Yes";
  2514.                         echo "</td>";
  2515.                         echo $tdWithClass;
  2516.                         echo "<input type='checkbox' name='".$i."_autoincrement' id='".$i."_autoincrement'/> Yes";
  2517.                         echo "</td>";
  2518.                         echo $tdWithClass;
  2519.                         echo "<input type='checkbox' name='".$i."_notnull'/> Yes";
  2520.                         echo "</td>";
  2521.                         echo $tdWithClass;
  2522.                         echo "<input type='text' name='".$i."_defaultvalue' style='width:100px;'/>";
  2523.                         echo "</td>";
  2524.                         echo "</tr>";
  2525.                     }
  2526.                     echo "<tr>";
  2527.                     echo "<td class='tdheader' style='text-align:right;' colspan='6'>";
  2528.                     echo "<input type='submit' value='Add Field(s)'/> ";
  2529.                     echo "<a href='".PAGE."?table=".$_POST['tablename']."&action=column_view'>Cancel</a>";
  2530.                     echo "</td>";
  2531.                     echo "</tr>";
  2532.                     echo "</table>";
  2533.                     echo "</form>";
  2534.                 }
  2535.                 break;
  2536.             /////////////////////////////////////////////// delete column
  2537.             case "column_delete":
  2538.                 if(isset($_POST['check']))
  2539.                     $pks = $_POST['check'];
  2540.                 else if(isset($_GET['pk']))
  2541.                     $pks = array($_GET['pk']);
  2542.                 $str = $pks[0];
  2543.                 $pkVal = $pks[0];
  2544.                 for($i=1; $i<sizeof($pks); $i++)
  2545.                 {
  2546.                     $str .= ", ".$pks[$i];
  2547.                     $pkVal .= ":".$pks[$i];
  2548.                 }
  2549.                 if($str=="") //nothing was selected so show an error
  2550.                 {
  2551.                     echo "<div class='confirm'>";
  2552.                     echo "Error: You did not select anything.";
  2553.                     echo "</div>";
  2554.                     echo "<br/><br/><a href='".PAGE."?table=".$_GET['table']."&action=column_view'>Return</a>";
  2555.                 }
  2556.                 else
  2557.                 {
  2558.                     echo "<form action='".PAGE."?table=".$_GET['table']."&action=column_delete&confirm=1&pk=".$pkVal."' method='post'>";
  2559.                     echo "<div class='confirm'>";
  2560.                     echo "Are you sure you want to delete column(s) ".$str." from table '".$_GET['table']."'?<br/><br/>";
  2561.                     echo "<input type='submit' value='Confirm'/> ";
  2562.                     echo "<a href='".PAGE."?table=".$_GET['table']."&action=column_view'>Cancel</a>";
  2563.                     echo "</div>";
  2564.                 }
  2565.                 break;
  2566.             /////////////////////////////////////////////// edit column
  2567.             case "column_edit":
  2568.                 //this section will contain the code for editing a column
  2569.                 break;
  2570.             /////////////////////////////////////////////// delete index
  2571.             case "index_delete":
  2572.                 echo "<form action='".PAGE."?table=".$_GET['table']."&action=index_delete&pk=".$_GET['pk']."&confirm=1' method='post'>";
  2573.                 echo "<div class='confirm'>";
  2574.                 echo "Are you sure you want to delete index '".$_GET['pk']."'?<br/><br/>";
  2575.                 echo "<input type='submit' value='Confirm'/> ";
  2576.                 echo "<a href='".PAGE."?table=".$_GET['table']."&action=column_view'>Cancel</a>";
  2577.                 echo "</div>";
  2578.                 echo "</form>";
  2579.                 break;
  2580.             /////////////////////////////////////////////// create index
  2581.             case "index_create":
  2582.                 echo "<h2>Creating new index on table '".$_POST['tablename']."'</h2>";
  2583.                 if($_POST['numcolumns']=="" || intval($_POST['numcolumns'])<=0)
  2584.                     echo "You must specify the number of table fields.";
  2585.                 else if($_POST['tablename']=="")
  2586.                     echo "You must specify a table name.";
  2587.                 else
  2588.                 {
  2589.                     echo "<form action='".PAGE."?table=".$_POST['tablename']."&action=index_create&confirm=1' method='post'>";
  2590.                     $num = intval($_POST['numcolumns']);
  2591.                     $query = "PRAGMA table_info('".$_POST['tablename']."')";
  2592.                     $result = $db->selectArray($query);
  2593.                     echo "<fieldset><legend>Define index properties</legend>";
  2594.                     echo "Index name: <input type='text' name='name'/><br/>";
  2595.                     echo "Duplicate values: ";
  2596.                     echo "<select name='duplicate'>";
  2597.                     echo "<option value='yes'>Allowed</option>";
  2598.                     echo "<option value='no'>Not Allowed</option>";
  2599.                     echo "</select><br/>";
  2600.                     echo "</fieldset>";
  2601.                     echo "<br/>";
  2602.                     echo "<fieldset><legend>Define index columns</legend>";
  2603.                     for($i=0; $i<$num; $i++)
  2604.                     {
  2605.                         echo "<select name='".$i."_field'>";
  2606.                         echo "<option value=''>--Ignore--</option>";
  2607.                         for($j=0; $j<sizeof($result); $j++)
  2608.                             echo "<option value='".$result[$j][1]."'>".$result[$j][1]."</option>";
  2609.                         echo "</select> ";
  2610.                         echo "<select name='".$i."_order'>";
  2611.                         echo "<option value=''></option>";
  2612.                         echo "<option value=' ASC'>Ascending</option>";
  2613.                         echo "<option value=' DESC'>Descending</option>";
  2614.                         echo "</select><br/>";
  2615.                     }
  2616.                     echo "</fieldset>";
  2617.                     echo "<br/><br/>";
  2618.                     echo "<input type='hidden' name='num' value='".$num."'/>";
  2619.                     echo "<input type='submit' value='Create Index'/> ";
  2620.                     echo "<a href='".PAGE."?table=".$_POST['tablename']."&action=column_view'>Cancel</a>";
  2621.                     echo "</form>";
  2622.                 }
  2623.                 break;
  2624.         }
  2625.         echo "</div>";
  2626.     }
  2627.     $view = "structure";
  2628.     if(!isset($_GET['table']) && !isset($_GET['confirm']) && (!isset($_GET['action']) || (isset($_GET['action']) && $_GET['action']!="table_create"))) //the absence of these fields means we are viewing the database homepage
  2629.     {
  2630.         if(isset($_GET['view']))
  2631.             $view = $_GET['view'];
  2632.         else
  2633.             $view = "structure";
  2634.  
  2635.         echo "<a href='".PAGE."?view=structure' ";
  2636.         if($view=="structure")
  2637.             echo "class='tab_pressed'";
  2638.         else
  2639.             echo "class='tab'";
  2640.         echo ">Structure</a>";
  2641.         echo "<a href='".PAGE."?view=sql' ";
  2642.         if($view=="sql")
  2643.             echo "class='tab_pressed'";
  2644.         else
  2645.             echo "class='tab'";
  2646.         echo ">SQL</a>";
  2647.         echo "<a href='".PAGE."?view=export' ";
  2648.         if($view=="export")
  2649.             echo "class='tab_pressed'";
  2650.         else
  2651.             echo "class='tab'";
  2652.         echo ">Export</a>";
  2653.         echo "<a href='".PAGE."?view=import' ";
  2654.         if($view=="import")
  2655.             echo "class='tab_pressed'";
  2656.         else
  2657.             echo "class='tab'";
  2658.         echo ">Import</a>";
  2659.         echo "<a href='".PAGE."?view=vacuum' ";
  2660.         if($view=="vacuum")
  2661.             echo "class='tab_pressed'";
  2662.         else
  2663.             echo "class='tab'";
  2664.         echo ">Vacuum</a>";
  2665.         echo "<div style='clear:both;'></div>";
  2666.         echo "<div id='main'>";
  2667.  
  2668.         if($view=="structure") //database structure - view of all the tables
  2669.         {
  2670.             echo "<b>Database name</b>: ".$db->getName()."<br/>";
  2671.             echo "<b>Path to database</b>: ".$db->getPath()."<br/>";
  2672.             echo "<b>Size of database</b>: ".$db->getSize()."<br/>";
  2673.             echo "<b>Database last modified</b>: ".$db->getDate()."<br/>";
  2674.             if($db->getType()=="SQLiteDatabase")
  2675.             {
  2676.                 echo "<b>SQLite version</b>: ".sqlite_libversion()."<br/>";
  2677.                 echo "<b>SQLite encoding</b>: ".sqlite_libencoding()."<br/>";
  2678.             }
  2679.             if($db->getType()=="SQLite3")
  2680.                 echo "<b>SQLite version</b>: ".(SQLite3::version())."<br/>";
  2681.             else
  2682.                 echo "<b>SQLite version</b>: ".$db->getVersion()."<br/>";
  2683.             echo "<b>SQLite extension</b>: ".$db->getType()."<br/>";
  2684.             echo "<b>PHP version</b>: ".phpversion()."<br/><br/>";
  2685.  
  2686.             $query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
  2687.             $result = $db->selectArray($query);
  2688.  
  2689.             $j = 0;
  2690.             for($i=0; $i<sizeof($result); $i++)
  2691.                 if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  2692.                     $j++;
  2693.  
  2694.             if($j==0)
  2695.                 echo "No tables in database.<br/><br/>";
  2696.             else
  2697.             {
  2698.                 echo "<table border='0' cellpadding='2' cellspacing='1'>";
  2699.                 echo "<tr>";
  2700.                 echo "<td class='tdheader'>Table</td>";
  2701.                 echo "<td class='tdheader' colspan='10'>Action</td>";
  2702.                 echo "<td class='tdheader'>Records</td>";
  2703.                 echo "</tr>";
  2704.                
  2705.                 $totalRecords = 0;
  2706.                 for($i=0; $i<sizeof($result); $i++)
  2707.                 {
  2708.                     if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  2709.                     {
  2710.                         $records = $db->numRows($result[$i]['name']);
  2711.                         $totalRecords += $records;
  2712.                         $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  2713.                         $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  2714.                        
  2715.                         echo "<tr>";
  2716.                         echo $tdWithClassLeft;
  2717.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=row_view'>".$result[$i]['name']."</a><br/>";
  2718.                         echo "</td>";
  2719.                         echo $tdWithClass;
  2720.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=row_view'>Browse</a>";
  2721.                         echo "</td>";
  2722.                         echo $tdWithClass;
  2723.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=column_view'>Structure</a>";
  2724.                         echo "</td>";
  2725.                         echo $tdWithClass;
  2726.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=table_sql'>SQL</a>";
  2727.                         echo "</td>";
  2728.                         echo $tdWithClass;
  2729.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=table_search'>Search</a>";
  2730.                         echo "</td>";
  2731.                         echo $tdWithClass;
  2732.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=row_create'>Insert</a>";
  2733.                         echo "</td>";
  2734.                         echo $tdWithClass;
  2735.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=table_export'>Export</a>";
  2736.                         echo "</td>";
  2737.                         echo $tdWithClass;
  2738.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=table_import'>Import</a>";
  2739.                         echo "</td>";
  2740.                         echo $tdWithClass;
  2741.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=table_rename'>Rename</a>";
  2742.                         echo "</td>";
  2743.                         echo $tdWithClass;
  2744.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=table_empty' style='color:red;'>Empty</a>";
  2745.                         echo "</td>";
  2746.                         echo $tdWithClass;
  2747.                         echo "<a href='".PAGE."?table=".$result[$i]['name']."&action=table_drop' style='color:red;'>Drop</a>";
  2748.                         echo "</td>";
  2749.                         echo $tdWithClass;
  2750.                         echo $records;
  2751.                         echo "</td>";
  2752.                         echo "</tr>";
  2753.                     }
  2754.                 }
  2755.                 echo "<tr>";
  2756.                 echo "<td class='tdheader' colspan='11'>".sizeof($result)." table(s) total</td>";
  2757.                 echo "<td class='tdheader' colspan='1' style='text-align:right;'>".$totalRecords."</td>";
  2758.                 echo "</tr>";
  2759.                 echo "</table>";
  2760.                 echo "<br/>";
  2761.             }
  2762.             echo "<fieldset>";
  2763.             echo "<legend><b>Create new table on database '".$db->getName()."'</b></legend>";
  2764.             echo "<form action='".PAGE."?action=table_create' method='post'>";
  2765.             echo "Name: <input type='text' name='tablename' style='width:200px;'/> ";
  2766.             echo "Number of Fields: <input type='text' name='tablefields' style='width:90px;'/> ";
  2767.             echo "<input type='submit' name='createtable' value='Go'/>";
  2768.             echo "</form>";
  2769.             echo "</fieldset>";
  2770.         }
  2771.         else if($view=="sql") //database SQL editor
  2772.         {
  2773.             $isSelect = false;
  2774.             if(isset($_POST['query']) && $_POST['query']!="")
  2775.             {
  2776.                 $delimiter = $_POST['delimiter'];
  2777.                 $queryStr = stripslashes($_POST['queryval']);
  2778.                 $query = explode($delimiter, $queryStr); //explode the query string into individual queries based on the delimiter
  2779.  
  2780.                 for($i=0; $i<sizeof($query); $i++) //iterate through the queries exploded by the delimiter
  2781.                 {
  2782.                     if(str_replace(" ", "", str_replace("\n", "", str_replace("\r", "", $query[$i])))!="") //make sure this query is not an empty string
  2783.                     {
  2784.                         $startTime = microtime(true);
  2785.                         if(strpos(strtolower($query[$i]), "select ")!==false)
  2786.                         {
  2787.                             $isSelect = true;
  2788.                             $result = $db->selectArray($query[$i], "assoc");
  2789.                         }
  2790.                         else
  2791.                         {
  2792.                             $isSelect = false;
  2793.                             $result = $db->query($query[$i]);
  2794.                         }
  2795.                         $endTime = microtime(true);
  2796.                         $time = round(($endTime - $startTime), 4);
  2797.  
  2798.                         echo "<div class='confirm'>";
  2799.                         echo "<b>";
  2800.                         if($isSelect || $result)
  2801.                         {
  2802.                             if($isSelect)
  2803.                             {
  2804.                                 $affected = sizeof($result);
  2805.                                 echo "Showing ".$affected." row(s). ";
  2806.                             }
  2807.                             else
  2808.                             {
  2809.                                 $affected = $db->getAffectedRows();
  2810.                                 echo $affected." row(s) affected. ";
  2811.                             }
  2812.                             echo "(Query took ".$time." sec)</b><br/>";
  2813.                         }
  2814.                         else
  2815.                         {
  2816.                             echo "There is a problem with the syntax of your query ";
  2817.                             echo "(Query was not executed)</b><br/>";
  2818.                         }
  2819.                         echo "<span style='font-size:11px;'>".$query[$i]."</span>";
  2820.                         echo "</div><br/>";
  2821.                         if($isSelect)
  2822.                         {
  2823.                             if(sizeof($result)>0)
  2824.                             {
  2825.                                 $headers = array_keys($result[0]);
  2826.  
  2827.                                 echo "<table border='0' cellpadding='2' cellspacing='1'>";
  2828.                                 echo "<tr>";
  2829.                                 for($j=0; $j<sizeof($headers); $j++)
  2830.                                 {
  2831.                                     echo "<td class='tdheader'>";
  2832.                                     echo $headers[$j];
  2833.                                     echo "</td>";
  2834.                                 }
  2835.                                 echo "</tr>";
  2836.                                 for($j=0; $j<sizeof($result); $j++)
  2837.                                 {
  2838.                                     $tdWithClass = "<td class='td".($j%2 ? "1" : "2")."'>";
  2839.                                     echo "<tr>";
  2840.                                     for($z=0; $z<sizeof($headers); $z++)
  2841.                                     {
  2842.                                         echo $tdWithClass;
  2843.                                         echo $result[$j][$headers[$z]];
  2844.                                         echo "</td>";
  2845.                                     }
  2846.                                     echo "</tr>";
  2847.                                 }
  2848.                                 echo "</table><br/><br/>";
  2849.                             }
  2850.                         }
  2851.                     }
  2852.                 }
  2853.             }
  2854.             else
  2855.             {
  2856.                 $delimiter = ";";
  2857.                 $queryStr = "";
  2858.             }
  2859.  
  2860.             echo "<fieldset>";
  2861.             echo "<legend><b>Run SQL query/queries on database '".$db->getName()."'</b></legend>";
  2862.             echo "<form action='".PAGE."?view=sql' method='post'>";
  2863.             echo "<textarea style='width:100%; height:300px;' name='queryval'>".$queryStr."</textarea>";
  2864.             echo "Delimiter <input type='text' name='delimiter' value='".$delimiter."' style='width:50px;'/> ";
  2865.             echo "<input type='submit' name='query' value='Go'/>";
  2866.             echo "</form>";
  2867.         }
  2868.         else if($view=="vacuum")
  2869.         {
  2870.             if(isset($_POST['vacuum']))
  2871.             {
  2872.                 $query = "VACUUM";
  2873.                 $db->query($query);
  2874.                 echo "<div class='confirm'>";
  2875.                 echo "The database, '".$db->getName()."', has been VACUUMed.";
  2876.                 echo "</div><br/>";
  2877.             }
  2878.             echo "<form method='post' action='".PAGE."?view=vacuum'>";
  2879.             echo "Large databases sometimes need to be VACUUMed to reduce their footprint on the server. Click the button below to VACUUM the database, '".$db->getName()."'.";
  2880.             echo "<br/><br/>";
  2881.             echo "<input type='submit' value='VACUUM' name='vacuum'/>";
  2882.             echo "</form>";
  2883.         }
  2884.         else if($view=="export")
  2885.         {
  2886.             echo "<form method='post' action='".PAGE."?view=export'>";
  2887.             echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>Export</b></legend>";
  2888.             echo "<select multiple='multiple' size='10' style='width:240px;' name='tables[]'>";
  2889.             $query = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
  2890.             $result = $db->selectArray($query);
  2891.             for($i=0; $i<sizeof($result); $i++)
  2892.             {
  2893.                 if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  2894.                     echo "<option value='".$result[$i]['name']."' selected='selected'>".$result[$i]['name']."</option>";
  2895.             }
  2896.             echo "</select>";
  2897.             echo "<br/><br/>";
  2898.             echo "<input type='radio' name='export_type' checked='checked' value='sql'/> SQL";
  2899.             echo "</fieldset>";
  2900.             echo "<fieldset style='float:left;'><legend><b>Options</b></legend>";
  2901.             echo "<input type='checkbox' checked='checked' name='structure'/> Export with structure [<a onmouseover='tooltip.show(\"Creates the queries to add the tables and their columns\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  2902.             echo "<input type='checkbox' checked='checked' name='data'/> Export with data [<a onmouseover='tooltip.show(\"Creates the queries to insert the table rows\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  2903.             echo "<input type='checkbox' name='drop'/> Add DROP TABLE [<a onmouseover='tooltip.show(\"Creates the queries to remove the tables before potentially adding them so that errors do not occur if they already exist\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  2904.             echo "<input type='checkbox' checked='checked' name='transaction'/> Add TRANSACTION [<a onmouseover='tooltip.show(\"Performs queries within transactions so that if an error occurs, the table is not returned to a partially incomplete and unusable state\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  2905.             echo "<input type='checkbox' checked='checked' name='comments'/> Comments [<a onmouseover='tooltip.show(\"Adds comments to the file to explain what is happening in each part of it\");' onmouseout='tooltip.hide();'>?</a>]<br/>";
  2906.             echo "</fieldset>";
  2907.             echo "<div style='clear:both;'></div>";
  2908.             echo "<br/><br/>";
  2909.             echo "<fieldset style='float:left;'><legend><b>Save As</b></legend>";
  2910.             echo "<input type='hidden' name='database_num' value='".$_SESSION['currentDB']."'/>";
  2911.             echo "<input type='text' name='filename' value='".str_replace(" ", "_", $db->getName())."_".date("n-j-y")."' style='width:400px;'/> <input type='submit' name='export' value='Export'/>";
  2912.             echo "</fieldset>";
  2913.             echo "</form>";
  2914.         }
  2915.         else if($view=="import")
  2916.         {
  2917.             if(isset($_POST['import']))
  2918.             {
  2919.                 echo "<div class='confirm'>";
  2920.                 echo "Import was successful.";
  2921.                 echo "</div><br/>";
  2922.             }
  2923.             echo "<form method='post' action='".PAGE."?view=import' enctype='multipart/form-data'>";
  2924.             echo "<fieldset><legend><b>File to import</b></legend>";
  2925.             echo "<input type='radio' name='export_type' checked='checked' value='sql'/> SQL";
  2926.             echo "<br/><br/>";
  2927.             echo "<input type='file' value='Choose File' name='file' style='background-color:transparent; border-style:none;'/> <input type='submit' value='Import' name='import'/>";
  2928.             echo "</fieldset>";
  2929.         }
  2930.  
  2931.         echo "</div>";
  2932.     }
  2933.  
  2934.     echo "<br/>";
  2935.     $endTimeTot = microtime(true); //get the current time at this point in the execution
  2936.     $timeTot = round(($endTimeTot - $startTimeTot), 4); //calculate the total time for page load
  2937.     echo "<span style='font-size:11px;'>Powered by <a href='http://code.google.com/p/phpliteadmin/' target='_blank' style='font-size:11px;'>".PROJECT."</a> and <a href='http://www.danedesigns.com' target='_blank' style='font-size:11px;'>Dane Designs</a> | Page generated in ".$timeTot." seconds.</span>";
  2938.     echo "</div>";
  2939.     echo "</div>";
  2940.     $db->close(); //close the database
  2941. }
  2942. echo "</body>";
  2943. echo "</html>";
  2944.  
  2945. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement