@@ -1,28 +1,28 @@ |
||
1 | 1 | <?php |
2 | 2 | defined('ROOT_PATH') || exit('Access denied'); |
3 | 3 | /** |
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | 26 | |
27 | 27 | class Module{ |
28 | 28 |
@@ -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 Module{ |
|
27 | + class Module { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * list of loaded module |
@@ -42,9 +42,9 @@ discard block |
||
42 | 42 | * The signleton of the logger |
43 | 43 | * @return Object the Log instance |
44 | 44 | */ |
45 | - private static function getLogger(){ |
|
46 | - if(self::$logger == null){ |
|
47 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
45 | + private static function getLogger() { |
|
46 | + if (self::$logger == null) { |
|
47 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
48 | 48 | self::$logger[0]->setLogger('Library::Module'); |
49 | 49 | } |
50 | 50 | return self::$logger[0]; |
@@ -53,25 +53,25 @@ discard block |
||
53 | 53 | /** |
54 | 54 | * Initialise the module list by scanning the directory MODULE_PATH |
55 | 55 | */ |
56 | - public function init(){ |
|
56 | + public function init() { |
|
57 | 57 | $logger = self::getLogger(); |
58 | 58 | $logger->debug('Check if the application contains the modules ...'); |
59 | 59 | $moduleDir = opendir(MODULE_PATH); |
60 | - while(($module = readdir($moduleDir)) !== false){ |
|
61 | - if($module != '.' && $module != '..' && preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){ |
|
60 | + while (($module = readdir($moduleDir)) !== false) { |
|
61 | + if ($module != '.' && $module != '..' && preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)) { |
|
62 | 62 | self::$list[] = $module; |
63 | 63 | } |
64 | - else{ |
|
65 | - $logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name'); |
|
64 | + else { |
|
65 | + $logger->info('Skipping [' . $module . '], may be this is not a directory or does not exists or is invalid name'); |
|
66 | 66 | } |
67 | 67 | } |
68 | 68 | closedir($moduleDir); |
69 | 69 | ksort(self::$list); |
70 | 70 | |
71 | - if(self::hasModule()){ |
|
71 | + if (self::hasModule()) { |
|
72 | 72 | $logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']'); |
73 | 73 | } |
74 | - else{ |
|
74 | + else { |
|
75 | 75 | $logger->info('The application contains no module skipping'); |
76 | 76 | } |
77 | 77 | } |
@@ -80,9 +80,9 @@ discard block |
||
80 | 80 | * Get the list of the custom autoload configuration from module if exists |
81 | 81 | * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values |
82 | 82 | */ |
83 | - public static function getModulesAutoloadConfig(){ |
|
83 | + public static function getModulesAutoloadConfig() { |
|
84 | 84 | $logger = self::getLogger(); |
85 | - if(! self::hasModule()){ |
|
85 | + if (!self::hasModule()) { |
|
86 | 86 | $logger->info('No module was loaded skipping.'); |
87 | 87 | return false; |
88 | 88 | } |
@@ -95,27 +95,27 @@ discard block |
||
95 | 95 | |
96 | 96 | foreach (self::$list as $module) { |
97 | 97 | $file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php'; |
98 | - if(file_exists($file)){ |
|
98 | + if (file_exists($file)) { |
|
99 | 99 | require_once $file; |
100 | - if(! empty($autoload) && is_array($autoload)){ |
|
100 | + if (!empty($autoload) && is_array($autoload)) { |
|
101 | 101 | //libraries autoload |
102 | - if(! empty($autoload['libraries']) && is_array($autoload['libraries'])){ |
|
102 | + if (!empty($autoload['libraries']) && is_array($autoload['libraries'])) { |
|
103 | 103 | $autoloads['libraries'] = array_merge($autoloads['libraries'], $autoload['libraries']); |
104 | 104 | } |
105 | 105 | //config autoload |
106 | - if(! empty($autoload['config']) && is_array($autoload['config'])){ |
|
106 | + if (!empty($autoload['config']) && is_array($autoload['config'])) { |
|
107 | 107 | $autoloads['config'] = array_merge($autoloads['config'], $autoload['config']); |
108 | 108 | } |
109 | 109 | //models autoload |
110 | - if(! empty($autoload['models']) && is_array($autoload['models'])){ |
|
110 | + if (!empty($autoload['models']) && is_array($autoload['models'])) { |
|
111 | 111 | $autoloads['models'] = array_merge($autoloads['models'], $autoload['models']); |
112 | 112 | } |
113 | 113 | //functions autoload |
114 | - if(! empty($autoload['functions']) && is_array($autoload['functions'])){ |
|
114 | + if (!empty($autoload['functions']) && is_array($autoload['functions'])) { |
|
115 | 115 | $autoloads['functions'] = array_merge($autoloads['functions'], $autoload['functions']); |
116 | 116 | } |
117 | 117 | //languages autoload |
118 | - if(! empty($autoload['languages']) && is_array($autoload['languages'])){ |
|
118 | + if (!empty($autoload['languages']) && is_array($autoload['languages'])) { |
|
119 | 119 | $autoloads['languages'] = array_merge($autoloads['languages'], $autoload['languages']); |
120 | 120 | } |
121 | 121 | unset($autoload); |
@@ -129,23 +129,23 @@ discard block |
||
129 | 129 | * Get the list of the custom routes configuration from module if exists |
130 | 130 | * @return array|boolean the routes list or false if no module contains the routes configuration |
131 | 131 | */ |
132 | - public static function getModulesRoutes(){ |
|
132 | + public static function getModulesRoutes() { |
|
133 | 133 | $logger = self::getLogger(); |
134 | - if(! self::hasModule()){ |
|
134 | + if (!self::hasModule()) { |
|
135 | 135 | $logger->info('No module was loaded skipping.'); |
136 | 136 | return false; |
137 | 137 | } |
138 | 138 | $routes = array(); |
139 | 139 | foreach (self::$list as $module) { |
140 | 140 | $file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php'; |
141 | - if(file_exists($file)){ |
|
141 | + if (file_exists($file)) { |
|
142 | 142 | require_once $file; |
143 | - if(! empty($route) && is_array($route)){ |
|
143 | + if (!empty($route) && is_array($route)) { |
|
144 | 144 | $routes = array_merge($routes, $route); |
145 | 145 | unset($route); |
146 | 146 | } |
147 | - else{ |
|
148 | - show_error('No routing configuration found in [' .$file. '] for module [' . $module . ']'); |
|
147 | + else { |
|
148 | + show_error('No routing configuration found in [' . $file . '] for module [' . $module . ']'); |
|
149 | 149 | } |
150 | 150 | } |
151 | 151 | } |
@@ -159,23 +159,23 @@ discard block |
||
159 | 159 | * @param string $module the module name |
160 | 160 | * @return boolean|string false or null if no module have this controller, path the full path of the controller |
161 | 161 | */ |
162 | - public static function findControllerFullPath($class, $module = null){ |
|
162 | + public static function findControllerFullPath($class, $module = null) { |
|
163 | 163 | $logger = self::getLogger(); |
164 | - if(! self::hasModule()){ |
|
164 | + if (!self::hasModule()) { |
|
165 | 165 | $logger->info('No module was loaded skiping.'); |
166 | 166 | return false; |
167 | 167 | } |
168 | 168 | $class = str_ireplace('.php', '', $class); |
169 | 169 | $class = ucfirst($class); |
170 | - $classFile = $class.'.php'; |
|
171 | - $logger->debug('Checking the controller [' . $class . '] in module [' .$module. '] ...'); |
|
170 | + $classFile = $class . '.php'; |
|
171 | + $logger->debug('Checking the controller [' . $class . '] in module [' . $module . '] ...'); |
|
172 | 172 | $filePath = MODULE_PATH . $module . DS . 'controllers' . DS . $classFile; |
173 | - if(file_exists($filePath)){ |
|
174 | - $logger->info('Found controller [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
173 | + if (file_exists($filePath)) { |
|
174 | + $logger->info('Found controller [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
175 | 175 | return $filePath; |
176 | 176 | } |
177 | - else{ |
|
178 | - $logger->info('Controller [' . $class . '] does not exist in the module [' .$module. ']'); |
|
177 | + else { |
|
178 | + $logger->info('Controller [' . $class . '] does not exist in the module [' . $module . ']'); |
|
179 | 179 | return false; |
180 | 180 | } |
181 | 181 | } |
@@ -186,23 +186,23 @@ discard block |
||
186 | 186 | * @param string $module the module name |
187 | 187 | * @return boolean|string false or null if no module have this model, return the full path of this model |
188 | 188 | */ |
189 | - public static function findModelFullPath($class, $module = null){ |
|
189 | + public static function findModelFullPath($class, $module = null) { |
|
190 | 190 | $logger = self::getLogger(); |
191 | - if(! self::hasModule()){ |
|
191 | + if (!self::hasModule()) { |
|
192 | 192 | $logger->info('No module was loaded skiping.'); |
193 | 193 | return false; |
194 | 194 | } |
195 | 195 | $class = str_ireplace('.php', '', $class); |
196 | 196 | $class = ucfirst($class); |
197 | - $classFile = $class.'.php'; |
|
198 | - $logger->debug('Checking model [' . $class . '] in module [' .$module. '] ...'); |
|
197 | + $classFile = $class . '.php'; |
|
198 | + $logger->debug('Checking model [' . $class . '] in module [' . $module . '] ...'); |
|
199 | 199 | $filePath = MODULE_PATH . $module . DS . 'models' . DS . $classFile; |
200 | - if(file_exists($filePath)){ |
|
201 | - $logger->info('Found model [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
200 | + if (file_exists($filePath)) { |
|
201 | + $logger->info('Found model [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
202 | 202 | return $filePath; |
203 | 203 | } |
204 | - else{ |
|
205 | - $logger->info('Model [' . $class . '] does not exist in the module [' .$module. ']'); |
|
204 | + else { |
|
205 | + $logger->info('Model [' . $class . '] does not exist in the module [' . $module . ']'); |
|
206 | 206 | return false; |
207 | 207 | } |
208 | 208 | } |
@@ -213,22 +213,22 @@ discard block |
||
213 | 213 | * @param string $module the module name |
214 | 214 | * @return boolean|string false or null if no module have this configuration, return the full path of this configuration |
215 | 215 | */ |
216 | - public static function findConfigFullPath($configuration, $module = null){ |
|
216 | + public static function findConfigFullPath($configuration, $module = null) { |
|
217 | 217 | $logger = self::getLogger(); |
218 | - if(! self::hasModule()){ |
|
218 | + if (!self::hasModule()) { |
|
219 | 219 | $logger->info('No module was loaded skiping.'); |
220 | 220 | return false; |
221 | 221 | } |
222 | 222 | $configuration = str_ireplace('.php', '', $configuration); |
223 | - $file = $configuration.'.php'; |
|
224 | - $logger->debug('Checking configuration [' . $configuration . '] in module [' .$module. '] ...'); |
|
223 | + $file = $configuration . '.php'; |
|
224 | + $logger->debug('Checking configuration [' . $configuration . '] in module [' . $module . '] ...'); |
|
225 | 225 | $filePath = MODULE_PATH . $module . DS . 'config' . DS . $file; |
226 | - if(file_exists($filePath)){ |
|
227 | - $logger->info('Found configuration [' . $configuration . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
226 | + if (file_exists($filePath)) { |
|
227 | + $logger->info('Found configuration [' . $configuration . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
228 | 228 | return $filePath; |
229 | 229 | } |
230 | - else{ |
|
231 | - $logger->info('Configuration [' . $configuration . '] does not exist in the module [' .$module. ']'); |
|
230 | + else { |
|
231 | + $logger->info('Configuration [' . $configuration . '] does not exist in the module [' . $module . ']'); |
|
232 | 232 | return false; |
233 | 233 | } |
234 | 234 | } |
@@ -239,23 +239,23 @@ discard block |
||
239 | 239 | * @param string $module the module name |
240 | 240 | * @return boolean|string false or null if no module have this helper, return the full path of this helper |
241 | 241 | */ |
242 | - public static function findFunctionFullPath($helper, $module = null){ |
|
242 | + public static function findFunctionFullPath($helper, $module = null) { |
|
243 | 243 | $logger = self::getLogger(); |
244 | - if(! self::hasModule()){ |
|
244 | + if (!self::hasModule()) { |
|
245 | 245 | $logger->info('No module was loaded skiping.'); |
246 | 246 | return false; |
247 | 247 | } |
248 | 248 | $helper = str_ireplace('.php', '', $helper); |
249 | 249 | $helper = str_ireplace('function_', '', $helper); |
250 | - $file = 'function_'.$helper.'.php'; |
|
251 | - $logger->debug('Checking helper [' . $helper . '] in module [' .$module. '] ...'); |
|
250 | + $file = 'function_' . $helper . '.php'; |
|
251 | + $logger->debug('Checking helper [' . $helper . '] in module [' . $module . '] ...'); |
|
252 | 252 | $filePath = MODULE_PATH . $module . DS . 'functions' . DS . $file; |
253 | - if(file_exists($filePath)){ |
|
254 | - $logger->info('Found helper [' . $helper . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
253 | + if (file_exists($filePath)) { |
|
254 | + $logger->info('Found helper [' . $helper . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
255 | 255 | return $filePath; |
256 | 256 | } |
257 | - else{ |
|
258 | - $logger->info('Helper [' . $helper . '] does not exist in the module [' .$module. ']'); |
|
257 | + else { |
|
258 | + $logger->info('Helper [' . $helper . '] does not exist in the module [' . $module . ']'); |
|
259 | 259 | return false; |
260 | 260 | } |
261 | 261 | } |
@@ -267,22 +267,22 @@ discard block |
||
267 | 267 | * @param string $module the module name |
268 | 268 | * @return boolean|string false or null if no module have this library, return the full path of this library |
269 | 269 | */ |
270 | - public static function findLibraryFullPath($class, $module = null){ |
|
270 | + public static function findLibraryFullPath($class, $module = null) { |
|
271 | 271 | $logger = self::getLogger(); |
272 | - if(! self::hasModule()){ |
|
272 | + if (!self::hasModule()) { |
|
273 | 273 | $logger->info('No module was loaded skiping.'); |
274 | 274 | return false; |
275 | 275 | } |
276 | 276 | $class = str_ireplace('.php', '', $class); |
277 | - $file = $class.'.php'; |
|
278 | - $logger->debug('Checking library [' . $class . '] in module [' .$module. '] ...'); |
|
277 | + $file = $class . '.php'; |
|
278 | + $logger->debug('Checking library [' . $class . '] in module [' . $module . '] ...'); |
|
279 | 279 | $filePath = MODULE_PATH . $module . DS . 'libraries' . DS . $file; |
280 | - if(file_exists($filePath)){ |
|
281 | - $logger->info('Found library [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
280 | + if (file_exists($filePath)) { |
|
281 | + $logger->info('Found library [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
282 | 282 | return $filePath; |
283 | 283 | } |
284 | - else{ |
|
285 | - $logger->info('Library [' . $class . '] does not exist in the module [' .$module. ']'); |
|
284 | + else { |
|
285 | + $logger->info('Library [' . $class . '] does not exist in the module [' . $module . ']'); |
|
286 | 286 | return false; |
287 | 287 | } |
288 | 288 | } |
@@ -294,9 +294,9 @@ discard block |
||
294 | 294 | * @param string $module the module name to check |
295 | 295 | * @return boolean|string false or null if no module have this view, path the full path of the view |
296 | 296 | */ |
297 | - public static function findViewFullPath($view, $module = null){ |
|
297 | + public static function findViewFullPath($view, $module = null) { |
|
298 | 298 | $logger = self::getLogger(); |
299 | - if(! self::hasModule()){ |
|
299 | + if (!self::hasModule()) { |
|
300 | 300 | $logger->info('No module was loaded skiping.'); |
301 | 301 | return false; |
302 | 302 | } |
@@ -304,14 +304,14 @@ discard block |
||
304 | 304 | $view = trim($view, '/\\'); |
305 | 305 | $view = str_ireplace('/', DS, $view); |
306 | 306 | $viewFile = $view . '.php'; |
307 | - $logger->debug('Checking view [' . $view . '] in module [' .$module. '] ...'); |
|
307 | + $logger->debug('Checking view [' . $view . '] in module [' . $module . '] ...'); |
|
308 | 308 | $filePath = MODULE_PATH . $module . DS . 'views' . DS . $viewFile; |
309 | - if(file_exists($filePath)){ |
|
310 | - $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
309 | + if (file_exists($filePath)) { |
|
310 | + $logger->info('Found view [' . $view . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
311 | 311 | return $filePath; |
312 | 312 | } |
313 | - else{ |
|
314 | - $logger->info('View [' . $view . '] does not exist in the module [' .$module. ']'); |
|
313 | + else { |
|
314 | + $logger->info('View [' . $view . '] does not exist in the module [' . $module . ']'); |
|
315 | 315 | return false; |
316 | 316 | } |
317 | 317 | } |
@@ -323,23 +323,23 @@ discard block |
||
323 | 323 | * @param string $appLang the application language like 'en', 'fr' |
324 | 324 | * @return boolean|string false or null if no module have this language, return the full path of this language |
325 | 325 | */ |
326 | - public static function findLanguageFullPath($language, $module = null, $appLang){ |
|
326 | + public static function findLanguageFullPath($language, $module = null, $appLang) { |
|
327 | 327 | $logger = self::getLogger(); |
328 | - if(! self::hasModule()){ |
|
328 | + if (!self::hasModule()) { |
|
329 | 329 | $logger->info('No module was loaded skiping.'); |
330 | 330 | return false; |
331 | 331 | } |
332 | 332 | $language = str_ireplace('.php', '', $language); |
333 | 333 | $language = str_ireplace('lang_', '', $language); |
334 | - $file = 'lang_'.$language.'.php'; |
|
335 | - $logger->debug('Checking language [' . $language . '] in module [' .$module. '] ...'); |
|
334 | + $file = 'lang_' . $language . '.php'; |
|
335 | + $logger->debug('Checking language [' . $language . '] in module [' . $module . '] ...'); |
|
336 | 336 | $filePath = MODULE_PATH . $module . DS . 'lang' . DS . $appLang . DS . $file; |
337 | - if(file_exists($filePath)){ |
|
338 | - $logger->info('Found language [' . $language . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
337 | + if (file_exists($filePath)) { |
|
338 | + $logger->info('Found language [' . $language . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
339 | 339 | return $filePath; |
340 | 340 | } |
341 | - else{ |
|
342 | - $logger->info('Language [' . $language . '] does not exist in the module [' .$module. ']'); |
|
341 | + else { |
|
342 | + $logger->info('Language [' . $language . '] does not exist in the module [' . $module . ']'); |
|
343 | 343 | return false; |
344 | 344 | } |
345 | 345 | } |
@@ -348,7 +348,7 @@ discard block |
||
348 | 348 | * Get the list of module loaded |
349 | 349 | * @return array the module list |
350 | 350 | */ |
351 | - public static function getModuleList(){ |
|
351 | + public static function getModuleList() { |
|
352 | 352 | return self::$list; |
353 | 353 | } |
354 | 354 | |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | * Check if the application has an module |
357 | 357 | * @return boolean |
358 | 358 | */ |
359 | - public static function hasModule(){ |
|
359 | + public static function hasModule() { |
|
360 | 360 | return !empty(self::$list); |
361 | 361 | } |
362 | 362 |
@@ -60,8 +60,7 @@ discard block |
||
60 | 60 | while(($module = readdir($moduleDir)) !== false){ |
61 | 61 | if($module != '.' && $module != '..' && preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){ |
62 | 62 | self::$list[] = $module; |
63 | - } |
|
64 | - else{ |
|
63 | + } else{ |
|
65 | 64 | $logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name'); |
66 | 65 | } |
67 | 66 | } |
@@ -70,8 +69,7 @@ discard block |
||
70 | 69 | |
71 | 70 | if(self::hasModule()){ |
72 | 71 | $logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']'); |
73 | - } |
|
74 | - else{ |
|
72 | + } else{ |
|
75 | 73 | $logger->info('The application contains no module skipping'); |
76 | 74 | } |
77 | 75 | } |
@@ -143,8 +141,7 @@ discard block |
||
143 | 141 | if(! empty($route) && is_array($route)){ |
144 | 142 | $routes = array_merge($routes, $route); |
145 | 143 | unset($route); |
146 | - } |
|
147 | - else{ |
|
144 | + } else{ |
|
148 | 145 | show_error('No routing configuration found in [' .$file. '] for module [' . $module . ']'); |
149 | 146 | } |
150 | 147 | } |
@@ -173,8 +170,7 @@ discard block |
||
173 | 170 | if(file_exists($filePath)){ |
174 | 171 | $logger->info('Found controller [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
175 | 172 | return $filePath; |
176 | - } |
|
177 | - else{ |
|
173 | + } else{ |
|
178 | 174 | $logger->info('Controller [' . $class . '] does not exist in the module [' .$module. ']'); |
179 | 175 | return false; |
180 | 176 | } |
@@ -200,8 +196,7 @@ discard block |
||
200 | 196 | if(file_exists($filePath)){ |
201 | 197 | $logger->info('Found model [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
202 | 198 | return $filePath; |
203 | - } |
|
204 | - else{ |
|
199 | + } else{ |
|
205 | 200 | $logger->info('Model [' . $class . '] does not exist in the module [' .$module. ']'); |
206 | 201 | return false; |
207 | 202 | } |
@@ -226,8 +221,7 @@ discard block |
||
226 | 221 | if(file_exists($filePath)){ |
227 | 222 | $logger->info('Found configuration [' . $configuration . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
228 | 223 | return $filePath; |
229 | - } |
|
230 | - else{ |
|
224 | + } else{ |
|
231 | 225 | $logger->info('Configuration [' . $configuration . '] does not exist in the module [' .$module. ']'); |
232 | 226 | return false; |
233 | 227 | } |
@@ -253,8 +247,7 @@ discard block |
||
253 | 247 | if(file_exists($filePath)){ |
254 | 248 | $logger->info('Found helper [' . $helper . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
255 | 249 | return $filePath; |
256 | - } |
|
257 | - else{ |
|
250 | + } else{ |
|
258 | 251 | $logger->info('Helper [' . $helper . '] does not exist in the module [' .$module. ']'); |
259 | 252 | return false; |
260 | 253 | } |
@@ -280,8 +273,7 @@ discard block |
||
280 | 273 | if(file_exists($filePath)){ |
281 | 274 | $logger->info('Found library [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
282 | 275 | return $filePath; |
283 | - } |
|
284 | - else{ |
|
276 | + } else{ |
|
285 | 277 | $logger->info('Library [' . $class . '] does not exist in the module [' .$module. ']'); |
286 | 278 | return false; |
287 | 279 | } |
@@ -309,8 +301,7 @@ discard block |
||
309 | 301 | if(file_exists($filePath)){ |
310 | 302 | $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
311 | 303 | return $filePath; |
312 | - } |
|
313 | - else{ |
|
304 | + } else{ |
|
314 | 305 | $logger->info('View [' . $view . '] does not exist in the module [' .$module. ']'); |
315 | 306 | return false; |
316 | 307 | } |
@@ -337,8 +328,7 @@ discard block |
||
337 | 328 | if(file_exists($filePath)){ |
338 | 329 | $logger->info('Found language [' . $language . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
339 | 330 | return $filePath; |
340 | - } |
|
341 | - else{ |
|
331 | + } else{ |
|
342 | 332 | $logger->info('Language [' . $language . '] does not exist in the module [' .$module. ']'); |
343 | 333 | return false; |
344 | 334 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
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 | 26 | |
27 | 27 | class Log{ |
28 | 28 |
@@ -137,8 +137,7 @@ discard block |
||
137 | 137 | if(! in_array(strtolower($this->logger), $configLoggerName)){ |
138 | 138 | return; |
139 | 139 | } |
140 | - } |
|
141 | - else if(strtolower($this->logger) !== strtolower($configLoggerName)){ |
|
140 | + } else if(strtolower($this->logger) !== strtolower($configLoggerName)){ |
|
142 | 141 | return; |
143 | 142 | } |
144 | 143 | } |
@@ -218,20 +217,15 @@ discard block |
||
218 | 217 | //the default value is NONE, so means no need test for NONE |
219 | 218 | if($level == 'fatal'){ |
220 | 219 | $value = self::FATAL; |
221 | - } |
|
222 | - else if($level == 'error'){ |
|
220 | + } else if($level == 'error'){ |
|
223 | 221 | $value = self::ERROR; |
224 | - } |
|
225 | - else if($level == 'warning' || $level == 'warn'){ |
|
222 | + } else if($level == 'warning' || $level == 'warn'){ |
|
226 | 223 | $value = self::WARNING; |
227 | - } |
|
228 | - else if($level == 'info'){ |
|
224 | + } else if($level == 'info'){ |
|
229 | 225 | $value = self::INFO; |
230 | - } |
|
231 | - else if($level == 'debug'){ |
|
226 | + } else if($level == 'debug'){ |
|
232 | 227 | $value = self::DEBUG; |
233 | - } |
|
234 | - else if($level == 'all'){ |
|
228 | + } else if($level == 'all'){ |
|
235 | 229 | $value = self::ALL; |
236 | 230 | } |
237 | 231 | return $value; |
@@ -248,17 +242,13 @@ discard block |
||
248 | 242 | //the default value is NONE, so means no need test for NONE |
249 | 243 | if($level == self::FATAL){ |
250 | 244 | $value = 'FATAL'; |
251 | - } |
|
252 | - else if($level == self::ERROR){ |
|
245 | + } else if($level == self::ERROR){ |
|
253 | 246 | $value = 'ERROR'; |
254 | - } |
|
255 | - else if($level == self::WARNING){ |
|
247 | + } else if($level == self::WARNING){ |
|
256 | 248 | $value = 'WARNING'; |
257 | - } |
|
258 | - else if($level == self::INFO){ |
|
249 | + } else if($level == self::INFO){ |
|
259 | 250 | $value = 'INFO'; |
260 | - } |
|
261 | - else if($level == self::DEBUG){ |
|
251 | + } else if($level == self::DEBUG){ |
|
262 | 252 | $value = 'DEBUG'; |
263 | 253 | } |
264 | 254 | //no need for ALL |
@@ -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 |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
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 | 26 | |
27 | 27 | class Controller{ |
28 | 28 |
@@ -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 Controller{ |
|
27 | + class Controller { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The name of the module if this controller belong to an module |
@@ -47,32 +47,32 @@ discard block |
||
47 | 47 | /** |
48 | 48 | * Class constructor |
49 | 49 | */ |
50 | - public function __construct(){ |
|
51 | - $this->logger =& class_loader('Log', 'classes'); |
|
50 | + public function __construct() { |
|
51 | + $this->logger = & class_loader('Log', 'classes'); |
|
52 | 52 | $this->logger->setLogger('MainController'); |
53 | 53 | self::$instance = & $this; |
54 | 54 | |
55 | 55 | $this->logger->debug('Adding the loaded classes to the super instance'); |
56 | - foreach (class_loaded() as $var => $class){ |
|
57 | - $this->$var =& class_loader($class); |
|
56 | + foreach (class_loaded() as $var => $class) { |
|
57 | + $this->$var = & class_loader($class); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | $this->logger->debug('Setting the cache handler instance'); |
61 | 61 | //set cache handler instance |
62 | - if(get_config('cache_enable', false)){ |
|
63 | - if(isset($this->{strtolower(get_config('cache_handler'))})){ |
|
62 | + if (get_config('cache_enable', false)) { |
|
63 | + if (isset($this->{strtolower(get_config('cache_handler'))})) { |
|
64 | 64 | $this->cache = $this->{strtolower(get_config('cache_handler'))}; |
65 | 65 | unset($this->{strtolower(get_config('cache_handler'))}); |
66 | 66 | } |
67 | 67 | } |
68 | 68 | $this->logger->debug('Loading the required classes into super instance'); |
69 | - $this->eventdispatcher =& class_loader('EventDispatcher', 'classes'); |
|
70 | - $this->loader =& class_loader('Loader', 'classes'); |
|
71 | - $this->lang =& class_loader('Lang', 'classes'); |
|
72 | - $this->request =& class_loader('Request', 'classes'); |
|
69 | + $this->eventdispatcher = & class_loader('EventDispatcher', 'classes'); |
|
70 | + $this->loader = & class_loader('Loader', 'classes'); |
|
71 | + $this->lang = & class_loader('Lang', 'classes'); |
|
72 | + $this->request = & class_loader('Request', 'classes'); |
|
73 | 73 | //dispatch the request instance created event |
74 | 74 | $this->eventdispatcher->dispatch('REQUEST_CREATED'); |
75 | - $this->response =& class_loader('Response', 'classes', 'classes'); |
|
75 | + $this->response = & class_loader('Response', 'classes', 'classes'); |
|
76 | 76 | |
77 | 77 | |
78 | 78 | //set session config |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | set_session_config(); |
81 | 81 | |
82 | 82 | //determine the current module |
83 | - if(isset($this->router) && $this->router->getModule()){ |
|
83 | + if (isset($this->router) && $this->router->getModule()) { |
|
84 | 84 | $this->moduleName = $this->router->getModule(); |
85 | 85 | } |
86 | 86 | //dispatch the loaded instance of super controller event |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
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 | 26 | |
27 | 27 | /** |
28 | 28 | * Class for Benchmark |
@@ -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); |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
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 | 26 | |
27 | 27 | /** |
28 | 28 | * PDF library to generate PDF document using the library DOMPDF |
@@ -45,8 +45,8 @@ discard block |
||
45 | 45 | * Create PDF library instance |
46 | 46 | */ |
47 | 47 | public function __construct(){ |
48 | - $this->logger =& class_loader('Log', 'classes'); |
|
49 | - $this->logger->setLogger('Library::PDF'); |
|
48 | + $this->logger =& class_loader('Log', 'classes'); |
|
49 | + $this->logger->setLogger('Library::PDF'); |
|
50 | 50 | |
51 | 51 | require_once VENDOR_PATH.'dompdf/dompdf_config.inc.php'; |
52 | 52 | $this->dompdf = new Dompdf(); |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
79 | - * Return the instance of Dompdf |
|
80 | - * |
|
81 | - * @return object the dompdf instance |
|
82 | - */ |
|
79 | + * Return the instance of Dompdf |
|
80 | + * |
|
81 | + * @return object the dompdf instance |
|
82 | + */ |
|
83 | 83 | public function getDompdf(){ |
84 | 84 | return $this->dompdf; |
85 | 85 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * PDF library to generate PDF document using the library DOMPDF |
29 | 29 | */ |
30 | - class PDF{ |
|
30 | + class PDF { |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * The dompdf instance |
@@ -44,11 +44,11 @@ discard block |
||
44 | 44 | /** |
45 | 45 | * Create PDF library instance |
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::PDF'); |
50 | 50 | |
51 | - require_once VENDOR_PATH.'dompdf/dompdf_config.inc.php'; |
|
51 | + require_once VENDOR_PATH . 'dompdf/dompdf_config.inc.php'; |
|
52 | 52 | $this->dompdf = new Dompdf(); |
53 | 53 | } |
54 | 54 | |
@@ -62,15 +62,15 @@ discard block |
||
62 | 62 | * @return string|void if $stream is true send PDF document to browser for download, else return the generated PDF |
63 | 63 | * content like to join in Email attachment of for other purpose use. |
64 | 64 | */ |
65 | - public function generate($html, $filename = 'output.pdf', $stream = true, $paper = 'A4', $orientation = 'portrait'){ |
|
66 | - $this->logger->info('Generating of PDF document: filename [' .$filename. '], stream [' .($stream ? 'TRUE':'FALSE'). '], paper [' .$paper. '], orientation [' .$orientation. ']'); |
|
65 | + public function generate($html, $filename = 'output.pdf', $stream = true, $paper = 'A4', $orientation = 'portrait') { |
|
66 | + $this->logger->info('Generating of PDF document: filename [' . $filename . '], stream [' . ($stream ? 'TRUE' : 'FALSE') . '], paper [' . $paper . '], orientation [' . $orientation . ']'); |
|
67 | 67 | $this->dompdf->load_html($html); |
68 | 68 | $this->dompdf->set_paper($paper, $orientation); |
69 | 69 | $this->dompdf->render(); |
70 | - if($stream){ |
|
70 | + if ($stream) { |
|
71 | 71 | $this->dompdf->stream($filename); |
72 | 72 | } |
73 | - else{ |
|
73 | + else { |
|
74 | 74 | return $this->dompdf->output(); |
75 | 75 | } |
76 | 76 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * |
81 | 81 | * @return object the dompdf instance |
82 | 82 | */ |
83 | - public function getDompdf(){ |
|
83 | + public function getDompdf() { |
|
84 | 84 | return $this->dompdf; |
85 | 85 | } |
86 | 86 |
@@ -69,8 +69,7 @@ |
||
69 | 69 | $this->dompdf->render(); |
70 | 70 | if($stream){ |
71 | 71 | $this->dompdf->stream($filename); |
72 | - } |
|
73 | - else{ |
|
72 | + } else{ |
|
74 | 73 | return $this->dompdf->output(); |
75 | 74 | } |
76 | 75 | } |
@@ -74,9 +74,9 @@ discard block |
||
74 | 74 | protected $_uid; |
75 | 75 | |
76 | 76 | /** |
77 | - * The logger instance |
|
78 | - * @var Log |
|
79 | - */ |
|
77 | + * The logger instance |
|
78 | + * @var Log |
|
79 | + */ |
|
80 | 80 | private $logger; |
81 | 81 | |
82 | 82 | /** |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | public function __construct() |
88 | 88 | { |
89 | 89 | $this->logger =& class_loader('Log', 'classes'); |
90 | - $this->logger->setLogger('Library::Email'); |
|
90 | + $this->logger->setLogger('Library::Email'); |
|
91 | 91 | $this->reset(); |
92 | 92 | } |
93 | 93 | |
@@ -112,13 +112,13 @@ discard block |
||
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
115 | - * setFrom |
|
116 | - * |
|
117 | - * @param string $email The email to send as from. |
|
118 | - * @param string $name The name to send as from. |
|
119 | - * |
|
120 | - * @return self |
|
121 | - */ |
|
115 | + * setFrom |
|
116 | + * |
|
117 | + * @param string $email The email to send as from. |
|
118 | + * @param string $name The name to send as from. |
|
119 | + * |
|
120 | + * @return self |
|
121 | + */ |
|
122 | 122 | public function setFrom($email, $name = null) |
123 | 123 | { |
124 | 124 | $this->addMailHeader('From', (string) $email, (string) $name); |
@@ -140,11 +140,11 @@ discard block |
||
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
143 | - * Set destination using array |
|
144 | - * @params array $emails the list of recipient. This is an associative array name => email |
|
145 | - * @example array('John Doe' => '[email protected]') |
|
146 | - * @return Object the current instance |
|
147 | - */ |
|
143 | + * Set destination using array |
|
144 | + * @params array $emails the list of recipient. This is an associative array name => email |
|
145 | + * @example array('John Doe' => '[email protected]') |
|
146 | + * @return Object the current instance |
|
147 | + */ |
|
148 | 148 | public function setTos(array $emails) |
149 | 149 | { |
150 | 150 | foreach ($emails as $name => $email) { |
@@ -150,8 +150,7 @@ |
||
150 | 150 | foreach ($emails as $name => $email) { |
151 | 151 | if(is_numeric($name)){ |
152 | 152 | $this->setTo($email); |
153 | - } |
|
154 | - else{ |
|
153 | + } else{ |
|
155 | 154 | $this->setTo($email, $name); |
156 | 155 | } |
157 | 156 | } |
@@ -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 | } |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
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 | 26 | |
27 | 27 | class Cookie{ |
28 | 28 |
@@ -93,8 +93,7 @@ |
||
93 | 93 | $logger->info('Delete cookie item ['.$item.']'); |
94 | 94 | unset($_COOKIE[$item]); |
95 | 95 | return true; |
96 | - } |
|
97 | - else{ |
|
96 | + } else{ |
|
98 | 97 | $logger->warning('Cookie item ['.$item.'] to be deleted does not exists'); |
99 | 98 | return false; |
100 | 99 | } |
@@ -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 Cookie{ |
|
27 | + class Cookie { |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * The logger instance |
@@ -36,9 +36,9 @@ discard block |
||
36 | 36 | * The signleton of the logger |
37 | 37 | * @return Object the Log 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::Cookie'); |
43 | 43 | } |
44 | 44 | return self::$logger[0]; |
@@ -50,9 +50,9 @@ discard block |
||
50 | 50 | * @param mixed $default the default value to use if can not find the cokkie item in the list |
51 | 51 | * @return mixed the cookie value if exist or the default value |
52 | 52 | */ |
53 | - public static function get($item, $default = null){ |
|
53 | + public static function get($item, $default = null) { |
|
54 | 54 | $logger = self::getLogger(); |
55 | - if(array_key_exists($item, $_COOKIE)){ |
|
55 | + if (array_key_exists($item, $_COOKIE)) { |
|
56 | 56 | return $_COOKIE[$item]; |
57 | 57 | } |
58 | 58 | $logger->warning('Cannot find cookie item [' . $item . '], using the default value [' . $default . ']'); |
@@ -69,14 +69,14 @@ discard block |
||
69 | 69 | * @param boolean $secure if this cookie will be available on secure connection or not |
70 | 70 | * @param boolean $httponly if this cookie will be available under HTTP protocol. |
71 | 71 | */ |
72 | - public static function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false){ |
|
73 | - if(headers_sent()){ |
|
72 | + public static function set($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false) { |
|
73 | + if (headers_sent()) { |
|
74 | 74 | show_error('There exists a cookie that we wanted to create that we couldn\'t |
75 | 75 | because headers was already sent. Make sure to do this first |
76 | 76 | before outputing anything.'); |
77 | 77 | } |
78 | 78 | $timestamp = $expire; |
79 | - if($expire){ |
|
79 | + if ($expire) { |
|
80 | 80 | $timestamp = time() + $expire; |
81 | 81 | } |
82 | 82 | setcookie($name, $value, $timestamp, $path, $domain, $secure, $httponly); |
@@ -87,15 +87,15 @@ discard block |
||
87 | 87 | * @param string $item the cookie item name to be cleared |
88 | 88 | * @return boolean true if the item exists and is deleted successfully otherwise will return false. |
89 | 89 | */ |
90 | - public static function delete($item){ |
|
90 | + public static function delete($item) { |
|
91 | 91 | $logger = self::getLogger(); |
92 | - if(array_key_exists($item, $_COOKIE)){ |
|
93 | - $logger->info('Delete cookie item ['.$item.']'); |
|
92 | + if (array_key_exists($item, $_COOKIE)) { |
|
93 | + $logger->info('Delete cookie item [' . $item . ']'); |
|
94 | 94 | unset($_COOKIE[$item]); |
95 | 95 | return true; |
96 | 96 | } |
97 | - else{ |
|
98 | - $logger->warning('Cookie item ['.$item.'] to be deleted does not exists'); |
|
97 | + else { |
|
98 | + $logger->warning('Cookie item [' . $item . '] to be deleted does not exists'); |
|
99 | 99 | return false; |
100 | 100 | } |
101 | 101 | } |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | * @param string $item the cookie item name |
106 | 106 | * @return boolean true if the cookie item is set, false or not |
107 | 107 | */ |
108 | - public static function exists($item){ |
|
108 | + public static function exists($item) { |
|
109 | 109 | return array_key_exists($item, $_COOKIE); |
110 | 110 | } |
111 | 111 |
@@ -22,7 +22,7 @@ |
||
22 | 22 | * You should have received a copy of the GNU General Public License |
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 | 26 | |
27 | 27 | /** |
28 | 28 | * @file Assets.php |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * @since 1.0.0 |
39 | 39 | * @filesource |
40 | 40 | */ |
41 | - class Assets{ |
|
41 | + class Assets { |
|
42 | 42 | |
43 | 43 | /** |
44 | 44 | * The logger instance |
@@ -50,10 +50,10 @@ discard block |
||
50 | 50 | * The signleton of the logger |
51 | 51 | * @return Object the Log instance |
52 | 52 | */ |
53 | - private static function getLogger(){ |
|
54 | - if(self::$logger == null){ |
|
53 | + private static function getLogger() { |
|
54 | + if (self::$logger == null) { |
|
55 | 55 | //can't assign reference to static variable |
56 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
56 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
57 | 57 | self::$logger[0]->setLogger('Library::Assets'); |
58 | 58 | } |
59 | 59 | return self::$logger[0]; |
@@ -72,13 +72,13 @@ discard block |
||
72 | 72 | * @param $asset the name of the assets file path with the extension. |
73 | 73 | * @return string|null the absolute path of the assets file, if it exists otherwise returns null if the file does not exist. |
74 | 74 | */ |
75 | - public static function path($asset){ |
|
75 | + public static function path($asset) { |
|
76 | 76 | $logger = self::getLogger(); |
77 | 77 | $path = ASSETS_PATH . $asset; |
78 | 78 | |
79 | 79 | $logger->debug('Including the Assets file [' . $path . ']'); |
80 | 80 | //Check if the file exists |
81 | - if(file_exists($path)){ |
|
81 | + if (file_exists($path)) { |
|
82 | 82 | $logger->info('Assets file [' . $path . '] included successfully'); |
83 | 83 | return Url::base_url($path); |
84 | 84 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | * @param $path the name of the css file without the extension. |
99 | 99 | * @return string|null the absolute path of the css file, if it exists otherwise returns null if the file does not exist. |
100 | 100 | */ |
101 | - public static function css($path){ |
|
101 | + public static function css($path) { |
|
102 | 102 | $logger = self::getLogger(); |
103 | 103 | /* |
104 | 104 | * if the file name contains the ".css" extension, replace it with |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | |
110 | 110 | $logger->debug('Including the Assets file [' . $path . '] for CSS'); |
111 | 111 | //Check if the file exists |
112 | - if(file_exists($path)){ |
|
112 | + if (file_exists($path)) { |
|
113 | 113 | $logger->info('Assets file [' . $path . '] for CSS included successfully'); |
114 | 114 | return Url::base_url($path); |
115 | 115 | } |
@@ -129,12 +129,12 @@ discard block |
||
129 | 129 | * @param $path the name of the javascript file without the extension. |
130 | 130 | * @return string|null the absolute path of the javascript file, if it exists otherwise returns null if the file does not exist. |
131 | 131 | */ |
132 | - public static function js($path){ |
|
132 | + public static function js($path) { |
|
133 | 133 | $logger = self::getLogger(); |
134 | 134 | $path = str_ireplace('.js', '', $path); |
135 | 135 | $path = ASSETS_PATH . 'js/' . $path . '.js'; |
136 | 136 | $logger->debug('Including the Assets file [' . $path . '] for javascript'); |
137 | - if(file_exists($path)){ |
|
137 | + if (file_exists($path)) { |
|
138 | 138 | $logger->info('Assets file [' . $path . '] for Javascript included successfully'); |
139 | 139 | return Url::base_url($path); |
140 | 140 | } |
@@ -154,11 +154,11 @@ discard block |
||
154 | 154 | * @param $path the name of the image file with the extension. |
155 | 155 | * @return string|null the absolute path of the image file, if it exists otherwise returns null if the file does not exist. |
156 | 156 | */ |
157 | - public static function img($path){ |
|
157 | + public static function img($path) { |
|
158 | 158 | $logger = self::getLogger(); |
159 | 159 | $path = ASSETS_PATH . 'images/' . $path; |
160 | 160 | $logger->debug('Including the Assets file [' . $path . '] for image'); |
161 | - if(file_exists($path)){ |
|
161 | + if (file_exists($path)) { |
|
162 | 162 | $logger->info('Assets file [' . $path . '] for image included successfully'); |
163 | 163 | return Url::base_url($path); |
164 | 164 | } |
@@ -487,8 +487,7 @@ discard block |
||
487 | 487 | } |
488 | 488 | chdir($destination_directory); |
489 | 489 | } |
490 | - } |
|
491 | - else{ |
|
490 | + } else{ |
|
492 | 491 | $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
493 | 492 | } |
494 | 493 | } |
@@ -640,8 +639,7 @@ discard block |
||
640 | 639 | // set original filename if not have a new name |
641 | 640 | if (empty($this->filename)) { |
642 | 641 | $this->filename = $this->file_array[$this->input]['name']; |
643 | - } |
|
644 | - else{ |
|
642 | + } else{ |
|
645 | 643 | // Replace %s for extension in filename |
646 | 644 | // Before: /[\w\d]*(.[\d\w]+)$/i |
647 | 645 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
@@ -1,797 +1,797 @@ |
||
1 | 1 | <?php |
2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
3 | - /** |
|
4 | - * TNH Framework |
|
5 | - * |
|
6 | - * A simple PHP framework using HMVC architecture |
|
7 | - * |
|
8 | - * This content is released under the GNU GPL License (GPL) |
|
9 | - * |
|
10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
11 | - * |
|
12 | - * This program is free software; you can redistribute it and/or |
|
13 | - * modify it under the terms of the GNU General Public License |
|
14 | - * as published by the Free Software Foundation; either version 3 |
|
15 | - * of the License, or (at your option) any later version. |
|
16 | - * |
|
17 | - * This program is distributed in the hope that it will be useful, |
|
18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | - * GNU General Public License for more details. |
|
21 | - * |
|
22 | - * You should have received a copy of the GNU General Public License |
|
23 | - * along with this program; if not, write to the Free Software |
|
24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | - */ |
|
26 | - |
|
27 | - |
|
28 | - |
|
29 | - /** |
|
30 | - * Upload |
|
31 | - * |
|
32 | - * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
33 | - * |
|
34 | - * @author Olaf Erlandsen <[email protected]> |
|
35 | - * @author http://www.webdevfreelance.com/ |
|
36 | - * |
|
37 | - * @package FileUpload |
|
38 | - * @version 1.5 |
|
39 | - */ |
|
40 | - class Upload{ |
|
41 | - |
|
42 | - /** |
|
43 | - * Version |
|
44 | - * |
|
45 | - * @since 1.5 |
|
46 | - * @version 1.0 |
|
47 | - */ |
|
48 | - const VERSION = '1.5'; |
|
49 | - |
|
50 | - /** |
|
51 | - * Upload function name |
|
52 | - * Remember: |
|
53 | - * Default function: move_uploaded_file |
|
54 | - * Native options: |
|
55 | - * - move_uploaded_file (Default and best option) |
|
56 | - * - copy |
|
57 | - * |
|
58 | - * @since 1.0 |
|
59 | - * @version 1.0 |
|
60 | - * @var string |
|
61 | - */ |
|
62 | - private $upload_function = 'move_uploaded_file'; |
|
63 | - |
|
64 | - /** |
|
65 | - * Array with the information obtained from the |
|
66 | - * variable $_FILES or $HTTP_POST_FILES. |
|
67 | - * |
|
68 | - * @since 1.0 |
|
69 | - * @version 1.0 |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - private $file_array = array(); |
|
73 | - |
|
74 | - /** |
|
75 | - * If the file you are trying to upload already exists it will |
|
76 | - * be overwritten if you set the variable to true. |
|
77 | - * |
|
78 | - * @since 1.0 |
|
79 | - * @version 1.0 |
|
80 | - * @var boolean |
|
81 | - */ |
|
82 | - private $overwrite_file = false; |
|
83 | - |
|
84 | - /** |
|
85 | - * Input element |
|
86 | - * Example: |
|
87 | - * <input type="file" name="file" /> |
|
88 | - * Result: |
|
89 | - * FileUpload::$input = file |
|
90 | - * |
|
91 | - * @since 1.0 |
|
92 | - * @version 1.0 |
|
93 | - * @var string |
|
94 | - */ |
|
95 | - private $input; |
|
96 | - |
|
97 | - /** |
|
98 | - * Path output |
|
99 | - * |
|
100 | - * @since 1.0 |
|
101 | - * @version 1.0 |
|
102 | - * @var string |
|
103 | - */ |
|
104 | - private $destination_directory; |
|
105 | - |
|
106 | - /** |
|
107 | - * Output filename |
|
108 | - * |
|
109 | - * @since 1.0 |
|
110 | - * @version 1.0 |
|
111 | - * @var string |
|
112 | - */ |
|
113 | - private $filename; |
|
114 | - |
|
115 | - /** |
|
116 | - * Max file size |
|
117 | - * |
|
118 | - * @since 1.0 |
|
119 | - * @version 1.0 |
|
120 | - * @var float |
|
121 | - */ |
|
122 | - private $max_file_size= 0.0; |
|
123 | - |
|
124 | - /** |
|
125 | - * List of allowed mime types |
|
126 | - * |
|
127 | - * @since 1.0 |
|
128 | - * @version 1.0 |
|
129 | - * @var array |
|
130 | - */ |
|
131 | - private $allowed_mime_types = array(); |
|
132 | - |
|
133 | - /** |
|
134 | - * Callbacks |
|
135 | - * |
|
136 | - * @since 1.0 |
|
137 | - * @version 1.0 |
|
138 | - * @var array |
|
139 | - */ |
|
140 | - private $callbacks = array('before' => null, 'after' => null); |
|
141 | - |
|
142 | - /** |
|
143 | - * File object |
|
144 | - * |
|
145 | - * @since 1.0 |
|
146 | - * @version 1.0 |
|
147 | - * @var object |
|
148 | - */ |
|
149 | - private $file; |
|
150 | - |
|
151 | - /** |
|
152 | - * Helping mime types |
|
153 | - * |
|
154 | - * @since 1.0 |
|
155 | - * @version 1.0 |
|
156 | - * @var array |
|
157 | - */ |
|
158 | - private $mime_helping = array( |
|
159 | - 'text' => array('text/plain',), |
|
160 | - 'image' => array( |
|
161 | - 'image/jpeg', |
|
162 | - 'image/jpg', |
|
163 | - 'image/pjpeg', |
|
164 | - 'image/png', |
|
165 | - 'image/gif', |
|
166 | - ), |
|
167 | - 'document' => array( |
|
168 | - 'application/pdf', |
|
169 | - 'application/msword', |
|
170 | - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
171 | - 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
172 | - 'application/vnd.ms-powerpoint', |
|
173 | - 'application/vnd.ms-excel', |
|
174 | - 'application/vnd.oasis.opendocument.spreadsheet', |
|
175 | - 'application/vnd.oasis.opendocument.presentation', |
|
176 | - ), |
|
177 | - 'video' => array( |
|
178 | - 'video/3gpp', |
|
179 | - 'video/3gpp', |
|
180 | - 'video/x-msvideo', |
|
181 | - 'video/avi', |
|
182 | - 'video/mpeg4', |
|
183 | - 'video/mp4', |
|
184 | - 'video/mpeg', |
|
185 | - 'video/mpg', |
|
186 | - 'video/quicktime', |
|
187 | - 'video/x-sgi-movie', |
|
188 | - 'video/x-ms-wmv', |
|
189 | - 'video/x-flv', |
|
190 | - ), |
|
191 | - ); |
|
192 | - |
|
193 | - /** |
|
194 | - * The upload error message |
|
195 | - * @var array |
|
196 | - */ |
|
197 | - public $error_messages = array(); |
|
198 | - |
|
199 | - /** |
|
200 | - * The upload error message |
|
201 | - * @var string |
|
202 | - */ |
|
203 | - protected $error = null; |
|
204 | - |
|
205 | - /** |
|
206 | - * The logger instance |
|
207 | - * @var Log |
|
208 | - */ |
|
209 | - private $logger; |
|
210 | - |
|
211 | - |
|
212 | - /** |
|
213 | - * Construct |
|
214 | - * |
|
215 | - * @since 0.1 |
|
216 | - * @version 1.0.1 |
|
217 | - * @return object |
|
218 | - * @method object __construct |
|
219 | - */ |
|
220 | - public function __construct(){ |
|
221 | - $this->logger =& class_loader('Log', 'classes'); |
|
222 | - $this->logger->setLogger('Library::Upload'); |
|
223 | - |
|
224 | - Loader::lang('file_upload'); |
|
225 | - $obj =& get_instance(); |
|
226 | - |
|
227 | - $this->error_messages = array( |
|
228 | - 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
229 | - 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
230 | - 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
231 | - 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
232 | - 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
233 | - 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
234 | - 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
235 | - 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
236 | - 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
237 | - 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
238 | - 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
239 | - ); |
|
240 | - |
|
241 | - $this->file = array( |
|
242 | - 'status' => false, // True: success upload |
|
243 | - 'mime' => '', // Empty string |
|
244 | - 'filename' => '', // Empty string |
|
245 | - 'original' => '', // Empty string |
|
246 | - 'size' => 0, // 0 Bytes |
|
247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
248 | - 'destination' => './', // Default: ./ |
|
249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | - 'error' => null, // File error |
|
251 | - ); |
|
252 | - |
|
253 | - // Change dir to current dir |
|
254 | - $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
255 | - |
|
256 | - // Set file array |
|
257 | - if (isset($_FILES) && is_array($_FILES)) { |
|
258 | - $this->file_array = $_FILES; |
|
259 | - } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
|
260 | - $this->file_array = $HTTP_POST_FILES; |
|
261 | - } |
|
262 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
263 | - } |
|
264 | - /** |
|
265 | - * Set input. |
|
266 | - * If you have $_FILES["file"], you must use the key "file" |
|
267 | - * Example: |
|
268 | - * $object->setInput("file"); |
|
269 | - * |
|
270 | - * @since 1.0 |
|
271 | - * @version 1.0 |
|
272 | - * @param string $input |
|
273 | - * @return object |
|
274 | - * @method boolean setInput |
|
275 | - */ |
|
276 | - public function setInput($input) |
|
277 | - { |
|
278 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
279 | - $this->input = $input; |
|
280 | - } |
|
281 | - return $this; |
|
282 | - } |
|
283 | - /** |
|
284 | - * Set new filename |
|
285 | - * Example: |
|
286 | - * FileUpload::setFilename("new file.txt") |
|
287 | - * Remember: |
|
288 | - * Use %s to retrive file extension |
|
289 | - * |
|
290 | - * @since 1.0 |
|
291 | - * @version 1.0 |
|
292 | - * @param string $filename |
|
293 | - * @return object |
|
294 | - * @method boolean setFilename |
|
295 | - */ |
|
296 | - public function setFilename($filename) |
|
297 | - { |
|
298 | - if ($this->isFilename($filename)) { |
|
299 | - $this->filename = $filename; |
|
300 | - } |
|
301 | - return $this; |
|
302 | - } |
|
303 | - /** |
|
304 | - * Set automatic filename |
|
305 | - * |
|
306 | - * @since 1.0 |
|
307 | - * @version 1.5 |
|
308 | - * @param string $extension |
|
309 | - * @return object |
|
310 | - * @method boolean setAutoFilename |
|
311 | - */ |
|
312 | - public function setAutoFilename() |
|
313 | - { |
|
314 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
315 | - $this->filename .= time(); |
|
316 | - return $this; |
|
317 | - } |
|
318 | - /** |
|
319 | - * Set file size limit |
|
320 | - * |
|
321 | - * @since 1.0 |
|
322 | - * @version 1.0 |
|
323 | - * @param double $file_size |
|
324 | - * @return object |
|
325 | - * @method boolean setMaxFileSize |
|
326 | - */ |
|
327 | - public function setMaxFileSize($file_size) |
|
328 | - { |
|
329 | - $file_size = $this->sizeInBytes($file_size); |
|
330 | - if (is_numeric($file_size) && $file_size > -1) { |
|
331 | - // Get php config |
|
332 | - $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
|
333 | - // Calculate difference |
|
334 | - if ($php_size < $file_size) { |
|
335 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
336 | - } |
|
337 | - $this->max_file_size = $file_size; |
|
338 | - } |
|
339 | - return $this; |
|
340 | - } |
|
341 | - /** |
|
342 | - * Set array mime types |
|
343 | - * |
|
344 | - * @since 1.0 |
|
345 | - * @version 1.0 |
|
346 | - * @param array $mimes |
|
347 | - * @return object |
|
348 | - * @method boolean setAllowedMimeTypes |
|
349 | - */ |
|
350 | - public function setAllowedMimeTypes(array $mimes) |
|
351 | - { |
|
352 | - if (count($mimes) > 0) { |
|
353 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
354 | - } |
|
355 | - return $this; |
|
356 | - } |
|
357 | - /** |
|
358 | - * Set input callback |
|
359 | - * |
|
360 | - * @since 1.0 |
|
361 | - * @version 1.0 |
|
362 | - * @param mixed $callback |
|
363 | - * @return object |
|
364 | - * @method boolean setCallbackInput |
|
365 | - */ |
|
366 | - public function setCallbackInput($callback) |
|
367 | - { |
|
368 | - if (is_callable($callback, false)) { |
|
369 | - $this->callbacks['input'] = $callback; |
|
370 | - } |
|
371 | - return $this; |
|
372 | - } |
|
373 | - /** |
|
374 | - * Set output callback |
|
375 | - * |
|
376 | - * @since 1.0 |
|
377 | - * @version 1.0 |
|
378 | - * @param mixed $callback |
|
379 | - * @return object |
|
380 | - * @method boolean setCallbackOutput |
|
381 | - */ |
|
382 | - public function setCallbackOutput($callback) |
|
383 | - { |
|
384 | - if (is_callable($callback, false)) { |
|
385 | - $this->callbacks['output'] = $callback; |
|
386 | - } |
|
387 | - return $this; |
|
388 | - } |
|
389 | - /** |
|
390 | - * Append a mime type to allowed mime types |
|
391 | - * |
|
392 | - * @since 1.0 |
|
393 | - * @version 1.0.1 |
|
394 | - * @param string $mime |
|
395 | - * @return object |
|
396 | - * @method boolean setAllowMimeType |
|
397 | - */ |
|
398 | - public function setAllowMimeType($mime) |
|
399 | - { |
|
400 | - if (!empty($mime) && is_string($mime)) { |
|
401 | - $this->allowed_mime_types[] = strtolower($mime); |
|
402 | - $this->file['allowed_mime_types'][] = strtolower($mime); |
|
403 | - } |
|
404 | - return $this; |
|
405 | - } |
|
406 | - /** |
|
407 | - * Set allowed mime types from mime helping |
|
408 | - * |
|
409 | - * @since 1.0.1 |
|
410 | - * @version 1.0.1 |
|
411 | - * @return object |
|
412 | - * @method boolean setMimeHelping |
|
413 | - */ |
|
414 | - public function setMimeHelping($name) |
|
415 | - { |
|
416 | - if (!empty($name) && is_string($name)) { |
|
417 | - if (array_key_exists($name, $this->mime_helping)) { |
|
418 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
419 | - } |
|
420 | - } |
|
421 | - return $this; |
|
422 | - } |
|
423 | - /** |
|
424 | - * Set function to upload file |
|
425 | - * Examples: |
|
426 | - * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
427 | - * 2.- FileUpload::setUploadFunction("copy"); |
|
428 | - * |
|
429 | - * @since 1.0 |
|
430 | - * @version 1.0 |
|
431 | - * @param string $function |
|
432 | - * @return object |
|
433 | - * @method boolean setUploadFunction |
|
434 | - */ |
|
435 | - public function setUploadFunction($function) |
|
436 | - { |
|
437 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
438 | - if (is_callable( $function)) { |
|
439 | - $this->upload_function = $function; |
|
440 | - } |
|
441 | - } |
|
442 | - return $this; |
|
443 | - } |
|
444 | - /** |
|
445 | - * Clear allowed mime types cache |
|
446 | - * |
|
447 | - * @since 1.0 |
|
448 | - * @version 1.0 |
|
449 | - * @return object |
|
450 | - * @method boolean clearAllowedMimeTypes |
|
451 | - */ |
|
452 | - public function clearAllowedMimeTypes() |
|
453 | - { |
|
454 | - $this->allowed_mime_types = array(); |
|
455 | - $this->file['allowed_mime_types'] = array(); |
|
456 | - return $this; |
|
457 | - } |
|
458 | - /** |
|
459 | - * Set destination output |
|
460 | - * |
|
461 | - * @since 1.0 |
|
462 | - * @version 1.0 |
|
463 | - * @param string $destination_directory Destination path |
|
464 | - * @param boolean $create_if_not_exist |
|
465 | - * @return object |
|
466 | - * @method boolean setDestinationDirectory |
|
467 | - */ |
|
468 | - public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
469 | - $destination_directory = realpath($destination_directory); |
|
470 | - if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
471 | - $destination_directory .= DIRECTORY_SEPARATOR; |
|
472 | - } |
|
473 | - |
|
474 | - if ($this->isDirpath($destination_directory)) { |
|
475 | - if ($this->dirExists($destination_directory)) { |
|
476 | - $this->destination_directory = $destination_directory; |
|
477 | - if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
478 | - $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
479 | - } |
|
480 | - chdir($destination_directory); |
|
481 | - } elseif ($create_if_not_exist === true) { |
|
482 | - if (mkdir($destination_directory, 0775, true)) { |
|
483 | - if ($this->dirExists($destination_directory)) { |
|
484 | - $this->destination_directory = $destination_directory; |
|
485 | - if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
486 | - $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
487 | - } |
|
488 | - chdir($destination_directory); |
|
489 | - } |
|
490 | - } |
|
491 | - else{ |
|
492 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
493 | - } |
|
494 | - } |
|
495 | - } |
|
496 | - return $this; |
|
497 | - } |
|
498 | - /** |
|
499 | - * Check file exists |
|
500 | - * |
|
501 | - * @since 1.0 |
|
502 | - * @version 1.0.1 |
|
503 | - * @param string $file_destination |
|
504 | - * @return boolean |
|
505 | - * @method boolean fileExists |
|
506 | - */ |
|
507 | - public function fileExists($file_destination) |
|
508 | - { |
|
509 | - if ($this->isFilename($file_destination)) { |
|
510 | - return (file_exists($file_destination) && is_file($file_destination)); |
|
511 | - } |
|
512 | - return false; |
|
513 | - } |
|
514 | - /** |
|
515 | - * Check dir exists |
|
516 | - * |
|
517 | - * @since 1.0 |
|
518 | - * @version 1.0.1 |
|
519 | - * @param string $path |
|
520 | - * @return boolean |
|
521 | - * @method boolean dirExists |
|
522 | - */ |
|
523 | - public function dirExists($path) |
|
524 | - { |
|
525 | - if ($this->isDirpath($path)) { |
|
526 | - return (file_exists($path) && is_dir($path)); |
|
527 | - } |
|
528 | - return false; |
|
529 | - } |
|
530 | - /** |
|
531 | - * Check valid filename |
|
532 | - * |
|
533 | - * @since 1.0 |
|
534 | - * @version 1.0.1 |
|
535 | - * @param string $filename |
|
536 | - * @return boolean |
|
537 | - * @method boolean isFilename |
|
538 | - */ |
|
539 | - public function isFilename($filename) |
|
540 | - { |
|
541 | - $filename = basename($filename); |
|
542 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
543 | - } |
|
544 | - /** |
|
545 | - * Validate mime type with allowed mime types, |
|
546 | - * but if allowed mime types is empty, this method return true |
|
547 | - * |
|
548 | - * @since 1.0 |
|
549 | - * @version 1.0 |
|
550 | - * @param string $mime |
|
551 | - * @return boolean |
|
552 | - * @method boolean checkMimeType |
|
553 | - */ |
|
554 | - public function checkMimeType($mime) |
|
555 | - { |
|
556 | - if (count($this->allowed_mime_types) == 0) { |
|
557 | - return true; |
|
558 | - } |
|
559 | - return in_array(strtolower($mime), $this->allowed_mime_types); |
|
560 | - } |
|
561 | - /** |
|
562 | - * Retrive status of upload |
|
563 | - * |
|
564 | - * @since 1.0 |
|
565 | - * @version 1.0 |
|
566 | - * @return boolean |
|
567 | - * @method boolean getStatus |
|
568 | - */ |
|
569 | - public function getStatus() |
|
570 | - { |
|
571 | - return $this->file['status']; |
|
572 | - } |
|
573 | - /** |
|
574 | - * Check valid path |
|
575 | - * |
|
576 | - * @since 1.0 |
|
577 | - * @version 1.0.1 |
|
578 | - * @param string $filename |
|
579 | - * @return boolean |
|
580 | - * @method boolean isDirpath |
|
581 | - */ |
|
582 | - public function isDirpath($path) |
|
583 | - { |
|
584 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
585 | - if (DIRECTORY_SEPARATOR == '/') { |
|
586 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
587 | - } else { |
|
588 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
589 | - } |
|
590 | - } |
|
591 | - return false; |
|
592 | - } |
|
593 | - /** |
|
594 | - * Allow overwriting files |
|
595 | - * |
|
596 | - * @since 1.0 |
|
597 | - * @version 1.0 |
|
598 | - * @return object |
|
599 | - * @method boolean allowOverwriting |
|
600 | - */ |
|
601 | - public function allowOverwriting() |
|
602 | - { |
|
603 | - $this->overwrite_file = true; |
|
604 | - return $this; |
|
605 | - } |
|
606 | - /** |
|
607 | - * File info |
|
608 | - * |
|
609 | - * @since 1.0 |
|
610 | - * @version 1.0 |
|
611 | - * @return object |
|
612 | - * @method object getInfo |
|
613 | - */ |
|
614 | - public function getInfo() |
|
615 | - { |
|
616 | - return (object)$this->file; |
|
617 | - } |
|
618 | - |
|
619 | - public function isUploaded(){ |
|
620 | - return isset($this->file_array[$this->input]) |
|
621 | - && |
|
622 | - is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
623 | - } |
|
624 | - /** |
|
625 | - * Upload file |
|
626 | - * |
|
627 | - * @since 1.0 |
|
628 | - * @version 1.0.1 |
|
629 | - * @return boolean |
|
630 | - * @method boolean save |
|
631 | - */ |
|
632 | - public function save(){ |
|
633 | - //check if file upload is allowed in the configuration |
|
634 | - if(! ini_get('file_uploads')){ |
|
635 | - $this->setError($this->error_messages['file_uploads']); |
|
636 | - return false; |
|
637 | - } |
|
638 | - if (count($this->file_array) > 0) { |
|
639 | - if (array_key_exists($this->input, $this->file_array)) { |
|
640 | - // set original filename if not have a new name |
|
641 | - if (empty($this->filename)) { |
|
642 | - $this->filename = $this->file_array[$this->input]['name']; |
|
643 | - } |
|
644 | - else{ |
|
645 | - // Replace %s for extension in filename |
|
646 | - // Before: /[\w\d]*(.[\d\w]+)$/i |
|
647 | - // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
648 | - // Support unicode(utf-8) characters |
|
649 | - // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
650 | - $extension = preg_replace( |
|
651 | - '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
652 | - '$1', |
|
653 | - $this->file_array[$this->input]['name'] |
|
654 | - ); |
|
655 | - $this->filename = $this->filename.'.'.$extension; |
|
656 | - } |
|
657 | - |
|
658 | - // set file info |
|
659 | - $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
660 | - $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
661 | - $this->file['original'] = $this->file_array[$this->input]['name']; |
|
662 | - $this->file['size'] = $this->file_array[$this->input]['size']; |
|
663 | - $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
664 | - $this->file['destination'] = $this->destination_directory . $this->filename; |
|
665 | - $this->file['filename'] = $this->filename; |
|
666 | - $this->file['error'] = $this->file_array[$this->input]['error']; |
|
667 | - |
|
668 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
669 | - |
|
670 | - //check for php upload error |
|
671 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
672 | - $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
673 | - return false; |
|
674 | - } |
|
2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
3 | + /** |
|
4 | + * TNH Framework |
|
5 | + * |
|
6 | + * A simple PHP framework using HMVC architecture |
|
7 | + * |
|
8 | + * This content is released under the GNU GPL License (GPL) |
|
9 | + * |
|
10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
11 | + * |
|
12 | + * This program is free software; you can redistribute it and/or |
|
13 | + * modify it under the terms of the GNU General Public License |
|
14 | + * as published by the Free Software Foundation; either version 3 |
|
15 | + * of the License, or (at your option) any later version. |
|
16 | + * |
|
17 | + * This program is distributed in the hope that it will be useful, |
|
18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | + * GNU General Public License for more details. |
|
21 | + * |
|
22 | + * You should have received a copy of the GNU General Public License |
|
23 | + * along with this program; if not, write to the Free Software |
|
24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25 | + */ |
|
26 | + |
|
27 | + |
|
28 | + |
|
29 | + /** |
|
30 | + * Upload |
|
31 | + * |
|
32 | + * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
33 | + * |
|
34 | + * @author Olaf Erlandsen <[email protected]> |
|
35 | + * @author http://www.webdevfreelance.com/ |
|
36 | + * |
|
37 | + * @package FileUpload |
|
38 | + * @version 1.5 |
|
39 | + */ |
|
40 | + class Upload{ |
|
41 | + |
|
42 | + /** |
|
43 | + * Version |
|
44 | + * |
|
45 | + * @since 1.5 |
|
46 | + * @version 1.0 |
|
47 | + */ |
|
48 | + const VERSION = '1.5'; |
|
49 | + |
|
50 | + /** |
|
51 | + * Upload function name |
|
52 | + * Remember: |
|
53 | + * Default function: move_uploaded_file |
|
54 | + * Native options: |
|
55 | + * - move_uploaded_file (Default and best option) |
|
56 | + * - copy |
|
57 | + * |
|
58 | + * @since 1.0 |
|
59 | + * @version 1.0 |
|
60 | + * @var string |
|
61 | + */ |
|
62 | + private $upload_function = 'move_uploaded_file'; |
|
63 | + |
|
64 | + /** |
|
65 | + * Array with the information obtained from the |
|
66 | + * variable $_FILES or $HTTP_POST_FILES. |
|
67 | + * |
|
68 | + * @since 1.0 |
|
69 | + * @version 1.0 |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + private $file_array = array(); |
|
73 | + |
|
74 | + /** |
|
75 | + * If the file you are trying to upload already exists it will |
|
76 | + * be overwritten if you set the variable to true. |
|
77 | + * |
|
78 | + * @since 1.0 |
|
79 | + * @version 1.0 |
|
80 | + * @var boolean |
|
81 | + */ |
|
82 | + private $overwrite_file = false; |
|
83 | + |
|
84 | + /** |
|
85 | + * Input element |
|
86 | + * Example: |
|
87 | + * <input type="file" name="file" /> |
|
88 | + * Result: |
|
89 | + * FileUpload::$input = file |
|
90 | + * |
|
91 | + * @since 1.0 |
|
92 | + * @version 1.0 |
|
93 | + * @var string |
|
94 | + */ |
|
95 | + private $input; |
|
96 | + |
|
97 | + /** |
|
98 | + * Path output |
|
99 | + * |
|
100 | + * @since 1.0 |
|
101 | + * @version 1.0 |
|
102 | + * @var string |
|
103 | + */ |
|
104 | + private $destination_directory; |
|
105 | + |
|
106 | + /** |
|
107 | + * Output filename |
|
108 | + * |
|
109 | + * @since 1.0 |
|
110 | + * @version 1.0 |
|
111 | + * @var string |
|
112 | + */ |
|
113 | + private $filename; |
|
114 | + |
|
115 | + /** |
|
116 | + * Max file size |
|
117 | + * |
|
118 | + * @since 1.0 |
|
119 | + * @version 1.0 |
|
120 | + * @var float |
|
121 | + */ |
|
122 | + private $max_file_size= 0.0; |
|
123 | + |
|
124 | + /** |
|
125 | + * List of allowed mime types |
|
126 | + * |
|
127 | + * @since 1.0 |
|
128 | + * @version 1.0 |
|
129 | + * @var array |
|
130 | + */ |
|
131 | + private $allowed_mime_types = array(); |
|
132 | + |
|
133 | + /** |
|
134 | + * Callbacks |
|
135 | + * |
|
136 | + * @since 1.0 |
|
137 | + * @version 1.0 |
|
138 | + * @var array |
|
139 | + */ |
|
140 | + private $callbacks = array('before' => null, 'after' => null); |
|
141 | + |
|
142 | + /** |
|
143 | + * File object |
|
144 | + * |
|
145 | + * @since 1.0 |
|
146 | + * @version 1.0 |
|
147 | + * @var object |
|
148 | + */ |
|
149 | + private $file; |
|
150 | + |
|
151 | + /** |
|
152 | + * Helping mime types |
|
153 | + * |
|
154 | + * @since 1.0 |
|
155 | + * @version 1.0 |
|
156 | + * @var array |
|
157 | + */ |
|
158 | + private $mime_helping = array( |
|
159 | + 'text' => array('text/plain',), |
|
160 | + 'image' => array( |
|
161 | + 'image/jpeg', |
|
162 | + 'image/jpg', |
|
163 | + 'image/pjpeg', |
|
164 | + 'image/png', |
|
165 | + 'image/gif', |
|
166 | + ), |
|
167 | + 'document' => array( |
|
168 | + 'application/pdf', |
|
169 | + 'application/msword', |
|
170 | + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
171 | + 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
172 | + 'application/vnd.ms-powerpoint', |
|
173 | + 'application/vnd.ms-excel', |
|
174 | + 'application/vnd.oasis.opendocument.spreadsheet', |
|
175 | + 'application/vnd.oasis.opendocument.presentation', |
|
176 | + ), |
|
177 | + 'video' => array( |
|
178 | + 'video/3gpp', |
|
179 | + 'video/3gpp', |
|
180 | + 'video/x-msvideo', |
|
181 | + 'video/avi', |
|
182 | + 'video/mpeg4', |
|
183 | + 'video/mp4', |
|
184 | + 'video/mpeg', |
|
185 | + 'video/mpg', |
|
186 | + 'video/quicktime', |
|
187 | + 'video/x-sgi-movie', |
|
188 | + 'video/x-ms-wmv', |
|
189 | + 'video/x-flv', |
|
190 | + ), |
|
191 | + ); |
|
192 | + |
|
193 | + /** |
|
194 | + * The upload error message |
|
195 | + * @var array |
|
196 | + */ |
|
197 | + public $error_messages = array(); |
|
198 | + |
|
199 | + /** |
|
200 | + * The upload error message |
|
201 | + * @var string |
|
202 | + */ |
|
203 | + protected $error = null; |
|
204 | + |
|
205 | + /** |
|
206 | + * The logger instance |
|
207 | + * @var Log |
|
208 | + */ |
|
209 | + private $logger; |
|
210 | + |
|
211 | + |
|
212 | + /** |
|
213 | + * Construct |
|
214 | + * |
|
215 | + * @since 0.1 |
|
216 | + * @version 1.0.1 |
|
217 | + * @return object |
|
218 | + * @method object __construct |
|
219 | + */ |
|
220 | + public function __construct(){ |
|
221 | + $this->logger =& class_loader('Log', 'classes'); |
|
222 | + $this->logger->setLogger('Library::Upload'); |
|
223 | + |
|
224 | + Loader::lang('file_upload'); |
|
225 | + $obj =& get_instance(); |
|
226 | + |
|
227 | + $this->error_messages = array( |
|
228 | + 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
229 | + 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
230 | + 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
231 | + 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
232 | + 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
233 | + 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
234 | + 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
235 | + 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
236 | + 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
237 | + 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
238 | + 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
239 | + ); |
|
240 | + |
|
241 | + $this->file = array( |
|
242 | + 'status' => false, // True: success upload |
|
243 | + 'mime' => '', // Empty string |
|
244 | + 'filename' => '', // Empty string |
|
245 | + 'original' => '', // Empty string |
|
246 | + 'size' => 0, // 0 Bytes |
|
247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
248 | + 'destination' => './', // Default: ./ |
|
249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | + 'error' => null, // File error |
|
251 | + ); |
|
252 | + |
|
253 | + // Change dir to current dir |
|
254 | + $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
255 | + |
|
256 | + // Set file array |
|
257 | + if (isset($_FILES) && is_array($_FILES)) { |
|
258 | + $this->file_array = $_FILES; |
|
259 | + } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
|
260 | + $this->file_array = $HTTP_POST_FILES; |
|
261 | + } |
|
262 | + $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
263 | + } |
|
264 | + /** |
|
265 | + * Set input. |
|
266 | + * If you have $_FILES["file"], you must use the key "file" |
|
267 | + * Example: |
|
268 | + * $object->setInput("file"); |
|
269 | + * |
|
270 | + * @since 1.0 |
|
271 | + * @version 1.0 |
|
272 | + * @param string $input |
|
273 | + * @return object |
|
274 | + * @method boolean setInput |
|
275 | + */ |
|
276 | + public function setInput($input) |
|
277 | + { |
|
278 | + if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
279 | + $this->input = $input; |
|
280 | + } |
|
281 | + return $this; |
|
282 | + } |
|
283 | + /** |
|
284 | + * Set new filename |
|
285 | + * Example: |
|
286 | + * FileUpload::setFilename("new file.txt") |
|
287 | + * Remember: |
|
288 | + * Use %s to retrive file extension |
|
289 | + * |
|
290 | + * @since 1.0 |
|
291 | + * @version 1.0 |
|
292 | + * @param string $filename |
|
293 | + * @return object |
|
294 | + * @method boolean setFilename |
|
295 | + */ |
|
296 | + public function setFilename($filename) |
|
297 | + { |
|
298 | + if ($this->isFilename($filename)) { |
|
299 | + $this->filename = $filename; |
|
300 | + } |
|
301 | + return $this; |
|
302 | + } |
|
303 | + /** |
|
304 | + * Set automatic filename |
|
305 | + * |
|
306 | + * @since 1.0 |
|
307 | + * @version 1.5 |
|
308 | + * @param string $extension |
|
309 | + * @return object |
|
310 | + * @method boolean setAutoFilename |
|
311 | + */ |
|
312 | + public function setAutoFilename() |
|
313 | + { |
|
314 | + $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
315 | + $this->filename .= time(); |
|
316 | + return $this; |
|
317 | + } |
|
318 | + /** |
|
319 | + * Set file size limit |
|
320 | + * |
|
321 | + * @since 1.0 |
|
322 | + * @version 1.0 |
|
323 | + * @param double $file_size |
|
324 | + * @return object |
|
325 | + * @method boolean setMaxFileSize |
|
326 | + */ |
|
327 | + public function setMaxFileSize($file_size) |
|
328 | + { |
|
329 | + $file_size = $this->sizeInBytes($file_size); |
|
330 | + if (is_numeric($file_size) && $file_size > -1) { |
|
331 | + // Get php config |
|
332 | + $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
|
333 | + // Calculate difference |
|
334 | + if ($php_size < $file_size) { |
|
335 | + $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
336 | + } |
|
337 | + $this->max_file_size = $file_size; |
|
338 | + } |
|
339 | + return $this; |
|
340 | + } |
|
341 | + /** |
|
342 | + * Set array mime types |
|
343 | + * |
|
344 | + * @since 1.0 |
|
345 | + * @version 1.0 |
|
346 | + * @param array $mimes |
|
347 | + * @return object |
|
348 | + * @method boolean setAllowedMimeTypes |
|
349 | + */ |
|
350 | + public function setAllowedMimeTypes(array $mimes) |
|
351 | + { |
|
352 | + if (count($mimes) > 0) { |
|
353 | + array_map(array($this , 'setAllowMimeType'), $mimes); |
|
354 | + } |
|
355 | + return $this; |
|
356 | + } |
|
357 | + /** |
|
358 | + * Set input callback |
|
359 | + * |
|
360 | + * @since 1.0 |
|
361 | + * @version 1.0 |
|
362 | + * @param mixed $callback |
|
363 | + * @return object |
|
364 | + * @method boolean setCallbackInput |
|
365 | + */ |
|
366 | + public function setCallbackInput($callback) |
|
367 | + { |
|
368 | + if (is_callable($callback, false)) { |
|
369 | + $this->callbacks['input'] = $callback; |
|
370 | + } |
|
371 | + return $this; |
|
372 | + } |
|
373 | + /** |
|
374 | + * Set output callback |
|
375 | + * |
|
376 | + * @since 1.0 |
|
377 | + * @version 1.0 |
|
378 | + * @param mixed $callback |
|
379 | + * @return object |
|
380 | + * @method boolean setCallbackOutput |
|
381 | + */ |
|
382 | + public function setCallbackOutput($callback) |
|
383 | + { |
|
384 | + if (is_callable($callback, false)) { |
|
385 | + $this->callbacks['output'] = $callback; |
|
386 | + } |
|
387 | + return $this; |
|
388 | + } |
|
389 | + /** |
|
390 | + * Append a mime type to allowed mime types |
|
391 | + * |
|
392 | + * @since 1.0 |
|
393 | + * @version 1.0.1 |
|
394 | + * @param string $mime |
|
395 | + * @return object |
|
396 | + * @method boolean setAllowMimeType |
|
397 | + */ |
|
398 | + public function setAllowMimeType($mime) |
|
399 | + { |
|
400 | + if (!empty($mime) && is_string($mime)) { |
|
401 | + $this->allowed_mime_types[] = strtolower($mime); |
|
402 | + $this->file['allowed_mime_types'][] = strtolower($mime); |
|
403 | + } |
|
404 | + return $this; |
|
405 | + } |
|
406 | + /** |
|
407 | + * Set allowed mime types from mime helping |
|
408 | + * |
|
409 | + * @since 1.0.1 |
|
410 | + * @version 1.0.1 |
|
411 | + * @return object |
|
412 | + * @method boolean setMimeHelping |
|
413 | + */ |
|
414 | + public function setMimeHelping($name) |
|
415 | + { |
|
416 | + if (!empty($name) && is_string($name)) { |
|
417 | + if (array_key_exists($name, $this->mime_helping)) { |
|
418 | + return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
419 | + } |
|
420 | + } |
|
421 | + return $this; |
|
422 | + } |
|
423 | + /** |
|
424 | + * Set function to upload file |
|
425 | + * Examples: |
|
426 | + * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
427 | + * 2.- FileUpload::setUploadFunction("copy"); |
|
428 | + * |
|
429 | + * @since 1.0 |
|
430 | + * @version 1.0 |
|
431 | + * @param string $function |
|
432 | + * @return object |
|
433 | + * @method boolean setUploadFunction |
|
434 | + */ |
|
435 | + public function setUploadFunction($function) |
|
436 | + { |
|
437 | + if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
438 | + if (is_callable( $function)) { |
|
439 | + $this->upload_function = $function; |
|
440 | + } |
|
441 | + } |
|
442 | + return $this; |
|
443 | + } |
|
444 | + /** |
|
445 | + * Clear allowed mime types cache |
|
446 | + * |
|
447 | + * @since 1.0 |
|
448 | + * @version 1.0 |
|
449 | + * @return object |
|
450 | + * @method boolean clearAllowedMimeTypes |
|
451 | + */ |
|
452 | + public function clearAllowedMimeTypes() |
|
453 | + { |
|
454 | + $this->allowed_mime_types = array(); |
|
455 | + $this->file['allowed_mime_types'] = array(); |
|
456 | + return $this; |
|
457 | + } |
|
458 | + /** |
|
459 | + * Set destination output |
|
460 | + * |
|
461 | + * @since 1.0 |
|
462 | + * @version 1.0 |
|
463 | + * @param string $destination_directory Destination path |
|
464 | + * @param boolean $create_if_not_exist |
|
465 | + * @return object |
|
466 | + * @method boolean setDestinationDirectory |
|
467 | + */ |
|
468 | + public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
469 | + $destination_directory = realpath($destination_directory); |
|
470 | + if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
471 | + $destination_directory .= DIRECTORY_SEPARATOR; |
|
472 | + } |
|
473 | + |
|
474 | + if ($this->isDirpath($destination_directory)) { |
|
475 | + if ($this->dirExists($destination_directory)) { |
|
476 | + $this->destination_directory = $destination_directory; |
|
477 | + if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
478 | + $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
479 | + } |
|
480 | + chdir($destination_directory); |
|
481 | + } elseif ($create_if_not_exist === true) { |
|
482 | + if (mkdir($destination_directory, 0775, true)) { |
|
483 | + if ($this->dirExists($destination_directory)) { |
|
484 | + $this->destination_directory = $destination_directory; |
|
485 | + if (substr($this->destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
486 | + $this->destination_directory .= DIRECTORY_SEPARATOR; |
|
487 | + } |
|
488 | + chdir($destination_directory); |
|
489 | + } |
|
490 | + } |
|
491 | + else{ |
|
492 | + $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
493 | + } |
|
494 | + } |
|
495 | + } |
|
496 | + return $this; |
|
497 | + } |
|
498 | + /** |
|
499 | + * Check file exists |
|
500 | + * |
|
501 | + * @since 1.0 |
|
502 | + * @version 1.0.1 |
|
503 | + * @param string $file_destination |
|
504 | + * @return boolean |
|
505 | + * @method boolean fileExists |
|
506 | + */ |
|
507 | + public function fileExists($file_destination) |
|
508 | + { |
|
509 | + if ($this->isFilename($file_destination)) { |
|
510 | + return (file_exists($file_destination) && is_file($file_destination)); |
|
511 | + } |
|
512 | + return false; |
|
513 | + } |
|
514 | + /** |
|
515 | + * Check dir exists |
|
516 | + * |
|
517 | + * @since 1.0 |
|
518 | + * @version 1.0.1 |
|
519 | + * @param string $path |
|
520 | + * @return boolean |
|
521 | + * @method boolean dirExists |
|
522 | + */ |
|
523 | + public function dirExists($path) |
|
524 | + { |
|
525 | + if ($this->isDirpath($path)) { |
|
526 | + return (file_exists($path) && is_dir($path)); |
|
527 | + } |
|
528 | + return false; |
|
529 | + } |
|
530 | + /** |
|
531 | + * Check valid filename |
|
532 | + * |
|
533 | + * @since 1.0 |
|
534 | + * @version 1.0.1 |
|
535 | + * @param string $filename |
|
536 | + * @return boolean |
|
537 | + * @method boolean isFilename |
|
538 | + */ |
|
539 | + public function isFilename($filename) |
|
540 | + { |
|
541 | + $filename = basename($filename); |
|
542 | + return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
543 | + } |
|
544 | + /** |
|
545 | + * Validate mime type with allowed mime types, |
|
546 | + * but if allowed mime types is empty, this method return true |
|
547 | + * |
|
548 | + * @since 1.0 |
|
549 | + * @version 1.0 |
|
550 | + * @param string $mime |
|
551 | + * @return boolean |
|
552 | + * @method boolean checkMimeType |
|
553 | + */ |
|
554 | + public function checkMimeType($mime) |
|
555 | + { |
|
556 | + if (count($this->allowed_mime_types) == 0) { |
|
557 | + return true; |
|
558 | + } |
|
559 | + return in_array(strtolower($mime), $this->allowed_mime_types); |
|
560 | + } |
|
561 | + /** |
|
562 | + * Retrive status of upload |
|
563 | + * |
|
564 | + * @since 1.0 |
|
565 | + * @version 1.0 |
|
566 | + * @return boolean |
|
567 | + * @method boolean getStatus |
|
568 | + */ |
|
569 | + public function getStatus() |
|
570 | + { |
|
571 | + return $this->file['status']; |
|
572 | + } |
|
573 | + /** |
|
574 | + * Check valid path |
|
575 | + * |
|
576 | + * @since 1.0 |
|
577 | + * @version 1.0.1 |
|
578 | + * @param string $filename |
|
579 | + * @return boolean |
|
580 | + * @method boolean isDirpath |
|
581 | + */ |
|
582 | + public function isDirpath($path) |
|
583 | + { |
|
584 | + if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
585 | + if (DIRECTORY_SEPARATOR == '/') { |
|
586 | + return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
587 | + } else { |
|
588 | + return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
589 | + } |
|
590 | + } |
|
591 | + return false; |
|
592 | + } |
|
593 | + /** |
|
594 | + * Allow overwriting files |
|
595 | + * |
|
596 | + * @since 1.0 |
|
597 | + * @version 1.0 |
|
598 | + * @return object |
|
599 | + * @method boolean allowOverwriting |
|
600 | + */ |
|
601 | + public function allowOverwriting() |
|
602 | + { |
|
603 | + $this->overwrite_file = true; |
|
604 | + return $this; |
|
605 | + } |
|
606 | + /** |
|
607 | + * File info |
|
608 | + * |
|
609 | + * @since 1.0 |
|
610 | + * @version 1.0 |
|
611 | + * @return object |
|
612 | + * @method object getInfo |
|
613 | + */ |
|
614 | + public function getInfo() |
|
615 | + { |
|
616 | + return (object)$this->file; |
|
617 | + } |
|
618 | + |
|
619 | + public function isUploaded(){ |
|
620 | + return isset($this->file_array[$this->input]) |
|
621 | + && |
|
622 | + is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
623 | + } |
|
624 | + /** |
|
625 | + * Upload file |
|
626 | + * |
|
627 | + * @since 1.0 |
|
628 | + * @version 1.0.1 |
|
629 | + * @return boolean |
|
630 | + * @method boolean save |
|
631 | + */ |
|
632 | + public function save(){ |
|
633 | + //check if file upload is allowed in the configuration |
|
634 | + if(! ini_get('file_uploads')){ |
|
635 | + $this->setError($this->error_messages['file_uploads']); |
|
636 | + return false; |
|
637 | + } |
|
638 | + if (count($this->file_array) > 0) { |
|
639 | + if (array_key_exists($this->input, $this->file_array)) { |
|
640 | + // set original filename if not have a new name |
|
641 | + if (empty($this->filename)) { |
|
642 | + $this->filename = $this->file_array[$this->input]['name']; |
|
643 | + } |
|
644 | + else{ |
|
645 | + // Replace %s for extension in filename |
|
646 | + // Before: /[\w\d]*(.[\d\w]+)$/i |
|
647 | + // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
648 | + // Support unicode(utf-8) characters |
|
649 | + // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
650 | + $extension = preg_replace( |
|
651 | + '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
652 | + '$1', |
|
653 | + $this->file_array[$this->input]['name'] |
|
654 | + ); |
|
655 | + $this->filename = $this->filename.'.'.$extension; |
|
656 | + } |
|
657 | + |
|
658 | + // set file info |
|
659 | + $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
660 | + $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
661 | + $this->file['original'] = $this->file_array[$this->input]['name']; |
|
662 | + $this->file['size'] = $this->file_array[$this->input]['size']; |
|
663 | + $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
664 | + $this->file['destination'] = $this->destination_directory . $this->filename; |
|
665 | + $this->file['filename'] = $this->filename; |
|
666 | + $this->file['error'] = $this->file_array[$this->input]['error']; |
|
667 | + |
|
668 | + $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
669 | + |
|
670 | + //check for php upload error |
|
671 | + if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
672 | + $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
673 | + return false; |
|
674 | + } |
|
675 | 675 | |
676 | - //check for mime type |
|
677 | - if (!$this->checkMimeType($this->file['mime'])) { |
|
678 | - $this->setError($this->error_messages['accept_file_types']); |
|
679 | - return false; |
|
680 | - } |
|
681 | - |
|
682 | - // Check file size |
|
683 | - if ($this->max_file_size > 0) { |
|
684 | - if ($this->max_file_size < $this->file['size']) { |
|
685 | - $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
686 | - return false; |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - // Check if exists file |
|
691 | - if ($this->fileExists($this->destination_directory . $this->filename)) { |
|
692 | - // Check if overwrite file |
|
693 | - if ($this->overwrite_file === false) { |
|
694 | - $this->setError($this->error_messages['overwritten_not_allowed']); |
|
695 | - return false; |
|
696 | - } |
|
697 | - } |
|
698 | - |
|
699 | - // Execute input callback |
|
700 | - if (!empty( $this->callbacks['input'])) { |
|
701 | - call_user_func($this->callbacks['input'], (object)$this->file); |
|
702 | - } |
|
676 | + //check for mime type |
|
677 | + if (!$this->checkMimeType($this->file['mime'])) { |
|
678 | + $this->setError($this->error_messages['accept_file_types']); |
|
679 | + return false; |
|
680 | + } |
|
681 | + |
|
682 | + // Check file size |
|
683 | + if ($this->max_file_size > 0) { |
|
684 | + if ($this->max_file_size < $this->file['size']) { |
|
685 | + $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
686 | + return false; |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + // Check if exists file |
|
691 | + if ($this->fileExists($this->destination_directory . $this->filename)) { |
|
692 | + // Check if overwrite file |
|
693 | + if ($this->overwrite_file === false) { |
|
694 | + $this->setError($this->error_messages['overwritten_not_allowed']); |
|
695 | + return false; |
|
696 | + } |
|
697 | + } |
|
698 | + |
|
699 | + // Execute input callback |
|
700 | + if (!empty( $this->callbacks['input'])) { |
|
701 | + call_user_func($this->callbacks['input'], (object)$this->file); |
|
702 | + } |
|
703 | 703 | |
704 | 704 | |
705 | - $this->file['status'] = call_user_func_array( |
|
706 | - $this->upload_function, array( |
|
707 | - $this->file_array[$this->input]['tmp_name'], |
|
708 | - $this->destination_directory . $this->filename |
|
709 | - ) |
|
710 | - ); |
|
711 | - |
|
712 | - // Execute output callback |
|
713 | - if (!empty( $this->callbacks['output'])) { |
|
714 | - call_user_func($this->callbacks['output'], (object)$this->file); |
|
715 | - } |
|
716 | - return $this->file['status']; |
|
717 | - } |
|
718 | - } |
|
719 | - } |
|
720 | - |
|
721 | - /** |
|
722 | - * File size for humans. |
|
723 | - * |
|
724 | - * @since 1.0 |
|
725 | - * @version 1.0 |
|
726 | - * @param integer $bytes |
|
727 | - * @param integer $precision |
|
728 | - * @return string |
|
729 | - * @method string sizeFormat |
|
730 | - */ |
|
731 | - public function sizeFormat($size, $precision = 2) |
|
732 | - { |
|
733 | - if($size > 0){ |
|
734 | - $base = log($size) / log(1024); |
|
735 | - $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
736 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
737 | - } |
|
738 | - return null; |
|
739 | - } |
|
705 | + $this->file['status'] = call_user_func_array( |
|
706 | + $this->upload_function, array( |
|
707 | + $this->file_array[$this->input]['tmp_name'], |
|
708 | + $this->destination_directory . $this->filename |
|
709 | + ) |
|
710 | + ); |
|
711 | + |
|
712 | + // Execute output callback |
|
713 | + if (!empty( $this->callbacks['output'])) { |
|
714 | + call_user_func($this->callbacks['output'], (object)$this->file); |
|
715 | + } |
|
716 | + return $this->file['status']; |
|
717 | + } |
|
718 | + } |
|
719 | + } |
|
720 | + |
|
721 | + /** |
|
722 | + * File size for humans. |
|
723 | + * |
|
724 | + * @since 1.0 |
|
725 | + * @version 1.0 |
|
726 | + * @param integer $bytes |
|
727 | + * @param integer $precision |
|
728 | + * @return string |
|
729 | + * @method string sizeFormat |
|
730 | + */ |
|
731 | + public function sizeFormat($size, $precision = 2) |
|
732 | + { |
|
733 | + if($size > 0){ |
|
734 | + $base = log($size) / log(1024); |
|
735 | + $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
736 | + return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
737 | + } |
|
738 | + return null; |
|
739 | + } |
|
740 | 740 | |
741 | 741 | |
742 | - /** |
|
743 | - * Convert human file size to bytes |
|
744 | - * |
|
745 | - * @since 1.0 |
|
746 | - * @version 1.0.1 |
|
747 | - * @param integer $size |
|
748 | - * @return string |
|
749 | - * @method string sizeInBytes |
|
750 | - */ |
|
751 | - public function sizeInBytes($size) |
|
752 | - { |
|
753 | - $unit = 'B'; |
|
754 | - $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
755 | - $matches = array(); |
|
756 | - preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
757 | - if (array_key_exists('unit', $matches)) { |
|
758 | - $unit = strtoupper($matches['unit']); |
|
759 | - } |
|
760 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
761 | - } |
|
762 | - |
|
763 | - /** |
|
764 | - * Get the upload error message |
|
765 | - * @return string |
|
766 | - */ |
|
767 | - public function getError(){ |
|
768 | - return $this->error; |
|
769 | - } |
|
770 | - |
|
771 | - /** |
|
772 | - * Set the upload error message |
|
773 | - * @param string $message the upload error message to set |
|
774 | - */ |
|
775 | - public function setError($message){ |
|
776 | - $this->logger->info('The upload got error : ' . $message); |
|
777 | - $this->error = $message; |
|
778 | - } |
|
779 | - |
|
780 | - /** |
|
781 | - * Get the PHP upload error message for the given code |
|
782 | - * @param int $code the error code |
|
783 | - * @return string the error message |
|
784 | - */ |
|
785 | - private function getPhpUploadErrorMessageByCode($code){ |
|
786 | - $codeMessageMaps = array( |
|
787 | - 1 => $this->error_messages['upload_err_ini_size'], |
|
788 | - 2 => $this->error_messages['upload_err_form_size'], |
|
789 | - 3 => $this->error_messages['upload_err_partial'], |
|
790 | - 4 => $this->error_messages['upload_err_no_file'], |
|
791 | - 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
792 | - 7 => $this->error_messages['upload_err_cant_write'], |
|
793 | - 8 => $this->error_messages['upload_err_extension'], |
|
794 | - ); |
|
795 | - return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
796 | - } |
|
797 | - } |
|
742 | + /** |
|
743 | + * Convert human file size to bytes |
|
744 | + * |
|
745 | + * @since 1.0 |
|
746 | + * @version 1.0.1 |
|
747 | + * @param integer $size |
|
748 | + * @return string |
|
749 | + * @method string sizeInBytes |
|
750 | + */ |
|
751 | + public function sizeInBytes($size) |
|
752 | + { |
|
753 | + $unit = 'B'; |
|
754 | + $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
755 | + $matches = array(); |
|
756 | + preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
757 | + if (array_key_exists('unit', $matches)) { |
|
758 | + $unit = strtoupper($matches['unit']); |
|
759 | + } |
|
760 | + return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
761 | + } |
|
762 | + |
|
763 | + /** |
|
764 | + * Get the upload error message |
|
765 | + * @return string |
|
766 | + */ |
|
767 | + public function getError(){ |
|
768 | + return $this->error; |
|
769 | + } |
|
770 | + |
|
771 | + /** |
|
772 | + * Set the upload error message |
|
773 | + * @param string $message the upload error message to set |
|
774 | + */ |
|
775 | + public function setError($message){ |
|
776 | + $this->logger->info('The upload got error : ' . $message); |
|
777 | + $this->error = $message; |
|
778 | + } |
|
779 | + |
|
780 | + /** |
|
781 | + * Get the PHP upload error message for the given code |
|
782 | + * @param int $code the error code |
|
783 | + * @return string the error message |
|
784 | + */ |
|
785 | + private function getPhpUploadErrorMessageByCode($code){ |
|
786 | + $codeMessageMaps = array( |
|
787 | + 1 => $this->error_messages['upload_err_ini_size'], |
|
788 | + 2 => $this->error_messages['upload_err_form_size'], |
|
789 | + 3 => $this->error_messages['upload_err_partial'], |
|
790 | + 4 => $this->error_messages['upload_err_no_file'], |
|
791 | + 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
792 | + 7 => $this->error_messages['upload_err_cant_write'], |
|
793 | + 8 => $this->error_messages['upload_err_extension'], |
|
794 | + ); |
|
795 | + return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
796 | + } |
|
797 | + } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @package FileUpload |
38 | 38 | * @version 1.5 |
39 | 39 | */ |
40 | - class Upload{ |
|
40 | + class Upload { |
|
41 | 41 | |
42 | 42 | /** |
43 | 43 | * Version |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * @version 1.0 |
70 | 70 | * @var array |
71 | 71 | */ |
72 | - private $file_array = array(); |
|
72 | + private $file_array = array(); |
|
73 | 73 | |
74 | 74 | /** |
75 | 75 | * If the file you are trying to upload already exists it will |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | * @version 1.0 |
120 | 120 | * @var float |
121 | 121 | */ |
122 | - private $max_file_size= 0.0; |
|
122 | + private $max_file_size = 0.0; |
|
123 | 123 | |
124 | 124 | /** |
125 | 125 | * List of allowed mime types |
@@ -217,12 +217,12 @@ discard block |
||
217 | 217 | * @return object |
218 | 218 | * @method object __construct |
219 | 219 | */ |
220 | - public function __construct(){ |
|
221 | - $this->logger =& class_loader('Log', 'classes'); |
|
220 | + public function __construct() { |
|
221 | + $this->logger = & class_loader('Log', 'classes'); |
|
222 | 222 | $this->logger->setLogger('Library::Upload'); |
223 | 223 | |
224 | 224 | Loader::lang('file_upload'); |
225 | - $obj =& get_instance(); |
|
225 | + $obj = & get_instance(); |
|
226 | 226 | |
227 | 227 | $this->error_messages = array( |
228 | 228 | 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
@@ -239,15 +239,15 @@ discard block |
||
239 | 239 | ); |
240 | 240 | |
241 | 241 | $this->file = array( |
242 | - 'status' => false, // True: success upload |
|
243 | - 'mime' => '', // Empty string |
|
244 | - 'filename' => '', // Empty string |
|
245 | - 'original' => '', // Empty string |
|
246 | - 'size' => 0, // 0 Bytes |
|
247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
248 | - 'destination' => './', // Default: ./ |
|
249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | - 'error' => null, // File error |
|
242 | + 'status' => false, // True: success upload |
|
243 | + 'mime' => '', // Empty string |
|
244 | + 'filename' => '', // Empty string |
|
245 | + 'original' => '', // Empty string |
|
246 | + 'size' => 0, // 0 Bytes |
|
247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
248 | + 'destination' => './', // Default: ./ |
|
249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
250 | + 'error' => null, // File error |
|
251 | 251 | ); |
252 | 252 | |
253 | 253 | // Change dir to current dir |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
260 | 260 | $this->file_array = $HTTP_POST_FILES; |
261 | 261 | } |
262 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
262 | + $this->logger->info('The upload file information are : ' . stringfy_vars($this->file_array)); |
|
263 | 263 | } |
264 | 264 | /** |
265 | 265 | * Set input. |
@@ -275,7 +275,7 @@ discard block |
||
275 | 275 | */ |
276 | 276 | public function setInput($input) |
277 | 277 | { |
278 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
278 | + if (!empty($input) && (is_string($input) || is_numeric($input))) { |
|
279 | 279 | $this->input = $input; |
280 | 280 | } |
281 | 281 | return $this; |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | */ |
312 | 312 | public function setAutoFilename() |
313 | 313 | { |
314 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
314 | + $this->filename = sha1(mt_rand(1, 9999) . uniqid()); |
|
315 | 315 | $this->filename .= time(); |
316 | 316 | return $this; |
317 | 317 | } |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
333 | 333 | // Calculate difference |
334 | 334 | if ($php_size < $file_size) { |
335 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
335 | + $this->logger->warning('The upload max file size you set [' . $file_size . '] is greather than the PHP configuration for upload max file size [' . $php_size . ']'); |
|
336 | 336 | } |
337 | 337 | $this->max_file_size = $file_size; |
338 | 338 | } |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | public function setAllowedMimeTypes(array $mimes) |
351 | 351 | { |
352 | 352 | if (count($mimes) > 0) { |
353 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
353 | + array_map(array($this, 'setAllowMimeType'), $mimes); |
|
354 | 354 | } |
355 | 355 | return $this; |
356 | 356 | } |
@@ -415,7 +415,7 @@ discard block |
||
415 | 415 | { |
416 | 416 | if (!empty($name) && is_string($name)) { |
417 | 417 | if (array_key_exists($name, $this->mime_helping)) { |
418 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
418 | + return $this->setAllowedMimeTypes($this->mime_helping[$name]); |
|
419 | 419 | } |
420 | 420 | } |
421 | 421 | return $this; |
@@ -434,8 +434,8 @@ discard block |
||
434 | 434 | */ |
435 | 435 | public function setUploadFunction($function) |
436 | 436 | { |
437 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
438 | - if (is_callable( $function)) { |
|
437 | + if (!empty($function) && (is_array($function) || is_string($function))) { |
|
438 | + if (is_callable($function)) { |
|
439 | 439 | $this->upload_function = $function; |
440 | 440 | } |
441 | 441 | } |
@@ -488,8 +488,8 @@ discard block |
||
488 | 488 | chdir($destination_directory); |
489 | 489 | } |
490 | 490 | } |
491 | - else{ |
|
492 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
491 | + else { |
|
492 | + $this->logger->warning('Can not create the upload directory [' . $destination_directory . ']'); |
|
493 | 493 | } |
494 | 494 | } |
495 | 495 | } |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | public function isFilename($filename) |
540 | 540 | { |
541 | 541 | $filename = basename($filename); |
542 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
542 | + return (!empty($filename) && (is_string($filename) || is_numeric($filename))); |
|
543 | 543 | } |
544 | 544 | /** |
545 | 545 | * Validate mime type with allowed mime types, |
@@ -581,11 +581,11 @@ discard block |
||
581 | 581 | */ |
582 | 582 | public function isDirpath($path) |
583 | 583 | { |
584 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
584 | + if (!empty($path) && (is_string($path) || is_numeric($path))) { |
|
585 | 585 | if (DIRECTORY_SEPARATOR == '/') { |
586 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
586 | + return (preg_match('/^[^*?"<>|:]*$/', $path) == 1); |
|
587 | 587 | } else { |
588 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
588 | + return (preg_match("/^[^*?\"<>|:]*$/", substr($path, 2)) == 1); |
|
589 | 589 | } |
590 | 590 | } |
591 | 591 | return false; |
@@ -613,10 +613,10 @@ discard block |
||
613 | 613 | */ |
614 | 614 | public function getInfo() |
615 | 615 | { |
616 | - return (object)$this->file; |
|
616 | + return (object) $this->file; |
|
617 | 617 | } |
618 | 618 | |
619 | - public function isUploaded(){ |
|
619 | + public function isUploaded() { |
|
620 | 620 | return isset($this->file_array[$this->input]) |
621 | 621 | && |
622 | 622 | is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
@@ -629,9 +629,9 @@ discard block |
||
629 | 629 | * @return boolean |
630 | 630 | * @method boolean save |
631 | 631 | */ |
632 | - public function save(){ |
|
632 | + public function save() { |
|
633 | 633 | //check if file upload is allowed in the configuration |
634 | - if(! ini_get('file_uploads')){ |
|
634 | + if (!ini_get('file_uploads')) { |
|
635 | 635 | $this->setError($this->error_messages['file_uploads']); |
636 | 636 | return false; |
637 | 637 | } |
@@ -641,7 +641,7 @@ discard block |
||
641 | 641 | if (empty($this->filename)) { |
642 | 642 | $this->filename = $this->file_array[$this->input]['name']; |
643 | 643 | } |
644 | - else{ |
|
644 | + else { |
|
645 | 645 | // Replace %s for extension in filename |
646 | 646 | // Before: /[\w\d]*(.[\d\w]+)$/i |
647 | 647 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
@@ -652,7 +652,7 @@ discard block |
||
652 | 652 | '$1', |
653 | 653 | $this->file_array[$this->input]['name'] |
654 | 654 | ); |
655 | - $this->filename = $this->filename.'.'.$extension; |
|
655 | + $this->filename = $this->filename . '.' . $extension; |
|
656 | 656 | } |
657 | 657 | |
658 | 658 | // set file info |
@@ -665,10 +665,10 @@ discard block |
||
665 | 665 | $this->file['filename'] = $this->filename; |
666 | 666 | $this->file['error'] = $this->file_array[$this->input]['error']; |
667 | 667 | |
668 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
668 | + $this->logger->info('The upload file information to process is : ' . stringfy_vars($this->file)); |
|
669 | 669 | |
670 | 670 | //check for php upload error |
671 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
671 | + if (is_numeric($this->file['error']) && $this->file['error'] > 0) { |
|
672 | 672 | $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
673 | 673 | return false; |
674 | 674 | } |
@@ -697,8 +697,8 @@ discard block |
||
697 | 697 | } |
698 | 698 | |
699 | 699 | // Execute input callback |
700 | - if (!empty( $this->callbacks['input'])) { |
|
701 | - call_user_func($this->callbacks['input'], (object)$this->file); |
|
700 | + if (!empty($this->callbacks['input'])) { |
|
701 | + call_user_func($this->callbacks['input'], (object) $this->file); |
|
702 | 702 | } |
703 | 703 | |
704 | 704 | |
@@ -710,8 +710,8 @@ discard block |
||
710 | 710 | ); |
711 | 711 | |
712 | 712 | // Execute output callback |
713 | - if (!empty( $this->callbacks['output'])) { |
|
714 | - call_user_func($this->callbacks['output'], (object)$this->file); |
|
713 | + if (!empty($this->callbacks['output'])) { |
|
714 | + call_user_func($this->callbacks['output'], (object) $this->file); |
|
715 | 715 | } |
716 | 716 | return $this->file['status']; |
717 | 717 | } |
@@ -730,10 +730,10 @@ discard block |
||
730 | 730 | */ |
731 | 731 | public function sizeFormat($size, $precision = 2) |
732 | 732 | { |
733 | - if($size > 0){ |
|
733 | + if ($size > 0) { |
|
734 | 734 | $base = log($size) / log(1024); |
735 | 735 | $suffixes = array('B', 'K', 'M', 'G', 'T'); |
736 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
736 | + return round(pow(1024, $base - floor($base)), $precision) . (isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
737 | 737 | } |
738 | 738 | return null; |
739 | 739 | } |
@@ -757,14 +757,14 @@ discard block |
||
757 | 757 | if (array_key_exists('unit', $matches)) { |
758 | 758 | $unit = strtoupper($matches['unit']); |
759 | 759 | } |
760 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
760 | + return (floatval($matches['size']) * pow(1024, $units[$unit])); |
|
761 | 761 | } |
762 | 762 | |
763 | 763 | /** |
764 | 764 | * Get the upload error message |
765 | 765 | * @return string |
766 | 766 | */ |
767 | - public function getError(){ |
|
767 | + public function getError() { |
|
768 | 768 | return $this->error; |
769 | 769 | } |
770 | 770 | |
@@ -772,7 +772,7 @@ discard block |
||
772 | 772 | * Set the upload error message |
773 | 773 | * @param string $message the upload error message to set |
774 | 774 | */ |
775 | - public function setError($message){ |
|
775 | + public function setError($message) { |
|
776 | 776 | $this->logger->info('The upload got error : ' . $message); |
777 | 777 | $this->error = $message; |
778 | 778 | } |
@@ -782,7 +782,7 @@ discard block |
||
782 | 782 | * @param int $code the error code |
783 | 783 | * @return string the error message |
784 | 784 | */ |
785 | - private function getPhpUploadErrorMessageByCode($code){ |
|
785 | + private function getPhpUploadErrorMessageByCode($code) { |
|
786 | 786 | $codeMessageMaps = array( |
787 | 787 | 1 => $this->error_messages['upload_err_ini_size'], |
788 | 788 | 2 => $this->error_messages['upload_err_form_size'], |