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\System;
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) 2019, Afterlogic Corp.
14: *
15: * @category Core
16: */
17: class Application
18: {
19: /**
20: * @type string
21: */
22: public const AUTH_TOKEN_KEY = 'AuthToken';
23:
24: /**
25: * @var \Aurora\System\Module\Manager
26: */
27: protected $oModuleManager;
28:
29: protected static $sBaseUrl = '';
30:
31: /**
32: * @return void
33: */
34: protected function __construct()
35: {
36: // \MailSo\Config::$FixIconvByMbstring = false;
37: \MailSo\Config::$SystemLogger = Api::SystemLogger();
38: \register_shutdown_function([$this, '__ApplicationShutdown']);
39: }
40:
41: public function __ApplicationShutdown()
42: {
43: $aStatistic = \MailSo\Base\Loader::Statistic();
44: if (\is_array($aStatistic)) {
45: if (isset($aStatistic['php']['memory_get_peak_usage'])) {
46: \Aurora\Api::Log('INFO[MEMORY]: Memory peak usage: ' . $aStatistic['php']['memory_get_peak_usage']);
47: }
48:
49: if (isset($aStatistic['time'])) {
50: \Aurora\Api::Log('INFO[TIME]: Time delta: ' . $aStatistic['time']);
51: }
52: }
53: }
54:
55: /**
56: * @return \Aurora\System\Application
57: */
58: public static function NewInstance()
59: {
60: return new self();
61: }
62:
63: /**
64: * @return \Aurora\System\Application
65: */
66: public static function SingletonInstance()
67: {
68: static $oInstance = null;
69: if (null === $oInstance) {
70: $oInstance = self::NewInstance();
71: }
72:
73: return $oInstance;
74: }
75:
76: public static function DebugMode($bDebug)
77: {
78: Api::$bDebug = $bDebug;
79: }
80:
81: public static function UseDbLogs()
82: {
83: Api::$bUseDbLog = true;
84: }
85:
86: public static function Start($sDefaultEntry = 'default')
87: {
88: if (!defined('AU_APP_START')) {
89: define('AU_APP_START', microtime(true));
90: }
91:
92: try {
93: Api::Init();
94: } catch (\Aurora\System\Exceptions\ApiException $oEx) {
95: \Aurora\System\Api::LogException($oEx);
96: }
97:
98: $oSettings = Api::GetSettings();
99: $sAllowedOrigin = $oSettings->AllowCrossDomainRequestsFromOrigin;
100: if ($sAllowedOrigin) {
101: // you cannot simply set Access-Control-Allow-Origin: * to allow any origin, it's doesn't work correctly with cookies
102: header('Access-Control-Allow-Origin: ' . (trim($sAllowedOrigin) === '*' ? @$_SERVER['HTTP_ORIGIN'] : $sAllowedOrigin));
103: // if set to false server tells the browser do not sent credentials (cookie)
104: header('Access-Control-Allow-Credentials: true');
105:
106: if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
107: if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
108: // may also be used: PUT, PATCH, HEAD etc, or *
109: header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
110: }
111:
112: if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
113: header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
114: }
115:
116: exit(0);
117: }
118: }
119:
120: self::GetVersion();
121:
122: $mResult = self::SingletonInstance()->Route(
123: \strtolower(
124: Router::getItemByIndex(0, $sDefaultEntry)
125: )
126: );
127: if (\MailSo\Base\Http::SingletonInstance()->GetRequest('Format') !== 'Raw') {
128: echo $mResult;
129: }
130: }
131:
132: /**
133: * @return string
134: */
135: public static function GetVersion()
136: {
137: if (!defined('AU_APP_VERSION')) {
138: $sVersion = @\file_get_contents(AU_APP_ROOT_PATH . 'VERSION');
139: \define('AU_APP_VERSION', $sVersion);
140: }
141: return AU_APP_VERSION;
142: }
143:
144: /**
145: * @return array
146: */
147: public static function GetPaths()
148: {
149: return Router::getItems();
150: }
151:
152: public function Route($sRoute)
153: {
154: return Api::GetModuleManager()->RunEntry($sRoute);
155: }
156:
157: public static function setBaseUrl($sBaseUrl = '')
158: {
159: self::$sBaseUrl = $sBaseUrl;
160: }
161:
162: public static function getBaseUrl()
163: {
164: if (empty(self::$sBaseUrl)) {
165: self::$sBaseUrl = \MailSo\Base\Http::SingletonInstance()->GetFullUrl();
166: }
167: return self::$sBaseUrl;
168: }
169: }
170: