-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.php
executable file
·116 lines (87 loc) · 3.01 KB
/
parser.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
<?php
$config = parse_ini_file("settings.ini", TRUE);
// check email
$inbox = imap_open($config["email"]["imap"], $config["email"]["imap_user"], $config["email"]["imap_pass"]) or die('Cannot connect to email: ' . imap_last_error());
$emails = imap_search($inbox, 'ALL');
if($emails)
{
foreach($emails as $msg_number)
{
// check agent name
$header = imap_headerinfo($inbox, $msg_number);
$subject = $header->subject;
$subject = ucwords($subject); // uppercase it
// grab agent names from CSV and load into string
$row = 1;
$out = array();
$agents = array();
if (($handle = fopen($config["db"]["schedule"], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
if (substr($data[0], 0, 1) == '#') {
$data[0] = substr($data[0], 1);
$out[] = $data[0];
}
$agents[] = $data[0];
}
fclose($handle);
}
// find overlap between subject and agent names
// [ARRAY cannot account for spaces between first name and last initial]
//$requester = array_intersect($agents, $subject);
//$requester = array_values($requester); // reset index numbers
foreach ($agents as $name) {
if (strpos($subject, $name) !== false) {
$requester = $name;
}
}
// parse message body
$message = imap_body($inbox, $msg_number);
$message = strip_tags($message, '');
$message = preg_replace("/\s+/", " ", $message);
// grab start and end dates
$msg1 = explode('Start Time: ', $message);
$msg2 = explode(' ', $msg1[1]);
// determine date span of time off
$startDay = substr($msg2[0], 0, -5); // remove year
$endDay = substr($msg2[5], 0, -5);
$timeoff = array();
$begin = new DateTime($startDay);
$end = new DateTime($endDay);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $dt) {
$timeoff[] = $dt->format("n/j");
}
$timeoff[] = $end->format("n/j");
// add back # if agent currently out
if (in_array($requester, $out)) {
$requester = '#' . $requester;
}
// sed those dates into CSV
if(strpos($message, "added") !== false) {
$file = file_get_contents($config["db"]["schedule"]);
$file = preg_replace('@(^'.$requester.'.*)".*$@m', '${1}', $file);
foreach ($timeoff as $date) {
$file = preg_replace('@(^'.$requester.'.*).*$@m', '${1}'.$date.',', $file);
}
$file = preg_replace('@(^'.$requester.'.*),$@m', '${1},"', $file);
file_put_contents($config["db"]["schedule"], $file);
imap_delete($inbox,$msg_number);
} elseif (strpos($message, "deleted") !== false) {
$file = file_get_contents($config["db"]["schedule"]);
foreach ($timeoff as $date) {
$pattern = '@(^'.$requester.'.*)'.$date.',(.*)@m';
$file = preg_replace($pattern, '${1}${2}', $file);
}
file_put_contents($config["db"]["schedule"], $file);
imap_delete($inbox,$msg_number);
} else {
// This is a change of date that needs to be updated manually.
}
}
}
imap_expunge($inbox);
imap_close($inbox);
?>