-
Notifications
You must be signed in to change notification settings - Fork 0
/
getmail.php
296 lines (214 loc) · 10.2 KB
/
getmail.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/current_pg_function.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/global-settings.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/pre-function.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/classes/PHPMailer/PHPMailerAutoload.php');
$php_temp = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/temps/letter-tmp.php');
function getFileExtension($fileName){
$parts = explode(".",$fileName);
return $parts[count($parts)-1];
}
$imapPath = "{".TLW_SMTP_HOST.":143/imap/notls/novalidate-cert/user=esign}INBOX";
$username = TLW_SOURCE_EMAIL;
$password = TLW_SMTP_PWD;
$inbox = imap_open($imapPath, $username, $password, NULL, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));
$log_date = date('Y-m-d', time());
$prev_log_date = date('Y-m-d', strtotime($log_date.'- 1 day'));
//pre($inbox);
// Check if Unsigned logs for prev day extists
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/logs/unsigned-'.$prev_log_date.'.log')) {
$prev_unsigned_src = $_SERVER['DOCUMENT_ROOT'].'/admin/logs/unsigned-'.$prev_log_date.'.log';
$prev_unsigned_dest = $_SERVER['DOCUMENT_ROOT'].'/admin/logs/archives/unsent-logs-archive/unsigned-'.$prev_log_date.'.log';
$prev_unsigned_logs_raw = file_get_contents($prev_unsigned_src);
$prev_unsigned_logs = unserialize($prev_unsigned_logs_raw);
rename($prev_unsigned_src, $prev_unsigned_dest);
if (!empty($prev_unsigned_logs)) {
foreach($prev_unsigned_logs as $pl) {
if (!in_array($pl, $unsigned_logs)) {
$unsigned_logs[] = $pl;
}
}
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/unsigned-'.$log_date.'.log', serialize($unsigned_logs));
}
}
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/logs/signed-'.$prev_log_date.'.log')) {
$prev_signed_src = $_SERVER['DOCUMENT_ROOT'].'/admin/logs/signed-'.$prev_log_date.'.log';
$prev_signed_dest = $_SERVER['DOCUMENT_ROOT'].'/admin/logs/archives/sent-logs-archive/signed-'.$prev_log_date.'.log';
rename($prev_signed_src, $prev_signed_dest);
}
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/logs/email-logs-'.$prev_log_date.'.log')) {
$prev_emails_src = $_SERVER['DOCUMENT_ROOT'].'/admin/logs/email-logs-'.$prev_log_date.'.log';
$prev_emails_dest = $_SERVER['DOCUMENT_ROOT'].'/admin/logs/archives/email-logs-archive/email-logs-'.$prev_log_date.'.log';
rename($prev_emails_src, $prev_emails_dest);
}
if ($inbox){
echo "Inbox connection successful....<br />\n";
echo date('l jS, F Y @ G:i')."<br /><br />\n";
/* grab emails */
$emails = imap_search($inbox,'UNSEEN', SE_UID);
$check = imap_mailboxmsginfo($inbox);
$email_logs = array();
$unsigned_logs = array();
$signed_logs = array();
echo "Total Messages: " . $check->Nmsgs . "<br />\n";
echo "Unread Messages: " . $check->Unread . "<br />\n";
echo "Deleted Messages: " . $check->Deleted . "<br /><br />\n";
if ( (time() > strtotime("Today 9pm")) && $check->Deleted > 0 ) {
imap_expunge($inbox);
}
/*
Check if log files exist and if not create
one for the current date with empty array.
*/
if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/logs/email-logs-'.$log_date.'.log')) {
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/email-logs-'.$log_date.'.log', serialize($unsigned_logs));
} else {
$email_logs_raw = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/email-logs-'.$log_date.'.log');
$email_logs = unserialize($email_logs_raw);
}
// Check if Unsigned logs for current date extists
if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/logs/unsigned-'.$log_date.'.log')) {
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/unsigned-'.$log_date.'.log', serialize($unsigned_logs));
} else {
$unsigned_logs_raw = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/unsigned-'.$log_date.'.log');
$unsigned_logs = unserialize($unsigned_logs_raw);
}
// Check if signed logs for current date extists
if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/admin/logs/signed-'.$log_date.'.log')) {
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/signed-'.$log_date.'.log', serialize($signed_logs)); ;
}
// Check if Emails are Unread
$email_logs[] = array('check-date' => time(), 'Nmsgs' => $check->Nmsgs, 'Unread' => $check->Unread, 'Deleted' => $check->Deleted );
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/email-logs-'.$log_date.'.log', serialize($email_logs));
//$emails = false;
if($emails) {
rsort($emails);
$id = $emails[0];
/* for every email... */
/* get information specific to this email */
$overview = imap_fetch_overview($inbox, $id, FT_UID);
$message = imap_fetchbody($inbox, $id, 2, FT_UID);
$structure = imap_fetchstructure($inbox, $id, FT_UID);
$parts_total = count($structure->parts);
$parts = $structure->parts;
foreach($parts as $k => $v) {
if ($v->subtype == 'PLAIN') {
unset($parts[$k]);
}
}
rsort($parts);
$attachments = array();
if( isset($parts) && $parts_total > 0 ) {
foreach($parts as $k => $v) {
if ($parts_total == 1) {
$k = 0;
}
if( $v->ifdparameters ) {
foreach($v->dparameters as $object) {
//pre($object);
if(strtolower($object->attribute) == 'filename') {
$attachments[$k]['is_attachment'] = true;
$attachments[$k]['filename'] = $object->value;
}
}
}
if ( $v->ifparameters ) {
foreach( $v->parameters as $object ) {
if(strtolower($object->attribute) == 'name') {
$attachments[$k]['is_attachment'] = true;
$attachments[$k]['name'] = $object->value;
}
}
}
//pre($attachments);
if ( $attachments[$k]['is_attachment'] ) {
$attachments[$k]['attachment'] = imap_fetchbody($inbox, $id, $k+2, FT_UID);
if($v->encoding == 3) { // 3 = BASE64
$attachments[$k]['attachment'] = base64_decode($attachments[$k]['attachment']);
} elseif ($v->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$k]['attachment'] = quoted_printable_decode($attachments[$k]['attachment']);
}
} // if( $attachments[$k]['is_attachment'] )
} // foreach($parts as $k => $v)
} // if(isset($structure['parts']) && count($structure['parts']))
foreach ($overview as $ov) {
$seen_msg = $ov->seen;
$subject = $ov->subject;
$subject_parts = explode('&', $subject);
$client_ref = strtolower($subject_parts[0]);
$client_fname = strtolower($subject_parts[1]);
$client_lname = strtolower($subject_parts[2]);
$client_email = $subject_parts[3];
$handler_email = $subject_parts[4];
$doc_title = $subject_parts[5];
$result = null;
$data = array();
if (count($attachments) == 1) {
$name = $attachments[0]['name'];
$contents = $attachments[0]['attachment'];
$doc_dir = $client_ref;
$doc_ext = getFileExtension($name);
$doc_name = $client_ref.'-letter.'. $doc_ext;
$date_sent = time();
if ( !is_dir($_SERVER['DOCUMENT_ROOT'].'/'.$doc_dir) ) {
//DATA ARRAY VALUES
$data['id'] = str_pad(rand('0','99999999'), 8, "0", STR_PAD_LEFT);
$data['ref'] = $client_ref;
$data['doc_title'] = ucwords($doc_title);
$data['handler'] = $handler_email;
$data['email'] = $client_email;
$data['firstname'] = ucwords($client_fname);
$data['lastname'] = ucwords($client_lname);
$data['tkn'] = md5( uniqid(rand(), true) );
$data['sent'] = $date_sent;
$files_added = false;
$dir = mkdir($client_ref, 0755);
if ($dir == 1) {
//FILES CREATED
$new_doc = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/'.$doc_dir .'/'. $doc_name, $contents);
$new_html = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/'.$doc_dir .'/sign.php', $php_temp);
$new_data = file_put_contents($_SERVER['DOCUMENT_ROOT'].'/'.$doc_dir .'/data.txt', serialize($data));
if ($new_doc && $new_html && $new_data) {
$files_added = true;
}
}
//pre($data);
include_once($_SERVER['DOCUMENT_ROOT'].'/inc/emails/send-client-email.php');
if ($files_added && sendClientEmail($data)) {
$unsigned_logs[] = $data;
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/unsigned-'.$log_date.'.log', serialize($unsigned_logs));
imap_delete($inbox, $overview[0]->msgno);
echo "Client email was sent and files added to folder successfully!<br />\n";
} else {
echo "Could not create client folder and send client email!";
imap_clearflag_full($inbox, $overview[0]->msgno, "\\Seen", ST_UID);
}
} else { // If Folder already exists delete email
echo "A folder for client ".$client_ref." already exists.";
imap_delete($inbox, $overview[0]->msgno);
}
} else { // If no attachments delete email
echo "There was no files attached to the email!";
imap_delete($inbox, $overview[0]->msgno);
}
} // foreach overview
echo "Emails have been processed.\n";
} else {
echo "No new emails to process.\n";
} //if ther are emails
$imap_error_logs[] = imap_errors();
$imap_alert_logs[] = imap_alerts();
imap_close($inbox);
} else {
if (file_exists('logs/imap-error-logs-'.$log_date.'.log')) {
$raw_imap_error_logs = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/imap-error-logs-'.$log_date.'.log');
$imap_error_logs = unserialize($raw_imap_error_logs);
$imap_error_logs[] = imap_errors();
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/imap-error-logs-'.$log_date.'.log', serialize($imap_error_logs));
} else {
$imap_error_logs = array();
$imap_error_logs[] = imap_errors();
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/logs/imap-error-logs-'.$log_date.'.log', serialize($imap_error_logs));
}
}
?>