<?php
require_once('inc/logincheck.php');
CommTool::checkIncludeByAdminPhp();
// $dbBackupUrl = $adminHttpPath."index.php?lang={$kuWebsiteEditVersionLanguage}&path=database&menu=db_action&action=onekeybackup";
//set_time_limit(0);    //
include_once("../inc/thirdpart/phpzip/pclzip.lib.php");


if("zipwebsite" == $action)
{
  $action = "onekeybackup";
  include("database/db_action.inc.php");
  $dbBackupUrl = $adminHttpPath."index.php?lang={$kuWebsiteEditVersionLanguage}&path=database&menu=db_action&action=onekeybackup";

  $fileWebsite = "databak/kuwebs_website_".date('YmdHis', time()).".zip";
  $archive2 = new PclZip($fileWebsite);
  $v_list = $archive2->create('../', PCLZIP_OPT_REMOVE_PATH, '../');
  promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=zipwebsite", $promptBackUpCopyFileSuccess.$fileWebsite);
  exit;
}
else if("zipupload" == $action)
{
  $fileUpload = "databak/kuwebs_upload_".date('YmdHis', time()).".zip";
  $archive1 = new PclZip($fileUpload);
  $v_list = $archive1->add('../upload/', PCLZIP_OPT_REMOVE_PATH, '../');
  $v_list = $archive1->add('../config/', PCLZIP_OPT_REMOVE_PATH, '../');
  promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=zipwebsite", $promptBackUpCopyFileSuccess.$fileUpload);
  exit;
}
else if("filedown" == $action)
{
  $fileext = fileExt($filename);
  if('zip' == $fileext)
  {
    file_down(KUWEBS_ADMIN_PATH.'databak/'.$filename);
  }
}
else if("delete" == $action)
{
  if($filename)
  {
    if('zip' == fileExt($filename))
    {
      @unlink(KUWEBS_ADMIN_PATH.'databak/'.$filename);
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=zipwebsite", $promptDeleteDataSuccess);
    }
  }
  else if($filenames)
  {
    if(is_array($filenames))
    {
      foreach($filenames as $key=>$fileName)
      {
        if('zip' == fileExt($fileName))
        {
          @unlink(KUWEBS_ADMIN_PATH.'databak/'.$fileName);
        }
      }
      promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=zipwebsite", $prompBatchDeleteSuccess);
    }
  }
  else
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=zipwebsite", $promptDeleteDatabaseRestoreSuccess);
  }
}
else if("upload" == $action)
{
  
  $sqlFile = $_FILES["uploadfile"]['name'];             
  $strAtPoint = strrpos($sqlFile, ".");             
  $lenght = strlen($sqlFile) - $strAtPoint;         
  $extName = strtolower(substr($sqlFile,$strAtPoint,$lenght)); 
  if('.zip' != $extName)
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=database&menu=restore", $promptUploadCorrectDatabaseFormat);
    exit;
  }
  $tempName = $_FILES["uploadfile"]['tmp_name'];
  if(@move_uploaded_file("$tempName", "databak/$sqlFile"))
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=zipwebsite", $promptUploadDatabaseSuccess);
    exit;
  }
  else
  {
    promptMessage("index.php?lang={$kuWebsiteEditVersionLanguage}&path=system&menu=zipwebsite", $promptUploadDatabaseFail);
    exit;
  }

}
else
{
  $info = array();
  $infoWebsite = array();
  $websiteFiles = glob(KUWEBS_ADMIN_PATH.'databak/kuwebs_website_*.zip');
  if(is_array($websiteFiles))
  {
    $prePre = '';
    foreach($websiteFiles as $key=>$file)
    {
      echo '1';
      if(preg_match("/(kuwebs_website_[0-9]{14})\.zip/i", basename($file), $num))
      {echo '2';
        $info['fileName'] = basename($file);
        $info['fileSize'] = round(filesize($file)/1024, 2);
        $info['makeTime'] = date('Y-m-d H:i:s', filemtime($file));
        $info['pre'] = $num[1];
        $info['number'] = $num[2];
        if(!$id) $prebgcolor = '#CFEFFF';
        if($info['pre'] == $prepre)
        {
         $info['bgcolor'] = $prebgcolor;
        }
        else
        {
         $info['bgcolor'] = $prebgcolor == '#CFEFFF' ? '#F1F3F5' : '#CFEFFF';
        }
        $prebgcolor = $info['bgcolor'];
        $prepre = $info['pre'];
        $infoWebsite[] = $info;
      }
    }
  }

  $infoUpload = array();
  $uploadFiles = glob(KUWEBS_ADMIN_PATH.'databak/kuwebs_upload_*.zip');
  if(is_array($uploadFiles))
  {
    $prePre = '';
    foreach($uploadFiles as $key=>$file)
    {
      if(preg_match("/(kuwebs_upload_[0-9]{14})\.zip/i", basename($file), $num))
      {
        $info['fileName'] = basename($file);
        $info['fileSize'] = round(filesize($file)/1024, 2);
        $info['makeTime'] = date('Y-m-d H:i:s', filemtime($file));
        $info['pre'] = $num[1];
        $info['number'] = $num[2];
        if(!$id) $prebgcolor = '#CFEFFF';
        if($info['pre'] == $prepre)
        {
         $info['bgcolor'] = $prebgcolor;
        }
        else
        {
         $info['bgcolor'] = $prebgcolor == '#CFEFFF' ? '#F1F3F5' : '#CFEFFF';
        }
        $prebgcolor = $info['bgcolor'];
        $prepre = $info['pre'];
        $infoUpload[] = $info;
      }
    }
  }
}

include_once template('zipwebsite.html', $adminTemplateName);
?>