Blame view

common/components/mail/MailAttachmentsSaver.php 5.67 KB
02359b64   Mihail   add mail parser a...
1
2
3
4
5
6
7
8
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 01.09.2015
   * Time: 10:53
   */
  
7f6a9301   Mihail   add mails classes
9
  namespace common\components\mail;
02359b64   Mihail   add mail parser a...
10
11
  
  
57e5a4b9   Mihail   add classes to wo...
12
13
14
15
16
17
  /**
   * Class MailAttachmentsSaver
   * @package common\components\mail
   * сохраняет вложения в указанную папку по полученому соединению к ящику,
   * а также хранит имена сохраненных файлов
   */
8e7f5c9b   Mihail   add MailAttachmen...
18
19
  class MailAttachmentsSaver
  {
57e5a4b9   Mihail   add classes to wo...
20
21
22
23
  
      /**
       * @var соединение с ящиком - экземляр класса - MailReader
       */
7f6a9301   Mihail   add mails classes
24
      protected $mail_reader;
57e5a4b9   Mihail   add classes to wo...
25
26
27
28
  
      /**
       * @var - string, тип сообщений - например UNSEEN. Значения можно перечислять разделяя запятой.
       */
7f6a9301   Mihail   add mails classes
29
      protected $massage_type;
57e5a4b9   Mihail   add classes to wo...
30
31
32
33
  
      /**
       * @var array - после сохранения будет содержать сохраненные файлы, ключ - путь к файлу, значение - расширение
       */
d95262f3   Mihail   finish with mail ...
34
      protected $saved_files_arr = [];
57e5a4b9   Mihail   add classes to wo...
35
36
37
38
  
      /**
       * @var - префикс который будет прибавлен к оригинальному имени сохраняемого файла
       */
4f3f27e8   Mihail   temp commit - tes...
39
      protected $file_name_prefix;
8e7f5c9b   Mihail   add MailAttachmen...
40
  
4f3f27e8   Mihail   temp commit - tes...
41
42
43
  
  
      public function __construct(MailReader $mail_reader)
8e7f5c9b   Mihail   add MailAttachmen...
44
      {
7f6a9301   Mihail   add mails classes
45
          $this->mail_reader = $mail_reader;
4f3f27e8   Mihail   temp commit - tes...
46
  
57e5a4b9   Mihail   add classes to wo...
47
          $this->saved_files_arr = [];
7f6a9301   Mihail   add mails classes
48
  
8e7f5c9b   Mihail   add MailAttachmen...
49
      }
02359b64   Mihail   add mail parser a...
50
  
4f3f27e8   Mihail   temp commit - tes...
51
52
53
54
55
56
57
      /**
       * @param mixed $file_name_prefix
       */
      public function setFileNamePrefix($file_name_prefix)
      {
          $this->file_name_prefix = $file_name_prefix;
      }
02359b64   Mihail   add mail parser a...
58
  
4f3f27e8   Mihail   temp commit - tes...
59
      public function saveAttachmentsTo( $destination, $massage_type )
8e7f5c9b   Mihail   add MailAttachmen...
60
      {
4f3f27e8   Mihail   temp commit - tes...
61
          $this->massage_type = $massage_type;
57e5a4b9   Mihail   add classes to wo...
62
          $emails = $this->mail_reader->getEmails($this->massage_type);
02359b64   Mihail   add mail parser a...
63
64
  
          /* if emails are returned, cycle through each... */
4f3f27e8   Mihail   temp commit - tes...
65
          $result = false;
8e7f5c9b   Mihail   add MailAttachmen...
66
          if ($emails) {
02359b64   Mihail   add mail parser a...
67
68
69
70
71
72
  
              /* begin output var */
              $output = '';
  
              /* put the newest emails on top */
              rsort($emails);
57e5a4b9   Mihail   add classes to wo...
73
              // CustomVarDamp::dump($emails);
02359b64   Mihail   add mail parser a...
74
75
              foreach ($emails as $email_number) {
  
7f6a9301   Mihail   add mails classes
76
                  $structure = $this->mail_reader->getCurrentEmailStructure($email_number);
02359b64   Mihail   add mail parser a...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
                  $attachments = array();
                  if (isset($structure->parts) && count($structure->parts)) {
                      for ($i = 0; $i < count($structure->parts); $i++) {
                          $attachments[$i] = array(
                              'is_attachment' => false,
                              'filename' => '',
                              'name' => '',
                              'attachment' => '');
  
                          if ($structure->parts[$i]->ifdparameters) {
                              foreach ($structure->parts[$i]->dparameters as $object) {
                                  if (strtolower($object->attribute) == 'filename') {
                                      $attachments[$i]['is_attachment'] = true;
                                      $attachments[$i]['filename'] = $object->value;
                                  }
                              }
                          }
  
                          if ($structure->parts[$i]->ifparameters) {
                              foreach ($structure->parts[$i]->parameters as $object) {
                                  if (strtolower($object->attribute) == 'name') {
                                      $attachments[$i]['is_attachment'] = true;
                                      $attachments[$i]['name'] = $object->value;
                                  }
                              }
                          }
  
                          if ($attachments[$i]['is_attachment']) {
57e5a4b9   Mihail   add classes to wo...
105
                              $attachments[$i]['attachment'] = $this->mail_reader->getEmailBody($email_number, $i + 1);
02359b64   Mihail   add mail parser a...
106
107
108
109
110
111
                              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
                                  $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
                              }
                          }
8e7f5c9b   Mihail   add MailAttachmen...
112
                      }
02359b64   Mihail   add mail parser a...
113
  
8e7f5c9b   Mihail   add MailAttachmen...
114
                      if (count($attachments) != 0) {
02359b64   Mihail   add mail parser a...
115
  
57e5a4b9   Mihail   add classes to wo...
116
117
118
119
120
121
                          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']);
7f6a9301   Mihail   add mails classes
122
                                  }
57e5a4b9   Mihail   add classes to wo...
123
124
125
                                  $ext = pathinfo($name, PATHINFO_EXTENSION);
                                  mb_internal_encoding("UTF-8");
                                  file_put_contents($name, $val['attachment']);
d95262f3   Mihail   finish with mail ...
126
                                  // все извлеченные файлы сохраняем в $this->saved_files_arr
57e5a4b9   Mihail   add classes to wo...
127
                                  $this->setSavedFile( $name , $ext );
4f3f27e8   Mihail   temp commit - tes...
128
                                  $result = true;
8e7f5c9b   Mihail   add MailAttachmen...
129
                              }
8e7f5c9b   Mihail   add MailAttachmen...
130
  
57e5a4b9   Mihail   add classes to wo...
131
                          }
02359b64   Mihail   add mail parser a...
132
133
134
135
                      }
  
                  }
  
02359b64   Mihail   add mail parser a...
136
              }
7f6a9301   Mihail   add mails classes
137
  
02359b64   Mihail   add mail parser a...
138
          }
57e5a4b9   Mihail   add classes to wo...
139
  
4f3f27e8   Mihail   temp commit - tes...
140
          return $result;
57e5a4b9   Mihail   add classes to wo...
141
142
143
      }
  
      /**
d95262f3   Mihail   finish with mail ...
144
       * @return array - массив извлеченных файлов
57e5a4b9   Mihail   add classes to wo...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
       */
      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;
      }
  
  
02359b64   Mihail   add mail parser a...
160
  }