| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\Modules\OfficeDocumentViewer; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | class Module extends \Aurora\System\Module\AbstractModule |
| 20: | { |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 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: | |
| 34: | |
| 35: | public static function getInstance() |
| 36: | { |
| 37: | return parent::getInstance(); |
| 38: | } |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public static function Decorator() |
| 44: | { |
| 45: | return parent::Decorator(); |
| 46: | } |
| 47: | |
| 48: | |
| 49: | |
| 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: | |
| 67: | |
| 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: | |
| 78: | |
| 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 |
| 109: | ); |
| 110: | |
| 111: | $sHash = \Aurora\System\Api::EncodeKeyValues($aValues); |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 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: | |
| 142: | |
| 143: | |
| 144: | public function onGetFile(&$aArguments, &$aResult) |
| 145: | { |
| 146: | if ($this->isOfficeDocument($aArguments['Name'])) { |
| 147: | $aArguments['NoRedirect'] = true; |
| 148: | } |
| 149: | } |
| 150: | } |
| 151: | |