🎨 Reformat some code

This commit is contained in:
Clement Desmidt 2019-01-25 22:19:50 +01:00
parent db51375185
commit 43e7dd2808
10 changed files with 63 additions and 52 deletions

View File

@ -54,7 +54,7 @@ class Scenario
if ($this->backup->isValid()) { if ($this->backup->isValid()) {
$this->transport->send(); $this->transport->send();
} else { } else {
throw new \Exception("Backup configuration is invalid."); throw new \Exception('Backup configuration is invalid.');
} }
} }
@ -75,7 +75,7 @@ class Scenario
// check the given scenario // check the given scenario
if (is_readable($scenario)) { if (is_readable($scenario)) {
$scenario = json_decode(file_get_contents($scenario), true); $scenario = json_decode(file_get_contents($scenario), true);
if (!is_null($scenario) && static::isValid($scenario)) { if ($scenario !== null && static::isValid($scenario)) {
try { try {
$scenario = new self($scenario); $scenario = new self($scenario);
$scenario->send(); $scenario->send();
@ -99,9 +99,8 @@ class Scenario
public static function isValid(array $scenario) public static function isValid(array $scenario)
{ {
return return
isset($scenario['backup']) && isset($scenario['backup'], $scenario['transport']) &&
count($scenario['backup']) === 1 && count($scenario['backup']) === 1 &&
isset($scenario['transport']) &&
count($scenario['transport']) === 1; count($scenario['transport']) === 1;
} }
} }

View File

@ -30,21 +30,25 @@ class Dropbox extends TransportAbstract
public function send() public function send()
{ {
$sent = true; $sent = true;
$files = $this->backup->getFilesTobackup(); $files = $this->backup->getFilesToBackup();
foreach ($files as $file => $name) { foreach ($files as $file => $name) {
$file = fopen($file, 'r'); $file = fopen($file, 'rb');
$upload = $this->dropbox->uploadFile($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $file); $upload = $this->dropbox->uploadFile($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $file);
if (!$upload) { if (!$upload) {
$sent = false; $sent = false;
echo 'DROPBOX upload manqu<71>e de '.$file.' vers '.$this->folder.$name; echo 'DROPBOX upload manqu<71>e de '.$file.' vers '.$this->folder.$name;
} }
} }
$streams = $this->backup->getStreamsTobackup(); $streams = $this->backup->getStreamsToBackup();
foreach ($streams as $stream => $name) { foreach ($streams as $stream => $name) {
$upload = $this->dropbox->uploadFileFromString($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $stream); $upload = $this->dropbox->uploadFileFromString(
$this->folder . '/' . $name,
\Dropbox\WriteMode::force(),
$stream
);
if (!$upload) { if (!$upload) {
$sent = false; $sent = false;
echo 'DROPBOX upload manqu<71>e de '.$file.' vers '.$this->folder.$name; echo 'DROPBOX upload manquée de ' . $file . ' vers '.$this->folder.$name;
} }
} }
return $sent; return $sent;

View File

@ -132,16 +132,19 @@ class Email extends TransportAbstract
$type = (string) finfo_file($finfo, $file); $type = (string) finfo_file($finfo, $file);
finfo_close($finfo); finfo_close($finfo);
return $type; return $type;
} else if (function_exists('mime_content_type')) {
return mime_content_type($file);
} else {
$ext = strtolower(array_pop(explode('.', $file)));
if (array_key_exists($ext, self::$mimeTypes)) {
return self::$mimeTypes[$ext];
} else {
return 'application/octet-stream';
}
} }
if (function_exists('mime_content_type')) {
return mime_content_type($file);
}
$file_info = explode('.', $file);
$ext = strtolower(array_pop($file_info));
if (array_key_exists($ext, self::$mimeTypes)) {
return self::$mimeTypes[$ext];
}
return 'application/octet-stream';
} }
/** /**
@ -150,8 +153,8 @@ class Email extends TransportAbstract
public function __construct(BackupAbstract $backup, array $config) public function __construct(BackupAbstract $backup, array $config)
{ {
parent::__construct($backup, $config); parent::__construct($backup, $config);
$this->setFiles($this->backup->getFilesTobackup()); $this->setFiles($this->backup->getFilesToBackup());
$this->setStreams($this->backup->getStreamsTobackup()); $this->setStreams($this->backup->getStreamsToBackup());
$this->email_to = $this->config['to']; $this->email_to = $this->config['to'];
$this->email_from = $this->config['from']; $this->email_from = $this->config['from'];
$this->encoding = $this->config['encoding']; $this->encoding = $this->config['encoding'];
@ -200,8 +203,8 @@ class Email extends TransportAbstract
// Checking files are selected // Checking files are selected
$zip = new \ZipArchive(); // Load zip library $zip = new \ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name $zip_name = time(). '.zip'; // Zip name
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)==true) { if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)===true) {
if (!empty($this->files)) { if (!empty($this->files)) {
foreach ($this->files as $file => $name) { foreach ($this->files as $file => $name) {
$zip->addFile($file, $name); // Adding files into zip $zip->addFile($file, $name); // Adding files into zip
@ -214,9 +217,9 @@ class Email extends TransportAbstract
$this->files = array(TEMP_DIR.$zip_name=>$zip_name); $this->files = array(TEMP_DIR.$zip_name=>$zip_name);
$random_hash = md5(date('r', time())); $random_hash = md5(date('r'));
$headers = "From: " . $this->email_from . "\r\nReply-To: " . $this->email_from; $headers = 'From: ' . $this->email_from . "\r\nReply-To: " . $this->email_from;
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"" . $random_hash . "\""; $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"" . $random_hash . '"';
$output = " $output = "
--$random_hash --$random_hash
@ -229,26 +232,26 @@ Content-Transfer-Encoding: 8bit
foreach ($this->files as $file => $name) { foreach ($this->files as $file => $name) {
$output .= " $output .= "
--$random_hash --$random_hash
Content-Type: " . self::getMimeType($file) . "; name=" . $name . " Content-Type: " . self::getMimeType($file) . '; name=' . $name . '
Content-Transfer-Encoding: base64 Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=" . $name . " Content-Disposition: attachment; filename=' . $name . '
" . chunk_split(base64_encode(file_get_contents($file))); ' . chunk_split(base64_encode(file_get_contents($file)));
} }
} }
if (!empty($this->streams)) { if (!empty($this->streams)) {
foreach ($this->streams as $name => $stream) { foreach ($this->streams as $name => $stream) {
if (count(explode('.', $name))<2) { if (substr_count($name, '.') + 1 <2) {
$name = 'backup'.$name.'.txt'; $name = 'backup'.$name.'.txt';
} }
$output .= " $output .= "
--$random_hash --$random_hash
Content-Type: text/plain; name=" . $name . " Content-Type: text/plain; name=" . $name . '
Content-Transfer-Encoding: base64 Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=" . $name . " Content-Disposition: attachment; filename=' . $name . '
" . chunk_split(base64_encode($stream)); ' . chunk_split(base64_encode($stream));
} }
} }
$output.="--$random_hash--"; $output.="--$random_hash--";

View File

@ -25,7 +25,7 @@ class Ftp extends TransportAbstract
} }
$this->connection = ftp_connect($this->host); $this->connection = ftp_connect($this->host);
if ($this->connection == false) { if ($this->connection === false) {
throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host)); throw new \Exception(sprintf('I can\'t connect to the FTP %s', $this->host));
} }
@ -35,8 +35,8 @@ class Ftp extends TransportAbstract
throw new \Exception($msg); throw new \Exception($msg);
} }
$this->setFiles($this->backup->getFilesTobackup()); $this->setFiles($this->backup->getFilesToBackup());
$this->setStreams($this->backup->getStreamsTobackup()); $this->setStreams($this->backup->getStreamsToBackup());
} }
private function setFiles($files = array()) private function setFiles($files = array())
@ -57,6 +57,7 @@ class Ftp extends TransportAbstract
/** /**
* @return bool * @return bool
* @throws \Exception
*/ */
public function send() public function send()
{ {
@ -74,7 +75,7 @@ class Ftp extends TransportAbstract
if (!empty($this->streams)) { if (!empty($this->streams)) {
foreach ($this->streams as $name => $stream) { foreach ($this->streams as $name => $stream) {
if (count(explode('.', $name)) < 2) { if (substr_count($name, '.') + 1 < 2) {
$name = 'backup' . $name . '.txt'; $name = 'backup' . $name . '.txt';
} }
file_put_contents($name, $stream); file_put_contents($name, $stream);

View File

@ -29,13 +29,13 @@ class Folder extends TransportAbstract
*/ */
public function send() public function send()
{ {
foreach ($this->backup->getFilesTobackup() as $file => $name) { foreach ($this->backup->getFilesToBackup() as $file => $name) {
if (copy($file, $this->folder . $name) === false) { if (copy($file, $this->folder . $name) === false) {
throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder)); throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder));
}; };
} }
foreach ($this->backup->getStreamsTobackup() as $name => $file) { foreach ($this->backup->getStreamsToBackup() as $name => $file) {
if (count(explode('.', $name)) < 2) { if (substr_count($name, '.') + 1 < 2) {
$name = 'backup' . $name . '.txt'; $name = 'backup' . $name . '.txt';
} }
if (file_put_contents($this->folder . $name, $file) === false) { if (file_put_contents($this->folder . $name, $file) === false) {

View File

@ -42,7 +42,7 @@ class Sftp extends TransportAbstract
public function send() public function send()
{ {
$sent = true; $sent = true;
$files = $this->backup->getFilesTobackup(); $files = $this->backup->getFilesToBackup();
if (!empty($files)) { if (!empty($files)) {
foreach ($files as $file => $name) { foreach ($files as $file => $name) {
$upload = $this->connection->put($this->folder.'/'.$name, $file, LibSFTP::SOURCE_LOCAL_FILE); $upload = $this->connection->put($this->folder.'/'.$name, $file, LibSFTP::SOURCE_LOCAL_FILE);
@ -53,12 +53,12 @@ class Sftp extends TransportAbstract
} }
} }
$streams = $this->backup->getStreamsTobackup(); $streams = $this->backup->getStreamsToBackup();
if (!empty($streams)) { if (!empty($streams)) {
foreach ($streams as $name => $stream) { foreach ($streams as $name => $stream) {
$upload = $this->connection->put($this->folder.'/'.$name, $stream); $upload = $this->connection->put($this->folder.'/'.$name, $stream);
if (!$upload) { if (!$upload) {
echo 'SFTP upload manqu<EFBFBD>e de '.$name.' vers '.$this->folder.$name; echo 'SFTP upload manquée de '.$name.' vers '.$this->folder.$name;
$sent = false; $sent = false;
} }
} }

View File

@ -151,7 +151,7 @@ abstract class BackupAbstract
foreach ($this->options as $name => $value) { foreach ($this->options as $name => $value) {
$method = sprintf('setOption%s', ucfirst($name)); $method = sprintf('setOption%s', ucfirst($name));
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
call_user_func([$this, $method], $value); $this->$method($value);
} }
} }
@ -174,7 +174,7 @@ abstract class BackupAbstract
if (touch(TEMP_DIR . $zip_name) === false) { if (touch(TEMP_DIR . $zip_name) === false) {
throw new \Exception('Backup::Zip::Permission denied.'); throw new \Exception('Backup::Zip::Permission denied.');
} }
if ($zip->open(TEMP_DIR . $zip_name, \ZipArchive::OVERWRITE) == true) { if ($zip->open(TEMP_DIR . $zip_name, \ZipArchive::OVERWRITE) === true) {
foreach ($this->files_to_backup as $file => $name) { foreach ($this->files_to_backup as $file => $name) {
$zip->addFile($file, $name); // Adding files into zip $zip->addFile($file, $name); // Adding files into zip
} }

View File

@ -15,7 +15,7 @@ class Files extends BackupAbstract
} }
$filesToBackup = $config['files']; $filesToBackup = $config['files'];
if (!empty($filesToBackup) && is_array($filesToBackup)) { if (!empty($filesToBackup) && is_array($filesToBackup)) {
$names = array_map("basename", $filesToBackup); $names = array_map('basename', $filesToBackup);
$this->files_to_backup = array_combine($filesToBackup, $names); $this->files_to_backup = array_combine($filesToBackup, $names);
} }
parent::__construct($config); parent::__construct($config);

View File

@ -60,17 +60,15 @@ class Mysql extends BackupAbstract
*/ */
private function getFromTables() private function getFromTables()
{ {
$return = ""; $return = '';
foreach ($this->tables as $table) { foreach ($this->tables as $table) {
if (is_array($table)) { if (is_array($table)) {
$table = $table[0]; $table = $table[0];
} }
$result = $this->pdo->prepare('SELECT * FROM ' . $table); $result = $this->pdo->query('SELECT * FROM ' . $table);
$result->execute();
$num_fields = $result->columnCount(); $num_fields = $result->columnCount();
$return .= 'DROP TABLE IF EXISTS ' . $table . ';'; $return .= 'DROP TABLE IF EXISTS ' . $table . ';';
$result2 = $this->pdo->prepare('SHOW CREATE TABLE ' . $table); $result2 = $this->pdo->query('SHOW CREATE TABLE ' . $table);
$result2->execute();
$row2 = $result2->fetch(); $row2 = $result2->fetch();
$return.= "\n\n" . $row2[1] . ";\n\n"; $return.= "\n\n" . $row2[1] . ";\n\n";
foreach ($result as $row) { foreach ($result as $row) {
@ -123,6 +121,6 @@ class Mysql extends BackupAbstract
*/ */
protected function build() protected function build()
{ {
empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables); empty($this->tables) || $this->tables === '*' ? $this->everything() : $this->fromTables($this->tables);
} }
} }

View File

@ -12,7 +12,13 @@
], ],
"require": { "require": {
"phpseclib/phpseclib": ">=1.0.0", "phpseclib/phpseclib": ">=1.0.0",
"dropbox/dropbox-sdk": "~1.1" "dropbox/dropbox-sdk": "~1.1",
"ext-fileinfo": "*",
"ext-zip": "*",
"ext-ftp": "*",
"ext-pdo": "*",
"ext-mbstring": "*",
"ext-json": "*"
}, },
"autoload": { "autoload": {
"classmap": ["app/code/", "vendor/"] "classmap": ["app/code/", "vendor/"]