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