From 8f043ab6e9e18f83981c8be177de365988abdfb3 Mon Sep 17 00:00:00 2001 From: Mihail Date: Tue, 3 Nov 2015 16:44:33 +0200 Subject: [PATCH] add classes to work with archives --- common/components/archive_reader/ArchiveCreator.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/components/archive_reader/ArchiveReader.php | 27 +++++++++++++++++++++++++++ common/components/archive_reader/RarArchiveReader.php | 30 ++++++++++++++++++++++++++++++ common/components/mail/ImapMailReader.php | 7 ++++++- common/components/mail/MailAttachmentsSaver.php | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------- common/components/mail/MailReader.php | 4 ++-- console/controllers/ParserController.php | 31 ++++++++++++++++++++++++------- 7 files changed, 207 insertions(+), 29 deletions(-) create mode 100644 common/components/archive_reader/ArchiveCreator.php create mode 100644 common/components/archive_reader/ArchiveReader.php create mode 100644 common/components/archive_reader/RarArchiveReader.php diff --git a/common/components/archive_reader/ArchiveCreator.php b/common/components/archive_reader/ArchiveCreator.php new file mode 100644 index 0000000..813a642 --- /dev/null +++ b/common/components/archive_reader/ArchiveCreator.php @@ -0,0 +1,59 @@ +open($file); + return $arh_reader; + } + + } + + } + // не найден подходящий обработчик + throw new \Exception( "Для расширения {$ext} не найден подходящий распаковщик" ); + } + + protected function isHandableExtension( $ext ){ + + $this->setHandleExtension( ); + return (bool) array_search( $ext, $this->handleExtension); + } + + protected function setHandleExtension( ){ + if ( !$this->handleExtension ) { + foreach (get_declared_classes() as $class) { + if (is_subclass_of( $class, ArchiveReader::class )) + + $this->handleExtension[] = $class::getExtension(); + + } + } + } + + public function getHandleExtension( ){ + + return $this->handleExtension; + + } + + +} \ No newline at end of file diff --git a/common/components/archive_reader/ArchiveReader.php b/common/components/archive_reader/ArchiveReader.php new file mode 100644 index 0000000..bb02faf --- /dev/null +++ b/common/components/archive_reader/ArchiveReader.php @@ -0,0 +1,27 @@ +extracted_files; + } + public function setExtractedFiles($name, $ext){ + $this->extracted_files[$name] = $ext; + } + +} \ No newline at end of file diff --git a/common/components/archive_reader/RarArchiveReader.php b/common/components/archive_reader/RarArchiveReader.php new file mode 100644 index 0000000..b487aff --- /dev/null +++ b/common/components/archive_reader/RarArchiveReader.php @@ -0,0 +1,30 @@ +connection = imap_open($hostname, $username, $password); if ($this->connection === false) - throw new \Exception('Cannot connect to Gmail: ' . imap_last_error()); + throw new \Exception('Cannot connect to mail: ' . imap_last_error()); } @@ -26,6 +26,11 @@ class ImapMailReader extends MailReader { return imap_search( $this->connection, $flag ); } + public function getEmailBody( $email_number, $section ) + { + return imap_fetchbody($this->connection, $email_number, $section ); + } + /** * @return mixed */ diff --git a/common/components/mail/MailAttachmentsSaver.php b/common/components/mail/MailAttachmentsSaver.php index 616f909..34b028d 100644 --- a/common/components/mail/MailAttachmentsSaver.php +++ b/common/components/mail/MailAttachmentsSaver.php @@ -11,24 +11,47 @@ namespace common\components\mail; use common\components\CustomVarDamp; +/** + * Class MailAttachmentsSaver + * @package common\components\mail + * сохраняет вложения в указанную папку по полученому соединению к ящику, + * а также хранит имена сохраненных файлов + */ class MailAttachmentsSaver { + + /** + * @var соединение с ящиком - экземляр класса - MailReader + */ protected $mail_reader; + + /** + * @var - string, тип сообщений - например UNSEEN. Значения можно перечислять разделяя запятой. + */ protected $massage_type; + + /** + * @var array - после сохранения будет содержать сохраненные файлы, ключ - путь к файлу, значение - расширение + */ + protected $saved_files_arr; + + /** + * @var - префикс который будет прибавлен к оригинальному имени сохраняемого файла + */ public $file_name_prefix; - public function __construct( MailReader $mail_reader, $massage_type ) + public function __construct(MailReader $mail_reader, $massage_type) { $this->mail_reader = $mail_reader; $this->massage_type = $massage_type; + $this->saved_files_arr = []; } public function saveAttachmentsTo( $destination ) { - - $emails = $this->mail_reader->getEmails( $this->massage_type ); + $emails = $this->mail_reader->getEmails($this->massage_type); /* if emails are returned, cycle through each... */ @@ -39,7 +62,7 @@ class MailAttachmentsSaver /* put the newest emails on top */ rsort($emails); - // CustomVarDamp::dump($emails); + // CustomVarDamp::dump($emails); foreach ($emails as $email_number) { $structure = $this->mail_reader->getCurrentEmailStructure($email_number); @@ -71,7 +94,7 @@ class MailAttachmentsSaver } if ($attachments[$i]['is_attachment']) { - $attachments[$i]['attachment'] = imap_fetchbody($this->mail_reader->connection, $email_number, $i + 1); + $attachments[$i]['attachment'] = $this->mail_reader->getEmailBody($email_number, $i + 1); if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64 $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE @@ -82,23 +105,20 @@ class MailAttachmentsSaver if (count($attachments) != 0) { - foreach($attachments as $key => &$val){ - if ($val['is_attachment'] == 1) { - - if( isset($this->file_name_prefix) ){ - $name = $destination . '/' . $this->file_name_prefix . mb_decode_mimeheader($val['name']); - }else{ - $name = $destination . '/' . mb_decode_mimeheader($val['name']); - } - mb_internal_encoding("UTF-8"); - - //file_put_contents(\Yii::getAlias('@temp_upload') . '/' . $name, $val['attachment']); - file_put_contents( $name, $val['attachment'] ); + foreach ($attachments as $key => &$val) { + if ($val['is_attachment'] == 1) { + if (isset($this->file_name_prefix)) { + $name = $destination . '/' . $this->file_name_prefix . mb_decode_mimeheader($val['name']); + } else { + $name = $destination . '/' . mb_decode_mimeheader($val['name']); } - + $ext = pathinfo($name, PATHINFO_EXTENSION); + mb_internal_encoding("UTF-8"); + file_put_contents($name, $val['attachment']); + $this->setSavedFile( $name , $ext ); } - } + } } } @@ -106,4 +126,24 @@ class MailAttachmentsSaver } } + + } + + /** + * @return array + */ + public function getSavedFilesArr() + { + return $this->saved_files_arr; + } + + /** + * @param array $saved_files_arr + */ + public function setSavedFile($saved_file, $saved_file_ext) + { + $this->saved_files_arr[$saved_file] = $saved_file_ext; + } + + } \ No newline at end of file diff --git a/common/components/mail/MailReader.php b/common/components/mail/MailReader.php index eb778cc..3a749ad 100644 --- a/common/components/mail/MailReader.php +++ b/common/components/mail/MailReader.php @@ -35,12 +35,12 @@ abstract class MailReader { return $this->hostname; } - - public abstract function getListMailboxes(); public abstract function getEmails( $flag ); + public abstract function getEmailBody( $email_number, $section ); + public abstract function getCurrentEmailStructure( $email ); public abstract function reOpen( $hostname ); diff --git a/console/controllers/ParserController.php b/console/controllers/ParserController.php index c43ba4e..8cc6b46 100644 --- a/console/controllers/ParserController.php +++ b/console/controllers/ParserController.php @@ -1,6 +1,7 @@ getListMailboxes(); - foreach ($mailboxes as $custom_label) { - $words = explode(" ",str_replace(array($mail_reader->getHostname(),"!"),"",imap_utf7_decode($custom_label))); - $importer_id = (int)preg_replace("/[^A-Z0-9]+/","",strtoupper($words[0])); + foreach ( $mailboxes as $custom_label ) { + $words = explode(" ",str_replace( array($mail_reader->getHostname(),"!"),"",imap_utf7_decode($custom_label)) ); + $importer_id = (int)preg_replace("/[^A-Z0-9]+/","", strtoupper($words[0])); - $mail_reader->reOpen($custom_label); + $mail_reader->reOpen( $custom_label ); $saver = new MailAttachmentsSaver( $mail_reader, 'UNSEEN' ); $saver->file_name_parefix = $importer_id . '~!~'; $saver->saveAttachmentsTo(\Yii::getAlias('@temp_upload')); -// $files_model = new ImportersFiles(); -// $files_model->importer_id = $importer_id; } + $files = $saver->getSavedFilesArr(); + $arch_creator = new ArchiveCreator(); + $arch_extensions = $arch_creator->getHandleExtension(); + $files_on_unpack = array_intersect( $files , $arch_extensions ); + foreach ($files_on_unpack as $arch_ext => $arch_name) { + $arch_reader = $arch_creator->create( $arch_name, $arch_ext); + $arch_reader->extractTo(\Yii::getAlias('@temp_upload')); + unset( $files[$arch_name] ); + $files = array_merge( $files, $arch_reader->getExtractedFiles()); + } + + foreach ( $files as $file => $key ) { + + + // $files_model = new ImportersFiles(); +// $files_model->importer_id = $importer_id; + // $db->lastInsertId() + } } } \ No newline at end of file -- libgit2 0.21.4