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\OfficeDocumentViewer;
9:
10: /**
11: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
12: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
13: * @copyright Copyright (c) 2023, Afterlogic Corp.
14: *
15: * @property Settings $oModuleSettings
16: *
17: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractModule
20: {
21: /**
22: * Initializes module.
23: *
24: * @ignore
25: */
26: public function init()
27: {
28: $this->subscribeEvent('System::RunEntry::before', array($this, 'onBeforeFileViewEntry'), 5);
29: $this->subscribeEvent('Files::GetFile', array($this, 'onGetFile'), 10);
30: }
31:
32: /**
33: * @return Module
34: */
35: public static function getInstance()
36: {
37: return parent::getInstance();
38: }
39:
40: /**
41: * @return Module
42: */
43: public static function Decorator()
44: {
45: return parent::Decorator();
46: }
47:
48: /**
49: * @return Settings
50: */
51: public function getModuleSettings()
52: {
53: return $this->oModuleSettings;
54: }
55:
56: public function GetSettings()
57: {
58: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
59:
60: return array(
61: 'ExtensionsToView' => $this->oModuleSettings->ExtensionsToView
62: );
63: }
64:
65: /**
66: * @param string $sFileName = ''
67: * @return bool
68: */
69: protected function isOfficeDocument($sFileName = '')
70: {
71: $sExtensions = implode('|', $this->oModuleSettings->ExtensionsToView);
72: return !!preg_match('/\.(' . $sExtensions . ')$/', strtolower(trim($sFileName)));
73: }
74:
75: /**
76: *
77: * @param array $aArguments
78: * @param array $aResult
79: */
80: public function onBeforeFileViewEntry(&$aArguments, &$aResult)
81: {
82: $aEntries = [
83: 'download-file',
84: 'file-cache',
85: 'mail-attachment'
86:
87: ];
88: if (in_array($aArguments['EntryName'], $aEntries)) {
89: $sEntry = (string) \Aurora\System\Router::getItemByIndex(0, '');
90: $sHash = (string) \Aurora\System\Router::getItemByIndex(1, '');
91: $sAction = (string) \Aurora\System\Router::getItemByIndex(2, '');
92:
93: $aValues = \Aurora\System\Api::DecodeKeyValues($sHash);
94:
95: $sFileName = isset($aValues['FileName']) ? urldecode($aValues['FileName']) : '';
96: if (empty($sFileName)) {
97: $sFileName = isset($aValues['Name']) ? urldecode($aValues['Name']) : '';
98: }
99:
100: if ($this->isOfficeDocument($sFileName) && $sAction === 'view') {
101: if (!isset($aValues[\Aurora\System\Application::AUTH_TOKEN_KEY])) {
102: $aValues[\Aurora\System\Application::AUTH_TOKEN_KEY] = \Aurora\System\Api::UserSession()->Set(
103: array(
104: 'token' => 'auth',
105: 'id' => \Aurora\System\Api::getAuthenticatedUserId()
106: ),
107: time(),
108: time() + 60 * 5 // 5 min
109: );
110:
111: $sHash = \Aurora\System\Api::EncodeKeyValues($aValues);
112:
113: // 'https://view.officeapps.live.com/op/view.aspx?src=';
114: // 'https://view.officeapps.live.com/op/embed.aspx?src=';
115: // 'https://docs.google.com/viewer?embedded=true&url=';
116:
117: $sViewerUrl = $this->oModuleSettings->ViewerUrl;
118: if (!empty($sViewerUrl)) {
119: if (isset($_SERVER['HTTP_REFERER'])) {
120: $sHost = $_SERVER['HTTP_REFERER'];
121: } else {
122: $sHost = $_SERVER['HTTP_HOST'];
123: }
124: \header('Location: ' . $sViewerUrl . urlencode($sHost . '?' . $sEntry . '/' . $sHash . '/' . $sAction . '/' . time()));
125: }
126: } else {
127: $sAuthToken = $aValues[\Aurora\System\Application::AUTH_TOKEN_KEY] ?? null;
128: if ($sAuthToken) {
129: \Aurora\System\Api::setAuthToken($sAuthToken);
130: \Aurora\System\Api::setUserId(
131: \Aurora\System\Api::getAuthenticatedUserId($sAuthToken)
132: );
133: }
134: }
135: }
136: }
137: }
138:
139: /**
140: *
141: * @param array $aArguments
142: * @param array $aResult
143: */
144: public function onGetFile(&$aArguments, &$aResult)
145: {
146: if ($this->isOfficeDocument($aArguments['Name'])) {
147: $aArguments['NoRedirect'] = true;
148: }
149: }
150: }
151: