1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\GoogleDrive; |
9: | |
10: | use GuzzleHttp\Psr7\Request; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | class Module extends \Aurora\System\Module\AbstractModule |
22: | { |
23: | protected static $sStorageType = 'google'; |
24: | protected static $iStorageOrder = 200; |
25: | |
26: | protected $oClient = null; |
27: | |
28: | protected $oService = null; |
29: | |
30: | protected $aRequireModules = array( |
31: | 'Files', |
32: | 'OAuthIntegratorWebclient', |
33: | 'GoogleAuthWebclient' |
34: | ); |
35: | |
36: | protected function issetScope($sScope) |
37: | { |
38: | return \in_array($sScope, \explode(' ', $this->getConfig('Scopes'))); |
39: | } |
40: | |
41: | public function init() |
42: | { |
43: | $this->AddEntries( |
44: | [ |
45: | 'google-drive-thumb' => 'EntryThumbnail' |
46: | ] |
47: | ); |
48: | |
49: | $this->subscribeEvent('GoogleAuthWebclient::PopulateScopes', array($this, 'onPopulateScopes')); |
50: | $this->subscribeEvent('Files::GetStorages::after', array($this, 'onAfterGetStorages')); |
51: | $this->subscribeEvent('Files::GetFile', array($this, 'onGetFile')); |
52: | $this->subscribeEvent('Files::GetItems', array($this, 'onGetItems')); |
53: | $this->subscribeEvent('Files::GetFileInfo::after', array($this, 'onAfterGetFileInfo')); |
54: | $this->subscribeEvent('Files::GetFile::after', array($this, 'onAfterGetFile')); |
55: | $this->subscribeEvent('Files::CreateFolder::after', array($this, 'onAfterCreateFolder')); |
56: | $this->subscribeEvent('Files::CreateFile', array($this, 'onCreateFile')); |
57: | $this->subscribeEvent('Files::CreatePublicLink::after', array($this, 'onAfterCreatePublicLink')); |
58: | $this->subscribeEvent('Files::DeletePublicLink::after', array($this, 'onAfterDeletePublicLink')); |
59: | $this->subscribeEvent('Files::Delete::after', array($this, 'onAfterDelete')); |
60: | $this->subscribeEvent('Files::Rename::after', array($this, 'onAfterRename')); |
61: | $this->subscribeEvent('Files::Move::after', array($this, 'onAfterMove')); |
62: | $this->subscribeEvent('Files::Copy::after', array($this, 'onAfterCopy')); |
63: | $this->subscribeEvent('Files::CheckUrl', array($this, 'onAfterCheckUrl')); |
64: | $this->subscribeEvent('Files::PopulateFileItem::after', array($this, 'onAfterPopulateFileItem')); |
65: | |
66: | $this->subscribeEvent('Google::GetSettings', array($this, 'onGetSettings')); |
67: | $this->subscribeEvent('Google::UpdateSettings::after', array($this, 'onAfterUpdateSettings')); |
68: | |
69: | $this->subscribeEvent('Files::GetItems::before', array($this, 'CheckUrlFile')); |
70: | $this->subscribeEvent('Files::UploadFile::before', array($this, 'CheckUrlFile')); |
71: | $this->subscribeEvent('Files::CreateFolder::before', array($this, 'CheckUrlFile')); |
72: | |
73: | $this->subscribeEvent('Files::CheckQuota::after', array($this, 'onAfterCheckQuota')); |
74: | $this->subscribeEvent('Files::GetQuota::after', array($this, 'onAfterGetQuota')); |
75: | } |
76: | |
77: | public function onPopulateScopes($sScope, &$aResult) |
78: | { |
79: | $aScopes = \explode('|', $sScope); |
80: | foreach ($aScopes as $sScope) { |
81: | if ($sScope === 'storage') { |
82: | $aResult[] = 'https://www.googleapis.com/auth/drive'; |
83: | } |
84: | } |
85: | } |
86: | |
87: | public function onAfterGetStorages($aArgs, &$mResult) |
88: | { |
89: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous); |
90: | |
91: | $bEnableGoogleModule = false; |
92: | $oGoogleModule = \Aurora\System\Api::GetModule('Google'); |
93: | if ($oGoogleModule instanceof \Aurora\System\Module\AbstractModule) { |
94: | $bEnableGoogleModule = $oGoogleModule->getConfig('EnableModule', false); |
95: | } else { |
96: | $bEnableGoogleModule = false; |
97: | } |
98: | |
99: | $oOAuthAccount = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator()->GetAccount(self::$sStorageType); |
100: | |
101: | if ($oOAuthAccount instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount && |
102: | $oOAuthAccount->Type === self::$sStorageType && $bEnableGoogleModule && |
103: | $this->issetScope('storage') && $oOAuthAccount->issetScope('storage')) { |
104: | $mResult[] = [ |
105: | 'Type' => self::$sStorageType, |
106: | 'IsExternal' => true, |
107: | 'DisplayName' => 'Google Drive', |
108: | 'Order' => self::$iStorageOrder, |
109: | ]; |
110: | } |
111: | } |
112: | |
113: | protected function GetClient() |
114: | { |
115: | if (!isset($this->oClient)) { |
116: | $oGoogleModule = \Aurora\System\Api::GetModule('Google'); |
117: | if ($oGoogleModule instanceof \Aurora\System\Module\AbstractModule) { |
118: | if (!$oGoogleModule->getConfig('EnableModule', false) || !$this->issetScope('storage')) { |
119: | return false; |
120: | } |
121: | } else { |
122: | return false; |
123: | } |
124: | |
125: | $oOAuthIntegratorWebclientModule = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator(); |
126: | $oSocialAccount = $oOAuthIntegratorWebclientModule->GetAccount(self::$sStorageType); |
127: | if ($oSocialAccount) { |
128: | $oGoogleModule = \Aurora\Modules\Google\Module::Decorator(); |
129: | if ($oGoogleModule) { |
130: | $oClient = new \Google_Client(); |
131: | $oClient->setClientId($oGoogleModule->getConfig('Id', '')); |
132: | $oClient->setClientSecret($oGoogleModule->getConfig('Secret', '')); |
133: | $oClient->addScope([ |
134: | 'https://www.googleapis.com/auth/userinfo.email', |
135: | 'https://www.googleapis.com/auth/userinfo.profile', |
136: | 'https://www.googleapis.com/auth/drive' |
137: | ]); |
138: | $bRefreshToken = false; |
139: | try { |
140: | $oClient->setAccessToken($oSocialAccount->AccessToken); |
141: | } catch (\Exception $oException) { |
142: | $bRefreshToken = true; |
143: | } |
144: | if ($oClient->isAccessTokenExpired() || $bRefreshToken) { |
145: | $oClient->refreshToken($oSocialAccount->RefreshToken); |
146: | $oSocialAccount->AccessToken = $oClient->getAccessToken(); |
147: | $oOAuthIntegratorWebclientModule->UpdateAccount($oSocialAccount); |
148: | } |
149: | if ($oClient->getAccessToken()) { |
150: | $this->oClient = $oClient; |
151: | } |
152: | } |
153: | } |
154: | } |
155: | |
156: | return $this->oClient; |
157: | } |
158: | |
159: | protected function GetDriveService() |
160: | { |
161: | if (!isset($this->oService)) { |
162: | $oClient = $this->GetClient(); |
163: | if ($oClient) { |
164: | $this->oService = new \Google_Service_Drive($oClient); |
165: | } |
166: | } |
167: | |
168: | return $this->oService; |
169: | } |
170: | |
171: | protected function _dirname($sPath) |
172: | { |
173: | $sPath = \dirname($sPath); |
174: | return \str_replace(DIRECTORY_SEPARATOR, '/', $sPath); |
175: | } |
176: | |
177: | protected function _basename($sPath) |
178: | { |
179: | $aPath = \explode('/', $sPath); |
180: | return \end($aPath); |
181: | } |
182: | |
183: | |
184: | |
185: | |
186: | protected function PopulateFileInfo($sType, $sPath, $oFile) |
187: | { |
188: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous); |
189: | |
190: | $mResult = false; |
191: | if ($oFile) { |
192: | if (isset($oFile->shortcutDetails)) { |
193: | $oFile->mimeType = $oFile->shortcutDetails['targetMimeType']; |
194: | $oFile->id = $oFile->shortcutDetails['targetId']; |
195: | } |
196: | |
197: | $this->PopulateGoogleDriveFileInfo($oFile); |
198: | |
199: | $mResult = new \Aurora\Modules\Files\Classes\FileItem(); |
200: | $mResult->IsExternal = true; |
201: | $mResult->TypeStr = $sType; |
202: | $mResult->IsFolder = ($oFile->mimeType === "application/vnd.google-apps.folder"); |
203: | $mResult->Id = $oFile->id; |
204: | $mResult->Name = $oFile->name; |
205: | $mResult->Path = ''; |
206: | $mResult->Size = $oFile->size; |
207: | $mResult->FullPath = $oFile->id; |
208: | $mResult->ContentType = $oFile->mimeType; |
209: | if (isset($oFile->thumbnailLink)) { |
210: | $mResult->Thumb = true; |
211: | $mResult->ThumbnailUrl = '?google-drive-thumb/' . \Aurora\System\Utils::UrlSafeBase64Encode($oFile->getThumbnailLink()); |
212: | } |
213: | |
214: | if ($mResult->IsFolder) { |
215: | $mResult->AddAction([ |
216: | 'list' => [] |
217: | ]); |
218: | } else { |
219: | $mResult->AddAction([ |
220: | 'view' => [ |
221: | 'url' => '?download-file/' . $this->getItemHash($mResult) .'/view' |
222: | ] |
223: | ]); |
224: | $mResult->AddAction([ |
225: | 'download' => [ |
226: | 'url' => '?download-file/' . $this->getItemHash($mResult) |
227: | ] |
228: | ]); |
229: | } |
230: | |
231: | |
232: | $mResult->LastModified = \date_timestamp_get(date_create($oFile->createdTime)); |
233: | } |
234: | |
235: | return $mResult; |
236: | } |
237: | |
238: | protected function _getFileInfo($sName) |
239: | { |
240: | $mResult = false; |
241: | $oService = $this->GetDriveService(); |
242: | if ($oService) { |
243: | $mResult = $oService->files->get($sName); |
244: | } |
245: | |
246: | return $mResult; |
247: | } |
248: | |
249: | |
250: | |
251: | |
252: | |
253: | |
254: | protected function getItemHash($oItem) |
255: | { |
256: | return \Aurora\System\Api::EncodeKeyValues(array( |
257: | 'UserId' => \Aurora\System\Api::getAuthenticatedUserId(), |
258: | 'Type' => $oItem->TypeStr, |
259: | 'Path' => '', |
260: | 'Name' => $oItem->FullPath, |
261: | 'FileName' => $oItem->Name |
262: | )); |
263: | } |
264: | |
265: | |
266: | |
267: | |
268: | public function onAfterGetFileInfo($aArgs, &$mResult) |
269: | { |
270: | if ($aArgs['Type'] === self::$sStorageType) { |
271: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous); |
272: | $oFileInfo = $this->_getFileInfo($aArgs['Id']); |
273: | if ($oFileInfo) { |
274: | $mResult = $this->PopulateFileInfo($aArgs['Type'], $aArgs['Path'], $oFileInfo); |
275: | return true; |
276: | } |
277: | } |
278: | } |
279: | |
280: | |
281: | |
282: | public function onGetFile($aArgs, &$Result) |
283: | { |
284: | if ($aArgs['Type'] === self::$sStorageType) { |
285: | $oService = $this->GetDriveService(); |
286: | if ($oService) { |
287: | $oFile = $oService->files->get($aArgs['Id']); |
288: | |
289: | $sMimeType = $this->getMimeTypeForExport($oFile); |
290: | $aArgs['Name'] = $oFile->name; |
291: | |
292: | if (empty($sMimeType)) { |
293: | $sFileData = $oService->files->get($aArgs['Id'], ['alt' => 'media']); |
294: | } else { |
295: | $sFileData = $oService->files->export($aArgs['Id'], $sMimeType, [ |
296: | 'alt' => 'media' |
297: | ]); |
298: | } |
299: | |
300: | $Result = \fopen('php://memory', 'r+'); |
301: | \fwrite($Result, $sFileData->getBody()); |
302: | \rewind($Result); |
303: | |
304: | return true; |
305: | } |
306: | } |
307: | } |
308: | |
309: | public function CheckUrlFile(&$aArgs, &$mResult) |
310: | { |
311: | if (strpos($aArgs['Path'], '.url') !== false) { |
312: | list($sUrl, $sId) = explode('.url', $aArgs['Path']); |
313: | $sUrl .= '.url'; |
314: | $aArgs['Path'] = $sUrl; |
315: | $this->prepareArgs($aArgs); |
316: | if ($sId) { |
317: | $aArgs['Path'] = basename($sId); |
318: | } |
319: | } |
320: | } |
321: | |
322: | |
323: | |
324: | |
325: | public function onGetItems($aArgs, &$mResult) |
326: | { |
327: | if ($aArgs['Type'] === self::$sStorageType) { |
328: | $mResult = []; |
329: | $oService = $this->GetDriveService(); |
330: | if ($oService) { |
331: | $sPath = \ltrim(\basename($aArgs['Path']), '/'); |
332: | |
333: | $aFileItems = []; |
334: | $sPageToken = null; |
335: | |
336: | if (empty($sPath)) { |
337: | $sPath = 'root'; |
338: | } |
339: | |
340: | $sQuery = "'".$sPath."' in parents and trashed = false"; |
341: | if (!empty($aArgs['Pattern'])) { |
342: | $sQuery .= " and name contains '".$aArgs['Pattern']."'"; |
343: | } |
344: | |
345: | do { |
346: | try { |
347: | $aParameters = [ |
348: | 'q' => $sQuery, |
349: | 'fields' => '*', |
350: | 'orderBy' => 'name' |
351: | ]; |
352: | if ($sPageToken) { |
353: | $aParameters['pageToken'] = $sPageToken; |
354: | } |
355: | |
356: | $oFiles = $oService->files->listFiles($aParameters); |
357: | $aFileItems = \array_merge($aFileItems, $oFiles->getFiles()); |
358: | $sPageToken = $oFiles->getNextPageToken(); |
359: | } catch (\Exception $e) { |
360: | $sPageToken = null; |
361: | } |
362: | } while ($sPageToken); |
363: | |
364: | foreach ($aFileItems as $oChild) { |
365: | $oItem = $this->PopulateFileInfo($aArgs['Type'], $aArgs['Path'], $oChild); |
366: | if ($oItem) { |
367: | $mResult[] = $oItem; |
368: | } |
369: | } |
370: | |
371: | if (isset($aArgs['PathRequired']) && $aArgs['PathRequired'] === true) { |
372: | $mResult['Path'] = array(); |
373: | if ($sPath !== 'root') { |
374: | $oPathInfo = $oService->files->get($sPath); |
375: | $mResult['Path'][] = $this->PopulateFileInfo($aArgs['Type'], $aArgs['Path'], $oPathInfo); |
376: | while (true) { |
377: | $aParrents = $oService->parents->listParents($sPath); |
378: | if ($aParrents == null ||count($aParrents) == 0) { |
379: | break; |
380: | } |
381: | $oParrent = $aParrents[0]; |
382: | $sPath = $oParrent->id; |
383: | if (!$oParrent->isRoot) { |
384: | $oItem = $oService->files->get($sPath); |
385: | if ($oItem) { |
386: | $mResult['Path'][] = $this->PopulateFileInfo($aArgs['Type'], $aArgs['Path'], $oItem); |
387: | } |
388: | } |
389: | } |
390: | } |
391: | } |
392: | } |
393: | |
394: | return true; |
395: | } |
396: | } |
397: | |
398: | protected function prepareArgs(&$aData) |
399: | { |
400: | $aPathInfo = \pathinfo($aData['Path']); |
401: | $sExtension = isset($aPathInfo['extension']) ? $aPathInfo['extension'] : ''; |
402: | if ($sExtension === 'url') { |
403: | $aArgs = array( |
404: | 'UserId' => $aData['UserId'], |
405: | 'Type' => $aData['Type'], |
406: | 'Path' => $aPathInfo['dirname'], |
407: | 'Name' => $aPathInfo['basename'], |
408: | 'IsThumb' => false |
409: | ); |
410: | $mResult = false; |
411: | \Aurora\System\Api::GetModuleManager()->broadcastEvent( |
412: | 'Files', |
413: | 'GetFile', |
414: | $aArgs, |
415: | $mResult |
416: | ); |
417: | if (\is_resource($mResult)) { |
418: | $aUrlFileInfo = \Aurora\System\Utils::parseIniString(\stream_get_contents($mResult)); |
419: | if ($aUrlFileInfo && isset($aUrlFileInfo['URL'])) { |
420: | if ((false !== \strpos($aUrlFileInfo['URL'], 'drive.google.com'))) { |
421: | $aData['Type'] = 'google'; |
422: | $aData['Path'] = $this->GetIdByLink($aUrlFileInfo['URL']); |
423: | } |
424: | } |
425: | } |
426: | } |
427: | } |
428: | |
429: | |
430: | |
431: | |
432: | public function onAfterCreateFolder(&$aArgs, &$mResult) |
433: | { |
434: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
435: | |
436: | if ($aArgs['Type'] === self::$sStorageType) { |
437: | $oService = $this->GetDriveService(); |
438: | if ($oService) { |
439: | $folder = new \Google_Service_Drive_DriveFile(); |
440: | $folder->setName($aArgs['FolderName']); |
441: | $folder->setMimeType('application/vnd.google-apps.folder'); |
442: | |
443: | |
444: | if ($aArgs['Path'] != null) { |
445: | $folder->setParents(array($aArgs['Path'])); |
446: | } |
447: | |
448: | try { |
449: | $oService->files->create($folder, array()); |
450: | $mResult = true; |
451: | } catch (\Exception $ex) { |
452: | $mResult = false; |
453: | } |
454: | } |
455: | } |
456: | } |
457: | |
458: | |
459: | |
460: | |
461: | public function onCreateFile($aArgs, &$Result) |
462: | { |
463: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
464: | |
465: | if ($aArgs['Type'] === self::$sStorageType) { |
466: | $oService = $this->GetDriveService(); |
467: | if ($oService) { |
468: | $sMimeType = \MailSo\Base\Utils::MimeContentType($aArgs['Name']); |
469: | $file = new \Google_Service_Drive_DriveFile(); |
470: | $file->setName($aArgs['Name']); |
471: | $file->setMimeType($sMimeType); |
472: | |
473: | $Path = \trim($aArgs['Path'], '/'); |
474: | |
475: | if ($Path != null) { |
476: | $file->setParents(array($Path)); |
477: | } |
478: | |
479: | try { |
480: | $sData = ''; |
481: | if (\is_resource($aArgs['Data'])) { |
482: | \rewind($aArgs['Data']); |
483: | $sData = \stream_get_contents($aArgs['Data']); |
484: | } else { |
485: | $sData = $aArgs['Data']; |
486: | } |
487: | $oService->files->create($file, array( |
488: | 'data' => $sData, |
489: | 'mimeType' => $sMimeType, |
490: | 'uploadType' => 'media' |
491: | )); |
492: | $Result = true; |
493: | } catch (\Exception $ex) { |
494: | $Result = false; |
495: | } |
496: | } |
497: | } |
498: | } |
499: | |
500: | |
501: | |
502: | |
503: | public function onAfterDelete(&$aData, &$mResult) |
504: | { |
505: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
506: | |
507: | if ($aData['Type'] === self::$sStorageType) { |
508: | $oService = $this->GetDriveService(); |
509: | if ($oService) { |
510: | $mResult = false; |
511: | |
512: | foreach ($aData['Items'] as $aItem) { |
513: | try { |
514: | $oService->files->delete($aItem['Name']); |
515: | $mResult = true; |
516: | } catch (\Exception $ex) { |
517: | $mResult = false; |
518: | } |
519: | } |
520: | } |
521: | } |
522: | } |
523: | |
524: | |
525: | |
526: | |
527: | public function onAfterRename(&$aData, &$mResult) |
528: | { |
529: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
530: | |
531: | if ($aData['Type'] === self::$sStorageType) { |
532: | $oService = $this->GetDriveService(); |
533: | if ($oService) { |
534: | $mResult = false; |
535: | |
536: | $file = new \Google_Service_Drive_DriveFile(); |
537: | $file->setName($aData['NewName']); |
538: | |
539: | $additionalParams = array(); |
540: | |
541: | try { |
542: | $oService->files->update($aData['Name'], $file, $additionalParams); |
543: | $mResult = true; |
544: | } catch (\Exception $ex) { |
545: | $mResult = false; |
546: | } |
547: | } |
548: | } |
549: | } |
550: | |
551: | |
552: | |
553: | |
554: | public function onAfterMove(&$aData, &$mResult) |
555: | { |
556: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
557: | |
558: | if ($aData['FromType'] === self::$sStorageType) { |
559: | $oService = $this->GetDriveService(); |
560: | if ($oService) { |
561: | $mResult = false; |
562: | |
563: | $aData['FromPath'] = $aData['FromPath'] === '' ? 'root' : \trim($aData['FromPath'], '/'); |
564: | $aData['ToPath'] = $aData['ToPath'] === '' ? 'root' : \trim($aData['ToPath'], '/'); |
565: | |
566: | foreach ($aData['Files'] as $aItem) { |
567: | $oFile = $oService->files->get($aItem['Name'], ['fields' => 'parents']); |
568: | try { |
569: | $previousParents = join(',', $oFile->parents); |
570: | $emptyFileMetadata = new \Google_Service_Drive_DriveFile(); |
571: | $oFile = $oService->files->update( |
572: | $aItem['Name'], |
573: | $emptyFileMetadata, |
574: | [ |
575: | 'addParents' => $aData['ToPath'], |
576: | 'removeParents' => $previousParents, |
577: | 'fields' => 'id, parents'] |
578: | ); |
579: | |
580: | $mResult = true; |
581: | } catch (\Exception $ex) { |
582: | $mResult = false; |
583: | } |
584: | } |
585: | } |
586: | } |
587: | } |
588: | |
589: | |
590: | |
591: | |
592: | public function onAfterCopy(&$aData, &$mResult) |
593: | { |
594: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
595: | |
596: | if ($aData['FromType'] === self::$sStorageType) { |
597: | $oService = $this->GetDriveService(); |
598: | if ($oService) { |
599: | $mResult = false; |
600: | |
601: | $aData['ToPath'] = $aData['ToPath'] === '' ? 'root' : \trim($aData['ToPath'], '/'); |
602: | |
603: | foreach ($aData['Files'] as $aItem) { |
604: | try { |
605: | $emptyFileMetadata = new \Google_Service_Drive_DriveFile(); |
606: | $emptyFileMetadata->parents = [$aData['ToPath']]; |
607: | $oService->files->copy( |
608: | $aItem['Name'], |
609: | $emptyFileMetadata, |
610: | [ |
611: | 'fields' => 'id, parents'] |
612: | ); |
613: | |
614: | $mResult = true; |
615: | } catch (\Exception $ex) { |
616: | $mResult = false; |
617: | } |
618: | } |
619: | } |
620: | } |
621: | } |
622: | |
623: | public function onAfterPopulateFileItem($aArgs, &$oItem) |
624: | { |
625: | if ($oItem->IsLink) { |
626: | if (false !== strpos($oItem->LinkUrl, 'drive.google.com')) { |
627: | $oItem->LinkType = 'google'; |
628: | |
629: | $oFileInfo = $this->GetLinkInfo($oItem->LinkUrl); |
630: | if ($oFileInfo) { |
631: | if (isset($oFileInfo->thumbnailLink)) { |
632: | $oItem->Thumb = true; |
633: | $oItem->ThumbnailUrl = $oFileInfo->thumbnailLink; |
634: | } |
635: | if ($oFileInfo->mimeType === "application/vnd.google-apps.folder") { |
636: | $oItem->UnshiftAction(array( |
637: | 'list' => array() |
638: | )); |
639: | |
640: | $oItem->Thumb = true; |
641: | $oItem->ThumbnailUrl = \MailSo\Base\Http::SingletonInstance()->GetFullUrl() . 'modules/' . self::GetName() . '/images/drive.png'; |
642: | } else { |
643: | $oItem->Size = isset($oFileInfo->fileSize) ? $oFileInfo->fileSize : $oItem->Size; |
644: | } |
645: | } |
646: | return true; |
647: | } |
648: | } |
649: | } |
650: | |
651: | protected function getMimeTypeForExport(&$oFileInfo) |
652: | { |
653: | switch($oFileInfo->mimeType) { |
654: | case 'application/vnd.google-apps.document': |
655: | $oFileInfo->name = $oFileInfo->name . '.docx'; |
656: | return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; |
657: | break; |
658: | case 'application/vnd.google-apps.spreadsheet': |
659: | $oFileInfo->name = $oFileInfo->name . '.xlsx'; |
660: | return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; |
661: | break; |
662: | case 'application/vnd.google-apps.drawing': |
663: | $oFileInfo->name = $oFileInfo->name . '.png'; |
664: | return 'image/png'; |
665: | break; |
666: | case 'application/vnd.google-apps.presentation': |
667: | $oFileInfo->name = $oFileInfo->name . '.pptx'; |
668: | return 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; |
669: | break; |
670: | default: |
671: | return ''; |
672: | break; |
673: | } |
674: | } |
675: | |
676: | protected function PopulateGoogleDriveFileInfo(&$oFileInfo) |
677: | { |
678: | if ($oFileInfo->mimeType !== "application/vnd.google-apps.folder") { |
679: | $this->getMimeTypeForExport($oFileInfo); |
680: | } |
681: | } |
682: | |
683: | protected function GetIdByLink($sLink) |
684: | { |
685: | $matches = array(); |
686: | \preg_match("%https://\w+\.google\.com/\w+/d/(.*?)/.*%", $sLink, $matches); |
687: | if (!isset($matches[1])) { |
688: | \preg_match("%https://\w+\.google\.com/open\?id=(.*)%", $sLink, $matches); |
689: | } |
690: | |
691: | return isset($matches[1]) ? $matches[1] : ''; |
692: | } |
693: | |
694: | protected function GetLinkInfo($sLink, $bLinkAsId = false) |
695: | { |
696: | $mResult = false; |
697: | $sGDId = ''; |
698: | if ($bLinkAsId) { |
699: | $sGDId = $sLink; |
700: | } else { |
701: | $sGDId = $this->GetIdByLink($sLink); |
702: | } |
703: | |
704: | if ($sGDId !== '') { |
705: | $oFileInfo = $this->_getFileInfo($sGDId); |
706: | if ($oFileInfo) { |
707: | $this->PopulateGoogleDriveFileInfo($oFileInfo); |
708: | $mResult = $oFileInfo; |
709: | } else { |
710: | $mResult = false; |
711: | } |
712: | } else { |
713: | $mResult = false; |
714: | } |
715: | |
716: | return $mResult; |
717: | } |
718: | |
719: | |
720: | |
721: | |
722: | |
723: | |
724: | |
725: | |
726: | public function onGetSettings($aArgs, &$mResult) |
727: | { |
728: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
729: | |
730: | if (!empty($oUser)) { |
731: | $aScope = array( |
732: | 'Name' => 'storage', |
733: | 'Description' => $this->i18N('SCOPE_FILESTORAGE'), |
734: | 'Value' => false |
735: | ); |
736: | if ($oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) { |
737: | $aScope['Value'] = $this->issetScope('storage'); |
738: | $mResult['Scopes'][] = $aScope; |
739: | } |
740: | if ($oUser->isNormalOrTenant()) { |
741: | if ($aArgs['OAuthAccount'] instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount) { |
742: | $aScope['Value'] = $aArgs['OAuthAccount']->issetScope('storage'); |
743: | } |
744: | if ($this->issetScope('storage')) { |
745: | $mResult['Scopes'][] = $aScope; |
746: | } |
747: | } |
748: | } |
749: | } |
750: | |
751: | public function onAfterUpdateSettings($aArgs, &$mResult) |
752: | { |
753: | $sScope = ''; |
754: | if (isset($aArgs['Scopes']) && is_array($aArgs['Scopes'])) { |
755: | foreach ($aArgs['Scopes'] as $aScope) { |
756: | if ($aScope['Name'] === 'storage') { |
757: | if ($aScope['Value']) { |
758: | $sScope = 'storage'; |
759: | break; |
760: | } |
761: | } |
762: | } |
763: | } |
764: | $this->setConfig('Scopes', $sScope); |
765: | $this->saveModuleConfig(); |
766: | } |
767: | |
768: | public function onAfterCheckUrl(&$aArgs, &$aReslult) |
769: | { |
770: | } |
771: | |
772: | public function onAfterGetQuota(&$aArgs, &$aResult) |
773: | { |
774: | if ($aArgs['Type'] === self::$sStorageType) { |
775: | $mResult = [0, 0]; |
776: | return true; |
777: | } |
778: | } |
779: | |
780: | public function onAfterCheckQuota(&$aArgs, &$mResult) |
781: | { |
782: | if ($aArgs['Type'] === self::$sStorageType) { |
783: | $mResult = true; |
784: | return true; |
785: | } |
786: | } |
787: | |
788: | public function EntryThumbnail() |
789: | { |
790: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
791: | |
792: | $sUrl = \Aurora\System\Utils::UrlSafeBase64Decode(\Aurora\System\Router::getItemByIndex(1, '')); |
793: | |
794: | $request = new Request( |
795: | 'GET', |
796: | $sUrl |
797: | ); |
798: | |
799: | $client = $this->GetClient(); |
800: | $response = $client->execute($request); |
801: | echo $response->getBody(); |
802: | } |
803: | } |
804: | |