Commit 8f043ab6e9e18f83981c8be177de365988abdfb3

Authored by Mihail
1 parent 8b0defd0

add classes to work with archives

common/components/archive_reader/ArchiveCreator.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Tsurkanov
  5 + * Date: 03.11.2015
  6 + * Time: 14:51
  7 + */
  8 +
  9 +namespace common\components\archive_reader;
  10 +
  11 +
  12 +class ArchiveCreator {
  13 +
  14 + protected $handleExtension = [];
  15 +
  16 + public function create( $file, $ext ){
  17 + if ( isHandableExtension( $ext )) {
  18 + $arh_class = ucfirst( $ext ) . 'ArchiveReader';
  19 + if ( class_exists( $arh_class ) ) {
  20 +
  21 + $arh_reader = new $arh_class();
  22 +
  23 + if ($arh_reader instanceof ArchiveReader ) {
  24 + $arh_reader->open($file);
  25 + return $arh_reader;
  26 + }
  27 +
  28 + }
  29 +
  30 + }
  31 + // не найден подходящий обработчик
  32 + throw new \Exception( "Для расширения {$ext} не найден подходящий распаковщик" );
  33 + }
  34 +
  35 + protected function isHandableExtension( $ext ){
  36 +
  37 + $this->setHandleExtension( );
  38 + return (bool) array_search( $ext, $this->handleExtension);
  39 + }
  40 +
  41 + protected function setHandleExtension( ){
  42 + if ( !$this->handleExtension ) {
  43 + foreach (get_declared_classes() as $class) {
  44 + if (is_subclass_of( $class, ArchiveReader::class ))
  45 +
  46 + $this->handleExtension[] = $class::getExtension();
  47 +
  48 + }
  49 + }
  50 + }
  51 +
  52 + public function getHandleExtension( ){
  53 +
  54 + return $this->handleExtension;
  55 +
  56 + }
  57 +
  58 +
  59 +}
0 60 \ No newline at end of file
... ...
common/components/archive_reader/ArchiveReader.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Tsurkanov
  5 + * Date: 03.11.2015
  6 + * Time: 14:48
  7 + */
  8 +namespace common\components\archive_reader;
  9 +abstract class ArchiveReader
  10 +{
  11 +
  12 + protected $extracted_files = [];
  13 +
  14 + public abstract function open( $file, $password = '');
  15 +
  16 + public abstract function extractTo($destination);
  17 +
  18 + public static abstract function getExtension();
  19 +
  20 + public function getExtractedFiles(){
  21 + return $this->extracted_files;
  22 + }
  23 + public function setExtractedFiles($name, $ext){
  24 + $this->extracted_files[$name] = $ext;
  25 + }
  26 +
  27 +}
0 28 \ No newline at end of file
... ...
common/components/archive_reader/RarArchiveReader.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Tsurkanov
  5 + * Date: 03.11.2015
  6 + * Time: 15:12
  7 + */
  8 +
  9 +namespace common\components\archive_reader;
  10 +
  11 +
  12 +class RarArchiveReader extends ArchiveReader {
  13 +
  14 + public function open( $file, $password = '' ){
  15 + $rar_arch = \RarArchive::open( $file, $password );
  16 + if ($rar_arch === FALSE)
  17 + die("Failed opening file");
  18 + }
  19 +
  20 + public function extractTo( $destination){
  21 +
  22 + }
  23 + public static function getExtension(){
  24 + return 'rar';
  25 + }
  26 +
  27 +
  28 +
  29 +
  30 +}
0 31 \ No newline at end of file
... ...
common/components/mail/ImapMailReader.php
... ... @@ -17,7 +17,7 @@ class ImapMailReader extends MailReader {
17 17 $this->connection = imap_open($hostname, $username, $password);
18 18  
19 19 if ($this->connection === false)
20   - throw new \Exception('Cannot connect to Gmail: ' . imap_last_error());
  20 + throw new \Exception('Cannot connect to mail: ' . imap_last_error());
21 21  
22 22 }
23 23  
... ... @@ -26,6 +26,11 @@ class ImapMailReader extends MailReader {
26 26 return imap_search( $this->connection, $flag );
27 27 }
28 28  
  29 + public function getEmailBody( $email_number, $section )
  30 + {
  31 + return imap_fetchbody($this->connection, $email_number, $section );
  32 + }
  33 +
29 34 /**
30 35 * @return mixed
31 36 */
... ...
common/components/mail/MailAttachmentsSaver.php
... ... @@ -11,24 +11,47 @@ namespace common\components\mail;
11 11  
12 12 use common\components\CustomVarDamp;
13 13  
  14 +/**
  15 + * Class MailAttachmentsSaver
  16 + * @package common\components\mail
  17 + * сохраняет вложения в указанную папку по полученому соединению к ящику,
  18 + * а также хранит имена сохраненных файлов
  19 + */
14 20 class MailAttachmentsSaver
15 21 {
  22 +
  23 + /**
  24 + * @var соединение с ящиком - экземляр класса - MailReader
  25 + */
16 26 protected $mail_reader;
  27 +
  28 + /**
  29 + * @var - string, тип сообщений - например UNSEEN. Значения можно перечислять разделяя запятой.
  30 + */
17 31 protected $massage_type;
  32 +
  33 + /**
  34 + * @var array - после сохранения будет содержать сохраненные файлы, ключ - путь к файлу, значение - расширение
  35 + */
  36 + protected $saved_files_arr;
  37 +
  38 + /**
  39 + * @var - префикс который будет прибавлен к оригинальному имени сохраняемого файла
  40 + */
18 41 public $file_name_prefix;
19 42  
20   - public function __construct( MailReader $mail_reader, $massage_type )
  43 + public function __construct(MailReader $mail_reader, $massage_type)
21 44 {
22 45 $this->mail_reader = $mail_reader;
23 46 $this->massage_type = $massage_type;
  47 + $this->saved_files_arr = [];
24 48  
25 49 }
26 50  
27 51  
28 52 public function saveAttachmentsTo( $destination )
29 53 {
30   -
31   - $emails = $this->mail_reader->getEmails( $this->massage_type );
  54 + $emails = $this->mail_reader->getEmails($this->massage_type);
32 55  
33 56 /* if emails are returned, cycle through each... */
34 57  
... ... @@ -39,7 +62,7 @@ class MailAttachmentsSaver
39 62  
40 63 /* put the newest emails on top */
41 64 rsort($emails);
42   - // CustomVarDamp::dump($emails);
  65 + // CustomVarDamp::dump($emails);
43 66 foreach ($emails as $email_number) {
44 67  
45 68 $structure = $this->mail_reader->getCurrentEmailStructure($email_number);
... ... @@ -71,7 +94,7 @@ class MailAttachmentsSaver
71 94 }
72 95  
73 96 if ($attachments[$i]['is_attachment']) {
74   - $attachments[$i]['attachment'] = imap_fetchbody($this->mail_reader->connection, $email_number, $i + 1);
  97 + $attachments[$i]['attachment'] = $this->mail_reader->getEmailBody($email_number, $i + 1);
75 98 if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
76 99 $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
77 100 } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
... ... @@ -82,23 +105,20 @@ class MailAttachmentsSaver
82 105  
83 106 if (count($attachments) != 0) {
84 107  
85   - foreach($attachments as $key => &$val){
86   - if ($val['is_attachment'] == 1) {
87   -
88   - if( isset($this->file_name_prefix) ){
89   - $name = $destination . '/' . $this->file_name_prefix . mb_decode_mimeheader($val['name']);
90   - }else{
91   - $name = $destination . '/' . mb_decode_mimeheader($val['name']);
92   - }
93   - mb_internal_encoding("UTF-8");
94   -
95   - //file_put_contents(\Yii::getAlias('@temp_upload') . '/' . $name, $val['attachment']);
96   - file_put_contents( $name, $val['attachment'] );
  108 + foreach ($attachments as $key => &$val) {
  109 + if ($val['is_attachment'] == 1) {
  110 + if (isset($this->file_name_prefix)) {
  111 + $name = $destination . '/' . $this->file_name_prefix . mb_decode_mimeheader($val['name']);
  112 + } else {
  113 + $name = $destination . '/' . mb_decode_mimeheader($val['name']);
97 114 }
98   -
  115 + $ext = pathinfo($name, PATHINFO_EXTENSION);
  116 + mb_internal_encoding("UTF-8");
  117 + file_put_contents($name, $val['attachment']);
  118 + $this->setSavedFile( $name , $ext );
99 119 }
100   - }
101 120  
  121 + }
102 122 }
103 123  
104 124 }
... ... @@ -106,4 +126,24 @@ class MailAttachmentsSaver
106 126 }
107 127  
108 128 }
  129 +
  130 + }
  131 +
  132 + /**
  133 + * @return array
  134 + */
  135 + public function getSavedFilesArr()
  136 + {
  137 + return $this->saved_files_arr;
  138 + }
  139 +
  140 + /**
  141 + * @param array $saved_files_arr
  142 + */
  143 + public function setSavedFile($saved_file, $saved_file_ext)
  144 + {
  145 + $this->saved_files_arr[$saved_file] = $saved_file_ext;
  146 + }
  147 +
  148 +
109 149 }
110 150 \ No newline at end of file
... ...
common/components/mail/MailReader.php
... ... @@ -35,12 +35,12 @@ abstract class MailReader {
35 35 return $this->hostname;
36 36 }
37 37  
38   -
39   -
40 38 public abstract function getListMailboxes();
41 39  
42 40 public abstract function getEmails( $flag );
43 41  
  42 + public abstract function getEmailBody( $email_number, $section );
  43 +
44 44 public abstract function getCurrentEmailStructure( $email );
45 45  
46 46 public abstract function reOpen( $hostname );
... ...
console/controllers/ParserController.php
1 1 <?php
2 2 namespace console\controllers;
3 3  
  4 +use common\components\archive_reader\ArchiveCreator;
4 5 use common\components\CustomVarDamp;
5 6 use common\components\mail\ImapMailReader;
6 7 use common\components\mail\MailAttachmentsSaver;
... ... @@ -116,7 +117,7 @@ class ParserController extends Controller
116 117  
117 118 public function actionTest()
118 119 {
119   - Console::output('It is working ');
  120 + Console::output('It is working');
120 121 \Yii::info('2', 'parser');
121 122  
122 123 }
... ... @@ -127,19 +128,35 @@ class ParserController extends Controller
127 128  
128 129 $mail_reader = new ImapMailReader( '{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'tsurkanovm@gmail.com', 'Wtvr@2000' );
129 130 $mailboxes = $mail_reader->getListMailboxes();
130   - foreach ($mailboxes as $custom_label) {
131   - $words = explode(" ",str_replace(array($mail_reader->getHostname(),"!"),"",imap_utf7_decode($custom_label)));
132   - $importer_id = (int)preg_replace("/[^A-Z0-9]+/","",strtoupper($words[0]));
  131 + foreach ( $mailboxes as $custom_label ) {
  132 + $words = explode(" ",str_replace( array($mail_reader->getHostname(),"!"),"",imap_utf7_decode($custom_label)) );
  133 + $importer_id = (int)preg_replace("/[^A-Z0-9]+/","", strtoupper($words[0]));
133 134  
134   - $mail_reader->reOpen($custom_label);
  135 + $mail_reader->reOpen( $custom_label );
135 136 $saver = new MailAttachmentsSaver( $mail_reader, 'UNSEEN' );
136 137 $saver->file_name_parefix = $importer_id . '~!~';
137 138 $saver->saveAttachmentsTo(\Yii::getAlias('@temp_upload'));
138 139  
139   -// $files_model = new ImportersFiles();
140   -// $files_model->importer_id = $importer_id;
141 140 }
142 141  
  142 + $files = $saver->getSavedFilesArr();
  143 + $arch_creator = new ArchiveCreator();
  144 + $arch_extensions = $arch_creator->getHandleExtension();
  145 + $files_on_unpack = array_intersect( $files , $arch_extensions );
  146 + foreach ($files_on_unpack as $arch_ext => $arch_name) {
  147 + $arch_reader = $arch_creator->create( $arch_name, $arch_ext);
  148 + $arch_reader->extractTo(\Yii::getAlias('@temp_upload'));
  149 + unset( $files[$arch_name] );
  150 + $files = array_merge( $files, $arch_reader->getExtractedFiles());
  151 + }
  152 +
  153 + foreach ( $files as $file => $key ) {
  154 +
  155 +
  156 + // $files_model = new ImportersFiles();
  157 +// $files_model->importer_id = $importer_id;
  158 + // $db->lastInsertId()
  159 + }
143 160  
144 161 }
145 162 }
146 163 \ No newline at end of file
... ...