-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMSGraph.php
More file actions
318 lines (271 loc) · 9.71 KB
/
MSGraph.php
File metadata and controls
318 lines (271 loc) · 9.71 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
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
<?php
namespace ProcessMaker\Flysystem\Adapter;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Stream;
use League\Flysystem\Adapter\AbstractAdapter;
use League\Flysystem\Config;
use ProcessMaker\Flysystem\Adapter\MSGraph\AuthException;
use ProcessMaker\Flysystem\Adapter\MSGraph\ModeException;
use ProcessMaker\Flysystem\Adapter\MSGraph\SiteInvalidException;
use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
class MSGraph extends AbstractAdapter
{
const MODE_SHAREPOINT = 'sharepoint';
const MODE_ONEDRIVE = 'onedrive';
// Our mode, if sharepoint or onedrive
private $mode;
// Our Microsoft Graph Client
private $graph;
// Our Microsoft Graph Access Token
private $token;
// Our targetId, sharepoint site if sharepoint, drive id if onedrive
private $targetId;
// Our driveId, which if non empty points to a Drive
private $driveId;
// Our url prefix to be used for most file operations. This gets created in our constructor
private $prefix;
public function __construct($appId, $appPassword, $tokenEndpoint, $mode = self::MODE_ONEDRIVE, $targetId, $driveName = null)
{
if($mode != self::MODE_ONEDRIVE && $mode != self::MODE_SHAREPOINT) {
throw new ModeException("Unknown mode specified: " . $mode);
}
$this->mode = $mode;
// Initialize the OAuth client
$oauthClient = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => $appId,
'clientSecret' => $appPassword,
'urlAuthorize' => '',
'urlResourceOwnerDetails' => '',
'urlAccessToken' => $tokenEndpoint,
]);
try {
$this->token = $oauthClient->getAccessToken('client_credentials', [
'scope' => 'https://graph.microsoft.com/.default'
]);
} catch(IdentityProviderException $e) {
throw new AuthException($e->getMessage());
}
// Assign graph instance
$this->graph = new Graph();
$this->graph->setAccessToken($this->token->getToken());
// Check for existence
if($mode == self::MODE_SHAREPOINT) {
try {
$site = $this->graph->createRequest('GET', '/sites/' . $targetId)
->setReturnType(Model\Site::class)
->execute();
// Assign the site id triplet to our targetId
$this->targetId = $site->getId();
} catch(\Exception $e) {
if($e->getCode() == 400) {
throw new SiteInvalidException("The sharepoint site " . $targetId . " is invalid.");
}
throw $e;
}
$this->prefix = "/sites/" . $this->targetId . '/drive/items/';
if($driveName != '') {
// Then we specified a drive name, so let's enumerate the drives and find it
$drives = $this->graph->createRequest('GET', '/sites/' . $this->targetId . '/drives')
->execute();
$drives = $drives->getBody()['value'];
foreach($drives as $drive) {
if($drive['name'] == $driveName) {
$this->driveId = $drive['id'];
$this->prefix = "/drives/" . $this->driveId . "/items/";
break;
}
}
if(!$this->driveId) {
throw new SiteInvalidException("The sharepoint drive with name " . $driveName . " could not be found.");
}
}
}
}
public function has($path)
{
if($this->mode == self::MODE_SHAREPOINT) {
try {
$driveItem = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $path)
->setReturnType(Model\DriveItem::class)
->execute();
// Successfully retrieved meta data.
return true;
} catch(ClientException $e) {
if($e->getCode() == 404) {
// Not found, let's return false;
return false;
}
throw $e;
} catch(Exception $e) {
throw $e;
}
}
return false;
}
public function read($path)
{
if($this->mode == self::MODE_SHAREPOINT) {
try {
$driveItem = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $path)
->setReturnType(Model\DriveItem::class)
->execute();
// Successfully retrieved meta data.
// Now get content
$contentStream = $this->graph->createRequest('GET', $this->prefix . $driveItem->getId() .'/content')
->setReturnType(Stream::class)
->execute();
$contents = '';
$bufferSize = 8012;
// Copy over the data into a string
while (!$contentStream->eof()) {
$contents .= $contentStream->read($bufferSize);
}
return ['contents' => $contents];
} catch(ClientException $e) {
if($e->getCode() == 404) {
// Not found, let's return false;
return false;
}
throw $e;
} catch(Exception $e) {
throw $e;
}
}
return false;
}
public function getUrl($path)
{
if($this->mode == self::MODE_SHAREPOINT) {
try {
$driveItem = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $path)
->setReturnType(Model\DriveItem::class)
->execute();
// Successfully retrieved meta data.
// Return url property
return $driveItem->getWebUrl();
} catch(ClientException $e) {
if($e->getCode() == 404) {
// Not found, let's return false;
return false;
}
throw $e;
} catch(Exception $e) {
throw $e;
}
}
return false;
}
public function readStream($path)
{
}
public function listContents($directory = '', $recursive = false)
{
if ($this->mode == self::MODE_SHAREPOINT) {
try {
$drive = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $directory)
->setReturnType(Model\Drive::class)
->execute();
// Successfully retrieved meta data.
// Now get content
$driveItems = $this->graph->createRequest('GET', $this->prefix . $drive->getId() .'/children')
->setReturnType(Model\DriveItem::class)
->execute();
$children = [];
foreach ($driveItems as $driveItem) {
$item = $driveItem->getProperties();
$item['path'] = $directory . '/' . $driveItem->getName();
$children[] = $item;
}
return $children;
} catch (ClientException $e) {
throw $e;
} catch (Exception $e) {
throw $e;
}
}
return [];
}
public function getMetadata($path)
{
}
public function getSize($path)
{
}
public function getMimetype($path)
{
}
public function getTimestamp($path)
{
}
public function getVisibility($path)
{
}
// Write methods
public function write($path, $contents, Config $config)
{
if($this->mode == self::MODE_SHAREPOINT) {
// Attempt to write to sharepoint
try {
$driveItem = $this->graph->createRequest('PUT', $this->prefix . 'root:/' . $path . ':/content')
->attachBody($contents)
->setReturnType(Model\DriveItem::class)
->execute();
// Successfully created
return true;
} catch(Exception $e) {
throw $e;
}
}
}
public function writeStream($path, $resource, Config $config)
{
}
public function update($path, $contents, Config $config)
{
}
public function updateStream($path, $resource, Config $config)
{
}
public function rename($path, $newpath)
{
}
public function copy($path, $newpath)
{
}
public function delete($path)
{
if($this->mode == self::MODE_SHAREPOINT) {
try {
$driveItem = $this->graph->createRequest('GET', $this->prefix . 'root:/' . $path)
->setReturnType(Model\DriveItem::class)
->execute();
// Successfully retrieved meta data.
// Now delete the file
$this->graph->createRequest('DELETE', $this->prefix . $driveItem->getId())
->execute();
return true;
} catch(ClientException $e) {
if($e->getCode() == 404) {
// Not found, let's return false;
return false;
}
throw $e;
} catch(Exception $e) {
throw $e;
}
}
return false;
}
public function deleteDir($dirname)
{
}
public function createDir($dirname, Config $config)
{
}
public function setVisibility($path, $visibility)
{
}
}