Conditions | 12 |
Paths | 22 |
Total Lines | 103 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
7 | public function __bootstrap() |
||
8 | { |
||
9 | // Session name |
||
10 | $sessionName = config('app.id').'_session'; |
||
11 | session_name($sessionName); |
||
12 | |||
13 | // Session lifetime |
||
14 | $lifetime = config('session.lifetime'); |
||
15 | |||
16 | // Security garbage collector |
||
17 | ini_set('session.gc_maxlifetime', |
||
18 | ($lifetime == 0 ? 180 * 60 : $lifetime * 60)); // PHP session garbage collection timeout in minutes |
||
19 | ini_set('session.gc_divisor', 100); // Set divisor and probability for cronjob Ubuntu/Debian |
||
20 | // ini_set('session.gc_probability', 1); // @see http://www.php.net/manual/en/function.session-save-path.php#98106 |
||
21 | |||
22 | // Set session save path |
||
23 | if (config('session.savepath')) { |
||
24 | ini_set('session.save_path', str_replace('~', LOCAL_ROOT, config('session.savepath'))); |
||
25 | } |
||
26 | |||
27 | // Set sessions to use cookies |
||
28 | ini_set('session.use_cookies', 1); |
||
29 | ini_set('session.use_only_cookies', |
||
30 | 1); // @see http://www.php.net/manual/en/session.configuration.php#ini.session.use-only-cookies |
||
31 | |||
32 | // Session cookie parameter |
||
33 | $path = config('app.path'); |
||
34 | $domain = config('security.cookie.domain'); |
||
35 | $secure = config('security.cookie.secure'); |
||
36 | $httponly = config('security.cookie.httponly'); |
||
37 | |||
38 | // Set cookie lifetime |
||
39 | session_set_cookie_params($lifetime * 60, $path, $domain, $secure, $httponly); |
||
40 | session_cache_limiter('private_no_expire'); |
||
41 | |||
42 | // Start the session! |
||
43 | session_start(); |
||
44 | |||
45 | // Strengthen session security with REMOTE_ADDR and HTTP_USER_AGENT |
||
46 | // @see http://shiflett.org/articles/session-hijacking |
||
47 | |||
48 | // Removed REMOTE_ADDR, use HTTP_X_FORWARDED_FOR if available |
||
49 | $remoteIp = Ajde_Http_Request::getClientIP(); |
||
50 | |||
51 | // Ignore Google Chrome frame as it has a split personality |
||
52 | // @todo TODO: security issue!! |
||
53 | // @see http://www.chromium.org/developers/how-tos/chrome-frame-getting-started/understanding-chrome-frame-user-agent |
||
54 | if ( |
||
55 | isset($_SERVER['HTTP_USER_AGENT']) && |
||
56 | substr_count($_SERVER['HTTP_USER_AGENT'], 'chromeframe/') === 0 && |
||
57 | isset($_SESSION['client']) && |
||
58 | $_SESSION['client'] !== md5($remoteIp.$_SERVER['HTTP_USER_AGENT'].config('security.secret')) |
||
59 | ) { |
||
60 | |||
61 | // TODO: overhead to call session_regenerate_id? is it not required?? |
||
62 | //session_regenerate_id(); |
||
63 | |||
64 | // thoroughly destroy the current session |
||
65 | session_destroy(); |
||
66 | unset($_SESSION); |
||
67 | setcookie(session_name(), session_id(), time() - 3600, $path, $domain, $secure, $httponly); |
||
68 | |||
69 | // TODO: |
||
70 | $exception = new Ajde_Core_Exception_Security('Possible session hijacking detected. Bailing out.'); |
||
71 | if (config('app.debug') === true) { |
||
72 | throw $exception; |
||
73 | } else { |
||
74 | // don't redirect/log for resource items, as they should have no side effect |
||
75 | // this makes it possible for i.e. web crawlers/error pages to view resources |
||
76 | $request = Ajde_Http_Request::fromGlobal(); |
||
77 | $route = $request->initRoute(); |
||
78 | Ajde::app()->setRequest($request); |
||
|
|||
79 | if (!in_array($route->getFormat(), ['css', 'js'])) { |
||
80 | Ajde_Exception_Log::logException($exception); |
||
81 | Ajde_Cache::getInstance()->disable(); |
||
82 | // Just destroying the session should be enough |
||
83 | // Ajde_Http_Response::dieOnCode(Ajde_Http_Response::RESPONSE_TYPE_FORBIDDEN); |
||
84 | } |
||
85 | } |
||
86 | } else { |
||
87 | $_SESSION['client'] = md5($remoteIp.issetor($_SERVER['HTTP_USER_AGENT']).config('security.secret')); |
||
88 | |||
89 | if ($lifetime > 0) { |
||
90 | // Force send new cookie with updated lifetime (forcing keep-alive) |
||
91 | // @see http://www.php.net/manual/en/function.session-set-cookie-params.php#100672 |
||
92 | //session_regenerate_id(); |
||
93 | |||
94 | // Set cookie manually if session_start didn't just sent a cookie |
||
95 | // @see http://www.php.net/manual/en/function.session-set-cookie-params.php#100657 |
||
96 | if (isset($_COOKIE[$sessionName])) { |
||
97 | setcookie(session_name(), session_id(), time() + ($lifetime * 60), $path, $domain, $secure, |
||
98 | $httponly); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // remove cache headers invoked by session_start(); |
||
104 | if (version_compare(PHP_VERSION, '5.3.0') >= 0) { |
||
105 | header_remove('X-Powered-By'); |
||
106 | } |
||
107 | |||
108 | return true; |
||
109 | } |
||
110 | |||
180 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: