Blame view

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