diff --git a/app/code/Scenario.php b/app/code/Scenario.php index d375aea..cb412df 100644 --- a/app/code/Scenario.php +++ b/app/code/Scenario.php @@ -7,7 +7,8 @@ use Shikiryu\Backup\Transport\TransportAbstract; use Shikiryu\Backup\Backup\Factory as BackupFactory; use Shikiryu\Backup\Transport\Factory as TransportFactory; -class Scenario { +class Scenario +{ /* @var $backup BackupAbstract */ private $backup; @@ -103,5 +104,4 @@ class Scenario { isset($scenario['transport']) && count($scenario['transport']) === 1; } - -} \ No newline at end of file +} diff --git a/app/code/Transport/Dropbox.php b/app/code/Transport/Dropbox.php index a3ff8a7..972114c 100644 --- a/app/code/Transport/Dropbox.php +++ b/app/code/Transport/Dropbox.php @@ -7,7 +7,11 @@ use Shikiryu\Backup\Transport\TransportAbstract; class Dropbox extends TransportAbstract { - + /** + * Dropbox Client + * + * @var Client + */ private $dropbox; protected $token = ''; @@ -26,23 +30,23 @@ class Dropbox extends TransportAbstract public function send() { $sent = true; - $files = $this->backup->getFilesToBackup(); + $files = $this->backup->getFilesTobackup(); foreach ($files as $file => $name) { $file = fopen($file, 'r'); $upload = $this->dropbox->uploadFile($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $file); if (!$upload) { $sent = false; - echo 'DROPBOX upload manquée de '.$file.' vers '.$this->folder.$name; + echo 'DROPBOX upload manqu�e de '.$file.' vers '.$this->folder.$name; } } - $streams = $this->backup->getStreamsToBackup(); + $streams = $this->backup->getStreamsTobackup(); foreach ($streams as $stream => $name) { $upload = $this->dropbox->uploadFileFromString($this->folder.'/'.$name, \Dropbox\WriteMode::force(), $stream); if (!$upload) { $sent = false; - echo 'DROPBOX upload manquée de '.$file.' vers '.$this->folder.$name; + echo 'DROPBOX upload manqu�e de '.$file.' vers '.$this->folder.$name; } } return $sent; } -} \ No newline at end of file +} diff --git a/app/code/Transport/Email.php b/app/code/Transport/Email.php index 9691bf4..2521340 100644 --- a/app/code/Transport/Email.php +++ b/app/code/Transport/Email.php @@ -147,8 +147,8 @@ class Email extends TransportAbstract */ public function __construct(BackupAbstract $backup, array $config) { parent::__construct($backup, $config); - $this->setFiles($this->backup->getFilesToBackup()); - $this->setStreams($this->backup->getStreamsToBackup()); + $this->setFiles($this->backup->getFilesTobackup()); + $this->setStreams($this->backup->getStreamsTobackup()); $this->email_to = $this->config['to']; $this->email_from = $this->config['from']; $this->encoding = $this->config['encoding']; diff --git a/app/code/Transport/FTP.php b/app/code/Transport/FTP.php index 6a909db..d8eb7f1 100644 --- a/app/code/Transport/FTP.php +++ b/app/code/Transport/FTP.php @@ -1,4 +1,5 @@ path = $this->config['path']; if (!empty($this->folder)) { - $this->folder = sprintf('/%s/', ltrim(rtrim($this->folder, '/'),'/')); + $this->folder = sprintf('/%s/', ltrim(rtrim($this->folder, '/'), '/')); } $this->connection = ftp_connect($this->host); @@ -29,13 +31,14 @@ class Ftp extends TransportAbstract $login = @ftp_login($this->connection, $this->login, $this->password); if ($login === false) { - throw new \Exception(sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password)); + $msg = sprintf('Connexion FTP %s refusée avec %s et %s', $this->host, $this->login, $this->password); + throw new \Exception($msg); } - $this->setFiles($this->backup->getFilesToBackup()); - $this->setStreams($this->backup->getStreamsToBackup()); + $this->setFiles($this->backup->getFilesTobackup()); + $this->setStreams($this->backup->getStreamsTobackup()); } - + private function setFiles($files = array()) { if (is_array($files) && !empty($files)) { @@ -44,13 +47,14 @@ class Ftp extends TransportAbstract return $this; } - private function setStreams($streams = array()) { + private function setStreams($streams = array()) + { if (is_array($streams) && !empty($streams)) { $this->streams = $streams; } return $this; } - + /** * @return bool */ @@ -58,7 +62,7 @@ class Ftp extends TransportAbstract { $sent = true; ftp_pasv($this->connection, true); - if (!empty($this->files)){ + if (!empty($this->files)) { foreach ($this->files as $file => $name) { $upload = ftp_put($this->connection, $this->folder.$name, $file, FTP_BINARY); if (!$upload) { @@ -67,11 +71,12 @@ class Ftp extends TransportAbstract } } } - - if (!empty($this->streams)){ + + if (!empty($this->streams)) { foreach ($this->streams as $name => $stream) { - if (count(explode('.', $name)) < 2) + if (count(explode('.', $name)) < 2) { $name = 'backup' . $name . '.txt'; + } file_put_contents($name, $stream); $upload = ftp_put($this->connection, $this->folder.$name, $name, FTP_ASCII); if (!$upload) { @@ -85,23 +90,12 @@ class Ftp extends TransportAbstract if (!$sent) { throw new \Exception('At least an upload didnt work.'); } - + return $sent; } - + public function __destruct() { ftp_close($this->connection); } - - - - - - - - - - } -?> diff --git a/app/code/Transport/Factory.php b/app/code/Transport/Factory.php index 77bed60..bade0cc 100644 --- a/app/code/Transport/Factory.php +++ b/app/code/Transport/Factory.php @@ -21,4 +21,4 @@ class Factory } return null; } -} \ No newline at end of file +} diff --git a/app/code/Transport/Folder.php b/app/code/Transport/Folder.php index d167727..329dd90 100644 --- a/app/code/Transport/Folder.php +++ b/app/code/Transport/Folder.php @@ -2,43 +2,46 @@ namespace Shikiryu\Backup\Transport; - use Shikiryu\Backup\Backup\BackupAbstract; class Folder extends TransportAbstract { - /** @var string */ - protected $folder; + /** + * Folder to backup + * + * @var string + */ + protected $folder; - public function __construct(BackupAbstract $backup, array $config = array()) - { - parent::__construct($backup, $config); + public function __construct(BackupAbstract $backup, array $config = array()) + { + parent::__construct($backup, $config); - if (!empty($this->folder)) { - $this->folder = sprintf('%s/', rtrim($this->folder, '/')); - } - } + if (!empty($this->folder)) { + $this->folder = sprintf('%s/', rtrim($this->folder, '/')); + } + } - /** - * @return bool - * - * @throws \Exception - */ - public function send() - { - foreach ($this->backup->getFilesToBackup() as $file => $name) { - if (copy($file, $this->folder . $name) === false) { - throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder)); - }; - } - foreach ($this->backup->getStreamsToBackup() as $name => $file) { - if (count(explode('.', $name)) < 2) { - $name = 'backup' . $name . '.txt'; - } - if (file_put_contents($this->folder . $name, $file) === false) { - throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder)); - } - } - return true; - } -} \ No newline at end of file + /** + * @return bool + * + * @throws \Exception + */ + public function send() + { + foreach ($this->backup->getFilesTobackup() as $file => $name) { + if (copy($file, $this->folder . $name) === false) { + throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder)); + }; + } + foreach ($this->backup->getStreamsTobackup() as $name => $file) { + if (count(explode('.', $name)) < 2) { + $name = 'backup' . $name . '.txt'; + } + if (file_put_contents($this->folder . $name, $file) === false) { + throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder)); + } + } + return true; + } +} diff --git a/app/code/Transport/SFTP.php b/app/code/Transport/SFTP.php index 2adb348..d171b0b 100644 --- a/app/code/Transport/SFTP.php +++ b/app/code/Transport/SFTP.php @@ -6,7 +6,7 @@ use phpseclib\Net\SFTP as LibSFTP; class Sftp extends TransportAbstract { - /** @var string */ + /** @var string */ protected $host; /** @var int */ protected $port = 22; @@ -16,7 +16,7 @@ class Sftp extends TransportAbstract protected $password; /** @var string */ protected $folder; - /** @var LibSFTP */ + /** @var LibSFTP */ private $connection; /** @@ -36,29 +36,29 @@ class Sftp extends TransportAbstract /** * @return bool - * + * * @throws \Exception */ public function send() { $sent = true; - $files = $this->backup->getFilesToBackup(); - if (!empty($files)){ + $files = $this->backup->getFilesTobackup(); + if (!empty($files)) { foreach ($files as $file => $name) { $upload = $this->connection->put($this->folder.'/'.$name, $file, LibSFTP::SOURCE_LOCAL_FILE); if (!$upload) { $sent = false; - echo 'SFTP upload manquée de '.$file.' vers '.$this->folder.$name; + echo 'SFTP upload manqu�e de '.$file.' vers '.$this->folder.$name; } } } - $streams = $this->backup->getStreamsToBackup(); - if (!empty($streams)){ + $streams = $this->backup->getStreamsTobackup(); + if (!empty($streams)) { foreach ($streams as $name => $stream) { $upload = $this->connection->put($this->folder.'/'.$name, $stream); if (!$upload) { - echo 'SFTP upload manquée de '.$name.' vers '.$this->folder.$name; + echo 'SFTP upload manqu�e de '.$name.' vers '.$this->folder.$name; $sent = false; } } @@ -69,4 +69,4 @@ class Sftp extends TransportAbstract } return $sent; } -} \ No newline at end of file +} diff --git a/app/code/Transport/TransportAbstract.php b/app/code/Transport/TransportAbstract.php index 00cedf3..13adf35 100644 --- a/app/code/Transport/TransportAbstract.php +++ b/app/code/Transport/TransportAbstract.php @@ -7,7 +7,7 @@ use Shikiryu\Backup\Backup\BackupAbstract; abstract class TransportAbstract { - /** @var BackupAbstract */ + /** @var BackupAbstract */ protected $backup; /** @var array */ protected $config; @@ -25,5 +25,5 @@ abstract class TransportAbstract /** * @return bool */ - public abstract function send(); -} \ No newline at end of file + abstract public function send(); +} diff --git a/app/code/backup/BackupAbstract.php b/app/code/backup/BackupAbstract.php index 69b623f..dfd519e 100644 --- a/app/code/backup/BackupAbstract.php +++ b/app/code/backup/BackupAbstract.php @@ -4,15 +4,29 @@ namespace Shikiryu\Backup\Backup; abstract class BackupAbstract { - /** @var array */ + /** + * Options + * + * @var array + */ protected $options; - /** @var string[] */ - protected $_filesToBackup = []; - /** @var string[] */ - protected $_streamsToBackup = []; + /** + * File path to backup + * + * @var string[] + */ + protected $files_to_backup = []; + /** + * Streams to backup + * + * @var string[] + */ + protected $streams_to_backup = []; /** - * @param array $config + * Constructor + * + * @param array $config array of options and parameters */ function __construct($config = array()) { @@ -29,45 +43,53 @@ abstract class BackupAbstract /** * Magic setter method * - * @param $name - * @param $value + * @param string $name attribute name + * @param mixed $value attribute value + * + * @return $this */ public function __set($name, $value) { $this->$name = $value; + + return $this; } /** + * Getter + * * @return array */ public function getFilesToBackup() { - return $this->_filesToBackup; + return $this->files_to_backup; } /** + * Getter + * * @return array */ public function getStreamsToBackup() { - return $this->_streamsToBackup; + return $this->streams_to_backup; } /** * Check if all files got the minimum given size. * - * @param int $file_size + * @param int $file_size file size * * @return bool */ public function checkMinimumFilesize($file_size) { - foreach ($this->_filesToBackup as $file => $name) { + foreach ($this->files_to_backup as $file => $name) { if (filesize($file) < $file_size) { return false; } } - foreach ($this->_streamsToBackup as $name => $file) { + foreach ($this->streams_to_backup as $name => $file) { if (mb_strlen($file, 'utf-8') < $file_size) { return false; } @@ -75,44 +97,58 @@ abstract class BackupAbstract return true; } - /** - * Initialize everything - */ + /** + * Initialize everything + * + * @return $this + */ protected function init() { $this->preBuild(); $this->build(); $this->postBuild(); $this->applyOptions(); + + return $this; } - - /** - * Function that can be used to initialize the backup - */ + + /** + * Function that can be used to initialize the backup + * + * @return void + */ abstract protected function preBuild(); + /** - * Function that can be used after the backup - */ + * Function that can be used after the backup + * + * @return void + */ abstract protected function postBuild(); + /** - * Mandatory function doing the backup - */ + * Mandatory function doing the backup + * + * @return void + */ abstract protected function build(); + /** - * Check if the backup is valid - * - * @return bool - */ + * Check if the backup is valid + * + * @return bool + */ abstract public function isValid(); /** + * Apply options + * * @return $this */ protected function applyOptions() { // TODO isValid here ? - foreach($this->options as $name => $value) - { + foreach ($this->options as $name => $value) { $method = sprintf('setOption%s', ucfirst($name)); if (method_exists($this, $method)) { call_user_func([$this, $method], $value); @@ -132,18 +168,18 @@ abstract class BackupAbstract protected function setOptionZip() { $zip = new \ZipArchive(); - $zip_name = sprintf('%s.zip', (!empty($this->options['name']) ? $this->options['name'] : time())); // Zip name - if (touch(TEMP_DIR.$zip_name) === false) { + // Zip name + $zip_name = !empty($this->options['name']) ? $this->options['name'] : time(); + $zip_name = sprintf('%s.zip', $zip_name); + if (touch(TEMP_DIR . $zip_name) === false) { throw new \Exception('Backup::Zip::Permission denied.'); } - if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::OVERWRITE)==TRUE) { - foreach($this->_filesToBackup as $file => $name) - { + if ($zip->open(TEMP_DIR . $zip_name, \ZipArchive::OVERWRITE) == TRUE) { + foreach ($this->files_to_backup as $file => $name) { $zip->addFile($file, $name); // Adding files into zip } - foreach($this->_streamsToBackup as $file => $name) - { + foreach ($this->streams_to_backup as $file => $name) { $zip->addFromString($file, $name); // Adding streams into zip } $zip->close(); @@ -151,8 +187,8 @@ abstract class BackupAbstract throw new \Exception('Backup::Zip::Can\'t zip the given backup.'); } - $this->_filesToBackup = [TEMP_DIR.$zip_name => $zip_name]; - $this->_streamsToBackup = []; + $this->files_to_backup = [TEMP_DIR . $zip_name => $zip_name]; + $this->streams_to_backup = []; return $this; } @@ -160,7 +196,7 @@ abstract class BackupAbstract /** * Add the current date with the given format into the files names * - * @param string $format + * @param string $format date format * * @return $this */ @@ -170,20 +206,20 @@ abstract class BackupAbstract $format = 'Ymd'; } $tmpFiles = array(); - foreach ($this->_filesToBackup as $file => $name) { + foreach ($this->files_to_backup as $file => $name) { $nameA = explode('.', $name); $nameA[] = end($nameA); $nameA[count($nameA) - 2] = date($format); $name = implode('.', $nameA); $tmpFiles[$file] = $name; } - $this->_filesToBackup = $tmpFiles; + $this->files_to_backup = $tmpFiles; $tmpStream = array(); - foreach ($this->_streamsToBackup as $name => $stream) { + foreach ($this->streams_to_backup as $name => $stream) { $tmpStream[$name . '-' . date($format)] = $stream; } - $this->_streamsToBackup = $tmpStream; + $this->streams_to_backup = $tmpStream; return $this; } @@ -191,7 +227,7 @@ abstract class BackupAbstract /** * Add the current time with the given format into the files names * - * @param string $format + * @param string $format time format * * @return $this */ @@ -201,29 +237,31 @@ abstract class BackupAbstract $format = 'his'; } $tmpFiles = array(); - foreach ($this->_filesToBackup as $file => $name) { + foreach ($this->files_to_backup as $file => $name) { $nameA = explode('.', $name); $nameA[] = end($nameA); $nameA[count($nameA) - 2] = date($format); $name = implode('.', $nameA); $tmpFiles[$file] = $name; } - $this->_filesToBackup = $tmpFiles; + $this->files_to_backup = $tmpFiles; $tmpStream = array(); - foreach ($this->_streamsToBackup as $name => $stream) { + foreach ($this->streams_to_backup as $name => $stream) { $tmpStream[$name . '-' . date($format)] = $stream; } - $this->_streamsToBackup = $tmpStream; + $this->streams_to_backup = $tmpStream; return $this; } /** - * @param $name + * Set option name * + * @param mixed $name option's name + * * @throws \Exception - * + * * @SuppressWarnings("unused") */ protected function setOptionName($name) diff --git a/app/code/backup/Factory.php b/app/code/backup/Factory.php index ce2a40f..f720efa 100644 --- a/app/code/backup/Factory.php +++ b/app/code/backup/Factory.php @@ -17,4 +17,4 @@ class Factory } return null; } -} \ No newline at end of file +} diff --git a/app/code/backup/Files.php b/app/code/backup/Files.php index a2b3b70..63ea9f9 100644 --- a/app/code/backup/Files.php +++ b/app/code/backup/Files.php @@ -16,7 +16,7 @@ class Files extends BackupAbstract $filesToBackup = $config['files']; if(!empty($filesToBackup) && is_array($filesToBackup)){ $names = array_map("basename",$filesToBackup); - $this->_filesToBackup = array_combine($filesToBackup,$names); + $this->files_to_backup = array_combine($filesToBackup,$names); } parent::__construct($config); } @@ -31,7 +31,7 @@ class Files extends BackupAbstract public function isValid() { $result = true; - foreach ($this->_filesToBackup as $file => $name) { + foreach ($this->files_to_backup as $file => $name) { if (!file_exists($file)) { $result = false; break; diff --git a/app/code/backup/Folder.php b/app/code/backup/Folder.php index a36a4b5..66561fb 100644 --- a/app/code/backup/Folder.php +++ b/app/code/backup/Folder.php @@ -4,41 +4,40 @@ namespace Shikiryu\Backup\Backup; class Folder extends BackupAbstract { - public function __construct(array $config = array()) { parent::__construct($config); } /** - * @return bool - */ + * @return bool + */ public function isValid() { // TODO: Implement isValid() method. } - /** - * Function that can be used to initialize the backup - */ + /** + * Function that can be used to initialize the backup + */ protected function preBuild() { // TODO: Implement preBuild() method. } - /** - * Function that can be used after the backup - */ + /** + * Function that can be used after the backup + */ protected function postBuild() { // TODO: Implement postBuild() method. } - /** - * Mandatory function doing the backup - */ + /** + * Mandatory function doing the backup + */ protected function build() { // TODO: Implement build() method. } -} \ No newline at end of file +} diff --git a/app/code/backup/Mysql.php b/app/code/backup/Mysql.php index b0cd461..b02a06b 100644 --- a/app/code/backup/Mysql.php +++ b/app/code/backup/Mysql.php @@ -6,13 +6,13 @@ class Mysql extends BackupAbstract { /** - * @var $pdo \PDO - */ + * @var $pdo \PDO + */ private $pdo; /* - * from config - */ + * from config + */ protected $tables; protected $host; protected $database; @@ -20,47 +20,49 @@ class Mysql extends BackupAbstract protected $pwd; /** - * @param array $config - */ - public function __construct(array $config = array()) { + * @param array $config + */ + public function __construct(array $config = array()) + { parent::__construct($config); } /** - * @param array $tables - * @return $this|string - */ + * @param array $tables + * @return $this|string + */ private function fromTables($tables = array()) { - if(!empty($tables)) { + if (!empty($tables)) { $this->tables = $tables; } - $this->_streamsToBackup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables(); + $this->streams_to_backup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables(); return $this; } /** - * set the list of table to backup to all tables - * - * @return $this - */ - private function everything() { + * set the list of table to backup to all tables + * + * @return $this + */ + private function everything() + { $this->tables = array(); - foreach($this->pdo->query('SHOW TABLES') as $table) { + foreach ($this->pdo->query('SHOW TABLES') as $table) { $this->tables[] = $table; } - $this->_streamsToBackup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables(); + $this->streams_to_backup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables(); return $this; } /** - * @return string - */ + * @return string + */ private function getFromTables() { $return = ""; foreach ($this->tables as $table) { - if(is_array($table)) { + if (is_array($table)) { $table = $table[0]; } $result = $this->pdo->prepare('SELECT * FROM ' . $table); @@ -71,21 +73,21 @@ class Mysql extends BackupAbstract $result2->execute(); $row2 = $result2->fetch(); $return.= "\n\n" . $row2[1] . ";\n\n"; - foreach($result as $row){ - $return.= 'INSERT INTO ' . $table . ' VALUES('; - for ($j = 0; $j < $num_fields; $j++) { - $row[$j] = addslashes($row[$j]); -// $row[$j] = preg_replace("\n", "\\n", $row[$j]); - if (isset($row[$j])) { - $return.= '"' . $row[$j] . '"'; - } else { - $return.= '""'; - } - if ($j < ($num_fields - 1)) { - $return.= ','; - } + foreach ($result as $row) { + $return.= 'INSERT INTO ' . $table . ' VALUES('; + for ($j = 0; $j < $num_fields; $j++) { + $row[$j] = addslashes($row[$j]); + // $row[$j] = preg_replace("\n", "\\n", $row[$j]); + if (isset($row[$j])) { + $return.= '"' . $row[$j] . '"'; + } else { + $return.= '""'; } - $return.= ");\n"; + if ($j < ($num_fields - 1)) { + $return.= ','; + } + } + $return.= ");\n"; } $return.="\n\n\n"; } @@ -93,36 +95,34 @@ class Mysql extends BackupAbstract } /** - * @return bool - */ + * @return bool + */ public function isValid() { - return !empty($this->_streamsToBackup); + return !empty($this->streams_to_backup); } - /** - * Function that can be used to initialize the backup - */ + /** + * Function that can be used to initialize the backup + */ protected function preBuild() { $this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->database, $this->login, $this->pwd); } - /** - * Function that can be used after the backup - */ + /** + * Function that can be used after the backup + */ protected function postBuild() { // TODO: Implement postBuild() method. } - /** - * Mandatory function doing the backup - */ + /** + * Mandatory function doing the backup + */ protected function build() { empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables); } } - -?>