<?php
/***********************************************************************
Copyright (c) 2011, Andrew V. Pleshakov (hide@address.com)
All rights reserved.
This file is part of SPA (SQLite PHP Admin)
See file LICENSE.txt for details
************************************************************************/
require_once('include.php');
if(!$current_db) {
header('Location: main.php');
die;
}
$object = $_GET['object'];
$action = $_GET['action'];
if(isset($_POST['prefver'])) { // create new database
$ver = intval($_POST['prefver']);
$loc = '';
if($err = SQLite23::createDB($current_db,$ver)) // public static method
$loc=urlencode("?err={$err}");
header("Location: dbase.php{$loc}");
die;
}
if(!$pla_db) {
header('Location: main.php');
die;
}
if ($action == 'drop_table') {
if(!$pla_db->query("DROP TABLE '$object'"))
raiseError($pla_db->error);
header("Location: index.php?bd=1");
die;
}
if ($action == 'empty_table') {
if(!$pla_db->query("DELETE FROM '$object'"))
raiseError($pla_db->error);
$loc = 'dbase.php';
if(isset($_SERVER['HTTP_REFERER'])) $loc = $_SERVER['HTTP_REFERER'];
header("Location: $loc");
die;
}
if ($action == 'drop_index') {
if(!$pla_db->query("DROP INDEX '$object'"))
raiseError($pla_db->error);
$_SESSION['lquery'] = $pla_db->lastQuery();
$loc = 'dbase.php';
if(isset($_SERVER['HTTP_REFERER'])) $loc = $_SERVER['HTTP_REFERER'];
header("Location: $loc");
die;
}
if ($action == 'new_index') {
if(!$object)
raiseError('You did not specify index name');
$unq = $col = '';
if(isset($_GET['unique'])) $unq = 'UNIQUE';
if(isset($_GET['column']))
$col = implode('","',$_GET['column']);
else
raiseError('You did not specify column name(s) on which to create index');
if(!$pla_db->query("CREATE $unq INDEX '$object' ON '{$_GET['table']}'(\"$col\")"))
raiseError($pla_db->error);
$_SESSION['lquery'] = $pla_db->lastQuery();
$loc = 'dbase.php';
if(isset($_SERVER['HTTP_REFERER'])) $loc = $_SERVER['HTTP_REFERER'];
header("Location: $loc");
die;
}
if ($action == 'vacuum') {
$pla_db->vacuum();
header("Location: dbase.php");
die;
}
if ($action == 'schema') {
$cnt = '';
$pla_db->query('SELECT sql FROM SQLITE_MASTER');
while($row = $pla_db->fetchArray())
$cnt .= $row[0]."\n";
$tar['content'] = $cnt;
die(design_render('db/schema',$tar));
}
if ($action == 'drop') {// DROP SQLite database (just deleting a file)
$pla_db->close();
unset($pla_db);
if(@unlink($current_db)) { // remove an entry from alias CFG file
$trk = ReadAliasCFG();
$j = count($trk);
for($i=0;$i<=$j;$i++) {
if($i == $j) { // this is new PATH!
raiseError("Bad path='$current_db'!",'File error');
}
$row = explode('|',$trk[$i],3);
if($row[1] == $current_db) break;
}
unset($trk[$i]);
SaveAliasCFG($trk);
}
else raiseError("Can not delete file $current_db",'File error');
$_SESSION['currentdb'] = '';
header("Location: main.php");
die;
}
if(isset($_POST['move'])) {
$newname = trim($_POST['mvdbname']);
if(file_exists($newname))
raiseError("File $newname already exists",'File error');
$pla_db->close();
unset($pla_db);
if(@rename($current_db,$newname)) {
$trk = ReadAliasCFG();
$j = count($trk);
for($i=0;$i<$j;$i++) {
$row = explode('|',$trk[$i],3);
if($row[1] == $current_db) break;
}
if($i < $j) $trk[$i] = "{$row[0]}|{$newname}|{$row[2]}";
SaveAliasCFG($trk);
$_SESSION['currentdb'] = $newname;
}
else raiseError("Could not rename $current_db to $newname",'File error');
header('Location: index.php?bd=1');
die;
}
if(isset($_POST['copy'])) {
$newname = trim($_POST['cpdbname']);
if(file_exists($newname))
raiseError("File $newname already exists",'File error');
if(@copy($current_db,$newname)) {
$trk = ReadAliasCFG();
$j = count($trk);
for($i=0;$i<$j;$i++) {
$row = explode('|',$trk[$i],3);
if($row[1] == $current_db) break;
}
if($i < $j) $trk[$j] = "{$row[0]}(copy)|{$newname}|{$row[2]}";
SaveAliasCFG($trk);
}
else raiseError("Could not copy $current_db to $newname",'File error');
header('Location: index.php?bd=1');
die;
}