mirror of
https://github.com/Chouchen/Shikiryu_Backup.git
synced 2021-06-30 16:02:14 +02:00
🎨 Apply PSR2
This commit is contained in:
parent
ab7887ce84
commit
db51375185
@ -125,7 +125,8 @@ class Email extends TransportAbstract
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getMimeType($file) {
|
||||
public static function getMimeType($file)
|
||||
{
|
||||
if (function_exists('finfo_open')) {
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$type = (string) finfo_file($finfo, $file);
|
||||
@ -135,17 +136,19 @@ class Email extends TransportAbstract
|
||||
return mime_content_type($file);
|
||||
} else {
|
||||
$ext = strtolower(array_pop(explode('.', $file)));
|
||||
if (array_key_exists($ext, self::$mimeTypes))
|
||||
if (array_key_exists($ext, self::$mimeTypes)) {
|
||||
return self::$mimeTypes[$ext];
|
||||
else
|
||||
} else {
|
||||
return 'application/octet-stream';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BackupAbstract $backup
|
||||
*/
|
||||
public function __construct(BackupAbstract $backup, array $config) {
|
||||
public function __construct(BackupAbstract $backup, array $config)
|
||||
{
|
||||
parent::__construct($backup, $config);
|
||||
$this->setFiles($this->backup->getFilesTobackup());
|
||||
$this->setStreams($this->backup->getStreamsTobackup());
|
||||
@ -157,11 +160,11 @@ class Email extends TransportAbstract
|
||||
$this->encoding = $this->config['encoding'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $files
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
/**
|
||||
* @param array $files
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
private function setFiles($files = array())
|
||||
{
|
||||
if (is_array($files) && !empty($files)) {
|
||||
@ -170,11 +173,11 @@ class Email extends TransportAbstract
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $streams
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
/**
|
||||
* @param array $streams
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
private function setStreams($streams = array())
|
||||
{
|
||||
if (is_array($streams) && !empty($streams)) {
|
||||
@ -194,23 +197,22 @@ class Email extends TransportAbstract
|
||||
{
|
||||
|
||||
// TODO check if file is empty
|
||||
|
||||
// Checking files are selected
|
||||
$zip = new \ZipArchive(); // Load zip library
|
||||
$zip_name = time().".zip"; // Zip name
|
||||
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)==TRUE) {
|
||||
if(!empty($this->files)) {
|
||||
foreach($this->files as $file => $name)
|
||||
{
|
||||
|
||||
// Checking files are selected
|
||||
$zip = new \ZipArchive(); // Load zip library
|
||||
$zip_name = time().".zip"; // Zip name
|
||||
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)==true) {
|
||||
if (!empty($this->files)) {
|
||||
foreach ($this->files as $file => $name) {
|
||||
$zip->addFile($file, $name); // Adding files into zip
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
} else {
|
||||
$zip->close();
|
||||
} else {
|
||||
throw new \Exception('Transport::Email::Can\'t zip the given backup.');
|
||||
}
|
||||
|
||||
$this->files = array(TEMP_DIR.$zip_name=>$zip_name);
|
||||
|
||||
$this->files = array(TEMP_DIR.$zip_name=>$zip_name);
|
||||
|
||||
$random_hash = md5(date('r', time()));
|
||||
$headers = "From: " . $this->email_from . "\r\nReply-To: " . $this->email_from;
|
||||
@ -223,30 +225,33 @@ Content-Transfer-Encoding: 8bit
|
||||
|
||||
" . $this->message . "\r\n";
|
||||
|
||||
if(!empty($this->files))
|
||||
foreach($this->files as $file => $name) {
|
||||
$output .= "
|
||||
if (!empty($this->files)) {
|
||||
foreach ($this->files as $file => $name) {
|
||||
$output .= "
|
||||
--$random_hash
|
||||
Content-Type: " . self::getMimeType($file) . "; name=" . $name . "
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Disposition: attachment; filename=" . $name . "
|
||||
|
||||
" . chunk_split(base64_encode(file_get_contents($file)));
|
||||
}
|
||||
}
|
||||
|
||||
if(!empty($this->streams))
|
||||
foreach($this->streams as $name=>$stream) {
|
||||
if(count(explode('.',$name))<2) $name = 'backup'.$name.'.txt';
|
||||
$output .= "
|
||||
if (!empty($this->streams)) {
|
||||
foreach ($this->streams as $name => $stream) {
|
||||
if (count(explode('.', $name))<2) {
|
||||
$name = 'backup'.$name.'.txt';
|
||||
}
|
||||
$output .= "
|
||||
--$random_hash
|
||||
Content-Type: text/plain; name=" . $name . "
|
||||
Content-Transfer-Encoding: base64
|
||||
Content-Disposition: attachment; filename=" . $name . "
|
||||
|
||||
" . chunk_split(base64_encode($stream));
|
||||
}
|
||||
}
|
||||
$output.="--$random_hash--";
|
||||
return mail($this->email_to, $this->subject, $output, $headers);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,42 +6,42 @@ use Shikiryu\Backup\Backup\BackupAbstract;
|
||||
|
||||
class Folder extends TransportAbstract
|
||||
{
|
||||
/**
|
||||
* Folder to backup
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ abstract class BackupAbstract
|
||||
*
|
||||
* @param array $config array of options and parameters
|
||||
*/
|
||||
function __construct($config = array())
|
||||
public function __construct($config = array())
|
||||
{
|
||||
|
||||
$this->options = !empty($config['options']) ? $config['options'] : [];
|
||||
@ -45,7 +45,7 @@ abstract class BackupAbstract
|
||||
*
|
||||
* @param string $name attribute name
|
||||
* @param mixed $value attribute value
|
||||
*
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
@ -99,7 +99,7 @@ abstract class BackupAbstract
|
||||
|
||||
/**
|
||||
* Initialize everything
|
||||
*
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function init()
|
||||
@ -114,21 +114,21 @@ abstract class BackupAbstract
|
||||
|
||||
/**
|
||||
* Function that can be used to initialize the backup
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function preBuild();
|
||||
|
||||
/**
|
||||
* Function that can be used after the backup
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function postBuild();
|
||||
|
||||
/**
|
||||
* Mandatory function doing the backup
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function build();
|
||||
@ -142,7 +142,7 @@ abstract class BackupAbstract
|
||||
|
||||
/**
|
||||
* Apply options
|
||||
*
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function applyOptions()
|
||||
@ -174,7 +174,7 @@ abstract class BackupAbstract
|
||||
if (touch(TEMP_DIR . $zip_name) === false) {
|
||||
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) {
|
||||
$zip->addFile($file, $name); // Adding files into zip
|
||||
}
|
||||
@ -257,7 +257,7 @@ abstract class BackupAbstract
|
||||
|
||||
/**
|
||||
* Set option name
|
||||
*
|
||||
*
|
||||
* @param mixed $name option's name
|
||||
*
|
||||
* @throws \Exception
|
||||
@ -270,5 +270,4 @@ abstract class BackupAbstract
|
||||
throw new \Exception('name option is for zip only.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,20 +14,20 @@ class Files extends BackupAbstract
|
||||
throw new \Exception('Files needs a "files" configuration.');
|
||||
}
|
||||
$filesToBackup = $config['files'];
|
||||
if(!empty($filesToBackup) && is_array($filesToBackup)){
|
||||
$names = array_map("basename",$filesToBackup);
|
||||
$this->files_to_backup = array_combine($filesToBackup,$names);
|
||||
if (!empty($filesToBackup) && is_array($filesToBackup)) {
|
||||
$names = array_map("basename", $filesToBackup);
|
||||
$this->files_to_backup = array_combine($filesToBackup, $names);
|
||||
}
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the backup is valid
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @SuppressWarnings("unused")
|
||||
*/
|
||||
/**
|
||||
* Check if the backup is valid
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @SuppressWarnings("unused")
|
||||
*/
|
||||
public function isValid()
|
||||
{
|
||||
$result = true;
|
||||
@ -39,29 +39,28 @@ class Files extends BackupAbstract
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Loading…
Reference in New Issue
Block a user