PageRenderTime 82ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/phpliteadmin.php

https://bitbucket.org/nicolus/delation
PHP | 4793 lines | 4597 code | 80 blank | 116 comment | 226 complexity | 3e15b00a35eb38a2d93fff5b6fa297ae MD5 | raw file
  1. <?php
  2. //
  3. // Project: phpLiteAdmin (http://phpliteadmin.googlecode.com)
  4. // Version: 1.9.3
  5. // Summary: PHP-based admin tool to manage SQLite2 and SQLite3 databases on the web
  6. // Last updated: 2012-11-02
  7. // Developers:
  8. // Dane Iracleous (daneiracleous@gmail.com)
  9. // Ian Aldrighetti (ian.aldrighetti@gmail.com)
  10. // George Flanagin & Digital Gaslight, Inc (george@digitalgaslight.com)
  11. // Christopher Kramer (crazy4chrissi@gmail.com)
  12. //
  13. //
  14. // Copyright (C) 2012 phpLiteAdmin
  15. //
  16. // This program is free software: you can redistribute it and/or modify
  17. // it under the terms of the GNU General Public License as published by
  18. // the Free Software Foundation, either version 3 of the License, or
  19. // (at your option) any later version.
  20. //
  21. // This program is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. // GNU General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU General Public License
  27. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. //
  29. ///////////////////////////////////////////////////////////////////////////
  30. //please report any bugs you encounter to http://code.google.com/p/phpliteadmin/issues/list
  31. //BEGIN USER-DEFINED VARIABLES
  32. //////////////////////////////
  33. //password to gain access
  34. $password = "admin";
  35. //directory relative to this file to search for databases (if false, manually list databases in the $databases variable)
  36. $directory = false;
  37. //whether or not to scan the subdirectories of the above directory infinitely deep
  38. $subdirectories = false;
  39. //if the above $directory variable is set to false, you must specify the databases manually in an array as the next variable
  40. //if any of the databases do not exist as they are referenced by their path, they will be created automatically
  41. $databases = array
  42. (
  43. array
  44. (
  45. "path"=> "db/db.sqlite",
  46. "name"=> "Database 1"
  47. ),
  48. );
  49. //a list of custom functions that can be applied to columns in the databases
  50. //make sure to define every function below if it is not a core PHP function
  51. $custom_functions = array('md5', 'md5rev', 'sha1', 'sha1rev', 'time', 'mydate', 'strtotime', 'myreplace');
  52. //define all the non-core custom functions
  53. function md5rev($value)
  54. {
  55. return strrev(md5($value));
  56. }
  57. function sha1rev($value)
  58. {
  59. return strrev(sha1($value));
  60. }
  61. function mydate($value)
  62. {
  63. return date("g:ia n/j/y", intval($value));
  64. }
  65. function myreplace($value)
  66. {
  67. return ereg_replace("[^A-Za-z0-9]", "", strval($value));
  68. }
  69. //changing the following variable allows multiple phpLiteAdmin installs to work under the same domain.
  70. $cookie_name = 'pla3412';
  71. //whether or not to put the app in debug mode where errors are outputted
  72. $debug = false;
  73. ////////////////////////////
  74. //END USER-DEFINED VARIABLES
  75. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  76. //there is no reason for the average user to edit anything below this comment
  77. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  78. session_start(); //don't mess with this - required for the login session
  79. date_default_timezone_set(date_default_timezone_get()); //needed to fix STRICT warnings about timezone issues
  80. if($debug==true)
  81. {
  82. ini_set("display_errors", 1);
  83. error_reporting(E_STRICT | E_ALL);
  84. }
  85. $startTimeTot = microtime(true); //start the timer to record page load time
  86. //the salt and password encrypting is probably unnecessary protection but is done just for the sake of being very secure
  87. //create a random salt for this session if a cookie doesn't already exist for it
  88. if(!isset($_SESSION[$cookie_name.'_salt']) && !isset($_COOKIE[$cookie_name.'_salt']))
  89. {
  90. $n = rand(10e16, 10e20);
  91. $_SESSION[$cookie_name.'_salt'] = base_convert($n, 10, 36);
  92. }
  93. else if(!isset($_SESSION[$cookie_name.'_salt']) && isset($_COOKIE[$cookie_name.'_salt'])) //session doesn't exist, but cookie does so grab it
  94. {
  95. $_SESSION[$cookie_name.'_salt'] = $_COOKIE[$cookie_name.'_salt'];
  96. }
  97. //build the basename of this file for later reference
  98. $info = pathinfo($_SERVER['PHP_SELF']);
  99. $thisName = $info['basename'];
  100. //constants
  101. define("PROJECT", "phpLiteAdmin");
  102. define("VERSION", "1.9.3");
  103. define("PAGE", $thisName);
  104. define("COOKIENAME", $cookie_name);
  105. define("SYSTEMPASSWORD", $password); // Makes things easier.
  106. define("SYSTEMPASSWORDENCRYPTED", md5($password."_".$_SESSION[$cookie_name.'_salt'])); //extra security - salted and encrypted password used for checking
  107. define("FORCETYPE", false); //force the extension that will be used (set to false in almost all circumstances except debugging)
  108. //data types array
  109. $types = array("INTEGER", "REAL", "TEXT", "BLOB");
  110. define("DATATYPES", serialize($types));
  111. //available SQLite functions array (don't add anything here or there will be problems)
  112. $functions = array("abs", "hex", "length", "lower", "ltrim", "random", "round", "rtrim", "trim", "typeof", "upper");
  113. define("FUNCTIONS", serialize($functions));
  114. define("CUSTOM_FUNCTIONS", serialize($custom_functions));
  115. //function that allows SQL delimiter to be ignored inside comments or strings
  116. function explode_sql($delimiter, $sql)
  117. {
  118. $ign = array('"' => '"', "'" => "'", "/*" => "*/", "--" => "\n"); // Ignore sequences.
  119. $out = array();
  120. $last = 0;
  121. $slen = strlen($sql);
  122. $dlen = strlen($delimiter);
  123. $i = 0;
  124. while($i < $slen)
  125. {
  126. // Split on delimiter
  127. if($slen - $i >= $dlen && substr($sql, $i, $dlen) == $delimiter)
  128. {
  129. array_push($out, substr($sql, $last, $i - $last));
  130. $last = $i + $dlen;
  131. $i += $dlen;
  132. continue;
  133. }
  134. // Eat comments and string literals
  135. foreach($ign as $start => $end)
  136. {
  137. $ilen = strlen($start);
  138. if($slen - $i >= $ilen && substr($sql, $i, $ilen) == $start)
  139. {
  140. $i+=strlen($start);
  141. $elen = strlen($end);
  142. while($i < $slen)
  143. {
  144. if($slen - $i >= $elen && substr($sql, $i, $elen) == $end)
  145. {
  146. // SQL comment characters can be escaped by doubling the character. This recognizes and skips those.
  147. if($start == $end && $slen - $i >= $elen*2 && substr($sql, $i, $elen*2) == $end.$end)
  148. {
  149. $i += $elen * 2;
  150. continue;
  151. }
  152. else
  153. {
  154. $i += $elen;
  155. continue 3;
  156. }
  157. }
  158. $i++;
  159. }
  160. continue 2;
  161. }
  162. }
  163. $i++;
  164. }
  165. if($last < $slen)
  166. array_push($out, substr($sql, $last, $slen - $last));
  167. return $out;
  168. }
  169. //function to scan entire directory tree and subdirectories
  170. function dir_tree($dir)
  171. {
  172. $path = '';
  173. $stack[] = $dir;
  174. while($stack)
  175. {
  176. $thisdir = array_pop($stack);
  177. if($dircont = scandir($thisdir))
  178. {
  179. $i=0;
  180. while(isset($dircont[$i]))
  181. {
  182. if($dircont[$i] !== '.' && $dircont[$i] !== '..')
  183. {
  184. $current_file = "{$thisdir}/{$dircont[$i]}";
  185. if(is_file($current_file))
  186. {
  187. $path[] = "{$thisdir}/{$dircont[$i]}";
  188. }
  189. elseif (is_dir($current_file))
  190. {
  191. $path[] = "{$thisdir}/{$dircont[$i]}";
  192. $stack[] = $current_file;
  193. }
  194. }
  195. $i++;
  196. }
  197. }
  198. }
  199. return $path;
  200. }
  201. //the function echo the help [?] links to the documentation
  202. function helpLink($name)
  203. {
  204. return "<a href='javascript:void' onclick='openHelp(\"".$name."\");' class='helpq' title='Help: ".$name."'>[?]</a>";
  205. }
  206. // function to encode value into HTML just like htmlentities, but with adjusted default settings
  207. function htmlencode($value, $flags=ENT_QUOTES, $encoding ="UTF-8")
  208. {
  209. return htmlentities($value, $flags, $encoding);
  210. }
  211. // 22 August 2011: gkf added this function to support display of
  212. // default values in the form used to INSERT new data.
  213. function deQuoteSQL($s)
  214. {
  215. return trim(trim($s), "'");
  216. }
  217. //
  218. // Authorization class
  219. // Maintains user's logged-in state and security of application
  220. //
  221. class Authorization
  222. {
  223. public function grant($remember)
  224. {
  225. if($remember) //user wants to be remembered, so set a cookie
  226. {
  227. $expire = time()+60*60*24*30; //set expiration to 1 month from now
  228. setcookie(COOKIENAME, SYSTEMPASSWORD, $expire);
  229. setcookie(COOKIENAME."_salt", $_SESSION[COOKIENAME.'_salt'], $expire);
  230. }
  231. else
  232. {
  233. //user does not want to be remembered, so destroy any potential cookies
  234. setcookie(COOKIENAME, "", time()-86400);
  235. setcookie(COOKIENAME."_salt", "", time()-86400);
  236. unset($_COOKIE[COOKIENAME]);
  237. unset($_COOKIE[COOKIENAME.'_salt']);
  238. }
  239. $_SESSION[COOKIENAME.'password'] = SYSTEMPASSWORDENCRYPTED;
  240. }
  241. public function revoke()
  242. {
  243. //destroy everything - cookies and session vars
  244. setcookie(COOKIENAME, "", time()-86400);
  245. setcookie(COOKIENAME."_salt", "", time()-86400);
  246. unset($_COOKIE[COOKIENAME]);
  247. unset($_COOKIE[COOKIENAME.'_salt']);
  248. session_unset();
  249. session_destroy();
  250. }
  251. public function isAuthorized()
  252. {
  253. // Is this just session long? (What!?? -DI)
  254. if((isset($_SESSION[COOKIENAME.'password']) && $_SESSION[COOKIENAME.'password'] == SYSTEMPASSWORDENCRYPTED) || (isset($_COOKIE[COOKIENAME]) && isset($_COOKIE[COOKIENAME.'_salt']) && md5($_COOKIE[COOKIENAME]."_".$_COOKIE[COOKIENAME.'_salt']) == SYSTEMPASSWORDENCRYPTED))
  255. return true;
  256. else
  257. {
  258. return false;
  259. }
  260. }
  261. }
  262. //
  263. // Database class
  264. // Generic database abstraction class to manage interaction with database without worrying about SQLite vs. PHP versions
  265. //
  266. class Database
  267. {
  268. protected $db; //reference to the DB object
  269. protected $type; //the extension for PHP that handles SQLite
  270. protected $data;
  271. protected $lastResult;
  272. protected $fns;
  273. public function __construct($data)
  274. {
  275. $this->data = $data;
  276. $this->fns = array();
  277. try
  278. {
  279. 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
  280. {
  281. echo "<div class='confirm' style='margin:20px;'>";
  282. echo "The database, '".htmlencode($this->data["path"])."', does not exist and cannot be created because the containing directory, '".htmlencode(dirname($this->data["path"]))."', is not writable. The application is unusable until you make it writable.";
  283. echo "<form action='".PAGE."' method='post'>";
  284. echo "<input type='submit' value='Log Out' name='logout' class='btn'/>";
  285. echo "</form>";
  286. echo "</div><br/>";
  287. exit();
  288. }
  289. $ver = $this->getVersion();
  290. switch(true)
  291. {
  292. case (FORCETYPE=="PDO" || ((FORCETYPE==false || $ver!=-1) && class_exists("PDO") && ($ver==-1 || $ver==3))):
  293. $this->db = new PDO("sqlite:".$this->data['path']);
  294. if($this->db!=NULL)
  295. {
  296. $this->type = "PDO";
  297. $cfns = unserialize(CUSTOM_FUNCTIONS);
  298. for($i=0; $i<sizeof($cfns); $i++)
  299. {
  300. $this->db->sqliteCreateFunction($cfns[$i], $cfns[$i], 1);
  301. $this->addUserFunction($cfns[$i]);
  302. }
  303. break;
  304. }
  305. case (FORCETYPE=="SQLite3" || ((FORCETYPE==false || $ver!=-1) && class_exists("SQLite3") && ($ver==-1 || $ver==3))):
  306. $this->db = new SQLite3($this->data['path']);
  307. if($this->db!=NULL)
  308. {
  309. $cfns = unserialize(CUSTOM_FUNCTIONS);
  310. for($i=0; $i<sizeof($cfns); $i++)
  311. {
  312. $this->db->createFunction($cfns[$i], $cfns[$i], 1);
  313. $this->addUserFunction($cfns[$i]);
  314. }
  315. $this->type = "SQLite3";
  316. break;
  317. }
  318. case (FORCETYPE=="SQLiteDatabase" || ((FORCETYPE==false || $ver!=-1) && class_exists("SQLiteDatabase") && ($ver==-1 || $ver==2))):
  319. $this->db = new SQLiteDatabase($this->data['path']);
  320. if($this->db!=NULL)
  321. {
  322. $cfns = unserialize(CUSTOM_FUNCTIONS);
  323. for($i=0; $i<sizeof($cfns); $i++)
  324. {
  325. $this->db->createFunction($cfns[$i], $cfns[$i], 1);
  326. $this->addUserFunction($cfns[$i]);
  327. }
  328. $this->type = "SQLiteDatabase";
  329. break;
  330. }
  331. default:
  332. $this->showError();
  333. exit();
  334. }
  335. }
  336. catch(Exception $e)
  337. {
  338. $this->showError();
  339. exit();
  340. }
  341. }
  342. public function getUserFunctions()
  343. {
  344. return $this->fns;
  345. }
  346. public function addUserFunction($name)
  347. {
  348. array_push($this->fns, $name);
  349. }
  350. public function getError()
  351. {
  352. if($this->type=="PDO")
  353. {
  354. $e = $this->db->errorInfo();
  355. return $e[2];
  356. }
  357. else if($this->type=="SQLite3")
  358. {
  359. return $this->db->lastErrorMsg();
  360. }
  361. else
  362. {
  363. return sqlite_error_string($this->db->lastError());
  364. }
  365. }
  366. public function showError()
  367. {
  368. $classPDO = class_exists("PDO");
  369. $classSQLite3 = class_exists("SQLite3");
  370. $classSQLiteDatabase = class_exists("SQLiteDatabase");
  371. if($classPDO)
  372. $strPDO = "installed";
  373. else
  374. $strPDO = "not installed";
  375. if($classSQLite3)
  376. $strSQLite3 = "installed";
  377. else
  378. $strSQLite3 = "not installed";
  379. if($classSQLiteDatabase)
  380. $strSQLiteDatabase = "installed";
  381. else
  382. $strSQLiteDatabase = "not installed";
  383. echo "<div class='confirm' style='margin:20px;'>";
  384. 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/>";
  385. echo "<i>Checking supported SQLite PHP extensions...<br/><br/>";
  386. echo "<b>PDO</b>: ".$strPDO."<br/>";
  387. echo "<b>SQLite3</b>: ".$strSQLite3."<br/>";
  388. echo "<b>SQLiteDatabase</b>: ".$strSQLiteDatabase."<br/><br/>...done.</i><br/><br/>";
  389. if(!$classPDO && !$classSQLite3 && !$classSQLiteDatabase)
  390. 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.";
  391. else
  392. {
  393. if(!$classPDO && !$classSQLite3 && $this->getVersion()==3)
  394. 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.";
  395. else if(!$classSQLiteDatabase && $this->getVersion()==2)
  396. 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.";
  397. else
  398. echo "The problem cannot be diagnosed properly. Please file an issue report at http://phpliteadmin.googlecode.com.";
  399. }
  400. echo "</div><br/>";
  401. }
  402. public function __destruct()
  403. {
  404. if($this->db)
  405. $this->close();
  406. }
  407. //get the exact PHP extension being used for SQLite
  408. public function getType()
  409. {
  410. return $this->type;
  411. }
  412. //get the name of the database
  413. public function getName()
  414. {
  415. return $this->data["name"];
  416. }
  417. //get the filename of the database
  418. public function getPath()
  419. {
  420. return $this->data["path"];
  421. }
  422. //get the version of the database
  423. public function getVersion()
  424. {
  425. if(file_exists($this->data['path'])) //make sure file exists before getting its contents
  426. {
  427. $content = strtolower(file_get_contents($this->data['path'], NULL, NULL, 0, 40)); //get the first 40 characters of the database file
  428. $p = strpos($content, "** this file contains an sqlite 2"); //this text is at the beginning of every SQLite2 database
  429. if($p!==false) //the text is found - this is version 2
  430. return 2;
  431. else
  432. return 3;
  433. }
  434. else //return -1 to indicate that it does not exist and needs to be created
  435. {
  436. return -1;
  437. }
  438. }
  439. //get the size of the database
  440. public function getSize()
  441. {
  442. return round(filesize($this->data["path"])*0.0009765625, 1)." KB";
  443. }
  444. //get the last modified time of database
  445. public function getDate()
  446. {
  447. return date("g:ia \o\\n F j, Y", filemtime($this->data["path"]));
  448. }
  449. //get number of affected rows from last query
  450. public function getAffectedRows()
  451. {
  452. if($this->type=="PDO")
  453. return $this->lastResult->rowCount();
  454. else if($this->type=="SQLite3")
  455. return $this->db->changes();
  456. else if($this->type=="SQLiteDatabase")
  457. return $this->db->changes();
  458. }
  459. public function close()
  460. {
  461. if($this->type=="PDO")
  462. $this->db = NULL;
  463. else if($this->type=="SQLite3")
  464. $this->db->close();
  465. else if($this->type=="SQLiteDatabase")
  466. $this->db = NULL;
  467. }
  468. public function beginTransaction()
  469. {
  470. $this->query("BEGIN");
  471. }
  472. public function commitTransaction()
  473. {
  474. $this->query("COMMIT");
  475. }
  476. public function rollbackTransaction()
  477. {
  478. $this->query("ROLLBACK");
  479. }
  480. //generic query wrapper
  481. public function query($query, $ignoreAlterCase=false)
  482. {
  483. global $debug;
  484. if(strtolower(substr(ltrim($query),0,5))=='alter' && $ignoreAlterCase==false) //this query is an ALTER query - call the necessary function
  485. {
  486. preg_match("/^\s*ALTER\s+TABLE\s+\"((?:[^\"]|\"\")+)\"\s+(.*)$/i",$query,$matches);
  487. if(!isset($matches[1]) || !isset($matches[2]))
  488. {
  489. if($debug) echo "<span title='".htmlencode($query)."' onclick='this.innerHTML=\"".htmlencode(str_replace('"','\"',$query))."\"' style='cursor:pointer'>SQL?</span><br />";
  490. return false;
  491. }
  492. $tablename = str_replace('""','"',$matches[1]);
  493. $alterdefs = $matches[2];
  494. if($debug) echo "ALTER TABLE QUERY=(".htmlencode($query)."), tablename=($tablename), alterdefs=($alterdefs)<hr>";
  495. $result = $this->alterTable($tablename, $alterdefs);
  496. }
  497. else //this query is normal - proceed as normal
  498. {
  499. $result = $this->db->query($query);
  500. if($debug) echo "<span title='".htmlencode($query)."' onclick='this.innerHTML=\"".htmlencode(str_replace('"','\"',$query))."\"' style='cursor:pointer'>SQL?</span><br />";
  501. }
  502. if(!$result)
  503. return false;
  504. $this->lastResult = $result;
  505. return $result;
  506. }
  507. //wrapper for an INSERT and returns the ID of the inserted row
  508. public function insert($query)
  509. {
  510. $result = $this->query($query);
  511. if($this->type=="PDO")
  512. return $this->db->lastInsertId();
  513. else if($this->type=="SQLite3")
  514. return $this->db->lastInsertRowID();
  515. else if($this->type=="SQLiteDatabase")
  516. return $this->db->lastInsertRowid();
  517. }
  518. //returns an array for SELECT
  519. public function select($query, $mode="both")
  520. {
  521. $result = $this->query($query);
  522. if(!$result) //make sure the result is valid
  523. return NULL;
  524. if($this->type=="PDO")
  525. {
  526. if($mode=="assoc")
  527. $mode = PDO::FETCH_ASSOC;
  528. else if($mode=="num")
  529. $mode = PDO::FETCH_NUM;
  530. else
  531. $mode = PDO::FETCH_BOTH;
  532. return $result->fetch($mode);
  533. }
  534. else if($this->type=="SQLite3")
  535. {
  536. if($mode=="assoc")
  537. $mode = SQLITE3_ASSOC;
  538. else if($mode=="num")
  539. $mode = SQLITE3_NUM;
  540. else
  541. $mode = SQLITE3_BOTH;
  542. return $result->fetchArray($mode);
  543. }
  544. else if($this->type=="SQLiteDatabase")
  545. {
  546. if($mode=="assoc")
  547. $mode = SQLITE_ASSOC;
  548. else if($mode=="num")
  549. $mode = SQLITE_NUM;
  550. else
  551. $mode = SQLITE_BOTH;
  552. return $result->fetch($mode);
  553. }
  554. }
  555. //returns an array of arrays after doing a SELECT
  556. public function selectArray($query, $mode="both")
  557. {
  558. $result = $this->query($query);
  559. if(!$result) //make sure the result is valid
  560. return NULL;
  561. if($this->type=="PDO")
  562. {
  563. if($mode=="assoc")
  564. $mode = PDO::FETCH_ASSOC;
  565. else if($mode=="num")
  566. $mode = PDO::FETCH_NUM;
  567. else
  568. $mode = PDO::FETCH_BOTH;
  569. return $result->fetchAll($mode);
  570. }
  571. else if($this->type=="SQLite3")
  572. {
  573. if($mode=="assoc")
  574. $mode = SQLITE3_ASSOC;
  575. else if($mode=="num")
  576. $mode = SQLITE3_NUM;
  577. else
  578. $mode = SQLITE3_BOTH;
  579. $arr = array();
  580. $i = 0;
  581. while($res = $result->fetchArray($mode))
  582. {
  583. $arr[$i] = $res;
  584. $i++;
  585. }
  586. return $arr;
  587. }
  588. else if($this->type=="SQLiteDatabase")
  589. {
  590. if($mode=="assoc")
  591. $mode = SQLITE_ASSOC;
  592. else if($mode=="num")
  593. $mode = SQLITE_NUM;
  594. else
  595. $mode = SQLITE_BOTH;
  596. return $result->fetchAll($mode);
  597. }
  598. }
  599. // SQlite supports multiple ways of surrounding names in quotes:
  600. // single-quotes, double-quotes, backticks, square brackets.
  601. // As sqlite does not keep this strict, we also need to be flexible here.
  602. // This function generates a regex that matches any of the possibilities.
  603. private function sqlite_surroundings_preg($name,$preg_quote=true,$notAllowedIfNone="'\"")
  604. {
  605. if($name=="*" || $name=="+")
  606. {
  607. $nameSingle = "(?:[^']|'')".$name;
  608. $nameDouble = "(?:[^\"]|\"\")".$name;
  609. $nameBacktick = "(?:[^`]|``)".$name;
  610. $nameSquare = "(?:[^\]]|\]\])".$name;
  611. $nameNo = "[^".$notAllowedIfNone."]".$name;
  612. }
  613. else
  614. {
  615. if($preg_quote) $name = preg_quote($name,"/");
  616. $nameSingle = str_replace("'","''",$name);
  617. $nameDouble = str_replace('"','""',$name);
  618. $nameBacktick = str_replace('`','``',$name);
  619. $nameSquare = str_replace(']',']]',$name);
  620. $nameNo = $name;
  621. }
  622. $preg = "(?:'".$nameSingle."'|". // single-quote surrounded or not in quotes (correct SQL for values/new names)
  623. $nameNo."|". // not surrounded (correct SQL if not containing reserved words, spaces or some special chars)
  624. "\"".$nameDouble."\"|". // double-quote surrounded (correct SQL for identifiers)
  625. "`".$nameBacktick."`|". // backtick surrounded (MySQL-Style)
  626. "\[".$nameSquare."\])"; // square-bracket surrounded (MS Access/SQL server-Style)
  627. return $preg;
  628. }
  629. // function that is called for an alter table statement in a query
  630. // code borrowed with permission from http://code.jenseng.com/db/
  631. // this has been completely debugged / rewritten by Christopher Kramer
  632. public function alterTable($table, $alterdefs)
  633. {
  634. global $debug;
  635. if($debug) echo "ALTER TABLE: table=($table), alterdefs=($alterdefs)<hr>";
  636. if($alterdefs != '')
  637. {
  638. $recreateQueries = array();
  639. $tempQuery = "SELECT sql,name,type FROM sqlite_master WHERE tbl_name = ".$this->quote($table)." ORDER BY type DESC";
  640. $result = $this->query($tempQuery);
  641. $resultArr = $this->selectArray($tempQuery);
  642. if($this->type=="PDO")
  643. $result->closeCursor();
  644. if(sizeof($resultArr)<1)
  645. return false;
  646. for($i=0; $i<sizeof($resultArr); $i++)
  647. {
  648. $row = $resultArr[$i];
  649. if($row['type'] != 'table')
  650. {
  651. // store the CREATE statements of triggers and indexes to recreate them later
  652. $recreateQueries[] = $row['sql']."; ";
  653. if($debug) echo "recreate=(".$row['sql'].";)<hr />";
  654. }
  655. else
  656. {
  657. // ALTER the table
  658. $tmpname = 't'.time();
  659. $origsql = $row['sql'];
  660. $createtemptableSQL = "CREATE TEMPORARY TABLE ".$this->quote($tmpname)." ".
  661. preg_replace("/^\s*CREATE\s+TABLE\s+".$this->sqlite_surroundings_preg($table)."\s*(\(.*)$/i", '$1', $origsql, 1);
  662. if($debug) echo "createtemptableSQL=($createtemptableSQL)<hr>";
  663. $createindexsql = array();
  664. preg_match_all("/(?:DROP|ADD|CHANGE|RENAME TO)\s+(?:\"(?:[^\"]|\"\")+\"|'(?:[^']|'')+')((?:[^,')]|'[^']*')+)?/i",$alterdefs,$matches);
  665. $defs = $matches[0];
  666. $get_oldcols_query = "PRAGMA table_info(".$this->quote_id($table).")";
  667. $result_oldcols = $this->selectArray($get_oldcols_query);
  668. $newcols = array();
  669. $coltypes = array();
  670. foreach($result_oldcols as $column_info)
  671. {
  672. $newcols[$column_info['name']] = $column_info['name'];
  673. $coltypes[$column_info['name']] = $column_info['type'];
  674. }
  675. $newcolumns = '';
  676. $oldcolumns = '';
  677. reset($newcols);
  678. while(list($key, $val) = each($newcols))
  679. {
  680. $newcolumns .= ($newcolumns?', ':'').$this->quote_id($val);
  681. $oldcolumns .= ($oldcolumns?', ':'').$this->quote_id($key);
  682. }
  683. $copytotempsql = 'INSERT INTO '.$this->quote_id($tmpname).'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$this->quote_id($table);
  684. $dropoldsql = 'DROP TABLE '.$this->quote_id($table);
  685. $createtesttableSQL = $createtemptableSQL;
  686. if(count($defs)<1)
  687. {
  688. if($debug) echo "ERROR: defs&lt;1<hr />";
  689. return false;
  690. }
  691. foreach($defs as $def)
  692. {
  693. if($debug) echo "def=$def<hr />";
  694. $parse_def = preg_match("/^(DROP|ADD|CHANGE|RENAME TO)\s+(?:\"((?:[^\"]|\"\")+)\"|'((?:[^']|'')+)')((?:\s+'((?:[^']|'')+)')?\s+(TEXT|INTEGER|BLOB|REAL).*)?\s*$/i",$def,$matches);
  695. if($parse_def===false)
  696. {
  697. if($debug) echo "ERROR: !parse_def<hr />";
  698. return false;
  699. }
  700. if(!isset($matches[1]))
  701. {
  702. if($debug) echo "ERROR: !isset(matches[1])<hr />";
  703. return false;
  704. }
  705. $action = strtolower($matches[1]);
  706. if($action == 'add' || $action == 'rename to')
  707. $column = str_replace("''","'",$matches[3]); // enclosed in ''
  708. else
  709. $column = str_replace('""','"',$matches[2]); // enclosed in ""
  710. $column_escaped = str_replace("'","''",$column);
  711. if($debug) echo "action=($action), column=($column), column_escaped=($column_escaped)<hr />";
  712. /* we build a regex that devides the CREATE TABLE statement parts:
  713. Part example Group Explanation
  714. 1. CREATE TABLE t... ( $1
  715. 2. 'col1' ..., 'col2' ..., 'colN' ..., $3 (with col1-colN being columns that are not changed and listed before the col to change)
  716. 3. 'colX' ..., - (with colX being the column to change/drop)
  717. 4. 'colX+1' ..., ..., 'colK') $5 (with colX+1-colK being columns after the column to change/drop)
  718. */
  719. $preg_create_table = "\s*(CREATE\s+TEMPORARY\s+TABLE\s+'?".preg_quote($tmpname,"/")."'?\s*\()"; // This is group $1 (keep unchanged)
  720. $preg_column_definiton = "\s*".$this->sqlite_surroundings_preg("+",false," '\"\[`")."(?:\s+".$this->sqlite_surroundings_preg("*",false,"'\",`\[) ").")+"; // catches a complete column definition, even if it is
  721. // 'column' TEXT NOT NULL DEFAULT 'we have a comma, here and a double ''quote!'
  722. if($debug) echo "preg_column_definition=(".$preg_column_definiton.")<hr />";
  723. $preg_columns_before = // columns before the one changed/dropped (keep)
  724. "(?:".
  725. "(". // group $2. Keep this one unchanged!
  726. "(?:".
  727. "$preg_column_definiton,\s*". // column definition + comma
  728. ")*". // there might be any number of such columns here
  729. $preg_column_definiton. // last column definition
  730. ")". // end of group $2
  731. ",\s*" // the last comma of the last column before the column to change. Do not keep it!
  732. .")?"; // there might be no columns before
  733. if($debug) echo "preg_columns_before=(".$preg_columns_before.")<hr />";
  734. $preg_columns_after = "(,\s*([^)]+))?"; // the columns after the column to drop. This is group $3 (drop) or $4(change) (keep!)
  735. // we could remove the comma using $6 instead of $5, but then we might have no comma at all.
  736. // Keeping it leaves a problem if we drop the first column, so we fix that case in another regex.
  737. $table_new = $table;
  738. switch($action)
  739. {
  740. case 'add':
  741. if(!isset($matches[4]))
  742. {
  743. return false;
  744. }
  745. $new_col_definition = "'$column_escaped' ".$matches[4];
  746. $preg_pattern_add = "/^".$preg_create_table."(.*)\\)\s*$/";
  747. // append the column definiton in the CREATE TABLE statement
  748. $newSQL = preg_replace($preg_pattern_add, '$1$2, ', $createtesttableSQL).$new_col_definition.')';
  749. if($debug)
  750. {
  751. echo $createtesttableSQL."<hr>";
  752. echo $newSQL."<hr>";
  753. echo $preg_pattern_add."<hr>";
  754. }
  755. if($newSQL==$createtesttableSQL) // pattern did not match, so column removal did not succed
  756. return false;
  757. $createtesttableSQL = $newSQL;
  758. break;
  759. case 'change':
  760. if(!isset($matches[5]) || !isset($matches[6]))
  761. {
  762. return false;
  763. }
  764. $new_col_name = $matches[5];
  765. $new_col_type = $matches[6];
  766. $new_col_definition = "'$new_col_name' $new_col_type";
  767. $preg_column_to_change = "\s*".$this->sqlite_surroundings_preg($column)."(?:\s+".preg_quote($coltypes[$column]).")?(\s+(?:".$this->sqlite_surroundings_preg("*",false,",'\")`\[").")+)?";
  768. // replace this part (we want to change this column)
  769. // group $3 contains the column constraints (keep!). the name & data type is replaced.
  770. $preg_pattern_change = "/^".$preg_create_table.$preg_columns_before.$preg_column_to_change.$preg_columns_after."\s*\\)\s*$/";
  771. // replace the column definiton in the CREATE TABLE statement
  772. $newSQL = preg_replace($preg_pattern_change, '$1$2,'.strtr($new_col_definition, array('\\' => '\\\\', '$' => '\$')).'$3$4)', $createtesttableSQL);
  773. // remove comma at the beginning if the first column is changed
  774. // probably somebody is able to put this into the first regex (using lookahead probably).
  775. $newSQL = preg_replace("/^\s*(CREATE\s+TEMPORARY\s+TABLE\s+'".preg_quote($tmpname,"/")."'\s+\(),\s*/",'$1',$newSQL);
  776. if($debug)
  777. {
  778. echo "preg_column_to_change=(".$preg_column_to_change.")<hr />";
  779. echo $createtesttableSQL."<hr />";
  780. echo $newSQL."<hr />";
  781. echo $preg_pattern_change."<hr />";
  782. }
  783. if($newSQL==$createtesttableSQL || $newSQL=="") // pattern did not match, so column removal did not succed
  784. return false;
  785. $createtesttableSQL = $newSQL;
  786. $newcols[$column] = str_replace("''","'",$new_col_name);
  787. break;
  788. case 'drop':
  789. $preg_column_to_drop = "\s*".$this->sqlite_surroundings_preg($column)."\s+(?:".$this->sqlite_surroundings_preg("*",false,",')\"\[`").")+"; // delete this part (we want to drop this column)
  790. $preg_pattern_drop = "/^".$preg_create_table.$preg_columns_before.$preg_column_to_drop.$preg_columns_after."\s*\\)\s*$/";
  791. // remove the column out of the CREATE TABLE statement
  792. $newSQL = preg_replace($preg_pattern_drop, '$1$2$3)', $createtesttableSQL);
  793. // remove comma at the beginning if the first column is removed
  794. // probably somebody is able to put this into the first regex (using lookahead probably).
  795. $newSQL = preg_replace("/^\s*(CREATE\s+TEMPORARY\s+TABLE\s+'".preg_quote($tmpname,"/")."'\s+\(),\s*/",'$1',$newSQL);
  796. if($debug)
  797. {
  798. echo $createtesttableSQL."<hr>";
  799. echo $newSQL."<hr>";
  800. echo $preg_pattern_drop."<hr>";
  801. }
  802. if($newSQL==$createtesttableSQL || $newSQL=="") // pattern did not match, so column removal did not succed
  803. return false;
  804. $createtesttableSQL = $newSQL;
  805. unset($newcols[$column]);
  806. break;
  807. case 'rename to':
  808. // don't change column definition at all
  809. $newSQL = $createtesttableSQL;
  810. // only change the name of the table
  811. $table_new = $column;
  812. break;
  813. default:
  814. if($default) echo 'ERROR: unknown alter operation!<hr />';
  815. return false;
  816. }
  817. }
  818. $droptempsql = 'DROP TABLE '.$this->quote_id($tmpname);
  819. $createnewtableSQL = "CREATE TABLE ".$this->quote($table_new)." ".preg_replace("/^\s*CREATE\s+TEMPORARY\s+TABLE\s+'?".str_replace("'","''",preg_quote($tmpname,"/"))."'?\s+(.*)$/i", '$1', $createtesttableSQL, 1);
  820. $newcolumns = '';
  821. $oldcolumns = '';
  822. reset($newcols);
  823. while(list($key,$val) = each($newcols))
  824. {
  825. $newcolumns .= ($newcolumns?', ':'').$this->quote_id($val);
  826. $oldcolumns .= ($oldcolumns?', ':'').$this->quote_id($key);
  827. }
  828. $copytonewsql = 'INSERT INTO '.$this->quote_id($table_new).'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$this->quote_id($tmpname);
  829. }
  830. }
  831. $alter_transaction = 'BEGIN; ';
  832. $alter_transaction .= $createtemptableSQL.'; '; //create temp table
  833. $alter_transaction .= $copytotempsql.'; '; //copy to table
  834. $alter_transaction .= $dropoldsql.'; '; //drop old table
  835. $alter_transaction .= $createnewtableSQL.'; '; //recreate original table
  836. $alter_transaction .= $copytonewsql.'; '; //copy back to original table
  837. $alter_transaction .= $droptempsql.'; '; //drop temp table
  838. $preg_index="/^\s*(CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:".$this->sqlite_surroundings_preg("+",false," '\"\[`")."\s*)*ON\s+)(".$this->sqlite_surroundings_preg($table).")(\s*\((?:".$this->sqlite_surroundings_preg("+",false," '\"\[`")."\s*)*\)\s*;)\s*$/i";
  839. for($i=0; $i<sizeof($recreateQueries); $i++)
  840. {
  841. // recreate triggers / indexes
  842. if($table == $table_new)
  843. {
  844. // we had no RENAME TO, so we can recreate indexes/triggers just like the original ones
  845. $alter_transaction .= $recreateQueries[$i];
  846. } else
  847. {
  848. // we had a RENAME TO, so we need to exchange the table-name in the CREATE-SQL of triggers & indexes
  849. // first let's try if it's an index...
  850. $recreate_queryIndex = preg_replace($preg_index, '$1'.$this->quote_id(strtr($table_new, array('\\' => '\\\\', '$' => '\$'))).'$3 ', $recreateQueries[$i]);
  851. if($recreate_queryIndex!=$recreateQueries[$i] && $recreate_queryIndex != NULL)
  852. {
  853. // the CREATE INDEX regex did match
  854. $alter_transaction .= $recreate_queryIndex;
  855. } else
  856. {
  857. // the CREATE INDEX regex did not match, so we try if it's a CREATE TRIGGER
  858. $recreate_queryTrigger = $recreateQueries[$i];
  859. // TODO: IMPLEMENT
  860. $alter_transaction .= $recreate_queryTrigger;
  861. }
  862. }
  863. }
  864. $alter_transaction .= 'COMMIT;';
  865. if($debug) echo $alter_transaction;
  866. return $this->multiQuery($alter_transaction);
  867. }
  868. }
  869. //multiple query execution
  870. public function multiQuery($query)
  871. {
  872. $error = "Unknown error.";
  873. if($this->type=="PDO")
  874. {
  875. $success = $this->db->exec($query);
  876. if(!$success) $error = implode(" - ", $this->db->errorInfo());
  877. }
  878. else if($this->type=="SQLite3")
  879. {
  880. $success = $this->db->exec($query);
  881. if(!$success) $error = $this->db->lastErrorMsg();
  882. }
  883. else
  884. {
  885. $success = $this->db->queryExec($query, $error);
  886. }
  887. if(!$success)
  888. {
  889. return "Error in query: '".htmlencode($error)."'";
  890. }
  891. else
  892. {
  893. return true;
  894. }
  895. }
  896. //get number of rows in table
  897. public function numRows($table)
  898. {
  899. $result = $this->select("SELECT Count(*) FROM ".$this->quote_id($table));
  900. return $result[0];
  901. }
  902. //correctly escape a string to be injected into an SQL query
  903. public function quote($value)
  904. {
  905. if($this->type=="PDO")
  906. {
  907. // PDO quote() escapes and adds quotes
  908. return $this->db->quote($value);
  909. }
  910. else if($this->type=="SQLite3")
  911. {
  912. return "'".$this->db->escapeString($value)."'";
  913. }
  914. else
  915. {
  916. return "'".sqlite_escape_string($value)."'";
  917. }
  918. }
  919. //correctly escape an identifier (column / table / trigger / index name) to be injected into an SQL query
  920. public function quote_id($value)
  921. {
  922. // double-quotes need to be escaped by doubling them
  923. $value = str_replace('"','""',$value);
  924. return '"'.$value.'"';
  925. }
  926. //import sql
  927. public function import_sql($query)
  928. {
  929. return $this->multiQuery($query);
  930. }
  931. //import csv
  932. public function import_csv($filename, $table, $field_terminate, $field_enclosed, $field_escaped, $null, $fields_in_first_row)
  933. {
  934. // CSV import implemented by Christopher Kramer - http://www.christosoft.de
  935. $csv_handle = fopen($filename,'r');
  936. $csv_insert = "BEGIN;\n";
  937. $csv_number_of_rows = 0;
  938. // PHP requires enclosure defined, but has no problem if it was not used
  939. if($field_enclosed=="") $field_enclosed='"';
  940. // PHP requires escaper defined
  941. if($field_escaped=="") $field_escaped='\\';
  942. while(!feof($csv_handle))
  943. {
  944. $csv_data = fgetcsv($csv_handle, 0, $field_terminate, $field_enclosed, $field_escaped);
  945. if($csv_data[0] != NULL || count($csv_data)>1)
  946. {
  947. $csv_number_of_rows++;
  948. if($fields_in_first_row && $csv_number_of_rows==1) continue;
  949. $csv_col_number = count($csv_data);
  950. $csv_insert .= "INSERT INTO ".$this->quote_id($table)." VALUES (";
  951. foreach($csv_data as $csv_col => $csv_cell)
  952. {
  953. if($csv_cell == $null) $csv_insert .= "NULL";
  954. else
  955. {
  956. $csv_insert.= $this->quote($csv_cell);
  957. }
  958. if($csv_col == $csv_col_number-2 && $csv_data[$csv_col+1]=='')
  959. {
  960. // the CSV row ends with the separator (like old phpliteadmin exported)
  961. break;
  962. }
  963. if($csv_col < $csv_col_number-1) $csv_insert .= ",";
  964. }
  965. $csv_insert .= ");\n";
  966. if($csv_number_of_rows > 5000)
  967. {
  968. $csv_insert .= "COMMIT;\nBEGIN;\n";
  969. $csv_number_of_rows = 0;
  970. }
  971. }
  972. }
  973. $csv_insert .= "COMMIT;";
  974. fclose($csv_handle);
  975. return $this->multiQuery($csv_insert);
  976. }
  977. //export csv
  978. public function export_csv($tables, $field_terminate, $field_enclosed, $field_escaped, $null, $crlf, $fields_in_first_row)
  979. {
  980. $field_enclosed = stripslashes($field_enclosed);
  981. $query = "SELECT * FROM sqlite_master WHERE type='table' or type='view' ORDER BY type DESC";
  982. $result = $this->selectArray($query);
  983. for($i=0; $i<sizeof($result); $i++)
  984. {
  985. $valid = false;
  986. for($j=0; $j<sizeof($tables); $j++)
  987. {
  988. if($result[$i]['tbl_name']==$tables[$j])
  989. $valid = true;
  990. }
  991. if($valid)
  992. {
  993. $query = "PRAGMA table_info(".$this->quote_id($result[$i]['tbl_name']).")";
  994. $temp = $this->selectArray($query);
  995. $cols = array();
  996. for($z=0; $z<sizeof($temp); $z++)
  997. $cols[$z] = $temp[$z][1];
  998. if($fields_in_first_row)
  999. {
  1000. for($z=0; $z<sizeof($cols); $z++)
  1001. {
  1002. echo $field_enclosed.$cols[$z].$field_enclosed;
  1003. // do not terminate the last column!
  1004. if($z < sizeof($cols)-1)
  1005. echo $field_terminate;
  1006. }
  1007. echo "\r\n";
  1008. }
  1009. $query = "SELECT * FROM ".$this->quote_id($result[$i]['tbl_name']);
  1010. $arr = $this->selectArray($query, "assoc");
  1011. for($z=0; $z<sizeof($arr); $z++)
  1012. {
  1013. for($y=0; $y<sizeof($cols); $y++)
  1014. {
  1015. $cell = $arr[$z][$cols[$y]];
  1016. if($crlf)
  1017. {
  1018. $cell = str_replace("\n","", $cell);
  1019. $cell = str_replace("\r","", $cell);
  1020. }
  1021. $cell = str_replace($field_terminate,$field_escaped.$field_terminate,$cell);
  1022. $cell = str_replace($field_enclosed,$field_escaped.$field_enclosed,$cell);
  1023. // do not enclose NULLs
  1024. if($cell == NULL)
  1025. echo $null;
  1026. else
  1027. echo $field_enclosed.$cell.$field_enclosed;
  1028. // do not terminate the last column!
  1029. if($y < sizeof($cols)-1)
  1030. echo $field_terminate;
  1031. }
  1032. if($z<sizeof($arr)-1)
  1033. echo "\r\n";
  1034. }
  1035. if($i<sizeof($result)-1)
  1036. echo "\r\n";
  1037. }
  1038. }
  1039. }
  1040. //export sql
  1041. public function export_sql($tables, $drop, $structure, $data, $transaction, $comments)
  1042. {
  1043. if($comments)
  1044. {
  1045. echo "----\r\n";
  1046. echo "-- phpLiteAdmin database dump (http://phpliteadmin.googlecode.com)\r\n";
  1047. echo "-- phpLiteAdmin version: ".VERSION."\r\n";
  1048. echo "-- Exported on ".date('M jS, Y, h:i:sA')."\r\n";
  1049. echo "-- Database file: ".$this->getPath()."\r\n";
  1050. echo "----\r\n";
  1051. }
  1052. $query = "SELECT * FROM sqlite_master WHERE type='table' OR type='index' OR type='view' OR type='trigger' ORDER BY type='trigger', type='index', type='view', type='table'";
  1053. $result = $this->selectArray($query);
  1054. if($transaction)
  1055. echo "BEGIN TRANSACTION;\r\n";
  1056. //iterate through each table
  1057. for($i=0; $i<sizeof($result); $i++)
  1058. {
  1059. $valid = false;
  1060. for($j=0; $j<sizeof($tables); $j++)
  1061. {
  1062. if($result[$i]['tbl_name']==$tables[$j])
  1063. $valid = true;
  1064. }
  1065. if($valid)
  1066. {
  1067. if($drop)
  1068. {
  1069. if($comments)
  1070. {
  1071. echo "\r\n----\r\n";
  1072. echo "-- Drop ".$result[$i]['type']." for ".$result[$i]['name']."\r\n";
  1073. echo "----\r\n";
  1074. }
  1075. echo "DROP ".strtoupper($result[$i]['type'])." ".$this->quote_id($result[$i]['name']).";\r\n";
  1076. }
  1077. if($structure)
  1078. {
  1079. if($comments)
  1080. {
  1081. echo "\r\n----\r\n";
  1082. if($result[$i]['type']=="table" || $result[$i]['type']=="view")
  1083. echo "-- ".ucfirst($result[$i]['type'])." structure for ".$result[$i]['tbl_name']."\r\n";
  1084. else // index or trigger
  1085. echo "-- Structure for ".$result[$i]['type']." ".$result[$i]['name']." on table ".$result[$i]['tbl_name']."\r\n";
  1086. echo "----\r\n";
  1087. }
  1088. echo $result[$i]['sql'].";\r\n";
  1089. }
  1090. if($data && $result[$i]['type']=="table")
  1091. {
  1092. $query = "SELECT * FROM ".$this->quote_id($result[$i]['tbl_name']);
  1093. $arr = $this->selectArray($query, "assoc");
  1094. if($comments)
  1095. {
  1096. echo "\r\n----\r\n";
  1097. echo "-- Data dump for ".$result[$i]['tbl_name'].", a total of ".sizeof($arr)." rows\r\n";
  1098. echo "----\r\n";
  1099. }
  1100. $query = "PRAGMA table_info(".$this->quote_id($result[$i]['tbl_name']).")";
  1101. $temp = $this->selectArray($query);
  1102. $cols = array();
  1103. $cols_quoted = array();
  1104. $vals = array();
  1105. for($z=0; $z<sizeof($temp); $z++)
  1106. {
  1107. $cols[$z] = $temp[$z][1];
  1108. $cols_quoted[$z] = $this->quote_id($temp[$z][1]);
  1109. }
  1110. for($z=0; $z<sizeof($arr); $z++)
  1111. {
  1112. for($y=0; $y<sizeof($cols); $y++)
  1113. {
  1114. if(!isset($vals[$z]))
  1115. $vals[$z] = array();
  1116. if($arr[$z][$cols[$y]] === NULL)
  1117. $vals[$z][$cols[$y]] = 'NULL';
  1118. else
  1119. $vals[$z][$cols[$y]] = $this->quote($arr[$z][$cols[$y]]);
  1120. }
  1121. }
  1122. for($j=0; $j<sizeof($vals); $j++)
  1123. echo "INSERT INTO ".$this->quote_id($result[$i]['tbl_name'])." (".implode(",", $cols_quoted).") VALUES (".implode(",", $vals[$j]).");\r\n";
  1124. }
  1125. }
  1126. }
  1127. if($transaction)
  1128. echo "COMMIT;\r\n";
  1129. }
  1130. }
  1131. $auth = new Authorization(); //create authorization object
  1132. if(isset($_POST['logout'])) //user has attempted to log out
  1133. $auth->revoke();
  1134. else if(isset($_POST['login']) || isset($_POST['proc_login'])) //user has attempted to log in
  1135. {
  1136. $_POST['login'] = true;
  1137. if($_POST['password']==SYSTEMPASSWORD) //make sure passwords match before granting authorization
  1138. {
  1139. if(isset($_POST['remember']))
  1140. $auth->grant(true);
  1141. else
  1142. $auth->grant(false);
  1143. }
  1144. }
  1145. if($auth->isAuthorized())
  1146. {
  1147. //user is deleting a database
  1148. if(isset($_GET['database_delete']))
  1149. {
  1150. $dbpath = $_POST['database_delete'];
  1151. unlink($dbpath);
  1152. unset($_SESSION[COOKIENAME.'currentDB']);
  1153. }
  1154. //user is renaming a database
  1155. if(isset($_GET['database_rename']))
  1156. {
  1157. $oldpath = $_POST['oldname'];
  1158. $newpath = $_POST['newname'];
  1159. if(!file_exists($newpath))
  1160. {
  1161. copy($oldpath, $newpath);
  1162. unlink($oldpath);
  1163. $justrenamed = true;
  1164. }
  1165. else
  1166. {
  1167. $dbexists = true;
  1168. }
  1169. }
  1170. //user is creating a new Database
  1171. if(isset($_POST['new_dbname']) && $auth->isAuthorized())
  1172. {
  1173. $str = preg_replace('@[^\w-.]@','', $_POST['new_dbname']);
  1174. $dbname = $str;
  1175. $dbpath = $str;
  1176. $info = pathinfo($dbpath);
  1177. $tdata = array();
  1178. $tdata['name'] = $dbname;
  1179. $tdata['path'] = $directory."/".$dbpath;
  1180. $td = new Database($tdata);
  1181. $td->query("VACUUM");
  1182. }
  1183. //if the user wants to scan a directory for databases, do so
  1184. if($directory!==false)
  1185. {
  1186. if($directory[strlen($directory)-1]=="/") //if user has a trailing slash in the directory, remove it
  1187. $directory = substr($directory, 0, strlen($directory)-1);
  1188. if(is_dir($directory)) //make sure the directory is valid
  1189. {
  1190. if($subdirectories===true)
  1191. $arr = dir_tree($directory);
  1192. else
  1193. $arr = scandir($directory);
  1194. $databases = array();
  1195. $j = 0;
  1196. for($i=0; $i<sizeof($arr); $i++) //iterate through all the files in the databases
  1197. {
  1198. if($subdirectories===false)
  1199. $arr[$i] = $directory."/".$arr[$i];
  1200. if(!is_file($arr[$i])) continue;
  1201. $con = file_get_contents($arr[$i], NULL, NULL, 0, 60);
  1202. if(strpos($con, "** This file contains an SQLite 2.1 database **", 0)!==false || strpos($con, "SQLite format 3", 0)!==false)
  1203. {
  1204. $databases[$j]['path'] = $arr[$i];
  1205. if($subdirectories===false)
  1206. $databases[$j]['name'] = basename($arr[$i]);
  1207. else
  1208. $databases[$j]['name'] = $arr[$i];
  1209. // 22 August 2011: gkf fixed bug 49.
  1210. $perms = 0;
  1211. $perms += is_readable($databases[$j]['path']) ? 4 : 0;
  1212. $perms += is_writeable($databases[$j]['path']) ? 2 : 0;
  1213. switch($perms)
  1214. {
  1215. case 6: $perms = "[rw] "; break;
  1216. case 4: $perms = "[r ] "; break;
  1217. case 2: $perms = "[ w] "; break; // God forbid, but it might happen.
  1218. default: $perms = "[ ] "; break;
  1219. }
  1220. $databases[$j]['perms'] = $perms;
  1221. $j++;
  1222. }
  1223. }
  1224. // 22 August 2011: gkf fixed bug #50.
  1225. sort($databases);
  1226. if(isset($tdata))
  1227. {
  1228. foreach($databases as $db_id => $database)
  1229. {
  1230. if($database['path'] == $tdata)
  1231. {
  1232. $_SESSION[COOKIENAME.'currentDB'] = $database;
  1233. break;
  1234. }
  1235. }
  1236. }
  1237. if(isset($justrenamed))
  1238. {
  1239. foreach($databases as $db_id => $database)
  1240. {
  1241. if($database['path'] == $newpath)
  1242. {
  1243. $_SESSION[COOKIENAME.'currentDB'] = $database;
  1244. break;
  1245. }
  1246. }
  1247. }
  1248. }
  1249. else //the directory is not valid - display error and exit
  1250. {
  1251. echo "<div class='confirm' style='margin:20px;'>";
  1252. echo "The directory you specified to scan for databases does not exist or is not a directory.";
  1253. echo "</div>";
  1254. exit();
  1255. }
  1256. }
  1257. else
  1258. {
  1259. for($i=0; $i<sizeof($databases); $i++)
  1260. {
  1261. if(!file_exists($databases[$i]['path']))
  1262. continue; //skip if file not found ! - probably a warning can be displayed - later
  1263. $perms = 0;
  1264. $perms += is_readable($databases[$i]['path']) ? 4 : 0;
  1265. $perms += is_writeable($databases[$i]['path']) ? 2 : 0;
  1266. switch($perms)
  1267. {
  1268. case 6: $perms = "[rw] "; break;
  1269. case 4: $perms = "[r ] "; break;
  1270. case 2: $perms = "[ w] "; break; // God forbid, but it might happen.
  1271. default: $perms = "[ ] "; break;
  1272. }
  1273. $databases[$i]['perms'] = $perms;
  1274. }
  1275. sort($databases);
  1276. }
  1277. //user is downloading the exported database file
  1278. if(isset($_POST['export']))
  1279. {
  1280. if($_POST['export_type']=="sql")
  1281. {
  1282. header('Content-Type: text/sql');
  1283. header('Content-Disposition: attachment; filename="'.$_POST['filename'].'.'.$_POST['export_type'].'";');
  1284. if(isset($_POST['tables']))
  1285. $tables = $_POST['tables'];
  1286. else
  1287. {
  1288. $tables = array();
  1289. $tables[0] = $_POST['single_table'];
  1290. }
  1291. $drop = isset($_POST['drop']);
  1292. $structure = isset($_POST['structure']);
  1293. $data = isset($_POST['data']);
  1294. $transaction = isset($_POST['transaction']);
  1295. $comments = isset($_POST['comments']);
  1296. $db = new Database($_SESSION[COOKIENAME.'currentDB']);
  1297. echo $db->export_sql($tables, $drop, $structure, $data, $transaction, $comments);
  1298. }
  1299. else if($_POST['export_type']=="csv")
  1300. {
  1301. header("Content-type: application/csv");
  1302. header('Content-Disposition: attachment; filename="'.$_POST['filename'].'.'.$_POST['export_type'].'";');
  1303. header("Pragma: no-cache");
  1304. header("Expires: 0");
  1305. if(isset($_POST['tables']))
  1306. $tables = $_POST['tables'];
  1307. else
  1308. {
  1309. $tables = array();
  1310. $tables[0] = $_POST['single_table'];
  1311. }
  1312. $field_terminate = $_POST['export_csv_fieldsterminated'];
  1313. $field_enclosed = $_POST['export_csv_fieldsenclosed'];
  1314. $field_escaped = $_POST['export_csv_fieldsescaped'];
  1315. $null = $_POST['export_csv_replacenull'];
  1316. $crlf = isset($_POST['export_csv_crlf']);
  1317. $fields_in_first_row = isset($_POST['export_csv_fieldnames']);
  1318. $db = new Database($_SESSION[COOKIENAME.'currentDB']);
  1319. echo $db->export_csv($tables, $field_terminate, $field_enclosed, $field_escaped, $null, $crlf, $fields_in_first_row);
  1320. }
  1321. exit();
  1322. }
  1323. //user is importing a file
  1324. if(isset($_POST['import']))
  1325. {
  1326. $db = new Database($_SESSION[COOKIENAME.'currentDB']);
  1327. if($_POST['import_type']=="sql")
  1328. {
  1329. $data = file_get_contents($_FILES["file"]["tmp_name"]);
  1330. $importSuccess = $db->import_sql($data);
  1331. }
  1332. else
  1333. {
  1334. $field_terminate = $_POST['import_csv_fieldsterminated'];
  1335. $field_enclosed = $_POST['import_csv_fieldsenclosed'];
  1336. $field_escaped = $_POST['import_csv_fieldsescaped'];
  1337. $null = $_POST['import_csv_replacenull'];
  1338. $fields_in_first_row = isset($_POST['import_csv_fieldnames']);
  1339. $importSuccess = $db->import_csv($_FILES["file"]["tmp_name"], $_POST['single_table'], $field_terminate, $field_enclosed, $field_escaped, $null, $fields_in_first_row);
  1340. }
  1341. }
  1342. }
  1343. header('Content-Type: text/html; charset=utf-8');
  1344. // here begins the HTML.
  1345. ?>
  1346. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1347. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  1348. <head>
  1349. <!-- Copyright <?php echo date("Y"); ?> phpLiteAdmin (http://phpliteadmin.googlecode.com) -->
  1350. <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  1351. <title><?php echo PROJECT ?></title>
  1352. <?php
  1353. if(!file_exists("phpliteadmin.css")) //only use the inline stylesheet if an external one does not exist
  1354. {
  1355. ?>
  1356. <!-- begin the customizable stylesheet/theme -->
  1357. <style type="text/css">
  1358. /* overall styles for entire page */
  1359. body
  1360. {
  1361. margin: 0px;
  1362. padding: 0px;
  1363. font-family: Arial, Helvetica, sans-serif;
  1364. font-size: 14px;
  1365. color: #000000;
  1366. background-color: #e0ebf6;
  1367. }
  1368. /* general styles for hyperlink */
  1369. a
  1370. {
  1371. color: #03F;
  1372. text-decoration: none;
  1373. cursor :pointer;
  1374. }
  1375. a:hover
  1376. {
  1377. color: #06F;
  1378. }
  1379. hr
  1380. {
  1381. height: 1px;
  1382. border: 0;
  1383. color: #bbb;
  1384. background-color: #bbb;
  1385. width: 100%;
  1386. }
  1387. /* logo text containing name of project */
  1388. h1
  1389. {
  1390. margin: 0px;
  1391. padding: 5px;
  1392. font-size: 24px;
  1393. background-color: #f3cece;
  1394. text-align: center;
  1395. color: #000;
  1396. border-top-left-radius:5px;
  1397. border-top-right-radius:5px;
  1398. -moz-border-radius-topleft:5px;
  1399. -moz-border-radius-topright:5px;
  1400. }
  1401. /* the div container for the links */
  1402. #headerlinks
  1403. {
  1404. text-align:center;
  1405. margin-bottom:10px;
  1406. padding:5px;
  1407. border-color:#03F;
  1408. border-width:1px;
  1409. border-style:solid;
  1410. border-left-style:none;
  1411. border-right-style:none;
  1412. font-size:12px;
  1413. background-color:#e0ebf6;
  1414. font-weight:bold;
  1415. }
  1416. /* version text within the logo */
  1417. h1 #version
  1418. {
  1419. color: #000000;
  1420. font-size: 16px;
  1421. }
  1422. /* logo text within logo */
  1423. h1 #logo
  1424. {
  1425. color:#000;
  1426. }
  1427. /* general header for various views */
  1428. h2
  1429. {
  1430. margin:0px;
  1431. padding:0px;
  1432. font-size:14px;
  1433. margin-bottom:20px;
  1434. }
  1435. /* input buttons and areas for entering text */
  1436. input, select, textarea
  1437. {
  1438. font-family:Arial, Helvetica, sans-serif;
  1439. background-color:#eaeaea;
  1440. color:#03F;
  1441. border-color:#03F;
  1442. border-style:solid;
  1443. border-width:1px;
  1444. margin:5px;
  1445. border-radius:5px;
  1446. -moz-border-radius:5px;
  1447. padding:3px;
  1448. }
  1449. /* just input buttons */
  1450. input.btn
  1451. {
  1452. cursor:pointer;
  1453. }
  1454. input.btn:hover
  1455. {
  1456. background-color:#ccc;
  1457. }
  1458. /* general styles for hyperlink */
  1459. fieldset
  1460. {
  1461. padding:15px;
  1462. border-color:#03F;
  1463. border-width:1px;
  1464. border-style:solid;
  1465. border-radius:5px;
  1466. -moz-border-radius:5px;
  1467. background-color:#f9f9f9;
  1468. }
  1469. /* outer div that holds everything */
  1470. #container
  1471. {
  1472. padding:10px;
  1473. }
  1474. /* div of left box with log, list of databases, etc. */
  1475. #leftNav
  1476. {
  1477. float:left;
  1478. min-width:250px;
  1479. padding:0px;
  1480. border-color:#03F;
  1481. border-width:1px;
  1482. border-style:solid;
  1483. background-color:#FFF;
  1484. padding-bottom:15px;
  1485. border-radius:5px;
  1486. -moz-border-radius:5px;
  1487. }
  1488. /* div holding the content to the right of the leftNav */
  1489. #content
  1490. {
  1491. overflow:hidden;
  1492. padding-left:10px;
  1493. }
  1494. /* div holding the login fields */
  1495. #loginBox
  1496. {
  1497. width:500px;
  1498. margin-left:auto;
  1499. margin-right:auto;
  1500. margin-top:50px;
  1501. border-color:#03F;
  1502. border-width:1px;
  1503. border-style:solid;
  1504. background-color:#FFF;
  1505. border-radius:5px;
  1506. -moz-border-radius:5px;
  1507. }
  1508. /* div under tabs with tab-specific content */
  1509. #main
  1510. {
  1511. border-color:#03F;
  1512. border-width:1px;
  1513. border-style:solid;
  1514. padding:15px;
  1515. overflow:auto;
  1516. background-color:#FFF;
  1517. border-bottom-left-radius:5px;
  1518. border-bottom-right-radius:5px;
  1519. border-top-right-radius:5px;
  1520. -moz-border-radius-bottomleft:5px;
  1521. -moz-border-radius-bottomright:5px;
  1522. -moz-border-radius-topright:5px;
  1523. }
  1524. /* odd-numbered table rows */
  1525. .td1
  1526. {
  1527. background-color:#f9e3e3;
  1528. text-align:right;
  1529. font-size:12px;
  1530. padding-left:10px;
  1531. padding-right:10px;
  1532. }
  1533. /* even-numbered table rows */
  1534. .td2
  1535. {
  1536. background-color:#f3cece;
  1537. text-align:right;
  1538. font-size:12px;
  1539. padding-left:10px;
  1540. padding-right:10px;
  1541. }
  1542. /* table column headers */
  1543. .tdheader
  1544. {
  1545. border-color:#03F;
  1546. border-width:1px;
  1547. border-style:solid;
  1548. font-weight:bold;
  1549. font-size:12px;
  1550. padding-left:10px;
  1551. padding-right:10px;
  1552. background-color:#e0ebf6;
  1553. border-radius:5px;
  1554. -moz-border-radius:5px;
  1555. }
  1556. /* div holding the confirmation text of certain actions */
  1557. .confirm
  1558. {
  1559. border-color:#03F;
  1560. border-width:1px;
  1561. border-style:dashed;
  1562. padding:15px;
  1563. background-color:#e0ebf6;
  1564. }
  1565. /* tab navigation for each table */
  1566. .tab
  1567. {
  1568. display:block;
  1569. padding:5px;
  1570. padding-right:8px;
  1571. padding-left:8px;
  1572. border-color:#03F;
  1573. border-width:1px;
  1574. border-style:solid;
  1575. margin-right:5px;
  1576. float:left;
  1577. border-bottom-style:none;
  1578. position:relative;
  1579. top:1px;
  1580. padding-bottom:4px;
  1581. background-color:#eaeaea;
  1582. border-top-left-radius:5px;
  1583. border-top-right-radius:5px;
  1584. -moz-border-radius-topleft:5px;
  1585. -moz-border-radius-topright:5px;
  1586. }
  1587. /* pressed state of tab */
  1588. .tab_pressed
  1589. {
  1590. display:block;
  1591. padding:5px;
  1592. padding-right:8px;
  1593. padding-left:8px;
  1594. border-color:#03F;
  1595. border-width:1px;
  1596. border-style:solid;
  1597. margin-right:5px;
  1598. float:left;
  1599. border-bottom-style:none;
  1600. position:relative;
  1601. top:1px;
  1602. background-color:#FFF;
  1603. cursor:default;
  1604. border-top-left-radius:5px;
  1605. border-top-right-radius:5px;
  1606. -moz-border-radius-topleft:5px;
  1607. -moz-border-radius-topright:5px;
  1608. }
  1609. /* help section */
  1610. .helpq
  1611. {
  1612. font-size:11px;
  1613. font-weight:normal;
  1614. }
  1615. #help_container
  1616. {
  1617. padding:0px;
  1618. font-size:12px;
  1619. margin-left:auto;
  1620. margin-right:auto;
  1621. background-color:#ffffff;
  1622. }
  1623. .help_outer
  1624. {
  1625. background-color:#FFF;
  1626. padding:0px;
  1627. height:300px;
  1628. overflow:hidden;
  1629. position:relative;
  1630. }
  1631. .help_list
  1632. {
  1633. padding:10px;
  1634. height:auto;
  1635. }
  1636. .headd
  1637. {
  1638. font-size:14px;
  1639. font-weight:bold;
  1640. display:block;
  1641. padding:10px;
  1642. background-color:#e0ebf6;
  1643. border-color:#03F;
  1644. border-width:1px;
  1645. border-style:solid;
  1646. border-left-style:none;
  1647. border-right-style:none;
  1648. }
  1649. .help_inner
  1650. {
  1651. padding:10px;
  1652. }
  1653. .help_top
  1654. {
  1655. display:block;
  1656. position:absolute;
  1657. right:10px;
  1658. bottom:10px;
  1659. }
  1660. </style>
  1661. <!-- end the customizable stylesheet/theme -->
  1662. <?php
  1663. }
  1664. else //an external stylesheet exists - import it
  1665. {
  1666. echo "<link href='phpliteadmin.css' rel='stylesheet' type='text/css' />";
  1667. }
  1668. if(isset($_GET['help'])) //this page is used as the popup help section
  1669. {
  1670. //help section array
  1671. $help = array
  1672. (
  1673. 'SQLite Library Extensions' =>
  1674. 'phpLiteAdmin uses PHP library extensions that allow interaction with SQLite databases. Currently, phpLiteAdmin supports PDO, SQLite3, and SQLiteDatabase. Both PDO and SQLite3 deal with version 3 of SQLite, while SQLiteDatabase deals with version 2. So, if your PHP installation includes more than one SQLite library extension, PDO and SQLite3 will take precedence to make use of the better technology. However, if you have existing databases that are of version 2 of SQLite, phpLiteAdmin will be forced to use SQLiteDatabase for only those databases. Not all databases need to be of the same version. During the database creation, however, the most advanced extension will be used.',
  1675. 'Creating a New Database' =>
  1676. 'When you create a new database, the name you entered will be appended with the appropriate file extension (.db, .db3, .sqlite, etc.) if you do not include it yourself. The database will be created in the directory you specified as the $directory variable.',
  1677. 'Tables vs. Views' =>
  1678. 'On the main database page, there is a list of tables and views. Since views are read-only, certain operations will be disabled. These disabled operations will be apparent by their omission in the location where they should appear on the row for a view. If you want to change the data for a view, you need to drop that view and create a new view with the appropriate SELECT statement that queries other existing tables. For more information, see <a href="http://en.wikipedia.org/wiki/View_(database)" target="_blank">http://en.wikipedia.org/wiki/View_(database)</a>',
  1679. 'Writing a Select Statement for a New View' =>
  1680. 'When you create a new view, you must write an SQL SELECT statement that it will use as its data. A view is simply a read-only table that can be accessed and queried like a regular table, except it cannot be modified through insertion, column editing, or row editing. It is only used for conveniently fetching data.',
  1681. 'Export Structure to SQL File' =>
  1682. 'During the process for exporting to an SQL file, you may choose to include the queries that create the table and columns.',
  1683. 'Export Data to SQL File' =>
  1684. 'During the process for exporting to an SQL file, you may choose to include the queries that populate the table(s) with the current records of the table(s).',
  1685. 'Add Drop Table to Exported SQL File' =>
  1686. 'During the process for exporting to an SQL file, you may choose to include queries to DROP the existing tables before adding them so that problems do not occur when trying to create tables that already exist.',
  1687. 'Add Transaction to Exported SQL File' =>
  1688. 'During the process for exporting to an SQL file, you may choose to wrap the queries around a TRANSACTION so that if an error occurs at any time during the importation process using the exported file, the database can be reverted to its previous state, preventing partially updated data from populating the database.',
  1689. 'Add Comments to Exported SQL File' =>
  1690. 'During the process for exporting to an SQL file, you may choose to include comments that explain each step of the process so that a human can better understand what is happening.',
  1691. );
  1692. ?>
  1693. </head>
  1694. <body>
  1695. <div id='help_container'>
  1696. <?php
  1697. echo "<div class='help_list'>";
  1698. echo "<span style='font-size:18px;'>".PROJECT." v".VERSION." Help Documentation</span><br/><br/>";
  1699. foreach((array)$help as $key => $val)
  1700. {
  1701. echo "<a href='#".$key."'>".$key."</a><br/>";
  1702. }
  1703. echo "</div>";
  1704. echo "<br/><br/>";
  1705. foreach((array)$help as $key => $val)
  1706. {
  1707. echo "<div class='help_outer'>";
  1708. echo "<a class='headd' name='".$key."'>".$key."</a>";
  1709. echo "<div class='help_inner'>";
  1710. echo $val;
  1711. echo "</div>";
  1712. echo "<a class='help_top' href='#top'>Back to Top</a>";
  1713. echo "</div>";
  1714. }
  1715. ?>
  1716. </div>
  1717. </body>
  1718. </html>
  1719. <?php
  1720. exit();
  1721. }
  1722. ?>
  1723. <!-- JavaScript Support -->
  1724. <script type="text/javascript">
  1725. /* <![CDATA[ */
  1726. //initiated autoincrement checkboxes
  1727. function initAutoincrement()
  1728. {
  1729. var i=0;
  1730. while(document.getElementById('i'+i+'_autoincrement')!=undefined)
  1731. {
  1732. document.getElementById('i'+i+'_autoincrement').disabled = true;
  1733. i++;
  1734. }
  1735. }
  1736. //makes sure autoincrement can only be selected when integer type is selected
  1737. function toggleAutoincrement(i)
  1738. {
  1739. var type = document.getElementById('i'+i+'_type');
  1740. var primarykey = document.getElementById('i'+i+'_primarykey');
  1741. var autoincrement = document.getElementById('i'+i+'_autoincrement');
  1742. if(type.value=='INTEGER' && primarykey.checked)
  1743. autoincrement.disabled = false;
  1744. else
  1745. {
  1746. autoincrement.disabled = true;
  1747. autoincrement.checked = false;
  1748. }
  1749. }
  1750. function toggleNull(i)
  1751. {
  1752. var pk = document.getElementById('i'+i+'_primarykey');
  1753. var notnull = document.getElementById('i'+i+'_notnull');
  1754. if(pk.checked)
  1755. {
  1756. notnull.disabled = true;
  1757. notnull.checked = true;
  1758. }
  1759. else
  1760. {
  1761. notnull.disabled = false;
  1762. }
  1763. }
  1764. //finds and checks all checkboxes for all rows on the Browse or Structure tab for a table
  1765. function checkAll(field)
  1766. {
  1767. var i=0;
  1768. while(document.getElementById('check_'+i)!=undefined)
  1769. {
  1770. document.getElementById('check_'+i).checked = true;
  1771. i++;
  1772. }
  1773. }
  1774. //finds and unchecks all checkboxes for all rows on the Browse or Structure tab for a table
  1775. function uncheckAll(field)
  1776. {
  1777. var i=0;
  1778. while(document.getElementById('check_'+i)!=undefined)
  1779. {
  1780. document.getElementById('check_'+i).checked = false;
  1781. i++;
  1782. }
  1783. }
  1784. //unchecks the ignore checkbox if user has typed something into one of the fields for adding new rows
  1785. function changeIgnore(area, e, u)
  1786. {
  1787. if(area.value!="")
  1788. {
  1789. if(document.getElementById(e)!=undefined)
  1790. document.getElementById(e).checked = false;
  1791. if(document.getElementById(u)!=undefined)
  1792. document.getElementById(u).checked = false;
  1793. }
  1794. }
  1795. //moves fields from select menu into query textarea for SQL tab
  1796. function moveFields()
  1797. {
  1798. var fields = document.getElementById("fieldcontainer");
  1799. var selected = new Array();
  1800. for(var i=0; i<fields.options.length; i++)
  1801. if(fields.options[i].selected)
  1802. selected.push(fields.options[i].value);
  1803. for(var i=0; i<selected.length; i++)
  1804. insertAtCaret("queryval", '"'+selected[i].replace(/"/g,'""')+'"');
  1805. }
  1806. //helper function for moveFields
  1807. function insertAtCaret(areaId,text)
  1808. {
  1809. var txtarea = document.getElementById(areaId);
  1810. var scrollPos = txtarea.scrollTop;
  1811. var strPos = 0;
  1812. var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ));
  1813. if(br=="ie")
  1814. {
  1815. txtarea.focus();
  1816. var range = document.selection.createRange();
  1817. range.moveStart ('character', -txtarea.value.length);
  1818. strPos = range.text.length;
  1819. }
  1820. else if(br=="ff")
  1821. strPos = txtarea.selectionStart;
  1822. var front = (txtarea.value).substring(0,strPos);
  1823. var back = (txtarea.value).substring(strPos,txtarea.value.length);
  1824. txtarea.value=front+text+back;
  1825. strPos = strPos + text.length;
  1826. if(br=="ie")
  1827. {
  1828. txtarea.focus();
  1829. var range = document.selection.createRange();
  1830. range.moveStart ('character', -txtarea.value.length);
  1831. range.moveStart ('character', strPos);
  1832. range.moveEnd ('character', 0);
  1833. range.select();
  1834. }
  1835. else if(br=="ff")
  1836. {
  1837. txtarea.selectionStart = strPos;
  1838. txtarea.selectionEnd = strPos;
  1839. txtarea.focus();
  1840. }
  1841. txtarea.scrollTop = scrollPos;
  1842. }
  1843. function notNull(checker)
  1844. {
  1845. document.getElementById(checker).checked = false;
  1846. }
  1847. function disableText(checker, textie)
  1848. {
  1849. if(checker.checked)
  1850. {
  1851. document.getElementById(textie).value = "";
  1852. document.getElementById(textie).disabled = true;
  1853. }
  1854. else
  1855. {
  1856. document.getElementById(textie).disabled = false;
  1857. }
  1858. }
  1859. function toggleExports(val)
  1860. {
  1861. document.getElementById("exportoptions_sql").style.display = "none";
  1862. document.getElementById("exportoptions_csv").style.display = "none";
  1863. document.getElementById("exportoptions_"+val).style.display = "block";
  1864. }
  1865. function toggleImports(val)
  1866. {
  1867. document.getElementById("importoptions_sql").style.display = "none";
  1868. document.getElementById("importoptions_csv").style.display = "none";
  1869. document.getElementById("importoptions_"+val).style.display = "block";
  1870. }
  1871. function openHelp(section)
  1872. {
  1873. PopupCenter('<?php echo PAGE."?help=1"; ?>#'+section, "Help Section");
  1874. }
  1875. var helpsec = false;
  1876. function PopupCenter(pageURL, title)
  1877. {
  1878. helpsec = window.open(pageURL, title, "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=300");
  1879. }
  1880. /* ]]> */
  1881. </script>
  1882. </head>
  1883. <body>
  1884. <?php
  1885. if(ini_get("register_globals") == "on" || ini_get("register_globals")=="1") //check whether register_globals is turned on - if it is, we need to not continue
  1886. {
  1887. echo "<div class='confirm' style='margin:20px;'>";
  1888. echo "It appears that the PHP directive, 'register_globals' is enabled. This is bad. You need to disable it before continuing.";
  1889. echo "</div>";
  1890. echo "</body></html>";
  1891. exit();
  1892. }
  1893. if(!$auth->isAuthorized()) //user is not authorized - display the login screen
  1894. {
  1895. echo "<div id='loginBox'>";
  1896. echo "<h1><span id='logo'>".PROJECT."</span> <span id='version'>v".VERSION."</span></h1>";
  1897. echo "<div style='padding:15px; text-align:center;'>";
  1898. if(isset($_POST['login']))
  1899. echo "<span style='color:red;'>Incorrect password.</span><br/><br/>";
  1900. echo "<form action='".PAGE."' method='post'>";
  1901. echo "Password: <input type='password' name='password'/><br/>";
  1902. echo "<input type='checkbox' name='remember' value='yes' checked='checked'/> Remember me<br/><br/>";
  1903. echo "<input type='submit' value='Log In' name='login' class='btn'/>";
  1904. echo "<input type='hidden' name='proc_login' value='true' />";
  1905. echo "</form>";
  1906. echo "</div>";
  1907. echo "</div>";
  1908. echo "<br/>";
  1909. echo "<div style='text-align:center;'>";
  1910. $endTimeTot = microtime(true);
  1911. $timeTot = round(($endTimeTot - $startTimeTot), 4);
  1912. echo "<span style='font-size:11px;'>Powered by <a href='http://phpliteadmin.googlecode.com' target='_blank' style='font-size:11px;'>".PROJECT."</a> | Page generated in ".$timeTot." seconds.</span>";
  1913. echo "</div>";
  1914. }
  1915. else //user is authorized - display the main application
  1916. {
  1917. if(!isset($_SESSION[COOKIENAME.'currentDB']))
  1918. $_SESSION[COOKIENAME.'currentDB'] = $databases[0];
  1919. //set the current database to the first in the array (default)
  1920. if(sizeof($databases)>0)
  1921. $currentDB = $databases[0];
  1922. else //the database array is empty - show error and halt execution
  1923. {
  1924. if($directory!==false && is_writable($directory))
  1925. {
  1926. echo "<div class='confirm' style='margin:20px;'>";
  1927. echo "Welcome to phpLiteAdmin. It appears that you have selected to scan a directory for databases to manage. However, phpLiteAdmin could not find any valid SQLite databases. You may use the form below to create your first database.";
  1928. echo "</div>";
  1929. echo "<fieldset style='margin:15px;'><legend><b>Create New Database</b></legend>";
  1930. echo "<form name='create_database' method='post' action='".PAGE."'>";
  1931. echo "<input type='text' name='new_dbname' style='width:150px;'/> <input type='submit' value='Create' class='btn'/>";
  1932. echo "</form>";
  1933. echo "</fieldset>";
  1934. }
  1935. else
  1936. {
  1937. echo "<div class='confirm' style='margin:20px;'>";
  1938. echo "Error: The directory you specified does not contain any existing databases to manage, and the directory is not writable. This means you can't create any new databases using phpLiteAdmin. Either make the directory writable or manually upload databases to the directory.";
  1939. echo "</div><br/>";
  1940. }
  1941. exit();
  1942. }
  1943. if(isset($_POST['database_switch'])) //user is switching database with drop-down menu
  1944. {
  1945. foreach($databases as $db_id => $database)
  1946. {
  1947. if($database['path'] == $_POST['database_switch'])
  1948. {
  1949. $_SESSION[COOKIENAME."currentDB"] = $database;
  1950. break;
  1951. }
  1952. }
  1953. $currentDB = $_SESSION[COOKIENAME.'currentDB'];
  1954. }
  1955. else if(isset($_GET['switchdb']))
  1956. {
  1957. foreach($databases as $db_id => $database)
  1958. {
  1959. if($database['path'] == $_GET['switchdb'])
  1960. {
  1961. $_SESSION[COOKIENAME."currentDB"] = $database;
  1962. break;
  1963. }
  1964. }
  1965. $currentDB = $_SESSION[COOKIENAME.'currentDB'];
  1966. }
  1967. if(isset($_SESSION[COOKIENAME.'currentDB']))
  1968. $currentDB = $_SESSION[COOKIENAME.'currentDB'];
  1969. //create the objects
  1970. $db = new Database($currentDB); //create the Database object
  1971. //switch board for various operations a user could have requested - these actions are invisible and produce no output
  1972. if(isset($_GET['action']) && isset($_GET['confirm']))
  1973. {
  1974. switch($_GET['action'])
  1975. {
  1976. //table actions
  1977. /////////////////////////////////////////////// create table
  1978. case "table_create":
  1979. $num = intval($_POST['rows']);
  1980. $name = $_POST['tablename'];
  1981. $primary_keys = array();
  1982. for($i=0; $i<$num; $i++)
  1983. {
  1984. if($_POST[$i.'_field']!="" && isset($_POST[$i.'_primarykey']))
  1985. {
  1986. $primary_keys[] = $_POST[$i.'_field'];
  1987. }
  1988. }
  1989. $query = "CREATE TABLE ".$db->quote($name)." (";
  1990. for($i=0; $i<$num; $i++)
  1991. {
  1992. if($_POST[$i.'_field']!="")
  1993. {
  1994. $query .= $db->quote($_POST[$i.'_field'])." ";
  1995. $query .= $_POST[$i.'_type']." ";
  1996. if(isset($_POST[$i.'_primarykey']))
  1997. {
  1998. if(count($primary_keys)==1)
  1999. {
  2000. $query .= "PRIMARY KEY ";
  2001. if(isset($_POST[$i.'_autoincrement']))
  2002. $query .= "AUTOINCREMENT ";
  2003. }
  2004. $query .= "NOT NULL ";
  2005. }
  2006. if(!isset($_POST[$i.'_primarykey']) && isset($_POST[$i.'_notnull']))
  2007. $query .= "NOT NULL ";
  2008. if($_POST[$i.'_defaultvalue']!="")
  2009. {
  2010. if($_POST[$i.'_type']=="INTEGER" && is_numeric($_POST[$i.'_defaultvalue']))
  2011. $query .= "default ".$_POST[$i.'_defaultvalue']." ";
  2012. else
  2013. $query .= "default ".$db->quote($_POST[$i.'_defaultvalue'])." ";
  2014. }
  2015. $query = substr($query, 0, sizeof($query)-2);
  2016. $query .= ", ";
  2017. }
  2018. }
  2019. if (count($primary_keys)>1)
  2020. {
  2021. $compound_key = "";
  2022. foreach ($primary_keys as $primary_key)
  2023. {
  2024. $compound_key .= ($compound_key=="" ? "" : ", ") . $db->quote($primary_key);
  2025. }
  2026. $query .= "PRIMARY KEY (".$compound_key."), ";
  2027. }
  2028. $query = substr($query, 0, sizeof($query)-3);
  2029. $query .= ")";
  2030. $result = $db->query($query);
  2031. if(!$result)
  2032. $error = true;
  2033. $completed = "Table '".htmlencode($_POST['tablename'])."' has been created.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2034. break;
  2035. /////////////////////////////////////////////// empty table
  2036. case "table_empty":
  2037. $query = "DELETE FROM ".$db->quote_id($_POST['tablename']);
  2038. $result = $db->query($query);
  2039. if(!$result)
  2040. $error = true;
  2041. $query = "VACUUM";
  2042. $result = $db->query($query);
  2043. if(!$result)
  2044. $error = true;
  2045. $completed = "Table '".htmlencode($_POST['tablename'])."' has been emptied.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2046. break;
  2047. /////////////////////////////////////////////// create view
  2048. case "view_create":
  2049. $query = "CREATE VIEW ".$db->quote($_POST['viewname'])." AS ".stripslashes($_POST['select']);
  2050. $result = $db->query($query);
  2051. if(!$result)
  2052. $error = true;
  2053. $completed = "View '".htmlencode($_POST['viewname'])."' has been created.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2054. break;
  2055. /////////////////////////////////////////////// drop table
  2056. case "table_drop":
  2057. $query = "DROP TABLE ".$db->quote_id($_POST['tablename']);
  2058. $db->query($query);
  2059. $completed = "Table '".htmlencode($_POST['tablename'])."' has been dropped.";
  2060. break;
  2061. /////////////////////////////////////////////// drop view
  2062. case "view_drop":
  2063. $query = "DROP VIEW ".$db->quote_id($_POST['viewname']);
  2064. $db->query($query);
  2065. $completed = "View '".htmlencode($_POST['viewname'])."' has been dropped.";
  2066. break;
  2067. /////////////////////////////////////////////// rename table
  2068. case "table_rename":
  2069. $query = "ALTER TABLE ".$db->quote_id($_POST['oldname'])." RENAME TO ".$db->quote($_POST['newname']);
  2070. if($db->getVersion()==3)
  2071. $result = $db->query($query, true);
  2072. else
  2073. $result = $db->query($query, false);
  2074. if(!$result)
  2075. $error = true;
  2076. $completed = "Table '".htmlencode($_POST['oldname'])."' has been renamed to '".htmlencode($_POST['newname'])."'.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2077. break;
  2078. //row actions
  2079. /////////////////////////////////////////////// create row
  2080. case "row_create":
  2081. $completed = "";
  2082. $num = $_POST['numRows'];
  2083. $fields = explode(":", $_POST['fields']);
  2084. $z = 0;
  2085. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  2086. $result = $db->selectArray($query);
  2087. for($i=0; $i<$num; $i++)
  2088. {
  2089. if(!isset($_POST[$i.":ignore"]))
  2090. {
  2091. $query = "INSERT INTO ".$db->quote_id($_GET['table'])." (";
  2092. for($j=0; $j<sizeof($fields); $j++)
  2093. {
  2094. $query .= "".$db->quote_id($fields[$j]).",";
  2095. }
  2096. $query = substr($query, 0, sizeof($query)-2);
  2097. $query .= ") VALUES (";
  2098. for($j=0; $j<sizeof($fields); $j++)
  2099. {
  2100. // PHP replaces space with underscore
  2101. $fields[$j] = str_replace(" ","_",$fields[$j]);
  2102. $null = isset($_POST[$i.":".$fields[$j]."_null"]);
  2103. if(!$null)
  2104. {
  2105. if(!isset($_POST[$i.":".$fields[$j]]) && $debug)
  2106. {
  2107. echo "MISSING POST INDEX (".$i.":".$fields[$j].")<br><pre />";
  2108. var_dump($_POST);
  2109. echo "</pre><hr />";
  2110. }
  2111. $value = $_POST[$i.":".$fields[$j]];
  2112. }
  2113. else
  2114. $value = "";
  2115. $type = $result[$j][2];
  2116. $function = $_POST["function_".$i."_".$fields[$j]];
  2117. if($function!="")
  2118. $query .= $function."(";
  2119. //di - messed around with this logic for null values
  2120. if(($type=="TEXT" || $type=="BLOB") && $null==false)
  2121. $query .= $db->quote($value);
  2122. else if(($type=="INTEGER" || $type=="REAL") && $null==false && $value=="")
  2123. $query .= "NULL";
  2124. else if($null==true)
  2125. $query .= "NULL";
  2126. else
  2127. $query .= $db->quote($value);
  2128. if($function!="")
  2129. $query .= ")";
  2130. $query .= ",";
  2131. }
  2132. $query = substr($query, 0, sizeof($query)-2);
  2133. $query .= ")";
  2134. $result1 = $db->query($query);
  2135. if(!$result1)
  2136. $error = true;
  2137. $completed .= "<span style='font-size:11px;'>".htmlencode($query)."</span><br/>";
  2138. $z++;
  2139. }
  2140. }
  2141. $completed = $z." row(s) inserted.<br/><br/>".$completed;
  2142. break;
  2143. /////////////////////////////////////////////// delete row
  2144. case "row_delete":
  2145. $pks = explode(":", $_GET['pk']);
  2146. $query = "DELETE FROM ".$db->quote_id($_GET['table'])." WHERE ROWID = ".$pks[0];
  2147. for($i=1; $i<sizeof($pks); $i++)
  2148. {
  2149. $query .= " OR ROWID = ".$pks[$i];
  2150. }
  2151. $result = $db->query($query);
  2152. if(!$result)
  2153. $error = true;
  2154. $completed = sizeof($pks)." row(s) deleted.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2155. break;
  2156. /////////////////////////////////////////////// edit row
  2157. case "row_edit":
  2158. $pks = explode(":", $_GET['pk']);
  2159. $fields = explode(":", $_POST['fieldArray']);
  2160. $z = 0;
  2161. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  2162. $result = $db->selectArray($query);
  2163. if(isset($_POST['new_row']))
  2164. $completed = "";
  2165. else
  2166. $completed = sizeof($pks)." row(s) affected.<br/><br/>";
  2167. for($i=0; $i<sizeof($pks); $i++)
  2168. {
  2169. if(isset($_POST['new_row']))
  2170. {
  2171. $query = "INSERT INTO ".$db->quote_id($_GET['table'])." (";
  2172. for($j=0; $j<sizeof($fields); $j++)
  2173. {
  2174. $query .= $db->quote_id($fields[$j]).",";
  2175. }
  2176. $query = substr($query, 0, sizeof($query)-2);
  2177. $query .= ") VALUES (";
  2178. for($j=0; $j<sizeof($fields); $j++)
  2179. {
  2180. $field_index = str_replace(" ","_",$fields[$j]);
  2181. $value = $_POST[$pks[$i].":".$field_index];
  2182. $null = isset($_POST[$pks[$i].":".$field_index."_null"]);
  2183. $type = $result[$j][2];
  2184. $function = $_POST["function_".$pks[$i]."_".$field_index];
  2185. if($function!="")
  2186. $query .= $function."(";
  2187. //di - messed around with this logic for null values
  2188. if(($type=="TEXT" || $type=="BLOB") && $null==false)
  2189. $query .= $db->quote($value);
  2190. else if(($type=="INTEGER" || $type=="REAL") && $null==false && $value=="")
  2191. $query .= "NULL";
  2192. else if($null==true)
  2193. $query .= "NULL";
  2194. else
  2195. $query .= $db->quote($value);
  2196. if($function!="")
  2197. $query .= ")";
  2198. $query .= ",";
  2199. }
  2200. $query = substr($query, 0, sizeof($query)-2);
  2201. $query .= ")";
  2202. $result1 = $db->query($query);
  2203. if(!$result1)
  2204. $error = true;
  2205. $z++;
  2206. }
  2207. else
  2208. {
  2209. $query = "UPDATE ".$db->quote_id($_GET['table'])." SET ";
  2210. for($j=0; $j<sizeof($fields); $j++)
  2211. {
  2212. if(!is_numeric($pks[$i])) continue;
  2213. $field_index = str_replace(" ","_",$fields[$j]);
  2214. $function = $_POST["function_".$pks[$i]."_".$field_index];
  2215. $null = isset($_POST[$pks[$i].":".$field_index."_null"]);
  2216. $query .= $db->quote_id($fields[$j])."=";
  2217. if($function!="")
  2218. $query .= $function."(";
  2219. if($null)
  2220. $query .= "NULL";
  2221. else
  2222. $query .= $db->quote($_POST[$pks[$i].":".$field_index]);
  2223. if($function!="")
  2224. $query .= ")";
  2225. $query .= ", ";
  2226. }
  2227. $query = substr($query, 0, sizeof($query)-3);
  2228. $query .= " WHERE ROWID = ".$pks[$i];
  2229. $result1 = $db->query($query);
  2230. if(!$result1)
  2231. {
  2232. $error = true;
  2233. }
  2234. }
  2235. $completed .= "<span style='font-size:11px;'>".htmlencode($query)."</span><br/>";
  2236. }
  2237. if(isset($_POST['new_row']))
  2238. $completed = $z." row(s) inserted.<br/><br/>".$completed;
  2239. break;
  2240. //column actions
  2241. /////////////////////////////////////////////// create column
  2242. case "column_create":
  2243. $num = intval($_POST['rows']);
  2244. for($i=0; $i<$num; $i++)
  2245. {
  2246. if($_POST[$i.'_field']!="")
  2247. {
  2248. $query = "ALTER TABLE ".$db->quote_id($_GET['table'])." ADD ".$db->quote($_POST[$i.'_field'])." ";
  2249. $query .= $_POST[$i.'_type']." ";
  2250. if(isset($_POST[$i.'_primarykey']))
  2251. $query .= "PRIMARY KEY ";
  2252. if(isset($_POST[$i.'_notnull']))
  2253. $query .= "NOT NULL ";
  2254. if($_POST[$i.'_defaultvalue']!="")
  2255. {
  2256. if($_POST[$i.'_type']=="INTEGER" && is_numeric($_POST[$i.'_defaultvalue']))
  2257. $query .= "DEFAULT ".$_POST[$i.'_defaultvalue']." ";
  2258. else
  2259. $query .= "DEFAULT ".$db->quote($_POST[$i.'_defaultvalue'])." ";
  2260. }
  2261. if($db->getVersion()==3)
  2262. $result = $db->query($query, true);
  2263. else
  2264. $result = $db->query($query, false);
  2265. if(!$result)
  2266. $error = true;
  2267. }
  2268. }
  2269. $completed = "Table '".htmlencode($_GET['table'])."' has been altered successfully.";
  2270. break;
  2271. /////////////////////////////////////////////// delete column
  2272. case "column_delete":
  2273. $pks = explode(":", $_GET['pk']);
  2274. $query = "ALTER TABLE ".$db->quote_id($_GET['table']).' DROP '.$db->quote_id($pks[0]);
  2275. for($i=1; $i<sizeof($pks); $i++)
  2276. {
  2277. $query .= ", DROP ".$db->quote_id($pks[$i]);
  2278. }
  2279. $result = $db->query($query);
  2280. if(!$result)
  2281. $error = true;
  2282. $completed = "Table '".htmlencode($_GET['table'])."' has been altered successfully.";
  2283. break;
  2284. /////////////////////////////////////////////// edit column
  2285. case "column_edit":
  2286. $query = "ALTER TABLE ".$db->quote_id($_GET['table']).' CHANGE '.$db->quote_id($_POST['oldvalue'])." ".$db->quote($_POST['0_field'])." ".$_POST['0_type'];
  2287. $result = $db->query($query);
  2288. if(!$result)
  2289. $error = true;
  2290. $completed = "Table '".htmlencode($_GET['table'])."' has been altered successfully.";
  2291. break;
  2292. /////////////////////////////////////////////// delete trigger
  2293. case "trigger_delete":
  2294. $query = "DROP TRIGGER ".$db->quote_id($_GET['pk']);
  2295. $result = $db->query($query);
  2296. if(!$result)
  2297. $error = true;
  2298. $completed = "Trigger '".htmlencode($_GET['pk'])."' deleted.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2299. break;
  2300. /////////////////////////////////////////////// delete index
  2301. case "index_delete":
  2302. $query = "DROP INDEX ".$db->quote_id($_GET['pk']);
  2303. $result = $db->query($query);
  2304. if(!$result)
  2305. $error = true;
  2306. $completed = "Index '".htmlencode($_GET['pk'])."' deleted.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2307. break;
  2308. /////////////////////////////////////////////// create trigger
  2309. case "trigger_create":
  2310. $str = "CREATE TRIGGER ".$db->quote($_POST['trigger_name']);
  2311. if($_POST['beforeafter']!="")
  2312. $str .= " ".$_POST['beforeafter'];
  2313. $str .= " ".$_POST['event']." ON ".$db->quote_id($_GET['table']);
  2314. if(isset($_POST['foreachrow']))
  2315. $str .= " FOR EACH ROW";
  2316. if($_POST['whenexpression']!="")
  2317. $str .= " WHEN ".stripslashes($_POST['whenexpression']);
  2318. $str .= " BEGIN";
  2319. $str .= " ".stripslashes($_POST['triggersteps']);
  2320. $str .= " END";
  2321. $query = $str;
  2322. $result = $db->query($query);
  2323. if(!$result)
  2324. $error = true;
  2325. $completed = "Trigger created.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2326. break;
  2327. /////////////////////////////////////////////// create index
  2328. case "index_create":
  2329. $num = $_POST['num'];
  2330. if($_POST['name']=="")
  2331. {
  2332. $completed = "Index name must not be blank.";
  2333. }
  2334. else if($_POST['0_field']=="")
  2335. {
  2336. $completed = "You must specify at least one index column.";
  2337. }
  2338. else
  2339. {
  2340. $str = "CREATE ";
  2341. if($_POST['duplicate']=="no")
  2342. $str .= "UNIQUE ";
  2343. $str .= "INDEX ".$db->quote($_POST['name'])." ON ".$db->quote_id($_GET['table'])." (";
  2344. $str .= $db->quote_id($_POST['0_field']).$_POST['0_order'];
  2345. for($i=1; $i<$num; $i++)
  2346. {
  2347. if($_POST[$i.'_field']!="")
  2348. $str .= ", ".$_POST[$i.'_field'].$_POST[$i.'_order'];
  2349. }
  2350. $str .= ")";
  2351. $query = $str;
  2352. $result = $db->query($query);
  2353. if(!$result)
  2354. $error = true;
  2355. $completed = "Index created.<br/><span style='font-size:11px;'>".htmlencode($query)."</span>";
  2356. }
  2357. break;
  2358. }
  2359. }
  2360. echo "<div id='container'>";
  2361. echo "<div id='leftNav'>";
  2362. echo "<h1>";
  2363. echo "<a href='".PAGE."'>";
  2364. echo "<span id='logo'>".PROJECT."</span> <span id='version'>v".VERSION."</span>";
  2365. echo "</a>";
  2366. echo "</h1>";
  2367. echo "<div id='headerlinks'>";
  2368. echo "<a href='javascript:void' onclick='openHelp(\"top\");'>Documentation</a> | ";
  2369. echo "<a href='http://www.gnu.org/licenses/gpl.html' target='_blank'>License</a> | ";
  2370. echo "<a href='http://code.google.com/p/phpliteadmin/' target='_blank'>Project Site</a>";
  2371. echo "</div>";
  2372. echo "<fieldset style='margin:15px;'><legend><b>Change Database</b></legend>";
  2373. if(sizeof($databases)<10) //if there aren't a lot of databases, just show them as a list of links instead of drop down menu
  2374. {
  2375. for($i=0; $i<sizeof($databases); $i++)
  2376. {
  2377. // 22 August 2011: gkf fixed bug #49
  2378. echo $databases[$i]['perms'];
  2379. if($databases[$i] == $_SESSION[COOKIENAME.'currentDB'])
  2380. echo "<a href='".PAGE."?switchdb=".urlencode($databases[$i]['path'])."' style='text-decoration:underline;'>".htmlencode($databases[$i]['name'])."</a>";
  2381. else
  2382. echo "<a href='".PAGE."?switchdb=".urlencode($databases[$i]['path'])."'>".htmlencode($databases[$i]['name'])."</a>";
  2383. if($i<sizeof($databases)-1)
  2384. echo "<br/>";
  2385. }
  2386. }
  2387. else //there are a lot of databases - show a drop down menu
  2388. {
  2389. echo "<form action='".PAGE."' method='post'>";
  2390. echo "<select name='database_switch'>";
  2391. // 22 August 2011: gkf fixed bug #49
  2392. for($i=0; $i<sizeof($databases); $i++)
  2393. {
  2394. if($databases[$i] == $_SESSION[COOKIENAME.'currentDB'])
  2395. echo "<option value='".htmlencode($databases[$i]['path'])."' selected='selected'>".htmlencode($databases[$i]['perms'].$databases[$i]['name'])."</option>";
  2396. else
  2397. echo "<option value='".htmlencode($databases[$i]['path'])."'>".htmlencode($databases[$i]['perms'].$databases[$i]['name'])."</option>";
  2398. }
  2399. echo "</select> ";
  2400. echo "<input type='submit' value='Go' class='btn'>";
  2401. echo "</form>";
  2402. }
  2403. echo "</fieldset>";
  2404. echo "<fieldset style='margin:15px;'><legend>";
  2405. echo "<a href='".PAGE."'";
  2406. if(!isset($_GET['table']))
  2407. echo " style='text-decoration:underline;'";
  2408. echo ">".$currentDB['name']."</a>";
  2409. echo "</legend>";
  2410. //Display list of tables
  2411. $query = "SELECT type, name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name";
  2412. $result = $db->selectArray($query);
  2413. $j=0;
  2414. for($i=0; $i<sizeof($result); $i++)
  2415. {
  2416. if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  2417. {
  2418. if($result[$i]['type']=="table")
  2419. echo "<span style='font-size:11px;'>[table]</span> <a href='".PAGE."?action=row_view&amp;table=".urlencode($result[$i]['name'])."'";
  2420. else
  2421. echo "<span style='font-size:11px;'>[view]</span> <a href='".PAGE."?action=row_view&amp;table=".urlencode($result[$i]['name'])."&amp;view=1'";
  2422. if(isset($_GET['table']) && $_GET['table']==$result[$i]['name'])
  2423. echo " style='text-decoration:underline;'";
  2424. echo ">".htmlencode($result[$i]['name'])."</a><br/>";
  2425. $j++;
  2426. }
  2427. }
  2428. if($j==0)
  2429. echo "No tables in database.";
  2430. echo "</fieldset>";
  2431. if($directory!==false && is_writable($directory))
  2432. {
  2433. echo "<fieldset style='margin:15px;'><legend><b>Create New Database</b> ".helpLink("Creating a New Database")."</legend>";
  2434. echo "<form name='create_database' method='post' action='".PAGE."'>";
  2435. echo "<input type='text' name='new_dbname' style='width:150px;'/> <input type='submit' value='Create' class='btn'/>";
  2436. echo "</form>";
  2437. echo "</fieldset>";
  2438. }
  2439. echo "<div style='text-align:center;'>";
  2440. echo "<form action='".PAGE."' method='post'>";
  2441. echo "<input type='submit' value='Log Out' name='logout' class='btn'/>";
  2442. echo "</form>";
  2443. echo "</div>";
  2444. echo "</div>";
  2445. echo "<div id='content'>";
  2446. //breadcrumb navigation
  2447. echo "<a href='".PAGE."'>".$currentDB['name']."</a>";
  2448. if(isset($_GET['table']))
  2449. echo " &rarr; <a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_view'>".htmlencode($_GET['table'])."</a>";
  2450. echo "<br/><br/>";
  2451. //user has performed some action so show the resulting message
  2452. if(isset($_GET['confirm']))
  2453. {
  2454. echo "<div id='main'>";
  2455. echo "<div class='confirm'>";
  2456. if(isset($error) && $error) //an error occured during the action, so show an error message
  2457. echo "Error: ".$db->getError().".<br/>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>";
  2458. else //action was performed successfully - show success message
  2459. echo $completed;
  2460. echo "</div>";
  2461. if($_GET['action']=="row_delete" || $_GET['action']=="row_create" || $_GET['action']=="row_edit")
  2462. echo "<br/><br/><a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_view'>Return</a>";
  2463. else if($_GET['action']=="column_create" || $_GET['action']=="column_delete" || $_GET['action']=="column_edit" || $_GET['action']=="index_create" || $_GET['action']=="index_delete" || $_GET['action']=="trigger_delete" || $_GET['action']=="trigger_create")
  2464. echo "<br/><br/><a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_view'>Return</a>";
  2465. else
  2466. echo "<br/><br/><a href='".PAGE."'>Return</a>";
  2467. echo "</div>";
  2468. }
  2469. //show the various tab views for a table
  2470. 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" || $_GET['action']=="table_triggers"))
  2471. {
  2472. if(!isset($_GET['view']))
  2473. {
  2474. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_view' ";
  2475. if($_GET['action']=="row_view")
  2476. echo "class='tab_pressed'";
  2477. else
  2478. echo "class='tab'";
  2479. echo ">Browse</a>";
  2480. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_view' ";
  2481. if($_GET['action']=="column_view")
  2482. echo "class='tab_pressed'";
  2483. else
  2484. echo "class='tab'";
  2485. echo ">Structure</a>";
  2486. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_sql' ";
  2487. if($_GET['action']=="table_sql")
  2488. echo "class='tab_pressed'";
  2489. else
  2490. echo "class='tab'";
  2491. echo ">SQL</a>";
  2492. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_search' ";
  2493. if($_GET['action']=="table_search")
  2494. echo "class='tab_pressed'";
  2495. else
  2496. echo "class='tab'";
  2497. echo ">Search</a>";
  2498. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_create' ";
  2499. if($_GET['action']=="row_create")
  2500. echo "class='tab_pressed'";
  2501. else
  2502. echo "class='tab'";
  2503. echo ">Insert</a>";
  2504. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_export' ";
  2505. if($_GET['action']=="table_export")
  2506. echo "class='tab_pressed'";
  2507. else
  2508. echo "class='tab'";
  2509. echo ">Export</a>";
  2510. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_import' ";
  2511. if($_GET['action']=="table_import")
  2512. echo "class='tab_pressed'";
  2513. else
  2514. echo "class='tab'";
  2515. echo ">Import</a>";
  2516. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_rename' ";
  2517. if($_GET['action']=="table_rename")
  2518. echo "class='tab_pressed'";
  2519. else
  2520. echo "class='tab'";
  2521. echo ">Rename</a>";
  2522. echo "<a href='".PAGE."?action=table_empty&amp;table=".urlencode($_GET['table'])."' ";
  2523. echo "class='tab' style='color:red;'";
  2524. echo ">Empty</a>";
  2525. echo "<a href='".PAGE."?action=table_drop&amp;table=".urlencode($_GET['table'])."' ";
  2526. echo "class='tab' style='color:red;'";
  2527. echo ">Drop</a>";
  2528. echo "<div style='clear:both;'></div>";
  2529. }
  2530. else
  2531. {
  2532. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_view&amp;view=1' ";
  2533. if($_GET['action']=="row_view")
  2534. echo "class='tab_pressed'";
  2535. else
  2536. echo "class='tab'";
  2537. echo ">Browse</a>";
  2538. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_view&amp;view=1' ";
  2539. if($_GET['action']=="column_view")
  2540. echo "class='tab_pressed'";
  2541. else
  2542. echo "class='tab'";
  2543. echo ">Structure</a>";
  2544. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_sql&amp;view=1' ";
  2545. if($_GET['action']=="table_sql")
  2546. echo "class='tab_pressed'";
  2547. else
  2548. echo "class='tab'";
  2549. echo ">SQL</a>";
  2550. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_search&amp;view=1' ";
  2551. if($_GET['action']=="table_search")
  2552. echo "class='tab_pressed'";
  2553. else
  2554. echo "class='tab'";
  2555. echo ">Search</a>";
  2556. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_export&amp;view=1' ";
  2557. if($_GET['action']=="table_export")
  2558. echo "class='tab_pressed'";
  2559. else
  2560. echo "class='tab'";
  2561. echo ">Export</a>";
  2562. echo "<a href='".PAGE."?action=view_drop&amp;table=".urlencode($_GET['table'])."&amp;view=1' ";
  2563. echo "class='tab' style='color:red;'";
  2564. echo ">Drop</a>";
  2565. echo "<div style='clear:both;'></div>";
  2566. }
  2567. }
  2568. //switch board for the page display
  2569. if(isset($_GET['action']) && !isset($_GET['confirm']))
  2570. {
  2571. echo "<div id='main'>";
  2572. switch($_GET['action'])
  2573. {
  2574. //table actions
  2575. /////////////////////////////////////////////// create table
  2576. case "table_create":
  2577. $query = "SELECT name FROM sqlite_master WHERE type='table' AND name=".$db->quote($_POST['tablename']);
  2578. $results = $db->selectArray($query);
  2579. if(sizeof($results)>0)
  2580. $exists = true;
  2581. else
  2582. $exists = false;
  2583. echo "<h2>Creating new table: '".htmlencode($_POST['tablename'])."'</h2>";
  2584. if($_POST['tablefields']=="" || intval($_POST['tablefields'])<=0)
  2585. echo "You must specify the number of table fields.";
  2586. else if($_POST['tablename']=="")
  2587. echo "You must specify a table name.";
  2588. else if($exists)
  2589. echo "Table of the same name already exists.";
  2590. else
  2591. {
  2592. $num = intval($_POST['tablefields']);
  2593. $name = $_POST['tablename'];
  2594. echo "<form action='".PAGE."?action=table_create&amp;confirm=1' method='post'>";
  2595. echo "<input type='hidden' name='tablename' value='".htmlencode($name)."'/>";
  2596. echo "<input type='hidden' name='rows' value='".$num."'/>";
  2597. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2598. echo "<tr>";
  2599. $headings = array("Field", "Type", "Primary Key", "Autoincrement", "Not NULL", "Default Value");
  2600. for($k=0; $k<count($headings); $k++)
  2601. echo "<td class='tdheader'>" . $headings[$k] . "</td>";
  2602. echo "</tr>";
  2603. for($i=0; $i<$num; $i++)
  2604. {
  2605. $tdWithClass = "<td class='td" . ($i%2 ? "1" : "2") . "'>";
  2606. echo "<tr>";
  2607. echo $tdWithClass;
  2608. echo "<input type='text' name='".$i."_field' style='width:200px;'/>";
  2609. echo "</td>";
  2610. echo $tdWithClass;
  2611. echo "<select name='".$i."_type' id='i".$i."_type' onchange='toggleAutoincrement(".$i.");'>";
  2612. $types = unserialize(DATATYPES);
  2613. for($z=0; $z<sizeof($types); $z++)
  2614. echo "<option value='".htmlencode($types[$z])."'>".htmlencode($types[$z])."</option>";
  2615. echo "</select>";
  2616. echo "</td>";
  2617. echo $tdWithClass;
  2618. echo "<input type='checkbox' name='".$i."_primarykey' id='i".$i."_primarykey' onclick='toggleNull(".$i."); toggleAutoincrement(".$i.");'/> Yes";
  2619. echo "</td>";
  2620. echo $tdWithClass;
  2621. echo "<input type='checkbox' name='".$i."_autoincrement' id='i".$i."_autoincrement'/> Yes";
  2622. echo "</td>";
  2623. echo $tdWithClass;
  2624. echo "<input type='checkbox' name='".$i."_notnull' id='i".$i."_notnull'/> Yes";
  2625. echo "</td>";
  2626. echo $tdWithClass;
  2627. echo "<input type='text' name='".$i."_defaultvalue' style='width:100px;'/>";
  2628. echo "</td>";
  2629. echo "</tr>";
  2630. }
  2631. echo "<tr>";
  2632. echo "<td class='tdheader' style='text-align:right;' colspan='6'>";
  2633. echo "<input type='submit' value='Create' class='btn'/> ";
  2634. echo "<a href='".PAGE."'>Cancel</a>";
  2635. echo "</td>";
  2636. echo "</tr>";
  2637. echo "</table>";
  2638. echo "</form>";
  2639. echo "<script type='text/javascript'>window.onload=initAutoincrement;</script>";
  2640. }
  2641. break;
  2642. /////////////////////////////////////////////// perform SQL query on table
  2643. case "table_sql":
  2644. $isSelect = false;
  2645. if(isset($_POST['query']) && $_POST['query']!="")
  2646. {
  2647. $delimiter = $_POST['delimiter'];
  2648. $queryStr = stripslashes($_POST['queryval']);
  2649. $query = explode_sql($delimiter, $queryStr); //explode the query string into individual queries based on the delimiter
  2650. for($i=0; $i<sizeof($query); $i++) //iterate through the queries exploded by the delimiter
  2651. {
  2652. if(str_replace(" ", "", str_replace("\n", "", str_replace("\r", "", $query[$i])))!="") //make sure this query is not an empty string
  2653. {
  2654. $startTime = microtime(true);
  2655. if(strpos(strtolower($query[$i]), "select ")!==false
  2656. || strpos(strtolower($query[$i]), "pragma ")!==false) // pragma often returns rows just like select
  2657. {
  2658. $isSelect = true;
  2659. $result = $db->selectArray($query[$i], "assoc");
  2660. }
  2661. else
  2662. {
  2663. $isSelect = false;
  2664. $result = $db->query($query[$i]);
  2665. }
  2666. $endTime = microtime(true);
  2667. $time = round(($endTime - $startTime), 4);
  2668. echo "<div class='confirm'>";
  2669. echo "<b>";
  2670. if($result!==false)
  2671. {
  2672. if($isSelect)
  2673. {
  2674. $affected = sizeof($result);
  2675. echo "Showing ".$affected." row(s). ";
  2676. }
  2677. else
  2678. {
  2679. $affected = $db->getAffectedRows();
  2680. echo $affected." row(s) affected. ";
  2681. }
  2682. echo "(Query took ".$time." sec)</b><br/>";
  2683. }
  2684. else
  2685. {
  2686. echo "There is a problem with the syntax of your query ";
  2687. echo "(Query was not executed)</b><br/>";
  2688. }
  2689. echo "<span style='font-size:11px;'>".htmlencode($query[$i])."</span>";
  2690. echo "</div><br/>";
  2691. if($isSelect)
  2692. {
  2693. if(sizeof($result)>0)
  2694. {
  2695. $headers = array_keys($result[0]);
  2696. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2697. echo "<tr>";
  2698. for($j=0; $j<sizeof($headers); $j++)
  2699. {
  2700. echo "<td class='tdheader'>";
  2701. echo htmlencode($headers[$j]);
  2702. echo "</td>";
  2703. }
  2704. echo "</tr>";
  2705. for($j=0; $j<sizeof($result); $j++)
  2706. {
  2707. $tdWithClass = "<td class='td".($j%2 ? "1" : "2")."'>";
  2708. echo "<tr>";
  2709. for($z=0; $z<sizeof($headers); $z++)
  2710. {
  2711. echo $tdWithClass;
  2712. echo htmlencode($result[$j][$headers[$z]]);
  2713. echo "</td>";
  2714. }
  2715. echo "</tr>";
  2716. }
  2717. echo "</table><br/><br/>";
  2718. }
  2719. }
  2720. }
  2721. }
  2722. }
  2723. else
  2724. {
  2725. $delimiter = ";";
  2726. $queryStr = "SELECT * FROM ".$db->quote_id($_GET['table'])." WHERE 1";
  2727. }
  2728. echo "<fieldset>";
  2729. echo "<legend><b>Run SQL query/queries on database '".htmlencode($db->getName())."'</b></legend>";
  2730. if(!isset($_GET['view']))
  2731. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_sql' method='post'>";
  2732. else
  2733. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_sql&amp;view=1' method='post'>";
  2734. echo "<div style='float:left; width:70%;'>";
  2735. echo "<textarea style='width:97%; height:300px;' name='queryval' id='queryval'>".htmlencode($queryStr)."</textarea>";
  2736. echo "</div>";
  2737. echo "<div style='float:left; width:28%; padding-left:10px;'>";
  2738. echo "Fields<br/>";
  2739. echo "<select multiple='multiple' style='width:100%;' id='fieldcontainer'>";
  2740. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  2741. $result = $db->selectArray($query);
  2742. for($i=0; $i<sizeof($result); $i++)
  2743. {
  2744. echo "<option value='".htmlencode($result[$i][1])."'>".htmlencode($result[$i][1])."</option>";
  2745. }
  2746. echo "</select>";
  2747. echo "<input type='button' value='<<' onclick='moveFields();' class='btn'/>";
  2748. echo "</div>";
  2749. echo "<div style='clear:both;'></div>";
  2750. echo "Delimiter <input type='text' name='delimiter' value='".htmlencode($delimiter)."' style='width:50px;'/> ";
  2751. echo "<input type='submit' name='query' value='Go' class='btn'/>";
  2752. echo "</form>";
  2753. break;
  2754. /////////////////////////////////////////////// empty table
  2755. case "table_empty":
  2756. echo "<form action='".PAGE."?action=table_empty&amp;confirm=1' method='post'>";
  2757. echo "<input type='hidden' name='tablename' value='".htmlencode($_GET['table'])."'/>";
  2758. echo "<div class='confirm'>";
  2759. echo "Are you sure you want to empty the table '".htmlencode($_GET['table'])."'?<br/><br/>";
  2760. echo "<input type='submit' value='Confirm' class='btn'/> ";
  2761. echo "<a href='".PAGE."'>Cancel</a>";
  2762. echo "</div>";
  2763. break;
  2764. /////////////////////////////////////////////// drop table
  2765. case "table_drop":
  2766. echo "<form action='".PAGE."?action=table_drop&amp;confirm=1' method='post'>";
  2767. echo "<input type='hidden' name='tablename' value='".htmlencode($_GET['table'])."'/>";
  2768. echo "<div class='confirm'>";
  2769. echo "Are you sure you want to drop the table '".htmlencode($_GET['table'])."'?<br/><br/>";
  2770. echo "<input type='submit' value='Confirm' class='btn'/> ";
  2771. echo "<a href='".PAGE."'>Cancel</a>";
  2772. echo "</div>";
  2773. break;
  2774. /////////////////////////////////////////////// drop view
  2775. case "view_drop":
  2776. echo "<form action='".PAGE."?action=view_drop&amp;confirm=1' method='post'>";
  2777. echo "<input type='hidden' name='viewname' value='".htmlencode($_GET['table'])."'/>";
  2778. echo "<div class='confirm'>";
  2779. echo "Are you sure you want to drop the view '".htmlencode($_GET['table'])."'?<br/><br/>";
  2780. echo "<input type='submit' value='Confirm' class='btn'/> ";
  2781. echo "<a href='".PAGE."'>Cancel</a>";
  2782. echo "</div>";
  2783. break;
  2784. /////////////////////////////////////////////// export table
  2785. case "table_export":
  2786. echo "<form method='post' action='".PAGE."'>";
  2787. echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>Export</b></legend>";
  2788. echo "<input type='hidden' value='".htmlencode($_GET['table'])."' name='single_table'/>";
  2789. echo "<input type='radio' name='export_type' checked='checked' value='sql' onclick='toggleExports(\"sql\");'/> SQL";
  2790. echo "<br/><input type='radio' name='export_type' value='csv' onclick='toggleExports(\"csv\");'/> CSV";
  2791. echo "</fieldset>";
  2792. echo "<fieldset style='float:left; max-width:350px;' id='exportoptions_sql'><legend><b>Options</b></legend>";
  2793. echo "<input type='checkbox' checked='checked' name='structure'/> Export with structure ".helpLink("Export Structure to SQL File")."<br/>";
  2794. echo "<input type='checkbox' checked='checked' name='data'/> Export with data ".helpLink("Export Data to SQL File")."<br/>";
  2795. echo "<input type='checkbox' name='drop'/> Add DROP TABLE ".helpLink("Add Drop Table to Exported SQL File")."<br/>";
  2796. echo "<input type='checkbox' checked='checked' name='transaction'/> Add TRANSACTION ".helpLink("Add Transaction to Exported SQL File")."<br/>";
  2797. echo "<input type='checkbox' checked='checked' name='comments'/> Comments ".helpLink("Add Comments to Exported SQL File")."<br/>";
  2798. echo "</fieldset>";
  2799. echo "<fieldset style='float:left; max-width:350px; display:none;' id='exportoptions_csv'><legend><b>Options</b></legend>";
  2800. echo "<div style='float:left;'>Fields terminated by</div>";
  2801. echo "<input type='text' value=';' name='export_csv_fieldsterminated' style='float:right;'/>";
  2802. echo "<div style='clear:both;'>";
  2803. echo "<div style='float:left;'>Fields enclosed by</div>";
  2804. echo "<input type='text' value='\"' name='export_csv_fieldsenclosed' style='float:right;'/>";
  2805. echo "<div style='clear:both;'>";
  2806. echo "<div style='float:left;'>Fields escaped by</div>";
  2807. echo "<input type='text' value='\' name='export_csv_fieldsescaped' style='float:right;'/>";
  2808. echo "<div style='clear:both;'>";
  2809. echo "<div style='float:left;'>Replace NULL by</div>";
  2810. echo "<input type='text' value='NULL' name='export_csv_replacenull' style='float:right;'/>";
  2811. echo "<div style='clear:both;'>";
  2812. echo "<input type='checkbox' name='export_csv_crlf'/> Remove CRLF characters within fields<br/>";
  2813. echo "<input type='checkbox' checked='checked' name='export_csv_fieldnames'/> Put field names in first row";
  2814. echo "</fieldset>";
  2815. echo "<div style='clear:both;'></div>";
  2816. echo "<br/><br/>";
  2817. echo "<fieldset style='float:left;'><legend><b>Save As</b></legend>";
  2818. $file = pathinfo($db->getPath());
  2819. $name = $file['filename'];
  2820. echo "<input type='text' name='filename' value='".htmlencode($name).".".htmlencode($_GET['table']).".".date("n-j-y").".dump' style='width:400px;'/> <input type='submit' name='export' value='Export' class='btn'/>";
  2821. echo "</fieldset>";
  2822. echo "</form>";
  2823. break;
  2824. /////////////////////////////////////////////// import table
  2825. case "table_import":
  2826. if(isset($_POST['import']))
  2827. {
  2828. echo "<div class='confirm'>";
  2829. if($importSuccess===true)
  2830. echo "Import was successful.";
  2831. else
  2832. echo $importSuccess;
  2833. echo "</div><br/>";
  2834. }
  2835. echo "<form method='post' action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_import' enctype='multipart/form-data'>";
  2836. echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>Import into ".htmlencode($_GET['table'])."</b></legend>";
  2837. echo "<input type='radio' name='import_type' checked='checked' value='sql' onclick='toggleImports(\"sql\");'/> SQL";
  2838. echo "<br/><input type='radio' name='import_type' value='csv' onclick='toggleImports(\"csv\");'/> CSV";
  2839. echo "</fieldset>";
  2840. echo "<fieldset style='float:left; max-width:350px;' id='importoptions_sql'><legend><b>Options</b></legend>";
  2841. echo "No options";
  2842. echo "</fieldset>";
  2843. echo "<fieldset style='float:left; max-width:350px; display:none;' id='importoptions_csv'><legend><b>Options</b></legend>";
  2844. echo "<input type='hidden' value='".htmlencode($_GET['table'])."' name='single_table'/>";
  2845. echo "<div style='float:left;'>Fields terminated by</div>";
  2846. echo "<input type='text' value=';' name='import_csv_fieldsterminated' style='float:right;'/>";
  2847. echo "<div style='clear:both;'>";
  2848. echo "<div style='float:left;'>Fields enclosed by</div>";
  2849. echo "<input type='text' value='\"' name='import_csv_fieldsenclosed' style='float:right;'/>";
  2850. echo "<div style='clear:both;'>";
  2851. echo "<div style='float:left;'>Fields escaped by</div>";
  2852. echo "<input type='text' value='\' name='import_csv_fieldsescaped' style='float:right;'/>";
  2853. echo "<div style='clear:both;'>";
  2854. echo "<div style='float:left;'>NULL represented by</div>";
  2855. echo "<input type='text' value='NULL' name='import_csv_replacenull' style='float:right;'/>";
  2856. echo "<div style='clear:both;'>";
  2857. echo "<input type='checkbox' checked='checked' name='import_csv_fieldnames'/> Field names in first row";
  2858. echo "</fieldset>";
  2859. echo "<div style='clear:both;'></div>";
  2860. echo "<br/><br/>";
  2861. echo "<fieldset><legend><b>File to import</b></legend>";
  2862. echo "<input type='file' value='Choose File' name='file' style='background-color:transparent; border-style:none;'/> <input type='submit' value='Import' name='import' class='btn'/>";
  2863. echo "</fieldset>";
  2864. break;
  2865. /////////////////////////////////////////////// rename table
  2866. case "table_rename":
  2867. echo "<form action='".PAGE."?action=table_rename&amp;confirm=1' method='post'>";
  2868. echo "<input type='hidden' name='oldname' value='".htmlencode($_GET['table'])."'/>";
  2869. echo "Rename table '".htmlencode($_GET['table'])."' to <input type='text' name='newname' style='width:200px;'/> <input type='submit' value='Rename' name='rename' class='btn'/>";
  2870. echo "</form>";
  2871. break;
  2872. /////////////////////////////////////////////// search table
  2873. case "table_search":
  2874. if(isset($_GET['done']))
  2875. {
  2876. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  2877. $result = $db->selectArray($query);
  2878. $j = 0;
  2879. $arr = array();
  2880. for($i=0; $i<sizeof($result); $i++)
  2881. {
  2882. $field = $result[$i][1];
  2883. $field_index = str_replace(" ","_",$field);
  2884. $operator = $_POST[$field_index.":operator"];
  2885. $value = $_POST[$field_index];
  2886. if($value!="" || $operator=="!= ''" || $operator=="= ''")
  2887. {
  2888. if($operator=="= ''" || $operator=="!= ''")
  2889. $arr[$j] = $db->quote_id($field)." ".$operator;
  2890. else
  2891. $arr[$j] = $db->quote_id($field)." ".$operator." ".$db->quote($value);
  2892. $j++;
  2893. }
  2894. }
  2895. $query = "SELECT * FROM ".$db->quote_id($_GET['table']);
  2896. if(sizeof($arr)>0)
  2897. {
  2898. $query .= " WHERE ".$arr[0];
  2899. for($i=1; $i<sizeof($arr); $i++)
  2900. {
  2901. $query .= " AND ".$arr[$i];
  2902. }
  2903. }
  2904. $startTime = microtime(true);
  2905. $result = $db->selectArray($query, "assoc");
  2906. $endTime = microtime(true);
  2907. $time = round(($endTime - $startTime), 4);
  2908. echo "<div class='confirm'>";
  2909. echo "<b>";
  2910. if($result!==false)
  2911. {
  2912. $affected = sizeof($result);
  2913. echo "Showing ".$affected." row(s). ";
  2914. echo "(Query took ".$time." sec)</b><br/>";
  2915. }
  2916. else
  2917. {
  2918. echo "There is a problem with the syntax of your query ";
  2919. echo "(Query was not executed)</b><br/>";
  2920. }
  2921. echo "<span style='font-size:11px;'>".htmlencode($query)."</span>";
  2922. echo "</div><br/>";
  2923. if(sizeof($result)>0)
  2924. {
  2925. $headers = array_keys($result[0]);
  2926. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2927. echo "<tr>";
  2928. for($j=0; $j<sizeof($headers); $j++)
  2929. {
  2930. echo "<td class='tdheader'>";
  2931. echo htmlencode($headers[$j]);
  2932. echo "</td>";
  2933. }
  2934. echo "</tr>";
  2935. for($j=0; $j<sizeof($result); $j++)
  2936. {
  2937. $tdWithClass = "<td class='td".($j%2 ? "1" : "2")."'>";
  2938. echo "<tr>";
  2939. for($z=0; $z<sizeof($headers); $z++)
  2940. {
  2941. echo $tdWithClass;
  2942. echo htmlencode($result[$j][$headers[$z]]);
  2943. echo "</td>";
  2944. }
  2945. echo "</tr>";
  2946. }
  2947. echo "</table><br/><br/>";
  2948. }
  2949. if(!isset($_GET['view']))
  2950. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_search'>Do Another Search</a>";
  2951. else
  2952. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_search&amp;view=1'>Do Another Search</a>";
  2953. }
  2954. else
  2955. {
  2956. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  2957. $result = $db->selectArray($query);
  2958. if(!isset($_GET['view']))
  2959. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_search&amp;done=1' method='post'>";
  2960. else
  2961. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=table_search&amp;view=1&amp;done=1' method='post'>";
  2962. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  2963. echo "<tr>";
  2964. echo "<td class='tdheader'>Field</td>";
  2965. echo "<td class='tdheader'>Type</td>";
  2966. echo "<td class='tdheader'>Operator</td>";
  2967. echo "<td class='tdheader'>Value</td>";
  2968. echo "</tr>";
  2969. for($i=0; $i<sizeof($result); $i++)
  2970. {
  2971. $field = $result[$i][1];
  2972. $type = $result[$i][2];
  2973. $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  2974. $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  2975. echo "<tr>";
  2976. echo $tdWithClassLeft;
  2977. echo $field;
  2978. echo "</td>";
  2979. echo $tdWithClassLeft;
  2980. echo $type;
  2981. echo "</td>";
  2982. echo $tdWithClassLeft;
  2983. echo "<select name='".htmlencode($field).":operator'>";
  2984. echo "<option value='='>=</option>";
  2985. if($type=="INTEGER" || $type=="REAL")
  2986. {
  2987. echo "<option value='>'>></option>";
  2988. echo "<option value='>='>>=</option>";
  2989. echo "<option value='<'><</option>";
  2990. echo "<option value='<='><=</option>";
  2991. }
  2992. else if($type=="TEXT" || $type=="BLOB")
  2993. {
  2994. echo "<option value='= '''>= ''</option>";
  2995. echo "<option value='!= '''>!= ''</option>";
  2996. }
  2997. echo "<option value='!='>!=</option>";
  2998. if($type=="TEXT" || $type=="BLOB")
  2999. echo "<option value='LIKE' selected='selected'>LIKE</option>";
  3000. else
  3001. echo "<option value='LIKE'>LIKE</option>";
  3002. echo "<option value='NOT LIKE'>NOT LIKE</option>";
  3003. echo "</select>";
  3004. echo "</td>";
  3005. echo $tdWithClassLeft;
  3006. if($type=="INTEGER" || $type=="REAL" || $type=="NULL")
  3007. echo "<input type='text' name='".htmlencode($field)."'/>";
  3008. else
  3009. echo "<textarea name='".htmlencode($field)."' wrap='hard' rows='1' cols='60'></textarea>";
  3010. echo "</td>";
  3011. echo "</tr>";
  3012. }
  3013. echo "<tr>";
  3014. echo "<td class='tdheader' style='text-align:right;' colspan='4'>";
  3015. echo "<input type='submit' value='Search' class='btn'/>";
  3016. echo "</td>";
  3017. echo "</tr>";
  3018. echo "</table>";
  3019. echo "</form>";
  3020. }
  3021. break;
  3022. //row actions
  3023. /////////////////////////////////////////////// view row
  3024. case "row_view":
  3025. if(!isset($_POST['startRow']))
  3026. $_POST['startRow'] = 0;
  3027. if(isset($_POST['numRows']))
  3028. $_SESSION[COOKIENAME.'numRows'] = $_POST['numRows'];
  3029. if(!isset($_SESSION[COOKIENAME.'numRows']))
  3030. $_SESSION[COOKIENAME.'numRows'] = 30;
  3031. if(isset($_SESSION[COOKIENAME.'currentTable']) && $_SESSION[COOKIENAME.'currentTable']!=$_GET['table'])
  3032. {
  3033. unset($_SESSION[COOKIENAME.'sort']);
  3034. unset($_SESSION[COOKIENAME.'order']);
  3035. }
  3036. if(isset($_POST['viewtype']))
  3037. {
  3038. $_SESSION[COOKIENAME.'viewtype'] = $_POST['viewtype'];
  3039. }
  3040. $query = "SELECT Count(*) FROM ".$db->quote_id($_GET['table']);
  3041. $rowCount = $db->select($query);
  3042. $rowCount = intval($rowCount[0]);
  3043. $lastPage = intval($rowCount / $_SESSION[COOKIENAME.'numRows']);
  3044. $remainder = intval($rowCount % $_SESSION[COOKIENAME.'numRows']);
  3045. if($remainder==0)
  3046. $remainder = $_SESSION[COOKIENAME.'numRows'];
  3047. echo "<div style='overflow:hidden;'>";
  3048. //previous button
  3049. if($_POST['startRow']>0)
  3050. {
  3051. echo "<div style='float:left; overflow:hidden;'>";
  3052. echo "<form action='".PAGE."?action=row_view&amp;table=".urlencode($_GET['table'])."' method='post'>";
  3053. echo "<input type='hidden' name='startRow' value='0'/>";
  3054. echo "<input type='hidden' name='numRows' value='".$_SESSION[COOKIENAME.'numRows']."'/> ";
  3055. echo "<input type='submit' value='&larr;&larr;' name='previous' class='btn'/> ";
  3056. echo "</form>";
  3057. echo "</div>";
  3058. echo "<div style='float:left; overflow:hidden; margin-right:20px;'>";
  3059. echo "<form action='".PAGE."?action=row_view&amp;table=".urlencode($_GET['table'])."' method='post'>";
  3060. echo "<input type='hidden' name='startRow' value='".intval($_POST['startRow']-$_SESSION[COOKIENAME.'numRows'])."'/>";
  3061. echo "<input type='hidden' name='numRows' value='".$_SESSION[COOKIENAME.'numRows']."'/> ";
  3062. echo "<input type='submit' value='&larr;' name='previous_full' class='btn'/> ";
  3063. echo "</form>";
  3064. echo "</div>";
  3065. }
  3066. //show certain number buttons
  3067. echo "<div style='float:left; overflow:hidden;'>";
  3068. echo "<form action='".PAGE."?action=row_view&amp;table=".urlencode($_GET['table'])."' method='post'>";
  3069. echo "<input type='submit' value='Show : ' name='show' class='btn'/> ";
  3070. echo "<input type='text' name='numRows' style='width:50px;' value='".$_SESSION[COOKIENAME.'numRows']."'/> ";
  3071. echo "row(s) starting from record # ";
  3072. if(intval($_POST['startRow']+$_SESSION[COOKIENAME.'numRows']) < $rowCount)
  3073. echo "<input type='text' name='startRow' style='width:90px;' value='".intval($_POST['startRow']+$_SESSION[COOKIENAME.'numRows'])."'/>";
  3074. else
  3075. echo "<input type='text' name='startRow' style='width:90px;' value='0'/>";
  3076. echo " as a ";
  3077. echo "<select name='viewtype'>";
  3078. if(!isset($_SESSION[COOKIENAME.'viewtype']) || $_SESSION[COOKIENAME.'viewtype']=="table")
  3079. {
  3080. echo "<option value='table' selected='selected'>Table</option>";
  3081. echo "<option value='chart'>Chart</option>";
  3082. }
  3083. else
  3084. {
  3085. echo "<option value='table'>Table</option>";
  3086. echo "<option value='chart' selected='selected'>Chart</option>";
  3087. }
  3088. echo "</select>";
  3089. echo "</form>";
  3090. echo "</div>";
  3091. //next button
  3092. if(intval($_POST['startRow']+$_SESSION[COOKIENAME.'numRows'])<$rowCount)
  3093. {
  3094. echo "<div style='float:left; overflow:hidden; margin-left:20px; '>";
  3095. echo "<form action='".PAGE."?action=row_view&amp;table=".urlencode($_GET['table'])."' method='post'>";
  3096. echo "<input type='hidden' name='startRow' value='".intval($_POST['startRow']+$_SESSION[COOKIENAME.'numRows'])."'/>";
  3097. echo "<input type='hidden' name='numRows' value='".$_SESSION[COOKIENAME.'numRows']."'/> ";
  3098. echo "<input type='submit' value='&rarr;' name='next' class='btn'/> ";
  3099. echo "</form>";
  3100. echo "</div>";
  3101. echo "<div style='float:left; overflow:hidden;'>";
  3102. echo "<form action='".PAGE."?action=row_view&amp;table=".urlencode($_GET['table'])."' method='post'>";
  3103. echo "<input type='hidden' name='startRow' value='".intval($rowCount-$remainder)."'/>";
  3104. echo "<input type='hidden' name='numRows' value='".$_SESSION[COOKIENAME.'numRows']."'/> ";
  3105. echo "<input type='submit' value='&rarr;&rarr;' name='next_full' class='btn'/> ";
  3106. echo "</form>";
  3107. echo "</div>";
  3108. }
  3109. echo "<div style='clear:both;'></div>";
  3110. echo "</div>";
  3111. if(!isset($_GET['sort']))
  3112. $_GET['sort'] = NULL;
  3113. if(!isset($_GET['order']))
  3114. $_GET['order'] = NULL;
  3115. $table = $_GET['table'];
  3116. $numRows = $_SESSION[COOKIENAME.'numRows'];
  3117. $startRow = $_POST['startRow'];
  3118. if(isset($_GET['sort']))
  3119. {
  3120. $_SESSION[COOKIENAME.'sort'] = $_GET['sort'];
  3121. $_SESSION[COOKIENAME.'currentTable'] = $_GET['table'];
  3122. }
  3123. if(isset($_GET['order']))
  3124. {
  3125. $_SESSION[COOKIENAME.'order'] = $_GET['order'];
  3126. $_SESSION[COOKIENAME.'currentTable'] = $_GET['table'];
  3127. }
  3128. $_SESSION[COOKIENAME.'numRows'] = $numRows;
  3129. $query = "SELECT *, ROWID FROM ".$db->quote_id($table);
  3130. $queryDisp = "SELECT * FROM ".$db->quote_id($table);
  3131. $queryAdd = "";
  3132. if(isset($_SESSION[COOKIENAME.'sort']))
  3133. $queryAdd .= " ORDER BY ".$_SESSION[COOKIENAME.'sort'];
  3134. if(isset($_SESSION[COOKIENAME.'order']))
  3135. $queryAdd .= " ".$_SESSION[COOKIENAME.'order'];
  3136. $queryAdd .= " LIMIT ".$startRow.", ".$numRows;
  3137. $query .= $queryAdd;
  3138. $queryDisp .= $queryAdd;
  3139. $startTime = microtime(true);
  3140. $arr = $db->selectArray($query);
  3141. $endTime = microtime(true);
  3142. $time = round(($endTime - $startTime), 4);
  3143. $total = $db->numRows($table);
  3144. if(sizeof($arr)>0)
  3145. {
  3146. echo "<br/><div class='confirm'>";
  3147. echo "<b>Showing rows ".$startRow." - ".($startRow + sizeof($arr)-1)." (".$total." total, Query took ".$time." sec)</b><br/>";
  3148. echo "<span style='font-size:11px;'>".htmlencode($queryDisp)."</span>";
  3149. echo "</div><br/>";
  3150. if(isset($_GET['view']))
  3151. {
  3152. echo "'".htmlencode($_GET['table'])."' is a view, which means it is a SELECT statement treated as a read-only table. You may not edit or insert records. <a href='http://en.wikipedia.org/wiki/View_(database)' target='_blank'>http://en.wikipedia.org/wiki/View_(database)</a>";
  3153. echo "<br/><br/>";
  3154. }
  3155. $query = "PRAGMA table_info(".$db->quote_id($table).")";
  3156. $result = $db->selectArray($query);
  3157. $rowidColumn = sizeof($result);
  3158. if(!isset($_SESSION[COOKIENAME.'viewtype']) || $_SESSION[COOKIENAME.'viewtype']=="table")
  3159. {
  3160. echo "<form action='".PAGE."?action=row_editordelete&amp;table=".urlencode($table)."' method='post' name='checkForm'>";
  3161. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3162. echo "<tr>";
  3163. if(!isset($_GET['view']))
  3164. echo "<td colspan='3'></td>";
  3165. for($i=0; $i<sizeof($result); $i++)
  3166. {
  3167. echo "<td class='tdheader'>";
  3168. if(!isset($_GET['view']))
  3169. echo "<a href='".PAGE."?action=row_view&amp;table=".urlencode($table)."&amp;sort=".urlencode($result[$i][1]);
  3170. else
  3171. echo "<a href='".PAGE."?action=row_view&amp;table=".urlencode($table)."&amp;view=1&amp;sort=".urlencode($result[$i][1]);
  3172. if(isset($_SESSION[COOKIENAME.'sort']))
  3173. $orderTag = ($_SESSION[COOKIENAME.'sort']==$result[$i][1] && $_SESSION[COOKIENAME.'order']=="ASC") ? "DESC" : "ASC";
  3174. else
  3175. $orderTag = "ASC";
  3176. echo "&amp;order=".$orderTag;
  3177. echo "'>".$result[$i][1]."</a>";
  3178. if(isset($_SESSION[COOKIENAME.'sort']) && $_SESSION[COOKIENAME.'sort']==$result[$i][1])
  3179. echo (($_SESSION[COOKIENAME.'order']=="ASC") ? " <b>&uarr;</b>" : " <b>&darr;</b>");
  3180. echo "</td>";
  3181. }
  3182. echo "</tr>";
  3183. for($i=0; $i<sizeof($arr); $i++)
  3184. {
  3185. // -g-> $pk will always be the last column in each row of the array because we are doing a "SELECT *, ROWID FROM ..."
  3186. $pk = $arr[$i][$rowidColumn];
  3187. $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  3188. $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  3189. echo "<tr>";
  3190. if(!isset($_GET['view']))
  3191. {
  3192. echo $tdWithClass;
  3193. echo "<input type='checkbox' name='check[]' value='".htmlencode($pk)."' id='check_".htmlencode($i)."'/>";
  3194. echo "</td>";
  3195. echo $tdWithClass;
  3196. // -g-> Here, we need to put the ROWID in as the link for both the edit and delete.
  3197. echo "<a href='".PAGE."?table=".urlencode($table)."&amp;action=row_editordelete&amp;pk=".urlencode($pk)."&amp;type=edit'>edit</a>";
  3198. echo "</td>";
  3199. echo $tdWithClass;
  3200. echo "<a href='".PAGE."?table=".urlencode($table)."&amp;action=row_editordelete&amp;pk=".urlencode($pk)."&amp;type=delete' style='color:red;'>delete</a>";
  3201. echo "</td>";
  3202. }
  3203. for($j=0; $j<sizeof($result); $j++)
  3204. {
  3205. if(strtolower($result[$j][2])=="integer" || strtolower($result[$j][2])=="float" || strtolower($result[$j][2])=="real")
  3206. echo $tdWithClass;
  3207. else
  3208. echo $tdWithClassLeft;
  3209. // -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.
  3210. // di - i don't understand how SQLite returns null values. I played around with the conditional here and couldn't get empty strings to differeniate from actual null values...
  3211. if($arr[$i][$j]===NULL)
  3212. echo "<i>NULL</i>";
  3213. else
  3214. echo htmlencode($arr[$i][$j]);
  3215. echo "</td>";
  3216. }
  3217. echo "</tr>";
  3218. }
  3219. echo "</table>";
  3220. if(!isset($_GET['view']))
  3221. {
  3222. echo "<a onclick='checkAll()'>Check All</a> / <a onclick='uncheckAll()'>Uncheck All</a> <i>With selected:</i> ";
  3223. echo "<select name='type'>";
  3224. echo "<option value='edit'>Edit</option>";
  3225. echo "<option value='delete'>Delete</option>";
  3226. echo "</select> ";
  3227. echo "<input type='submit' value='Go' name='massGo' class='btn'/>";
  3228. }
  3229. echo "</form>";
  3230. }
  3231. else
  3232. {
  3233. if(!isset($_SESSION[COOKIENAME.$_GET['table'].'chartlabels']))
  3234. {
  3235. for($i=0; $i<sizeof($result); $i++)
  3236. {
  3237. if(strtolower($result[$i][2])=="text")
  3238. $_SESSION[COOKIENAME.$_GET['table'].'chartlabels'] = $i;
  3239. }
  3240. }
  3241. if(!isset($_SESSION[COOKIENAME.'chartlabels']))
  3242. $_SESSION[COOKIENAME.'chartlabels'] = 0;
  3243. if(!isset($_SESSION[COOKIENAME.$_GET['table'].'chartvalues']))
  3244. {
  3245. for($i=0; $i<sizeof($result); $i++)
  3246. {
  3247. if(strtolower($result[$i][2])=="integer" || strtolower($result[$i][2])=="float" || strtolower($result[$i][2])=="real")
  3248. $_SESSION[COOKIENAME.$_GET['table'].'chartvalues'] = $i;
  3249. }
  3250. }
  3251. if(!isset($_SESSION[COOKIENAME.'charttype']))
  3252. $_SESSION[COOKIENAME.'charttype'] = "bar";
  3253. if(isset($_POST['chartsettings']))
  3254. {
  3255. $_SESSION[COOKIENAME.'charttype'] = $_POST['charttype'];
  3256. $_SESSION[COOKIENAME.$_GET['table'].'chartlabels'] = $_POST['chartlabels'];
  3257. $_SESSION[COOKIENAME.$_GET['table'].'chartvalues'] = $_POST['chartvalues'];
  3258. }
  3259. //begin chart view
  3260. ?>
  3261. <script type='text/javascript' src='https://www.google.com/jsapi'></script>
  3262. <script type='text/javascript'>
  3263. google.load('visualization', '1.0', {'packages':['corechart']});
  3264. google.setOnLoadCallback(drawChart);
  3265. function drawChart()
  3266. {
  3267. var data = new google.visualization.DataTable();
  3268. data.addColumn('string', '<?php echo $result[$_SESSION[COOKIENAME.$_GET['table'].'chartlabels']][1]; ?>');
  3269. data.addColumn('number', '<?php echo $result[$_SESSION[COOKIENAME.$_GET['table'].'chartvalues']][1]; ?>');
  3270. data.addRows([
  3271. <?php
  3272. for($i=0; $i<sizeof($arr); $i++)
  3273. {
  3274. $label = str_replace("'", "", htmlencode($arr[$i][$_SESSION[COOKIENAME.$_GET['table'].'chartlabels']]));
  3275. $value = htmlencode($arr[$i][$_SESSION[COOKIENAME.$_GET['table'].'chartvalues']]);
  3276. if($value==NULL || $value=="")
  3277. $value = 0;
  3278. echo "['".$label."', ".$value."]";
  3279. if($i<sizeof($arr)-1)
  3280. echo ",";
  3281. }
  3282. $height = (sizeof($arr)+1) * 30;
  3283. if($height>1000)
  3284. $height = 1000;
  3285. else if($height<300)
  3286. $height = 300;
  3287. if($_SESSION[COOKIENAME.'charttype']=="pie")
  3288. $height = 800;
  3289. ?>
  3290. ]);
  3291. var chartWidth = document.getElementById("content").offsetWidth - document.getElementById("chartsettingsbox").offsetWidth - 100;
  3292. if(chartWidth>1000)
  3293. chartWidth = 1000;
  3294. var options =
  3295. {
  3296. 'width':chartWidth,
  3297. 'height':<?php echo $height; ?>,
  3298. 'title':'<?php echo $result[$_SESSION[COOKIENAME.$_GET['table'].'chartlabels']][1]." vs ".$result[$_SESSION[COOKIENAME.$_GET['table'].'chartvalues']][1]; ?>'
  3299. };
  3300. <?php
  3301. if($_SESSION[COOKIENAME.'charttype']=="bar")
  3302. echo "var chart = new google.visualization.BarChart(document.getElementById('chart_div'));";
  3303. else if($_SESSION[COOKIENAME.'charttype']=="pie")
  3304. echo "var chart = new google.visualization.PieChart(document.getElementById('chart_div'));";
  3305. else
  3306. echo "var chart = new google.visualization.LineChart(document.getElementById('chart_div'));";
  3307. ?>
  3308. chart.draw(data, options);
  3309. }
  3310. </script>
  3311. <div id="chart_div" style="float:left;">If you can read this, it means the chart could not be generated. The data you are trying to view may not be appropriate for a chart.</div>
  3312. <?php
  3313. echo "<fieldset style='float:right; text-align:center;' id='chartsettingsbox'><legend><b>Chart Settings</b></legend>";
  3314. echo "<form action='".PAGE."?action=row_view&amp;table=".urlencode($_GET['table'])."' method='post'>";
  3315. echo "Chart Type: <select name='charttype'>";
  3316. echo "<option value='bar'";
  3317. if($_SESSION[COOKIENAME.'charttype']=="bar")
  3318. echo " selected='selected'";
  3319. echo ">Bar Chart</option>";
  3320. echo "<option value='pie'";
  3321. if($_SESSION[COOKIENAME.'charttype']=="pie")
  3322. echo " selected='selected'";
  3323. echo ">Pie Chart</option>";
  3324. echo "<option value='line'";
  3325. if($_SESSION[COOKIENAME.'charttype']=="line")
  3326. echo " selected='selected'";
  3327. echo ">Line Chart</option>";
  3328. echo "</select>";
  3329. echo "<br/><br/>";
  3330. echo "Labels: <select name='chartlabels'>";
  3331. for($i=0; $i<sizeof($result); $i++)
  3332. {
  3333. if(isset($_SESSION[COOKIENAME.$_GET['table'].'chartlabels']) && $_SESSION[COOKIENAME.$_GET['table'].'chartlabels']==$i)
  3334. echo "<option value='".$i."' selected='selected'>".htmlencode($result[$i][1])."</option>";
  3335. else
  3336. echo "<option value='".$i."'>".htmlencode($result[$i][1])."</option>";
  3337. }
  3338. echo "</select>";
  3339. echo "<br/><br/>";
  3340. echo "Values: <select name='chartvalues'>";
  3341. for($i=0; $i<sizeof($result); $i++)
  3342. {
  3343. if(strtolower($result[$i][2])=="integer" || strtolower($result[$i][2])=="float" || strtolower($result[$i][2])=="real")
  3344. {
  3345. if(isset($_SESSION[COOKIENAME.$_GET['table'].'chartvalues']) && $_SESSION[COOKIENAME.$_GET['table'].'chartvalues']==$i)
  3346. echo "<option value='".$i."' selected='selected'>".htmlencode($result[$i][1])."</option>";
  3347. else
  3348. echo "<option value='".$i."'>".htmlencode($result[$i][1])."</option>";
  3349. }
  3350. }
  3351. echo "</select>";
  3352. echo "<br/><br/>";
  3353. echo "<input type='submit' name='chartsettings' value='Update' class='btn'/>";
  3354. echo "</form>";
  3355. echo "</fieldset>";
  3356. echo "<div style='clear:both;'></div>";
  3357. //end chart view
  3358. }
  3359. }
  3360. else if($rowCount>0)//no rows - do nothing
  3361. {
  3362. echo "<br/><br/>There are no rows in the table for the range you selected.";
  3363. }
  3364. else
  3365. {
  3366. echo "<br/><br/>This table is empty. <a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_create'>Click here</a> to insert rows.";
  3367. }
  3368. break;
  3369. /////////////////////////////////////////////// create row
  3370. case "row_create":
  3371. $fieldStr = "";
  3372. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_create' method='post'>";
  3373. echo "Restart insertion with ";
  3374. echo "<select name='num'>";
  3375. for($i=1; $i<=40; $i++)
  3376. {
  3377. if(isset($_POST['num']) && $_POST['num']==$i)
  3378. echo "<option value='".$i."' selected='selected'>".$i."</option>";
  3379. else
  3380. echo "<option value='".$i."'>".$i."</option>";
  3381. }
  3382. echo "</select>";
  3383. echo " rows ";
  3384. echo "<input type='submit' value='Go' class='btn'/>";
  3385. echo "</form>";
  3386. echo "<br/>";
  3387. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  3388. $result = $db->selectArray($query);
  3389. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_create&amp;confirm=1' method='post'>";
  3390. if(isset($_POST['num']))
  3391. $num = $_POST['num'];
  3392. else
  3393. $num = 1;
  3394. echo "<input type='hidden' name='numRows' value='".$num."'/>";
  3395. for($j=0; $j<$num; $j++)
  3396. {
  3397. if($j>0)
  3398. echo "<input type='checkbox' value='ignore' name='".$j.":ignore' id='row_".$j."_ignore' checked='checked'/> Ignore<br/>";
  3399. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3400. echo "<tr>";
  3401. echo "<td class='tdheader'>Field</td>";
  3402. echo "<td class='tdheader'>Type</td>";
  3403. echo "<td class='tdheader'>Function</td>";
  3404. echo "<td class='tdheader'>Null</td>";
  3405. echo "<td class='tdheader'>Value</td>";
  3406. echo "</tr>";
  3407. for($i=0; $i<sizeof($result); $i++)
  3408. {
  3409. $field = $result[$i][1];
  3410. $field_html = htmlencode($field);
  3411. if($j==0)
  3412. $fieldStr .= ":".$field;
  3413. $type = strtolower($result[$i][2]);
  3414. $scalarField = $type=="integer" || $type=="real" || $type=="null";
  3415. $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  3416. $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  3417. echo "<tr>";
  3418. echo $tdWithClassLeft;
  3419. echo $field_html;
  3420. echo "</td>";
  3421. echo $tdWithClassLeft;
  3422. echo $type;
  3423. echo "</td>";
  3424. echo $tdWithClassLeft;
  3425. echo "<select name='function_".$j."_".$field_html."' onchange='notNull(\"row_".$j."_field_".$i."_null\");'>";
  3426. echo "<option value=''>&nbsp;</option>";
  3427. $functions = array_merge(unserialize(FUNCTIONS), $db->getUserFunctions());
  3428. for($z=0; $z<sizeof($functions); $z++)
  3429. {
  3430. echo "<option value='".htmlencode($functions[$z])."'>".htmlencode($functions[$z])."</option>";
  3431. }
  3432. echo "</select>";
  3433. echo "</td>";
  3434. //we need to have a column dedicated to nulls -di
  3435. echo $tdWithClassLeft;
  3436. if($result[$i][3]==0)
  3437. {
  3438. if($result[$i][4]===NULL)
  3439. echo "<input type='checkbox' name='".$j.":".$field_html."_null' id='row_".$j."_field_".$i."_null' checked='checked' onclick='disableText(this, \"row_".$j."_field_".$i."_value\");'/>";
  3440. else
  3441. echo "<input type='checkbox' name='".$j.":".$field_html."_null' id='row_".$j."_field_".$i."_null' onclick='disableText(this, \"row_".$j."_field_".$i."_value\");'/>";
  3442. }
  3443. echo "</td>";
  3444. echo $tdWithClassLeft;
  3445. // 22 August 2011: gkf fixed bug #55. The form is now prepopulated with the default values
  3446. // so that the insert proceeds normally.
  3447. // 22 August 2011: gkf fixed bug #53. The form now displays more of the text.
  3448. // 19 October 2011: di fixed the bug caused by the previous fix where the null column does not exist anymore
  3449. $type = strtolower($type);
  3450. if($scalarField)
  3451. echo "<input type='text' id='row_".$j."_field_".$i."_value' name='".$j.":".$field_html."' value='".deQuoteSQL($result[$i][4])."' onblur='changeIgnore(this, \"row_".$j."_ignore\");' onclick='notNull(\"row_".$j."_field_".$i."_null\");'/>";
  3452. else
  3453. echo "<textarea id='row_".$j."_field_".$i."_value' name='".$j.":".$field_html."' rows='5' cols='60' onclick='notNull(\"row_".$j."_field_".$i."_null\");' onblur='changeIgnore(this, \"row_".$j."_ignore\");'>".deQuoteSQL($result[$i][4])."</textarea>";
  3454. echo "</td>";
  3455. echo "</tr>";
  3456. }
  3457. echo "<tr>";
  3458. echo "<td class='tdheader' style='text-align:right;' colspan='5'>";
  3459. echo "<input type='submit' value='Insert' class='btn'/>";
  3460. echo "</td>";
  3461. echo "</tr>";
  3462. echo "</table><br/>";
  3463. }
  3464. $fieldStr = substr($fieldStr, 1);
  3465. echo "<input type='hidden' name='fields' value='".htmlencode($fieldStr)."'/>";
  3466. echo "</form>";
  3467. break;
  3468. /////////////////////////////////////////////// edit or delete row
  3469. case "row_editordelete":
  3470. if(isset($_POST['check']))
  3471. $pks = $_POST['check'];
  3472. else if(isset($_GET['pk']))
  3473. $pks = array($_GET['pk']);
  3474. $str = $pks[0];
  3475. $pkVal = $pks[0];
  3476. for($i=1; $i<sizeof($pks); $i++)
  3477. {
  3478. $str .= ", ".$pks[$i];
  3479. $pkVal .= ":".$pks[$i];
  3480. }
  3481. if($str=="") //nothing was selected so show an error
  3482. {
  3483. echo "<div class='confirm'>";
  3484. echo "Error: You did not select anything.";
  3485. echo "</div>";
  3486. echo "<br/><br/><a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_view'>Return</a>";
  3487. }
  3488. else
  3489. {
  3490. if((isset($_POST['type']) && $_POST['type']=="edit") || (isset($_GET['type']) && $_GET['type']=="edit")) //edit
  3491. {
  3492. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_edit&amp;confirm=1&amp;pk=".urlencode($pkVal)."' method='post'>";
  3493. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  3494. $result = $db->selectArray($query);
  3495. //build the POST array of fields
  3496. $fieldStr = $result[0][1];
  3497. for($j=1; $j<sizeof($result); $j++)
  3498. $fieldStr .= ":".$result[$j][1];
  3499. echo "<input type='hidden' name='fieldArray' value='".htmlencode($fieldStr)."'/>";
  3500. for($j=0; $j<sizeof($pks); $j++)
  3501. {
  3502. if(!is_numeric($pks[$j])) continue;
  3503. $query = "SELECT * FROM ".$db->quote_id($_GET['table'])." WHERE ROWID = ".$pks[$j];
  3504. $result1 = $db->select($query);
  3505. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3506. echo "<tr>";
  3507. echo "<td class='tdheader'>Field</td>";
  3508. echo "<td class='tdheader'>Type</td>";
  3509. echo "<td class='tdheader'>Function</td>";
  3510. echo "<td class='tdheader'>Null</td>";
  3511. echo "<td class='tdheader'>Value</td>";
  3512. echo "</tr>";
  3513. for($i=0; $i<sizeof($result); $i++)
  3514. {
  3515. $field = $result[$i][1];
  3516. $type = $result[$i][2];
  3517. $value = $result1[$i];
  3518. $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  3519. $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  3520. echo "<tr>";
  3521. echo $tdWithClass;
  3522. echo $field;
  3523. echo "</td>";
  3524. echo $tdWithClass;
  3525. echo $type;
  3526. echo "</td>";
  3527. echo $tdWithClassLeft;
  3528. echo "<select name='function_".htmlencode($pks[$j])."_".htmlencode($field)."' onchange='notNull(\"".htmlencode($pks[$j]).":".htmlencode($field)."_null\");'>";
  3529. echo "<option value=''></option>";
  3530. $functions = array_merge(unserialize(FUNCTIONS), $db->getUserFunctions());
  3531. for($z=0; $z<sizeof($functions); $z++)
  3532. {
  3533. echo "<option value='".htmlencode($functions[$z])."'>".htmlencode($functions[$z])."</option>";
  3534. }
  3535. echo "</select>";
  3536. echo "</td>";
  3537. echo $tdWithClassLeft;
  3538. if($result[$i][3]==0)
  3539. {
  3540. if($value===NULL)
  3541. echo "<input type='checkbox' name='".htmlencode($pks[$j]).":".htmlencode($field)."_null' id='".htmlencode($pks[$j]).":".htmlencode($field)."_null' checked='checked'/>";
  3542. else
  3543. echo "<input type='checkbox' name='".htmlencode($pks[$j]).":".htmlencode($field)."_null' id='".htmlencode($pks[$j]).":".htmlencode($field)."_null'/>";
  3544. }
  3545. echo "</td>";
  3546. echo $tdWithClassLeft;
  3547. if($type=="INTEGER" || $type=="REAL" || $type=="NULL")
  3548. echo "<input type='text' name='".htmlencode($pks[$j]).":".htmlencode($field)."' value='".htmlencode($value)."' onblur='changeIgnore(this, \"".$j."\", \"".htmlencode($pks[$j]).":".htmlencode($field)."_null\")' />";
  3549. else
  3550. echo "<textarea name='".htmlencode($pks[$j]).":".htmlencode($field)."' wrap='hard' rows='1' cols='60' onblur='changeIgnore(this, \"".$j."\", \"".htmlencode($pks[$j]).":".htmlencode($field)."_null\")'>".htmlencode($value)."</textarea>";
  3551. echo "</td>";
  3552. echo "</tr>";
  3553. }
  3554. echo "<tr>";
  3555. echo "<td class='tdheader' style='text-align:right;' colspan='5'>";
  3556. echo "<input type='submit' name='new_row' value='Insert As New Row' class='btn'/> ";
  3557. echo "<input type='submit' value='Save Changes' class='btn'/> ";
  3558. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_view'>Cancel</a>";
  3559. echo "</td>";
  3560. echo "</tr>";
  3561. echo "</table>";
  3562. echo "<br/>";
  3563. }
  3564. echo "</form>";
  3565. }
  3566. else //delete
  3567. {
  3568. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_delete&amp;confirm=1&amp;pk=".urlencode($pkVal)."' method='post'>";
  3569. echo "<div class='confirm'>";
  3570. echo "Are you sure you want to delete row(s) ".htmlencode($str)." from table '".htmlencode($_GET['table'])."'?<br/><br/>";
  3571. echo "<input type='submit' value='Confirm' class='btn'/> ";
  3572. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=row_view'>Cancel</a>";
  3573. echo "</div>";
  3574. }
  3575. }
  3576. break;
  3577. //column actions
  3578. /////////////////////////////////////////////// view column
  3579. case "column_view":
  3580. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  3581. $result = $db->selectArray($query);
  3582. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_delete' method='post' name='checkForm'>";
  3583. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3584. echo "<tr>";
  3585. if(!isset($_GET['view']))
  3586. echo "<td colspan='3'></td>";
  3587. echo "<td class='tdheader'>Column #</td>";
  3588. echo "<td class='tdheader'>Field</td>";
  3589. echo "<td class='tdheader'>Type</td>";
  3590. echo "<td class='tdheader'>Not Null</td>";
  3591. echo "<td class='tdheader'>Default Value</td>";
  3592. echo "<td class='tdheader'>Primary Key</td>";
  3593. echo "</tr>";
  3594. for($i=0; $i<sizeof($result); $i++)
  3595. {
  3596. $colVal = $result[$i][0];
  3597. $fieldVal = $result[$i][1];
  3598. $typeVal = $result[$i][2];
  3599. $notnullVal = $result[$i][3];
  3600. $defaultVal = $result[$i][4];
  3601. $primarykeyVal = $result[$i][5];
  3602. if(intval($notnullVal)!=0)
  3603. $notnullVal = "yes";
  3604. else
  3605. $notnullVal = "no";
  3606. if(intval($primarykeyVal)!=0)
  3607. $primarykeyVal = "yes";
  3608. else
  3609. $primarykeyVal = "no";
  3610. $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  3611. $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  3612. echo "<tr>";
  3613. if(!isset($_GET['view']))
  3614. {
  3615. echo $tdWithClass;
  3616. echo "<input type='checkbox' name='check[]' value='".htmlencode($fieldVal)."' id='check_".$i."'/>";
  3617. echo "</td>";
  3618. echo $tdWithClass;
  3619. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_edit&amp;pk=".urlencode($fieldVal)."'>edit</a>";
  3620. echo "</td>";
  3621. echo $tdWithClass;
  3622. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_delete&amp;pk=".urlencode($fieldVal)."' style='color:red;'>delete</a>";
  3623. echo "</td>";
  3624. }
  3625. echo $tdWithClass;
  3626. echo htmlencode($colVal);
  3627. echo "</td>";
  3628. echo $tdWithClassLeft;
  3629. echo htmlencode($fieldVal);
  3630. echo "</td>";
  3631. echo $tdWithClassLeft;
  3632. echo htmlencode($typeVal);
  3633. echo "</td>";
  3634. echo $tdWithClassLeft;
  3635. echo htmlencode($notnullVal);
  3636. echo "</td>";
  3637. echo $tdWithClassLeft;
  3638. echo htmlencode($defaultVal);
  3639. echo "</td>";
  3640. echo $tdWithClassLeft;
  3641. echo htmlencode($primarykeyVal);
  3642. echo "</td>";
  3643. echo "</tr>";
  3644. }
  3645. echo "</table>";
  3646. if(!isset($_GET['view']))
  3647. {
  3648. echo "<a onclick='checkAll()'>Check All</a> / <a onclick='uncheckAll()'>Uncheck All</a> <i>With selected:</i> ";
  3649. echo "<select name='massType'>";
  3650. //echo "<option value='edit'>Edit</option>";
  3651. echo "<option value='delete'>Delete</option>";
  3652. echo "</select> ";
  3653. echo "<input type='hidden' name='structureDel' value='true'/>";
  3654. echo "<input type='submit' value='Go' name='massGo' class='btn'/>";
  3655. }
  3656. echo "</form>";
  3657. if(!isset($_GET['view']))
  3658. {
  3659. echo "<br/>";
  3660. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_create' method='post'>";
  3661. echo "<input type='hidden' name='tablename' value='".htmlencode($_GET['table'])."'/>";
  3662. 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' class='btn'/>";
  3663. echo "</form>";
  3664. }
  3665. $query = "SELECT sql FROM sqlite_master WHERE name=".$db->quote($_GET['table']);
  3666. $master = $db->selectArray($query);
  3667. echo "<br/>";
  3668. if(!isset($_GET['view']))
  3669. $typ = "table";
  3670. else
  3671. $typ = "view";
  3672. echo "<br/>";
  3673. echo "<div class='confirm'>";
  3674. echo "<b>Query used to create this ".$typ."</b><br/>";
  3675. echo "<span style='font-size:11px;'>".htmlencode($master[0]['sql'])."</span>";
  3676. echo "</div>";
  3677. echo "<br/>";
  3678. if(!isset($_GET['view']))
  3679. {
  3680. echo "<br/><hr/><br/>";
  3681. //$query = "SELECT * FROM sqlite_master WHERE type='index' AND tbl_name='".$_GET['table']."'";
  3682. $query = "PRAGMA index_list(".$db->quote_id($_GET['table']).")";
  3683. $result = $db->selectArray($query);
  3684. if(sizeof($result)>0)
  3685. {
  3686. echo "<h2>Indexes:</h2>";
  3687. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3688. echo "<tr>";
  3689. echo "<td colspan='1'>";
  3690. echo "</td>";
  3691. echo "<td class='tdheader'>Name</td>";
  3692. echo "<td class='tdheader'>Unique</td>";
  3693. echo "<td class='tdheader'>Seq. No.</td>";
  3694. echo "<td class='tdheader'>Column #</td>";
  3695. echo "<td class='tdheader'>Field</td>";
  3696. echo "</tr>";
  3697. for($i=0; $i<sizeof($result); $i++)
  3698. {
  3699. if($result[$i]['unique']==0)
  3700. $unique = "no";
  3701. else
  3702. $unique = "yes";
  3703. $query = "PRAGMA index_info(".$db->quote_id($result[$i]['name']).")";
  3704. $info = $db->selectArray($query);
  3705. $span = sizeof($info);
  3706. $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  3707. $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  3708. $tdWithClassSpan = "<td class='td".($i%2 ? "1" : "2")."' rowspan='".$span."'>";
  3709. $tdWithClassLeftSpan = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;' rowspan='".$span."'>";
  3710. echo "<tr>";
  3711. echo $tdWithClassSpan;
  3712. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=index_delete&amp;pk=".urlencode($result[$i]['name'])."' style='color:red;'>delete</a>";
  3713. echo "</td>";
  3714. echo $tdWithClassLeftSpan;
  3715. echo $result[$i]['name'];
  3716. echo "</td>";
  3717. echo $tdWithClassLeftSpan;
  3718. echo $unique;
  3719. echo "</td>";
  3720. for($j=0; $j<$span; $j++)
  3721. {
  3722. if($j!=0)
  3723. echo "<tr>";
  3724. echo $tdWithClassLeft;
  3725. echo htmlencode($info[$j]['seqno']);
  3726. echo "</td>";
  3727. echo $tdWithClassLeft;
  3728. echo htmlencode($info[$j]['cid']);
  3729. echo "</td>";
  3730. echo $tdWithClassLeft;
  3731. echo htmlencode($info[$j]['name']);
  3732. echo "</td>";
  3733. echo "</tr>";
  3734. }
  3735. }
  3736. echo "</table><br/><br/>";
  3737. }
  3738. $query = "SELECT * FROM sqlite_master WHERE type='trigger' AND tbl_name=".$db->quote($_GET['table'])." ORDER BY name";
  3739. $result = $db->selectArray($query);
  3740. //print_r($result);
  3741. if(sizeof($result)>0)
  3742. {
  3743. echo "<h2>Triggers:</h2>";
  3744. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3745. echo "<tr>";
  3746. echo "<td colspan='1'>";
  3747. echo "</td>";
  3748. echo "<td class='tdheader'>Name</td>";
  3749. echo "<td class='tdheader'>SQL</td>";
  3750. echo "</tr>";
  3751. for($i=0; $i<sizeof($result); $i++)
  3752. {
  3753. $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  3754. echo "<tr>";
  3755. echo $tdWithClass;
  3756. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=trigger_delete&amp;pk=".urlencode($result[$i]['name'])."' style='color:red;'>delete</a>";
  3757. echo "</td>";
  3758. echo $tdWithClass;
  3759. echo htmlencode($result[$i]['name']);
  3760. echo "</td>";
  3761. echo $tdWithClass;
  3762. echo htmlencode($result[$i]['sql']);
  3763. echo "</td>";
  3764. }
  3765. echo "</table><br/><br/>";
  3766. }
  3767. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=index_create' method='post'>";
  3768. echo "<input type='hidden' name='tablename' value='".htmlencode($_GET['table'])."'/>";
  3769. echo "<br/><div class='tdheader'>";
  3770. echo "Create an index on <input type='text' name='numcolumns' style='width:30px;' value='1'/> columns <input type='submit' value='Go' name='addindex' class='btn'/>";
  3771. echo "</div>";
  3772. echo "</form>";
  3773. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=trigger_create' method='post'>";
  3774. echo "<input type='hidden' name='tablename' value='".htmlencode($_GET['table'])."'/>";
  3775. echo "<br/><div class='tdheader'>";
  3776. echo "Create a new trigger <input type='submit' value='Go' name='addindex' class='btn'/>";
  3777. echo "</div>";
  3778. echo "</form>";
  3779. }
  3780. break;
  3781. /////////////////////////////////////////////// create column
  3782. case "column_create":
  3783. echo "<h2>Adding new field(s) to table '".htmlencode($_POST['tablename'])."'</h2>";
  3784. if($_POST['tablefields']=="" || intval($_POST['tablefields'])<=0)
  3785. echo "You must specify the number of table fields.";
  3786. else if($_POST['tablename']=="")
  3787. echo "You must specify a table name.";
  3788. else
  3789. {
  3790. $num = intval($_POST['tablefields']);
  3791. $name = $_POST['tablename'];
  3792. echo "<form action='".PAGE."?table=".urlencode($_POST['tablename'])."&amp;action=column_create&amp;confirm=1' method='post'>";
  3793. echo "<input type='hidden' name='tablename' value='".htmlencode($name)."'/>";
  3794. echo "<input type='hidden' name='rows' value='".$num."'/>";
  3795. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3796. echo "<tr>";
  3797. $headings = array("Field", "Type", "Primary Key", "Autoincrement", "Not NULL", "Default Value");
  3798. for($k=0; $k<count($headings); $k++)
  3799. echo "<td class='tdheader'>" . $headings[$k] . "</td>";
  3800. echo "</tr>";
  3801. for($i=0; $i<$num; $i++)
  3802. {
  3803. $tdWithClass = "<td class='td" . ($i%2 ? "1" : "2") . "'>";
  3804. echo "<tr>";
  3805. echo $tdWithClass;
  3806. echo "<input type='text' name='".$i."_field' style='width:200px;'/>";
  3807. echo "</td>";
  3808. echo $tdWithClass;
  3809. echo "<select name='".$i."_type' id='i".$i."_type' onchange='toggleAutoincrement(".$i.");'>";
  3810. $types = unserialize(DATATYPES);
  3811. for($z=0; $z<sizeof($types); $z++)
  3812. echo "<option value='".htmlencode($types[$z])."'>".htmlencode($types[$z])."</option>";
  3813. echo "</select>";
  3814. echo "</td>";
  3815. echo $tdWithClass;
  3816. echo "<input type='checkbox' name='".$i."_primarykey'/> Yes";
  3817. echo "</td>";
  3818. echo $tdWithClass;
  3819. echo "<input type='checkbox' name='".$i."_autoincrement' id='i".$i."_autoincrement'/> Yes";
  3820. echo "</td>";
  3821. echo $tdWithClass;
  3822. echo "<input type='checkbox' name='".$i."_notnull'/> Yes";
  3823. echo "</td>";
  3824. echo $tdWithClass;
  3825. echo "<input type='text' name='".$i."_defaultvalue' style='width:100px;'/>";
  3826. echo "</td>";
  3827. echo "</tr>";
  3828. }
  3829. echo "<tr>";
  3830. echo "<td class='tdheader' style='text-align:right;' colspan='6'>";
  3831. echo "<input type='submit' value='Add Field(s)' class='btn'/> ";
  3832. echo "<a href='".PAGE."?table=".urlencode($_POST['tablename'])."&amp;action=column_view'>Cancel</a>";
  3833. echo "</td>";
  3834. echo "</tr>";
  3835. echo "</table>";
  3836. echo "</form>";
  3837. }
  3838. break;
  3839. /////////////////////////////////////////////// delete column
  3840. case "column_delete":
  3841. if(isset($_POST['check']))
  3842. $pks = $_POST['check'];
  3843. elseif(isset($_GET['pk']))
  3844. $pks = array($_GET['pk']);
  3845. else $pks = array();
  3846. if(sizeof($pks)==0) //nothing was selected so show an error
  3847. {
  3848. echo "<div class='confirm'>";
  3849. echo "Error: You did not select anything.";
  3850. echo "</div>";
  3851. echo "<br/><br/><a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_view'>Return</a>";
  3852. }
  3853. else
  3854. {
  3855. $str = $pks[0];
  3856. $pkVal = $pks[0];
  3857. for($i=1; $i<sizeof($pks); $i++)
  3858. {
  3859. $str .= ", ".$pks[$i];
  3860. $pkVal .= ":".$pks[$i];
  3861. }
  3862. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_delete&amp;confirm=1&amp;pk=".urlencode($pkVal)."' method='post'>";
  3863. echo "<div class='confirm'>";
  3864. echo "Are you sure you want to delete column(s) ".htmlencode($str)." from table '".htmlencode($_GET['table'])."'?<br/><br/>";
  3865. echo "<input type='submit' value='Confirm' class='btn'/> ";
  3866. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_view'>Cancel</a>";
  3867. echo "</div>";
  3868. }
  3869. break;
  3870. /////////////////////////////////////////////// edit column
  3871. case "column_edit":
  3872. echo "<h2>Editing column '".htmlencode($_GET['pk'])."' on table '".htmlencode($_GET['table'])."'</h2>";
  3873. echo "Due to the limitations of SQLite, only the field name and data type can be modified.<br/><br/>";
  3874. if(!isset($_GET['pk']))
  3875. echo "You must specify a column.";
  3876. else if(!isset($_GET['table']) || $_GET['table']=="")
  3877. echo "You must specify a table name.";
  3878. else
  3879. {
  3880. $query = "PRAGMA table_info(".$db->quote_id($_GET['table']).")";
  3881. $result = $db->selectArray($query);
  3882. for($i=0; $i<sizeof($result); $i++)
  3883. {
  3884. if($result[$i][1]==$_GET['pk'])
  3885. {
  3886. $colVal = $result[$i][0];
  3887. $fieldVal = $result[$i][1];
  3888. $typeVal = $result[$i][2];
  3889. $notnullVal = $result[$i][3];
  3890. $defaultVal = $result[$i][4];
  3891. $primarykeyVal = $result[$i][5];
  3892. break;
  3893. }
  3894. }
  3895. $name = $_GET['table'];
  3896. echo "<form action='".PAGE."?table=".urlencode($name)."&amp;action=column_edit&amp;confirm=1' method='post'>";
  3897. echo "<input type='hidden' name='tablename' value='".htmlencode($name)."'/>";
  3898. echo "<input type='hidden' name='oldvalue' value='".htmlencode($_GET['pk'])."'/>";
  3899. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  3900. echo "<tr>";
  3901. //$headings = array("Field", "Type", "Primary Key", "Autoincrement", "Not NULL", "Default Value");
  3902. $headings = array("Field", "Type");
  3903. for($k=0; $k<count($headings); $k++)
  3904. echo "<td class='tdheader'>".$headings[$k]."</td>";
  3905. echo "</tr>";
  3906. $i = 0;
  3907. $tdWithClass = "<td class='td" . ($i%2 ? "1" : "2") . "'>";
  3908. echo "<tr>";
  3909. echo $tdWithClass;
  3910. echo "<input type='text' name='".$i."_field' style='width:200px;' value='".htmlencode($fieldVal)."'/>";
  3911. echo "</td>";
  3912. echo $tdWithClass;
  3913. echo "<select name='".$i."_type' id='i".$i."_type' onchange='toggleAutoincrement(".$i.");'>";
  3914. $types = unserialize(DATATYPES);
  3915. for($z=0; $z<sizeof($types); $z++)
  3916. {
  3917. if($types[$z]==$typeVal)
  3918. echo "<option value='".htmlencode($types[$z])."' selected='selected'>".htmlencode($types[$z])."</option>";
  3919. else
  3920. echo "<option value='".htmlencode($types[$z])."'>".htmlencode($types[$z])."</option>";
  3921. }
  3922. echo "</select>";
  3923. echo "</td>";
  3924. /*
  3925. echo $tdWithClass;
  3926. if($primarykeyVal)
  3927. echo "<input type='checkbox' name='".$i."_primarykey' checked='checked'/> Yes";
  3928. else
  3929. echo "<input type='checkbox' name='".$i."_primarykey'/> Yes";
  3930. echo "</td>";
  3931. echo $tdWithClass;
  3932. if(1==2)
  3933. echo "<input type='checkbox' name='".$i."_autoincrement' id='".$i."_autoincrement' checked='checked'/> Yes";
  3934. else
  3935. echo "<input type='checkbox' name='".$i."_autoincrement' id='".$i."_autoincrement'/> Yes";
  3936. echo "</td>";
  3937. echo $tdWithClass;
  3938. if($notnullVal)
  3939. echo "<input type='checkbox' name='".$i."_notnull' checked='checked'/> Yes";
  3940. else
  3941. echo "<input type='checkbox' name='".$i."_notnull'/> Yes";
  3942. echo "</td>";
  3943. echo $tdWithClass;
  3944. echo "<input type='text' name='".$i."_defaultvalue' value='".$defaultVal."' style='width:100px;'/>";
  3945. echo "</td>";
  3946. */
  3947. echo "</tr>";
  3948. echo "<tr>";
  3949. echo "<td class='tdheader' style='text-align:right;' colspan='6'>";
  3950. echo "<input type='submit' value='Save Changes' class='btn'/> ";
  3951. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_view'>Cancel</a>";
  3952. echo "</td>";
  3953. echo "</tr>";
  3954. echo "</table>";
  3955. echo "</form>";
  3956. }
  3957. break;
  3958. /////////////////////////////////////////////// delete index
  3959. case "index_delete":
  3960. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=index_delete&amp;pk=".urlencode($_GET['pk'])."&amp;confirm=1' method='post'>";
  3961. echo "<div class='confirm'>";
  3962. echo "Are you sure you want to delete index '".htmlencode($_GET['pk'])."'?<br/><br/>";
  3963. echo "<input type='submit' value='Confirm' class='btn'/> ";
  3964. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_view'>Cancel</a>";
  3965. echo "</div>";
  3966. echo "</form>";
  3967. break;
  3968. /////////////////////////////////////////////// delete trigger
  3969. case "trigger_delete":
  3970. echo "<form action='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=trigger_delete&amp;pk=".urlencode($_GET['pk'])."&amp;confirm=1' method='post'>";
  3971. echo "<div class='confirm'>";
  3972. echo "Are you sure you want to delete trigger '".htmlencode($_GET['pk'])."'?<br/><br/>";
  3973. echo "<input type='submit' value='Confirm' class='btn'/> ";
  3974. echo "<a href='".PAGE."?table=".urlencode($_GET['table'])."&amp;action=column_view'>Cancel</a>";
  3975. echo "</div>";
  3976. echo "</form>";
  3977. break;
  3978. /////////////////////////////////////////////// create trigger
  3979. case "trigger_create":
  3980. echo "<h2>Creating new trigger on table '".htmlencode($_POST['tablename'])."'</h2>";
  3981. if($_POST['tablename']=="")
  3982. echo "You must specify a table name.";
  3983. else
  3984. {
  3985. echo "<form action='".PAGE."?table=".urlencode($_POST['tablename'])."&amp;action=trigger_create&amp;confirm=1' method='post'>";
  3986. echo "Trigger name: <input type='text' name='trigger_name'/><br/><br/>";
  3987. echo "<fieldset><legend>Database Event</legend>";
  3988. echo "Before/After: ";
  3989. echo "<select name='beforeafter'>";
  3990. echo "<option value=''></option>";
  3991. echo "<option value='BEFORE'>BEFORE</option>";
  3992. echo "<option value='AFTER'>AFTER</option>";
  3993. echo "<option value='INSTEAD OF'>INSTEAD OF</option>";
  3994. echo "</select>";
  3995. echo "<br/><br/>";
  3996. echo "Event: ";
  3997. echo "<select name='event'>";
  3998. echo "<option value='DELETE'>DELETE</option>";
  3999. echo "<option value='INSERT'>INSERT</option>";
  4000. echo "<option value='UPDATE'>UPDATE</option>";
  4001. echo "</select>";
  4002. echo "</fieldset><br/><br/>";
  4003. echo "<fieldset><legend>Trigger Action</legend>";
  4004. echo "<input type='checkbox' name='foreachrow'/> For Each Row<br/><br/>";
  4005. echo "WHEN expression (type expression without 'WHEN'):<br/>";
  4006. echo "<textarea name='whenexpression' style='width:500px; height:100px;'></textarea>";
  4007. echo "<br/><br/>";
  4008. echo "Trigger Steps (semicolon terminated):<br/>";
  4009. echo "<textarea name='triggersteps' style='width:500px; height:100px;'></textarea>";
  4010. echo "</fieldset><br/><br/>";
  4011. echo "<input type='submit' value='Create Trigger' class='btn'/> ";
  4012. echo "<a href='".PAGE."?table=".urlencode($_POST['tablename'])."&amp;action=column_view'>Cancel</a>";
  4013. echo "</form>";
  4014. }
  4015. break;
  4016. /////////////////////////////////////////////// create index
  4017. case "index_create":
  4018. echo "<h2>Creating new index on table '".htmlencode($_POST['tablename'])."'</h2>";
  4019. if($_POST['numcolumns']=="" || intval($_POST['numcolumns'])<=0)
  4020. echo "You must specify the number of table fields.";
  4021. else if($_POST['tablename']=="")
  4022. echo "You must specify a table name.";
  4023. else
  4024. {
  4025. echo "<form action='".PAGE."?table=".urlencode($_POST['tablename'])."&amp;action=index_create&amp;confirm=1' method='post'>";
  4026. $num = intval($_POST['numcolumns']);
  4027. $query = "PRAGMA table_info(".$db->quote_id($_POST['tablename']).")";
  4028. $result = $db->selectArray($query);
  4029. echo "<fieldset><legend>Define index properties</legend>";
  4030. echo "Index name: <input type='text' name='name'/><br/>";
  4031. echo "Duplicate values: ";
  4032. echo "<select name='duplicate'>";
  4033. echo "<option value='yes'>Allowed</option>";
  4034. echo "<option value='no'>Not Allowed</option>";
  4035. echo "</select><br/>";
  4036. echo "</fieldset>";
  4037. echo "<br/>";
  4038. echo "<fieldset><legend>Define index columns</legend>";
  4039. for($i=0; $i<$num; $i++)
  4040. {
  4041. echo "<select name='".$i."_field'>";
  4042. echo "<option value=''>--Ignore--</option>";
  4043. for($j=0; $j<sizeof($result); $j++)
  4044. echo "<option value='".htmlencode($result[$j][1])."'>".htmlencode($result[$j][1])."</option>";
  4045. echo "</select> ";
  4046. echo "<select name='".$i."_order'>";
  4047. echo "<option value=''></option>";
  4048. echo "<option value=' ASC'>Ascending</option>";
  4049. echo "<option value=' DESC'>Descending</option>";
  4050. echo "</select><br/>";
  4051. }
  4052. echo "</fieldset>";
  4053. echo "<br/><br/>";
  4054. echo "<input type='hidden' name='num' value='".$num."'/>";
  4055. echo "<input type='submit' value='Create Index' class='btn'/> ";
  4056. echo "<a href='".PAGE."?table=".urlencode($_POST['tablename'])."&amp;action=column_view'>Cancel</a>";
  4057. echo "</form>";
  4058. }
  4059. break;
  4060. }
  4061. echo "</div>";
  4062. }
  4063. $view = "structure";
  4064. 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
  4065. {
  4066. if(isset($_GET['view']))
  4067. $view = $_GET['view'];
  4068. else
  4069. $view = "structure";
  4070. echo "<a href='".PAGE."?view=structure' ";
  4071. if($view=="structure")
  4072. echo "class='tab_pressed'";
  4073. else
  4074. echo "class='tab'";
  4075. echo ">Structure</a>";
  4076. echo "<a href='".PAGE."?view=sql' ";
  4077. if($view=="sql")
  4078. echo "class='tab_pressed'";
  4079. else
  4080. echo "class='tab'";
  4081. echo ">SQL</a>";
  4082. echo "<a href='".PAGE."?view=export' ";
  4083. if($view=="export")
  4084. echo "class='tab_pressed'";
  4085. else
  4086. echo "class='tab'";
  4087. echo ">Export</a>";
  4088. echo "<a href='".PAGE."?view=import' ";
  4089. if($view=="import")
  4090. echo "class='tab_pressed'";
  4091. else
  4092. echo "class='tab'";
  4093. echo ">Import</a>";
  4094. echo "<a href='".PAGE."?view=vacuum' ";
  4095. if($view=="vacuum")
  4096. echo "class='tab_pressed'";
  4097. else
  4098. echo "class='tab'";
  4099. echo ">Vacuum</a>";
  4100. if($directory!==false && is_writable($directory))
  4101. {
  4102. echo "<a href='".PAGE."?view=rename' ";
  4103. if($view=="rename")
  4104. echo "class='tab_pressed'";
  4105. else
  4106. echo "class='tab'";
  4107. echo ">Rename Database</a>";
  4108. echo "<a href='".PAGE."?view=delete' style='color:red;' ";
  4109. if($view=="delete")
  4110. echo "class='tab_pressed'";
  4111. else
  4112. echo "class='tab'";
  4113. echo ">Delete Database</a>";
  4114. }
  4115. echo "<div style='clear:both;'></div>";
  4116. echo "<div id='main'>";
  4117. if($view=="structure") //database structure - view of all the tables
  4118. {
  4119. $query = "SELECT sqlite_version() AS sqlite_version";
  4120. $queryVersion = $db->select($query);
  4121. $realVersion = $queryVersion['sqlite_version'];
  4122. if(SYSTEMPASSWORD=="admin")
  4123. {
  4124. echo "<div class='confirm' style='margin:20px;'>";
  4125. echo "You are using the default password, which can be dangerous. You can change it easily at the top of phpliteadmin.php<br />You have been warned.";
  4126. echo "</div>";
  4127. }
  4128. echo "<b>Database name</b>: ".htmlencode($db->getName())."<br/>";
  4129. echo "<b>Path to database</b>: ".htmlencode($db->getPath())."<br/>";
  4130. echo "<b>Size of database</b>: ".$db->getSize()."<br/>";
  4131. echo "<b>Database last modified</b>: ".$db->getDate()."<br/>";
  4132. echo "<b>SQLite version</b>: ".$realVersion."<br/>";
  4133. echo "<b>SQLite extension</b> ".helpLink("SQLite Library Extensions").": ".$db->getType()."<br/>";
  4134. echo "<b>PHP version</b>: ".phpversion()."<br/><br/>";
  4135. if(isset($_GET['sort']))
  4136. $_SESSION[COOKIENAME.'sort'] = $_GET['sort'];
  4137. else
  4138. unset($_SESSION[COOKIENAME.'sort']);
  4139. if(isset($_GET['order']))
  4140. $_SESSION[COOKIENAME.'order'] = $_GET['order'];
  4141. else
  4142. unset($_SESSION[COOKIENAME.'order']);
  4143. $query = "SELECT type, name FROM sqlite_master WHERE type='table' OR type='view'";
  4144. $queryAdd = "";
  4145. if(isset($_SESSION[COOKIENAME.'sort']))
  4146. $queryAdd .= " ORDER BY ".$_SESSION[COOKIENAME.'sort'];
  4147. if(isset($_SESSION[COOKIENAME.'order']))
  4148. $queryAdd .= " ".$_SESSION[COOKIENAME.'order'];
  4149. $query .= $queryAdd;
  4150. $result = $db->selectArray($query);
  4151. $j = 0;
  4152. for($i=0; $i<sizeof($result); $i++)
  4153. if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  4154. $j++;
  4155. if($j==0)
  4156. echo "No tables in database.<br/><br/>";
  4157. else
  4158. {
  4159. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  4160. echo "<tr>";
  4161. echo "<td class='tdheader'>";
  4162. echo "<a href='".PAGE."?sort=type";
  4163. if(isset($_SESSION[COOKIENAME.'sort']))
  4164. $orderTag = ($_SESSION[COOKIENAME.'sort']=="type" && $_SESSION[COOKIENAME.'order']=="ASC") ? "DESC" : "ASC";
  4165. else
  4166. $orderTag = "ASC";
  4167. echo "&amp;order=".$orderTag;
  4168. echo "'>Type</a> ".helpLink("Tables vs. Views");
  4169. if(isset($_SESSION[COOKIENAME.'sort']) && $_SESSION[COOKIENAME.'sort']=="type")
  4170. echo (($_SESSION[COOKIENAME.'order']=="ASC") ? " <b>&uarr;</b>" : " <b>&darr;</b>");
  4171. echo "</td>";
  4172. echo "<td class='tdheader'>";
  4173. echo "<a href='".PAGE."?sort=name";
  4174. if(isset($_SESSION[COOKIENAME.'sort']))
  4175. $orderTag = ($_SESSION[COOKIENAME.'sort']=="name" && $_SESSION[COOKIENAME.'order']=="ASC") ? "DESC" : "ASC";
  4176. else
  4177. $orderTag = "ASC";
  4178. echo "&amp;order=".$orderTag;
  4179. echo "'>Name</a>";
  4180. if(isset($_SESSION[COOKIENAME.'sort']) && $_SESSION[COOKIENAME.'sort']=="name")
  4181. echo (($_SESSION[COOKIENAME.'order']=="ASC") ? " <b>&uarr;</b>" : " <b>&darr;</b>");
  4182. echo "</td>";
  4183. echo "<td class='tdheader' colspan='10'>Action</td>";
  4184. echo "<td class='tdheader'>Records</td>";
  4185. echo "</tr>";
  4186. $totalRecords = 0;
  4187. for($i=0; $i<sizeof($result); $i++)
  4188. {
  4189. if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  4190. {
  4191. $records = $db->numRows($result[$i]['name']);
  4192. $totalRecords += $records;
  4193. $tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
  4194. $tdWithClassLeft = "<td class='td".($i%2 ? "1" : "2")."' style='text-align:left;'>";
  4195. if($result[$i]['type']=="table")
  4196. {
  4197. echo "<tr>";
  4198. echo $tdWithClassLeft;
  4199. echo "Table";
  4200. echo "</td>";
  4201. echo $tdWithClassLeft;
  4202. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=row_view'>".htmlencode($result[$i]['name'])."</a>";
  4203. echo "</td>";
  4204. echo $tdWithClass;
  4205. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=row_view'>Browse</a>";
  4206. echo "</td>";
  4207. echo $tdWithClass;
  4208. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=column_view'>Structure</a>";
  4209. echo "</td>";
  4210. echo $tdWithClass;
  4211. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_sql'>SQL</a>";
  4212. echo "</td>";
  4213. echo $tdWithClass;
  4214. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_search'>Search</a>";
  4215. echo "</td>";
  4216. echo $tdWithClass;
  4217. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=row_create'>Insert</a>";
  4218. echo "</td>";
  4219. echo $tdWithClass;
  4220. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_export'>Export</a>";
  4221. echo "</td>";
  4222. echo $tdWithClass;
  4223. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_import'>Import</a>";
  4224. echo "</td>";
  4225. echo $tdWithClass;
  4226. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_rename'>Rename</a>";
  4227. echo "</td>";
  4228. echo $tdWithClass;
  4229. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_empty' style='color:red;'>Empty</a>";
  4230. echo "</td>";
  4231. echo $tdWithClass;
  4232. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_drop' style='color:red;'>Drop</a>";
  4233. echo "</td>";
  4234. echo $tdWithClass;
  4235. echo $records;
  4236. echo "</td>";
  4237. echo "</tr>";
  4238. }
  4239. else
  4240. {
  4241. echo "<tr>";
  4242. echo $tdWithClassLeft;
  4243. echo "View";
  4244. echo "</td>";
  4245. echo $tdWithClassLeft;
  4246. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=row_view&amp;view=1'>".htmlencode($result[$i]['name'])."</a>";
  4247. echo "</td>";
  4248. echo $tdWithClass;
  4249. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=row_view&amp;view=1'>Browse</a>";
  4250. echo "</td>";
  4251. echo $tdWithClass;
  4252. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=column_view&amp;view=1'>Structure</a>";
  4253. echo "</td>";
  4254. echo $tdWithClass;
  4255. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_sql&amp;view=1'>SQL</a>";
  4256. echo "</td>";
  4257. echo $tdWithClass;
  4258. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_search&amp;view=1'>Search</a>";
  4259. echo "</td>";
  4260. echo $tdWithClass;
  4261. echo "";
  4262. echo "</td>";
  4263. echo $tdWithClass;
  4264. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=table_export&amp;view=1'>Export</a>";
  4265. echo "</td>";
  4266. echo $tdWithClass;
  4267. echo "";
  4268. echo "</td>";
  4269. echo $tdWithClass;
  4270. echo "";
  4271. echo "</td>";
  4272. echo $tdWithClass;
  4273. echo "";
  4274. echo "</td>";
  4275. echo $tdWithClass;
  4276. echo "<a href='".PAGE."?table=".urlencode($result[$i]['name'])."&amp;action=view_drop&amp;view=1' style='color:red;'>Drop</a>";
  4277. echo "</td>";
  4278. echo $tdWithClass;
  4279. echo $records;
  4280. echo "</td>";
  4281. echo "</tr>";
  4282. }
  4283. }
  4284. }
  4285. echo "<tr>";
  4286. echo "<td class='tdheader' colspan='12'>".sizeof($result)." total</td>";
  4287. echo "<td class='tdheader' colspan='1' style='text-align:right;'>".$totalRecords."</td>";
  4288. echo "</tr>";
  4289. echo "</table>";
  4290. echo "<br/>";
  4291. }
  4292. echo "<fieldset>";
  4293. echo "<legend><b>Create new table on database '".htmlencode($db->getName())."'</b></legend>";
  4294. echo "<form action='".PAGE."?action=table_create' method='post'>";
  4295. echo "Name: <input type='text' name='tablename' style='width:200px;'/> ";
  4296. echo "Number of Fields: <input type='text' name='tablefields' style='width:90px;'/> ";
  4297. echo "<input type='submit' name='createtable' value='Go' class='btn'/>";
  4298. echo "</form>";
  4299. echo "</fieldset>";
  4300. echo "<br/>";
  4301. echo "<fieldset>";
  4302. echo "<legend><b>Create new view on database '".htmlencode($db->getName())."'</b></legend>";
  4303. echo "<form action='".PAGE."?action=view_create&amp;confirm=1' method='post'>";
  4304. echo "Name: <input type='text' name='viewname' style='width:200px;'/> ";
  4305. echo "Select Statement ".helpLink("Writing a Select Statement for a New View").": <input type='text' name='select' style='width:400px;'/> ";
  4306. echo "<input type='submit' name='createtable' value='Go' class='btn'/>";
  4307. echo "</form>";
  4308. echo "</fieldset>";
  4309. }
  4310. else if($view=="sql") //database SQL editor
  4311. {
  4312. $isSelect = false;
  4313. if(isset($_POST['query']) && $_POST['query']!="")
  4314. {
  4315. $delimiter = $_POST['delimiter'];
  4316. $queryStr = stripslashes($_POST['queryval']);
  4317. $query = explode_sql($delimiter, $queryStr); //explode the query string into individual queries based on the delimiter
  4318. for($i=0; $i<sizeof($query); $i++) //iterate through the queries exploded by the delimiter
  4319. {
  4320. if(str_replace(" ", "", str_replace("\n", "", str_replace("\r", "", $query[$i])))!="") //make sure this query is not an empty string
  4321. {
  4322. $startTime = microtime(true);
  4323. if(strpos(strtolower($query[$i]), "select ")!==false)
  4324. {
  4325. $isSelect = true;
  4326. $result = $db->selectArray($query[$i], "assoc");
  4327. }
  4328. else
  4329. {
  4330. $isSelect = false;
  4331. $result = $db->query($query[$i]);
  4332. }
  4333. $endTime = microtime(true);
  4334. $time = round(($endTime - $startTime), 4);
  4335. echo "<div class='confirm'>";
  4336. echo "<b>";
  4337. // 22 August 2011: gkf fixed bugs 46, 51 and 52.
  4338. if($result)
  4339. {
  4340. if($isSelect)
  4341. {
  4342. $affected = sizeof($result);
  4343. echo "Showing ".$affected." row(s). ";
  4344. }
  4345. else
  4346. {
  4347. $affected = $db->getAffectedRows();
  4348. echo $affected." row(s) affected. ";
  4349. }
  4350. echo "(Query took ".$time." sec)</b><br/>";
  4351. }
  4352. else
  4353. {
  4354. echo "There is a problem with the syntax of your query ";
  4355. echo "(Query was not executed)</b><br/>";
  4356. }
  4357. echo "<span style='font-size:11px;'>".htmlencode($query[$i])."</span>";
  4358. echo "</div><br/>";
  4359. if($isSelect)
  4360. {
  4361. if(sizeof($result)>0)
  4362. {
  4363. $headers = array_keys($result[0]);
  4364. echo "<table border='0' cellpadding='2' cellspacing='1' class='viewTable'>";
  4365. echo "<tr>";
  4366. for($j=0; $j<sizeof($headers); $j++)
  4367. {
  4368. echo "<td class='tdheader'>";
  4369. echo htmlencode($headers[$j]);
  4370. echo "</td>";
  4371. }
  4372. echo "</tr>";
  4373. for($j=0; $j<sizeof($result); $j++)
  4374. {
  4375. $tdWithClass = "<td class='td".($j%2 ? "1" : "2")."'>";
  4376. echo "<tr>";
  4377. for($z=0; $z<sizeof($headers); $z++)
  4378. {
  4379. echo $tdWithClass;
  4380. echo htmlencode($result[$j][$headers[$z]]);
  4381. echo "</td>";
  4382. }
  4383. echo "</tr>";
  4384. }
  4385. echo "</table><br/><br/>";
  4386. }
  4387. }
  4388. }
  4389. }
  4390. }
  4391. else
  4392. {
  4393. $delimiter = ";";
  4394. $queryStr = "";
  4395. }
  4396. echo "<fieldset>";
  4397. echo "<legend><b>Run SQL query/queries on database '".htmlencode($db->getName())."'</b></legend>";
  4398. echo "<form action='".PAGE."?view=sql' method='post'>";
  4399. echo "<textarea style='width:100%; height:300px;' name='queryval'>".htmlencode($queryStr)."</textarea>";
  4400. echo "Delimiter <input type='text' name='delimiter' value='".htmlencode($delimiter)."' style='width:50px;'/> ";
  4401. echo "<input type='submit' name='query' value='Go' class='btn'/>";
  4402. echo "</form>";
  4403. echo "</fieldset>";
  4404. }
  4405. else if($view=="vacuum")
  4406. {
  4407. if(isset($_POST['vacuum']))
  4408. {
  4409. $query = "VACUUM";
  4410. $db->query($query);
  4411. echo "<div class='confirm'>";
  4412. echo "The database, '".htmlencode($db->getName())."', has been VACUUMed.";
  4413. echo "</div><br/>";
  4414. }
  4415. echo "<form method='post' action='".PAGE."?view=vacuum'>";
  4416. echo "Large databases sometimes need to be VACUUMed to reduce their footprint on the server. Click the button below to VACUUM the database, '".htmlencode($db->getName())."'.";
  4417. echo "<br/><br/>";
  4418. echo "<input type='submit' value='VACUUM' name='vacuum' class='btn'/>";
  4419. echo "</form>";
  4420. }
  4421. else if($view=="export")
  4422. {
  4423. echo "<form method='post' action='".PAGE."?view=export'>";
  4424. echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>Export</b></legend>";
  4425. echo "<select multiple='multiple' size='10' style='width:240px;' name='tables[]'>";
  4426. $query = "SELECT name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name";
  4427. $result = $db->selectArray($query);
  4428. for($i=0; $i<sizeof($result); $i++)
  4429. {
  4430. if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  4431. echo "<option value='".htmlencode($result[$i]['name'])."' selected='selected'>".htmlencode($result[$i]['name'])."</option>";
  4432. }
  4433. echo "</select>";
  4434. echo "<br/><br/>";
  4435. echo "<input type='radio' name='export_type' checked='checked' value='sql' onclick='toggleExports(\"sql\");'/> SQL";
  4436. echo "<br/><input type='radio' name='export_type' value='csv' onclick='toggleExports(\"csv\");'/> CSV";
  4437. echo "</fieldset>";
  4438. echo "<fieldset style='float:left; max-width:350px;' id='exportoptions_sql'><legend><b>Options</b></legend>";
  4439. echo "<input type='checkbox' checked='checked' name='structure'/> Export with structure ".helpLink("Export Structure to SQL File")."<br/>";
  4440. echo "<input type='checkbox' checked='checked' name='data'/> Export with data ".helpLink("Export Data to SQL File")."<br/>";
  4441. echo "<input type='checkbox' name='drop'/> Add DROP TABLE ".helpLink("Add Drop Table to Exported SQL File")."<br/>";
  4442. echo "<input type='checkbox' checked='checked' name='transaction'/> Add TRANSACTION ".helpLink("Add Transaction to Exported SQL File")."<br/>";
  4443. echo "<input type='checkbox' checked='checked' name='comments'/> Comments ".helpLink("Add Comments to Exported SQL File")."<br/>";
  4444. echo "</fieldset>";
  4445. echo "<fieldset style='float:left; max-width:350px; display:none;' id='exportoptions_csv'><legend><b>Options</b></legend>";
  4446. echo "<div style='float:left;'>Fields terminated by</div>";
  4447. echo "<input type='text' value=';' name='export_csv_fieldsterminated' style='float:right;'/>";
  4448. echo "<div style='clear:both;'>";
  4449. echo "<div style='float:left;'>Fields enclosed by</div>";
  4450. echo "<input type='text' value='\"' name='export_csv_fieldsenclosed' style='float:right;'/>";
  4451. echo "<div style='clear:both;'>";
  4452. echo "<div style='float:left;'>Fields escaped by</div>";
  4453. echo "<input type='text' value='\' name='export_csv_fieldsescaped' style='float:right;'/>";
  4454. echo "<div style='clear:both;'>";
  4455. echo "<div style='float:left;'>Replace NULL by</div>";
  4456. echo "<input type='text' value='NULL' name='export_csv_replacenull' style='float:right;'/>";
  4457. echo "<div style='clear:both;'>";
  4458. echo "<input type='checkbox' name='export_csv_crlf'/> Remove CRLF characters within fields<br/>";
  4459. echo "<input type='checkbox' checked='checked' name='export_csv_fieldnames'/> Put field names in first row";
  4460. echo "</fieldset>";
  4461. echo "<div style='clear:both;'></div>";
  4462. echo "<br/><br/>";
  4463. echo "<fieldset style='float:left;'><legend><b>Save As</b></legend>";
  4464. $file = pathinfo($db->getPath());
  4465. $name = $file['filename'];
  4466. echo "<input type='text' name='filename' value='".htmlencode($name).".".date("n-j-y").".dump' style='width:400px;'/> <input type='submit' name='export' value='Export' class='btn'/>";
  4467. echo "</fieldset>";
  4468. echo "</form>";
  4469. }
  4470. else if($view=="import")
  4471. {
  4472. if(isset($_POST['import']))
  4473. {
  4474. echo "<div class='confirm'>";
  4475. if($importSuccess===true)
  4476. echo "Import was successful.";
  4477. else
  4478. echo $importSuccess;
  4479. echo "</div><br/>";
  4480. }
  4481. echo "<form method='post' action='".PAGE."?view=import' enctype='multipart/form-data'>";
  4482. echo "<fieldset style='float:left; width:260px; margin-right:20px;'><legend><b>Import</b></legend>";
  4483. echo "<input type='radio' name='import_type' checked='checked' value='sql' onclick='toggleImports(\"sql\");'/> SQL";
  4484. echo "<br/><input type='radio' name='import_type' value='csv' onclick='toggleImports(\"csv\");'/> CSV";
  4485. echo "</fieldset>";
  4486. echo "<fieldset style='float:left; max-width:350px;' id='importoptions_sql'><legend><b>Options</b></legend>";
  4487. echo "No options";
  4488. echo "</fieldset>";
  4489. echo "<fieldset style='float:left; max-width:350px; display:none;' id='importoptions_csv'><legend><b>Options</b></legend>";
  4490. echo "<div style='float:left;'>Table that CSV pertains to</div>";
  4491. echo "<select name='single_table' style='float:right;'>";
  4492. $query = "SELECT name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name";
  4493. $result = $db->selectArray($query);
  4494. for($i=0; $i<sizeof($result); $i++)
  4495. {
  4496. if(substr($result[$i]['name'], 0, 7)!="sqlite_" && $result[$i]['name']!="")
  4497. echo "<option value='".htmlencode($result[$i]['name'])."'>".htmlencode($result[$i]['name'])."</option>";
  4498. }
  4499. echo "</select>";
  4500. echo "<div style='clear:both;'>";
  4501. echo "<div style='float:left;'>Fields terminated by</div>";
  4502. echo "<input type='text' value=';' name='import_csv_fieldsterminated' style='float:right;'/>";
  4503. echo "<div style='clear:both;'>";
  4504. echo "<div style='float:left;'>Fields enclosed by</div>";
  4505. echo "<input type='text' value='\"' name='import_csv_fieldsenclosed' style='float:right;'/>";
  4506. echo "<div style='clear:both;'>";
  4507. echo "<div style='float:left;'>Fields escaped by</div>";
  4508. echo "<input type='text' value='\' name='import_csv_fieldsescaped' style='float:right;'/>";
  4509. echo "<div style='clear:both;'>";
  4510. echo "<div style='float:left;'>NULL represented by</div>";
  4511. echo "<input type='text' value='NULL' name='import_csv_replacenull' style='float:right;'/>";
  4512. echo "<div style='clear:both;'>";
  4513. echo "<input type='checkbox' checked='checked' name='import_csv_fieldnames'/> Field names in first row";
  4514. echo "</fieldset>";
  4515. echo "<div style='clear:both;'></div>";
  4516. echo "<br/><br/>";
  4517. echo "<fieldset><legend><b>File to import</b></legend>";
  4518. echo "<input type='file' value='Choose File' name='file' style='background-color:transparent; border-style:none;'/> <input type='submit' value='Import' name='import' class='btn'/>";
  4519. echo "</fieldset>";
  4520. }
  4521. else if($view=="rename")
  4522. {
  4523. if(isset($dbexists))
  4524. {
  4525. echo "<div class='confirm'>";
  4526. if($oldpath==$newpath)
  4527. echo "Error: You didn't change the value dumbass ;-)";
  4528. else
  4529. echo "Error: A database of the name '".htmlencode($newpath)."' already exists.";
  4530. echo "</div><br/>";
  4531. }
  4532. if(isset($justrenamed))
  4533. {
  4534. echo "<div class='confirm'>";
  4535. echo "Database '".htmlencode($oldpath)."' has been renamed to '".htmlencode($newpath)."'.";
  4536. echo "</div><br/>";
  4537. }
  4538. echo "<form action='".PAGE."?view=rename&amp;database_rename=1' method='post'>";
  4539. echo "<input type='hidden' name='oldname' value='".htmlencode($db->getPath())."'/>";
  4540. echo "Rename database '".htmlencode($db->getPath())."' to <input type='text' name='newname' style='width:200px;' value='".htmlencode($db->getPath())."'/> <input type='submit' value='Rename' name='rename' class='btn'/>";
  4541. echo "</form>";
  4542. }
  4543. else if($view=="delete")
  4544. {
  4545. echo "<form action='".PAGE."?database_delete=1' method='post'>";
  4546. echo "<div class='confirm'>";
  4547. echo "Are you sure you want to delete the database '".htmlencode($db->getPath())."'?<br/><br/>";
  4548. echo "<input name='database_delete' value='".htmlencode($db->getPath())."' type='hidden'/>";
  4549. echo "<input type='submit' value='Confirm' class='btn'/> ";
  4550. echo "<a href='".PAGE."'>Cancel</a>";
  4551. echo "</div>";
  4552. echo "</form>";
  4553. }
  4554. echo "</div>";
  4555. }
  4556. echo "<br/>";
  4557. $endTimeTot = microtime(true); //get the current time at this point in the execution
  4558. $timeTot = round(($endTimeTot - $startTimeTot), 4); //calculate the total time for page load
  4559. echo "<span style='font-size:11px;'>Powered by <a href='http://code.google.com/p/phpliteadmin/' target='_blank' style='font-size:11px;'>".PROJECT."</a> | Page generated in ".$timeTot." seconds.</span>";
  4560. echo "</div>";
  4561. echo "</div>";
  4562. $db->close(); //close the database
  4563. }
  4564. echo "</body>";
  4565. echo "</html>";
  4566. ?>