<?
/**
* @package as_admintool
* @desc as_admintool_restore.php - "SQL data restore" plugin for as_admintool.php
* @author Alexander Selifonov <as-works@narod.ru>
* @copyright Alexander Selifonov 2007
* @link http://as-works.narod.ru/en/php/
* @version 1.002.010
* modified 28.02.2008 (dd.mm.yyyy)
* Read "as_admintool.htm" for detailed instructions
*/
require_once('as_dbutils.php'); // DB access wrapper class
define('ASADM_RESTORE','restore'); // ID type for this module
define('ASADM_RESTORE_TPR',6); // Tables Per Row in restore form
# $as_admt_bckpfolder - default folder with backup files, with ending '/'
if(!isset($as_admt_bckpfolder)) $as_admt_bckpfolder = 'backup/';
# mandatory string - registering plugin
CAsAdminTool::RegisterPlugin(ASADM_RESTORE,'AsAdm_Restore_Form','AsAdm_Restore_Exec');
// interface localization here !
if(empty($as_iface['startrestore'])) $as_iface['startrestore'] ='Start restore';
if(empty($as_iface['restoreresult'])) $as_iface['restoreresult'] ='Restore result';
if(empty($as_iface['alltables'])) $as_iface['alltables'] ='All tables';
if(empty($as_iface['prompt_rsttablist'])) $as_iface['prompt_rsttablist'] ='Restore only : (empty-all tables from backup)';
if(empty($as_iface['choosefile'])) $as_iface['choosefile'] ='Please choose a file to restore from';
if(empty($as_iface['confirm_restore'])) $as_iface['confirm_restore'] ='All current contents in restored tables will be destroyed. Continue ?';
# if(empty($as_iface['no_backup_files'])) $as_iface['no_backup_files'] ='No backup files in the folder !';
# AsAdm_Restore_Form - function for drawing "backing up" client interface page
# first par $pginfo is array: [0] - pageid, [1] - parent layer width, [2] - parent layer height
function AsAdm_Restore_Form($pginfo,$usrparam=false, $dbname=false, $usrparam3=false) {
global $as_dbengine, $as_iface, $as_cssclass;
$pageid=isset($pginfo[0])? $pginfo[0]: 0;
$lwidth = isset($pginfo[1])? $pginfo[1]: 800;
$lheight = isset($pginfo[2])? $pginfo[2]: 600;
$bckpfolder = ($usrparam===false)? $as_admt_bckpfolder : $usrparam;
$self = $_SERVER['PHP_SELF'];
static $rest_js_drawn = false;
if(!$rest_js_drawn) { #<3> draw only once !!!
$rest_js_drawn = true;
?>
<script language='javascript'>
var ajax_restorebusy = false;
function AsAdm_RunRestore(pageid) {
if (ajax_restorebusy) return;
fm = asGetObj('asadt_restore_'+pageid);
if(fm.filename.selectedIndex<=0) { alert('<?=$as_iface['choosefile']?>'); return false; }
if(!confirm('<?=$as_iface['confirm_restore']?>')) return false;
var xmlreq = NewXMLHttpRequest();
if(!xmlreq) return false;
ajax_restorebusy = true;
asGetObj('restoreresult_'+pageid).innerHTML = '<?=$as_iface['msg_waiting']?>';
xmlreq.onreadystatechange= function() { //<3>
if (xmlreq.readyState == 4) { //<3A>
// alert(xmlreq.responseText); //debug
var spl = xmlreq.responseText.split("{|}");
delete xmlreq;
ajax_restorebusy = false;
if(spl.length < 2) {
asGetObj('restoreresult_'+pageid).innerHTML ='<?=$as_iface['msg_wrongreply']?> '+spl[0];
}
else {
asGetObj('restoreresult_'+pageid).innerHTML = spl[1];
} //<4>
} //<3A>
} //<3>
xmlreq.open('POST','<?=$self?>',true);
xmlreq.setRequestHeader("Content-Type", postcont);
params = 'adm_action_type=restore&pageid=' + pageid + '&'+ComputeParamString('asadt_restore_'+pageid);
xmlreq.send(params);
return false;
}
function ChangeBckpFile(obj,pageid) {
var btnobj = asGetObj('btnrestore'+pageid);
btnobj.disabled = (obj.selectedIndex<=0);
return false;
}
function RefreshBckpFiles(pageid) {
if (ajax_restorebusy) return;
var selobj = asGetObj("restore_fname"+pageid);
selobj.options.length=1; selobj.selectedIndex=0;
asGetObj('btnrestore'+pageid).disabled = true;
var xmlreq = NewXMLHttpRequest();
if(!xmlreq) return false;
ajax_restorebusy = true;
asGetObj('restoreresult_'+pageid).innerHTML = "<?=$as_iface['msg_waiting']?>";
xmlreq.onreadystatechange= function() { //<3>
if (xmlreq.readyState == 4) { //<3A>
// alert(xmlreq.responseText); //debug
var spl = xmlreq.responseText.split("{|}");
delete xmlreq;
ajax_restorebusy = false;
if(spl[0]!=pageid) {
asGetObj('restoreresult_'+pageid).innerHTML ='<?=$as_iface['msg_wrongreply']?> '+spl[0];
}
else {
asGetObj('restoreresult_'+pageid).innerHTML = "";
for(kf=1;kf<spl.length;kf++) {
selobj.options[kf] = new Option(spl[kf],spl[kf]);
}
} //<4>
} //<3A>
} //<3>
xmlreq.open("POST","<?=$self?>",true);
xmlreq.setRequestHeader("Content-Type", postcont);
params = 'adm_action_type=restore&adm_subaction=refreshfiles&pageid='+pageid;
xmlreq.send(params);
return false;
}
</script>
<?
}
$flst = array();
if (($handle = @opendir($bckpfolder))) {
while (false !== ($file = readdir($handle))) {
if (is_file($bckpfolder.$file)){
$ext = strtolower(AsAdm_GetExt($file));
if($ext == 'gz' || $ext =='xml') $flst[] = $file;
}
}
closedir($handle);
}
// if(count($flst)<1) { echo $bckpfolder .' : '.$as_iface['no_backup_files']; return; }
?>
<table id='asadt_tbl_<?=$pageid?>'>
<tr><form name='asadt_restore_<?=$pageid?>'>
<input type='hidden' name='backupfolder' value='<?=$bckpfolder?>'>
<?
if(!empty($dbname)) echo "<input type=hidden name='_dbname_' value='$dbname'>";
?>
<tr><td><?=$as_iface['choosefile']?></td><td> <select name='filename' id='restore_fname<?=$pageid?>' style='width:240px' onChange='return ChangeBckpFile(this,"<?=$pageid?>")'><OPTION value='' selected>---</OPTION>
<?
for($ii=0; $ii<count($flst); $ii++) { echo "<OPTION VALUE=\"{$flst[$ii]}\" >{$flst[$ii]}</OPTION>\n"; }
$r_height = max($lheight-96,60);
$r_width = max($lwidth-20,100);
?>
</select>
</td><td><button id='btnrefresh<?=$pageid?> class='' onClick="return RefreshBckpFiles('<?=$pageid?>')">Refresh</button></td></tr>
<tr><td><?=$as_iface['prompt_rsttablist']?></td><td> <input type='text' name='_only_tables_' class='{$as_cssclass['textfield']}' style='width:240'></td><td>
<button class='button' name="startrestore" id="btnrestore<?=$pageid?>" onClick='AsAdm_RunRestore(<?=$pageid?>)' disabled ><?=$as_iface['startrestore']?></button>
</td></tr></form></table>
<br><div align=center>
<table width='98%'>
<tr><td><?=$as_iface['restoreresult']?></td></tr>
<tr><td><div id='restoreresult_<?=$pageid?>' class='<?=$as_cssclass['resultarea']?>' style='overflow:auto; height:<?=$r_height?>px; width:<?=$r_width?>px;'> </div></td></tr>
</tr>
</table>
</div>
<?
}
// # AsAdm_Restore_Exec - function that executes restore and returns result string
function AsAdm_Restore_Exec($parms) {
global $as_dbengine, $as_iface, $as_cssclass, $as_admt_bckpfolder;
$pageid = isset($parms['pageid'])? $parms['pageid'] : '1';
$to_backuplist = empty($parms['_only_tables_'])? '' : split('[/, ;]',$parms['_only_tables_']);
$bckpfolder = $as_admt_bckpfolder;
$mode = empty($parms['adm_subaction'])? '': $parms['adm_subaction'];
if($mode=='refreshfiles') { #<2> client requested file list refresh
$ret = "$pageid";
if (($handle = @opendir($bckpfolder))) {
while (false !== ($file = readdir($handle))) {
if (is_file($bckpfolder.$file)){
$ext = strtolower(AsAdm_GetExt($file));
if($ext == 'gz' || $ext =='xml') $ret .="{|}$file";
}
}
closedir($handle);
}
return $ret;
} #<2>
$ret = "$pageid{|}";
$b_alltables = empty($parms['_all_tables_'])? false:true;
$bklist = array();
if(isset($parms['backupfolder'])) $bckpfolder = $parms['backupfolder'];
$fname = isset($parms['filename']) ? $parms['filename']: '';
$dbname = isset($parms['_dbname_']) ? $parms['_dbname_']: '';
if($fname==='' || !is_file($bckpfolder.$fname)) { echo "$pageid{|}Wrong call (no file or file not exist)"; exit; }
// $bklist has all tablenames to restore, let's start...
// $as_dbengine->SetVerbose(true);
if(!empty($dbname)) $as_dbengine->select_db($dbname);
ob_start(); // intercept all html output!
$result = $as_dbengine->BckpRestoreTables($bckpfolder.$fname, 1, $to_backuplist); #, $tlist);
if($result===false) $result = $as_dbengine->GetErrorMessage();
else $result = "<br>Restored : $result";
$ret = "$pageid{|}".ob_get_contents()." $result <br>";
ob_end_clean();
return $ret;
}
function AsAdm_GetExt($fname) { // returns extension from filename
$exts = split("[/\\.]", $fname);
$cnt = count($exts)-1;
return (isset($exts[$cnt])? $exts[$cnt] : '');
}
?>
|