1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\MailZipWebclientPlugin; |
9: | |
10: | use Aurora\Modules\Mail\Module as MailModule; |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | class Module extends \Aurora\System\Module\AbstractModule |
24: | { |
25: | |
26: | |
27: | |
28: | public $oApiFileCache = null; |
29: | |
30: | public function init() |
31: | { |
32: | $this->oApiFileCache = new \Aurora\System\Managers\Filecache(); |
33: | } |
34: | |
35: | |
36: | |
37: | |
38: | public static function getInstance() |
39: | { |
40: | return parent::getInstance(); |
41: | } |
42: | |
43: | |
44: | |
45: | |
46: | public static function Decorator() |
47: | { |
48: | return parent::Decorator(); |
49: | } |
50: | |
51: | |
52: | |
53: | |
54: | public function getModuleSettings() |
55: | { |
56: | return $this->oModuleSettings; |
57: | } |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | |
64: | public function GetSettings() |
65: | { |
66: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous); |
67: | |
68: | return array( |
69: | 'AllowZip' => class_exists('ZipArchive') |
70: | ); |
71: | } |
72: | |
73: | public function ExpandFile($UserId, $Hash) |
74: | { |
75: | $mResult = array(); |
76: | |
77: | $sUUID = \Aurora\System\Api::getUserUUIDById($UserId); |
78: | $aValues = \Aurora\System\Api::DecodeKeyValues($Hash); |
79: | $oMailModuleDecorator = MailModule::Decorator(); |
80: | |
81: | if (isset($aValues['AccountID'])) { |
82: | $oAccount = $oMailModuleDecorator->GetAccount($aValues['AccountID']); |
83: | if ($oAccount->IdUser === $UserId) { |
84: | $aFiles = $oMailModuleDecorator->SaveAttachmentsAsTempFiles($aValues['AccountID'], [$Hash]); |
85: | $sTempName = array_search($Hash, $aFiles); |
86: | if ($sTempName) { |
87: | $sTempZipPath = $this->oApiFileCache->generateFullFilePath($sUUID, $sTempName); |
88: | $mResult = $this->expandZipAttachment($sUUID, $sTempZipPath); |
89: | } |
90: | } |
91: | } else { |
92: | $sTempName = (isset($aValues['TempName'])) ? $aValues['TempName'] : 0; |
93: | $sTempZipPath = $this->oApiFileCache->generateFullFilePath($sUUID, $sTempName); |
94: | $mResult = $this->expandZipAttachment($sUUID, $sTempZipPath); |
95: | } |
96: | |
97: | return $mResult; |
98: | } |
99: | |
100: | private function expandZipAttachment($sUUID, $sTempZipPath) |
101: | { |
102: | $aResult = array(); |
103: | $bHasMore = false; |
104: | |
105: | $oZip = new \ZipArchive(); |
106: | |
107: | if (file_exists($sTempZipPath) && $oZip->open($sTempZipPath)) { |
108: | |
109: | $aFilesData = []; |
110: | for ($iIndex = 0; $iIndex < $oZip->numFiles; $iIndex++) { |
111: | $aStat = $oZip->statIndex($iIndex); |
112: | if (!empty($aStat['name'])) { |
113: | $aNameParts = explode('/', $aStat['name']); |
114: | $iFileLevel = count($aNameParts); |
115: | $sFileName = \MailSo\Base\Utils::Utf8Clear(basename($aStat['name'])); |
116: | if (!isset($aFilesData[$iFileLevel]) || !is_array($aFilesData[$iFileLevel])) { |
117: | $aFilesData[$iFileLevel] = []; |
118: | } |
119: | $aFilesData[$iFileLevel][] = [ |
120: | 'FileName' => $sFileName, |
121: | 'Index' => $iIndex |
122: | ]; |
123: | } |
124: | } |
125: | |
126: | |
127: | |
128: | |
129: | $iFoldersCount = 0; |
130: | $iExpandZipFilesLimit = $this->oModuleSettings->ExpandZipFilesLimit; |
131: | foreach ($aFilesData as $aFiles) { |
132: | if (count($aResult) >= $iExpandZipFilesLimit) { |
133: | break; |
134: | } |
135: | $iFilesCount = count($aFiles); |
136: | for ($iFileIndex = 0; $iFileIndex < $iFilesCount && count($aResult) < $iExpandZipFilesLimit; $iFileIndex++) { |
137: | $aFileItemData = $aFiles[$iFileIndex]; |
138: | $sFile = $oZip->getFromIndex($aFileItemData['Index']); |
139: | $iFileSize = $sFile ? strlen($sFile) : 0; |
140: | if ($sFile) { |
141: | $sTempName = md5(microtime(true) . rand(1000, 9999)); |
142: | |
143: | if ($this->oApiFileCache->put($sUUID, $sTempName, $sFile, '', self::GetName())) { |
144: | unset($sFile); |
145: | |
146: | $aResult[] = \Aurora\System\Utils::GetClientFileResponse( |
147: | self::GetName(), |
148: | \Aurora\System\Api::getAuthenticatedUserId(), |
149: | $aFileItemData['FileName'], |
150: | $sTempName, |
151: | $iFileSize |
152: | ); |
153: | } else { |
154: | unset($sFile); |
155: | } |
156: | } else { |
157: | |
158: | $iFoldersCount++; |
159: | } |
160: | } |
161: | } |
162: | |
163: | $bHasMore = ($iFoldersCount + count($aResult)) < $oZip->numFiles; |
164: | $oZip->close(); |
165: | } |
166: | return [ |
167: | 'Files' => $aResult, |
168: | 'HasMore' => $bHasMore |
169: | ]; |
170: | } |
171: | |
172: | protected function getNonExistentFileName($aFiles, $sFileName) |
173: | { |
174: | $iIndex = 1; |
175: | $sFileNamePathInfo = pathinfo($sFileName); |
176: | $sFileNameExt = ''; |
177: | $sFileNameWOExt = $sFileName; |
178: | if (isset($sFileNamePathInfo['extension'])) { |
179: | $sFileNameExt = '.' . $sFileNamePathInfo['extension']; |
180: | } |
181: | |
182: | if (isset($sFileNamePathInfo['filename'])) { |
183: | $sFileNameWOExt = $sFileNamePathInfo['filename']; |
184: | } |
185: | |
186: | while (count(array_filter($aFiles, function ($item) use ($sFileName) { return $item[1] === $sFileName; })) > 0) { |
187: | $sFileName = $sFileNameWOExt . '(' . $iIndex . ')' . $sFileNameExt; |
188: | $iIndex++; |
189: | } |
190: | |
191: | return $sFileName; |
192: | } |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | |
199: | |
200: | public function SaveAttachments($UserId, $AccountID, $Attachments = array()) |
201: | { |
202: | $mResult = false; |
203: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
204: | |
205: | $aAddFiles = array(); |
206: | $sUUID = \Aurora\System\Api::getUserUUIDById($UserId); |
207: | |
208: | $oMailModuleDecorator = MailModule::Decorator(); |
209: | if ($oMailModuleDecorator) { |
210: | $aTempFiles = $oMailModuleDecorator->SaveAttachmentsAsTempFiles($AccountID, $Attachments); |
211: | if (\is_array($aTempFiles)) { |
212: | foreach ($aTempFiles as $sTempName => $sData) { |
213: | $aData = \Aurora\System\Api::DecodeKeyValues($sData); |
214: | if (\is_array($aData) && isset($aData['FileName'])) { |
215: | $sFileName = (string) $aData['FileName']; |
216: | $sTempPath = $this->oApiFileCache->generateFullFilePath($sUUID, $sTempName); |
217: | $sFileName = $this->getNonExistentFileName($aAddFiles, $sFileName); |
218: | $aAddFiles[] = array($sTempPath, $sFileName); |
219: | } |
220: | } |
221: | } |
222: | } |
223: | |
224: | if (count($aAddFiles) > 0) { |
225: | $oZip = new \ZipArchive(); |
226: | |
227: | $sZipTempName = md5(microtime()); |
228: | $sZipTempPath = $this->oApiFileCache->generateFullFilePath($sUUID, $sZipTempName, '', self::GetName()); |
229: | if ($oZip->open($sZipTempPath, \ZipArchive::CREATE)) { |
230: | foreach ($aAddFiles as $aItem) { |
231: | $oZip->addFile($aItem[0], $aItem[1]); |
232: | } |
233: | $oZip->close(); |
234: | $iFileSize = $this->oApiFileCache->fileSize($sUUID, $sZipTempName, '', self::GetName()); |
235: | $mResult = \Aurora\System\Utils::GetClientFileResponse( |
236: | self::GetName(), |
237: | $UserId, |
238: | 'attachments.zip', |
239: | $sZipTempName, |
240: | $iFileSize |
241: | ); |
242: | } |
243: | } |
244: | |
245: | return $mResult; |
246: | } |
247: | } |
248: | |