-
Notifications
You must be signed in to change notification settings - Fork 2
/
report-generator.php
367 lines (313 loc) · 10.5 KB
/
report-generator.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
/**
* Microsoft Graph Report Generator
*
* @category Class
* @author Caitlin Bales
* @license MIT
* @link https://graph.microsoft.com/
*/
require_once __DIR__ . '/vendor/autoload.php';
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
/*
* Visit this URL in a browser as an admin to grant consent on behalf
* of the application. This only needs to be performed once per application ID.
*
* https://login.microsoftonline.com/{tenant}/adminconsent?client_id={client_id}&state=12345&redirect_uri=http://localhost:8000
*/
$adminEmail = "admin@tenant.onmicrosoft.com"; // email to send the report to
$tenant = "tenant.onmicrosoft.com"; // name or ID of your tenant
$applicationId = "client-id"; // reigster an application at apps.dev.microsoft.com
$applicationSecret = "client-secret";
// We'll pass this data into our email template
$content = "";
// Create a new Graph object
$token = getAccessToken($tenant, $applicationId, $applicationSecret);
$graph = new Graph();
$graph->setAccessToken($token);
// Get data for each set
$content .= getGroupData($graph);
$content .= getEmployeeData($graph);
$content .= getOneDriveData($graph);
$content .= getSharepointData($graph);
// Send the report as an email
createEmail($graph, $adminEmail, $tenant, $content);
exit();
/**
* Get an access token for Microsoft Graph
*
* @param string $tenant The tenant to request a token for
*
* @return string $accessToken
*/
function getAccessToken($tenant, $applicationId, $applicationSecret)
{
// Log in to Microsoft using client-credential flow
$url = "https://login.microsoftonline.com/" . $tenant . "/oauth2/v2.0/token";
$body = "grant_type=client_credentials" .
"&client_id=" . $applicationId .
"&client_secret=" . $applicationSecret .
"&scope=https://graph.microsoft.com/.default";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_POSTFIELDS => $body,
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$response = json_decode($response, true);
if (array_key_exists("access_token", $response)) {
return $response["access_token"];
} else {
echo "Failed to retrieve access token";
exit();
}
}
/**
* Get data about the tenant's groups
*
* @param Graph $graph The client to use to fetch Graph data
*
* @return string $data An HTML string of data about the tenant's groups
*/
function getGroupData($graph)
{
echo "Generating group table\n";
$data = array("title" => "Groups",
"headers" => array("Group", "Members"),
"content" => array()
);
$groups = $graph->createRequest(
"GET",
"/groups?\$filter=groupTypes/any(a:a eq 'unified')"
)
->setReturnType(Model\Group::class)
->execute();
// Get a count of members in each group
foreach ($groups as $group) {
$name = $group->getDisplayName();
$members = $graph->createRequest(
"GET",
"/groups/".$group->getId()."/members"
)
->setReturnType(Model\User::class)
->execute();
$memberCount = count($members);
$data["content"][] = array($name, $memberCount);
}
return createTable($data);
}
/**
* Get data about the tenant's employees
*
* @param Graph $graph The client to use to fetch Graph data
*
* @return string $data An HTML string of data about the tenant's employees
*/
function getEmployeeData($graph)
{
echo "Generating employee table\n";
$data = array(
"title" => "Employees",
"headers" => array("Employee", "Tenure", "Group Memberships", "Messages"),
"content" => array()
);
$users = $graph->createRequest("GET", "/users")
->setReturnType(Model\User::class)
->execute();
foreach ($users as $user) {
// Weed out the conference rooms
if ($user->getGivenName()) {
$name = $user->getDisplayName();
// Get the user's tenure
$response = $graph->createRequest(
"GET",
"/users/" . $user->getId() . "?\$select=hireDate"
)
->execute()
->getBody();
if ($response["hireDate"] != "0001-01-01T00:00:00Z") {
$tenure = date_diff(
new DateTime($response["hireDate"]),
new DateTime()
);
$tenure = $tenure->format("%y years");
} else {
$tenure = "Unknown";
}
// Get a list of groups the user is a member of
$groups = $graph->createRequest(
"GET",
"/users/" . $user->getId() . "/memberOf"
)
->setReturnType(Model\DirectoryObject::class)
->execute();
$groupCount = 0;
// Get only memberships of type "group"
foreach ($groups as $group) {
$type = $group->getProperties()['@odata.type'];
if ($type == "#microsoft.graph.group") {
$groupCount++;
}
}
// Get a count of the user's messages
try {
$messageCount = 0;
$messageGrabber = $graph->createCollectionRequest(
"GET",
"/users/" . $user->getId() . "/messages"
)
->setReturnType(Model\Message::class)
->setPageSize(10);
while (!$messageGrabber->isEnd()) {
$messages = $messageGrabber->getPage();
$messageCount += count($messages);
}
}
catch (Exception $e) {
$messageCount = "Unknown";
}
$data["content"][] = array($name, $tenure, $groupCount, $messageCount);
}
}
return createTable($data);
}
/**
* Get data about the tenant's drives
*
* @param Graph $graph The client to use to fetch Graph data
*
* @return string $data An HTML string of data about the tenant's drives
*/
function getOneDriveData($graph)
{
echo "Generating OneDrive table";
$data = array(
"title" => "OneDrive",
"headers" => array("Drive", "Used"),
"content" => array()
);
$drives = $graph->createRequest("GET", "/drives")
->setReturnType(Model\Drive::class)
->execute();
foreach ($drives as $drive) {
// Get the used storage space on the drive
$quota = $drive->getQuota();
$used = ceil(($quota->getTotal() - $quota->getRemaining())/1000000);
// Get an abbreviated identifier of the drive
$driveName = substr($drive->getId(), 0, 5) .
"..." .
substr($drive->getId(), -7);
$data["content"][] = array($driveName, $used . "MB");
}
return createTable($data);
}
/**
* Get data about the tenant's SharePoint site
*
* @param Graph $graph The client to use to fetch Graph data
*
* @return string $data An HTML string of data about the tenant's SharePoint site
*/
function getSharepointData($graph)
{
echo "Generating Sharepoint table";
$graph->setApiVersion("beta");
$data = array(
"title" => "Planner",
"headers" => array("Plan"),
"content" => array()
);
$site = $graph->createRequest("GET", "/sharepoint/site")
->setReturnType(Model\Site::class)
->execute();
$lists = $graph->createRequest("GET", "/sharepoint/sites/" . $site->getId() . "/lists")
->setReturnType(Model\SharepointList::class)
->execute();
foreach ($lists as $list) {
$items = $graph->createRequest("GET", "/sharepoint/sites/" . $site->getId() . "/lists/" . $list->getId() . "/items")
->setReturnType(Model\ListItem::class)
->execute();
$data["content"][] = array($list->getName(), count($items));
}
return createTable($data);
}
/**
* Parse the data into an HTML table
*
* @param array $data Information to include in the table
*
* @return string $content The HTML table
*/
function createTable($data)
{
$content = "<div class='data-table'>";
$content .= "<h2>" . $data['title'] . "</h2>";
$content .= "<table>";
$content .= "<tr>";
foreach ($data['headers'] as $header) {
$content .="<th>" . $header . "</th>";
}
$content .="</tr>";
foreach ($data['content'] as $item=>$info) {
$content .= "<tr>";
foreach ($info as $dataPoint) {
$content .= "<td>" . $dataPoint . "</td>";
}
$content .= "</tr>";
}
$content .= "</table>";
$content .= "</div>";
return $content;
}
/**
* Send the report as an email to the admin
*
* @param Graph $graph The client to use to fetch Graph data
* @param string $adminEmail The address to send the report to
* @param string $tenant The name of the tenant
* @param string $content The contents of the email
*
* @return null
*/
function createEmail($graph, $adminEmail, $tenant, $content)
{
$body = file_get_contents("email-template.html");
$body = str_replace('$content', $content, $body);
$adminName = "Admin";
$mailBody = array(
"Message" => array(
"subject" => "Current data for tenant " . $tenant,
"body" => array(
"contentType" => "html",
"content" => $body
),
"sender" => array(
"emailAddress" => array(
"name" => $adminName,
"address" => $adminEmail
)
),
"from" => array(
"emailAddress" => array(
"name" => $adminName,
"address" => $adminEmail
)
),
"toRecipients" => array(array(
"emailAddress" => array(
"name" => $adminName,
"address" => $adminEmail
)
)
)
)
);
$graph->createRequest("POST", "/users/$adminEmail/sendmail")
->attachBody($mailBody)
->execute();
}