Blame view

common/components/mail/MailAttachmentsSaver.php 5.26 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 - префикс который будет прибавлен к оригинальному имени сохраняемого файла
       */
7f6a9301   Mihail   add mails classes
41
      public $file_name_prefix;
8e7f5c9b   Mihail   add MailAttachmen...
42
  
57e5a4b9   Mihail   add classes to wo...
43
      public function __construct(MailReader $mail_reader, $massage_type)
8e7f5c9b   Mihail   add MailAttachmen...
44
      {
7f6a9301   Mihail   add mails classes
45
46
          $this->mail_reader = $mail_reader;
          $this->massage_type = $massage_type;
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
  
02359b64   Mihail   add mail parser a...
51
  
7f6a9301   Mihail   add mails classes
52
      public function saveAttachmentsTo( $destination )
8e7f5c9b   Mihail   add MailAttachmen...
53
      {
57e5a4b9   Mihail   add classes to wo...
54
          $emails = $this->mail_reader->getEmails($this->massage_type);
02359b64   Mihail   add mail parser a...
55
56
  
          /* if emails are returned, cycle through each... */
7f6a9301   Mihail   add mails classes
57
  
8e7f5c9b   Mihail   add MailAttachmen...
58
          if ($emails) {
02359b64   Mihail   add mail parser a...
59
60
61
62
63
64
  
              /* begin output var */
              $output = '';
  
              /* put the newest emails on top */
              rsort($emails);
57e5a4b9   Mihail   add classes to wo...
65
              // CustomVarDamp::dump($emails);
02359b64   Mihail   add mail parser a...
66
67
              foreach ($emails as $email_number) {
  
7f6a9301   Mihail   add mails classes
68
                  $structure = $this->mail_reader->getCurrentEmailStructure($email_number);
02359b64   Mihail   add mail parser a...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
                  $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...
97
                              $attachments[$i]['attachment'] = $this->mail_reader->getEmailBody($email_number, $i + 1);
02359b64   Mihail   add mail parser a...
98
99
100
101
102
103
                              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...
104
                      }
02359b64   Mihail   add mail parser a...
105
  
8e7f5c9b   Mihail   add MailAttachmen...
106
                      if (count($attachments) != 0) {
02359b64   Mihail   add mail parser a...
107
  
57e5a4b9   Mihail   add classes to wo...
108
109
110
111
112
113
                          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
114
                                  }
57e5a4b9   Mihail   add classes to wo...
115
116
117
118
                                  $ext = pathinfo($name, PATHINFO_EXTENSION);
                                  mb_internal_encoding("UTF-8");
                                  file_put_contents($name, $val['attachment']);
                                  $this->setSavedFile( $name , $ext );
8e7f5c9b   Mihail   add MailAttachmen...
119
                              }
8e7f5c9b   Mihail   add MailAttachmen...
120
  
57e5a4b9   Mihail   add classes to wo...
121
                          }
02359b64   Mihail   add mail parser a...
122
123
124
125
                      }
  
                  }
  
02359b64   Mihail   add mail parser a...
126
              }
7f6a9301   Mihail   add mails classes
127
  
02359b64   Mihail   add mail parser a...
128
          }
57e5a4b9   Mihail   add classes to wo...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  
      }
  
      /**
       * @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...
149
  }