1 | <?php |
||
20 | class EnvironmentManager |
||
21 | { |
||
22 | const APPLICATION_TYPE_DIRECTORY = 'directory'; |
||
23 | const APPLICATION_TYPE_DOMAIN = 'domain'; |
||
24 | |||
25 | const COOKIE_AUTO_LOGIN_PREFIX = 'atln'; |
||
26 | const COOKIE_SESSION_PREFIX = 'atsn'; |
||
27 | |||
28 | const DEFAULT_APPLICATION = 'website'; |
||
29 | const DEFAULT_APPLICATION_URI = '/'; |
||
30 | const DEFAULT_MODULE = 'Website'; |
||
31 | const DEFAULT_THEME = 'default'; |
||
32 | const DEFAULT_THEME_RESOURCE_PATH = '/resources/default_theme'; |
||
33 | |||
34 | const SESSION_SALT = 'WebHemi'; |
||
35 | |||
36 | /** @var ConfigInterface */ |
||
37 | private $config; |
||
38 | /** @var string */ |
||
39 | private $url; |
||
40 | /** @var string */ |
||
41 | private $subDomain; |
||
42 | /** @var string */ |
||
43 | private $mainDomain; |
||
44 | /** @var string */ |
||
45 | private $applicationDomain; |
||
46 | /** @var string */ |
||
47 | private $documentRoot; |
||
48 | /** @var string */ |
||
49 | private $selectedModule; |
||
50 | /** @var string */ |
||
51 | private $selectedApplication; |
||
52 | /** @var string */ |
||
53 | private $selectedApplicationUri; |
||
54 | /** @var string */ |
||
55 | private $selectedTheme; |
||
56 | /** @var string */ |
||
57 | private $selectedThemeResourcePath; |
||
58 | /** @var array */ |
||
59 | private $environmentData; |
||
60 | |||
61 | /** |
||
62 | * ModuleManager constructor. |
||
63 | * |
||
64 | * @param ConfigInterface $config |
||
65 | * @param array $getData |
||
66 | * @param array $postData |
||
67 | * @param array $serverData |
||
68 | * @param array $cookieData |
||
69 | * @param array $filesData |
||
70 | */ |
||
71 | public function __construct( |
||
72 | ConfigInterface $config, |
||
73 | array $getData, |
||
74 | array $postData, |
||
75 | array $serverData, |
||
76 | array $cookieData, |
||
77 | array $filesData |
||
78 | ) { |
||
79 | $this->config = $config; |
||
80 | $this->documentRoot = realpath(__DIR__.'/../../../'); |
||
81 | |||
82 | $this->environmentData = [ |
||
83 | 'GET' => $getData, |
||
84 | 'POST' => $postData, |
||
85 | 'SERVER' => $serverData, |
||
86 | 'COOKIE' => $cookieData, |
||
87 | 'FILES' => $filesData, |
||
88 | ]; |
||
89 | |||
90 | $isHttps = isset($this->environmentData['SERVER']['HTTPS']) && $this->environmentData['SERVER']['HTTPS']; |
||
91 | $this->url = 'http'.($isHttps ? 's' : '').'://' |
||
92 | .$this->environmentData['SERVER']['HTTP_HOST'] |
||
93 | .$this->environmentData['SERVER']['REQUEST_URI'] |
||
94 | .$this->environmentData['SERVER']['QUERY_STRING']; |
||
95 | |||
96 | $this->selectedModule = self::DEFAULT_MODULE; |
||
97 | $this->selectedApplication = self::DEFAULT_APPLICATION; |
||
98 | $this->selectedTheme = self::DEFAULT_THEME; |
||
99 | $this->selectedThemeResourcePath = self::DEFAULT_THEME_RESOURCE_PATH; |
||
100 | $this->selectedApplicationUri = self::DEFAULT_APPLICATION_URI; |
||
101 | |||
102 | $this->secureSession() |
||
103 | ->setDomain() |
||
104 | ->selectModuleApplicationAndTheme(); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Gets the selected application. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getSelectedApplication() |
||
116 | |||
117 | /** |
||
118 | * Get the URI path for the selected application. Required for the RouterAdapter to work with directory-based |
||
119 | * applications correctly. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getSelectedApplicationUri() |
||
127 | |||
128 | /** |
||
129 | * Gets the selected module. |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getSelectedModule() |
||
137 | |||
138 | /** |
||
139 | * Gets the selected theme. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getSelectedTheme() |
||
147 | |||
148 | /** |
||
149 | * Gets the resource path for the selected theme. |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getResourcePath() |
||
157 | |||
158 | /** |
||
159 | * Gets environment data. |
||
160 | * |
||
161 | * @param string $key |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | public function getEnvironmentData($key) |
||
173 | |||
174 | /** |
||
175 | * Gets the template settings for a specific theme. |
||
176 | * |
||
177 | * @return ConfigInterface |
||
178 | */ |
||
179 | public function getApplicationTemplateSettings() |
||
183 | |||
184 | /** |
||
185 | * Gets the routing settings for the selected module. |
||
186 | * |
||
187 | * @return ConfigInterface |
||
188 | */ |
||
189 | public function getModuleRouteSettings() |
||
193 | |||
194 | /** |
||
195 | * Overwrite PHP settings to be more secure |
||
196 | * |
||
197 | * @codeCoverageIgnore |
||
198 | * |
||
199 | * @return $this |
||
200 | */ |
||
201 | private function secureSession() |
||
218 | |||
219 | /** |
||
220 | * Parses server data and tries to set domain information. |
||
221 | * |
||
222 | * @return $this |
||
223 | */ |
||
224 | private function setDomain() |
||
254 | |||
255 | /** |
||
256 | * From the parsed domain data, selects the application, module and theme. |
||
257 | * |
||
258 | * @return $this |
||
259 | */ |
||
260 | private function selectModuleApplicationAndTheme() |
||
298 | |||
299 | /** |
||
300 | * Checks from type, path it the current URI segment is valid. |
||
301 | * |
||
302 | * @param string $type |
||
303 | * @param string $path |
||
304 | * @param string $subDirectory |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | private function checkDirectoryIsValid($type, $path, $subDirectory) |
||
315 | |||
316 | /** |
||
317 | * Checks from type and path if the domain is valid. |
||
318 | * |
||
319 | * @param string $type |
||
320 | * @param string $path |
||
321 | * |
||
322 | * @return bool |
||
323 | */ |
||
324 | private function checkDomainIsValid($type, $path) |
||
330 | } |
||
331 |