diff --git a/Abstract.php b/Abstract.php index 4bced31..60ecf33 100644 --- a/Abstract.php +++ b/Abstract.php @@ -18,6 +18,22 @@ class Shikiryu_Backup_Abstract $this->_streamsToBackup = array(); } + /** + * @return array + */ + public function getFilesToBackup() + { + return $this->_filesToBackup; + } + + /** + * @return array + */ + public function getStreamsToBackup() + { + return $this->_streamsToBackup; + } + /** * Add the current date with the given format into the files names * @@ -78,17 +94,32 @@ class Shikiryu_Backup_Abstract return $this; } - function backupToEmail($to, $from, $objet, $mes) + /** + * + * @param $name string + * @param $args mixed + * + * @return bool + */ + public function __call($name, $args) + { + if (substr($name,0,8) == 'backupTo') { + $type = substr($name, 8); + return Shikiryu_Backup_Transport_Factory::getAndSend($type, $this, $args); + } + } + + /*function backupToEmail($to, $from, $object, $mes) { $email = new Shikiryu_Backup_Email(); $email->addTo($to) ->setFrom($from) - ->setSubject($objet) + ->setSubject($object) ->setMessage($mes) ->setFiles($this->_filesToBackup) ->setStreams($this->_streamsToBackup); return $email->send(); - } + }*/ function backupToDropbox() { @@ -103,6 +134,9 @@ class Shikiryu_Backup_Abstract ->send(); } + /** + * @param $folder + */ function backupToFolder($folder) { if (!empty($folder)) { diff --git a/Files.php b/Files.php index 250f48c..dd88be7 100644 --- a/Files.php +++ b/Files.php @@ -11,8 +11,7 @@ class Shikiryu_Backup_Files extends Shikiryu_Backup_Abstract { parent::__construct(); if(!empty($filesToBackup) && is_array($filesToBackup)){ - $names = $filesToBackup; - array_walk($names, array($this, '_getNames')); + $names = array_map("basename",$filesToBackup); $this->_filesToBackup = array_combine($filesToBackup,$names); } } @@ -24,13 +23,12 @@ class Shikiryu_Backup_Files extends Shikiryu_Backup_Abstract * * @return $this */ - function setFilePath($filestobackup = array()) + function setFilePath($filesToBackup = array()) { - if(!empty($filestobackup) && is_array($filestobackup)) + if(!empty($filesToBackup) && is_array($filesToBackup)) { - $names = $filestobackup; - array_walk($names, 'basename'); - $this->_filesToBackup = array_combine($filestobackup,$names); + $names = array_map("basename",$filesToBackup); + $this->_filesToBackup = array_combine($filesToBackup,$names); } return $this; } diff --git a/Mysql.php b/Mysql.php index 083c31e..b66d27b 100644 --- a/Mysql.php +++ b/Mysql.php @@ -2,7 +2,8 @@ include_once dirname(__FILE__) . '/Abstract.php'; -class Shikiryu_Backup_MYSQL extends Shikiryu_Backup_Abstract { +class Shikiryu_Backup_MYSQL extends Shikiryu_Backup_Abstract +{ /** * @var $_pdo PDO diff --git a/Transport/Abstract.php b/Transport/Abstract.php index 2b055bf..86277e5 100644 --- a/Transport/Abstract.php +++ b/Transport/Abstract.php @@ -1,5 +1,22 @@ config = $config[ucfirst(strtolower($type))]; + $this->backup = $backup; + } + + /** + * @return bool + */ + public abstract function send(); } \ No newline at end of file diff --git a/Transport/Email.php b/Transport/Email.php index 86d4552..31c0881 100644 --- a/Transport/Email.php +++ b/Transport/Email.php @@ -1,14 +1,15 @@ 'text/plain', 'htm' => 'text/html', @@ -138,10 +139,18 @@ class Shikiryu_Backup_Email { } /** - * @param string $enc encoding of the mail + * @param Shikiryu_Backup_Abstract $backup */ - function __construct($enc = 'UTF-8') { - $this->_encoding = $enc; + public function __construct(Shikiryu_Backup_Abstract $backup) { + parent::__construct($backup); + $this->setFiles($this->backup->getFilesToBackup()); + $this->setStreams($this->backup->getStreamsToBackup()); + $this->to = $this->config['to']; + $this->from = $this->config['from']; + $this->encoding = $this->config['encoding']; + $this->subjet = $this->config['subject']; + $this->message = $this->config['message']; + $this->encoding = $this->config['encoding']; } /** @@ -151,8 +160,9 @@ class Shikiryu_Backup_Email { * * @return $this */ - function addTo($to) { - $this->_to = $to; + private function addTo($to) + { + $this->to = $to; return $this; } @@ -163,8 +173,9 @@ class Shikiryu_Backup_Email { * * @return $this */ - function setFrom($from) { - $this->_from = $from; + private function setFrom($from) + { + $this->from = $from; return $this; } @@ -175,8 +186,9 @@ class Shikiryu_Backup_Email { * * @return $this */ - function setSubject($sub) { - $this->_subject = strip_tags($sub); + private function setSubject($sub) + { + $this->subject = strip_tags($sub); return $this; } @@ -187,21 +199,24 @@ class Shikiryu_Backup_Email { * * @return $this */ - function setMessage($mes) { - $this->_message = strip_tags($mes); + private function setMessage($mes) + { + $this->message = strip_tags($mes); return $this; } - function setFiles($files = array()) { + private function setFiles($files = array()) + { if (is_array($files) && !empty($files)) { - $this->_files = $files; + $this->files = $files; } return $this; } - - function setStreams($streams = array()) { + + private function setStreams($streams = array()) + { if (is_array($streams) && !empty($streams)) { - $this->_streams = $streams; + $this->streams = $streams; } return $this; } @@ -213,14 +228,14 @@ class Shikiryu_Backup_Email { * * @return bool */ - function send() { + public function send() { // Checking files are selected $zip = new ZipArchive(); // Load zip library $zip_name = time().".zip"; // Zip name if($zip->open(dirname(__FILE__).'/bu/'.$zip_name, ZIPARCHIVE::CREATE)==TRUE) { - if(!empty($this->_files)) { - foreach($this->_files as $file) + if(!empty($this->files)) { + foreach($this->files as $file) { $zip->addFile($file); // Adding files into zip } @@ -228,21 +243,21 @@ class Shikiryu_Backup_Email { $zip->close(); } - $this->_files = array(dirname(__FILE__).'/bu/'.$zip_name=>dirname(__FILE__).'/bu/'.$zip_name); + $this->files = array(dirname(__FILE__).'/bu/'.$zip_name=>dirname(__FILE__).'/bu/'.$zip_name); $random_hash = md5(date('r', time())); - $headers = "From: " . $this->_from . "\r\nReply-To: " . $this->_from; + $headers = "From: " . $this->from . "\r\nReply-To: " . $this->from; $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"" . $random_hash . "\""; $output = " --$random_hash -Content-Type: text/plain; charset='" . strtolower($this->_encoding) . "' +Content-Type: text/plain; charset='" . strtolower($this->encoding) . "' Content-Transfer-Encoding: 8bit -" . $this->_message . "\r\n"; +" . $this->message . "\r\n"; - if(!empty($this->_files)) - foreach($this->_files as $file=>$name) { + if(!empty($this->files)) + foreach($this->files as $file=>$name) { $name = end(explode('/', $name)); $output .= " --$random_hash @@ -253,8 +268,8 @@ Content-Disposition: attachment; filename=" . $name . " " . chunk_split(base64_encode(file_get_contents($file))); } - if(!empty($this->_streams)) - foreach($this->_streams as $name=>$stream) { + if(!empty($this->streams)) + foreach($this->streams as $name=>$stream) { if(count(explode('.',$name))<2) $name = 'backup'.$name.'.txt'; $output .= " --$random_hash @@ -265,7 +280,7 @@ Content-Disposition: attachment; filename=" . $name . " " . chunk_split(base64_encode($stream)); } $output.="--$random_hash--"; - return mail($this->_to, $this->_subject, $output, $headers); + return mail($this->to, $this->subject, $output, $headers); } } diff --git a/Transport/FTP.php b/Transport/FTP.php index 55e35ab..74052fb 100644 --- a/Transport/FTP.php +++ b/Transport/FTP.php @@ -1,74 +1,80 @@ path = $this->config['path']; + if (!empty($this->config['path'])) { + $this->path = sprintf('/%s/', ltrim(rtrim($this->config['path'], '/'),'/')); } - $this->_path = $path; - $this->_connection = ftp_connect($server); - $login = ftp_login($this->_connection, $login, $pwd); - if (!$this->_connection || !$login) { + $this->connection = ftp_connect($this->config['host']); + + $login = ftp_login($this->connection, $this->config['login'], $this->config['password']); + if (!$this->connection || !$login) { throw new Exception('Connexion FTP refusée.'); } + + $this->setFiles($this->backup->getFilesToBackup()); + $this->setStreams($this->backup->getStreamsToBackup()); } - function setFiles($files = array()) + private function setFiles($files = array()) { if (is_array($files) && !empty($files)) - $this->_files = $files; + $this->files = $files; return $this; } - - function setStreams($streams = array()) { + + private function setStreams($streams = array()) { if (is_array($streams) && !empty($streams)) - $this->_streams = $streams; + $this->streams = $streams; return $this; } - function send() + public function send() { - if (!empty($this->_files)){ - foreach ($this->_files as $file => $name) { - $upload = ftp_put($this->_connection, $this->_path.$name, $file, FTP_ASCII); + $sent = true; + if (!empty($this->files)){ + foreach ($this->files as $file => $name) { + // TODO PASSIVE MODE + $upload = ftp_put($this->connection, $this->path.$name, $file, FTP_ASCII); if (!$upload) { - echo 'FTP upload manquée de '.$file.' vers '.$this->_path.$name; + $sent = false; + echo 'FTP upload manquée de '.$file.' vers '.$this->path.$name; } -// else echo 'upload réussi de '.$file.' vers '.$this->_path.$name; } } - if (!empty($this->_streams)){ - foreach ($this->_streams as $name => $stream) { + if (!empty($this->streams)){ + foreach ($this->streams as $name => $stream) { if (count(explode('.', $name)) < 2) $name = 'backup' . $name . '.txt'; file_put_contents($name, $stream); - $upload = ftp_put($this->_connection, $this->_path.$name, $name, FTP_ASCII); + // TODO PASSIVE MODE + $upload = ftp_put($this->connection, $this->path.$name, $name, FTP_ASCII); if (!$upload) { echo 'FTP upload manquée de '.$name.' vers '.$this->_path.$name; + $sent = false; } -// else echo 'upload réussi de '.$name.' vers '.$this->_path.$name; unlink($name); } } } - function __destruct() + public function __destruct() { - ftp_close($this->_connection); + ftp_close($this->connection); } diff --git a/Transport/Factory.php b/Transport/Factory.php new file mode 100644 index 0000000..b1cd561 --- /dev/null +++ b/Transport/Factory.php @@ -0,0 +1,20 @@ +send(); + } + return false; + } +} \ No newline at end of file diff --git a/config/config.ini b/config/config.ini new file mode 100644 index 0000000..c85c76a --- /dev/null +++ b/config/config.ini @@ -0,0 +1,12 @@ +[Email] +to=text@example.com +from=myserver@example.com +encoding=UTF-8 +subject=[server.com] Backup +message=Hi,\n\nYou can find your backup in this mail.\n\nRegards,\nMyself. + +[Ftp] +host=ftp.domain.com +login=login +password=password +folder=folder \ No newline at end of file diff --git a/tests/test.php b/tests/test.php index 1b046ab..c3764bc 100644 --- a/tests/test.php +++ b/tests/test.php @@ -1,14 +1,33 @@ folder +// test 1 +echo "starting test 1 \n"; +$file = TEST_FOLDER.'/test1'; +file_put_contents($file, 'test1'); +echo file_exists($file) ? 'OK' : 'KO'; +echo "\n"; +$backup = new Shikiryu_Backup_Files(array($file)); +$date = date('Ydm'); +$backup->addDate($date)->backupToFolder('bu'); +$backupFile = TEST_FOLDER.'/bu/'.$date.'.test1'; +if (file_exists($backupFile)) { + echo 'OK'; + unlink($backupFile); +} else { + die('KO - see what\'s in bu'); +} + +/* $backup->addDate()->addTime()->backupToEmail('from@gmail.com', 'to@gmail.com', 'test class', 'coucou'); $backup->addDate()->addTime()->backupToFTP('ftp.domain.com', 'login', 'password', '/folder'); -$backup->backupToFolder('bu'); $backup2 = new Shikiryu_Backup_MYSQL('localhost', 'login', 'password', 'db'); $backup2->fromTables(array('table1'))->addDate()->addTime(); $backup2->backupToFolder('bu'); ?> +*/ \ No newline at end of file