Shikiryu_Backup/app/code/transport/Email.php

263 lines
8.0 KiB
PHP
Raw Normal View History

2015-07-03 00:00:30 +02:00
<?php
2015-07-10 19:07:14 +02:00
namespace Shikiryu\Backup\Transport;
2021-06-30 16:00:24 +02:00
use Exception;
2015-07-10 23:13:46 +02:00
use Shikiryu\Backup\Backup\BackupAbstract;
2021-06-30 16:00:24 +02:00
use ZIPARCHIVE;
2015-07-10 23:13:46 +02:00
2015-07-10 19:07:14 +02:00
class Email extends TransportAbstract
2015-07-09 00:46:17 +02:00
{
2015-07-03 00:00:30 +02:00
private $email_to;
private $email_from;
2015-07-09 00:46:17 +02:00
private $encoding;
private $subject;
private $message;
private $files;
private $streams;
2015-07-03 00:00:30 +02:00
public static $mimeTypes = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'xslt' => 'application/xslt+xml',
'xsl' => 'application/xml',
'dtd' => 'application/xml-dtd',
'atom' => 'application/atom+xml',
'mathml' => 'application/mathml+xml',
'rdf' => 'application/rdf+xml',
'smi' => 'application/smil',
'smil' => 'application/smil',
'vxml' => 'application/voicexml+xml',
'latex' => 'application/x-latex',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'wrl' => 'model/vrml',
'wrml' => 'model/vrml',
'ics' => 'text/calendar',
'ifb' => 'text/calendar',
'sgml' => 'text/sgml',
'htc' => 'text/x-component',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/x-icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
'tar' => 'application/x-tar',
'gz' => 'application/x-gzip',
'tgz' => 'application/x-gzip',
// audio/video
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'wav' => 'audio/wav',
'aiff' => 'audio/aiff',
'aif' => 'audio/aiff',
'avi' => 'video/msvideo',
'wmv' => 'video/x-ms-wmv',
'ogg' => 'application/ogg',
'flv' => 'video/x-flv',
'dvi' => 'application/x-dvi',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'm3u' => 'audio/x-mpegurl',
'm4u' => 'video/vnd.mpegurl',
'ram' => 'audio/x-pn-realaudio',
'ra' => 'audio/x-pn-realaudio',
'rm' => 'application/vnd.rn-realmedia',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'swf' => 'application/x-shockwave-flash',
// ms office
'doc' => 'application/msword',
'docx' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'xlm' => 'application/vnd.ms-excel',
'xla' => 'application/vnd.ms-excel',
'xld' => 'application/vnd.ms-excel',
'xlt' => 'application/vnd.ms-excel',
'xlc' => 'application/vnd.ms-excel',
'xlw' => 'application/vnd.ms-excel',
'xll' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'pps' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
/**
* Determine mime type
*
* @param string $file path to the file
*
* @return string
*/
2019-01-25 21:39:18 +01:00
public static function getMimeType($file)
{
2015-07-03 00:00:30 +02:00
if (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$type = (string) finfo_file($finfo, $file);
finfo_close($finfo);
return $type;
2019-01-25 22:19:50 +01:00
}
if (function_exists('mime_content_type')) {
2015-07-03 00:00:30 +02:00
return mime_content_type($file);
}
2019-01-25 22:19:50 +01:00
$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';
2015-07-03 00:00:30 +02:00
}
/**
2015-07-10 23:13:46 +02:00
* @param BackupAbstract $backup
2015-07-03 00:00:30 +02:00
*/
2019-01-25 21:39:18 +01:00
public function __construct(BackupAbstract $backup, array $config)
{
2015-07-10 23:13:46 +02:00
parent::__construct($backup, $config);
2019-01-25 22:19:50 +01:00
$this->setFiles($this->backup->getFilesToBackup());
$this->setStreams($this->backup->getStreamsToBackup());
$this->email_to = $this->config['to'];
$this->email_from = $this->config['from'];
2015-07-09 00:46:17 +02:00
$this->encoding = $this->config['encoding'];
2015-07-11 00:12:31 +02:00
$this->subject = $this->config['subject'];
2015-07-09 00:46:17 +02:00
$this->message = $this->config['message'];
$this->encoding = $this->config['encoding'];
2015-07-03 00:00:30 +02:00
}
2019-01-25 21:39:18 +01:00
/**
* @param array $files
*
* @return $this
*/
2015-07-09 00:46:17 +02:00
private function setFiles($files = array())
{
2015-07-03 00:00:30 +02:00
if (is_array($files) && !empty($files)) {
2015-07-09 00:46:17 +02:00
$this->files = $files;
2015-07-03 00:00:30 +02:00
}
return $this;
}
2015-07-09 00:46:17 +02:00
2019-01-25 21:39:18 +01:00
/**
* @param array $streams
*
* @return $this
*/
2015-07-09 00:46:17 +02:00
private function setStreams($streams = array())
{
2015-07-03 00:00:30 +02:00
if (is_array($streams) && !empty($streams)) {
2015-07-09 00:46:17 +02:00
$this->streams = $streams;
2015-07-03 00:00:30 +02:00
}
return $this;
}
/**
* Send the mail
*
* @see #mail
* @return bool
2021-06-30 16:00:24 +02:00
* @throws Exception
2015-07-03 00:00:30 +02:00
*/
2015-07-11 00:12:31 +02:00
public function send()
{
2015-07-12 20:34:54 +02:00
// TODO check if file is empty
2019-01-25 21:39:18 +01:00
// Checking files are selected
2021-06-30 16:00:24 +02:00
$zip = new ZipArchive(); // Load zip library
2019-01-25 22:19:50 +01:00
$zip_name = time(). '.zip'; // Zip name
2021-06-30 16:00:24 +02:00
if ($zip->open(TEMP_DIR.$zip_name, ZIPARCHIVE::CREATE)===true) {
2019-01-25 21:39:18 +01:00
if (!empty($this->files)) {
foreach ($this->files as $file => $name) {
2015-07-11 00:12:31 +02:00
$zip->addFile($file, $name); // Adding files into zip
2015-07-03 00:00:30 +02:00
}
}
2019-01-25 21:39:18 +01:00
$zip->close();
} else {
2021-06-30 16:00:24 +02:00
throw new Exception('Transport::Email::Can\'t zip the given backup.');
2015-07-11 00:12:31 +02:00
}
2019-01-25 21:39:18 +01:00
$this->files = array(TEMP_DIR.$zip_name=>$zip_name);
2015-07-03 00:00:30 +02:00
2019-01-25 22:19:50 +01:00
$random_hash = md5(date('r'));
$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 . '"';
2015-07-03 00:00:30 +02:00
$output = "
--$random_hash
2015-07-09 00:46:17 +02:00
Content-Type: text/plain; charset='" . strtolower($this->encoding) . "'
2015-07-03 00:00:30 +02:00
Content-Transfer-Encoding: 8bit
2015-07-09 00:46:17 +02:00
" . $this->message . "\r\n";
2015-07-03 00:00:30 +02:00
2019-01-25 21:39:18 +01:00
if (!empty($this->files)) {
foreach ($this->files as $file => $name) {
$output .= "
2015-07-03 00:00:30 +02:00
--$random_hash
2019-01-25 22:19:50 +01:00
Content-Type: " . self::getMimeType($file) . '; name=' . $name . '
2015-07-03 00:00:30 +02:00
Content-Transfer-Encoding: base64
2019-01-25 22:19:50 +01:00
Content-Disposition: attachment; filename=' . $name . '
2015-07-03 00:00:30 +02:00
2019-01-25 22:19:50 +01:00
' . chunk_split(base64_encode(file_get_contents($file)));
2019-01-25 21:39:18 +01:00
}
2015-07-03 00:00:30 +02:00
}
2019-01-25 21:39:18 +01:00
if (!empty($this->streams)) {
foreach ($this->streams as $name => $stream) {
2019-01-25 22:19:50 +01:00
if (substr_count($name, '.') + 1 <2) {
2019-01-25 21:39:18 +01:00
$name = 'backup'.$name.'.txt';
}
$output .= "
2015-07-03 00:00:30 +02:00
--$random_hash
2019-01-25 22:19:50 +01:00
Content-Type: text/plain; name=" . $name . '
2015-07-03 00:00:30 +02:00
Content-Transfer-Encoding: base64
2019-01-25 22:19:50 +01:00
Content-Disposition: attachment; filename=' . $name . '
2015-07-03 00:00:30 +02:00
2019-01-25 22:19:50 +01:00
' . chunk_split(base64_encode($stream));
2019-01-25 21:39:18 +01:00
}
2015-07-03 00:00:30 +02:00
}
$output.="--$random_hash--";
return mail($this->email_to, $this->subject, $output, $headers);
2015-07-03 00:00:30 +02:00
}
}