Location: PHPKode > projects > as_admintool > asadmintool/as_admintool_restore.php
<?
/**
* @package as_admintool
* @desc as_admintool_restore.php - "SQL data restore" plugin for as_admintool.php
* @author Alexander Selifonov <as-hide@address.com>
* @copyright Alexander Selifonov 2007
* @link http://as-works.narod.ru/en/php/
* @version 1.000.000
* modified 03.03.2007 (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['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 = document.getElementById('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;
  document.getElementById('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) {
        document.getElementById('restoreresult_'+pageid).innerHTML ='<?=$as_iface['msg_wrongreply']?> '+spl[0];
      }
      else {
        document.getElementById('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;
}
</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']?> <select name='filename' style='width:240px'><OPTION value=''>---</OPTION>
<? 
for($ii=0; $ii<count($flst); $ii++) { echo "<OPTION VALUE=\"{$flst[$ii]}\" >{$flst[$ii]}</OPTION>\n"; } 
$r_height = max($lheight-80,60);
$r_width = max($lwidth-20,100);
?>
</select>
</td><td><button class='button' name='startrestore' onClick='AsAdm_RunRestore(<?=$pageid?>)'><?=$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;'>&nbsp;</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_admt_bckpfolder;
  $pageid = isset($parms['pageid'])? $parms['pageid'] : '1';
  $ret = "$pageid{|}";
  $b_alltables = empty($parms['_all_tables_'])? false:true;
  $bklist = array();
  $bckpfolder = $as_admt_bckpfolder;
  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); #, $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] : '');
}
?>
Return current item: as_admintool