2010-12-02 23:34:41 +00:00
|
|
|
<?php
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-08-10 17:11:55 -04:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2010-12-02 23:34:41 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
|
|
|
|
*
|
2010-12-02 23:34:41 +00:00
|
|
|
* Database Maintenance class
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-01-14 11:51:34 +00:00
|
|
|
*
|
|
|
|
|
* @package gulliver.system
|
2010-12-02 23:34:41 +00:00
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
class DataBaseMaintenance
|
2010-12-02 23:34:41 +00:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
private $host = null;
|
|
|
|
|
private $user = null;
|
|
|
|
|
private $passwd = null;
|
2010-12-02 23:34:41 +00:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
private $connect = null;
|
|
|
|
|
private $dbName = null;
|
2012-10-18 10:54:46 -04:00
|
|
|
public $result;
|
2018-02-01 13:06:32 +00:00
|
|
|
protected $tmpDir = null;
|
2012-10-18 10:54:46 -04:00
|
|
|
protected $outfile;
|
|
|
|
|
protected $infile;
|
2017-08-08 09:53:00 -04:00
|
|
|
protected $isWindows;
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* DataBaseMaintenance constructor.
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $host
|
|
|
|
|
* @param string $user
|
|
|
|
|
* @param string $passwd
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function __construct($host = null, $user = null, $passwd = null)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->tmpDir = './';
|
2018-02-01 13:06:32 +00:00
|
|
|
$this->setConnection(null);
|
|
|
|
|
$this->setDbName(null);
|
2017-08-08 09:53:00 -04:00
|
|
|
$this->isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
|
2018-02-01 13:06:32 +00:00
|
|
|
$this->setUser($user);
|
|
|
|
|
$this->setHost($host);
|
|
|
|
|
$this->setPasswd($passwd);
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
/**
|
|
|
|
|
* setUser
|
|
|
|
|
*
|
|
|
|
|
* @param string $user
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function setUser($user)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->user = $user;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Set Password
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
|
|
|
|
* @param string $passwd
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function setPasswd($passwd)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->passwd = $passwd;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Set Host
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
|
|
|
|
* @param string $host
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function setHost($host)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->host = $host;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Set TempDir
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
|
|
|
|
* @param string $tmpDir
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function setTempDir($tmpDir)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
|
|
|
|
$this->tmpDir = $tmpDir;
|
2017-12-04 13:25:35 +00:00
|
|
|
if (!file_exists($tmpDir)) {
|
|
|
|
|
mkdir($this->tmpDir);
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Set Db Name
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param $dbName
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function setDbName($dbName)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$this->dbName = $dbName;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Set Connection
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param $name
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function setConnection($name)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$this->connect = 'DB_' . $name;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Get User
|
|
|
|
|
* @return string
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function getUser()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return $this->user;
|
|
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Get Password
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getPasswd()
|
|
|
|
|
{
|
|
|
|
|
return $this->passwd;
|
|
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
/**
|
|
|
|
|
* Get Host
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHost()
|
|
|
|
|
{
|
|
|
|
|
return $this->host;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Get Name Connection
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return string
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function getConnect()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return $this->connect;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* get TempDir
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return $this->tmpDir
|
|
|
|
|
*/
|
|
|
|
|
public function getTempDir()
|
|
|
|
|
{
|
|
|
|
|
return $this->tmpDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Name DB
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return string
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function getDbName()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return $this->dbName;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Connect to DB
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $dbName
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @throws Exception
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function connect($dbName)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
$this->setConnection($dbName);
|
|
|
|
|
$this->setDbName($dbName);
|
|
|
|
|
InstallerModule::setNewConnection(
|
|
|
|
|
$this->getConnect(),
|
|
|
|
|
$this->getHost(),
|
|
|
|
|
$this->getUser(),
|
|
|
|
|
$this->getPasswd(),
|
|
|
|
|
$this->getDbName(),
|
|
|
|
|
'');
|
|
|
|
|
|
|
|
|
|
DB::connection($this->getConnect())
|
|
|
|
|
->statement("SET NAMES 'utf8'");
|
|
|
|
|
DB::connection($this->getConnect())
|
|
|
|
|
->statement('SET FOREIGN_KEY_CHECKS=0');
|
|
|
|
|
|
|
|
|
|
} catch (QueryException $exception) {
|
|
|
|
|
throw new Exception("Couldn't connect to host {$this->getHost()} with user {$this->getUser()}" . $exception->getMessage());
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Query
|
|
|
|
|
*
|
|
|
|
|
* @param string $sql
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function query($sql)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
$result = DB::connection($this->getConnect())
|
|
|
|
|
->select($sql);
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
} catch (QueryException $exception) {
|
|
|
|
|
throw new Exception("Couldn't connect to host {$this->getHost()} with user {$this->getUser()}" . $exception->getMessage());
|
|
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* get Tables List
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2017-12-04 13:25:35 +00:00
|
|
|
* @return array
|
2018-02-01 13:06:32 +00:00
|
|
|
* @throws Exception
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getTablesList()
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
return $this->query('SHOW TABLES');
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* dumpData
|
|
|
|
|
*
|
|
|
|
|
* @param string $table
|
|
|
|
|
*
|
|
|
|
|
* @return boolean true or false
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function dumpData($table)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2020-08-10 17:11:55 -04:00
|
|
|
$sql = "";
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
$this->outfile = $this->tmpDir . $table . '.dump';
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
//if the file exists delete it
|
|
|
|
|
if (is_file($this->outfile)) {
|
|
|
|
|
@unlink($this->outfile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT * INTO OUTFILE '{$this->outfile}' FIELDS TERMINATED BY '\t|\t' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\t\t\r\r\n' FROM $table";
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
DB::connection($this->getConnect())->raw($sql);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
} catch (QueryException $exception) {
|
2020-08-10 17:11:55 -04:00
|
|
|
$message = $exception->getMessage();
|
|
|
|
|
$context = [
|
|
|
|
|
'sql' => $sql
|
|
|
|
|
];
|
|
|
|
|
Log::channel(':MysqlCron')->error($message, Bootstrap::context($context));
|
|
|
|
|
G::outRes($message . "\n");
|
2012-10-18 10:54:46 -04:00
|
|
|
return false;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* restoreData
|
|
|
|
|
*
|
|
|
|
|
* @param string $backupFile
|
|
|
|
|
*
|
|
|
|
|
* @return boolean true or false
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function restoreData($backupFile)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2020-08-10 17:11:55 -04:00
|
|
|
$sql = "";
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
$tableName = str_replace('.dump', '', basename($backupFile));
|
|
|
|
|
$sql = "LOAD DATA INFILE '$backupFile' INTO TABLE $tableName FIELDS TERMINATED BY '\t|\t' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\t\t\r\r\n'";
|
|
|
|
|
DB::connection($this->getConnect())->raw($sql);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (QueryException $exception) {
|
2020-08-10 17:11:55 -04:00
|
|
|
$message = $exception->getMessage();
|
|
|
|
|
$context = [
|
|
|
|
|
'sql' => $sql
|
|
|
|
|
];
|
|
|
|
|
Log::channel(':MysqlCron')->error($message, Bootstrap::context($context));
|
|
|
|
|
G::outRes($message . "\n");
|
2012-10-18 10:54:46 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* restoreAllData
|
|
|
|
|
*
|
|
|
|
|
* @param string $type default value null
|
|
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @throws Exception
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function restoreAllData($type = null)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
foreach ($this->getTablesList() as $table) {
|
|
|
|
|
if (isset($type) && $type === 'sql') {
|
|
|
|
|
$this->infile = $this->tmpDir . $table . '.sql';
|
2017-12-04 13:25:35 +00:00
|
|
|
if (is_file($this->infile)) {
|
|
|
|
|
$queries = $this->restoreFromSql($this->infile, true);
|
|
|
|
|
if (!isset($queries)) {
|
2018-02-01 13:06:32 +00:00
|
|
|
$queries = 'unknown';
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2017-12-04 13:25:35 +00:00
|
|
|
printf("%-59s%20s", "Restored table $table", "$queries queries\n");
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2018-02-01 13:06:32 +00:00
|
|
|
$this->infile = $this->tmpDir . $table . '.dump';
|
2017-12-04 13:25:35 +00:00
|
|
|
if (is_file($this->infile)) {
|
|
|
|
|
$this->restoreData($this->infile);
|
|
|
|
|
printf("%20s %s %s\n", 'Restoring data from ', $this->infile, " in table $table");
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Create DB
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
|
|
|
|
* @param string $dbname
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param boolean $drop
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function createDb($dbname, $drop = false)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
if ($drop) {
|
|
|
|
|
DB::connection($this->getConnect())->statement("DROP DATABASE IF EXISTS $dbname");
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2011-01-14 23:11:13 +00:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
DB::connection($this->getConnect())->statement("CREATE DATABASE IF NOT EXISTS $dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
return true;
|
|
|
|
|
} catch (QueryException $exception) {
|
|
|
|
|
throw new Exception($exception->getMessage());
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
/**
|
|
|
|
|
* backupDataBaseSchema
|
|
|
|
|
*
|
|
|
|
|
* @param string $outfile
|
|
|
|
|
*
|
|
|
|
|
* @return none
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function backupDataBase($outfile)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
$password = escapeshellarg($this->getPasswd());
|
2017-12-04 13:25:35 +00:00
|
|
|
|
|
|
|
|
//On Windows, escapeshellarg() instead replaces percent signs, exclamation
|
|
|
|
|
//marks (delayed variable substitution) and double quotes with spaces and
|
2017-08-08 09:53:00 -04:00
|
|
|
//adds double quotes around the string.
|
|
|
|
|
//See: http://php.net/manual/en/function.escapeshellarg.php
|
|
|
|
|
if ($this->isWindows) {
|
2018-02-01 13:06:32 +00:00
|
|
|
$password = $this->escapeshellargCustom($this->getPasswd());
|
2017-08-08 09:53:00 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
$aHost = explode(':', $this->getHost());
|
2016-03-14 15:31:01 -04:00
|
|
|
$dbHost = $aHost[0];
|
|
|
|
|
if (isset($aHost[1])) {
|
|
|
|
|
$dbPort = $aHost[1];
|
|
|
|
|
$command = 'mysqldump'
|
2018-02-01 13:06:32 +00:00
|
|
|
. ' --user=' . $this->getUser()
|
2017-08-08 13:45:12 -04:00
|
|
|
. ' --password=' . $password
|
2017-08-02 16:06:56 -04:00
|
|
|
. ' --host=' . $dbHost
|
|
|
|
|
. ' --port=' . $dbPort
|
|
|
|
|
. ' --opt'
|
|
|
|
|
. ' --skip-comments'
|
2022-10-26 16:31:34 -04:00
|
|
|
. ' --no-tablespaces'
|
2018-02-01 13:06:32 +00:00
|
|
|
. ' ' . $this->getDbName()
|
2017-08-02 16:06:56 -04:00
|
|
|
. ' > ' . $outfile;
|
2016-03-14 15:31:01 -04:00
|
|
|
} else {
|
|
|
|
|
$command = 'mysqldump'
|
2017-08-02 16:06:56 -04:00
|
|
|
. ' --host=' . $dbHost
|
2018-02-01 13:06:32 +00:00
|
|
|
. ' --user=' . $this->getUser()
|
2017-08-02 16:06:56 -04:00
|
|
|
. ' --opt'
|
|
|
|
|
. ' --skip-comments'
|
2022-10-26 16:31:34 -04:00
|
|
|
. ' --no-tablespaces'
|
2017-08-08 13:45:12 -04:00
|
|
|
. ' --password=' . $password
|
2018-02-01 13:06:32 +00:00
|
|
|
. ' ' . $this->getDbName()
|
2017-08-02 16:06:56 -04:00
|
|
|
. ' > ' . $outfile;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2016-03-14 15:31:01 -04:00
|
|
|
shell_exec($command);
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
2017-08-08 09:53:00 -04:00
|
|
|
/**
|
|
|
|
|
* string escapeshellargCustom ( string $arg , character $quotes)
|
2017-12-04 13:25:35 +00:00
|
|
|
*
|
|
|
|
|
* escapeshellarg() adds single quotes around a string and quotes/escapes any
|
|
|
|
|
* existing single quotes allowing you to pass a string directly to a shell
|
|
|
|
|
* function and having it be treated as a single safe argument. This function
|
|
|
|
|
* should be used to escape individual arguments to shell functions coming
|
|
|
|
|
* from user input. The shell functions include exec(), system() and the
|
2017-08-08 09:53:00 -04:00
|
|
|
* backtick operator.
|
2017-12-04 13:25:35 +00:00
|
|
|
*
|
|
|
|
|
* On Windows, escapeshellarg() instead replaces percent signs, exclamation
|
|
|
|
|
* marks (delayed variable substitution) and double quotes with spaces and
|
2017-08-08 09:53:00 -04:00
|
|
|
* adds double quotes around the string.
|
|
|
|
|
*/
|
|
|
|
|
private function escapeshellargCustom($string, $quotes = "")
|
|
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
if ($quotes === '') {
|
2017-08-08 09:53:00 -04:00
|
|
|
$quotes = $this->isWindows ? "\"" : "'";
|
|
|
|
|
}
|
|
|
|
|
$n = strlen($string);
|
|
|
|
|
$special = ["!", "%", "\""];
|
2018-02-01 13:06:32 +00:00
|
|
|
$substring = '';
|
2017-08-08 09:53:00 -04:00
|
|
|
$result1 = [];
|
|
|
|
|
$result2 = [];
|
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
|
|
|
if (in_array($string[$i], $special, true)) {
|
|
|
|
|
$result2[] = $string[$i];
|
|
|
|
|
$result1[] = $substring;
|
2018-02-01 13:06:32 +00:00
|
|
|
$substring = '';
|
2017-08-08 09:53:00 -04:00
|
|
|
} else {
|
2018-02-01 13:06:32 +00:00
|
|
|
$substring .= $string[$i];
|
2017-08-08 09:53:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$result1[] = $substring;
|
|
|
|
|
//Rebuild the password string
|
|
|
|
|
$n = count($result1);
|
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
|
|
|
$result1[$i] = trim(escapeshellarg($result1[$i]), $quotes);
|
|
|
|
|
if (isset($result2[$i])) {
|
2018-02-01 13:06:32 +00:00
|
|
|
$result1[$i] .= $result2[$i];
|
2017-08-08 09:53:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//add simple quotes, see escapeshellarg function
|
2018-02-01 13:06:32 +00:00
|
|
|
$newString = $quotes . implode('', $result1) . $quotes;
|
2017-08-08 09:53:00 -04:00
|
|
|
return $newString;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
/**
|
2018-02-01 13:06:32 +00:00
|
|
|
* Restore from sql
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2018-02-01 13:06:32 +00:00
|
|
|
* @param string $sqlFile
|
|
|
|
|
* @param string $type
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
|
|
|
|
* @return boolean false or true
|
2018-02-01 13:06:32 +00:00
|
|
|
* @throws Exception
|
2012-10-18 10:54:46 -04:00
|
|
|
*/
|
2018-02-01 13:06:32 +00:00
|
|
|
public function restoreFromSql($sqlFile, $type = 'file')
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
if ($type == 'file' && !is_file($sqlFile)) {
|
|
|
|
|
throw new Exception("the $sqlFile doesn't exist!");
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
$metaFile = str_replace('.sql', '.meta', $sqlFile);
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
$queries = 0;
|
|
|
|
|
|
2017-12-04 13:25:35 +00:00
|
|
|
if (is_file($metaFile)) {
|
2012-10-18 10:54:46 -04:00
|
|
|
echo "Using $metaFile as metadata.\n";
|
2018-02-01 13:06:32 +00:00
|
|
|
$fp = fopen($sqlFile, 'rb');
|
2017-12-04 13:25:35 +00:00
|
|
|
$fpmd = fopen($metaFile, 'r');
|
|
|
|
|
while ($offset = fgets($fpmd, 1024)) {
|
|
|
|
|
$buffer = intval($offset); //reading the size of $oData
|
|
|
|
|
$query = fread($fp, $buffer); //reading string $oData
|
2018-02-01 13:06:32 +00:00
|
|
|
$queries++;
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
DB::connection($this->getConnect())->raw($query);
|
|
|
|
|
} catch (QueryException $exception) {
|
|
|
|
|
$varRes = $exception->getMessage() . "\n";
|
2017-12-04 13:25:35 +00:00
|
|
|
G::outRes($varRes);
|
2016-08-04 14:56:58 -04:00
|
|
|
$varRes = "==>" . $query . "<==\n";
|
2017-12-04 13:25:35 +00:00
|
|
|
G::outRes($varRes);
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
2010-12-02 23:34:41 +00:00
|
|
|
} else {
|
2012-10-18 10:54:46 -04:00
|
|
|
$queries = null;
|
|
|
|
|
try {
|
2018-02-01 13:06:32 +00:00
|
|
|
|
2017-12-04 13:25:35 +00:00
|
|
|
if ($type === 'file') {
|
2018-02-01 13:06:32 +00:00
|
|
|
$query = file_get_contents($sqlFile);
|
2017-12-04 13:25:35 +00:00
|
|
|
} elseif ($type === 'string') {
|
2018-02-01 13:06:32 +00:00
|
|
|
$query = $sqlFile;
|
2012-10-18 10:54:46 -04:00
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
if (empty(trim($query))) {
|
2012-10-18 10:54:46 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
DB::connection($this->getConnect())->raw($query);
|
|
|
|
|
} catch (QueryException $exception) {
|
|
|
|
|
throw new Exception($exception->getMessage());
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
echo $query;
|
2016-08-04 14:56:58 -04:00
|
|
|
$token = strtotime("now");
|
|
|
|
|
PMException::registerErrorLog($e, $token);
|
2017-12-04 13:25:35 +00:00
|
|
|
G::outRes(G::LoadTranslation("ID_EXCEPTION_LOG_INTERFAZ", array($token)));
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
|
|
|
|
return $queries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* getSchemaFromTable
|
|
|
|
|
*
|
|
|
|
|
* @param string $tablename
|
|
|
|
|
*
|
|
|
|
|
* @return string $tableSchema
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function getSchemaFromTable($tablename)
|
2016-08-18 17:23:40 -04:00
|
|
|
{
|
2018-02-01 13:06:32 +00:00
|
|
|
try {
|
|
|
|
|
$tableSchema = '';
|
|
|
|
|
$result = DB::connection($this->getConnect())->select("show create table `$tablename`");
|
|
|
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
|
$tableSchema = $result['Create Table'] . ";\n\n";
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
|
|
|
|
|
} catch (QueryException $exception) {
|
|
|
|
|
G::outRes($exception->getMessage());
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2018-02-01 13:06:32 +00:00
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
return $tableSchema;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* removeCommentsIntoString
|
|
|
|
|
*
|
|
|
|
|
* @param string $str
|
|
|
|
|
*
|
|
|
|
|
* @return string $str
|
|
|
|
|
*/
|
2017-12-04 13:25:35 +00:00
|
|
|
public function removeCommentsIntoString($str)
|
2012-10-18 10:54:46 -04:00
|
|
|
{
|
2017-12-04 13:25:35 +00:00
|
|
|
$str = preg_replace('/\/\*[\w\W]*\*\//', '', $str);
|
|
|
|
|
$str = preg_replace("/--[\w\W]*\\n/", '', $str);
|
|
|
|
|
$str = preg_replace("/\/\/[\w\W]*\\n/", '', $str);
|
|
|
|
|
$str = preg_replace("/\#[\w\W]*\\n/", '', $str);
|
2012-10-18 10:54:46 -04:00
|
|
|
return $str;
|
2010-12-02 23:34:41 +00:00
|
|
|
}
|
2017-12-04 13:25:35 +00:00
|
|
|
}
|