-
Notifications
You must be signed in to change notification settings - Fork 1
/
BitFunnel.php
214 lines (153 loc) · 6.22 KB
/
BitFunnel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
class BitFunnel
{
private $server;
private $connection;
private $emails;
public $errors;
public $attachments;
public function __construct($account)
{
$this->server = $account['server'];
$this->savePath = $account['savePath'];
$this->errors = array();
$this->attachments = array();
//connect to the mailbox based on the server properties
$this->connection = $this->connectToMailbox();
//get the emails (default scope is ALL)
$this->emails = $this->fetchRawEmails();
//sort the mail
$this->sortMailOrder();
//process the mail, strip attachments, save them to the server
$this->processMailBatch();
}
/*
Takes the current server and formats a connection string for imap_open
*/
private function formatHostName()
{
return "{".$this->server['host'].":".$this->server['port']. implode("/", $this->server['flags']) . "}" . $this->server['mailbox'];
}
/*
Establishes a connection to the mailbox, returns the imap stream
*/
private function connectToMailbox()
{
$this->connection = imap_open($this->formatHostname($this->server),$this->server['user'],$this->server['password']);
if ($this->connection) {
return $this->connection;
} else {
$this->errors[] = "Could not connect to mailbox on server.";
$this->errors[] = imap_last_error();
return false;
}
}
private function fetchRawEmails($scope = "ALL")
{
if (!$this->connection) {
$this->errors[] = "Could not fetch e-mails, connection to mailbox not present.";
return false;
} else {
$this->emails = imap_search($this->connection,$scope);
if (is_array($this->emails)) {
return $this->emails;
} else {
if ($scope != "ALL") {
$this->errors[] = "No e-mails found in mailbox using that scope.";
} else {
$this->errors[] = "No e-mails found in mailbox.";
}
return false;
}
}
}
/*
Sorts the emails to the newest first
Currently just uses rsort, will expand this later.
*/
private function sortMailOrder()
{
rsort($this->emails);
}
/*
Take the e-mails and process them
*/
private function processMailBatch()
{
/* for every email... */
foreach ($this->emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($this->connection,$email_number,0);
$message = imap_fetchbody($this->connection,$email_number,2);
$structure = imap_fetchstructure($this->connection,$email_number);
$parts = $structure->parts;
$hasAttachments = false;
//saves emails to $this->attachments
$this->extract_attachments($email_number);
}
if (is_array($this->attachments) && count($this->attachments)) {
$this->saveAttachmentsToDisk();
}
imap_close($this->connection);
return true;
}
private function saveAttachmentsToDisk()
{
foreach ($this->attachments as $a) {
//$output.= "<span class=\"date\"> <strong> " . $a["name"] . "</strong></span> ";
$pathToEndFile = $this->savePath . $a["name"];
if (!file_exists($pathToEndFile)) {
//save the file to the server
$fp=fopen($pathToEndFile,"w+");
fwrite($fp,$a["data"]);
fclose($fp);
}
}
}
//search the message body for attachments in the raw content
//it takes a imap connection and a message number
private function extractAttachments($email_number)
{
$attachments = array();
$structure = imap_fetchstructure($this->connection, $email_number);
if (isset($structure->parts) && count($structure->parts)) {
$this->attachments[] = $this->scanAndRipAttachmentsFromParts($sructure, $email_number);
} else {
//no attachments found
}
}
private function scanAndRipAttachmentsFromStructure($structure, $email_number)
{
$attachments = array();
//loop through each part of the message
for ($i = 0; $i < count($structure->parts); $i++) {
//look through the parameters of this part to see if it contains indicators of an attachment (for propertly formatted messages)
if ($structure->parts[$i]->ifdparameters) {
foreach ($structure->parts[$i]->dparameters as $object) {
//check_object_for_filename($object);
$attribute = strtolower($object->attribute);
if ($attribute == 'filename' || $attribute == "name") {
//attachment found!
$attachments[$i]['name'] = $object->value;
$attachments[$i]['data'] = imap_fetchbody($this->connection, $email_number, $i+1);
if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['data'] = base64_decode($attachments[$i]['data']);
} elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['data'] = quoted_printable_decode($attachments[$i]['data']);
}
}
}
}
//depending on the authoring environment of the message, attachments may be present in a sub part
//of the content, if this is present, check it for the presence of params and dump to the previous
//attachment array as a recursive process
if ($structure->parts[$i]->parts) {
$subAttachments = $this->extractAttachments($structure->parts[$i], $email_number);
if (is_array($subAttachments) && count($subAttachments)) {
array_push($attachments, $subAttachments);
}
}
}
return $attachments;
}
}