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: * @property Settings $oModuleSettings
18: *
19: * @package Modules
20: */
21: class Module extends \Aurora\System\Module\AbstractModule
22: {
23: /*
24: * @var $oApiFileCache \Aurora\System\Managers\Filecache
25: */
26: public $oApiFileCache = null;
27:
28: public function init()
29: {
30: $this->oApiFileCache = new \Aurora\System\Managers\Filecache();
31: }
32:
33: /**
34: * @return Module
35: */
36: public static function getInstance()
37: {
38: return parent::getInstance();
39: }
40:
41: /**
42: * @return Module
43: */
44: public static function Decorator()
45: {
46: return parent::Decorator();
47: }
48:
49: /**
50: * @return Settings
51: */
52: public function getModuleSettings()
53: {
54: return $this->oModuleSettings;
55: }
56:
57: public function ExpandFile($UserId, $Hash)
58: {
59: $mResult = array();
60:
61: $sUUID = \Aurora\System\Api::getUserUUIDById($UserId);
62: $aValues = \Aurora\System\Api::DecodeKeyValues($Hash);
63: $oMailDecorator = \Aurora\Modules\Mail\Module::Decorator();
64: $aFiles = $oMailDecorator->SaveAttachmentsAsTempFiles($aValues['AccountID'], [$Hash]);
65: foreach ($aFiles as $sTempName => $sHash) {
66: if ($sHash === $Hash) {
67: $rResource = $this->oApiFileCache->getFile($sUUID, $sTempName);
68: $mResult = $this->expandTnefAttachment($sUUID, $rResource);
69: }
70: }
71:
72: return $mResult;
73: }
74:
75: private function expandTnefAttachment($sUUID, $rResource)
76: {
77: $mResult = array();
78:
79: $attachment = new \TNEFDecoder\TNEFAttachment();
80: if ($rResource) {
81: $attachment->decodeTnef(\stream_get_contents($rResource));
82: $files = $attachment->getFiles();
83:
84: foreach ($files as $file) {
85: $sFileName = $file->getName();
86:
87: $sFileNameEncoding = mb_detect_encoding($sFileName, array("UTF-8", "SJIS"));
88: if ($sFileNameEncoding != "UTF-8") {
89: $sFileName = mb_convert_encoding($sFileName, "UTF-8", $sFileNameEncoding);
90: }
91:
92: $sTempName = md5(\microtime(true) . rand(1000, 9999));
93: $rItemStream = fopen('php://memory', 'r+');
94: fwrite($rItemStream, $file->getContent());
95: rewind($rItemStream);
96:
97: if ($this->oApiFileCache->putFile($sUUID, $sTempName, $rItemStream, '', self::GetName())) {
98: $sFileName = str_replace("\0", '', $sFileName);
99: $mResult[] = \Aurora\System\Utils::GetClientFileResponse(
100: self::GetName(),
101: \Aurora\System\Api::getAuthenticatedUserId(),
102: $sFileName,
103: $sTempName,
104: strlen($file->getSize())
105: );
106: }
107: }
108: }
109:
110: return $mResult;
111: }
112: }
113: