1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\MailTnefWebclientPlugin;
9:
10: /**
11: * Adds Expand button on TNEF-attachment and shows its content.
12: *
13: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
14: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
15: * @copyright Copyright (c) 2023, Afterlogic Corp.
16: *
17: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractModule
20: {
21: /*
22: * @var $oApiFileCache \Aurora\System\Managers\Filecache
23: */
24: public $oApiFileCache = null;
25:
26: public function init()
27: {
28: $this->oApiFileCache = new \Aurora\System\Managers\Filecache();
29: }
30:
31: public function ExpandFile($UserId, $Hash)
32: {
33: $mResult = array();
34:
35: $sUUID = \Aurora\System\Api::getUserUUIDById($UserId);
36: $aValues = \Aurora\System\Api::DecodeKeyValues($Hash);
37: $oMailDecorator = \Aurora\Modules\Mail\Module::Decorator();
38: $aFiles = $oMailDecorator->SaveAttachmentsAsTempFiles($aValues['AccountID'], [$Hash]);
39: foreach ($aFiles as $sTempName => $sHash) {
40: if ($sHash === $Hash) {
41: $rResource = $this->oApiFileCache->getFile($sUUID, $sTempName);
42: $mResult = $this->expandTnefAttachment($sUUID, $rResource);
43: }
44: }
45:
46: return $mResult;
47: }
48:
49: private function expandTnefAttachment($sUUID, $rResource)
50: {
51: $mResult = array();
52:
53: $oTNEF = new Classes\TNEF();
54: if ($oTNEF && $rResource) {
55: $aData = $oTNEF->Decode(\stream_get_contents($rResource));
56: if (is_array($aData)) {
57: foreach ($aData as $aItem) {
58: if (is_array($aItem) && isset($aItem['name'], $aItem['stream'])) {
59: $sFileName = \MailSo\Base\Utils::Utf8Clear(basename($aItem['name']));
60:
61: $sTempName = md5(\microtime(true).rand(1000, 9999));
62: $rItemStream = fopen('php://memory', 'r+');
63: fwrite($rItemStream, $aItem['stream']);
64: rewind($rItemStream);
65: if ($this->oApiFileCache->putFile($sUUID, $sTempName, $rItemStream, '', self::GetName())) {
66: $sFileName = str_replace("\0", '', $sFileName);
67: $mResult[] = \Aurora\System\Utils::GetClientFileResponse(
68: self::GetName(),
69: \Aurora\System\Api::getAuthenticatedUserId(),
70: $sFileName,
71: $sTempName,
72: strlen($aItem['stream'])
73: );
74: }
75: }
76: }
77: }
78: }
79:
80: return $mResult;
81: }
82: }
83: