@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * Class for Benchmark |
29 | 29 | */ |
30 | - class Benchmark{ |
|
30 | + class Benchmark { |
|
31 | 31 | /** |
32 | 32 | * The markers for excution time |
33 | 33 | * @var array |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | * This method is used to mark one point for benchmark (execution time and memory usage) |
45 | 45 | * @param string $name the marker name |
46 | 46 | */ |
47 | - public function mark($name){ |
|
47 | + public function mark($name) { |
|
48 | 48 | //Marker for execution time |
49 | 49 | $this->markersTime[$name] = microtime(true); |
50 | 50 | //Marker for memory usage |
@@ -58,12 +58,12 @@ discard block |
||
58 | 58 | * @param integer $decimalCount the number of decimal |
59 | 59 | * @return string the total execution time |
60 | 60 | */ |
61 | - public function elapsedTime($startMarkerName = null, $endMarkerName = null, $decimalCount = 6){ |
|
62 | - if(! $startMarkerName || !isset($this->markersTime[$startMarkerName])){ |
|
61 | + public function elapsedTime($startMarkerName = null, $endMarkerName = null, $decimalCount = 6) { |
|
62 | + if (!$startMarkerName || !isset($this->markersTime[$startMarkerName])) { |
|
63 | 63 | return 0; |
64 | 64 | } |
65 | 65 | |
66 | - if(! isset($this->markersTime[$endMarkerName])){ |
|
66 | + if (!isset($this->markersTime[$endMarkerName])) { |
|
67 | 67 | $this->markersTime[$endMarkerName] = microtime(true); |
68 | 68 | } |
69 | 69 | return number_format($this->markersTime[$endMarkerName] - $this->markersTime[$startMarkerName], $decimalCount); |
@@ -76,12 +76,12 @@ discard block |
||
76 | 76 | * @param integer $decimalCount the number of decimal |
77 | 77 | * @return string the total memory usage |
78 | 78 | */ |
79 | - public function memoryUsage($startMarkerName = null, $endMarkerName = null, $decimalCount = 6){ |
|
80 | - if(! $startMarkerName || !isset($this->markersMemory[$startMarkerName])){ |
|
79 | + public function memoryUsage($startMarkerName = null, $endMarkerName = null, $decimalCount = 6) { |
|
80 | + if (!$startMarkerName || !isset($this->markersMemory[$startMarkerName])) { |
|
81 | 81 | return 0; |
82 | 82 | } |
83 | 83 | |
84 | - if(! isset($this->markersMemory[$endMarkerName])){ |
|
84 | + if (!isset($this->markersMemory[$endMarkerName])) { |
|
85 | 85 | $this->markersMemory[$endMarkerName] = microtime(true); |
86 | 86 | } |
87 | 87 | return number_format($this->markersMemory[$endMarkerName] - $this->markersMemory[$startMarkerName], $decimalCount); |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | */ |
87 | 87 | public function __construct() |
88 | 88 | { |
89 | - $this->logger =& class_loader('Log', 'classes'); |
|
89 | + $this->logger = & class_loader('Log', 'classes'); |
|
90 | 90 | $this->logger->setLogger('Library::Email'); |
91 | 91 | $this->reset(); |
92 | 92 | } |
@@ -148,10 +148,10 @@ discard block |
||
148 | 148 | public function setTos(array $emails) |
149 | 149 | { |
150 | 150 | foreach ($emails as $name => $email) { |
151 | - if(is_numeric($name)){ |
|
151 | + if (is_numeric($name)) { |
|
152 | 152 | $this->setTo($email); |
153 | 153 | } |
154 | - else{ |
|
154 | + else { |
|
155 | 155 | $this->setTo($email, $name); |
156 | 156 | } |
157 | 157 | } |
@@ -281,8 +281,8 @@ discard block |
||
281 | 281 | */ |
282 | 282 | public function addAttachment($path, $filename = null, $data = null) |
283 | 283 | { |
284 | - if(! file_exists($path)){ |
|
285 | - show_error('The file [' .$path. '] does not exists.'); |
|
284 | + if (!file_exists($path)) { |
|
285 | + show_error('The file [' . $path . '] does not exists.'); |
|
286 | 286 | } |
287 | 287 | $filename = empty($filename) ? basename($path) : $filename; |
288 | 288 | $filename = $this->encodeUtf8($this->filterOther((string) $filename)); |
@@ -304,13 +304,13 @@ discard block |
||
304 | 304 | */ |
305 | 305 | public function getAttachmentData($path) |
306 | 306 | { |
307 | - if(! file_exists($path)){ |
|
308 | - show_error('The file [' .$path. '] does not exists.'); |
|
307 | + if (!file_exists($path)) { |
|
308 | + show_error('The file [' . $path . '] does not exists.'); |
|
309 | 309 | } |
310 | 310 | $filesize = filesize($path); |
311 | 311 | $handle = fopen($path, "r"); |
312 | 312 | $attachment = null; |
313 | - if(is_resource($handle)){ |
|
313 | + if (is_resource($handle)) { |
|
314 | 314 | $attachment = fread($handle, $filesize); |
315 | 315 | fclose($handle); |
316 | 316 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class Log{ |
|
27 | + class Log { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The defined constante for Log level |
@@ -52,14 +52,14 @@ discard block |
||
52 | 52 | /** |
53 | 53 | * Create new Log instance |
54 | 54 | */ |
55 | - public function __construct(){ |
|
55 | + public function __construct() { |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
59 | 59 | * Set the logger to identify each message in the log |
60 | 60 | * @param string $logger the logger name |
61 | 61 | */ |
62 | - public function setLogger($logger){ |
|
62 | + public function setLogger($logger) { |
|
63 | 63 | $this->logger = $logger; |
64 | 64 | } |
65 | 65 | |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | * @see Log::writeLog for more detail |
69 | 69 | * @param string $message the log message to save |
70 | 70 | */ |
71 | - public function fatal($message){ |
|
71 | + public function fatal($message) { |
|
72 | 72 | $this->writeLog($message, self::FATAL); |
73 | 73 | } |
74 | 74 | |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | * @see Log::writeLog for more detail |
78 | 78 | * @param string $message the log message to save |
79 | 79 | */ |
80 | - public function error($message){ |
|
80 | + public function error($message) { |
|
81 | 81 | $this->writeLog($message, self::ERROR); |
82 | 82 | } |
83 | 83 | |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * @see Log::writeLog for more detail |
87 | 87 | * @param string $message the log message to save |
88 | 88 | */ |
89 | - public function warning($message){ |
|
89 | + public function warning($message) { |
|
90 | 90 | $this->writeLog($message, self::WARNING); |
91 | 91 | } |
92 | 92 | |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @see Log::writeLog for more detail |
96 | 96 | * @param string $message the log message to save |
97 | 97 | */ |
98 | - public function info($message){ |
|
98 | + public function info($message) { |
|
99 | 99 | $this->writeLog($message, self::INFO); |
100 | 100 | } |
101 | 101 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | * @see Log::writeLog for more detail |
105 | 105 | * @param string $message the log message to save |
106 | 106 | */ |
107 | - public function debug($message){ |
|
107 | + public function debug($message) { |
|
108 | 108 | $this->writeLog($message, self::DEBUG); |
109 | 109 | } |
110 | 110 | |
@@ -115,59 +115,59 @@ discard block |
||
115 | 115 | * @param integer|string $level the log level in integer or string format, if is string will convert into integer |
116 | 116 | * to allow check the log level threshold. |
117 | 117 | */ |
118 | - public function writeLog($message, $level = self::INFO){ |
|
118 | + public function writeLog($message, $level = self::INFO) { |
|
119 | 119 | $configLogLevel = get_config('log_level'); |
120 | - if(! $configLogLevel){ |
|
120 | + if (!$configLogLevel) { |
|
121 | 121 | //so means no need log just stop here |
122 | 122 | return; |
123 | 123 | } |
124 | 124 | //check config log level |
125 | - if(! self::isValidConfigLevel($configLogLevel)){ |
|
125 | + if (!self::isValidConfigLevel($configLogLevel)) { |
|
126 | 126 | //NOTE: here need put the show_error() "logging" to false to prevent loop |
127 | 127 | show_error('Invalid config log level [' . $configLogLevel . '], the value must be one of the following: ' . implode(', ', array_map('strtoupper', self::$validConfigLevel)), $title = 'Log Config Error', $logging = false); |
128 | 128 | } |
129 | 129 | |
130 | 130 | //check if config log_logger_name is set |
131 | - if($this->logger){ |
|
131 | + if ($this->logger) { |
|
132 | 132 | $configLoggerName = get_config('log_logger_name', ''); |
133 | - if($configLoggerName){ |
|
134 | - if (is_array($configLoggerName)){ |
|
133 | + if ($configLoggerName) { |
|
134 | + if (is_array($configLoggerName)) { |
|
135 | 135 | //for best comparaison put all string to lowercase |
136 | 136 | $configLoggerName = array_map('strtolower', $configLoggerName); |
137 | - if(! in_array(strtolower($this->logger), $configLoggerName)){ |
|
137 | + if (!in_array(strtolower($this->logger), $configLoggerName)) { |
|
138 | 138 | return; |
139 | 139 | } |
140 | 140 | } |
141 | - else if(strtolower($this->logger) !== strtolower($configLoggerName)){ |
|
141 | + else if (strtolower($this->logger) !== strtolower($configLoggerName)) { |
|
142 | 142 | return; |
143 | 143 | } |
144 | 144 | } |
145 | 145 | } |
146 | 146 | |
147 | 147 | //if $level is not an integer |
148 | - if(! is_numeric($level)){ |
|
148 | + if (!is_numeric($level)) { |
|
149 | 149 | $level = self::getLevelValue($level); |
150 | 150 | } |
151 | 151 | |
152 | 152 | //check if can logging regarding the log level config |
153 | 153 | $configLevel = self::getLevelValue($configLogLevel); |
154 | - if($configLevel > $level){ |
|
154 | + if ($configLevel > $level) { |
|
155 | 155 | //can't log |
156 | 156 | return; |
157 | 157 | } |
158 | 158 | |
159 | 159 | $logSavePath = get_config('log_save_path'); |
160 | - if(! $logSavePath){ |
|
160 | + if (!$logSavePath) { |
|
161 | 161 | $logSavePath = LOGS_PATH; |
162 | 162 | } |
163 | 163 | |
164 | - if(! is_dir($logSavePath) || !is_writable($logSavePath)){ |
|
164 | + if (!is_dir($logSavePath) || !is_writable($logSavePath)) { |
|
165 | 165 | //NOTE: here need put the show_error() "logging" to false to prevent loop |
166 | 166 | show_error('Error : the log dir does not exists or is not writable', $title = 'Log directory error', $logging = false); |
167 | 167 | } |
168 | 168 | |
169 | 169 | $path = $logSavePath . 'logs-' . date('Y-m-d') . '.log'; |
170 | - if(! file_exists($path)){ |
|
170 | + if (!file_exists($path)) { |
|
171 | 171 | touch($path); |
172 | 172 | } |
173 | 173 | //may be at this time helper user_agent not yet included |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | |
190 | 190 | $str = $logDate . ' [' . str_pad($levelName, 7 /*warning len*/) . '] ' . ' [' . str_pad($ip, 15) . '] ' . $this->logger . ' : ' . $message . ' ' . '[' . $fileInfo['file'] . '::' . $fileInfo['line'] . ']' . "\n"; |
191 | 191 | $fp = fopen($path, 'a+'); |
192 | - if(is_resource($fp)){ |
|
192 | + if (is_resource($fp)) { |
|
193 | 193 | flock($fp, LOCK_EX); // exclusive lock, will get released when the file is closed |
194 | 194 | fwrite($fp, $str); |
195 | 195 | fclose($fp); |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | * |
204 | 204 | * @return boolean true if the given log level is valid, false if not |
205 | 205 | */ |
206 | - private static function isValidConfigLevel($level){ |
|
206 | + private static function isValidConfigLevel($level) { |
|
207 | 207 | $level = strtolower($level); |
208 | 208 | return in_array($level, self::$validConfigLevel); |
209 | 209 | } |
@@ -213,27 +213,27 @@ discard block |
||
213 | 213 | * @param string $level the log level in string format |
214 | 214 | * @return int the log level in integer format using the predefinied constants |
215 | 215 | */ |
216 | - private static function getLevelValue($level){ |
|
216 | + private static function getLevelValue($level) { |
|
217 | 217 | $level = strtolower($level); |
218 | 218 | $value = self::NONE; |
219 | 219 | |
220 | 220 | //the default value is NONE, so means no need test for NONE |
221 | - if($level == 'fatal'){ |
|
221 | + if ($level == 'fatal') { |
|
222 | 222 | $value = self::FATAL; |
223 | 223 | } |
224 | - else if($level == 'error'){ |
|
224 | + else if ($level == 'error') { |
|
225 | 225 | $value = self::ERROR; |
226 | 226 | } |
227 | - else if($level == 'warning' || $level == 'warn'){ |
|
227 | + else if ($level == 'warning' || $level == 'warn') { |
|
228 | 228 | $value = self::WARNING; |
229 | 229 | } |
230 | - else if($level == 'info'){ |
|
230 | + else if ($level == 'info') { |
|
231 | 231 | $value = self::INFO; |
232 | 232 | } |
233 | - else if($level == 'debug'){ |
|
233 | + else if ($level == 'debug') { |
|
234 | 234 | $value = self::DEBUG; |
235 | 235 | } |
236 | - else if($level == 'all'){ |
|
236 | + else if ($level == 'all') { |
|
237 | 237 | $value = self::ALL; |
238 | 238 | } |
239 | 239 | return $value; |
@@ -244,23 +244,23 @@ discard block |
||
244 | 244 | * @param integer $level the log level in integer format |
245 | 245 | * @return string the log level in string format |
246 | 246 | */ |
247 | - private static function getLevelName($level){ |
|
247 | + private static function getLevelName($level) { |
|
248 | 248 | $value = ''; |
249 | 249 | |
250 | 250 | //the default value is NONE, so means no need test for NONE |
251 | - if($level == self::FATAL){ |
|
251 | + if ($level == self::FATAL) { |
|
252 | 252 | $value = 'FATAL'; |
253 | 253 | } |
254 | - else if($level == self::ERROR){ |
|
254 | + else if ($level == self::ERROR) { |
|
255 | 255 | $value = 'ERROR'; |
256 | 256 | } |
257 | - else if($level == self::WARNING){ |
|
257 | + else if ($level == self::WARNING) { |
|
258 | 258 | $value = 'WARNING'; |
259 | 259 | } |
260 | - else if($level == self::INFO){ |
|
260 | + else if ($level == self::INFO) { |
|
261 | 261 | $value = 'INFO'; |
262 | 262 | } |
263 | - else if($level == self::DEBUG){ |
|
263 | + else if ($level == self::DEBUG) { |
|
264 | 264 | $value = 'DEBUG'; |
265 | 265 | } |
266 | 266 | //no need for ALL |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * For application languages management |
29 | 29 | */ |
30 | - class Lang{ |
|
30 | + class Lang { |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * The supported available language for this application. |
@@ -67,8 +67,8 @@ discard block |
||
67 | 67 | /** |
68 | 68 | * Construct new Lang instance |
69 | 69 | */ |
70 | - public function __construct(){ |
|
71 | - $this->logger =& class_loader('Log', 'classes'); |
|
70 | + public function __construct() { |
|
71 | + $this->logger = & class_loader('Log', 'classes'); |
|
72 | 72 | $this->logger->setLogger('Library::Lang'); |
73 | 73 | |
74 | 74 | $this->default = get_config('default_language', 'en'); |
@@ -76,8 +76,8 @@ discard block |
||
76 | 76 | |
77 | 77 | //add the supported languages ('key', 'display name') |
78 | 78 | $languages = get_config('languages', null); |
79 | - if(! empty($languages)){ |
|
80 | - foreach($languages as $key => $displayName){ |
|
79 | + if (!empty($languages)) { |
|
80 | + foreach ($languages as $key => $displayName) { |
|
81 | 81 | $this->addLang($key, $displayName); |
82 | 82 | } |
83 | 83 | } |
@@ -85,15 +85,15 @@ discard block |
||
85 | 85 | |
86 | 86 | //if the language exists in cookie use it |
87 | 87 | $cfgKey = get_config('language_cookie_name'); |
88 | - $this->logger->debug('Getting current language from cookie [' .$cfgKey. ']'); |
|
88 | + $this->logger->debug('Getting current language from cookie [' . $cfgKey . ']'); |
|
89 | 89 | $objCookie = & class_loader('Cookie'); |
90 | 90 | $cookieLang = $objCookie->get($cfgKey); |
91 | - if($cookieLang && $this->isValid($cookieLang)){ |
|
91 | + if ($cookieLang && $this->isValid($cookieLang)) { |
|
92 | 92 | $this->current = $cookieLang; |
93 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is valid so we will set the language using the cookie value [' .$cookieLang. ']'); |
|
93 | + $this->logger->info('Language from cookie [' . $cfgKey . '] is valid so we will set the language using the cookie value [' . $cookieLang . ']'); |
|
94 | 94 | } |
95 | - else{ |
|
96 | - $this->logger->info('Language from cookie [' .$cfgKey. '] is not set, use the default value [' .$this->getDefault(). ']'); |
|
95 | + else { |
|
96 | + $this->logger->info('Language from cookie [' . $cfgKey . '] is not set, use the default value [' . $this->getDefault() . ']'); |
|
97 | 97 | $this->current = $this->getDefault(); |
98 | 98 | } |
99 | 99 | } |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | * |
104 | 104 | * @return array the language message list |
105 | 105 | */ |
106 | - public function getAll(){ |
|
106 | + public function getAll() { |
|
107 | 107 | return $this->languages; |
108 | 108 | } |
109 | 109 | |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | * @param string $key the language key to identify |
114 | 114 | * @param string $value the language message value |
115 | 115 | */ |
116 | - public function set($key, $value){ |
|
116 | + public function set($key, $value) { |
|
117 | 117 | $this->languages[$key] = $value; |
118 | 118 | } |
119 | 119 | |
@@ -125,11 +125,11 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @return string the language message value |
127 | 127 | */ |
128 | - public function get($key, $default = 'LANGUAGE_ERROR'){ |
|
129 | - if(isset($this->languages[$key])){ |
|
128 | + public function get($key, $default = 'LANGUAGE_ERROR') { |
|
129 | + if (isset($this->languages[$key])) { |
|
130 | 130 | return $this->languages[$key]; |
131 | 131 | } |
132 | - $this->logger->warning('Language key [' .$key. '] does not exist use the default value [' .$default. ']'); |
|
132 | + $this->logger->warning('Language key [' . $key . '] does not exist use the default value [' . $default . ']'); |
|
133 | 133 | return $default; |
134 | 134 | } |
135 | 135 | |
@@ -140,10 +140,10 @@ discard block |
||
140 | 140 | * |
141 | 141 | * @return boolean true if the language directory exists, false or not |
142 | 142 | */ |
143 | - public function isValid($language){ |
|
143 | + public function isValid($language) { |
|
144 | 144 | $searchDir = array(CORE_LANG_PATH, APP_LANG_PATH); |
145 | - foreach($searchDir as $dir){ |
|
146 | - if(file_exists($dir . $language) && is_dir($dir . $language)){ |
|
145 | + foreach ($searchDir as $dir) { |
|
146 | + if (file_exists($dir . $language) && is_dir($dir . $language)) { |
|
147 | 147 | return true; |
148 | 148 | } |
149 | 149 | } |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | * |
156 | 156 | * @return string the default language |
157 | 157 | */ |
158 | - public function getDefault(){ |
|
158 | + public function getDefault() { |
|
159 | 159 | return $this->default; |
160 | 160 | } |
161 | 161 | |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | * |
165 | 165 | * @return string the current language |
166 | 166 | */ |
167 | - public function getCurrent(){ |
|
167 | + public function getCurrent() { |
|
168 | 168 | return $this->current; |
169 | 169 | } |
170 | 170 | |
@@ -174,14 +174,14 @@ discard block |
||
174 | 174 | * @param string $name the short language name like "en", "fr". |
175 | 175 | * @param string $description the human readable description of this language |
176 | 176 | */ |
177 | - public function addLang($name, $description){ |
|
178 | - if(isset($this->availables[$name])){ |
|
177 | + public function addLang($name, $description) { |
|
178 | + if (isset($this->availables[$name])) { |
|
179 | 179 | return; //already added cost in performance |
180 | 180 | } |
181 | - if($this->isValid($name)){ |
|
181 | + if ($this->isValid($name)) { |
|
182 | 182 | $this->availables[$name] = $description; |
183 | 183 | } |
184 | - else{ |
|
184 | + else { |
|
185 | 185 | show_error('The language [' . $name . '] is not valid or does not exists.'); |
186 | 186 | } |
187 | 187 | } |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | * |
192 | 192 | * @return array the list of the application language |
193 | 193 | */ |
194 | - public function getSupported(){ |
|
194 | + public function getSupported() { |
|
195 | 195 | return $this->availables; |
196 | 196 | } |
197 | 197 | |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | * |
201 | 201 | * @param array $langs the languages array of the messages to be added |
202 | 202 | */ |
203 | - public function addLangMessages(array $langs){ |
|
203 | + public function addLangMessages(array $langs) { |
|
204 | 204 | foreach ($langs as $key => $value) { |
205 | 205 | $this->set($key, $value); |
206 | 206 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | * along with this program; if not, write to the Free Software |
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | - class Session{ |
|
26 | + class Session { |
|
27 | 27 | |
28 | 28 | /** |
29 | 29 | * The session flash key to use |
@@ -41,9 +41,9 @@ discard block |
||
41 | 41 | * Get the logger singleton instance |
42 | 42 | * @return Log the logger instance |
43 | 43 | */ |
44 | - private static function getLogger(){ |
|
45 | - if(self::$logger == null){ |
|
46 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
44 | + private static function getLogger() { |
|
45 | + if (self::$logger == null) { |
|
46 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
47 | 47 | self::$logger[0]->setLogger('Library::Session'); |
48 | 48 | } |
49 | 49 | return self::$logger[0]; |
@@ -55,14 +55,14 @@ discard block |
||
55 | 55 | * @param mixed $default the default value to use if can not find the session item in the list |
56 | 56 | * @return mixed the session value if exist or the default value |
57 | 57 | */ |
58 | - public static function get($item, $default = null){ |
|
58 | + public static function get($item, $default = null) { |
|
59 | 59 | $logger = self::getLogger(); |
60 | - $logger->debug('Getting session data for item [' .$item. '] ...'); |
|
61 | - if(array_key_exists($item, $_SESSION)){ |
|
60 | + $logger->debug('Getting session data for item [' . $item . '] ...'); |
|
61 | + if (array_key_exists($item, $_SESSION)) { |
|
62 | 62 | $logger->info('Found session data for item [' . $item . '] the vaue is : [' . stringfy_vars($_SESSION[$item]) . ']'); |
63 | 63 | return $_SESSION[$item]; |
64 | 64 | } |
65 | - $logger->warning('Cannot find session item [' . $item . '] using the default value ['. $default . ']'); |
|
65 | + $logger->warning('Cannot find session item [' . $item . '] using the default value [' . $default . ']'); |
|
66 | 66 | return $default; |
67 | 67 | } |
68 | 68 | |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * @param string $item the session item name to set |
72 | 72 | * @param mixed $value the session item value |
73 | 73 | */ |
74 | - public static function set($item, $value){ |
|
74 | + public static function set($item, $value) { |
|
75 | 75 | $logger = self::getLogger(); |
76 | 76 | $logger->debug('Setting session data for item [' . $item . '], value [' . stringfy_vars($value) . ']'); |
77 | 77 | $_SESSION[$item] = $value; |
@@ -83,16 +83,16 @@ discard block |
||
83 | 83 | * @param mixed $default the default value to use if can not find the session flash item in the list |
84 | 84 | * @return mixed the session flash value if exist or the default value |
85 | 85 | */ |
86 | - public static function getFlash($item, $default = null){ |
|
86 | + public static function getFlash($item, $default = null) { |
|
87 | 87 | $logger = self::getLogger(); |
88 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
88 | + $key = self::SESSION_FLASH_KEY . '_' . $item; |
|
89 | 89 | $return = array_key_exists($key, $_SESSION) ? |
90 | 90 | ($_SESSION[$key]) : $default; |
91 | - if(array_key_exists($key, $_SESSION)){ |
|
91 | + if (array_key_exists($key, $_SESSION)) { |
|
92 | 92 | unset($_SESSION[$key]); |
93 | 93 | } |
94 | - else{ |
|
95 | - $logger->warning('Cannot find session flash item ['. $key .'] using the default value ['. $default .']'); |
|
94 | + else { |
|
95 | + $logger->warning('Cannot find session flash item [' . $key . '] using the default value [' . $default . ']'); |
|
96 | 96 | } |
97 | 97 | return $return; |
98 | 98 | } |
@@ -102,8 +102,8 @@ discard block |
||
102 | 102 | * @param string $item the session flash item name |
103 | 103 | * @return boolean |
104 | 104 | */ |
105 | - public static function hasFlash($item){ |
|
106 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
105 | + public static function hasFlash($item) { |
|
106 | + $key = self::SESSION_FLASH_KEY . '_' . $item; |
|
107 | 107 | return array_key_exists($key, $_SESSION); |
108 | 108 | } |
109 | 109 | |
@@ -112,8 +112,8 @@ discard block |
||
112 | 112 | * @param string $item the session flash item name to set |
113 | 113 | * @param mixed $value the session flash item value |
114 | 114 | */ |
115 | - public static function setFlash($item, $value){ |
|
116 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
115 | + public static function setFlash($item, $value) { |
|
116 | + $key = self::SESSION_FLASH_KEY . '_' . $item; |
|
117 | 117 | $_SESSION[$key] = $value; |
118 | 118 | } |
119 | 119 | |
@@ -121,14 +121,14 @@ discard block |
||
121 | 121 | * Clear the session item in the list |
122 | 122 | * @param string $item the session item name to be deleted |
123 | 123 | */ |
124 | - public static function clear($item){ |
|
124 | + public static function clear($item) { |
|
125 | 125 | $logger = self::getLogger(); |
126 | - if(array_key_exists($item, $_SESSION)){ |
|
127 | - $logger->info('Deleting of session for item ['.$item.' ]'); |
|
126 | + if (array_key_exists($item, $_SESSION)) { |
|
127 | + $logger->info('Deleting of session for item [' . $item . ' ]'); |
|
128 | 128 | unset($_SESSION[$item]); |
129 | 129 | } |
130 | - else{ |
|
131 | - $logger->warning('Session item ['.$item.'] to be deleted does not exists'); |
|
130 | + else { |
|
131 | + $logger->warning('Session item [' . $item . '] to be deleted does not exists'); |
|
132 | 132 | } |
133 | 133 | } |
134 | 134 | |
@@ -136,15 +136,15 @@ discard block |
||
136 | 136 | * Clear the session flash item in the list |
137 | 137 | * @param string $item the session flash item name to be deleted |
138 | 138 | */ |
139 | - public static function clearFlash($item){ |
|
139 | + public static function clearFlash($item) { |
|
140 | 140 | $logger = self::getLogger(); |
141 | - $key = self::SESSION_FLASH_KEY.'_'.$item; |
|
142 | - if(array_key_exists($key, $_SESSION)){ |
|
143 | - $logger->info('Delete session flash for item ['.$item.']'); |
|
141 | + $key = self::SESSION_FLASH_KEY . '_' . $item; |
|
142 | + if (array_key_exists($key, $_SESSION)) { |
|
143 | + $logger->info('Delete session flash for item [' . $item . ']'); |
|
144 | 144 | unset($_SESSION[$item]); |
145 | 145 | } |
146 | - else{ |
|
147 | - $logger->warning('Dession flash item ['.$item.'] to be deleted does not exists'); |
|
146 | + else { |
|
147 | + $logger->warning('Dession flash item [' . $item . '] to be deleted does not exists'); |
|
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
@@ -153,14 +153,14 @@ discard block |
||
153 | 153 | * @param string $item the session item name |
154 | 154 | * @return boolean |
155 | 155 | */ |
156 | - public static function exists($item){ |
|
156 | + public static function exists($item) { |
|
157 | 157 | return array_key_exists($item, $_SESSION); |
158 | 158 | } |
159 | 159 | |
160 | 160 | /** |
161 | 161 | * Destroy all session data values |
162 | 162 | */ |
163 | - public static function clearAll(){ |
|
163 | + public static function clearAll() { |
|
164 | 164 | session_unset(); |
165 | 165 | session_destroy(); |
166 | 166 | } |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class Request{ |
|
27 | + class Request { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The value for the super global $_GET |
@@ -90,21 +90,21 @@ discard block |
||
90 | 90 | /** |
91 | 91 | * Construct new request instance |
92 | 92 | */ |
93 | - public function __construct(){ |
|
93 | + public function __construct() { |
|
94 | 94 | $this->get = $_GET; |
95 | 95 | $this->post = $_POST; |
96 | 96 | $this->server = $_SERVER; |
97 | 97 | $this->query = $_REQUEST; |
98 | 98 | $this->cookie = $_COOKIE; |
99 | 99 | $this->file = $_FILES; |
100 | - $this->session =& class_loader('Session', 'classes'); |
|
100 | + $this->session = & class_loader('Session', 'classes'); |
|
101 | 101 | $this->method = $this->server('REQUEST_METHOD'); |
102 | 102 | $this->requestUri = $this->server('REQUEST_URI'); |
103 | 103 | $this->header = array(); |
104 | - if(function_exists('apache_request_headers')){ |
|
104 | + if (function_exists('apache_request_headers')) { |
|
105 | 105 | $this->header = apache_request_headers(); |
106 | 106 | } |
107 | - else if(function_exists('getallheaders')){ |
|
107 | + else if (function_exists('getallheaders')) { |
|
108 | 108 | $this->header = getallheaders(); |
109 | 109 | } |
110 | 110 | } |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | * Get the request method |
114 | 114 | * @return string |
115 | 115 | */ |
116 | - public function method(){ |
|
116 | + public function method() { |
|
117 | 117 | return $this->method; |
118 | 118 | } |
119 | 119 | |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * Get the request URI |
122 | 122 | * @return string |
123 | 123 | */ |
124 | - public function requestUri(){ |
|
124 | + public function requestUri() { |
|
125 | 125 | return $this->requestUri; |
126 | 126 | } |
127 | 127 | |
@@ -131,13 +131,13 @@ discard block |
||
131 | 131 | * @param boolean $xss if need apply some XSS attack rule on the value |
132 | 132 | * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
133 | 133 | */ |
134 | - public function query($key = null, $xss = true){ |
|
135 | - if(empty($key)){ |
|
134 | + public function query($key = null, $xss = true) { |
|
135 | + if (empty($key)) { |
|
136 | 136 | //return all |
137 | 137 | return $xss ? clean_input($this->query) : $this->query; |
138 | 138 | } |
139 | 139 | $query = array_key_exists($key, $this->query) ? $this->query[$key] : null; |
140 | - if($xss){ |
|
140 | + if ($xss) { |
|
141 | 141 | $query = clean_input($query); |
142 | 142 | } |
143 | 143 | return $query; |
@@ -149,13 +149,13 @@ discard block |
||
149 | 149 | * @param boolean $xss if need apply some XSS attack rule on the value |
150 | 150 | * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
151 | 151 | */ |
152 | - public function get($key = null, $xss = true){ |
|
153 | - if(empty($key)){ |
|
152 | + public function get($key = null, $xss = true) { |
|
153 | + if (empty($key)) { |
|
154 | 154 | //return all |
155 | 155 | return $xss ? clean_input($this->get) : $this->get; |
156 | 156 | } |
157 | 157 | $get = array_key_exists($key, $this->get) ? $this->get[$key] : null; |
158 | - if($xss){ |
|
158 | + if ($xss) { |
|
159 | 159 | $get = clean_input($get); |
160 | 160 | } |
161 | 161 | return $get; |
@@ -167,13 +167,13 @@ discard block |
||
167 | 167 | * @param boolean $xss if need apply some XSS attack rule on the value |
168 | 168 | * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
169 | 169 | */ |
170 | - public function post($key = null, $xss = true){ |
|
171 | - if(empty($key)){ |
|
170 | + public function post($key = null, $xss = true) { |
|
171 | + if (empty($key)) { |
|
172 | 172 | //return all |
173 | 173 | return $xss ? clean_input($this->post) : $this->post; |
174 | 174 | } |
175 | 175 | $post = array_key_exists($key, $this->post) ? $this->post[$key] : null; |
176 | - if($xss){ |
|
176 | + if ($xss) { |
|
177 | 177 | $post = clean_input($post); |
178 | 178 | } |
179 | 179 | return $post; |
@@ -185,13 +185,13 @@ discard block |
||
185 | 185 | * @param boolean $xss if need apply some XSS attack rule on the value |
186 | 186 | * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
187 | 187 | */ |
188 | - public function server($key = null, $xss = true){ |
|
189 | - if(empty($key)){ |
|
188 | + public function server($key = null, $xss = true) { |
|
189 | + if (empty($key)) { |
|
190 | 190 | //return all |
191 | 191 | return $xss ? clean_input($this->server) : $this->server; |
192 | 192 | } |
193 | 193 | $server = array_key_exists($key, $this->server) ? $this->server[$key] : null; |
194 | - if($xss){ |
|
194 | + if ($xss) { |
|
195 | 195 | $server = clean_input($server); |
196 | 196 | } |
197 | 197 | return $server; |
@@ -203,13 +203,13 @@ discard block |
||
203 | 203 | * @param boolean $xss if need apply some XSS attack rule on the value |
204 | 204 | * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
205 | 205 | */ |
206 | - public function cookie($key = null, $xss = true){ |
|
207 | - if(empty($key)){ |
|
206 | + public function cookie($key = null, $xss = true) { |
|
207 | + if (empty($key)) { |
|
208 | 208 | //return all |
209 | 209 | return $xss ? clean_input($this->cookie) : $this->cookie; |
210 | 210 | } |
211 | 211 | $cookie = array_key_exists($key, $this->cookie) ? $this->cookie[$key] : null; |
212 | - if($xss){ |
|
212 | + if ($xss) { |
|
213 | 213 | $cookie = clean_input($cookie); |
214 | 214 | } |
215 | 215 | return $cookie; |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | * @param string $key the item key to be fetched |
221 | 221 | * @return array|mixed the item value if the key exists or all array if the key does not exists or is empty |
222 | 222 | */ |
223 | - public function file($key){ |
|
223 | + public function file($key) { |
|
224 | 224 | $file = array_key_exists($key, $this->file) ? $this->file[$key] : null; |
225 | 225 | return $file; |
226 | 226 | } |
@@ -231,9 +231,9 @@ discard block |
||
231 | 231 | * @param boolean $xss if need apply some XSS attack rule on the value |
232 | 232 | * @return array|mixed the item value if the key exists or null if the key does not exists |
233 | 233 | */ |
234 | - public function session($key, $xss = true){ |
|
234 | + public function session($key, $xss = true) { |
|
235 | 235 | $session = $this->session->get($key); |
236 | - if($xss){ |
|
236 | + if ($xss) { |
|
237 | 237 | $session = clean_input($session); |
238 | 238 | } |
239 | 239 | return $session; |
@@ -245,9 +245,9 @@ discard block |
||
245 | 245 | * @param boolean $xss if need apply some XSS attack rule on the value |
246 | 246 | * @return mixed the item value if the key exists or null if the key does not exists |
247 | 247 | */ |
248 | - public function header($key, $xss = true){ |
|
248 | + public function header($key, $xss = true) { |
|
249 | 249 | $header = array_key_exists($key, $this->header) ? $this->header[$key] : null; |
250 | - if($xss){ |
|
250 | + if ($xss) { |
|
251 | 251 | $header = clean_input($header); |
252 | 252 | } |
253 | 253 | return $header; |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | 25 | */ |
26 | 26 | |
27 | - class Security{ |
|
27 | + class Security { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The logger instance |
@@ -36,9 +36,9 @@ discard block |
||
36 | 36 | * Get the logger singleton instance |
37 | 37 | * @return Log the logger instance |
38 | 38 | */ |
39 | - private static function getLogger(){ |
|
40 | - if(self::$logger == null){ |
|
41 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
39 | + private static function getLogger() { |
|
40 | + if (self::$logger == null) { |
|
41 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
42 | 42 | self::$logger[0]->setLogger('Library::Security'); |
43 | 43 | } |
44 | 44 | return self::$logger[0]; |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * This method is used to generate the CSRF token |
50 | 50 | * @return string the generated CSRF token |
51 | 51 | */ |
52 | - public static function generateCSRF(){ |
|
52 | + public static function generateCSRF() { |
|
53 | 53 | $logger = self::getLogger(); |
54 | 54 | $logger->debug('Generation of CSRF ...'); |
55 | 55 | |
@@ -57,14 +57,14 @@ discard block |
||
57 | 57 | $expire = get_config('csrf_expire', 60); |
58 | 58 | $keyExpire = 'csrf_expire'; |
59 | 59 | $currentTime = time(); |
60 | - if(Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime){ |
|
60 | + if (Session::exists($key) && Session::exists($keyExpire) && Session::get($keyExpire) > $currentTime) { |
|
61 | 61 | $logger->info('The CSRF token not yet expire just return it'); |
62 | 62 | return Session::get($key); |
63 | 63 | } |
64 | - else{ |
|
64 | + else { |
|
65 | 65 | $newTime = $currentTime + $expire; |
66 | 66 | $token = sha1(uniqid()) . sha1(uniqid()); |
67 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. '], token [' .$token. ']'); |
|
67 | + $logger->info('The CSRF informations are listed below: key [' . $key . '], key expire [' . $keyExpire . '], expire time [' . $expire . '], token [' . $token . ']'); |
|
68 | 68 | Session::set($keyExpire, $newTime); |
69 | 69 | Session::set($key, $token); |
70 | 70 | return Session::get($key); |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | * This method is used to check the CSRF if is valid, not yet expire, etc. |
76 | 76 | * @return boolean true if valid, false if not valid |
77 | 77 | */ |
78 | - public static function validateCSRF(){ |
|
78 | + public static function validateCSRF() { |
|
79 | 79 | $logger = self::getLogger(); |
80 | 80 | $logger->debug('Validation of CSRF ...'); |
81 | 81 | |
@@ -83,23 +83,23 @@ discard block |
||
83 | 83 | $expire = get_config('csrf_expire', 60); |
84 | 84 | $keyExpire = 'csrf_expire'; |
85 | 85 | $currentTime = time(); |
86 | - $logger->info('The CSRF informations are listed below: key [' .$key. '], key expire [' .$keyExpire. '], expire time [' .$expire. ']'); |
|
87 | - if(! Session::exists($key) || Session::get($keyExpire) <= $currentTime){ |
|
86 | + $logger->info('The CSRF informations are listed below: key [' . $key . '], key expire [' . $keyExpire . '], expire time [' . $expire . ']'); |
|
87 | + if (!Session::exists($key) || Session::get($keyExpire) <= $currentTime) { |
|
88 | 88 | $logger->warning('The CSRF session data is not valide'); |
89 | 89 | return false; |
90 | 90 | } |
91 | - else{ |
|
91 | + else { |
|
92 | 92 | //perform form data |
93 | 93 | //need use request->query() for best retrieve |
94 | 94 | //super instance |
95 | 95 | $obj = & get_instance(); |
96 | 96 | $token = $obj->request->query($key); |
97 | - if(! $token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime){ |
|
98 | - $logger->warning('The CSRF data [' .$token. '] is not valide may be attacker do his job'); |
|
97 | + if (!$token || $token !== Session::get($key) || Session::get($keyExpire) <= $currentTime) { |
|
98 | + $logger->warning('The CSRF data [' . $token . '] is not valide may be attacker do his job'); |
|
99 | 99 | return false; |
100 | 100 | } |
101 | - else{ |
|
102 | - $logger->info('The CSRF data [' .$token. '] is valide the form data is safe continue'); |
|
101 | + else { |
|
102 | + $logger->info('The CSRF data [' . $token . '] is valide the form data is safe continue'); |
|
103 | 103 | //remove the token from session |
104 | 104 | Session::clear($key); |
105 | 105 | Session::clear($keyExpire); |
@@ -111,24 +111,24 @@ discard block |
||
111 | 111 | /** |
112 | 112 | * This method is used to check the whitelist IP address access |
113 | 113 | */ |
114 | - public static function checkWhiteListIpAccess(){ |
|
114 | + public static function checkWhiteListIpAccess() { |
|
115 | 115 | $logger = self::getLogger(); |
116 | 116 | $logger->debug('Validation of the IP address access ...'); |
117 | 117 | $logger->debug('Check if whitelist IP access is enabled in the configuration ...'); |
118 | 118 | $isEnable = get_config('white_list_ip_enable', false); |
119 | - if($isEnable){ |
|
119 | + if ($isEnable) { |
|
120 | 120 | $logger->info('Whitelist IP access is enabled in the configuration'); |
121 | 121 | $list = get_config('white_list_ip_addresses', array()); |
122 | - if(! empty($list)){ |
|
122 | + if (!empty($list)) { |
|
123 | 123 | //Can't use Loader::functions() at this time because teh "Loader" library is loader after the security prossessing |
124 | 124 | require_once CORE_FUNCTIONS_PATH . 'function_user_agent.php'; |
125 | 125 | $ip = get_ip(); |
126 | - if((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)){ |
|
126 | + if ((count($list) == 1 && $list[0] == '*') || in_array($ip, $list)) { |
|
127 | 127 | $logger->info('IP address ' . $ip . ' allowed using the wildcard "*" or the full IP'); |
128 | 128 | //wildcard to access all ip address |
129 | 129 | return; |
130 | 130 | } |
131 | - else{ |
|
131 | + else { |
|
132 | 132 | // go through all whitelisted ips |
133 | 133 | foreach ($list as $ipaddr) { |
134 | 134 | // find the wild card * in whitelisted ip (f.e. find position in "127.0.*" or "127*") |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | } |
155 | 155 | } |
156 | 156 | } |
157 | - else{ |
|
157 | + else { |
|
158 | 158 | $logger->info('Whitelist IP access is not enabled in the configuration, ignore checking'); |
159 | 159 | } |
160 | 160 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * This class represent the event detail to dispatch to correspond listener |
29 | 29 | */ |
30 | - class EventInfo{ |
|
30 | + class EventInfo { |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * The event name |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public $stop; |
56 | 56 | |
57 | - public function __construct($name, $payload = array(), $returnBack = false, $stop = false){ |
|
57 | + public function __construct($name, $payload = array(), $returnBack = false, $stop = false) { |
|
58 | 58 | $this->name = $name; |
59 | 59 | $this->payload = $payload; |
60 | 60 | $this->returnBack = $returnBack; |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * also to dispatch the event |
30 | 30 | */ |
31 | 31 | |
32 | - class EventDispatcher{ |
|
32 | + class EventDispatcher { |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * The list of the registered listeners |
@@ -44,8 +44,8 @@ discard block |
||
44 | 44 | */ |
45 | 45 | private $logger; |
46 | 46 | |
47 | - public function __construct(){ |
|
48 | - $this->logger =& class_loader('Log', 'classes'); |
|
47 | + public function __construct() { |
|
48 | + $this->logger = & class_loader('Log', 'classes'); |
|
49 | 49 | $this->logger->setLogger('Library::EventDispatcher'); |
50 | 50 | } |
51 | 51 | |
@@ -54,13 +54,13 @@ discard block |
||
54 | 54 | * @param string $eventName the name of the event to register for |
55 | 55 | * @param callable $listener the function or class method to receive the event information after dispatch |
56 | 56 | */ |
57 | - public function addListener($eventName, callable $listener){ |
|
58 | - $this->logger->debug('Adding new event listener for the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
59 | - if(! isset($this->listeners[$eventName])){ |
|
57 | + public function addListener($eventName, callable $listener) { |
|
58 | + $this->logger->debug('Adding new event listener for the event name [' . $eventName . '], listener [' . stringfy_vars($listener) . ']'); |
|
59 | + if (!isset($this->listeners[$eventName])) { |
|
60 | 60 | $this->logger->info('This event does not have the registered event listener before, adding new one'); |
61 | 61 | $this->listeners[$eventName] = array(); |
62 | 62 | } |
63 | - else{ |
|
63 | + else { |
|
64 | 64 | $this->logger->info('This event already have the registered listener, add this listener to the list'); |
65 | 65 | } |
66 | 66 | $this->listeners[$eventName][] = $listener; |
@@ -71,19 +71,19 @@ discard block |
||
71 | 71 | * @param string $eventName the event name |
72 | 72 | * @param callable $listener the listener callback |
73 | 73 | */ |
74 | - public function removeListener($eventName, callable $listener){ |
|
75 | - $this->logger->debug('Removing of the event listener, the event name [' .$eventName. '], listener [' .stringfy_vars($listener). ']'); |
|
76 | - if(isset($this->listeners[$eventName])){ |
|
74 | + public function removeListener($eventName, callable $listener) { |
|
75 | + $this->logger->debug('Removing of the event listener, the event name [' . $eventName . '], listener [' . stringfy_vars($listener) . ']'); |
|
76 | + if (isset($this->listeners[$eventName])) { |
|
77 | 77 | $this->logger->info('This event have the listeners, check if this listener exists'); |
78 | - if(false !== $index = array_search($listener, $this->listeners[$eventName], true)){ |
|
79 | - $this->logger->info('Found the listener at index [' .$index. '] remove it'); |
|
78 | + if (false !== $index = array_search($listener, $this->listeners[$eventName], true)) { |
|
79 | + $this->logger->info('Found the listener at index [' . $index . '] remove it'); |
|
80 | 80 | unset($this->listeners[$eventName][$index]); |
81 | 81 | } |
82 | - else{ |
|
82 | + else { |
|
83 | 83 | $this->logger->info('Cannot found this listener in the event listener list'); |
84 | 84 | } |
85 | 85 | } |
86 | - else{ |
|
86 | + else { |
|
87 | 87 | $this->logger->info('This event does not have this listener ignore remove'); |
88 | 88 | } |
89 | 89 | } |
@@ -93,13 +93,13 @@ discard block |
||
93 | 93 | * remove all listeners for this event |
94 | 94 | * @param string $eventName the event name |
95 | 95 | */ |
96 | - public function removeAllListener($eventName = null){ |
|
97 | - $this->logger->debug('Removing of all event listener, the event name [' .$eventName. ']'); |
|
98 | - if($eventName !== null && isset($this->listeners[$eventName])){ |
|
96 | + public function removeAllListener($eventName = null) { |
|
97 | + $this->logger->debug('Removing of all event listener, the event name [' . $eventName . ']'); |
|
98 | + if ($eventName !== null && isset($this->listeners[$eventName])) { |
|
99 | 99 | $this->logger->info('The event name is set of exist in the listener just remove all event listener for this event'); |
100 | 100 | unset($this->listeners[$eventName]); |
101 | 101 | } |
102 | - else{ |
|
102 | + else { |
|
103 | 103 | $this->logger->info('The event name is not set or does not exist in the listener, so remove all event listener'); |
104 | 104 | $this->listeners = array(); |
105 | 105 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * @param string $eventName the event name |
111 | 111 | * @return array the listeners for this event or empty array if this event does not contain any listener |
112 | 112 | */ |
113 | - public function getListeners($eventName){ |
|
113 | + public function getListeners($eventName) { |
|
114 | 114 | return isset($this->listeners[$eventName]) ? $this->listeners[$eventName] : array(); |
115 | 115 | } |
116 | 116 | |
@@ -119,21 +119,21 @@ discard block |
||
119 | 119 | * @param mixed|object $event the event information |
120 | 120 | * @return void|object if event need return, will return the final EventInfo object. |
121 | 121 | */ |
122 | - public function dispatch($event){ |
|
123 | - if(! $event || !$event instanceof EventInfo){ |
|
122 | + public function dispatch($event) { |
|
123 | + if (!$event || !$event instanceof EventInfo) { |
|
124 | 124 | $this->logger->info('The event is not set or is not an instance of "EventInfo" create the default "EventInfo" object to use instead of.'); |
125 | 125 | $event = new EventInfo((string) $event); |
126 | 126 | } |
127 | - $this->logger->debug('Dispatch to the event listener, the event [' .stringfy_vars($event). ']'); |
|
128 | - if(isset($event->stop) && $event->stop){ |
|
127 | + $this->logger->debug('Dispatch to the event listener, the event [' . stringfy_vars($event) . ']'); |
|
128 | + if (isset($event->stop) && $event->stop) { |
|
129 | 129 | $this->logger->info('This event need stopped, no need call any listener'); |
130 | 130 | return; |
131 | 131 | } |
132 | - if($event->returnBack){ |
|
132 | + if ($event->returnBack) { |
|
133 | 133 | $this->logger->info('This event need return back, return the result for future use'); |
134 | 134 | return $this->dispatchToListerners($event); |
135 | 135 | } |
136 | - else{ |
|
136 | + else { |
|
137 | 137 | $this->logger->info('This event no need return back the result, just dispatch it'); |
138 | 138 | $this->dispatchToListerners($event); |
139 | 139 | } |
@@ -144,38 +144,38 @@ discard block |
||
144 | 144 | * @param object EventInfo $event the event information |
145 | 145 | * @return void|object if event need return, will return the final EventInfo instance. |
146 | 146 | */ |
147 | - private function dispatchToListerners(EventInfo $event){ |
|
147 | + private function dispatchToListerners(EventInfo $event) { |
|
148 | 148 | $eBackup = $event; |
149 | 149 | $list = $this->getListeners($event->name); |
150 | - if(empty($list)){ |
|
151 | - $this->logger->info('No event listener is registered for the event [' .$event->name. '] skipping.'); |
|
152 | - if($event->returnBack){ |
|
150 | + if (empty($list)) { |
|
151 | + $this->logger->info('No event listener is registered for the event [' . $event->name . '] skipping.'); |
|
152 | + if ($event->returnBack) { |
|
153 | 153 | return $event; |
154 | 154 | } |
155 | 155 | return; |
156 | 156 | } |
157 | - else{ |
|
158 | - $this->logger->info('Found the registered event listener for the event [' .$event->name. '] the list are: ' . stringfy_vars($list)); |
|
157 | + else { |
|
158 | + $this->logger->info('Found the registered event listener for the event [' . $event->name . '] the list are: ' . stringfy_vars($list)); |
|
159 | 159 | } |
160 | - foreach($list as $listener){ |
|
161 | - if($eBackup->returnBack){ |
|
160 | + foreach ($list as $listener) { |
|
161 | + if ($eBackup->returnBack) { |
|
162 | 162 | $returnedEvent = call_user_func_array($listener, array($event)); |
163 | - if($returnedEvent instanceof EventInfo){ |
|
163 | + if ($returnedEvent instanceof EventInfo) { |
|
164 | 164 | $event = $returnedEvent; |
165 | 165 | } |
166 | - else{ |
|
167 | - show_error('This event [' .$event->name. '] need you return the event object after processing'); |
|
166 | + else { |
|
167 | + show_error('This event [' . $event->name . '] need you return the event object after processing'); |
|
168 | 168 | } |
169 | 169 | } |
170 | - else{ |
|
170 | + else { |
|
171 | 171 | call_user_func_array($listener, array($event)); |
172 | 172 | } |
173 | - if($event->stop){ |
|
173 | + if ($event->stop) { |
|
174 | 174 | break; |
175 | 175 | } |
176 | 176 | } |
177 | 177 | //only test for original event may be during the flow some listeners change this parameter |
178 | - if($eBackup->returnBack){ |
|
178 | + if ($eBackup->returnBack) { |
|
179 | 179 | return $event; |
180 | 180 | } |
181 | 181 | } |