1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\OfficeDocumentViewer; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | class Module extends \Aurora\System\Module\AbstractModule |
18: | { |
19: | |
20: | |
21: | |
22: | |
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: | |
69: | |
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: | |
80: | |
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 |
111: | ); |
112: | |
113: | $sHash = \Aurora\System\Api::EncodeKeyValues($aValues); |
114: | |
115: | |
116: | |
117: | |
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: | |
144: | |
145: | |
146: | public function onGetFile(&$aArguments, &$aResult) |
147: | { |
148: | if ($this->isOfficeDocument($aArguments['Name'])) { |
149: | $aArguments['NoRedirect'] = true; |
150: | } |
151: | } |
152: | } |
153: | |