forked from uspdev/idmail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDMail.php
More file actions
210 lines (177 loc) · 6.34 KB
/
IDMail.php
File metadata and controls
210 lines (177 loc) · 6.34 KB
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
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Middleware;
class IDMail
{
var $client;
function __construct($cache_mode)
{
$this->client = $this->login();
if ($cache_mode == "all") {
$this->update_cache();
}
}
private function login()
{
$login = getenv('LOGIN');
$pass = getenv('PASSWORD');
$cookies = new CookieJar();
$client = new Client(['cookies' => $cookies]);
$response = $client->get("https://id-admin.internuvem.usp.br/portal/");
# get jsessionid
$cookie = $cookies->getCookieByName("JSESSIONID");
$jsessionid = $cookie->toArray()['Value'];
# generate SAML request
$response = $client->post("https://idpcafe.usp.br/idp/profile/SAML2/Redirect/SSO;jsessionid=$jsessionid?execution=e1s1", [
'form_params' => [
'j_username' => $login,
'j_password' => $pass,
'_eventId_proceed' => '',
]
]);
# extract SAML data from response
$dom = new \DOMDocument();
$dom->loadHTML($response->getBody());
$xpath = new \DOMXpath($dom);
$relaystate = $xpath->query("//html/body/form/div/input[@name='RelayState']/@value")[0]->textContent;
$samlresponse = $xpath->query("//html/body/form/div/input[@name='SAMLResponse']/@value")[0]->textContent;
# SAML authentication
$response = $client->post("https://id-admin.internuvem.usp.br/Shibboleth.sso/SAML2/POST", [
'form_params' => [
'RelayState' => $relaystate,
'SAMLResponse' => $samlresponse,
]
]);
return $client;
}
private function update_cache()
{
$response = $this->client->get("https://id-admin.internuvem.usp.br/sybase/json/".getenv('LOGIN')."/all_emails/");
file_put_contents(getenv('MAIL_CACHE')."/all_emails.json", $response->getBody());
}
function id_get_emails($nusp)
{
$response = $this->client->get("https://id-admin.internuvem.usp.br/sybase/json/$nusp/emails/");
file_put_contents(getenv('MAIL_CACHE')."/".$nusp.".json", $response->getBody());
return $response->getBody();
}
public function members($mode, $list, $emails)
{
$handler = $this->client->getConfig('handler');
$tap = Middleware::tap(function ($request) {
# debug
echo $request->getBody();
});
[$group, $domain] = explode('@', $list);
$response = $this->client->post("https://id-admin.internuvem.usp.br/gsuite/json/".$domain."/".$group."/group/members/", [
GuzzleHttp\RequestOptions::JSON => [$mode => $emails],
'handler' => $tap($handler)
]);
return $response->getBody();
}
static function extract_email($json, $domain, $type)
{
# dtainival deixou de existir por enquanto
#$last = ['date' => 0, 'email' => null];
if ($json->response == true) {
$last = ['date' => 0, 'email' => null,];
foreach ($json->result as $data) {
if (in_array($data->tipema, $type)) {
$email = $data->emausp;
$user = explode("@", $email);
if ($user[1] == $domain) {
$last['email'] = $email;
#$date = strtotime($data->dtainival);
#if ($last['date'] < $date) {
# $last['date'] = $date;
# $last['email'] = $email;
#}
}
}
}
}
return $last['email'];
}
static function extract_lists($json, $domain, $type)
{
$emails = [];
if ($json->response == true) {
foreach ($json->result as $data) {
if (in_array($data->tipema, $type)) {
$email = $data->emausp;
$user = explode("@", $email);
if ($user[1] == $domain) {
$name = "SEM NOME";
if ($data->nomaptema != null) {
$name = $data->nomaptema;
}
$emails[] = ['email' => $email, 'name' => $name];
}
}
}
}
return $emails;
}
static function get_cache($cache_file)
{
if (!file_exists($cache_file)) {
return null;
}
$delta = (time() - filemtime($cache_file))/86400;
if ($delta > 1) {
return null;
}
return file_get_contents($cache_file);
}
static function cache_get_emails($nusp)
{
$cache_file = getenv('MAIL_CACHE')."/".$nusp.".json";
return IDMail::get_cache($cache_file);
}
static function cache_find_email($nusp, $type)
{
$cache_file = getenv('MAIL_CACHE')."/all_emails.json";
$cache = IDMail::get_cache($cache_file);
if (!$cache) {
return null;
}
$json = json_decode($cache);
$last = ['date' => 0, 'email' => null];
if ($json->response == true) {
foreach ($json->result as $email => $data) {
if (in_array($data->tipo, $type) and $data->codpes == $nusp) {
$date = strtotime($data->dtainival);
if ($last['date'] < $date) {
$last['date'] = $date;
$last['email'] = $email;
}
}
}
}
return $last['email'];
}
static function find_email($nusp)
{
$email = IDMail::cache_find_email($nusp, ["P", "O"]);
if ($email == null) {
$idmail = new static("all");
$json = json_decode($idmail->id_get_emails($nusp));
$email = IDMail::extract_email($json, "ime.usp.br", ["P", "O"]);
}
return $email;
}
static function find_lists($nusp)
{
$cache = IDMail::cache_get_emails($nusp);
if ($cache) {
$json = json_decode($cache);
}
else {
$idmail = new IDMail("list");
$json = json_decode($idmail->id_get_emails($nusp));
}
return IDMail::extract_lists($json, "ime.usp.br", ["I", "G", "D"]);
}
}
?>