1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem\Environment; |
4 | | | |
5 | | | use Doctrine\Common\Annotations\AnnotationReader; |
6 | | | use Doctrine\ORM\EntityManager; |
7 | | | use DoctrineBoot; |
8 | | | use Phem\Core\Collection; |
9 | | | use Phem\Libraries\Forms\Annotations\Relation; |
10 | | | use Phem\Libraries\ResourceLoading\ResourceManager; |
11 | | | use Phem\Libraries\Security\Authentication\MySQLAuthenticationProvider; |
12 | | | use Phem\Libraries\Security\Authorization\IAuthenticationProvider; |
13 | | | use Phem\Libraries\Security\Authorization\IAuthorizationProvider; |
14 | | | use Phem\Libraries\Security\Authorization\MySQLAuthorizationProvider; |
15 | | | use Phem\Libraries\Security\Management\IPermissionManager; |
16 | | | use Phem\Libraries\Security\Management\IUserManager; |
17 | | | use Phem\Libraries\Security\Management\MySQLPermissionManager; |
18 | | | use Phem\Libraries\Security\Management\MySQLTaskManager; |
19 | | | use Phem\Libraries\Security\Management\MySQLUserManager; |
20 | | | use Phem\Libraries\Security\Management\TaskManager; |
21 | | | use ReflectionClass; |
22 | | | use Srovnavac\Models\Entities\File; |
23 | | | use Twig_Environment; |
24 | | | use Twig_Loader_Filesystem; |
25 | | | |
26 | | | /** |
27 | | | * This class provides basic global functions for managing sessions, http requests etc. |
28 | | | * |
29 | | | * @author Jakub PetrĹžĂlka <petrzilka@czweb.net> |
30 | | | */ |
31 | | | class EnvironmentManager |
32 | | | { |
33 | | | |
34 | | | private static $session; |
35 | | | private static $entityManagers; |
36 | | | private static $linkBuilder; |
37 | | | private static $temporaryVariables = Array(); |
38 | | | private static $twig; |
39 | | | |
40 | | | /* Resources */ |
41 | | | private static $resourceManager; |
42 | | | |
43 | | | /* Security */ |
44 | | | private static $authenticationProvider; |
45 | | | private static $authorizationProvider; |
46 | | | private static $permissionManager; |
47 | | | private static $taskManager; |
48 | | | private static $userManager; |
49 | | | |
50 | | | /** |
51 | | | * |
52 | | | * @return ResourceManager |
53 | | | */ |
54 | | | static function getResourceManager() |
55 | | | { |
56 | | | if (self::$resourceManager == null) |
57 | | | { |
58 | | | self::$resourceManager = new ResourceManager(); |
59 | | | |
60 | | | } |
61 | | | |
62 | | | return self::$resourceManager; |
63 | | | } |
64 | | | |
65 | | | /** |
66 | | | * |
67 | | | * @return IAuthenticationProvider |
68 | | | */ |
69 | | | static function getAuthenticationProvider() |
70 | | | { |
71 | | | if (self::$authenticationProvider == null) |
72 | | | { |
73 | | | self::$authenticationProvider = new MySQLAuthenticationProvider(); |
74 | | | } |
75 | | | |
76 | | | return self::$authenticationProvider; |
77 | | | } |
78 | | | |
79 | | | /** |
80 | | | * |
81 | | | * @return IAuthorizationProvider |
82 | | | */ |
83 | | | static function getAuthorizationProvider() |
84 | | | { |
85 | | | if (self::$authorizationProvider == null) |
86 | | | { |
87 | | | self::$authorizationProvider = new MySQLAuthorizationProvider(); |
88 | | | } |
89 | | | |
90 | | | return self::$authorizationProvider; |
91 | | | } |
92 | | | |
93 | | | /** |
94 | | | * |
95 | | | * @return IPermissionManager |
96 | | | */ |
97 | | | static function getPermissionManager() |
98 | | | { |
99 | | | if (self::$permissionManager == null) |
100 | | | { |
101 | | | self::$permissionManager = new MySQLPermissionManager(); |
102 | | | } |
103 | | | |
104 | | | return self::$permissionManager; |
105 | | | } |
106 | | | |
107 | | | /** |
108 | | | * |
109 | | | * @return TaskManager |
110 | | | */ |
111 | | | static function getTaskManager() |
112 | | | { |
113 | | | if (self::$taskManager == null) |
114 | | | { |
115 | | | self::$taskManager = new MySQLTaskManager(); |
116 | | | } |
117 | | | |
118 | | | return self::$taskManager; |
119 | | | } |
120 | | | |
121 | | | /** |
122 | | | * |
123 | | | * @return IUserManager |
124 | | | */ |
125 | | | static function getUserManager() |
126 | | | { |
127 | | | if (self::$userManager == null) |
128 | | | { |
129 | | | self::$userManager = new MySQLUserManager(); |
130 | | | } |
131 | | | |
132 | | | return self::$userManager; |
133 | | | } |
134 | | | |
135 | | | /** |
136 | | | * @param string $key |
137 | | | * @return mixed temporaryVariable |
138 | | | */ |
139 | | | public static |
140 | | | function getVariable($key) |
141 | | | { |
142 | | | if (array_key_exists($key, self::$temporaryVariables)) |
143 | | | return self::$temporaryVariables[$key]; |
144 | | | else |
145 | | | return null; |
146 | | | } |
147 | | | |
148 | | | /** |
149 | | | * @param string $key |
150 | | | * @param mixed $value |
151 | | | * @return mixed temporaryVariable |
152 | | | */ |
153 | | | public static function setVariable($key, $value) |
154 | | | { |
155 | | | if (self::$temporaryVariables == null) |
156 | | | self::$temporaryVariables = Array(); |
157 | | | self::$temporaryVariables[$key] = $value; |
158 | | | } |
159 | | | |
160 | | | /** |
161 | | | * Converts matching rule to regexp |
162 | | | * |
163 | | | * @param string $matchRule The input match rule |
164 | | | * @return string regexp |
165 | | | */ |
166 | | | private static function parseMatchRule($matchRule) |
167 | | | { |
168 | | | switch ($matchRule) |
169 | | | { |
170 | | | case "word": |
171 | | | return "/[A-Za-z0-9_]/"; |
172 | | | default: |
173 | | | return $matchRule; |
174 | | | } |
175 | | | } |
176 | | | |
177 | | | |
178 | | | |
179 | | | |
180 | | | /** |
181 | | | * |
182 | | | * Gets the http request variable - from POST or GET method |
183 | | | * (POST has higher priority if both of them are set) |
184 | | | * |
185 | | | * @param string $name Name of the variable |
186 | | | * @param mixed $defaultValue The default value being returned if matchRule check fails or if variable is not set |
187 | | | * @param string $matchRule Matching rule can be [ "word" | regex ] |
188 | | | * @return mixed The value of environment variable with given name |
189 | | | */ |
190 | | | static function getRequestVar($name, $defaultValue = null, $matchRule = null) |
191 | | | { |
192 | | | $regexp = self::parseMatchRule($matchRule); |
193 | | | |
194 | | | $var = null; |
195 | | | if (isset($_POST[$name])) |
196 | | | $var = htmlspecialchars($_POST[$name]); |
197 | | | else if (isset($_GET[$name])) |
198 | | | $var = htmlspecialchars($_GET[$name]); |
199 | | | |
200 | | | if (($var != null) && ((($matchRule != null) && (preg_match($regexp, $var) > 0)) || ($matchRule == null))) |
201 | | | { |
202 | | | return $var; |
203 | | | } else |
204 | | | { |
205 | | | return $defaultValue; |
206 | | | } |
207 | | | } |
208 | | | |
209 | | | /** |
210 | | | * Factory method for accessing the Session object |
211 | | | * |
212 | | | * @return Session The Session object |
213 | | | */ |
214 | | | static function getSession() |
215 | | | { |
216 | | | if (self::$session == null) |
217 | | | { |
218 | | | self::$session = new Session(); |
219 | | | } |
220 | | | |
221 | | | return self::$session; |
222 | | | } |
223 | | | |
224 | | | /** |
225 | | | * |
226 | | | * @return EntityManager Doctrine entity manager |
227 | | | */ |
228 | | | static function getEntityManager($selectedDb = null) |
229 | | | { |
230 | | | if (self::$entityManagers == null) |
231 | | | { |
232 | | | self::$entityManagers = new Collection(); |
233 | | | } |
234 | | | |
235 | | | $dbConfig = unserialize(DB_CONFIG); |
236 | | | |
237 | | | if ($selectedDb == null) |
238 | | | { |
239 | | | $selectedDb = self::getSession()->getVar(ROOT_NAMESPACE . ".selectedDatabase"); |
240 | | | } |
241 | | | |
242 | | | if ($selectedDb == null) |
243 | | | { |
244 | | | $selectedDb = DB_DEFAULT_CONFIG; |
245 | | | } |
246 | | | |
247 | | | if (self::$entityManagers->get($dbConfig[$selectedDb]['name']) == null) |
248 | | | { |
249 | | | |
250 | | | $connection = array( |
251 | | | 'driver' => 'pdo_mysql', |
252 | | | 'host' => DB_HOST, |
253 | | | 'dbname' => $dbConfig[$selectedDb]['name'], |
254 | | | 'user' => $dbConfig[$selectedDb]['user'], |
255 | | | 'password' => $dbConfig[$selectedDb]['password'], |
256 | | | 'charset' => 'utf8', |
257 | | | 'driverOptions' => array( |
258 | | | 1002 => 'SET NAMES utf8') |
259 | | | ); |
260 | | | |
261 | | | self::$entityManagers->put($dbConfig[$selectedDb]['name'], EntityManager::create($connection, DoctrineBoot::$config)); |
262 | | | } |
263 | | | |
264 | | | return self::$entityManagers->get($dbConfig[$selectedDb]['name']); |
265 | | | } |
266 | | | |
267 | | | /** |
268 | | | * |
269 | | | * @return LinkBuilder |
270 | | | */ |
271 | | | static function getLinkBuilder() |
272 | | | { |
273 | | | if (self::$linkBuilder == null) |
274 | | | { |
275 | | | self::$linkBuilder = new LinkBuilder(); |
276 | | | } |
277 | | | |
278 | | | return self::$linkBuilder; |
279 | | | } |
280 | | | |
281 | | | static function getTwig() |
282 | | | { |
283 | | | if (self::$twig == null) |
284 | | | { |
285 | | | $loader = new Twig_Loader_Filesystem(TEMPLATE_DIR); |
286 | | | $loader->addPath( |
287 | | | FRAMEWORK_DIR . DS . |
288 | | | BUILTIN_RESOURCES_DIR . DS . |
289 | | | BUILTIN_TEMPLATES_DIR |
290 | | | , BUILTIN_TEMPLATES_NAMESPACE |
291 | | | ); |
292 | | | $cacheAutoreload = false; |
293 | | | if (defined("TWIG_DEVEL_MODE")) |
294 | | | $cacheAutoreload = true; |
295 | | | $twig = new Twig_Environment($loader, array( |
296 | | | 'cache' => TEMPLATE_CACHE_DIR, |
297 | | | 'auto_reload' => $cacheAutoreload, |
298 | | | )); |
299 | | | self::$twig = $twig; |
300 | | | } |
301 | | | |
302 | | | return self::$twig; |
303 | | | } |
304 | | | |
305 | | | static function getSubmittedFormData($formName) |
306 | | | { |
307 | | | $formData = array(); |
308 | | | foreach ($_POST as $key => $value) |
309 | | | { |
310 | | | if (strpos($key, $formName) == 0) |
311 | | | { |
312 | | | $formData[substr($key, strlen($formName) + 1)] |
313 | | | = $value; |
314 | | | } |
315 | | | } |
316 | | | return $formData; |
317 | | | } |
318 | | | |
319 | | | static function getSubmittedFormObject($object = null, $data = null, $prefix = "Phem_") |
320 | | | { |
321 | | | if ($data == null) |
322 | | | $data = self::getSubmittedFormData($prefix); |
323 | | | |
324 | | | if (empty($data)) |
325 | | | return null; |
326 | | | |
327 | | | $className = $data['source']; |
328 | | | unset($data['source']); |
329 | | | if ($object == null) |
330 | | | { |
331 | | | $object = new $className(); |
332 | | | } |
333 | | | |
334 | | | /* @var $r ReflectionClass */ |
335 | | | $r = new ReflectionClass($className); |
336 | | | |
337 | | | foreach ($data as $property => $value) |
338 | | | { |
339 | | | $path = explode('_', $property); |
340 | | | if (count($path) == 1) |
341 | | | { |
342 | | | $setMethod = "set" . ucFirst($property); |
343 | | | if ($r->hasMethod($setMethod)) |
344 | | | { |
345 | | | $object->$setMethod($value); |
346 | | | unset($data[$property]); |
347 | | | } |
348 | | | } |
349 | | | else |
350 | | | { |
351 | | | $tmp = $object; |
352 | | | foreach ($path as $p) |
353 | | | { |
354 | | | $p = lcfirst($p); |
355 | | | $getMethod = "get" . ucFirst($p); |
356 | | | $setMethod = "set" . ucFirst($p); |
357 | | | if(!method_exists($tmp, $getMethod)) |
358 | | | throw new \Exception("Objekt tridy \"" . get_class($tmp) . "\" neobsahuje metodu \"" . $getMethod . "\"."); |
359 | | | if (!is_object($tmp->$getMethod())) |
360 | | | { |
361 | | | $rr = new ReflectionClass(get_class($tmp)); |
362 | | | $annotationReader = new AnnotationReader(); |
363 | | | $annotations = $annotationReader->getPropertyAnnotations($rr->getProperty($p)); |
364 | | | foreach ($annotations as $annotation) |
365 | | | { |
366 | | | if ($annotation instanceof Relation) |
367 | | | { |
368 | | | $constructor = $annotation->source; |
369 | | | $tmp->$setMethod(new $constructor()); |
370 | | | $tmp = $tmp->$getMethod(); |
371 | | | break; |
372 | | | } |
373 | | | } |
374 | | | } |
375 | | | else |
376 | | | { |
377 | | | $tmp = $tmp->$getMethod(); |
378 | | | } |
379 | | | /* pokud je to poslednĂ element cesty, nastav hodnotu */ |
380 | | | if ($p === lcfirst(end($path))) |
381 | | | { |
382 | | | $tmp->$setMethod($value); |
383 | | | } |
384 | | | } |
385 | | | } |
386 | | | } |
387 | | | return $object; |
388 | | | } |
389 | | | |
390 | | | static function closeDbConnection() |
391 | | | { |
392 | | | if (self::$entityManagers != null) |
393 | | | { |
394 | | | foreach (self::$entityManagers as $em) |
395 | | | { |
396 | | | $em->clear(); |
397 | | | $em->close(); |
398 | | | $em = null; |
399 | | | } |
400 | | | self::$entityManagers = null; |
401 | | | } |
402 | | | } |
403 | | | |
404 | | | /** |
405 | | | * Check if the request is allowed AJAX request |
406 | | | * |
407 | | | * @param string $sourceSiteURL the allowed site the request came from (HTTP_REFERER) |
408 | | | * @return boolean |
409 | | | */ |
410 | | | static function validAJAXRequest() |
411 | | | { |
412 | | | if ( |
413 | | | // make sure it's an AJAX request |
414 | | | isset($_SERVER['HTTP_X_REQUESTED_WITH']) && |
415 | | | // make sure it has a referrer |
416 | | | isset($_SERVER['HTTP_REFERER']) && |
417 | | | // make sure it comes from your website |
418 | | | strpos($_SERVER['HTTP_REFERER'], "http://" . $_SERVER['HTTP_HOST']) === 0 |
419 | | | ) |
420 | | | return true; |
421 | | | else |
422 | | | return false; |
423 | | | } |
424 | | | |
425 | | | } |