@@ -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,24 +53,24 @@ 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 | - if(is_resource($moduleDir)){ |
|
| 61 | - while(($module = readdir($moduleDir)) !== false){ |
|
| 62 | - if(preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){ |
|
| 60 | + if (is_resource($moduleDir)) { |
|
| 61 | + while (($module = readdir($moduleDir)) !== false) { |
|
| 62 | + if (preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)) { |
|
| 63 | 63 | self::$list[] = $module; |
| 64 | 64 | } |
| 65 | - else{ |
|
| 66 | - $logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name'); |
|
| 65 | + else { |
|
| 66 | + $logger->info('Skipping [' . $module . '], may be this is not a directory or does not exists or is invalid name'); |
|
| 67 | 67 | } |
| 68 | 68 | } |
| 69 | 69 | closedir($moduleDir); |
| 70 | 70 | } |
| 71 | 71 | ksort(self::$list); |
| 72 | 72 | |
| 73 | - if(self::hasModule()){ |
|
| 73 | + if (self::hasModule()) { |
|
| 74 | 74 | $logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']'); |
| 75 | 75 | } |
| 76 | 76 | } |
@@ -79,9 +79,9 @@ discard block |
||
| 79 | 79 | * Get the list of the custom autoload configuration from module if exists |
| 80 | 80 | * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values |
| 81 | 81 | */ |
| 82 | - public static function getModulesAutoloadConfig(){ |
|
| 82 | + public static function getModulesAutoloadConfig() { |
|
| 83 | 83 | $logger = self::getLogger(); |
| 84 | - if(! self::hasModule()){ |
|
| 84 | + if (!self::hasModule()) { |
|
| 85 | 85 | $logger->info('No module was loaded skipping.'); |
| 86 | 86 | return false; |
| 87 | 87 | } |
@@ -94,10 +94,10 @@ discard block |
||
| 94 | 94 | |
| 95 | 95 | foreach (self::$list as $module) { |
| 96 | 96 | $file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php'; |
| 97 | - if(file_exists($file)){ |
|
| 97 | + if (file_exists($file)) { |
|
| 98 | 98 | $autoload = array(); |
| 99 | 99 | require_once $file; |
| 100 | - if(! empty($autoload) && is_array($autoload)){ |
|
| 100 | + if (!empty($autoload) && is_array($autoload)) { |
|
| 101 | 101 | $autoloads = array_merge_recursive($autoloads, $autoload); |
| 102 | 102 | unset($autoload); |
| 103 | 103 | } |
@@ -110,19 +110,19 @@ discard block |
||
| 110 | 110 | * Get the list of the custom routes configuration from module if exists |
| 111 | 111 | * @return array|boolean the routes list or false if no module contains the routes configuration |
| 112 | 112 | */ |
| 113 | - public static function getModulesRoutes(){ |
|
| 113 | + public static function getModulesRoutes() { |
|
| 114 | 114 | $logger = self::getLogger(); |
| 115 | - if(! self::hasModule()){ |
|
| 115 | + if (!self::hasModule()) { |
|
| 116 | 116 | $logger->info('No module was loaded skipping.'); |
| 117 | 117 | return false; |
| 118 | 118 | } |
| 119 | 119 | $routes = array(); |
| 120 | 120 | foreach (self::$list as $module) { |
| 121 | 121 | $file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php'; |
| 122 | - if(file_exists($file)){ |
|
| 122 | + if (file_exists($file)) { |
|
| 123 | 123 | $route = array(); |
| 124 | 124 | require_once $file; |
| 125 | - if(! empty($route) && is_array($route)){ |
|
| 125 | + if (!empty($route) && is_array($route)) { |
|
| 126 | 126 | $routes = array_merge($routes, $route); |
| 127 | 127 | unset($route); |
| 128 | 128 | } |
@@ -138,23 +138,23 @@ discard block |
||
| 138 | 138 | * @param string $module the module name |
| 139 | 139 | * @return boolean|string false or null if no module have this controller, path the full path of the controller |
| 140 | 140 | */ |
| 141 | - public static function findControllerFullPath($class, $module = null){ |
|
| 141 | + public static function findControllerFullPath($class, $module = null) { |
|
| 142 | 142 | $logger = self::getLogger(); |
| 143 | - if(! self::hasModule()){ |
|
| 143 | + if (!self::hasModule()) { |
|
| 144 | 144 | $logger->info('No module was loaded skiping.'); |
| 145 | 145 | return false; |
| 146 | 146 | } |
| 147 | 147 | $class = str_ireplace('.php', '', $class); |
| 148 | 148 | $class = ucfirst($class); |
| 149 | - $classFile = $class.'.php'; |
|
| 150 | - $logger->debug('Checking the controller [' . $class . '] in module [' .$module. '] ...'); |
|
| 149 | + $classFile = $class . '.php'; |
|
| 150 | + $logger->debug('Checking the controller [' . $class . '] in module [' . $module . '] ...'); |
|
| 151 | 151 | $filePath = MODULE_PATH . $module . DS . 'controllers' . DS . $classFile; |
| 152 | - if(file_exists($filePath)){ |
|
| 153 | - $logger->info('Found controller [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 152 | + if (file_exists($filePath)) { |
|
| 153 | + $logger->info('Found controller [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 154 | 154 | return $filePath; |
| 155 | 155 | } |
| 156 | - else{ |
|
| 157 | - $logger->info('Controller [' . $class . '] does not exist in the module [' .$module. ']'); |
|
| 156 | + else { |
|
| 157 | + $logger->info('Controller [' . $class . '] does not exist in the module [' . $module . ']'); |
|
| 158 | 158 | return false; |
| 159 | 159 | } |
| 160 | 160 | } |
@@ -165,23 +165,23 @@ discard block |
||
| 165 | 165 | * @param string $module the module name |
| 166 | 166 | * @return boolean|string false or null if no module have this model, return the full path of this model |
| 167 | 167 | */ |
| 168 | - public static function findModelFullPath($class, $module = null){ |
|
| 168 | + public static function findModelFullPath($class, $module = null) { |
|
| 169 | 169 | $logger = self::getLogger(); |
| 170 | - if(! self::hasModule()){ |
|
| 170 | + if (!self::hasModule()) { |
|
| 171 | 171 | $logger->info('No module was loaded skiping.'); |
| 172 | 172 | return false; |
| 173 | 173 | } |
| 174 | 174 | $class = str_ireplace('.php', '', $class); |
| 175 | 175 | $class = ucfirst($class); |
| 176 | - $classFile = $class.'.php'; |
|
| 177 | - $logger->debug('Checking model [' . $class . '] in module [' .$module. '] ...'); |
|
| 176 | + $classFile = $class . '.php'; |
|
| 177 | + $logger->debug('Checking model [' . $class . '] in module [' . $module . '] ...'); |
|
| 178 | 178 | $filePath = MODULE_PATH . $module . DS . 'models' . DS . $classFile; |
| 179 | - if(file_exists($filePath)){ |
|
| 180 | - $logger->info('Found model [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 179 | + if (file_exists($filePath)) { |
|
| 180 | + $logger->info('Found model [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 181 | 181 | return $filePath; |
| 182 | 182 | } |
| 183 | - else{ |
|
| 184 | - $logger->info('Model [' . $class . '] does not exist in the module [' .$module. ']'); |
|
| 183 | + else { |
|
| 184 | + $logger->info('Model [' . $class . '] does not exist in the module [' . $module . ']'); |
|
| 185 | 185 | return false; |
| 186 | 186 | } |
| 187 | 187 | } |
@@ -192,22 +192,22 @@ discard block |
||
| 192 | 192 | * @param string $module the module name |
| 193 | 193 | * @return boolean|string false or null if no module have this configuration, return the full path of this configuration |
| 194 | 194 | */ |
| 195 | - public static function findConfigFullPath($configuration, $module = null){ |
|
| 195 | + public static function findConfigFullPath($configuration, $module = null) { |
|
| 196 | 196 | $logger = self::getLogger(); |
| 197 | - if(! self::hasModule()){ |
|
| 197 | + if (!self::hasModule()) { |
|
| 198 | 198 | $logger->info('No module was loaded skiping.'); |
| 199 | 199 | return false; |
| 200 | 200 | } |
| 201 | 201 | $configuration = str_ireplace('.php', '', $configuration); |
| 202 | - $file = $configuration.'.php'; |
|
| 203 | - $logger->debug('Checking configuration [' . $configuration . '] in module [' .$module. '] ...'); |
|
| 202 | + $file = $configuration . '.php'; |
|
| 203 | + $logger->debug('Checking configuration [' . $configuration . '] in module [' . $module . '] ...'); |
|
| 204 | 204 | $filePath = MODULE_PATH . $module . DS . 'config' . DS . $file; |
| 205 | - if(file_exists($filePath)){ |
|
| 206 | - $logger->info('Found configuration [' . $configuration . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 205 | + if (file_exists($filePath)) { |
|
| 206 | + $logger->info('Found configuration [' . $configuration . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 207 | 207 | return $filePath; |
| 208 | 208 | } |
| 209 | - else{ |
|
| 210 | - $logger->info('Configuration [' . $configuration . '] does not exist in the module [' .$module. ']'); |
|
| 209 | + else { |
|
| 210 | + $logger->info('Configuration [' . $configuration . '] does not exist in the module [' . $module . ']'); |
|
| 211 | 211 | return false; |
| 212 | 212 | } |
| 213 | 213 | } |
@@ -218,23 +218,23 @@ discard block |
||
| 218 | 218 | * @param string $module the module name |
| 219 | 219 | * @return boolean|string false or null if no module have this helper, return the full path of this helper |
| 220 | 220 | */ |
| 221 | - public static function findFunctionFullPath($helper, $module = null){ |
|
| 221 | + public static function findFunctionFullPath($helper, $module = null) { |
|
| 222 | 222 | $logger = self::getLogger(); |
| 223 | - if(! self::hasModule()){ |
|
| 223 | + if (!self::hasModule()) { |
|
| 224 | 224 | $logger->info('No module was loaded skiping.'); |
| 225 | 225 | return false; |
| 226 | 226 | } |
| 227 | 227 | $helper = str_ireplace('.php', '', $helper); |
| 228 | 228 | $helper = str_ireplace('function_', '', $helper); |
| 229 | - $file = 'function_'.$helper.'.php'; |
|
| 230 | - $logger->debug('Checking helper [' . $helper . '] in module [' .$module. '] ...'); |
|
| 229 | + $file = 'function_' . $helper . '.php'; |
|
| 230 | + $logger->debug('Checking helper [' . $helper . '] in module [' . $module . '] ...'); |
|
| 231 | 231 | $filePath = MODULE_PATH . $module . DS . 'functions' . DS . $file; |
| 232 | - if(file_exists($filePath)){ |
|
| 233 | - $logger->info('Found helper [' . $helper . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 232 | + if (file_exists($filePath)) { |
|
| 233 | + $logger->info('Found helper [' . $helper . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 234 | 234 | return $filePath; |
| 235 | 235 | } |
| 236 | - else{ |
|
| 237 | - $logger->info('Helper [' . $helper . '] does not exist in the module [' .$module. ']'); |
|
| 236 | + else { |
|
| 237 | + $logger->info('Helper [' . $helper . '] does not exist in the module [' . $module . ']'); |
|
| 238 | 238 | return false; |
| 239 | 239 | } |
| 240 | 240 | } |
@@ -246,22 +246,22 @@ discard block |
||
| 246 | 246 | * @param string $module the module name |
| 247 | 247 | * @return boolean|string false or null if no module have this library, return the full path of this library |
| 248 | 248 | */ |
| 249 | - public static function findLibraryFullPath($class, $module = null){ |
|
| 249 | + public static function findLibraryFullPath($class, $module = null) { |
|
| 250 | 250 | $logger = self::getLogger(); |
| 251 | - if(! self::hasModule()){ |
|
| 251 | + if (!self::hasModule()) { |
|
| 252 | 252 | $logger->info('No module was loaded skiping.'); |
| 253 | 253 | return false; |
| 254 | 254 | } |
| 255 | 255 | $class = str_ireplace('.php', '', $class); |
| 256 | - $file = $class.'.php'; |
|
| 257 | - $logger->debug('Checking library [' . $class . '] in module [' .$module. '] ...'); |
|
| 256 | + $file = $class . '.php'; |
|
| 257 | + $logger->debug('Checking library [' . $class . '] in module [' . $module . '] ...'); |
|
| 258 | 258 | $filePath = MODULE_PATH . $module . DS . 'libraries' . DS . $file; |
| 259 | - if(file_exists($filePath)){ |
|
| 260 | - $logger->info('Found library [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 259 | + if (file_exists($filePath)) { |
|
| 260 | + $logger->info('Found library [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 261 | 261 | return $filePath; |
| 262 | 262 | } |
| 263 | - else{ |
|
| 264 | - $logger->info('Library [' . $class . '] does not exist in the module [' .$module. ']'); |
|
| 263 | + else { |
|
| 264 | + $logger->info('Library [' . $class . '] does not exist in the module [' . $module . ']'); |
|
| 265 | 265 | return false; |
| 266 | 266 | } |
| 267 | 267 | } |
@@ -273,9 +273,9 @@ discard block |
||
| 273 | 273 | * @param string $module the module name to check |
| 274 | 274 | * @return boolean|string false or null if no module have this view, path the full path of the view |
| 275 | 275 | */ |
| 276 | - public static function findViewFullPath($view, $module = null){ |
|
| 276 | + public static function findViewFullPath($view, $module = null) { |
|
| 277 | 277 | $logger = self::getLogger(); |
| 278 | - if(! self::hasModule()){ |
|
| 278 | + if (!self::hasModule()) { |
|
| 279 | 279 | $logger->info('No module was loaded skiping.'); |
| 280 | 280 | return false; |
| 281 | 281 | } |
@@ -283,14 +283,14 @@ discard block |
||
| 283 | 283 | $view = trim($view, '/\\'); |
| 284 | 284 | $view = str_ireplace('/', DS, $view); |
| 285 | 285 | $viewFile = $view . '.php'; |
| 286 | - $logger->debug('Checking view [' . $view . '] in module [' .$module. '] ...'); |
|
| 286 | + $logger->debug('Checking view [' . $view . '] in module [' . $module . '] ...'); |
|
| 287 | 287 | $filePath = MODULE_PATH . $module . DS . 'views' . DS . $viewFile; |
| 288 | - if(file_exists($filePath)){ |
|
| 289 | - $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 288 | + if (file_exists($filePath)) { |
|
| 289 | + $logger->info('Found view [' . $view . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 290 | 290 | return $filePath; |
| 291 | 291 | } |
| 292 | - else{ |
|
| 293 | - $logger->info('View [' . $view . '] does not exist in the module [' .$module. ']'); |
|
| 292 | + else { |
|
| 293 | + $logger->info('View [' . $view . '] does not exist in the module [' . $module . ']'); |
|
| 294 | 294 | return false; |
| 295 | 295 | } |
| 296 | 296 | } |
@@ -302,23 +302,23 @@ discard block |
||
| 302 | 302 | * @param string $appLang the application language like 'en', 'fr' |
| 303 | 303 | * @return boolean|string false or null if no module have this language, return the full path of this language |
| 304 | 304 | */ |
| 305 | - public static function findLanguageFullPath($language, $module = null, $appLang){ |
|
| 305 | + public static function findLanguageFullPath($language, $module = null, $appLang) { |
|
| 306 | 306 | $logger = self::getLogger(); |
| 307 | - if(! self::hasModule()){ |
|
| 307 | + if (!self::hasModule()) { |
|
| 308 | 308 | $logger->info('No module was loaded skiping.'); |
| 309 | 309 | return false; |
| 310 | 310 | } |
| 311 | 311 | $language = str_ireplace('.php', '', $language); |
| 312 | 312 | $language = str_ireplace('lang_', '', $language); |
| 313 | - $file = 'lang_'.$language.'.php'; |
|
| 314 | - $logger->debug('Checking language [' . $language . '] in module [' .$module. '] ...'); |
|
| 313 | + $file = 'lang_' . $language . '.php'; |
|
| 314 | + $logger->debug('Checking language [' . $language . '] in module [' . $module . '] ...'); |
|
| 315 | 315 | $filePath = MODULE_PATH . $module . DS . 'lang' . DS . $appLang . DS . $file; |
| 316 | - if(file_exists($filePath)){ |
|
| 317 | - $logger->info('Found language [' . $language . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 316 | + if (file_exists($filePath)) { |
|
| 317 | + $logger->info('Found language [' . $language . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 318 | 318 | return $filePath; |
| 319 | 319 | } |
| 320 | - else{ |
|
| 321 | - $logger->info('Language [' . $language . '] does not exist in the module [' .$module. ']'); |
|
| 320 | + else { |
|
| 321 | + $logger->info('Language [' . $language . '] does not exist in the module [' . $module . ']'); |
|
| 322 | 322 | return false; |
| 323 | 323 | } |
| 324 | 324 | } |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | * Get the list of module loaded |
| 328 | 328 | * @return array the module list |
| 329 | 329 | */ |
| 330 | - public static function getModuleList(){ |
|
| 330 | + public static function getModuleList() { |
|
| 331 | 331 | return self::$list; |
| 332 | 332 | } |
| 333 | 333 | |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | * Check if the application has an module |
| 336 | 336 | * @return boolean |
| 337 | 337 | */ |
| 338 | - public static function hasModule(){ |
|
| 338 | + public static function hasModule() { |
|
| 339 | 339 | return !empty(self::$list); |
| 340 | 340 | } |
| 341 | 341 | |
@@ -61,8 +61,7 @@ discard block |
||
| 61 | 61 | while(($module = readdir($moduleDir)) !== false){ |
| 62 | 62 | if(preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){ |
| 63 | 63 | self::$list[] = $module; |
| 64 | - } |
|
| 65 | - else{ |
|
| 64 | + } else{ |
|
| 66 | 65 | $logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name'); |
| 67 | 66 | } |
| 68 | 67 | } |
@@ -152,8 +151,7 @@ discard block |
||
| 152 | 151 | if(file_exists($filePath)){ |
| 153 | 152 | $logger->info('Found controller [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 154 | 153 | return $filePath; |
| 155 | - } |
|
| 156 | - else{ |
|
| 154 | + } else{ |
|
| 157 | 155 | $logger->info('Controller [' . $class . '] does not exist in the module [' .$module. ']'); |
| 158 | 156 | return false; |
| 159 | 157 | } |
@@ -179,8 +177,7 @@ discard block |
||
| 179 | 177 | if(file_exists($filePath)){ |
| 180 | 178 | $logger->info('Found model [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 181 | 179 | return $filePath; |
| 182 | - } |
|
| 183 | - else{ |
|
| 180 | + } else{ |
|
| 184 | 181 | $logger->info('Model [' . $class . '] does not exist in the module [' .$module. ']'); |
| 185 | 182 | return false; |
| 186 | 183 | } |
@@ -205,8 +202,7 @@ discard block |
||
| 205 | 202 | if(file_exists($filePath)){ |
| 206 | 203 | $logger->info('Found configuration [' . $configuration . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 207 | 204 | return $filePath; |
| 208 | - } |
|
| 209 | - else{ |
|
| 205 | + } else{ |
|
| 210 | 206 | $logger->info('Configuration [' . $configuration . '] does not exist in the module [' .$module. ']'); |
| 211 | 207 | return false; |
| 212 | 208 | } |
@@ -232,8 +228,7 @@ discard block |
||
| 232 | 228 | if(file_exists($filePath)){ |
| 233 | 229 | $logger->info('Found helper [' . $helper . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 234 | 230 | return $filePath; |
| 235 | - } |
|
| 236 | - else{ |
|
| 231 | + } else{ |
|
| 237 | 232 | $logger->info('Helper [' . $helper . '] does not exist in the module [' .$module. ']'); |
| 238 | 233 | return false; |
| 239 | 234 | } |
@@ -259,8 +254,7 @@ discard block |
||
| 259 | 254 | if(file_exists($filePath)){ |
| 260 | 255 | $logger->info('Found library [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 261 | 256 | return $filePath; |
| 262 | - } |
|
| 263 | - else{ |
|
| 257 | + } else{ |
|
| 264 | 258 | $logger->info('Library [' . $class . '] does not exist in the module [' .$module. ']'); |
| 265 | 259 | return false; |
| 266 | 260 | } |
@@ -288,8 +282,7 @@ discard block |
||
| 288 | 282 | if(file_exists($filePath)){ |
| 289 | 283 | $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 290 | 284 | return $filePath; |
| 291 | - } |
|
| 292 | - else{ |
|
| 285 | + } else{ |
|
| 293 | 286 | $logger->info('View [' . $view . '] does not exist in the module [' .$module. ']'); |
| 294 | 287 | return false; |
| 295 | 288 | } |
@@ -316,8 +309,7 @@ discard block |
||
| 316 | 309 | if(file_exists($filePath)){ |
| 317 | 310 | $logger->info('Found language [' . $language . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 318 | 311 | return $filePath; |
| 319 | - } |
|
| 320 | - else{ |
|
| 312 | + } else{ |
|
| 321 | 313 | $logger->info('Language [' . $language . '] does not exist in the module [' .$module. ']'); |
| 322 | 314 | return false; |
| 323 | 315 | } |
@@ -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 | class FileCache implements CacheInterface{ |
| 28 | 28 | |
@@ -44,15 +44,15 @@ discard block |
||
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | - * instance of the Log class |
|
| 48 | - */ |
|
| 49 | - if(is_object($logger)){ |
|
| 50 | - $this->logger = $logger; |
|
| 51 | - } |
|
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | - $this->logger->setLogger('Library::FileCache'); |
|
| 55 | - } |
|
| 47 | + * instance of the Log class |
|
| 48 | + */ |
|
| 49 | + if(is_object($logger)){ |
|
| 50 | + $this->logger = $logger; |
|
| 51 | + } |
|
| 52 | + else{ |
|
| 53 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 54 | + $this->logger->setLogger('Library::FileCache'); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | 58 | if(! extension_loaded('zlib')){ |
@@ -80,26 +80,26 @@ discard block |
||
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
| 83 | - flock($handle, LOCK_SH); |
|
| 84 | - $data = file_get_contents($filePath); |
|
| 85 | - fclose($handle); |
|
| 86 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | - // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | - unlink($filePath); |
|
| 91 | - return false; |
|
| 92 | - } |
|
| 93 | - if (time() > $data['expire']) { |
|
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | - // Unlinking when the file was expired |
|
| 96 | - unlink($filePath); |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | - return $data['data']; |
|
| 102 | - } |
|
| 83 | + flock($handle, LOCK_SH); |
|
| 84 | + $data = file_get_contents($filePath); |
|
| 85 | + fclose($handle); |
|
| 86 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 87 | + if (! $data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 89 | + // If unserializing somehow didn't work out, we'll delete the file |
|
| 90 | + unlink($filePath); |
|
| 91 | + return false; |
|
| 92 | + } |
|
| 93 | + if (time() > $data['expire']) { |
|
| 94 | + $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 95 | + // Unlinking when the file was expired |
|
| 96 | + unlink($filePath); |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + else{ |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | + return $data['data']; |
|
| 102 | + } |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | |
@@ -121,25 +121,25 @@ discard block |
||
| 121 | 121 | } |
| 122 | 122 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
| 123 | 123 | //Serializing along with the TTL |
| 124 | - $cacheData = serialize(array( |
|
| 124 | + $cacheData = serialize(array( |
|
| 125 | 125 | 'mtime' => time(), |
| 126 | 126 | 'expire' => $expire, |
| 127 | 127 | 'data' => $data, |
| 128 | 128 | 'ttl' => $ttl |
| 129 | 129 | ) |
| 130 | 130 | ); |
| 131 | - $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | - fclose($handle); |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | - fclose($handle); |
|
| 131 | + $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
|
| 132 | + if(! $result){ |
|
| 133 | + $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 134 | + fclose($handle); |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + else{ |
|
| 138 | + $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 139 | + fclose($handle); |
|
| 140 | 140 | chmod($filePath, 0640); |
| 141 | 141 | return true; |
| 142 | - } |
|
| 142 | + } |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | } |
| 160 | 160 | else{ |
| 161 | 161 | $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
| 162 | - unlink($filePath); |
|
| 162 | + unlink($filePath); |
|
| 163 | 163 | return true; |
| 164 | 164 | } |
| 165 | 165 | } |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | } |
| 183 | 183 | else{ |
| 184 | 184 | $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
| 185 | - $data = file_get_contents($filePath); |
|
| 185 | + $data = file_get_contents($filePath); |
|
| 186 | 186 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 187 | 187 | if(! $data){ |
| 188 | 188 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
@@ -222,17 +222,17 @@ discard block |
||
| 222 | 222 | foreach ($list as $file) { |
| 223 | 223 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 224 | 224 | $data = file_get_contents($file); |
| 225 | - $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 226 | - if(! $data){ |
|
| 227 | - $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 228 | - } |
|
| 229 | - else if(time() > $data['expire']){ |
|
| 230 | - $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 231 | - unlink($file); |
|
| 232 | - } |
|
| 233 | - else{ |
|
| 234 | - $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 235 | - } |
|
| 225 | + $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
|
| 226 | + if(! $data){ |
|
| 227 | + $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
|
| 228 | + } |
|
| 229 | + else if(time() > $data['expire']){ |
|
| 230 | + $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
|
| 231 | + unlink($file); |
|
| 232 | + } |
|
| 233 | + else{ |
|
| 234 | + $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
|
| 235 | + } |
|
| 236 | 236 | } |
| 237 | 237 | } |
| 238 | 238 | } |
@@ -255,19 +255,19 @@ discard block |
||
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * @return boolean |
|
| 260 | - */ |
|
| 261 | - public function isCompressCacheData(){ |
|
| 262 | - return $this->compressCacheData; |
|
| 263 | - } |
|
| 258 | + /** |
|
| 259 | + * @return boolean |
|
| 260 | + */ |
|
| 261 | + public function isCompressCacheData(){ |
|
| 262 | + return $this->compressCacheData; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * @param boolean $compressCacheData |
|
| 267 | - * |
|
| 268 | - * @return object |
|
| 269 | - */ |
|
| 270 | - public function setCompressCacheData($status = true){ |
|
| 265 | + /** |
|
| 266 | + * @param boolean $compressCacheData |
|
| 267 | + * |
|
| 268 | + * @return object |
|
| 269 | + */ |
|
| 270 | + public function setCompressCacheData($status = true){ |
|
| 271 | 271 | //if Zlib extension is not loaded set compressCacheData to false |
| 272 | 272 | if($status === true && ! extension_loaded('zlib')){ |
| 273 | 273 | |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | $this->compressCacheData = $status; |
| 279 | 279 | } |
| 280 | 280 | return $this; |
| 281 | - } |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | 283 | /** |
| 284 | 284 | * Check whether the cache feature for the handle is supported |
@@ -290,28 +290,28 @@ discard block |
||
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | /** |
| 293 | - * Return the Log instance |
|
| 294 | - * @return object |
|
| 295 | - */ |
|
| 296 | - public function getLogger(){ |
|
| 297 | - return $this->logger; |
|
| 298 | - } |
|
| 293 | + * Return the Log instance |
|
| 294 | + * @return object |
|
| 295 | + */ |
|
| 296 | + public function getLogger(){ |
|
| 297 | + return $this->logger; |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - /** |
|
| 301 | - * Set the log instance |
|
| 302 | - * @param Log $logger the log object |
|
| 303 | - */ |
|
| 304 | - public function setLogger(Log $logger){ |
|
| 305 | - $this->logger = $logger; |
|
| 306 | - return $this; |
|
| 307 | - } |
|
| 300 | + /** |
|
| 301 | + * Set the log instance |
|
| 302 | + * @param Log $logger the log object |
|
| 303 | + */ |
|
| 304 | + public function setLogger(Log $logger){ |
|
| 305 | + $this->logger = $logger; |
|
| 306 | + return $this; |
|
| 307 | + } |
|
| 308 | 308 | |
| 309 | 309 | /** |
| 310 | - * Get the cache file full path for the given key |
|
| 311 | - * |
|
| 312 | - * @param $key the cache item key |
|
| 313 | - * @return string |
|
| 314 | - */ |
|
| 310 | + * Get the cache file full path for the given key |
|
| 311 | + * |
|
| 312 | + * @param $key the cache item key |
|
| 313 | + * @return string |
|
| 314 | + */ |
|
| 315 | 315 | private function getFilePath($key){ |
| 316 | 316 | return CACHE_PATH . md5($key) . '.cache'; |
| 317 | 317 | } |
@@ -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 FileCache implements CacheInterface{ |
|
| 27 | + class FileCache implements CacheInterface { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Whether to enable compression of the cache data file. |
@@ -39,23 +39,23 @@ discard block |
||
| 39 | 39 | private $logger; |
| 40 | 40 | |
| 41 | 41 | |
| 42 | - public function __construct(Log $logger = null){ |
|
| 43 | - if(! $this->isSupported()){ |
|
| 42 | + public function __construct(Log $logger = null) { |
|
| 43 | + if (!$this->isSupported()) { |
|
| 44 | 44 | show_error('The cache for file system is not available. Check the cache directory if is exists or is writable.'); |
| 45 | 45 | } |
| 46 | 46 | /** |
| 47 | 47 | * instance of the Log class |
| 48 | 48 | */ |
| 49 | - if(is_object($logger)){ |
|
| 49 | + if (is_object($logger)) { |
|
| 50 | 50 | $this->logger = $logger; |
| 51 | 51 | } |
| 52 | - else{ |
|
| 53 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 52 | + else { |
|
| 53 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 54 | 54 | $this->logger->setLogger('Library::FileCache'); |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | //if Zlib extension is not loaded set compressCacheData to false |
| 58 | - if(! extension_loaded('zlib')){ |
|
| 58 | + if (!extension_loaded('zlib')) { |
|
| 59 | 59 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 60 | 60 | $this->compressCacheData = false; |
| 61 | 61 | } |
@@ -66,17 +66,17 @@ discard block |
||
| 66 | 66 | * @param string $key the key to identify the cache data |
| 67 | 67 | * @return mixed the cache data if exists else return false |
| 68 | 68 | */ |
| 69 | - public function get($key){ |
|
| 70 | - $this->logger->debug('Getting cache data for key ['. $key .']'); |
|
| 69 | + public function get($key) { |
|
| 70 | + $this->logger->debug('Getting cache data for key [' . $key . ']'); |
|
| 71 | 71 | $filePath = $this->getFilePath($key); |
| 72 | - if(! file_exists($filePath)){ |
|
| 73 | - $this->logger->info('No cache file found for the key ['. $key .'], return false'); |
|
| 72 | + if (!file_exists($filePath)) { |
|
| 73 | + $this->logger->info('No cache file found for the key [' . $key . '], return false'); |
|
| 74 | 74 | return false; |
| 75 | 75 | } |
| 76 | - $this->logger->info('The cache file [' .$filePath. '] for the key ['. $key .'] exists, check if the cache data is valid'); |
|
| 77 | - $handle = fopen($filePath,'r'); |
|
| 78 | - if(! is_resource($handle)){ |
|
| 79 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 76 | + $this->logger->info('The cache file [' . $filePath . '] for the key [' . $key . '] exists, check if the cache data is valid'); |
|
| 77 | + $handle = fopen($filePath, 'r'); |
|
| 78 | + if (!is_resource($handle)) { |
|
| 79 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 80 | 80 | return false; |
| 81 | 81 | } |
| 82 | 82 | // Getting a shared lock |
@@ -84,20 +84,20 @@ discard block |
||
| 84 | 84 | $data = file_get_contents($filePath); |
| 85 | 85 | fclose($handle); |
| 86 | 86 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 87 | - if (! $data) { |
|
| 88 | - $this->logger->error('Can not unserialize the cache data for the key ['. $key .'], return false'); |
|
| 87 | + if (!$data) { |
|
| 88 | + $this->logger->error('Can not unserialize the cache data for the key [' . $key . '], return false'); |
|
| 89 | 89 | // If unserializing somehow didn't work out, we'll delete the file |
| 90 | 90 | unlink($filePath); |
| 91 | 91 | return false; |
| 92 | 92 | } |
| 93 | 93 | if (time() > $data['expire']) { |
| 94 | - $this->logger->info('The cache data for the key ['. $key .'] already expired delete the cache file [' .$filePath. ']'); |
|
| 94 | + $this->logger->info('The cache data for the key [' . $key . '] already expired delete the cache file [' . $filePath . ']'); |
|
| 95 | 95 | // Unlinking when the file was expired |
| 96 | 96 | unlink($filePath); |
| 97 | 97 | return false; |
| 98 | 98 | } |
| 99 | - else{ |
|
| 100 | - $this->logger->info('The cache not yet expire, now return the cache data for key ['. $key .'], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 99 | + else { |
|
| 100 | + $this->logger->info('The cache not yet expire, now return the cache data for key [' . $key . '], the cache will expire at [' . date('Y-m-d H:i:s', $data['expire']) . ']'); |
|
| 101 | 101 | return $data['data']; |
| 102 | 102 | } |
| 103 | 103 | } |
@@ -110,13 +110,13 @@ discard block |
||
| 110 | 110 | * @param integer $ttl the cache life time |
| 111 | 111 | * @return boolean true if success otherwise will return false |
| 112 | 112 | */ |
| 113 | - public function set($key, $data, $ttl = 0){ |
|
| 113 | + public function set($key, $data, $ttl = 0) { |
|
| 114 | 114 | $expire = time() + $ttl; |
| 115 | - $this->logger->debug('Setting cache data for key ['. $key .'], time to live [' .$ttl. '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 115 | + $this->logger->debug('Setting cache data for key [' . $key . '], time to live [' . $ttl . '], expire at [' . date('Y-m-d H:i:s', $expire) . ']'); |
|
| 116 | 116 | $filePath = $this->getFilePath($key); |
| 117 | - $handle = fopen($filePath,'w'); |
|
| 118 | - if(! is_resource($handle)){ |
|
| 119 | - $this->logger->error('Can not open the file cache [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 117 | + $handle = fopen($filePath, 'w'); |
|
| 118 | + if (!is_resource($handle)) { |
|
| 119 | + $this->logger->error('Can not open the file cache [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 120 | 120 | return false; |
| 121 | 121 | } |
| 122 | 122 | flock($handle, LOCK_EX); // exclusive lock, will get released when the file is closed |
@@ -129,13 +129,13 @@ discard block |
||
| 129 | 129 | ) |
| 130 | 130 | ); |
| 131 | 131 | $result = fwrite($handle, $this->compressCacheData ? gzdeflate($cacheData, 9) : $cacheData); |
| 132 | - if(! $result){ |
|
| 133 | - $this->logger->error('Can not write cache data into file [' .$filePath. '] for the key ['. $key .'], return false'); |
|
| 132 | + if (!$result) { |
|
| 133 | + $this->logger->error('Can not write cache data into file [' . $filePath . '] for the key [' . $key . '], return false'); |
|
| 134 | 134 | fclose($handle); |
| 135 | 135 | return false; |
| 136 | 136 | } |
| 137 | - else{ |
|
| 138 | - $this->logger->info('Cache data saved into file [' .$filePath. '] for the key ['. $key .']'); |
|
| 137 | + else { |
|
| 138 | + $this->logger->info('Cache data saved into file [' . $filePath . '] for the key [' . $key . ']'); |
|
| 139 | 139 | fclose($handle); |
| 140 | 140 | chmod($filePath, 0640); |
| 141 | 141 | return true; |
@@ -149,16 +149,16 @@ discard block |
||
| 149 | 149 | * @return boolean true if the cache is delete, false if can't delete |
| 150 | 150 | * the cache or the cache with the given key not exist |
| 151 | 151 | */ |
| 152 | - public function delete($key){ |
|
| 153 | - $this->logger->debug('Deleting of cache data for key [' .$key. ']'); |
|
| 152 | + public function delete($key) { |
|
| 153 | + $this->logger->debug('Deleting of cache data for key [' . $key . ']'); |
|
| 154 | 154 | $filePath = $this->getFilePath($key); |
| 155 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 156 | - if(! file_exists($filePath)){ |
|
| 155 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 156 | + if (!file_exists($filePath)) { |
|
| 157 | 157 | $this->logger->info('This cache file does not exists skipping'); |
| 158 | 158 | return false; |
| 159 | 159 | } |
| 160 | - else{ |
|
| 161 | - $this->logger->info('Found cache file [' .$filePath. '] remove it'); |
|
| 160 | + else { |
|
| 161 | + $this->logger->info('Found cache file [' . $filePath . '] remove it'); |
|
| 162 | 162 | unlink($filePath); |
| 163 | 163 | return true; |
| 164 | 164 | } |
@@ -172,25 +172,25 @@ discard block |
||
| 172 | 172 | * 'expire' => expiration time of the cache (Unix timestamp), |
| 173 | 173 | * 'ttl' => the time to live of the cache in second |
| 174 | 174 | */ |
| 175 | - public function getInfo($key){ |
|
| 176 | - $this->logger->debug('Getting of cache info for key [' .$key. ']'); |
|
| 175 | + public function getInfo($key) { |
|
| 176 | + $this->logger->debug('Getting of cache info for key [' . $key . ']'); |
|
| 177 | 177 | $filePath = $this->getFilePath($key); |
| 178 | - $this->logger->info('The file path for the key [' .$key. '] is [' .$filePath. ']'); |
|
| 179 | - if(! file_exists($filePath)){ |
|
| 178 | + $this->logger->info('The file path for the key [' . $key . '] is [' . $filePath . ']'); |
|
| 179 | + if (!file_exists($filePath)) { |
|
| 180 | 180 | $this->logger->info('This cache file does not exists skipping'); |
| 181 | 181 | return false; |
| 182 | 182 | } |
| 183 | - else{ |
|
| 184 | - $this->logger->info('Found cache file [' .$filePath. '] check the validity'); |
|
| 183 | + else { |
|
| 184 | + $this->logger->info('Found cache file [' . $filePath . '] check the validity'); |
|
| 185 | 185 | $data = file_get_contents($filePath); |
| 186 | 186 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 187 | - if(! $data){ |
|
| 187 | + if (!$data) { |
|
| 188 | 188 | $this->logger->warning('Can not unserialize the cache data for file [' . $filePath . ']'); |
| 189 | 189 | return false; |
| 190 | 190 | } |
| 191 | - else{ |
|
| 191 | + else { |
|
| 192 | 192 | $this->logger->info('This cache data is OK check for expire'); |
| 193 | - if(isset($data['expire']) && $data['expire'] > time()){ |
|
| 193 | + if (isset($data['expire']) && $data['expire'] > time()) { |
|
| 194 | 194 | $this->logger->info('This cache not yet expired return cache informations'); |
| 195 | 195 | $info = array( |
| 196 | 196 | 'mtime' => $data['mtime'], |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | ); |
| 200 | 200 | return $info; |
| 201 | 201 | } |
| 202 | - else{ |
|
| 202 | + else { |
|
| 203 | 203 | $this->logger->info('This cache already expired return false'); |
| 204 | 204 | return false; |
| 205 | 205 | } |
@@ -211,26 +211,26 @@ discard block |
||
| 211 | 211 | /** |
| 212 | 212 | * Used to delete expired cache data |
| 213 | 213 | */ |
| 214 | - public function deleteExpiredCache(){ |
|
| 214 | + public function deleteExpiredCache() { |
|
| 215 | 215 | $this->logger->debug('Deleting of expired cache files'); |
| 216 | 216 | $list = glob(CACHE_PATH . '*.cache'); |
| 217 | - if(! $list){ |
|
| 217 | + if (!$list) { |
|
| 218 | 218 | $this->logger->info('No cache files were found skipping'); |
| 219 | 219 | } |
| 220 | - else{ |
|
| 220 | + else { |
|
| 221 | 221 | $this->logger->info('Found [' . count($list) . '] cache files to remove if expired'); |
| 222 | 222 | foreach ($list as $file) { |
| 223 | 223 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
| 224 | 224 | $data = file_get_contents($file); |
| 225 | 225 | $data = @unserialize($this->compressCacheData ? gzinflate($data) : $data); |
| 226 | - if(! $data){ |
|
| 226 | + if (!$data) { |
|
| 227 | 227 | $this->logger->warning('Can not unserialize the cache data for file [' . $file . ']'); |
| 228 | 228 | } |
| 229 | - else if(time() > $data['expire']){ |
|
| 229 | + else if (time() > $data['expire']) { |
|
| 230 | 230 | $this->logger->info('The cache data for file [' . $file . '] already expired remove it'); |
| 231 | 231 | unlink($file); |
| 232 | 232 | } |
| 233 | - else{ |
|
| 233 | + else { |
|
| 234 | 234 | $this->logger->info('The cache data for file [' . $file . '] not yet expired skip it'); |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -240,13 +240,13 @@ discard block |
||
| 240 | 240 | /** |
| 241 | 241 | * Remove all file from cache folder |
| 242 | 242 | */ |
| 243 | - public function clean(){ |
|
| 243 | + public function clean() { |
|
| 244 | 244 | $this->logger->debug('Deleting of all cache files'); |
| 245 | 245 | $list = glob(CACHE_PATH . '*.cache'); |
| 246 | - if(! $list){ |
|
| 246 | + if (!$list) { |
|
| 247 | 247 | $this->logger->info('No cache files were found skipping'); |
| 248 | 248 | } |
| 249 | - else{ |
|
| 249 | + else { |
|
| 250 | 250 | $this->logger->info('Found [' . count($list) . '] cache files to remove'); |
| 251 | 251 | foreach ($list as $file) { |
| 252 | 252 | $this->logger->debug('Processing the cache file [' . $file . ']'); |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | /** |
| 259 | 259 | * @return boolean |
| 260 | 260 | */ |
| 261 | - public function isCompressCacheData(){ |
|
| 261 | + public function isCompressCacheData() { |
|
| 262 | 262 | return $this->compressCacheData; |
| 263 | 263 | } |
| 264 | 264 | |
@@ -267,14 +267,14 @@ discard block |
||
| 267 | 267 | * |
| 268 | 268 | * @return object |
| 269 | 269 | */ |
| 270 | - public function setCompressCacheData($status = true){ |
|
| 270 | + public function setCompressCacheData($status = true) { |
|
| 271 | 271 | //if Zlib extension is not loaded set compressCacheData to false |
| 272 | - if($status === true && ! extension_loaded('zlib')){ |
|
| 272 | + if ($status === true && !extension_loaded('zlib')) { |
|
| 273 | 273 | |
| 274 | 274 | $this->logger->warning('The zlib extension is not loaded set cache compress data to FALSE'); |
| 275 | 275 | $this->compressCacheData = false; |
| 276 | 276 | } |
| 277 | - else{ |
|
| 277 | + else { |
|
| 278 | 278 | $this->compressCacheData = $status; |
| 279 | 279 | } |
| 280 | 280 | return $this; |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | * |
| 286 | 286 | * @return bool |
| 287 | 287 | */ |
| 288 | - public function isSupported(){ |
|
| 288 | + public function isSupported() { |
|
| 289 | 289 | return CACHE_PATH && is_dir(CACHE_PATH) && is_writable(CACHE_PATH); |
| 290 | 290 | } |
| 291 | 291 | |
@@ -293,7 +293,7 @@ discard block |
||
| 293 | 293 | * Return the Log instance |
| 294 | 294 | * @return object |
| 295 | 295 | */ |
| 296 | - public function getLogger(){ |
|
| 296 | + public function getLogger() { |
|
| 297 | 297 | return $this->logger; |
| 298 | 298 | } |
| 299 | 299 | |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * Set the log instance |
| 302 | 302 | * @param Log $logger the log object |
| 303 | 303 | */ |
| 304 | - public function setLogger(Log $logger){ |
|
| 304 | + public function setLogger(Log $logger) { |
|
| 305 | 305 | $this->logger = $logger; |
| 306 | 306 | return $this; |
| 307 | 307 | } |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | * @param $key the cache item key |
| 313 | 313 | * @return string |
| 314 | 314 | */ |
| 315 | - private function getFilePath($key){ |
|
| 315 | + private function getFilePath($key) { |
|
| 316 | 316 | return CACHE_PATH . md5($key) . '.cache'; |
| 317 | 317 | } |
| 318 | 318 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | * along with this program; if not, write to the Free Software |
| 24 | 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | 25 | */ |
| 26 | - class Loader{ |
|
| 26 | + class Loader { |
|
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * List of loaded resources |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | private static $logger; |
| 39 | 39 | |
| 40 | 40 | |
| 41 | - public function __construct(){ |
|
| 41 | + public function __construct() { |
|
| 42 | 42 | //add the resources already loaded during application bootstrap |
| 43 | 43 | //in the list to prevent duplicate or loading the resources again. |
| 44 | 44 | static::$loaded = class_loaded(); |
@@ -51,9 +51,9 @@ discard block |
||
| 51 | 51 | * Get the logger singleton instance |
| 52 | 52 | * @return Log the logger instance |
| 53 | 53 | */ |
| 54 | - private static function getLogger(){ |
|
| 55 | - if(self::$logger == null){ |
|
| 56 | - self::$logger[0] =& class_loader('Log', 'classes'); |
|
| 54 | + private static function getLogger() { |
|
| 55 | + if (self::$logger == null) { |
|
| 56 | + self::$logger[0] = & class_loader('Log', 'classes'); |
|
| 57 | 57 | self::$logger[0]->setLogger('Library::Loader'); |
| 58 | 58 | } |
| 59 | 59 | return self::$logger[0]; |
@@ -67,25 +67,25 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return void |
| 69 | 69 | */ |
| 70 | - public static function model($class, $instance = null){ |
|
| 70 | + public static function model($class, $instance = null) { |
|
| 71 | 71 | $logger = static::getLogger(); |
| 72 | 72 | $class = str_ireplace('.php', '', $class); |
| 73 | 73 | $class = trim($class, '/\\'); |
| 74 | - $file = ucfirst($class).'.php'; |
|
| 74 | + $file = ucfirst($class) . '.php'; |
|
| 75 | 75 | $logger->debug('Loading model [' . $class . '] ...'); |
| 76 | - if(! $instance){ |
|
| 76 | + if (!$instance) { |
|
| 77 | 77 | //for module |
| 78 | - if(strpos($class, '/') !== false){ |
|
| 78 | + if (strpos($class, '/') !== false) { |
|
| 79 | 79 | $path = explode('/', $class); |
| 80 | - if(isset($path[1])){ |
|
| 80 | + if (isset($path[1])) { |
|
| 81 | 81 | $instance = strtolower($path[1]); |
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | - else{ |
|
| 84 | + else { |
|
| 85 | 85 | $instance = strtolower($class); |
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | - if(isset(static::$loaded[$instance])){ |
|
| 88 | + if (isset(static::$loaded[$instance])) { |
|
| 89 | 89 | $logger->info('Model [' . $class . '] already loaded no need to load it again, cost in performance'); |
| 90 | 90 | return; |
| 91 | 91 | } |
@@ -95,43 +95,43 @@ discard block |
||
| 95 | 95 | $searchModuleName = null; |
| 96 | 96 | $obj = & get_instance(); |
| 97 | 97 | //check if the request class contains module name |
| 98 | - if(strpos($class, '/') !== false){ |
|
| 98 | + if (strpos($class, '/') !== false) { |
|
| 99 | 99 | $path = explode('/', $class); |
| 100 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 100 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 101 | 101 | $searchModuleName = $path[0]; |
| 102 | 102 | $class = ucfirst($path[1]); |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | - else{ |
|
| 105 | + else { |
|
| 106 | 106 | $class = ucfirst($class); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 109 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 110 | 110 | $searchModuleName = $obj->moduleName; |
| 111 | 111 | } |
| 112 | 112 | $moduleModelFilePath = Module::findModelFullPath($class, $searchModuleName); |
| 113 | - if($moduleModelFilePath){ |
|
| 114 | - $logger->info('Found model [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleModelFilePath. '] we will used it'); |
|
| 113 | + if ($moduleModelFilePath) { |
|
| 114 | + $logger->info('Found model [' . $class . '] from module [' . $searchModuleName . '], the file path is [' . $moduleModelFilePath . '] we will used it'); |
|
| 115 | 115 | $classFilePath = $moduleModelFilePath; |
| 116 | 116 | } |
| 117 | - else{ |
|
| 117 | + else { |
|
| 118 | 118 | $logger->info('Cannot find model [' . $class . '] from modules using the default location'); |
| 119 | 119 | } |
| 120 | 120 | $logger->info('The model file path to be loaded is [' . $classFilePath . ']'); |
| 121 | - if(file_exists($classFilePath)){ |
|
| 121 | + if (file_exists($classFilePath)) { |
|
| 122 | 122 | require_once $classFilePath; |
| 123 | - if(class_exists($class)){ |
|
| 123 | + if (class_exists($class)) { |
|
| 124 | 124 | $c = new $class(); |
| 125 | 125 | $obj = & get_instance(); |
| 126 | 126 | $obj->{$instance} = $c; |
| 127 | 127 | static::$loaded[$instance] = $class; |
| 128 | 128 | $logger->info('Model [' . $class . '] --> ' . $classFilePath . ' loaded successfully.'); |
| 129 | 129 | } |
| 130 | - else{ |
|
| 131 | - show_error('The file '.$classFilePath.' exists but does not contain the class ['. $class . ']'); |
|
| 130 | + else { |
|
| 131 | + show_error('The file ' . $classFilePath . ' exists but does not contain the class [' . $class . ']'); |
|
| 132 | 132 | } |
| 133 | 133 | } |
| 134 | - else{ |
|
| 134 | + else { |
|
| 135 | 135 | show_error('Unable to find the model [' . $class . ']'); |
| 136 | 136 | } |
| 137 | 137 | } |
@@ -146,31 +146,31 @@ discard block |
||
| 146 | 146 | * |
| 147 | 147 | * @return void |
| 148 | 148 | */ |
| 149 | - public static function library($class, $instance = null, array $params = array()){ |
|
| 149 | + public static function library($class, $instance = null, array $params = array()) { |
|
| 150 | 150 | $logger = static::getLogger(); |
| 151 | 151 | $class = str_ireplace('.php', '', $class); |
| 152 | 152 | $class = trim($class, '/\\'); |
| 153 | - $file = ucfirst($class) .'.php'; |
|
| 153 | + $file = ucfirst($class) . '.php'; |
|
| 154 | 154 | $logger->debug('Loading library [' . $class . '] ...'); |
| 155 | - if(! $instance){ |
|
| 155 | + if (!$instance) { |
|
| 156 | 156 | //for module |
| 157 | - if(strpos($class, '/') !== false){ |
|
| 157 | + if (strpos($class, '/') !== false) { |
|
| 158 | 158 | $path = explode('/', $class); |
| 159 | - if(isset($path[1])){ |
|
| 159 | + if (isset($path[1])) { |
|
| 160 | 160 | $instance = strtolower($path[1]); |
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | - else{ |
|
| 163 | + else { |
|
| 164 | 164 | $instance = strtolower($class); |
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | - if(isset(static::$loaded[$instance])){ |
|
| 167 | + if (isset(static::$loaded[$instance])) { |
|
| 168 | 168 | $logger->info('Library [' . $class . '] already loaded no need to load it again, cost in performance'); |
| 169 | 169 | return; |
| 170 | 170 | } |
| 171 | 171 | $obj = & get_instance(); |
| 172 | 172 | //TODO for Database library |
| 173 | - if(strtolower($class) == 'database'){ |
|
| 173 | + if (strtolower($class) == 'database') { |
|
| 174 | 174 | $logger->info('This is the Database library ...'); |
| 175 | 175 | $dbInstance = & class_loader('Database', 'classes', $params); |
| 176 | 176 | $obj->{$instance} = $dbInstance; |
@@ -180,44 +180,44 @@ discard block |
||
| 180 | 180 | } |
| 181 | 181 | $libraryFilePath = null; |
| 182 | 182 | $logger->debug('Check if this is a system library ...'); |
| 183 | - if(file_exists(CORE_LIBRARY_PATH . $file)){ |
|
| 183 | + if (file_exists(CORE_LIBRARY_PATH . $file)) { |
|
| 184 | 184 | $libraryFilePath = CORE_LIBRARY_PATH . $file; |
| 185 | 185 | $class = ucfirst($class); |
| 186 | 186 | $logger->info('This library is a system library'); |
| 187 | 187 | } |
| 188 | - else{ |
|
| 188 | + else { |
|
| 189 | 189 | $logger->info('This library is not a system library'); |
| 190 | 190 | //first check if this library is in the module |
| 191 | 191 | $logger->debug('Checking library [' . $class . '] from module list ...'); |
| 192 | 192 | $searchModuleName = null; |
| 193 | 193 | //check if the request class contains module name |
| 194 | - if(strpos($class, '/') !== false){ |
|
| 194 | + if (strpos($class, '/') !== false) { |
|
| 195 | 195 | $path = explode('/', $class); |
| 196 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 196 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 197 | 197 | $searchModuleName = $path[0]; |
| 198 | 198 | $class = ucfirst($path[1]); |
| 199 | 199 | } |
| 200 | 200 | } |
| 201 | - else{ |
|
| 201 | + else { |
|
| 202 | 202 | $class = ucfirst($class); |
| 203 | 203 | } |
| 204 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 204 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 205 | 205 | $searchModuleName = $obj->moduleName; |
| 206 | 206 | } |
| 207 | 207 | $moduleLibraryPath = Module::findLibraryFullPath($class, $searchModuleName); |
| 208 | - if($moduleLibraryPath){ |
|
| 209 | - $logger->info('Found library [' . $class . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLibraryPath. '] we will used it'); |
|
| 208 | + if ($moduleLibraryPath) { |
|
| 209 | + $logger->info('Found library [' . $class . '] from module [' . $searchModuleName . '], the file path is [' . $moduleLibraryPath . '] we will used it'); |
|
| 210 | 210 | $libraryFilePath = $moduleLibraryPath; |
| 211 | 211 | } |
| 212 | - else{ |
|
| 212 | + else { |
|
| 213 | 213 | $logger->info('Cannot find library [' . $class . '] from modules using the default location'); |
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | - if(! $libraryFilePath){ |
|
| 216 | + if (!$libraryFilePath) { |
|
| 217 | 217 | $searchDir = array(LIBRARY_PATH); |
| 218 | - foreach($searchDir as $dir){ |
|
| 218 | + foreach ($searchDir as $dir) { |
|
| 219 | 219 | $filePath = $dir . $file; |
| 220 | - if(file_exists($filePath)){ |
|
| 220 | + if (file_exists($filePath)) { |
|
| 221 | 221 | $libraryFilePath = $filePath; |
| 222 | 222 | //is already found not to continue |
| 223 | 223 | break; |
@@ -225,20 +225,20 @@ discard block |
||
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | $logger->info('The library file path to be loaded is [' . $libraryFilePath . ']'); |
| 228 | - if($libraryFilePath){ |
|
| 228 | + if ($libraryFilePath) { |
|
| 229 | 229 | require_once $libraryFilePath; |
| 230 | - if(class_exists($class)){ |
|
| 230 | + if (class_exists($class)) { |
|
| 231 | 231 | $c = $params ? new $class($params) : new $class(); |
| 232 | 232 | $obj = & get_instance(); |
| 233 | 233 | $obj->{$instance} = $c; |
| 234 | 234 | static::$loaded[$instance] = $class; |
| 235 | 235 | $logger->info('Library [' . $class . '] --> ' . $libraryFilePath . ' loaded successfully.'); |
| 236 | 236 | } |
| 237 | - else{ |
|
| 238 | - show_error('The file '.$libraryFilePath.' exists but does not contain the class '.$class); |
|
| 237 | + else { |
|
| 238 | + show_error('The file ' . $libraryFilePath . ' exists but does not contain the class ' . $class); |
|
| 239 | 239 | } |
| 240 | 240 | } |
| 241 | - else{ |
|
| 241 | + else { |
|
| 242 | 242 | show_error('Unable to find library class [' . $class . ']'); |
| 243 | 243 | } |
| 244 | 244 | } |
@@ -250,14 +250,14 @@ discard block |
||
| 250 | 250 | * |
| 251 | 251 | * @return void |
| 252 | 252 | */ |
| 253 | - public static function functions($function){ |
|
| 253 | + public static function functions($function) { |
|
| 254 | 254 | $logger = static::getLogger(); |
| 255 | 255 | $function = str_ireplace('.php', '', $function); |
| 256 | 256 | $function = trim($function, '/\\'); |
| 257 | 257 | $function = str_ireplace('function_', '', $function); |
| 258 | - $file = 'function_'.$function.'.php'; |
|
| 258 | + $file = 'function_' . $function . '.php'; |
|
| 259 | 259 | $logger->debug('Loading helper [' . $function . '] ...'); |
| 260 | - if(isset(static::$loaded['function_' . $function])){ |
|
| 260 | + if (isset(static::$loaded['function_' . $function])) { |
|
| 261 | 261 | $logger->info('Helper [' . $function . '] already loaded no need to load it again, cost in performance'); |
| 262 | 262 | return; |
| 263 | 263 | } |
@@ -267,30 +267,30 @@ discard block |
||
| 267 | 267 | $searchModuleName = null; |
| 268 | 268 | $obj = & get_instance(); |
| 269 | 269 | //check if the request class contains module name |
| 270 | - if(strpos($function, '/') !== false){ |
|
| 270 | + if (strpos($function, '/') !== false) { |
|
| 271 | 271 | $path = explode('/', $function); |
| 272 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 272 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 273 | 273 | $searchModuleName = $path[0]; |
| 274 | 274 | $function = 'function_' . $path[1] . '.php'; |
| 275 | - $file = $path[0] . DS . 'function_'.$function.'.php'; |
|
| 275 | + $file = $path[0] . DS . 'function_' . $function . '.php'; |
|
| 276 | 276 | } |
| 277 | 277 | } |
| 278 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 278 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 279 | 279 | $searchModuleName = $obj->moduleName; |
| 280 | 280 | } |
| 281 | 281 | $moduleFunctionPath = Module::findFunctionFullPath($function, $searchModuleName); |
| 282 | - if($moduleFunctionPath){ |
|
| 283 | - $logger->info('Found helper [' . $function . '] from module [' .$searchModuleName. '], the file path is [' .$moduleFunctionPath. '] we will used it'); |
|
| 282 | + if ($moduleFunctionPath) { |
|
| 283 | + $logger->info('Found helper [' . $function . '] from module [' . $searchModuleName . '], the file path is [' . $moduleFunctionPath . '] we will used it'); |
|
| 284 | 284 | $functionFilePath = $moduleFunctionPath; |
| 285 | 285 | } |
| 286 | - else{ |
|
| 286 | + else { |
|
| 287 | 287 | $logger->info('Cannot find helper [' . $function . '] from modules using the default location'); |
| 288 | 288 | } |
| 289 | - if(! $functionFilePath){ |
|
| 289 | + if (!$functionFilePath) { |
|
| 290 | 290 | $searchDir = array(FUNCTIONS_PATH, CORE_FUNCTIONS_PATH); |
| 291 | - foreach($searchDir as $dir){ |
|
| 291 | + foreach ($searchDir as $dir) { |
|
| 292 | 292 | $filePath = $dir . $file; |
| 293 | - if(file_exists($filePath)){ |
|
| 293 | + if (file_exists($filePath)) { |
|
| 294 | 294 | $functionFilePath = $filePath; |
| 295 | 295 | //is already found not to continue |
| 296 | 296 | break; |
@@ -298,12 +298,12 @@ discard block |
||
| 298 | 298 | } |
| 299 | 299 | } |
| 300 | 300 | $logger->info('The helper file path to be loaded is [' . $functionFilePath . ']'); |
| 301 | - if($functionFilePath){ |
|
| 301 | + if ($functionFilePath) { |
|
| 302 | 302 | require_once $functionFilePath; |
| 303 | 303 | static::$loaded['function_' . $function] = $functionFilePath; |
| 304 | 304 | $logger->info('Helper [' . $function . '] --> ' . $functionFilePath . ' loaded successfully.'); |
| 305 | 305 | } |
| 306 | - else{ |
|
| 306 | + else { |
|
| 307 | 307 | show_error('Unable to find helper file [' . $file . ']'); |
| 308 | 308 | } |
| 309 | 309 | } |
@@ -315,14 +315,14 @@ discard block |
||
| 315 | 315 | * |
| 316 | 316 | * @return void |
| 317 | 317 | */ |
| 318 | - public static function config($filename){ |
|
| 318 | + public static function config($filename) { |
|
| 319 | 319 | $logger = static::getLogger(); |
| 320 | 320 | $filename = str_ireplace('.php', '', $filename); |
| 321 | 321 | $filename = trim($filename, '/\\'); |
| 322 | 322 | $filename = str_ireplace('config_', '', $filename); |
| 323 | - $file = 'config_'.$filename.'.php'; |
|
| 323 | + $file = 'config_' . $filename . '.php'; |
|
| 324 | 324 | $logger->debug('Loading configuration [' . $filename . '] ...'); |
| 325 | - if(isset(static::$loaded['config_' . $filename])){ |
|
| 325 | + if (isset(static::$loaded['config_' . $filename])) { |
|
| 326 | 326 | $logger->info('Configuration [' . $file . '] already loaded no need to load it again, cost in performance'); |
| 327 | 327 | return; |
| 328 | 328 | } |
@@ -332,34 +332,34 @@ discard block |
||
| 332 | 332 | $searchModuleName = null; |
| 333 | 333 | $obj = & get_instance(); |
| 334 | 334 | //check if the request class contains module name |
| 335 | - if(strpos($filename, '/') !== false){ |
|
| 335 | + if (strpos($filename, '/') !== false) { |
|
| 336 | 336 | $path = explode('/', $filename); |
| 337 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 337 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 338 | 338 | $searchModuleName = $path[0]; |
| 339 | 339 | $filename = $path[1] . '.php'; |
| 340 | 340 | } |
| 341 | 341 | } |
| 342 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 342 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 343 | 343 | $searchModuleName = $obj->moduleName; |
| 344 | 344 | } |
| 345 | 345 | $moduleConfigPath = Module::findConfigFullPath($filename, $searchModuleName); |
| 346 | - if($moduleConfigPath){ |
|
| 347 | - $logger->info('Found config [' . $filename . '] from module [' .$searchModuleName. '], the file path is [' .$moduleConfigPath. '] we will used it'); |
|
| 346 | + if ($moduleConfigPath) { |
|
| 347 | + $logger->info('Found config [' . $filename . '] from module [' . $searchModuleName . '], the file path is [' . $moduleConfigPath . '] we will used it'); |
|
| 348 | 348 | $configFilePath = $moduleConfigPath; |
| 349 | 349 | } |
| 350 | - else{ |
|
| 350 | + else { |
|
| 351 | 351 | $logger->info('Cannot find config [' . $filename . '] from modules using the default location'); |
| 352 | 352 | } |
| 353 | 353 | $logger->info('The config file path to be loaded is [' . $configFilePath . ']'); |
| 354 | 354 | $config = array(); |
| 355 | - if(file_exists($configFilePath)){ |
|
| 355 | + if (file_exists($configFilePath)) { |
|
| 356 | 356 | require_once $configFilePath; |
| 357 | - if(! empty($config) && is_array($config)){ |
|
| 357 | + if (!empty($config) && is_array($config)) { |
|
| 358 | 358 | Config::setAll($config); |
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | - else{ |
|
| 362 | - show_error('Unable to find config file ['. $configFilePath . ']'); |
|
| 361 | + else { |
|
| 362 | + show_error('Unable to find config file [' . $configFilePath . ']'); |
|
| 363 | 363 | } |
| 364 | 364 | static::$loaded['config_' . $filename] = $configFilePath; |
| 365 | 365 | $logger->info('Configuration [' . $configFilePath . '] loaded successfully.'); |
@@ -375,14 +375,14 @@ discard block |
||
| 375 | 375 | * |
| 376 | 376 | * @return void |
| 377 | 377 | */ |
| 378 | - public static function lang($language){ |
|
| 378 | + public static function lang($language) { |
|
| 379 | 379 | $logger = static::getLogger(); |
| 380 | 380 | $language = str_ireplace('.php', '', $language); |
| 381 | 381 | $language = trim($language, '/\\'); |
| 382 | 382 | $language = str_ireplace('lang_', '', $language); |
| 383 | - $file = 'lang_'.$language.'.php'; |
|
| 383 | + $file = 'lang_' . $language . '.php'; |
|
| 384 | 384 | $logger->debug('Loading language [' . $language . '] ...'); |
| 385 | - if(isset(static::$loaded['lang_' . $language])){ |
|
| 385 | + if (isset(static::$loaded['lang_' . $language])) { |
|
| 386 | 386 | $logger->info('Language [' . $language . '] already loaded no need to load it again, cost in performance'); |
| 387 | 387 | return; |
| 388 | 388 | } |
@@ -392,7 +392,7 @@ discard block |
||
| 392 | 392 | $cfgKey = get_config('language_cookie_name'); |
| 393 | 393 | $objCookie = & class_loader('Cookie'); |
| 394 | 394 | $cookieLang = $objCookie->get($cfgKey); |
| 395 | - if($cookieLang){ |
|
| 395 | + if ($cookieLang) { |
|
| 396 | 396 | $appLang = $cookieLang; |
| 397 | 397 | } |
| 398 | 398 | $languageFilePath = null; |
@@ -401,30 +401,30 @@ discard block |
||
| 401 | 401 | $searchModuleName = null; |
| 402 | 402 | $obj = & get_instance(); |
| 403 | 403 | //check if the request class contains module name |
| 404 | - if(strpos($language, '/') !== false){ |
|
| 404 | + if (strpos($language, '/') !== false) { |
|
| 405 | 405 | $path = explode('/', $language); |
| 406 | - if(isset($path[0]) && in_array($path[0], Module::getModuleList())){ |
|
| 406 | + if (isset($path[0]) && in_array($path[0], Module::getModuleList())) { |
|
| 407 | 407 | $searchModuleName = $path[0]; |
| 408 | 408 | $language = 'lang_' . $path[1] . '.php'; |
| 409 | - $file = $path[0] . DS .$language; |
|
| 409 | + $file = $path[0] . DS . $language; |
|
| 410 | 410 | } |
| 411 | 411 | } |
| 412 | - if(! $searchModuleName && !empty($obj->moduleName)){ |
|
| 412 | + if (!$searchModuleName && !empty($obj->moduleName)) { |
|
| 413 | 413 | $searchModuleName = $obj->moduleName; |
| 414 | 414 | } |
| 415 | 415 | $moduleLanguagePath = Module::findLanguageFullPath($language, $searchModuleName, $appLang); |
| 416 | - if($moduleLanguagePath){ |
|
| 417 | - $logger->info('Found language [' . $language . '] from module [' .$searchModuleName. '], the file path is [' .$moduleLanguagePath. '] we will used it'); |
|
| 416 | + if ($moduleLanguagePath) { |
|
| 417 | + $logger->info('Found language [' . $language . '] from module [' . $searchModuleName . '], the file path is [' . $moduleLanguagePath . '] we will used it'); |
|
| 418 | 418 | $languageFilePath = $moduleLanguagePath; |
| 419 | 419 | } |
| 420 | - else{ |
|
| 420 | + else { |
|
| 421 | 421 | $logger->info('Cannot find language [' . $language . '] from modules using the default location'); |
| 422 | 422 | } |
| 423 | - if(! $languageFilePath){ |
|
| 423 | + if (!$languageFilePath) { |
|
| 424 | 424 | $searchDir = array(APP_LANG_PATH, CORE_LANG_PATH); |
| 425 | - foreach($searchDir as $dir){ |
|
| 425 | + foreach ($searchDir as $dir) { |
|
| 426 | 426 | $filePath = $dir . $appLang . DS . $file; |
| 427 | - if(file_exists($filePath)){ |
|
| 427 | + if (file_exists($filePath)) { |
|
| 428 | 428 | $languageFilePath = $filePath; |
| 429 | 429 | //is already found not to continue |
| 430 | 430 | break; |
@@ -432,13 +432,13 @@ discard block |
||
| 432 | 432 | } |
| 433 | 433 | } |
| 434 | 434 | $logger->info('The language file path to be loaded is [' . $languageFilePath . ']'); |
| 435 | - if($languageFilePath){ |
|
| 435 | + if ($languageFilePath) { |
|
| 436 | 436 | $lang = array(); |
| 437 | 437 | require_once $languageFilePath; |
| 438 | - if(! empty($lang) && is_array($lang)){ |
|
| 439 | - $logger->info('Language file [' .$languageFilePath. '] contains the valid languages keys add them to language list'); |
|
| 438 | + if (!empty($lang) && is_array($lang)) { |
|
| 439 | + $logger->info('Language file [' . $languageFilePath . '] contains the valid languages keys add them to language list'); |
|
| 440 | 440 | //Note: may be here the class 'Lang' not yet loaded |
| 441 | - $langObj =& class_loader('Lang', 'classes'); |
|
| 441 | + $langObj = & class_loader('Lang', 'classes'); |
|
| 442 | 442 | $langObj->addLangMessages($lang); |
| 443 | 443 | //free the memory |
| 444 | 444 | unset($lang); |
@@ -446,13 +446,13 @@ discard block |
||
| 446 | 446 | static::$loaded['lang_' . $language] = $languageFilePath; |
| 447 | 447 | $logger->info('Language [' . $language . '] --> ' . $languageFilePath . ' loaded successfully.'); |
| 448 | 448 | } |
| 449 | - else{ |
|
| 449 | + else { |
|
| 450 | 450 | show_error('Unable to find language file [' . $file . ']'); |
| 451 | 451 | } |
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | |
| 455 | - private function getResourcesFromAutoloadConfig(){ |
|
| 455 | + private function getResourcesFromAutoloadConfig() { |
|
| 456 | 456 | $autoloads = array(); |
| 457 | 457 | $autoloads['config'] = array(); |
| 458 | 458 | $autoloads['languages'] = array(); |
@@ -460,17 +460,17 @@ discard block |
||
| 460 | 460 | $autoloads['models'] = array(); |
| 461 | 461 | $autoloads['functions'] = array(); |
| 462 | 462 | //loading of the resources in autoload.php configuration file |
| 463 | - if(file_exists(CONFIG_PATH . 'autoload.php')){ |
|
| 463 | + if (file_exists(CONFIG_PATH . 'autoload.php')) { |
|
| 464 | 464 | $autoload = array(); |
| 465 | 465 | require_once CONFIG_PATH . 'autoload.php'; |
| 466 | - if(! empty($autoload) && is_array($autoload)){ |
|
| 466 | + if (!empty($autoload) && is_array($autoload)) { |
|
| 467 | 467 | $autoloads = array_merge($autoloads, $autoload); |
| 468 | 468 | unset($autoload); |
| 469 | 469 | } |
| 470 | 470 | } |
| 471 | 471 | //loading autoload configuration for modules |
| 472 | 472 | $modulesAutoloads = Module::getModulesAutoloadConfig(); |
| 473 | - if(! empty($modulesAutoloads) && is_array($modulesAutoloads)){ |
|
| 473 | + if (!empty($modulesAutoloads) && is_array($modulesAutoloads)) { |
|
| 474 | 474 | $autoloads = array_merge_recursive($autoloads, $modulesAutoloads); |
| 475 | 475 | } |
| 476 | 476 | return $autoloads; |
@@ -480,7 +480,7 @@ discard block |
||
| 480 | 480 | * Load the autoload configuration |
| 481 | 481 | * @return void |
| 482 | 482 | */ |
| 483 | - private function loadResourcesFromAutoloadConfig(){ |
|
| 483 | + private function loadResourcesFromAutoloadConfig() { |
|
| 484 | 484 | $autoloads = array(); |
| 485 | 485 | $autoloads['config'] = array(); |
| 486 | 486 | $autoloads['languages'] = array(); |
@@ -513,7 +513,7 @@ discard block |
||
| 513 | 513 | * @param array $resources the resource to load |
| 514 | 514 | * @return void |
| 515 | 515 | */ |
| 516 | - private function loadAutoloadResourcesArray($method, array $resources){ |
|
| 516 | + private function loadAutoloadResourcesArray($method, array $resources) { |
|
| 517 | 517 | foreach ($resources as $name) { |
| 518 | 518 | $this->{$method}($name); |
| 519 | 519 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || exit('Access denied'); |
|
| 2 | + defined('ROOT_PATH') || exit('Access denied'); |
|
| 3 | 3 | /** |
| 4 | 4 | * TNH Framework |
| 5 | 5 | * |
@@ -22,156 +22,156 @@ 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 | - class Pagination{ |
|
| 27 | + class Pagination{ |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * The list of loaded config |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - private $config = array(); |
|
| 30 | + * The list of loaded config |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + private $config = array(); |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Create an instance of pagination |
|
| 37 | - * @param array $overwriteConfig the list of configuration to overwrite the defined configuration in config_pagination.php |
|
| 38 | - */ |
|
| 39 | - public function __construct($overwriteConfig = array()){ |
|
| 40 | - if(file_exists(CONFIG_PATH . 'config_pagination.php')){ |
|
| 41 | - $config = array(); |
|
| 42 | - require_once CONFIG_PATH . 'config_pagination.php'; |
|
| 43 | - if(empty($config) || ! is_array($config)){ |
|
| 44 | - show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
|
| 45 | - } |
|
| 35 | + /** |
|
| 36 | + * Create an instance of pagination |
|
| 37 | + * @param array $overwriteConfig the list of configuration to overwrite the defined configuration in config_pagination.php |
|
| 38 | + */ |
|
| 39 | + public function __construct($overwriteConfig = array()){ |
|
| 40 | + if(file_exists(CONFIG_PATH . 'config_pagination.php')){ |
|
| 41 | + $config = array(); |
|
| 42 | + require_once CONFIG_PATH . 'config_pagination.php'; |
|
| 43 | + if(empty($config) || ! is_array($config)){ |
|
| 44 | + show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
|
| 45 | + } |
|
| 46 | 46 | else{ |
| 47 | 47 | if(! empty($overwriteConfig)){ |
| 48 | 48 | $config = array_merge($config, $overwriteConfig); |
| 49 | 49 | } |
| 50 | 50 | $this->config = $config; |
| 51 | - //put it gobally |
|
| 51 | + //put it gobally |
|
| 52 | 52 | Config::setAll($config); |
| 53 | 53 | unset($config); |
| 54 | 54 | } |
| 55 | - } |
|
| 56 | - else{ |
|
| 57 | - show_error('Unable to find the pagination configuration file'); |
|
| 58 | - } |
|
| 59 | - } |
|
| 55 | + } |
|
| 56 | + else{ |
|
| 57 | + show_error('Unable to find the pagination configuration file'); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Set the pagination custom configuration to overwrite the default configuration in |
|
| 64 | - * config_pagination.php |
|
| 65 | - * @param array $config the configuration to set |
|
| 66 | - */ |
|
| 67 | - public function setConfig(array $config = array()){ |
|
| 68 | - if(! empty($config)){ |
|
| 69 | - $this->config = array_merge($this->config, $config); |
|
| 70 | - Config::setAll($config); |
|
| 71 | - } |
|
| 72 | - } |
|
| 62 | + /** |
|
| 63 | + * Set the pagination custom configuration to overwrite the default configuration in |
|
| 64 | + * config_pagination.php |
|
| 65 | + * @param array $config the configuration to set |
|
| 66 | + */ |
|
| 67 | + public function setConfig(array $config = array()){ |
|
| 68 | + if(! empty($config)){ |
|
| 69 | + $this->config = array_merge($this->config, $config); |
|
| 70 | + Config::setAll($config); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Generate the pagination link |
|
| 76 | - * @param int $totalRows the total number of data |
|
| 77 | - * @param int $currentPageNumber the current page number |
|
| 78 | - * @return string the pagination link |
|
| 79 | - */ |
|
| 80 | - public function getLink($totalRows, $currentPageNumber){ |
|
| 81 | - $pageQueryName = $this->config['page_query_string_name']; |
|
| 82 | - $numberOfLink = $this->config['nb_link']; |
|
| 74 | + /** |
|
| 75 | + * Generate the pagination link |
|
| 76 | + * @param int $totalRows the total number of data |
|
| 77 | + * @param int $currentPageNumber the current page number |
|
| 78 | + * @return string the pagination link |
|
| 79 | + */ |
|
| 80 | + public function getLink($totalRows, $currentPageNumber){ |
|
| 81 | + $pageQueryName = $this->config['page_query_string_name']; |
|
| 82 | + $numberOfLink = $this->config['nb_link']; |
|
| 83 | 83 | $numberOfRowPerPage = $this->config['pagination_per_page']; |
| 84 | - $queryString = Url::queryString(); |
|
| 85 | - $currentUrl = Url::current(); |
|
| 86 | - if($queryString == ''){ |
|
| 87 | - $query = '?' . $pageQueryName . '='; |
|
| 88 | - } |
|
| 89 | - else{ |
|
| 90 | - $tab = explode($pageQueryName . '=', $queryString); |
|
| 91 | - $nb = count($tab); |
|
| 92 | - if($nb == 1){ |
|
| 93 | - $query = '?' . $queryString . '&' . $pageQueryName . '='; |
|
| 94 | - } |
|
| 95 | - else{ |
|
| 96 | - if($tab[0] == ''){ |
|
| 97 | - $query = '?' . $pageQueryName . '='; |
|
| 98 | - } |
|
| 99 | - else{ |
|
| 100 | - $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - $temp = explode('?', $currentUrl); |
|
| 105 | - $query = $temp[0] . $query; |
|
| 106 | - $navbar = ''; |
|
| 107 | - $numberOfPage = ceil($totalRows / $numberOfRowPerPage); |
|
| 108 | - $currentPageNumber = (int) $currentPageNumber; |
|
| 84 | + $queryString = Url::queryString(); |
|
| 85 | + $currentUrl = Url::current(); |
|
| 86 | + if($queryString == ''){ |
|
| 87 | + $query = '?' . $pageQueryName . '='; |
|
| 88 | + } |
|
| 89 | + else{ |
|
| 90 | + $tab = explode($pageQueryName . '=', $queryString); |
|
| 91 | + $nb = count($tab); |
|
| 92 | + if($nb == 1){ |
|
| 93 | + $query = '?' . $queryString . '&' . $pageQueryName . '='; |
|
| 94 | + } |
|
| 95 | + else{ |
|
| 96 | + if($tab[0] == ''){ |
|
| 97 | + $query = '?' . $pageQueryName . '='; |
|
| 98 | + } |
|
| 99 | + else{ |
|
| 100 | + $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + $temp = explode('?', $currentUrl); |
|
| 105 | + $query = $temp[0] . $query; |
|
| 106 | + $navbar = ''; |
|
| 107 | + $numberOfPage = ceil($totalRows / $numberOfRowPerPage); |
|
| 108 | + $currentPageNumber = (int) $currentPageNumber; |
|
| 109 | 109 | if($currentPageNumber <= 0){ |
| 110 | 110 | $currentPageNumber = 1; |
| 111 | 111 | } |
| 112 | - if($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
| 113 | - ){ |
|
| 114 | - return $navbar; |
|
| 115 | - } |
|
| 116 | - if($numberOfLink % 2 == 0){ |
|
| 117 | - $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
|
| 118 | - $end = $currentPageNumber + ($numberOfLink / 2); |
|
| 119 | - } |
|
| 120 | - else{ |
|
| 121 | - $start = $currentPageNumber - floor($numberOfLink / 2); |
|
| 122 | - $end = $currentPageNumber + floor($numberOfLink / 2); |
|
| 123 | - } |
|
| 124 | - if($start <= 1){ |
|
| 125 | - $begin = 1; |
|
| 126 | - $end = $numberOfLink; |
|
| 127 | - } |
|
| 128 | - else if($start > 1 && $end < $numberOfPage){ |
|
| 129 | - $begin = $start; |
|
| 130 | - $end = $end; |
|
| 131 | - } |
|
| 132 | - else{ |
|
| 133 | - $begin = ($numberOfPage - $numberOfLink) + 1; |
|
| 134 | - $end = $numberOfPage; |
|
| 135 | - } |
|
| 136 | - if($numberOfPage <= $numberOfLink){ |
|
| 137 | - $begin = 1; |
|
| 138 | - $end = $numberOfPage; |
|
| 139 | - } |
|
| 140 | - if($currentPageNumber == 1){ |
|
| 141 | - for($i = $begin; $i <= $end; $i++){ |
|
| 142 | - if($i == $currentPageNumber){ |
|
| 143 | - $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
| 144 | - } |
|
| 145 | - else{ |
|
| 146 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close']; |
|
| 150 | - } |
|
| 151 | - else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
| 152 | - $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
|
| 153 | - for($i = $begin; $i <= $end; $i++){ |
|
| 154 | - if($i == $currentPageNumber){ |
|
| 155 | - $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
| 156 | - } |
|
| 157 | - else{ |
|
| 158 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
|
| 162 | - } |
|
| 163 | - else if($currentPageNumber == $numberOfPage){ |
|
| 164 | - $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
|
| 165 | - for($i = $begin; $i <= $end; $i++){ |
|
| 166 | - if($i == $currentPageNumber){ |
|
| 167 | - $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
| 168 | - } |
|
| 169 | - else{ |
|
| 170 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close']; |
|
| 175 | - return $navbar; |
|
| 176 | - } |
|
| 177 | - } |
|
| 112 | + if($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
| 113 | + ){ |
|
| 114 | + return $navbar; |
|
| 115 | + } |
|
| 116 | + if($numberOfLink % 2 == 0){ |
|
| 117 | + $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
|
| 118 | + $end = $currentPageNumber + ($numberOfLink / 2); |
|
| 119 | + } |
|
| 120 | + else{ |
|
| 121 | + $start = $currentPageNumber - floor($numberOfLink / 2); |
|
| 122 | + $end = $currentPageNumber + floor($numberOfLink / 2); |
|
| 123 | + } |
|
| 124 | + if($start <= 1){ |
|
| 125 | + $begin = 1; |
|
| 126 | + $end = $numberOfLink; |
|
| 127 | + } |
|
| 128 | + else if($start > 1 && $end < $numberOfPage){ |
|
| 129 | + $begin = $start; |
|
| 130 | + $end = $end; |
|
| 131 | + } |
|
| 132 | + else{ |
|
| 133 | + $begin = ($numberOfPage - $numberOfLink) + 1; |
|
| 134 | + $end = $numberOfPage; |
|
| 135 | + } |
|
| 136 | + if($numberOfPage <= $numberOfLink){ |
|
| 137 | + $begin = 1; |
|
| 138 | + $end = $numberOfPage; |
|
| 139 | + } |
|
| 140 | + if($currentPageNumber == 1){ |
|
| 141 | + for($i = $begin; $i <= $end; $i++){ |
|
| 142 | + if($i == $currentPageNumber){ |
|
| 143 | + $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
| 144 | + } |
|
| 145 | + else{ |
|
| 146 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close']; |
|
| 150 | + } |
|
| 151 | + else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
| 152 | + $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
|
| 153 | + for($i = $begin; $i <= $end; $i++){ |
|
| 154 | + if($i == $currentPageNumber){ |
|
| 155 | + $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
| 156 | + } |
|
| 157 | + else{ |
|
| 158 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
|
| 162 | + } |
|
| 163 | + else if($currentPageNumber == $numberOfPage){ |
|
| 164 | + $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
|
| 165 | + for($i = $begin; $i <= $end; $i++){ |
|
| 166 | + if($i == $currentPageNumber){ |
|
| 167 | + $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
|
| 168 | + } |
|
| 169 | + else{ |
|
| 170 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + $navbar = $this->config['pagination_open'] . $navbar . $this->config['pagination_close']; |
|
| 175 | + return $navbar; |
|
| 176 | + } |
|
| 177 | + } |
|
@@ -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 Pagination{ |
|
| 27 | + class Pagination { |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * The list of loaded config |
@@ -36,15 +36,15 @@ discard block |
||
| 36 | 36 | * Create an instance of pagination |
| 37 | 37 | * @param array $overwriteConfig the list of configuration to overwrite the defined configuration in config_pagination.php |
| 38 | 38 | */ |
| 39 | - public function __construct($overwriteConfig = array()){ |
|
| 40 | - if(file_exists(CONFIG_PATH . 'config_pagination.php')){ |
|
| 39 | + public function __construct($overwriteConfig = array()) { |
|
| 40 | + if (file_exists(CONFIG_PATH . 'config_pagination.php')) { |
|
| 41 | 41 | $config = array(); |
| 42 | 42 | require_once CONFIG_PATH . 'config_pagination.php'; |
| 43 | - if(empty($config) || ! is_array($config)){ |
|
| 43 | + if (empty($config) || !is_array($config)) { |
|
| 44 | 44 | show_error('No configuration found in ' . CONFIG_PATH . 'config_pagination.php'); |
| 45 | 45 | } |
| 46 | - else{ |
|
| 47 | - if(! empty($overwriteConfig)){ |
|
| 46 | + else { |
|
| 47 | + if (!empty($overwriteConfig)) { |
|
| 48 | 48 | $config = array_merge($config, $overwriteConfig); |
| 49 | 49 | } |
| 50 | 50 | $this->config = $config; |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | unset($config); |
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | - else{ |
|
| 56 | + else { |
|
| 57 | 57 | show_error('Unable to find the pagination configuration file'); |
| 58 | 58 | } |
| 59 | 59 | } |
@@ -64,8 +64,8 @@ discard block |
||
| 64 | 64 | * config_pagination.php |
| 65 | 65 | * @param array $config the configuration to set |
| 66 | 66 | */ |
| 67 | - public function setConfig(array $config = array()){ |
|
| 68 | - if(! empty($config)){ |
|
| 67 | + public function setConfig(array $config = array()) { |
|
| 68 | + if (!empty($config)) { |
|
| 69 | 69 | $this->config = array_merge($this->config, $config); |
| 70 | 70 | Config::setAll($config); |
| 71 | 71 | } |
@@ -77,26 +77,26 @@ discard block |
||
| 77 | 77 | * @param int $currentPageNumber the current page number |
| 78 | 78 | * @return string the pagination link |
| 79 | 79 | */ |
| 80 | - public function getLink($totalRows, $currentPageNumber){ |
|
| 80 | + public function getLink($totalRows, $currentPageNumber) { |
|
| 81 | 81 | $pageQueryName = $this->config['page_query_string_name']; |
| 82 | 82 | $numberOfLink = $this->config['nb_link']; |
| 83 | 83 | $numberOfRowPerPage = $this->config['pagination_per_page']; |
| 84 | 84 | $queryString = Url::queryString(); |
| 85 | 85 | $currentUrl = Url::current(); |
| 86 | - if($queryString == ''){ |
|
| 86 | + if ($queryString == '') { |
|
| 87 | 87 | $query = '?' . $pageQueryName . '='; |
| 88 | 88 | } |
| 89 | - else{ |
|
| 89 | + else { |
|
| 90 | 90 | $tab = explode($pageQueryName . '=', $queryString); |
| 91 | 91 | $nb = count($tab); |
| 92 | - if($nb == 1){ |
|
| 92 | + if ($nb == 1) { |
|
| 93 | 93 | $query = '?' . $queryString . '&' . $pageQueryName . '='; |
| 94 | 94 | } |
| 95 | - else{ |
|
| 96 | - if($tab[0] == ''){ |
|
| 95 | + else { |
|
| 96 | + if ($tab[0] == '') { |
|
| 97 | 97 | $query = '?' . $pageQueryName . '='; |
| 98 | 98 | } |
| 99 | - else{ |
|
| 99 | + else { |
|
| 100 | 100 | $query = '?' . $tab[0] . '' . $pageQueryName . '='; |
| 101 | 101 | } |
| 102 | 102 | } |
@@ -106,67 +106,67 @@ discard block |
||
| 106 | 106 | $navbar = ''; |
| 107 | 107 | $numberOfPage = ceil($totalRows / $numberOfRowPerPage); |
| 108 | 108 | $currentPageNumber = (int) $currentPageNumber; |
| 109 | - if($currentPageNumber <= 0){ |
|
| 109 | + if ($currentPageNumber <= 0) { |
|
| 110 | 110 | $currentPageNumber = 1; |
| 111 | 111 | } |
| 112 | - if($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
| 113 | - ){ |
|
| 112 | + if ($numberOfPage <= 1 || $numberOfLink <= 0 || $numberOfRowPerPage <= 0 || !is_numeric($numberOfLink) || !is_numeric($numberOfRowPerPage) |
|
| 113 | + ) { |
|
| 114 | 114 | return $navbar; |
| 115 | 115 | } |
| 116 | - if($numberOfLink % 2 == 0){ |
|
| 116 | + if ($numberOfLink % 2 == 0) { |
|
| 117 | 117 | $start = $currentPageNumber - ($numberOfLink / 2) + 1; |
| 118 | 118 | $end = $currentPageNumber + ($numberOfLink / 2); |
| 119 | 119 | } |
| 120 | - else{ |
|
| 120 | + else { |
|
| 121 | 121 | $start = $currentPageNumber - floor($numberOfLink / 2); |
| 122 | 122 | $end = $currentPageNumber + floor($numberOfLink / 2); |
| 123 | 123 | } |
| 124 | - if($start <= 1){ |
|
| 124 | + if ($start <= 1) { |
|
| 125 | 125 | $begin = 1; |
| 126 | 126 | $end = $numberOfLink; |
| 127 | 127 | } |
| 128 | - else if($start > 1 && $end < $numberOfPage){ |
|
| 128 | + else if ($start > 1 && $end < $numberOfPage) { |
|
| 129 | 129 | $begin = $start; |
| 130 | 130 | $end = $end; |
| 131 | 131 | } |
| 132 | - else{ |
|
| 132 | + else { |
|
| 133 | 133 | $begin = ($numberOfPage - $numberOfLink) + 1; |
| 134 | 134 | $end = $numberOfPage; |
| 135 | 135 | } |
| 136 | - if($numberOfPage <= $numberOfLink){ |
|
| 136 | + if ($numberOfPage <= $numberOfLink) { |
|
| 137 | 137 | $begin = 1; |
| 138 | 138 | $end = $numberOfPage; |
| 139 | 139 | } |
| 140 | - if($currentPageNumber == 1){ |
|
| 141 | - for($i = $begin; $i <= $end; $i++){ |
|
| 142 | - if($i == $currentPageNumber){ |
|
| 140 | + if ($currentPageNumber == 1) { |
|
| 141 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 142 | + if ($i == $currentPageNumber) { |
|
| 143 | 143 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 144 | 144 | } |
| 145 | - else{ |
|
| 145 | + else { |
|
| 146 | 146 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '" ' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | $navbar .= $this->config['next_open'] . '<a href="' . $query . ($currentPageNumber + 1) . '">' . $this->config['next_text'] . '</a>' . $this->config['next_close']; |
| 150 | 150 | } |
| 151 | - else if($currentPageNumber > 1 && $currentPageNumber < $numberOfPage){ |
|
| 151 | + else if ($currentPageNumber > 1 && $currentPageNumber < $numberOfPage) { |
|
| 152 | 152 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
| 153 | - for($i = $begin; $i <= $end; $i++){ |
|
| 154 | - if($i == $currentPageNumber){ |
|
| 153 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 154 | + if ($i == $currentPageNumber) { |
|
| 155 | 155 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 156 | 156 | } |
| 157 | - else{ |
|
| 158 | - $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i .'</a>' . $this->config['digit_close']; |
|
| 157 | + else { |
|
| 158 | + $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
|
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | - $navbar .= $this->config['next_open']."<a href='$query".($currentPageNumber + 1)."'>".$this->config['next_text']."</a>".$this->config['next_close']; |
|
| 161 | + $navbar .= $this->config['next_open'] . "<a href='$query" . ($currentPageNumber + 1) . "'>" . $this->config['next_text'] . "</a>" . $this->config['next_close']; |
|
| 162 | 162 | } |
| 163 | - else if($currentPageNumber == $numberOfPage){ |
|
| 163 | + else if ($currentPageNumber == $numberOfPage) { |
|
| 164 | 164 | $navbar .= $this->config['previous_open'] . '<a href="' . $query . ($currentPageNumber - 1) . '">' . $this->config['previous_text'] . '</a>' . $this->config['previous_close']; |
| 165 | - for($i = $begin; $i <= $end; $i++){ |
|
| 166 | - if($i == $currentPageNumber){ |
|
| 165 | + for ($i = $begin; $i <= $end; $i++) { |
|
| 166 | + if ($i == $currentPageNumber) { |
|
| 167 | 167 | $navbar .= $this->config['active_link_open'] . $currentPageNumber . $this->config['active_link_close']; |
| 168 | 168 | } |
| 169 | - else{ |
|
| 169 | + else { |
|
| 170 | 170 | $navbar .= $this->config['digit_open'] . '<a href="' . $query . $i . '"' . attributes_to_string($this->config['attributes']) . '>' . $i . '</a>' . $this->config['digit_close']; |
| 171 | 171 | } |
| 172 | 172 | } |
@@ -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 |
| 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 | } |
@@ -1,922 +1,922 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') || 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 | - class FormValidation{ |
|
| 2 | + defined('ROOT_PATH') || 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 | + class FormValidation{ |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * The form validation status |
|
| 32 | - * @var boolean |
|
| 33 | - */ |
|
| 34 | - protected $_success = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The list of errors messages |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - protected $_errorsMessages = array(); |
|
| 30 | + /** |
|
| 31 | + * The form validation status |
|
| 32 | + * @var boolean |
|
| 33 | + */ |
|
| 34 | + protected $_success = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The list of errors messages |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + protected $_errorsMessages = array(); |
|
| 41 | 41 | |
| 42 | - // Array of rule sets, fieldName => PIPE seperated ruleString |
|
| 43 | - protected $_rules = array(); |
|
| 42 | + // Array of rule sets, fieldName => PIPE seperated ruleString |
|
| 43 | + protected $_rules = array(); |
|
| 44 | 44 | |
| 45 | - // Array of errors, niceName => Error Message |
|
| 46 | - protected $_errors = array(); |
|
| 45 | + // Array of errors, niceName => Error Message |
|
| 46 | + protected $_errors = array(); |
|
| 47 | 47 | |
| 48 | - // Array of post Key => Nice name labels |
|
| 49 | - protected $_labels = array(); |
|
| 48 | + // Array of post Key => Nice name labels |
|
| 49 | + protected $_labels = array(); |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * The errors delimiters |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The each error delimiter |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
| 51 | + /** |
|
| 52 | + * The errors delimiters |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The each error delimiter |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | - * Indicated if need force the validation to be failed |
|
| 65 | - * @var boolean |
|
| 66 | - */ |
|
| 67 | - protected $_forceFail = false; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The list of the error messages overrides by the original |
|
| 71 | - * @var array |
|
| 72 | - */ |
|
| 73 | - protected $_errorPhraseOverrides = array(); |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * The logger instance |
|
| 77 | - * @var Log |
|
| 78 | - */ |
|
| 79 | - private $logger; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * The data to be validated, the default is to use $_POST |
|
| 83 | - * @var array |
|
| 84 | - */ |
|
| 85 | - private $data = array(); |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Whether to check the CSRF. This attribute is just a way to allow custom change of the |
|
| 64 | + * Indicated if need force the validation to be failed |
|
| 65 | + * @var boolean |
|
| 66 | + */ |
|
| 67 | + protected $_forceFail = false; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The list of the error messages overrides by the original |
|
| 71 | + * @var array |
|
| 72 | + */ |
|
| 73 | + protected $_errorPhraseOverrides = array(); |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * The logger instance |
|
| 77 | + * @var Log |
|
| 78 | + */ |
|
| 79 | + private $logger; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * The data to be validated, the default is to use $_POST |
|
| 83 | + * @var array |
|
| 84 | + */ |
|
| 85 | + private $data = array(); |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Whether to check the CSRF. This attribute is just a way to allow custom change of the |
|
| 89 | 89 | * CSRF global configuration |
| 90 | 90 | * |
| 91 | - * @var boolean |
|
| 92 | - */ |
|
| 93 | - public $enableCsrfCheck = false; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Set all errors and rule sets empty, and sets success to false. |
|
| 97 | - * |
|
| 98 | - * @return void |
|
| 99 | - */ |
|
| 100 | - public function __construct() { |
|
| 101 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 102 | - $this->logger->setLogger('Library::FormValidation'); |
|
| 91 | + * @var boolean |
|
| 92 | + */ |
|
| 93 | + public $enableCsrfCheck = false; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Set all errors and rule sets empty, and sets success to false. |
|
| 97 | + * |
|
| 98 | + * @return void |
|
| 99 | + */ |
|
| 100 | + public function __construct() { |
|
| 101 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 102 | + $this->logger->setLogger('Library::FormValidation'); |
|
| 103 | 103 | |
| 104 | 104 | //Load form validation language message |
| 105 | - Loader::lang('form_validation'); |
|
| 106 | - $obj = & get_instance(); |
|
| 107 | - $this->_errorsMessages = array( |
|
| 108 | - 'required' => $obj->lang->get('fv_required'), |
|
| 109 | - 'min_length' => $obj->lang->get('fv_min_length'), |
|
| 110 | - 'max_length' => $obj->lang->get('fv_max_length'), |
|
| 111 | - 'exact_length' => $obj->lang->get('fv_exact_length'), |
|
| 112 | - 'less_than' => $obj->lang->get('fv_less_than'), |
|
| 113 | - 'greater_than' => $obj->lang->get('fv_greater_than'), |
|
| 114 | - 'matches' => $obj->lang->get('fv_matches'), |
|
| 115 | - 'valid_email' => $obj->lang->get('fv_valid_email'), |
|
| 116 | - 'not_equal' => array( |
|
| 117 | - 'post:key' => $obj->lang->get('fv_not_equal_post_key'), |
|
| 118 | - 'string' => $obj->lang->get('fv_not_equal_string') |
|
| 119 | - ), |
|
| 120 | - 'depends' => $obj->lang->get('fv_depends'), |
|
| 121 | - 'is_unique' => $obj->lang->get('fv_is_unique'), |
|
| 122 | - 'is_unique_update' => $obj->lang->get('fv_is_unique_update'), |
|
| 123 | - 'exists' => $obj->lang->get('fv_exists'), |
|
| 124 | - 'regex' => $obj->lang->get('fv_regex'), |
|
| 125 | - 'in_list' => $obj->lang->get('fv_in_list'), |
|
| 126 | - 'numeric' => $obj->lang->get('fv_numeric'), |
|
| 127 | - 'callback' => $obj->lang->get('fv_callback'), |
|
| 128 | - ); |
|
| 129 | - $this->_resetValidation(); |
|
| 130 | - $this->setData($obj->request->post(null)); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Reset the form validation instance |
|
| 135 | - */ |
|
| 136 | - protected function _resetValidation() { |
|
| 137 | - $this->_rules = array(); |
|
| 138 | - $this->_labels = array(); |
|
| 139 | - $this->_errorPhraseOverrides = array(); |
|
| 140 | - $this->_errors = array(); |
|
| 141 | - $this->_success = false; |
|
| 142 | - $this->_forceFail = false; |
|
| 143 | - $this->data = array(); |
|
| 105 | + Loader::lang('form_validation'); |
|
| 106 | + $obj = & get_instance(); |
|
| 107 | + $this->_errorsMessages = array( |
|
| 108 | + 'required' => $obj->lang->get('fv_required'), |
|
| 109 | + 'min_length' => $obj->lang->get('fv_min_length'), |
|
| 110 | + 'max_length' => $obj->lang->get('fv_max_length'), |
|
| 111 | + 'exact_length' => $obj->lang->get('fv_exact_length'), |
|
| 112 | + 'less_than' => $obj->lang->get('fv_less_than'), |
|
| 113 | + 'greater_than' => $obj->lang->get('fv_greater_than'), |
|
| 114 | + 'matches' => $obj->lang->get('fv_matches'), |
|
| 115 | + 'valid_email' => $obj->lang->get('fv_valid_email'), |
|
| 116 | + 'not_equal' => array( |
|
| 117 | + 'post:key' => $obj->lang->get('fv_not_equal_post_key'), |
|
| 118 | + 'string' => $obj->lang->get('fv_not_equal_string') |
|
| 119 | + ), |
|
| 120 | + 'depends' => $obj->lang->get('fv_depends'), |
|
| 121 | + 'is_unique' => $obj->lang->get('fv_is_unique'), |
|
| 122 | + 'is_unique_update' => $obj->lang->get('fv_is_unique_update'), |
|
| 123 | + 'exists' => $obj->lang->get('fv_exists'), |
|
| 124 | + 'regex' => $obj->lang->get('fv_regex'), |
|
| 125 | + 'in_list' => $obj->lang->get('fv_in_list'), |
|
| 126 | + 'numeric' => $obj->lang->get('fv_numeric'), |
|
| 127 | + 'callback' => $obj->lang->get('fv_callback'), |
|
| 128 | + ); |
|
| 129 | + $this->_resetValidation(); |
|
| 130 | + $this->setData($obj->request->post(null)); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Reset the form validation instance |
|
| 135 | + */ |
|
| 136 | + protected function _resetValidation() { |
|
| 137 | + $this->_rules = array(); |
|
| 138 | + $this->_labels = array(); |
|
| 139 | + $this->_errorPhraseOverrides = array(); |
|
| 140 | + $this->_errors = array(); |
|
| 141 | + $this->_success = false; |
|
| 142 | + $this->_forceFail = false; |
|
| 143 | + $this->data = array(); |
|
| 144 | 144 | $this->enableCsrfCheck = false; |
| 145 | - } |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * Set the form validation data |
|
| 149 | - * @param array $data the values to be validated |
|
| 147 | + /** |
|
| 148 | + * Set the form validation data |
|
| 149 | + * @param array $data the values to be validated |
|
| 150 | 150 | * |
| 151 | - * @return FormValidation Current instance of object. |
|
| 152 | - */ |
|
| 153 | - public function setData(array $data){ |
|
| 154 | - $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
|
| 155 | - $this->data = $data; |
|
| 151 | + * @return FormValidation Current instance of object. |
|
| 152 | + */ |
|
| 153 | + public function setData(array $data){ |
|
| 154 | + $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
|
| 155 | + $this->data = $data; |
|
| 156 | 156 | return $this; |
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Get the form validation data |
|
| 161 | - * @return array the form validation data to be validated |
|
| 162 | - */ |
|
| 163 | - public function getData(){ |
|
| 164 | - return $this->data; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get the validation function name to validate a rule |
|
| 169 | - * |
|
| 170 | - * @return string the function name |
|
| 171 | - */ |
|
| 172 | - protected function _toCallCase($funcName, $prefix='_validate') { |
|
| 173 | - $funcName = strtolower($funcName); |
|
| 174 | - $finalFuncName = $prefix; |
|
| 175 | - foreach (explode('_', $funcName) as $funcNamePart) { |
|
| 176 | - $finalFuncName .= strtoupper($funcNamePart[0]) . substr($funcNamePart, 1); |
|
| 177 | - } |
|
| 178 | - return $finalFuncName; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Returns the boolean of the data status success. It goes by the simple |
|
| 183 | - * |
|
| 184 | - * @return boolean Whether or not the data validation has succeeded |
|
| 185 | - */ |
|
| 186 | - public function isSuccess() { |
|
| 187 | - return $this->_success; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Checks if the request method is POST or the Data to be validated is set |
|
| 192 | - * |
|
| 193 | - * @return boolean Whether or not the form has been submitted or the data is available for validation. |
|
| 194 | - */ |
|
| 195 | - public function canDoValidation() { |
|
| 196 | - return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Runs _run once POST data has been submitted or data is set manually. |
|
| 201 | - * |
|
| 202 | - * @return boolean |
|
| 203 | - */ |
|
| 204 | - public function run() { |
|
| 205 | - if ($this->canDoValidation()) { |
|
| 206 | - $this->logger->info('The data to validate are listed below: ' . stringfy_vars($this->getData())); |
|
| 207 | - $this->_run(); |
|
| 208 | - } |
|
| 209 | - return $this->isSuccess(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Validate the CSRF |
|
| 214 | - * @return void |
|
| 215 | - */ |
|
| 216 | - protected function validateCSRF(){ |
|
| 217 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 218 | - $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
| 219 | - //first check for CSRF |
|
| 220 | - if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 221 | - show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 222 | - } |
|
| 223 | - else{ |
|
| 224 | - $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - /** |
|
| 229 | - * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
| 230 | - * each rule against the data value. Sets _success to true if there are no errors |
|
| 231 | - * afterwards. |
|
| 232 | - */ |
|
| 233 | - protected function _run() { |
|
| 234 | - //validate CSRF |
|
| 235 | - $this->validateCSRF(); |
|
| 236 | - ///////////////////////////////////////////// |
|
| 237 | - $this->_forceFail = false; |
|
| 238 | - |
|
| 239 | - foreach ($this->getData() as $inputName => $inputVal) { |
|
| 240 | - if(is_array($this->data[$inputName])){ |
|
| 241 | - $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 242 | - } |
|
| 243 | - else{ |
|
| 244 | - $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if (array_key_exists($inputName, $this->_rules)) { |
|
| 248 | - foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 249 | - $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 255 | - $this->_success = true; |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Adds a rule to a form data validation field. |
|
| 261 | - * |
|
| 262 | - * @param string $inputField Name of the field or the data key to add a rule to |
|
| 263 | - * @param string $ruleSets PIPE seperated string of rules |
|
| 264 | - * |
|
| 265 | - * @return FormValidation Current instance of object. |
|
| 266 | - */ |
|
| 267 | - public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 268 | - $this->_rules[$inputField] = $ruleSets; |
|
| 269 | - $this->_labels[$inputField] = $inputLabel; |
|
| 270 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 271 | - return $this; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 276 | - * of rule names rather than a pipe-delimited string as well. |
|
| 277 | - * @param array $ruleSets |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Get the form validation data |
|
| 161 | + * @return array the form validation data to be validated |
|
| 162 | + */ |
|
| 163 | + public function getData(){ |
|
| 164 | + return $this->data; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get the validation function name to validate a rule |
|
| 169 | + * |
|
| 170 | + * @return string the function name |
|
| 171 | + */ |
|
| 172 | + protected function _toCallCase($funcName, $prefix='_validate') { |
|
| 173 | + $funcName = strtolower($funcName); |
|
| 174 | + $finalFuncName = $prefix; |
|
| 175 | + foreach (explode('_', $funcName) as $funcNamePart) { |
|
| 176 | + $finalFuncName .= strtoupper($funcNamePart[0]) . substr($funcNamePart, 1); |
|
| 177 | + } |
|
| 178 | + return $finalFuncName; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Returns the boolean of the data status success. It goes by the simple |
|
| 183 | + * |
|
| 184 | + * @return boolean Whether or not the data validation has succeeded |
|
| 185 | + */ |
|
| 186 | + public function isSuccess() { |
|
| 187 | + return $this->_success; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Checks if the request method is POST or the Data to be validated is set |
|
| 192 | + * |
|
| 193 | + * @return boolean Whether or not the form has been submitted or the data is available for validation. |
|
| 194 | + */ |
|
| 195 | + public function canDoValidation() { |
|
| 196 | + return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Runs _run once POST data has been submitted or data is set manually. |
|
| 201 | + * |
|
| 202 | + * @return boolean |
|
| 203 | + */ |
|
| 204 | + public function run() { |
|
| 205 | + if ($this->canDoValidation()) { |
|
| 206 | + $this->logger->info('The data to validate are listed below: ' . stringfy_vars($this->getData())); |
|
| 207 | + $this->_run(); |
|
| 208 | + } |
|
| 209 | + return $this->isSuccess(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Validate the CSRF |
|
| 214 | + * @return void |
|
| 215 | + */ |
|
| 216 | + protected function validateCSRF(){ |
|
| 217 | + if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 218 | + $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
| 219 | + //first check for CSRF |
|
| 220 | + if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 221 | + show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 222 | + } |
|
| 223 | + else{ |
|
| 224 | + $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + /** |
|
| 229 | + * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
| 230 | + * each rule against the data value. Sets _success to true if there are no errors |
|
| 231 | + * afterwards. |
|
| 232 | + */ |
|
| 233 | + protected function _run() { |
|
| 234 | + //validate CSRF |
|
| 235 | + $this->validateCSRF(); |
|
| 236 | + ///////////////////////////////////////////// |
|
| 237 | + $this->_forceFail = false; |
|
| 238 | + |
|
| 239 | + foreach ($this->getData() as $inputName => $inputVal) { |
|
| 240 | + if(is_array($this->data[$inputName])){ |
|
| 241 | + $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 242 | + } |
|
| 243 | + else{ |
|
| 244 | + $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if (array_key_exists($inputName, $this->_rules)) { |
|
| 248 | + foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 249 | + $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 255 | + $this->_success = true; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Adds a rule to a form data validation field. |
|
| 261 | + * |
|
| 262 | + * @param string $inputField Name of the field or the data key to add a rule to |
|
| 263 | + * @param string $ruleSets PIPE seperated string of rules |
|
| 278 | 264 | * |
| 279 | 265 | * @return FormValidation Current instance of object. |
| 280 | - */ |
|
| 281 | - public function setRules(array $ruleSets) { |
|
| 282 | - foreach ($ruleSets as $ruleSet) { |
|
| 283 | - $pipeDelimitedRules = null; |
|
| 284 | - if (is_array($ruleSet['rules'])) { |
|
| 285 | - $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 286 | - } else { |
|
| 287 | - $pipeDelimitedRules = $ruleSet['rules']; |
|
| 288 | - } |
|
| 289 | - $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 290 | - } |
|
| 291 | - return $this; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 296 | - * end of the errors block respectively. |
|
| 297 | - * |
|
| 298 | - * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 299 | - * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 300 | - * |
|
| 266 | + */ |
|
| 267 | + public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 268 | + $this->_rules[$inputField] = $ruleSets; |
|
| 269 | + $this->_labels[$inputField] = $inputLabel; |
|
| 270 | + $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 271 | + return $this; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 276 | + * of rule names rather than a pipe-delimited string as well. |
|
| 277 | + * @param array $ruleSets |
|
| 278 | + * |
|
| 301 | 279 | * @return FormValidation Current instance of object. |
| 302 | - */ |
|
| 303 | - public function setErrorsDelimiter($start, $end) { |
|
| 304 | - $this->_allErrorsDelimiter[0] = $start; |
|
| 305 | - $this->_allErrorsDelimiter[1] = $end; |
|
| 306 | - return $this; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * This is the individual error delimiter, each argument occurs once before and after |
|
| 311 | - * each individual error listed. |
|
| 312 | - * |
|
| 313 | - * @param string $start Displayed before each error. |
|
| 314 | - * @param string $end Displayed after each error. |
|
| 315 | - * |
|
| 280 | + */ |
|
| 281 | + public function setRules(array $ruleSets) { |
|
| 282 | + foreach ($ruleSets as $ruleSet) { |
|
| 283 | + $pipeDelimitedRules = null; |
|
| 284 | + if (is_array($ruleSet['rules'])) { |
|
| 285 | + $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 286 | + } else { |
|
| 287 | + $pipeDelimitedRules = $ruleSet['rules']; |
|
| 288 | + } |
|
| 289 | + $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 290 | + } |
|
| 291 | + return $this; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 296 | + * end of the errors block respectively. |
|
| 297 | + * |
|
| 298 | + * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 299 | + * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 300 | + * |
|
| 316 | 301 | * @return FormValidation Current instance of object. |
| 317 | - */ |
|
| 318 | - public function setErrorDelimiter($start, $end) { |
|
| 319 | - $this->_eachErrorDelimiter[0] = $start; |
|
| 320 | - $this->_eachErrorDelimiter[1] = $end; |
|
| 321 | - return $this; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Get the each errors delimiters |
|
| 326 | - * |
|
| 327 | - * @return array |
|
| 328 | - */ |
|
| 329 | - public function getErrorDelimiter() { |
|
| 330 | - return $this->_eachErrorDelimiter; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Get the all errors delimiters |
|
| 335 | - * |
|
| 336 | - * @return array |
|
| 337 | - */ |
|
| 338 | - public function getErrorsDelimiter() { |
|
| 339 | - return $this->_allErrorsDelimiter; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * This sets a custom error message that can override the default error phrase provided |
|
| 344 | - * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 345 | - * which will globally change the error phrase of that rule, or in the format of: |
|
| 346 | - * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 347 | - * that rule, applied on that field. |
|
| 348 | - * |
|
| 349 | - * @return boolean True on success, false on failure. |
|
| 350 | - */ |
|
| 351 | - public function setMessage() { |
|
| 352 | - $numArgs = func_num_args(); |
|
| 353 | - switch ($numArgs) { |
|
| 354 | - default: |
|
| 355 | - return false; |
|
| 356 | - // A global rule error message |
|
| 357 | - case 2: |
|
| 358 | - foreach ($this->post(null) as $key => $val) { |
|
| 359 | - $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 360 | - } |
|
| 361 | - break; |
|
| 362 | - // Field specific rule error message |
|
| 363 | - case 3: |
|
| 364 | - $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 365 | - break; |
|
| 366 | - } |
|
| 367 | - return true; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Adds a custom error message in the errorSet array, that will |
|
| 372 | - * forcibly display it. |
|
| 373 | - * |
|
| 374 | - * @param string $inputName The form input name or data key |
|
| 375 | - * @param string $errorMessage Error to display |
|
| 376 | - * |
|
| 377 | - * @return formValidation Current instance of the object |
|
| 378 | - */ |
|
| 379 | - public function setCustomError($inputName, $errorMessage) { |
|
| 380 | - $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 381 | - $this->_errors[$inputName] = $errorMessage; |
|
| 382 | - return $this; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 387 | - * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 388 | - * all values. |
|
| 389 | - * |
|
| 390 | - * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 391 | - * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 392 | - * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 393 | - */ |
|
| 394 | - public function post($key = null, $trim = true) { |
|
| 395 | - $returnValue = null; |
|
| 396 | - if (is_null($key)) { |
|
| 397 | - $returnValue = array(); |
|
| 398 | - foreach ($this->getData() as $key => $val) { |
|
| 399 | - $returnValue[$key] = $this->post($key, $trim); |
|
| 400 | - } |
|
| 401 | - } else { |
|
| 402 | - $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 403 | - } |
|
| 404 | - return $returnValue; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 409 | - * function or just returned. |
|
| 410 | - * |
|
| 411 | - * @param boolean $limit number of error to display or return |
|
| 412 | - * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 413 | - * |
|
| 414 | - * @return string Errors formatted for output |
|
| 415 | - */ |
|
| 416 | - public function displayErrors($limit = null, $echo = true) { |
|
| 417 | - list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 418 | - list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 419 | - $errorOutput = $errorsStart; |
|
| 420 | - $i = 0; |
|
| 421 | - if (!empty($this->_errors)) { |
|
| 422 | - foreach ($this->_errors as $fieldName => $error) { |
|
| 423 | - if ($i === $limit) { |
|
| 424 | - break; |
|
| 425 | - } |
|
| 426 | - $errorOutput .= $errorStart; |
|
| 427 | - $errorOutput .= $error; |
|
| 428 | - $errorOutput .= $errorEnd; |
|
| 429 | - $i++; |
|
| 430 | - } |
|
| 431 | - } |
|
| 432 | - $errorOutput .= $errorsEnd; |
|
| 433 | - echo ($echo) ? $errorOutput : ''; |
|
| 434 | - return (! $echo) ? $errorOutput : null; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Returns raw array of errors in no format instead of displaying them |
|
| 439 | - * formatted. |
|
| 440 | - * |
|
| 441 | - * @return array |
|
| 442 | - */ |
|
| 443 | - public function returnErrors() { |
|
| 444 | - return $this->_errors; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 449 | - * |
|
| 450 | - * @param string $ruleString String to be parsed. |
|
| 451 | - * |
|
| 452 | - * @return array Array of each value in original string. |
|
| 453 | - */ |
|
| 454 | - protected function _parseRuleString($ruleString) { |
|
| 455 | - $ruleSets = array(); |
|
| 456 | - /* |
|
| 302 | + */ |
|
| 303 | + public function setErrorsDelimiter($start, $end) { |
|
| 304 | + $this->_allErrorsDelimiter[0] = $start; |
|
| 305 | + $this->_allErrorsDelimiter[1] = $end; |
|
| 306 | + return $this; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * This is the individual error delimiter, each argument occurs once before and after |
|
| 311 | + * each individual error listed. |
|
| 312 | + * |
|
| 313 | + * @param string $start Displayed before each error. |
|
| 314 | + * @param string $end Displayed after each error. |
|
| 315 | + * |
|
| 316 | + * @return FormValidation Current instance of object. |
|
| 317 | + */ |
|
| 318 | + public function setErrorDelimiter($start, $end) { |
|
| 319 | + $this->_eachErrorDelimiter[0] = $start; |
|
| 320 | + $this->_eachErrorDelimiter[1] = $end; |
|
| 321 | + return $this; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Get the each errors delimiters |
|
| 326 | + * |
|
| 327 | + * @return array |
|
| 328 | + */ |
|
| 329 | + public function getErrorDelimiter() { |
|
| 330 | + return $this->_eachErrorDelimiter; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Get the all errors delimiters |
|
| 335 | + * |
|
| 336 | + * @return array |
|
| 337 | + */ |
|
| 338 | + public function getErrorsDelimiter() { |
|
| 339 | + return $this->_allErrorsDelimiter; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * This sets a custom error message that can override the default error phrase provided |
|
| 344 | + * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 345 | + * which will globally change the error phrase of that rule, or in the format of: |
|
| 346 | + * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 347 | + * that rule, applied on that field. |
|
| 348 | + * |
|
| 349 | + * @return boolean True on success, false on failure. |
|
| 350 | + */ |
|
| 351 | + public function setMessage() { |
|
| 352 | + $numArgs = func_num_args(); |
|
| 353 | + switch ($numArgs) { |
|
| 354 | + default: |
|
| 355 | + return false; |
|
| 356 | + // A global rule error message |
|
| 357 | + case 2: |
|
| 358 | + foreach ($this->post(null) as $key => $val) { |
|
| 359 | + $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 360 | + } |
|
| 361 | + break; |
|
| 362 | + // Field specific rule error message |
|
| 363 | + case 3: |
|
| 364 | + $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 365 | + break; |
|
| 366 | + } |
|
| 367 | + return true; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Adds a custom error message in the errorSet array, that will |
|
| 372 | + * forcibly display it. |
|
| 373 | + * |
|
| 374 | + * @param string $inputName The form input name or data key |
|
| 375 | + * @param string $errorMessage Error to display |
|
| 376 | + * |
|
| 377 | + * @return formValidation Current instance of the object |
|
| 378 | + */ |
|
| 379 | + public function setCustomError($inputName, $errorMessage) { |
|
| 380 | + $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 381 | + $this->_errors[$inputName] = $errorMessage; |
|
| 382 | + return $this; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 387 | + * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 388 | + * all values. |
|
| 389 | + * |
|
| 390 | + * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 391 | + * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 392 | + * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 393 | + */ |
|
| 394 | + public function post($key = null, $trim = true) { |
|
| 395 | + $returnValue = null; |
|
| 396 | + if (is_null($key)) { |
|
| 397 | + $returnValue = array(); |
|
| 398 | + foreach ($this->getData() as $key => $val) { |
|
| 399 | + $returnValue[$key] = $this->post($key, $trim); |
|
| 400 | + } |
|
| 401 | + } else { |
|
| 402 | + $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 403 | + } |
|
| 404 | + return $returnValue; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 409 | + * function or just returned. |
|
| 410 | + * |
|
| 411 | + * @param boolean $limit number of error to display or return |
|
| 412 | + * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 413 | + * |
|
| 414 | + * @return string Errors formatted for output |
|
| 415 | + */ |
|
| 416 | + public function displayErrors($limit = null, $echo = true) { |
|
| 417 | + list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 418 | + list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 419 | + $errorOutput = $errorsStart; |
|
| 420 | + $i = 0; |
|
| 421 | + if (!empty($this->_errors)) { |
|
| 422 | + foreach ($this->_errors as $fieldName => $error) { |
|
| 423 | + if ($i === $limit) { |
|
| 424 | + break; |
|
| 425 | + } |
|
| 426 | + $errorOutput .= $errorStart; |
|
| 427 | + $errorOutput .= $error; |
|
| 428 | + $errorOutput .= $errorEnd; |
|
| 429 | + $i++; |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | + $errorOutput .= $errorsEnd; |
|
| 433 | + echo ($echo) ? $errorOutput : ''; |
|
| 434 | + return (! $echo) ? $errorOutput : null; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Returns raw array of errors in no format instead of displaying them |
|
| 439 | + * formatted. |
|
| 440 | + * |
|
| 441 | + * @return array |
|
| 442 | + */ |
|
| 443 | + public function returnErrors() { |
|
| 444 | + return $this->_errors; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 449 | + * |
|
| 450 | + * @param string $ruleString String to be parsed. |
|
| 451 | + * |
|
| 452 | + * @return array Array of each value in original string. |
|
| 453 | + */ |
|
| 454 | + protected function _parseRuleString($ruleString) { |
|
| 455 | + $ruleSets = array(); |
|
| 456 | + /* |
|
| 457 | 457 | //////////////// hack for regex rule that can contain "|" |
| 458 | 458 | */ |
| 459 | - if(strpos($ruleString, 'regex') !== false){ |
|
| 460 | - $regexRule = array(); |
|
| 461 | - $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 462 | - preg_match($rule, $ruleString, $regexRule); |
|
| 463 | - $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 464 | - if(!empty($regexRule[0])){ |
|
| 465 | - $ruleSets[] = $regexRule[0]; |
|
| 466 | - } |
|
| 467 | - $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 468 | - foreach ($ruleStringRegex as $rule) { |
|
| 469 | - $rule = trim($rule); |
|
| 470 | - if($rule){ |
|
| 471 | - $ruleSets[] = $rule; |
|
| 472 | - } |
|
| 473 | - } |
|
| 459 | + if(strpos($ruleString, 'regex') !== false){ |
|
| 460 | + $regexRule = array(); |
|
| 461 | + $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 462 | + preg_match($rule, $ruleString, $regexRule); |
|
| 463 | + $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 464 | + if(!empty($regexRule[0])){ |
|
| 465 | + $ruleSets[] = $regexRule[0]; |
|
| 466 | + } |
|
| 467 | + $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 468 | + foreach ($ruleStringRegex as $rule) { |
|
| 469 | + $rule = trim($rule); |
|
| 470 | + if($rule){ |
|
| 471 | + $ruleSets[] = $rule; |
|
| 472 | + } |
|
| 473 | + } |
|
| 474 | 474 | |
| 475 | - } |
|
| 476 | - /***********************************/ |
|
| 477 | - else{ |
|
| 478 | - if (strpos($ruleString, '|') !== FALSE) { |
|
| 479 | - $ruleSets = explode('|', $ruleString); |
|
| 480 | - } else { |
|
| 481 | - $ruleSets[] = $ruleString; |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - return $ruleSets; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Returns whether or not a field obtains the rule "required". |
|
| 489 | - * |
|
| 490 | - * @param string $fieldName Field to check if required. |
|
| 491 | - * |
|
| 492 | - * @return boolean Whether or not the field is required. |
|
| 493 | - */ |
|
| 494 | - protected function _fieldIsRequired($fieldName) { |
|
| 495 | - $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 496 | - return (in_array('required', $rules)); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 501 | - * and adds an error to the errorSet if it fails validation of the rule. |
|
| 502 | - * |
|
| 503 | - * @param string $inputName Name or key of the validation data |
|
| 504 | - * @param string $inputVal Value of the validation data |
|
| 505 | - * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 506 | - * @return void |
|
| 507 | - */ |
|
| 508 | - protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 509 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 510 | - // Array to store args |
|
| 511 | - $ruleArgs = array(); |
|
| 512 | - |
|
| 513 | - preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 514 | - |
|
| 515 | - // Get the rule arguments, realRule is just the base rule name |
|
| 516 | - // Like min_length instead of min_length[3] |
|
| 517 | - $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 475 | + } |
|
| 476 | + /***********************************/ |
|
| 477 | + else{ |
|
| 478 | + if (strpos($ruleString, '|') !== FALSE) { |
|
| 479 | + $ruleSets = explode('|', $ruleString); |
|
| 480 | + } else { |
|
| 481 | + $ruleSets[] = $ruleString; |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + return $ruleSets; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Returns whether or not a field obtains the rule "required". |
|
| 489 | + * |
|
| 490 | + * @param string $fieldName Field to check if required. |
|
| 491 | + * |
|
| 492 | + * @return boolean Whether or not the field is required. |
|
| 493 | + */ |
|
| 494 | + protected function _fieldIsRequired($fieldName) { |
|
| 495 | + $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 496 | + return (in_array('required', $rules)); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + /** |
|
| 500 | + * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 501 | + * and adds an error to the errorSet if it fails validation of the rule. |
|
| 502 | + * |
|
| 503 | + * @param string $inputName Name or key of the validation data |
|
| 504 | + * @param string $inputVal Value of the validation data |
|
| 505 | + * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 506 | + * @return void |
|
| 507 | + */ |
|
| 508 | + protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 509 | + $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 510 | + // Array to store args |
|
| 511 | + $ruleArgs = array(); |
|
| 512 | + |
|
| 513 | + preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 514 | + |
|
| 515 | + // Get the rule arguments, realRule is just the base rule name |
|
| 516 | + // Like min_length instead of min_length[3] |
|
| 517 | + $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 518 | 518 | |
| 519 | - if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 520 | - $methodToCall = $this->_toCallCase($ruleName); |
|
| 521 | - call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 522 | - } |
|
| 523 | - return; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Set error for the given field or key |
|
| 528 | - * |
|
| 529 | - * @param string $inputName the input or key name |
|
| 530 | - * @param string $ruleName the rule name |
|
| 531 | - * @param array|string $replacements |
|
| 532 | - */ |
|
| 533 | - protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 534 | - $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 535 | - $rulePhrase = null; |
|
| 536 | - foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 537 | - if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 538 | - $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 539 | - } else { |
|
| 540 | - $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - // Any overrides? |
|
| 544 | - if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 545 | - $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 546 | - } |
|
| 547 | - // Type cast to array in case it's a string |
|
| 548 | - $replacements = (array) $replacements; |
|
| 519 | + if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 520 | + $methodToCall = $this->_toCallCase($ruleName); |
|
| 521 | + call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 522 | + } |
|
| 523 | + return; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Set error for the given field or key |
|
| 528 | + * |
|
| 529 | + * @param string $inputName the input or key name |
|
| 530 | + * @param string $ruleName the rule name |
|
| 531 | + * @param array|string $replacements |
|
| 532 | + */ |
|
| 533 | + protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 534 | + $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 535 | + $rulePhrase = null; |
|
| 536 | + foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 537 | + if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 538 | + $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 539 | + } else { |
|
| 540 | + $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + // Any overrides? |
|
| 544 | + if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 545 | + $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 546 | + } |
|
| 547 | + // Type cast to array in case it's a string |
|
| 548 | + $replacements = (array) $replacements; |
|
| 549 | 549 | $replacementCount = count($replacements); |
| 550 | - for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 551 | - $key = $i - 1; |
|
| 552 | - $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 553 | - } |
|
| 554 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
| 555 | - $this->_errors[$inputName] = $rulePhrase; |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * Used to run a callback for the callback rule, as well as pass in a default |
|
| 561 | - * argument of the post value. For example the username field having a rule: |
|
| 562 | - * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 563 | - * of eval over call_user_func is in case the function is not user defined. |
|
| 564 | - * |
|
| 565 | - * @param type $inputArg |
|
| 566 | - * @param string $callbackFunc |
|
| 567 | - * |
|
| 568 | - * @return mixed |
|
| 569 | - */ |
|
| 570 | - protected function _runCallback($inputArg, $callbackFunc) { |
|
| 550 | + for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 551 | + $key = $i - 1; |
|
| 552 | + $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 553 | + } |
|
| 554 | + if (! array_key_exists($inputName, $this->_errors)) { |
|
| 555 | + $this->_errors[$inputName] = $rulePhrase; |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * Used to run a callback for the callback rule, as well as pass in a default |
|
| 561 | + * argument of the post value. For example the username field having a rule: |
|
| 562 | + * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 563 | + * of eval over call_user_func is in case the function is not user defined. |
|
| 564 | + * |
|
| 565 | + * @param type $inputArg |
|
| 566 | + * @param string $callbackFunc |
|
| 567 | + * |
|
| 568 | + * @return mixed |
|
| 569 | + */ |
|
| 570 | + protected function _runCallback($inputArg, $callbackFunc) { |
|
| 571 | 571 | return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * Used for applying a rule only if the empty callback evaluates to true, |
|
| 576 | - * for example required[funcName] - This runs funcName without passing any |
|
| 577 | - * arguments. |
|
| 578 | - * |
|
| 579 | - * @param string $callbackFunc |
|
| 580 | - * |
|
| 581 | - * @return mixed |
|
| 582 | - */ |
|
| 583 | - protected function _runEmptyCallback($callbackFunc) { |
|
| 584 | - return eval('return ' . $callbackFunc . '();'); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Gets a specific label of a specific field input name. |
|
| 589 | - * |
|
| 590 | - * @param string $inputName |
|
| 591 | - * |
|
| 592 | - * @return string |
|
| 593 | - */ |
|
| 594 | - protected function _getLabel($inputName) { |
|
| 595 | - return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 596 | - } |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * Used for applying a rule only if the empty callback evaluates to true, |
|
| 576 | + * for example required[funcName] - This runs funcName without passing any |
|
| 577 | + * arguments. |
|
| 578 | + * |
|
| 579 | + * @param string $callbackFunc |
|
| 580 | + * |
|
| 581 | + * @return mixed |
|
| 582 | + */ |
|
| 583 | + protected function _runEmptyCallback($callbackFunc) { |
|
| 584 | + return eval('return ' . $callbackFunc . '();'); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Gets a specific label of a specific field input name. |
|
| 589 | + * |
|
| 590 | + * @param string $inputName |
|
| 591 | + * |
|
| 592 | + * @return string |
|
| 593 | + */ |
|
| 594 | + protected function _getLabel($inputName) { |
|
| 595 | + return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 596 | + } |
|
| 597 | 597 | |
| 598 | - /** |
|
| 599 | - * Peform validation for the rule "required" |
|
| 600 | - * @param string $inputName the form field or data key name used |
|
| 601 | - * @param string $ruleName the rule name for this validation ("required") |
|
| 602 | - * @param array $ruleArgs the rules argument |
|
| 603 | - */ |
|
| 598 | + /** |
|
| 599 | + * Peform validation for the rule "required" |
|
| 600 | + * @param string $inputName the form field or data key name used |
|
| 601 | + * @param string $ruleName the rule name for this validation ("required") |
|
| 602 | + * @param array $ruleArgs the rules argument |
|
| 603 | + */ |
|
| 604 | 604 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
| 605 | - $inputVal = $this->post($inputName); |
|
| 606 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 607 | - $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 608 | - if ($inputVal == '' && $callbackReturn == true) { |
|
| 609 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 610 | - } |
|
| 611 | - } |
|
| 605 | + $inputVal = $this->post($inputName); |
|
| 606 | + if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 607 | + $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 608 | + if ($inputVal == '' && $callbackReturn == true) { |
|
| 609 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 610 | + } |
|
| 611 | + } |
|
| 612 | 612 | else if($inputVal == '') { |
| 613 | 613 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 614 | - } |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Perform validation for the honey pot so means for the validation to be failed |
|
| 619 | - * @param string $inputName the form field or data key name used |
|
| 620 | - * @param string $ruleName the rule name for this validation |
|
| 621 | - * @param array $ruleArgs the rules argument |
|
| 622 | - */ |
|
| 623 | - protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 624 | - if ($this->data[$inputName] != '') { |
|
| 625 | - $this->_forceFail = true; |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - /** |
|
| 630 | - * Peform validation for the rule "callback" |
|
| 631 | - * @param string $inputName the form field or data key name used |
|
| 632 | - * @param string $ruleName the rule name for this validation ("callback") |
|
| 633 | - * @param array $ruleArgs the rules argument |
|
| 634 | - */ |
|
| 635 | - protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 636 | - if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 614 | + } |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Perform validation for the honey pot so means for the validation to be failed |
|
| 619 | + * @param string $inputName the form field or data key name used |
|
| 620 | + * @param string $ruleName the rule name for this validation |
|
| 621 | + * @param array $ruleArgs the rules argument |
|
| 622 | + */ |
|
| 623 | + protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 624 | + if ($this->data[$inputName] != '') { |
|
| 625 | + $this->_forceFail = true; |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + /** |
|
| 630 | + * Peform validation for the rule "callback" |
|
| 631 | + * @param string $inputName the form field or data key name used |
|
| 632 | + * @param string $ruleName the rule name for this validation ("callback") |
|
| 633 | + * @param array $ruleArgs the rules argument |
|
| 634 | + */ |
|
| 635 | + protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 636 | + if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 637 | 637 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
| 638 | 638 | if(! $result){ |
| 639 | 639 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
| 640 | 640 | } |
| 641 | - } |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Peform validation for the rule "depends" |
|
| 646 | - * @param string $inputName the form field or data key name used |
|
| 647 | - * @param string $ruleName the rule name for this validation ("depends") |
|
| 648 | - * @param array $ruleArgs the rules argument |
|
| 649 | - */ |
|
| 650 | - protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 651 | - if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 652 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 653 | - } |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * Peform validation for the rule "not_equal" |
|
| 658 | - * @param string $inputName the form field or data key name used |
|
| 659 | - * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 660 | - * @param array $ruleArgs the rules argument |
|
| 661 | - */ |
|
| 662 | - protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 663 | - $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 664 | - foreach ($canNotEqual as $doNotEqual) { |
|
| 665 | - $inputVal = $this->post($inputName); |
|
| 666 | - if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 667 | - if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 668 | - $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 669 | - continue; |
|
| 670 | - } |
|
| 671 | - } |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * Peform validation for the rule "depends" |
|
| 646 | + * @param string $inputName the form field or data key name used |
|
| 647 | + * @param string $ruleName the rule name for this validation ("depends") |
|
| 648 | + * @param array $ruleArgs the rules argument |
|
| 649 | + */ |
|
| 650 | + protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 651 | + if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 652 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 653 | + } |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * Peform validation for the rule "not_equal" |
|
| 658 | + * @param string $inputName the form field or data key name used |
|
| 659 | + * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 660 | + * @param array $ruleArgs the rules argument |
|
| 661 | + */ |
|
| 662 | + protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 663 | + $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 664 | + foreach ($canNotEqual as $doNotEqual) { |
|
| 665 | + $inputVal = $this->post($inputName); |
|
| 666 | + if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 667 | + if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 668 | + $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 669 | + continue; |
|
| 670 | + } |
|
| 671 | + } |
|
| 672 | 672 | else{ |
| 673 | - if ($inputVal == $doNotEqual) { |
|
| 674 | - $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 675 | - continue; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - } |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Peform validation for the rule "matches" |
|
| 683 | - * @param string $inputName the form field or data key name used |
|
| 684 | - * @param string $ruleName the rule name for this validation ("matches") |
|
| 685 | - * @param array $ruleArgs the rules argument |
|
| 686 | - */ |
|
| 687 | - protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 688 | - $inputVal = $this->post($inputName); |
|
| 689 | - if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 690 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 691 | - } |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * Peform validation for the rule "valid_email" |
|
| 696 | - * @param string $inputName the form field or data key name used |
|
| 697 | - * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 698 | - * @param array $ruleArgs the rules argument |
|
| 699 | - */ |
|
| 700 | - protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 701 | - $inputVal = $this->post($inputName); |
|
| 702 | - if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
| 703 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 704 | - return; |
|
| 705 | - } |
|
| 706 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * Peform validation for the rule "exact_length" |
|
| 712 | - * @param string $inputName the form field or data key name used |
|
| 713 | - * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 714 | - * @param array $ruleArgs the rules argument |
|
| 715 | - */ |
|
| 716 | - protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 717 | - $inputVal = $this->post($inputName); |
|
| 718 | - if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 719 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 720 | - return; |
|
| 721 | - } |
|
| 722 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 723 | - } |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - /** |
|
| 727 | - * Peform validation for the rule "max_length" |
|
| 728 | - * @param string $inputName the form field or data key name used |
|
| 729 | - * @param string $ruleName the rule name for this validation ("max_length") |
|
| 730 | - * @param array $ruleArgs the rules argument |
|
| 731 | - */ |
|
| 732 | - protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 733 | - $inputVal = $this->post($inputName); |
|
| 734 | - if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 735 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 736 | - return; |
|
| 737 | - } |
|
| 738 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 739 | - } |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - /** |
|
| 743 | - * Peform validation for the rule "min_length" |
|
| 744 | - * @param string $inputName the form field or data key name used |
|
| 745 | - * @param string $ruleName the rule name for this validation ("min_length") |
|
| 746 | - * @param array $ruleArgs the rules argument |
|
| 747 | - */ |
|
| 748 | - protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 749 | - $inputVal = $this->post($inputName); |
|
| 750 | - if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 751 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 752 | - return; |
|
| 753 | - } |
|
| 754 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 755 | - } |
|
| 756 | - } |
|
| 673 | + if ($inputVal == $doNotEqual) { |
|
| 674 | + $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 675 | + continue; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + } |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + /** |
|
| 682 | + * Peform validation for the rule "matches" |
|
| 683 | + * @param string $inputName the form field or data key name used |
|
| 684 | + * @param string $ruleName the rule name for this validation ("matches") |
|
| 685 | + * @param array $ruleArgs the rules argument |
|
| 686 | + */ |
|
| 687 | + protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 688 | + $inputVal = $this->post($inputName); |
|
| 689 | + if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 690 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 691 | + } |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * Peform validation for the rule "valid_email" |
|
| 696 | + * @param string $inputName the form field or data key name used |
|
| 697 | + * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 698 | + * @param array $ruleArgs the rules argument |
|
| 699 | + */ |
|
| 700 | + protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 701 | + $inputVal = $this->post($inputName); |
|
| 702 | + if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
| 703 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 704 | + return; |
|
| 705 | + } |
|
| 706 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Peform validation for the rule "exact_length" |
|
| 712 | + * @param string $inputName the form field or data key name used |
|
| 713 | + * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 714 | + * @param array $ruleArgs the rules argument |
|
| 715 | + */ |
|
| 716 | + protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 717 | + $inputVal = $this->post($inputName); |
|
| 718 | + if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 719 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 720 | + return; |
|
| 721 | + } |
|
| 722 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + /** |
|
| 727 | + * Peform validation for the rule "max_length" |
|
| 728 | + * @param string $inputName the form field or data key name used |
|
| 729 | + * @param string $ruleName the rule name for this validation ("max_length") |
|
| 730 | + * @param array $ruleArgs the rules argument |
|
| 731 | + */ |
|
| 732 | + protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 733 | + $inputVal = $this->post($inputName); |
|
| 734 | + if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 735 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 736 | + return; |
|
| 737 | + } |
|
| 738 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 739 | + } |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + /** |
|
| 743 | + * Peform validation for the rule "min_length" |
|
| 744 | + * @param string $inputName the form field or data key name used |
|
| 745 | + * @param string $ruleName the rule name for this validation ("min_length") |
|
| 746 | + * @param array $ruleArgs the rules argument |
|
| 747 | + */ |
|
| 748 | + protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 749 | + $inputVal = $this->post($inputName); |
|
| 750 | + if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 751 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 752 | + return; |
|
| 753 | + } |
|
| 754 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 755 | + } |
|
| 756 | + } |
|
| 757 | 757 | |
| 758 | - /** |
|
| 759 | - * Peform validation for the rule "less_than" |
|
| 760 | - * @param string $inputName the form field or data key name used |
|
| 761 | - * @param string $ruleName the rule name for this validation ("less_than") |
|
| 762 | - * @param array $ruleArgs the rules argument |
|
| 763 | - */ |
|
| 764 | - protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 765 | - $inputVal = $this->post($inputName); |
|
| 766 | - if ($inputVal >= $ruleArgs[1]) { |
|
| 767 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 768 | - return; |
|
| 769 | - } |
|
| 770 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 771 | - } |
|
| 772 | - } |
|
| 758 | + /** |
|
| 759 | + * Peform validation for the rule "less_than" |
|
| 760 | + * @param string $inputName the form field or data key name used |
|
| 761 | + * @param string $ruleName the rule name for this validation ("less_than") |
|
| 762 | + * @param array $ruleArgs the rules argument |
|
| 763 | + */ |
|
| 764 | + protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 765 | + $inputVal = $this->post($inputName); |
|
| 766 | + if ($inputVal >= $ruleArgs[1]) { |
|
| 767 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 768 | + return; |
|
| 769 | + } |
|
| 770 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 771 | + } |
|
| 772 | + } |
|
| 773 | 773 | |
| 774 | - /** |
|
| 775 | - * Peform validation for the rule "greater_than" |
|
| 776 | - * @param string $inputName the form field or data key name used |
|
| 777 | - * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 778 | - * @param array $ruleArgs the rules argument |
|
| 779 | - */ |
|
| 780 | - protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 781 | - $inputVal = $this->post($inputName); |
|
| 782 | - if ($inputVal <= $ruleArgs[1]) { |
|
| 783 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 784 | - return; |
|
| 785 | - } |
|
| 786 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 787 | - } |
|
| 788 | - } |
|
| 774 | + /** |
|
| 775 | + * Peform validation for the rule "greater_than" |
|
| 776 | + * @param string $inputName the form field or data key name used |
|
| 777 | + * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 778 | + * @param array $ruleArgs the rules argument |
|
| 779 | + */ |
|
| 780 | + protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 781 | + $inputVal = $this->post($inputName); |
|
| 782 | + if ($inputVal <= $ruleArgs[1]) { |
|
| 783 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 784 | + return; |
|
| 785 | + } |
|
| 786 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 787 | + } |
|
| 788 | + } |
|
| 789 | 789 | |
| 790 | - /** |
|
| 791 | - * Peform validation for the rule "numeric" |
|
| 792 | - * @param string $inputName the form field or data key name used |
|
| 793 | - * @param string $ruleName the rule name for this validation ("numeric") |
|
| 794 | - * @param array $ruleArgs the rules argument |
|
| 795 | - */ |
|
| 796 | - protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 797 | - $inputVal = $this->post($inputName); |
|
| 798 | - if (! is_numeric($inputVal)) { |
|
| 799 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 800 | - return; |
|
| 801 | - } |
|
| 802 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 803 | - } |
|
| 804 | - } |
|
| 790 | + /** |
|
| 791 | + * Peform validation for the rule "numeric" |
|
| 792 | + * @param string $inputName the form field or data key name used |
|
| 793 | + * @param string $ruleName the rule name for this validation ("numeric") |
|
| 794 | + * @param array $ruleArgs the rules argument |
|
| 795 | + */ |
|
| 796 | + protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 797 | + $inputVal = $this->post($inputName); |
|
| 798 | + if (! is_numeric($inputVal)) { |
|
| 799 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 800 | + return; |
|
| 801 | + } |
|
| 802 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 803 | + } |
|
| 804 | + } |
|
| 805 | 805 | |
| 806 | - /** |
|
| 807 | - * Peform validation for the rule "exists" |
|
| 808 | - * @param string $inputName the form field or data key name used |
|
| 809 | - * @param string $ruleName the rule name for this validation ("exists") |
|
| 810 | - * @param array $ruleArgs the rules argument |
|
| 811 | - */ |
|
| 806 | + /** |
|
| 807 | + * Peform validation for the rule "exists" |
|
| 808 | + * @param string $inputName the form field or data key name used |
|
| 809 | + * @param string $ruleName the rule name for this validation ("exists") |
|
| 810 | + * @param array $ruleArgs the rules argument |
|
| 811 | + */ |
|
| 812 | 812 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
| 813 | - $inputVal = $this->post($inputName); |
|
| 814 | - $obj = & get_instance(); |
|
| 815 | - if(! isset($obj->database)){ |
|
| 816 | - return; |
|
| 817 | - } |
|
| 818 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 819 | - $obj->database->from($table) |
|
| 820 | - ->where($column, $inputVal) |
|
| 821 | - ->get(); |
|
| 822 | - $nb = $obj->database->numRows(); |
|
| 823 | - if ($nb == 0) { |
|
| 824 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 825 | - return; |
|
| 826 | - } |
|
| 827 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 828 | - } |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - /** |
|
| 832 | - * Peform validation for the rule "is_unique" |
|
| 833 | - * @param string $inputName the form field or data key name used |
|
| 834 | - * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 835 | - * @param array $ruleArgs the rules argument |
|
| 836 | - */ |
|
| 837 | - protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 838 | - $inputVal = $this->post($inputName); |
|
| 839 | - $obj = & get_instance(); |
|
| 840 | - if(! isset($obj->database)){ |
|
| 841 | - return; |
|
| 842 | - } |
|
| 843 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 844 | - $obj->database->from($table) |
|
| 845 | - ->where($column, $inputVal) |
|
| 846 | - ->get(); |
|
| 847 | - $nb = $obj->database->numRows(); |
|
| 848 | - if ($nb != 0) { |
|
| 849 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 850 | - return; |
|
| 851 | - } |
|
| 852 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 853 | - } |
|
| 854 | - } |
|
| 813 | + $inputVal = $this->post($inputName); |
|
| 814 | + $obj = & get_instance(); |
|
| 815 | + if(! isset($obj->database)){ |
|
| 816 | + return; |
|
| 817 | + } |
|
| 818 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 819 | + $obj->database->from($table) |
|
| 820 | + ->where($column, $inputVal) |
|
| 821 | + ->get(); |
|
| 822 | + $nb = $obj->database->numRows(); |
|
| 823 | + if ($nb == 0) { |
|
| 824 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 825 | + return; |
|
| 826 | + } |
|
| 827 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 828 | + } |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + /** |
|
| 832 | + * Peform validation for the rule "is_unique" |
|
| 833 | + * @param string $inputName the form field or data key name used |
|
| 834 | + * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 835 | + * @param array $ruleArgs the rules argument |
|
| 836 | + */ |
|
| 837 | + protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 838 | + $inputVal = $this->post($inputName); |
|
| 839 | + $obj = & get_instance(); |
|
| 840 | + if(! isset($obj->database)){ |
|
| 841 | + return; |
|
| 842 | + } |
|
| 843 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 844 | + $obj->database->from($table) |
|
| 845 | + ->where($column, $inputVal) |
|
| 846 | + ->get(); |
|
| 847 | + $nb = $obj->database->numRows(); |
|
| 848 | + if ($nb != 0) { |
|
| 849 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 850 | + return; |
|
| 851 | + } |
|
| 852 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 853 | + } |
|
| 854 | + } |
|
| 855 | 855 | |
| 856 | - /** |
|
| 857 | - * Peform validation for the rule "is_unique_update" |
|
| 858 | - * @param string $inputName the form field or data key name used |
|
| 859 | - * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 860 | - * @param array $ruleArgs the rules argument |
|
| 861 | - */ |
|
| 862 | - protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 863 | - $inputVal = $this->post($inputName); |
|
| 864 | - $obj = & get_instance(); |
|
| 865 | - if(! isset($obj->database)){ |
|
| 866 | - return; |
|
| 867 | - } |
|
| 868 | - $data = explode(',', $ruleArgs[1]); |
|
| 869 | - if(count($data) < 2){ |
|
| 870 | - return; |
|
| 871 | - } |
|
| 872 | - list($table, $column) = explode('.', $data[0]); |
|
| 873 | - list($field, $val) = explode('=', $data[1]); |
|
| 874 | - $obj->database->from($table) |
|
| 875 | - ->where($column, $inputVal) |
|
| 876 | - ->where($field, '!=', trim($val)) |
|
| 877 | - ->get(); |
|
| 878 | - $nb = $obj->database->numRows(); |
|
| 879 | - if ($nb != 0) { |
|
| 880 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 881 | - return; |
|
| 882 | - } |
|
| 883 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 884 | - } |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - /** |
|
| 888 | - * Peform validation for the rule "in_list" |
|
| 889 | - * @param string $inputName the form field or data key name used |
|
| 890 | - * @param string $ruleName the rule name for this validation ("in_list") |
|
| 891 | - * @param array $ruleArgs the rules argument |
|
| 892 | - */ |
|
| 893 | - protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 894 | - $inputVal = $this->post($inputName); |
|
| 895 | - $list = explode(',', $ruleArgs[1]); |
|
| 896 | - $list = array_map('trim', $list); |
|
| 897 | - if (! in_array($inputVal, $list)) { |
|
| 898 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 899 | - return; |
|
| 900 | - } |
|
| 901 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 902 | - } |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - /** |
|
| 906 | - * Peform validation for the rule "regex" |
|
| 907 | - * @param string $inputName the form field or data key name used |
|
| 908 | - * @param string $ruleName the rule name for this validation ("regex") |
|
| 909 | - * @param array $ruleArgs the rules argument |
|
| 910 | - */ |
|
| 911 | - protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 912 | - $inputVal = $this->post($inputName); |
|
| 913 | - $regex = $ruleArgs[1]; |
|
| 914 | - if (! preg_match($regex, $inputVal)) { |
|
| 915 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 916 | - return; |
|
| 917 | - } |
|
| 918 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 919 | - } |
|
| 920 | - } |
|
| 856 | + /** |
|
| 857 | + * Peform validation for the rule "is_unique_update" |
|
| 858 | + * @param string $inputName the form field or data key name used |
|
| 859 | + * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 860 | + * @param array $ruleArgs the rules argument |
|
| 861 | + */ |
|
| 862 | + protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 863 | + $inputVal = $this->post($inputName); |
|
| 864 | + $obj = & get_instance(); |
|
| 865 | + if(! isset($obj->database)){ |
|
| 866 | + return; |
|
| 867 | + } |
|
| 868 | + $data = explode(',', $ruleArgs[1]); |
|
| 869 | + if(count($data) < 2){ |
|
| 870 | + return; |
|
| 871 | + } |
|
| 872 | + list($table, $column) = explode('.', $data[0]); |
|
| 873 | + list($field, $val) = explode('=', $data[1]); |
|
| 874 | + $obj->database->from($table) |
|
| 875 | + ->where($column, $inputVal) |
|
| 876 | + ->where($field, '!=', trim($val)) |
|
| 877 | + ->get(); |
|
| 878 | + $nb = $obj->database->numRows(); |
|
| 879 | + if ($nb != 0) { |
|
| 880 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 881 | + return; |
|
| 882 | + } |
|
| 883 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 884 | + } |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + /** |
|
| 888 | + * Peform validation for the rule "in_list" |
|
| 889 | + * @param string $inputName the form field or data key name used |
|
| 890 | + * @param string $ruleName the rule name for this validation ("in_list") |
|
| 891 | + * @param array $ruleArgs the rules argument |
|
| 892 | + */ |
|
| 893 | + protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 894 | + $inputVal = $this->post($inputName); |
|
| 895 | + $list = explode(',', $ruleArgs[1]); |
|
| 896 | + $list = array_map('trim', $list); |
|
| 897 | + if (! in_array($inputVal, $list)) { |
|
| 898 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 899 | + return; |
|
| 900 | + } |
|
| 901 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 902 | + } |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + /** |
|
| 906 | + * Peform validation for the rule "regex" |
|
| 907 | + * @param string $inputName the form field or data key name used |
|
| 908 | + * @param string $ruleName the rule name for this validation ("regex") |
|
| 909 | + * @param array $ruleArgs the rules argument |
|
| 910 | + */ |
|
| 911 | + protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 912 | + $inputVal = $this->post($inputName); |
|
| 913 | + $regex = $ruleArgs[1]; |
|
| 914 | + if (! preg_match($regex, $inputVal)) { |
|
| 915 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 916 | + return; |
|
| 917 | + } |
|
| 918 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 919 | + } |
|
| 920 | + } |
|
| 921 | 921 | |
| 922 | - } |
|
| 922 | + } |
|
@@ -25,13 +25,13 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | 27 | |
| 28 | - class FormValidation{ |
|
| 28 | + class FormValidation { |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * The form validation status |
| 32 | 32 | * @var boolean |
| 33 | 33 | */ |
| 34 | - protected $_success = false; |
|
| 34 | + protected $_success = false; |
|
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * The list of errors messages |
@@ -40,31 +40,31 @@ discard block |
||
| 40 | 40 | protected $_errorsMessages = array(); |
| 41 | 41 | |
| 42 | 42 | // Array of rule sets, fieldName => PIPE seperated ruleString |
| 43 | - protected $_rules = array(); |
|
| 43 | + protected $_rules = array(); |
|
| 44 | 44 | |
| 45 | 45 | // Array of errors, niceName => Error Message |
| 46 | - protected $_errors = array(); |
|
| 46 | + protected $_errors = array(); |
|
| 47 | 47 | |
| 48 | 48 | // Array of post Key => Nice name labels |
| 49 | - protected $_labels = array(); |
|
| 49 | + protected $_labels = array(); |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * The errors delimiters |
| 53 | 53 | * @var array |
| 54 | 54 | */ |
| 55 | - protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
| 55 | + protected $_allErrorsDelimiter = array('<div class="error">', '</div>'); |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * The each error delimiter |
| 59 | 59 | * @var array |
| 60 | 60 | */ |
| 61 | - protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
| 61 | + protected $_eachErrorDelimiter = array('<p class="error">', '</p>'); |
|
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * Indicated if need force the validation to be failed |
| 65 | 65 | * @var boolean |
| 66 | 66 | */ |
| 67 | - protected $_forceFail = false; |
|
| 67 | + protected $_forceFail = false; |
|
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * The list of the error messages overrides by the original |
@@ -98,13 +98,13 @@ discard block |
||
| 98 | 98 | * @return void |
| 99 | 99 | */ |
| 100 | 100 | public function __construct() { |
| 101 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 101 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 102 | 102 | $this->logger->setLogger('Library::FormValidation'); |
| 103 | 103 | |
| 104 | 104 | //Load form validation language message |
| 105 | 105 | Loader::lang('form_validation'); |
| 106 | 106 | $obj = & get_instance(); |
| 107 | - $this->_errorsMessages = array( |
|
| 107 | + $this->_errorsMessages = array( |
|
| 108 | 108 | 'required' => $obj->lang->get('fv_required'), |
| 109 | 109 | 'min_length' => $obj->lang->get('fv_min_length'), |
| 110 | 110 | 'max_length' => $obj->lang->get('fv_max_length'), |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | $this->_success = false; |
| 142 | 142 | $this->_forceFail = false; |
| 143 | 143 | $this->data = array(); |
| 144 | - $this->enableCsrfCheck = false; |
|
| 144 | + $this->enableCsrfCheck = false; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @return FormValidation Current instance of object. |
| 152 | 152 | */ |
| 153 | - public function setData(array $data){ |
|
| 153 | + public function setData(array $data) { |
|
| 154 | 154 | $this->logger->debug('Setting the form validation data, the values are: ' . stringfy_vars($data)); |
| 155 | 155 | $this->data = $data; |
| 156 | 156 | return $this; |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | * Get the form validation data |
| 161 | 161 | * @return array the form validation data to be validated |
| 162 | 162 | */ |
| 163 | - public function getData(){ |
|
| 163 | + public function getData() { |
|
| 164 | 164 | return $this->data; |
| 165 | 165 | } |
| 166 | 166 | |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * |
| 170 | 170 | * @return string the function name |
| 171 | 171 | */ |
| 172 | - protected function _toCallCase($funcName, $prefix='_validate') { |
|
| 172 | + protected function _toCallCase($funcName, $prefix = '_validate') { |
|
| 173 | 173 | $funcName = strtolower($funcName); |
| 174 | 174 | $finalFuncName = $prefix; |
| 175 | 175 | foreach (explode('_', $funcName) as $funcNamePart) { |
@@ -193,7 +193,7 @@ discard block |
||
| 193 | 193 | * @return boolean Whether or not the form has been submitted or the data is available for validation. |
| 194 | 194 | */ |
| 195 | 195 | public function canDoValidation() { |
| 196 | - return get_instance()->request->method() === 'POST' || ! empty($this->data); |
|
| 196 | + return get_instance()->request->method() === 'POST' || !empty($this->data); |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | /** |
@@ -213,14 +213,14 @@ discard block |
||
| 213 | 213 | * Validate the CSRF |
| 214 | 214 | * @return void |
| 215 | 215 | */ |
| 216 | - protected function validateCSRF(){ |
|
| 217 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 216 | + protected function validateCSRF() { |
|
| 217 | + if (get_instance()->request->method() == 'POST' || $this->enableCsrfCheck) { |
|
| 218 | 218 | $this->logger->debug('Check if CSRF is enabled in configuration'); |
| 219 | 219 | //first check for CSRF |
| 220 | - if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 220 | + if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && !Security::validateCSRF()) { |
|
| 221 | 221 | show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
| 222 | 222 | } |
| 223 | - else{ |
|
| 223 | + else { |
|
| 224 | 224 | $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
| 225 | 225 | } |
| 226 | 226 | } |
@@ -237,10 +237,10 @@ discard block |
||
| 237 | 237 | $this->_forceFail = false; |
| 238 | 238 | |
| 239 | 239 | foreach ($this->getData() as $inputName => $inputVal) { |
| 240 | - if(is_array($this->data[$inputName])){ |
|
| 240 | + if (is_array($this->data[$inputName])) { |
|
| 241 | 241 | $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
| 242 | 242 | } |
| 243 | - else{ |
|
| 243 | + else { |
|
| 244 | 244 | $this->data[$inputName] = trim($this->data[$inputName]); |
| 245 | 245 | } |
| 246 | 246 | |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | public function setRule($inputField, $inputLabel, $ruleSets) { |
| 268 | 268 | $this->_rules[$inputField] = $ruleSets; |
| 269 | 269 | $this->_labels[$inputField] = $inputLabel; |
| 270 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 270 | + $this->logger->info('Set the field rule: name [' . $inputField . '], label [' . $inputLabel . '], rules [' . $ruleSets . ']'); |
|
| 271 | 271 | return $this; |
| 272 | 272 | } |
| 273 | 273 | |
@@ -431,7 +431,7 @@ discard block |
||
| 431 | 431 | } |
| 432 | 432 | $errorOutput .= $errorsEnd; |
| 433 | 433 | echo ($echo) ? $errorOutput : ''; |
| 434 | - return (! $echo) ? $errorOutput : null; |
|
| 434 | + return (!$echo) ? $errorOutput : null; |
|
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | /** |
@@ -456,25 +456,25 @@ discard block |
||
| 456 | 456 | /* |
| 457 | 457 | //////////////// hack for regex rule that can contain "|" |
| 458 | 458 | */ |
| 459 | - if(strpos($ruleString, 'regex') !== false){ |
|
| 459 | + if (strpos($ruleString, 'regex') !== false) { |
|
| 460 | 460 | $regexRule = array(); |
| 461 | 461 | $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
| 462 | 462 | preg_match($rule, $ruleString, $regexRule); |
| 463 | 463 | $ruleStringTemp = preg_replace($rule, '', $ruleString); |
| 464 | - if(!empty($regexRule[0])){ |
|
| 464 | + if (!empty($regexRule[0])) { |
|
| 465 | 465 | $ruleSets[] = $regexRule[0]; |
| 466 | 466 | } |
| 467 | 467 | $ruleStringRegex = explode('|', $ruleStringTemp); |
| 468 | 468 | foreach ($ruleStringRegex as $rule) { |
| 469 | 469 | $rule = trim($rule); |
| 470 | - if($rule){ |
|
| 470 | + if ($rule) { |
|
| 471 | 471 | $ruleSets[] = $rule; |
| 472 | 472 | } |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | } |
| 476 | 476 | /***********************************/ |
| 477 | - else{ |
|
| 477 | + else { |
|
| 478 | 478 | if (strpos($ruleString, '|') !== FALSE) { |
| 479 | 479 | $ruleSets = explode('|', $ruleString); |
| 480 | 480 | } else { |
@@ -506,7 +506,7 @@ discard block |
||
| 506 | 506 | * @return void |
| 507 | 507 | */ |
| 508 | 508 | protected function _validateRule($inputName, $inputVal, $ruleName) { |
| 509 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 509 | + $this->logger->debug('Rule validation of field [' . $inputName . '], value [' . $inputVal . '], rule [' . $ruleName . ']'); |
|
| 510 | 510 | // Array to store args |
| 511 | 511 | $ruleArgs = array(); |
| 512 | 512 | |
@@ -551,7 +551,7 @@ discard block |
||
| 551 | 551 | $key = $i - 1; |
| 552 | 552 | $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
| 553 | 553 | } |
| 554 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
| 554 | + if (!array_key_exists($inputName, $this->_errors)) { |
|
| 555 | 555 | $this->_errors[$inputName] = $rulePhrase; |
| 556 | 556 | } |
| 557 | 557 | } |
@@ -603,13 +603,13 @@ discard block |
||
| 603 | 603 | */ |
| 604 | 604 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
| 605 | 605 | $inputVal = $this->post($inputName); |
| 606 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 606 | + if (array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 607 | 607 | $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
| 608 | 608 | if ($inputVal == '' && $callbackReturn == true) { |
| 609 | 609 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 610 | 610 | } |
| 611 | 611 | } |
| 612 | - else if($inputVal == '') { |
|
| 612 | + else if ($inputVal == '') { |
|
| 613 | 613 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 614 | 614 | } |
| 615 | 615 | } |
@@ -635,7 +635,7 @@ discard block |
||
| 635 | 635 | protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
| 636 | 636 | if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
| 637 | 637 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
| 638 | - if(! $result){ |
|
| 638 | + if (!$result) { |
|
| 639 | 639 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
| 640 | 640 | } |
| 641 | 641 | } |
@@ -669,7 +669,7 @@ discard block |
||
| 669 | 669 | continue; |
| 670 | 670 | } |
| 671 | 671 | } |
| 672 | - else{ |
|
| 672 | + else { |
|
| 673 | 673 | if ($inputVal == $doNotEqual) { |
| 674 | 674 | $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
| 675 | 675 | continue; |
@@ -699,8 +699,8 @@ discard block |
||
| 699 | 699 | */ |
| 700 | 700 | protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
| 701 | 701 | $inputVal = $this->post($inputName); |
| 702 | - if (! preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
| 703 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 702 | + if (!preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $inputVal)) { |
|
| 703 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 704 | 704 | return; |
| 705 | 705 | } |
| 706 | 706 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
@@ -716,7 +716,7 @@ discard block |
||
| 716 | 716 | protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
| 717 | 717 | $inputVal = $this->post($inputName); |
| 718 | 718 | if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 719 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 719 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 720 | 720 | return; |
| 721 | 721 | } |
| 722 | 722 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -732,7 +732,7 @@ discard block |
||
| 732 | 732 | protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
| 733 | 733 | $inputVal = $this->post($inputName); |
| 734 | 734 | if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 735 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 735 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 736 | 736 | return; |
| 737 | 737 | } |
| 738 | 738 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -748,7 +748,7 @@ discard block |
||
| 748 | 748 | protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
| 749 | 749 | $inputVal = $this->post($inputName); |
| 750 | 750 | if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
| 751 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 751 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 752 | 752 | return; |
| 753 | 753 | } |
| 754 | 754 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -764,7 +764,7 @@ discard block |
||
| 764 | 764 | protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
| 765 | 765 | $inputVal = $this->post($inputName); |
| 766 | 766 | if ($inputVal >= $ruleArgs[1]) { |
| 767 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 767 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 768 | 768 | return; |
| 769 | 769 | } |
| 770 | 770 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -780,7 +780,7 @@ discard block |
||
| 780 | 780 | protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
| 781 | 781 | $inputVal = $this->post($inputName); |
| 782 | 782 | if ($inputVal <= $ruleArgs[1]) { |
| 783 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 783 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 784 | 784 | return; |
| 785 | 785 | } |
| 786 | 786 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -795,8 +795,8 @@ discard block |
||
| 795 | 795 | */ |
| 796 | 796 | protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
| 797 | 797 | $inputVal = $this->post($inputName); |
| 798 | - if (! is_numeric($inputVal)) { |
|
| 799 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 798 | + if (!is_numeric($inputVal)) { |
|
| 799 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 800 | 800 | return; |
| 801 | 801 | } |
| 802 | 802 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -812,7 +812,7 @@ discard block |
||
| 812 | 812 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
| 813 | 813 | $inputVal = $this->post($inputName); |
| 814 | 814 | $obj = & get_instance(); |
| 815 | - if(! isset($obj->database)){ |
|
| 815 | + if (!isset($obj->database)) { |
|
| 816 | 816 | return; |
| 817 | 817 | } |
| 818 | 818 | list($table, $column) = explode('.', $ruleArgs[1]); |
@@ -821,7 +821,7 @@ discard block |
||
| 821 | 821 | ->get(); |
| 822 | 822 | $nb = $obj->database->numRows(); |
| 823 | 823 | if ($nb == 0) { |
| 824 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 824 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 825 | 825 | return; |
| 826 | 826 | } |
| 827 | 827 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -837,7 +837,7 @@ discard block |
||
| 837 | 837 | protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
| 838 | 838 | $inputVal = $this->post($inputName); |
| 839 | 839 | $obj = & get_instance(); |
| 840 | - if(! isset($obj->database)){ |
|
| 840 | + if (!isset($obj->database)) { |
|
| 841 | 841 | return; |
| 842 | 842 | } |
| 843 | 843 | list($table, $column) = explode('.', $ruleArgs[1]); |
@@ -846,7 +846,7 @@ discard block |
||
| 846 | 846 | ->get(); |
| 847 | 847 | $nb = $obj->database->numRows(); |
| 848 | 848 | if ($nb != 0) { |
| 849 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 849 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 850 | 850 | return; |
| 851 | 851 | } |
| 852 | 852 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -862,11 +862,11 @@ discard block |
||
| 862 | 862 | protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
| 863 | 863 | $inputVal = $this->post($inputName); |
| 864 | 864 | $obj = & get_instance(); |
| 865 | - if(! isset($obj->database)){ |
|
| 865 | + if (!isset($obj->database)) { |
|
| 866 | 866 | return; |
| 867 | 867 | } |
| 868 | 868 | $data = explode(',', $ruleArgs[1]); |
| 869 | - if(count($data) < 2){ |
|
| 869 | + if (count($data) < 2) { |
|
| 870 | 870 | return; |
| 871 | 871 | } |
| 872 | 872 | list($table, $column) = explode('.', $data[0]); |
@@ -877,7 +877,7 @@ discard block |
||
| 877 | 877 | ->get(); |
| 878 | 878 | $nb = $obj->database->numRows(); |
| 879 | 879 | if ($nb != 0) { |
| 880 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 880 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 881 | 881 | return; |
| 882 | 882 | } |
| 883 | 883 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -894,8 +894,8 @@ discard block |
||
| 894 | 894 | $inputVal = $this->post($inputName); |
| 895 | 895 | $list = explode(',', $ruleArgs[1]); |
| 896 | 896 | $list = array_map('trim', $list); |
| 897 | - if (! in_array($inputVal, $list)) { |
|
| 898 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 897 | + if (!in_array($inputVal, $list)) { |
|
| 898 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 899 | 899 | return; |
| 900 | 900 | } |
| 901 | 901 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
@@ -911,8 +911,8 @@ discard block |
||
| 911 | 911 | protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
| 912 | 912 | $inputVal = $this->post($inputName); |
| 913 | 913 | $regex = $ruleArgs[1]; |
| 914 | - if (! preg_match($regex, $inputVal)) { |
|
| 915 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 914 | + if (!preg_match($regex, $inputVal)) { |
|
| 915 | + if (!$this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 916 | 916 | return; |
| 917 | 917 | } |
| 918 | 918 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
@@ -1,807 +1,807 @@ |
||
| 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 | - } |
|
| 260 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 261 | - } |
|
| 262 | - /** |
|
| 263 | - * Set input. |
|
| 264 | - * If you have $_FILES["file"], you must use the key "file" |
|
| 265 | - * Example: |
|
| 266 | - * $object->setInput("file"); |
|
| 267 | - * |
|
| 268 | - * @since 1.0 |
|
| 269 | - * @version 1.0 |
|
| 270 | - * @param string $input |
|
| 271 | - * @return object |
|
| 272 | - * @method boolean setInput |
|
| 273 | - */ |
|
| 274 | - public function setInput($input) |
|
| 275 | - { |
|
| 276 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 277 | - $this->input = $input; |
|
| 278 | - } |
|
| 279 | - return $this; |
|
| 280 | - } |
|
| 281 | - /** |
|
| 282 | - * Set new filename |
|
| 283 | - * Example: |
|
| 284 | - * FileUpload::setFilename("new file.txt") |
|
| 285 | - * Remember: |
|
| 286 | - * Use %s to retrive file extension |
|
| 287 | - * |
|
| 288 | - * @since 1.0 |
|
| 289 | - * @version 1.0 |
|
| 290 | - * @param string $filename |
|
| 291 | - * @return object |
|
| 292 | - * @method boolean setFilename |
|
| 293 | - */ |
|
| 294 | - public function setFilename($filename) |
|
| 295 | - { |
|
| 296 | - if ($this->isFilename($filename)) { |
|
| 297 | - $this->filename = $filename; |
|
| 298 | - } |
|
| 299 | - return $this; |
|
| 300 | - } |
|
| 301 | - /** |
|
| 302 | - * Set automatic filename |
|
| 303 | - * |
|
| 304 | - * @since 1.0 |
|
| 305 | - * @version 1.5 |
|
| 306 | - * @param string $extension |
|
| 307 | - * @return object |
|
| 308 | - * @method boolean setAutoFilename |
|
| 309 | - */ |
|
| 310 | - public function setAutoFilename() |
|
| 311 | - { |
|
| 312 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 313 | - $this->filename .= time(); |
|
| 314 | - return $this; |
|
| 315 | - } |
|
| 316 | - /** |
|
| 317 | - * Set file size limit |
|
| 318 | - * |
|
| 319 | - * @since 1.0 |
|
| 320 | - * @version 1.0 |
|
| 321 | - * @param double $file_size |
|
| 322 | - * @return object |
|
| 323 | - * @method boolean setMaxFileSize |
|
| 324 | - */ |
|
| 325 | - public function setMaxFileSize($file_size) |
|
| 326 | - { |
|
| 327 | - $file_size = $this->sizeInBytes($file_size); |
|
| 328 | - if (is_numeric($file_size) && $file_size > -1) { |
|
| 329 | - // Get php config |
|
| 330 | - $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
|
| 331 | - // Calculate difference |
|
| 332 | - if ($php_size < $file_size) { |
|
| 333 | - $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. ']'); |
|
| 334 | - } |
|
| 335 | - $this->max_file_size = $file_size; |
|
| 336 | - } |
|
| 337 | - return $this; |
|
| 338 | - } |
|
| 339 | - /** |
|
| 340 | - * Set array mime types |
|
| 341 | - * |
|
| 342 | - * @since 1.0 |
|
| 343 | - * @version 1.0 |
|
| 344 | - * @param array $mimes |
|
| 345 | - * @return object |
|
| 346 | - * @method boolean setAllowedMimeTypes |
|
| 347 | - */ |
|
| 348 | - public function setAllowedMimeTypes(array $mimes) |
|
| 349 | - { |
|
| 350 | - if (count($mimes) > 0) { |
|
| 351 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 352 | - } |
|
| 353 | - return $this; |
|
| 354 | - } |
|
| 355 | - /** |
|
| 356 | - * Set input callback |
|
| 357 | - * |
|
| 358 | - * @since 1.0 |
|
| 359 | - * @version 1.0 |
|
| 360 | - * @param mixed $callback |
|
| 361 | - * @return object |
|
| 362 | - * @method boolean setCallbackInput |
|
| 363 | - */ |
|
| 364 | - public function setCallbackInput($callback) |
|
| 365 | - { |
|
| 366 | - if (is_callable($callback, false)) { |
|
| 367 | - $this->callbacks['input'] = $callback; |
|
| 368 | - } |
|
| 369 | - return $this; |
|
| 370 | - } |
|
| 371 | - /** |
|
| 372 | - * Set output callback |
|
| 373 | - * |
|
| 374 | - * @since 1.0 |
|
| 375 | - * @version 1.0 |
|
| 376 | - * @param mixed $callback |
|
| 377 | - * @return object |
|
| 378 | - * @method boolean setCallbackOutput |
|
| 379 | - */ |
|
| 380 | - public function setCallbackOutput($callback) |
|
| 381 | - { |
|
| 382 | - if (is_callable($callback, false)) { |
|
| 383 | - $this->callbacks['output'] = $callback; |
|
| 384 | - } |
|
| 385 | - return $this; |
|
| 386 | - } |
|
| 387 | - /** |
|
| 388 | - * Append a mime type to allowed mime types |
|
| 389 | - * |
|
| 390 | - * @since 1.0 |
|
| 391 | - * @version 1.0.1 |
|
| 392 | - * @param string $mime |
|
| 393 | - * @return object |
|
| 394 | - * @method boolean setAllowMimeType |
|
| 395 | - */ |
|
| 396 | - public function setAllowMimeType($mime) |
|
| 397 | - { |
|
| 398 | - if (!empty($mime) && is_string($mime)) { |
|
| 399 | - $this->allowed_mime_types[] = strtolower($mime); |
|
| 400 | - $this->file['allowed_mime_types'][] = strtolower($mime); |
|
| 401 | - } |
|
| 402 | - return $this; |
|
| 403 | - } |
|
| 404 | - /** |
|
| 405 | - * Set allowed mime types from mime helping |
|
| 406 | - * |
|
| 407 | - * @since 1.0.1 |
|
| 408 | - * @version 1.0.1 |
|
| 409 | - * @return object |
|
| 410 | - * @method boolean setMimeHelping |
|
| 411 | - */ |
|
| 412 | - public function setMimeHelping($name) |
|
| 413 | - { |
|
| 414 | - if (!empty($name) && is_string($name)) { |
|
| 415 | - if (array_key_exists($name, $this->mime_helping)) { |
|
| 416 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - return $this; |
|
| 420 | - } |
|
| 421 | - /** |
|
| 422 | - * Set function to upload file |
|
| 423 | - * Examples: |
|
| 424 | - * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
| 425 | - * 2.- FileUpload::setUploadFunction("copy"); |
|
| 426 | - * |
|
| 427 | - * @since 1.0 |
|
| 428 | - * @version 1.0 |
|
| 429 | - * @param string $function |
|
| 430 | - * @return object |
|
| 431 | - * @method boolean setUploadFunction |
|
| 432 | - */ |
|
| 433 | - public function setUploadFunction($function) |
|
| 434 | - { |
|
| 435 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 436 | - if (is_callable( $function)) { |
|
| 437 | - $this->upload_function = $function; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - return $this; |
|
| 441 | - } |
|
| 442 | - /** |
|
| 443 | - * Clear allowed mime types cache |
|
| 444 | - * |
|
| 445 | - * @since 1.0 |
|
| 446 | - * @version 1.0 |
|
| 447 | - * @return object |
|
| 448 | - * @method boolean clearAllowedMimeTypes |
|
| 449 | - */ |
|
| 450 | - public function clearAllowedMimeTypes() |
|
| 451 | - { |
|
| 452 | - $this->allowed_mime_types = array(); |
|
| 453 | - $this->file['allowed_mime_types'] = array(); |
|
| 454 | - return $this; |
|
| 455 | - } |
|
| 456 | - /** |
|
| 457 | - * Set destination output |
|
| 458 | - * |
|
| 459 | - * @since 1.0 |
|
| 460 | - * @version 1.0 |
|
| 461 | - * @param string $destination_directory Destination path |
|
| 462 | - * @param boolean $create_if_not_exist |
|
| 463 | - * @return object |
|
| 464 | - * @method boolean setDestinationDirectory |
|
| 465 | - */ |
|
| 466 | - public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
| 467 | - $destination_directory = realpath($destination_directory); |
|
| 468 | - if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
| 469 | - $destination_directory .= DIRECTORY_SEPARATOR; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - if ($this->isDirpath($destination_directory)) { |
|
| 473 | - if ($this->dirExists($destination_directory)) { |
|
| 474 | - $this->destination_directory = $destination_directory; |
|
| 475 | - chdir($destination_directory); |
|
| 476 | - } else if ($create_if_not_exist === true) { |
|
| 477 | - if (mkdir($destination_directory, 0775, true)) { |
|
| 478 | - $this->destination_directory = $destination_directory; |
|
| 479 | - chdir($destination_directory); |
|
| 480 | - } |
|
| 481 | - else{ |
|
| 482 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - } |
|
| 486 | - return $this; |
|
| 487 | - } |
|
| 488 | - /** |
|
| 489 | - * Check file exists |
|
| 490 | - * |
|
| 491 | - * @since 1.0 |
|
| 492 | - * @version 1.0.1 |
|
| 493 | - * @param string $file_destination |
|
| 494 | - * @return boolean |
|
| 495 | - * @method boolean fileExists |
|
| 496 | - */ |
|
| 497 | - public function fileExists($file_destination) |
|
| 498 | - { |
|
| 499 | - if ($this->isFilename($file_destination)) { |
|
| 500 | - return (file_exists($file_destination) && is_file($file_destination)); |
|
| 501 | - } |
|
| 502 | - return false; |
|
| 503 | - } |
|
| 504 | - /** |
|
| 505 | - * Check dir exists |
|
| 506 | - * |
|
| 507 | - * @since 1.0 |
|
| 508 | - * @version 1.0.1 |
|
| 509 | - * @param string $path |
|
| 510 | - * @return boolean |
|
| 511 | - * @method boolean dirExists |
|
| 512 | - */ |
|
| 513 | - public function dirExists($path) |
|
| 514 | - { |
|
| 515 | - if ($this->isDirpath($path)) { |
|
| 516 | - return (file_exists($path) && is_dir($path)); |
|
| 517 | - } |
|
| 518 | - return false; |
|
| 519 | - } |
|
| 520 | - /** |
|
| 521 | - * Check valid filename |
|
| 522 | - * |
|
| 523 | - * @since 1.0 |
|
| 524 | - * @version 1.0.1 |
|
| 525 | - * @param string $filename |
|
| 526 | - * @return boolean |
|
| 527 | - * @method boolean isFilename |
|
| 528 | - */ |
|
| 529 | - public function isFilename($filename) |
|
| 530 | - { |
|
| 531 | - $filename = basename($filename); |
|
| 532 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 533 | - } |
|
| 534 | - /** |
|
| 535 | - * Validate mime type with allowed mime types, |
|
| 536 | - * but if allowed mime types is empty, this method return true |
|
| 537 | - * |
|
| 538 | - * @since 1.0 |
|
| 539 | - * @version 1.0 |
|
| 540 | - * @param string $mime |
|
| 541 | - * @return boolean |
|
| 542 | - * @method boolean checkMimeType |
|
| 543 | - */ |
|
| 544 | - public function checkMimeType($mime) |
|
| 545 | - { |
|
| 546 | - if (count($this->allowed_mime_types) == 0) { |
|
| 547 | - return true; |
|
| 548 | - } |
|
| 549 | - return in_array(strtolower($mime), $this->allowed_mime_types); |
|
| 550 | - } |
|
| 551 | - /** |
|
| 552 | - * Retrive status of upload |
|
| 553 | - * |
|
| 554 | - * @since 1.0 |
|
| 555 | - * @version 1.0 |
|
| 556 | - * @return boolean |
|
| 557 | - * @method boolean getStatus |
|
| 558 | - */ |
|
| 559 | - public function getStatus() |
|
| 560 | - { |
|
| 561 | - return $this->file['status']; |
|
| 562 | - } |
|
| 563 | - /** |
|
| 564 | - * Check valid path |
|
| 565 | - * |
|
| 566 | - * @since 1.0 |
|
| 567 | - * @version 1.0.1 |
|
| 568 | - * @param string $filename |
|
| 569 | - * @return boolean |
|
| 570 | - * @method boolean isDirpath |
|
| 571 | - */ |
|
| 572 | - public function isDirpath($path) |
|
| 573 | - { |
|
| 574 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 575 | - if (DIRECTORY_SEPARATOR == '/') { |
|
| 576 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 577 | - } else { |
|
| 578 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 579 | - } |
|
| 580 | - } |
|
| 581 | - return false; |
|
| 582 | - } |
|
| 583 | - /** |
|
| 584 | - * Allow overwriting files |
|
| 585 | - * |
|
| 586 | - * @since 1.0 |
|
| 587 | - * @version 1.0 |
|
| 588 | - * @return object |
|
| 589 | - * @method boolean allowOverwriting |
|
| 590 | - */ |
|
| 591 | - public function allowOverwriting() |
|
| 592 | - { |
|
| 593 | - $this->overwrite_file = true; |
|
| 594 | - return $this; |
|
| 595 | - } |
|
| 596 | - /** |
|
| 597 | - * File info |
|
| 598 | - * |
|
| 599 | - * @since 1.0 |
|
| 600 | - * @version 1.0 |
|
| 601 | - * @return object |
|
| 602 | - * @method object getInfo |
|
| 603 | - */ |
|
| 604 | - public function getInfo() |
|
| 605 | - { |
|
| 606 | - return (object)$this->file; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Check if the file is uploaded |
|
| 612 | - * @return boolean |
|
| 613 | - */ |
|
| 614 | - public function isUploaded(){ |
|
| 615 | - return isset($this->file_array[$this->input]) |
|
| 616 | - && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
| 617 | - } |
|
| 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 | + } |
|
| 260 | + $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 261 | + } |
|
| 262 | + /** |
|
| 263 | + * Set input. |
|
| 264 | + * If you have $_FILES["file"], you must use the key "file" |
|
| 265 | + * Example: |
|
| 266 | + * $object->setInput("file"); |
|
| 267 | + * |
|
| 268 | + * @since 1.0 |
|
| 269 | + * @version 1.0 |
|
| 270 | + * @param string $input |
|
| 271 | + * @return object |
|
| 272 | + * @method boolean setInput |
|
| 273 | + */ |
|
| 274 | + public function setInput($input) |
|
| 275 | + { |
|
| 276 | + if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 277 | + $this->input = $input; |
|
| 278 | + } |
|
| 279 | + return $this; |
|
| 280 | + } |
|
| 281 | + /** |
|
| 282 | + * Set new filename |
|
| 283 | + * Example: |
|
| 284 | + * FileUpload::setFilename("new file.txt") |
|
| 285 | + * Remember: |
|
| 286 | + * Use %s to retrive file extension |
|
| 287 | + * |
|
| 288 | + * @since 1.0 |
|
| 289 | + * @version 1.0 |
|
| 290 | + * @param string $filename |
|
| 291 | + * @return object |
|
| 292 | + * @method boolean setFilename |
|
| 293 | + */ |
|
| 294 | + public function setFilename($filename) |
|
| 295 | + { |
|
| 296 | + if ($this->isFilename($filename)) { |
|
| 297 | + $this->filename = $filename; |
|
| 298 | + } |
|
| 299 | + return $this; |
|
| 300 | + } |
|
| 301 | + /** |
|
| 302 | + * Set automatic filename |
|
| 303 | + * |
|
| 304 | + * @since 1.0 |
|
| 305 | + * @version 1.5 |
|
| 306 | + * @param string $extension |
|
| 307 | + * @return object |
|
| 308 | + * @method boolean setAutoFilename |
|
| 309 | + */ |
|
| 310 | + public function setAutoFilename() |
|
| 311 | + { |
|
| 312 | + $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 313 | + $this->filename .= time(); |
|
| 314 | + return $this; |
|
| 315 | + } |
|
| 316 | + /** |
|
| 317 | + * Set file size limit |
|
| 318 | + * |
|
| 319 | + * @since 1.0 |
|
| 320 | + * @version 1.0 |
|
| 321 | + * @param double $file_size |
|
| 322 | + * @return object |
|
| 323 | + * @method boolean setMaxFileSize |
|
| 324 | + */ |
|
| 325 | + public function setMaxFileSize($file_size) |
|
| 326 | + { |
|
| 327 | + $file_size = $this->sizeInBytes($file_size); |
|
| 328 | + if (is_numeric($file_size) && $file_size > -1) { |
|
| 329 | + // Get php config |
|
| 330 | + $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
|
| 331 | + // Calculate difference |
|
| 332 | + if ($php_size < $file_size) { |
|
| 333 | + $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. ']'); |
|
| 334 | + } |
|
| 335 | + $this->max_file_size = $file_size; |
|
| 336 | + } |
|
| 337 | + return $this; |
|
| 338 | + } |
|
| 339 | + /** |
|
| 340 | + * Set array mime types |
|
| 341 | + * |
|
| 342 | + * @since 1.0 |
|
| 343 | + * @version 1.0 |
|
| 344 | + * @param array $mimes |
|
| 345 | + * @return object |
|
| 346 | + * @method boolean setAllowedMimeTypes |
|
| 347 | + */ |
|
| 348 | + public function setAllowedMimeTypes(array $mimes) |
|
| 349 | + { |
|
| 350 | + if (count($mimes) > 0) { |
|
| 351 | + array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 352 | + } |
|
| 353 | + return $this; |
|
| 354 | + } |
|
| 355 | + /** |
|
| 356 | + * Set input callback |
|
| 357 | + * |
|
| 358 | + * @since 1.0 |
|
| 359 | + * @version 1.0 |
|
| 360 | + * @param mixed $callback |
|
| 361 | + * @return object |
|
| 362 | + * @method boolean setCallbackInput |
|
| 363 | + */ |
|
| 364 | + public function setCallbackInput($callback) |
|
| 365 | + { |
|
| 366 | + if (is_callable($callback, false)) { |
|
| 367 | + $this->callbacks['input'] = $callback; |
|
| 368 | + } |
|
| 369 | + return $this; |
|
| 370 | + } |
|
| 371 | + /** |
|
| 372 | + * Set output callback |
|
| 373 | + * |
|
| 374 | + * @since 1.0 |
|
| 375 | + * @version 1.0 |
|
| 376 | + * @param mixed $callback |
|
| 377 | + * @return object |
|
| 378 | + * @method boolean setCallbackOutput |
|
| 379 | + */ |
|
| 380 | + public function setCallbackOutput($callback) |
|
| 381 | + { |
|
| 382 | + if (is_callable($callback, false)) { |
|
| 383 | + $this->callbacks['output'] = $callback; |
|
| 384 | + } |
|
| 385 | + return $this; |
|
| 386 | + } |
|
| 387 | + /** |
|
| 388 | + * Append a mime type to allowed mime types |
|
| 389 | + * |
|
| 390 | + * @since 1.0 |
|
| 391 | + * @version 1.0.1 |
|
| 392 | + * @param string $mime |
|
| 393 | + * @return object |
|
| 394 | + * @method boolean setAllowMimeType |
|
| 395 | + */ |
|
| 396 | + public function setAllowMimeType($mime) |
|
| 397 | + { |
|
| 398 | + if (!empty($mime) && is_string($mime)) { |
|
| 399 | + $this->allowed_mime_types[] = strtolower($mime); |
|
| 400 | + $this->file['allowed_mime_types'][] = strtolower($mime); |
|
| 401 | + } |
|
| 402 | + return $this; |
|
| 403 | + } |
|
| 404 | + /** |
|
| 405 | + * Set allowed mime types from mime helping |
|
| 406 | + * |
|
| 407 | + * @since 1.0.1 |
|
| 408 | + * @version 1.0.1 |
|
| 409 | + * @return object |
|
| 410 | + * @method boolean setMimeHelping |
|
| 411 | + */ |
|
| 412 | + public function setMimeHelping($name) |
|
| 413 | + { |
|
| 414 | + if (!empty($name) && is_string($name)) { |
|
| 415 | + if (array_key_exists($name, $this->mime_helping)) { |
|
| 416 | + return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + return $this; |
|
| 420 | + } |
|
| 421 | + /** |
|
| 422 | + * Set function to upload file |
|
| 423 | + * Examples: |
|
| 424 | + * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
| 425 | + * 2.- FileUpload::setUploadFunction("copy"); |
|
| 426 | + * |
|
| 427 | + * @since 1.0 |
|
| 428 | + * @version 1.0 |
|
| 429 | + * @param string $function |
|
| 430 | + * @return object |
|
| 431 | + * @method boolean setUploadFunction |
|
| 432 | + */ |
|
| 433 | + public function setUploadFunction($function) |
|
| 434 | + { |
|
| 435 | + if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 436 | + if (is_callable( $function)) { |
|
| 437 | + $this->upload_function = $function; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + return $this; |
|
| 441 | + } |
|
| 442 | + /** |
|
| 443 | + * Clear allowed mime types cache |
|
| 444 | + * |
|
| 445 | + * @since 1.0 |
|
| 446 | + * @version 1.0 |
|
| 447 | + * @return object |
|
| 448 | + * @method boolean clearAllowedMimeTypes |
|
| 449 | + */ |
|
| 450 | + public function clearAllowedMimeTypes() |
|
| 451 | + { |
|
| 452 | + $this->allowed_mime_types = array(); |
|
| 453 | + $this->file['allowed_mime_types'] = array(); |
|
| 454 | + return $this; |
|
| 455 | + } |
|
| 456 | + /** |
|
| 457 | + * Set destination output |
|
| 458 | + * |
|
| 459 | + * @since 1.0 |
|
| 460 | + * @version 1.0 |
|
| 461 | + * @param string $destination_directory Destination path |
|
| 462 | + * @param boolean $create_if_not_exist |
|
| 463 | + * @return object |
|
| 464 | + * @method boolean setDestinationDirectory |
|
| 465 | + */ |
|
| 466 | + public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
| 467 | + $destination_directory = realpath($destination_directory); |
|
| 468 | + if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
| 469 | + $destination_directory .= DIRECTORY_SEPARATOR; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + if ($this->isDirpath($destination_directory)) { |
|
| 473 | + if ($this->dirExists($destination_directory)) { |
|
| 474 | + $this->destination_directory = $destination_directory; |
|
| 475 | + chdir($destination_directory); |
|
| 476 | + } else if ($create_if_not_exist === true) { |
|
| 477 | + if (mkdir($destination_directory, 0775, true)) { |
|
| 478 | + $this->destination_directory = $destination_directory; |
|
| 479 | + chdir($destination_directory); |
|
| 480 | + } |
|
| 481 | + else{ |
|
| 482 | + $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + } |
|
| 486 | + return $this; |
|
| 487 | + } |
|
| 488 | + /** |
|
| 489 | + * Check file exists |
|
| 490 | + * |
|
| 491 | + * @since 1.0 |
|
| 492 | + * @version 1.0.1 |
|
| 493 | + * @param string $file_destination |
|
| 494 | + * @return boolean |
|
| 495 | + * @method boolean fileExists |
|
| 496 | + */ |
|
| 497 | + public function fileExists($file_destination) |
|
| 498 | + { |
|
| 499 | + if ($this->isFilename($file_destination)) { |
|
| 500 | + return (file_exists($file_destination) && is_file($file_destination)); |
|
| 501 | + } |
|
| 502 | + return false; |
|
| 503 | + } |
|
| 504 | + /** |
|
| 505 | + * Check dir exists |
|
| 506 | + * |
|
| 507 | + * @since 1.0 |
|
| 508 | + * @version 1.0.1 |
|
| 509 | + * @param string $path |
|
| 510 | + * @return boolean |
|
| 511 | + * @method boolean dirExists |
|
| 512 | + */ |
|
| 513 | + public function dirExists($path) |
|
| 514 | + { |
|
| 515 | + if ($this->isDirpath($path)) { |
|
| 516 | + return (file_exists($path) && is_dir($path)); |
|
| 517 | + } |
|
| 518 | + return false; |
|
| 519 | + } |
|
| 520 | + /** |
|
| 521 | + * Check valid filename |
|
| 522 | + * |
|
| 523 | + * @since 1.0 |
|
| 524 | + * @version 1.0.1 |
|
| 525 | + * @param string $filename |
|
| 526 | + * @return boolean |
|
| 527 | + * @method boolean isFilename |
|
| 528 | + */ |
|
| 529 | + public function isFilename($filename) |
|
| 530 | + { |
|
| 531 | + $filename = basename($filename); |
|
| 532 | + return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 533 | + } |
|
| 534 | + /** |
|
| 535 | + * Validate mime type with allowed mime types, |
|
| 536 | + * but if allowed mime types is empty, this method return true |
|
| 537 | + * |
|
| 538 | + * @since 1.0 |
|
| 539 | + * @version 1.0 |
|
| 540 | + * @param string $mime |
|
| 541 | + * @return boolean |
|
| 542 | + * @method boolean checkMimeType |
|
| 543 | + */ |
|
| 544 | + public function checkMimeType($mime) |
|
| 545 | + { |
|
| 546 | + if (count($this->allowed_mime_types) == 0) { |
|
| 547 | + return true; |
|
| 548 | + } |
|
| 549 | + return in_array(strtolower($mime), $this->allowed_mime_types); |
|
| 550 | + } |
|
| 551 | + /** |
|
| 552 | + * Retrive status of upload |
|
| 553 | + * |
|
| 554 | + * @since 1.0 |
|
| 555 | + * @version 1.0 |
|
| 556 | + * @return boolean |
|
| 557 | + * @method boolean getStatus |
|
| 558 | + */ |
|
| 559 | + public function getStatus() |
|
| 560 | + { |
|
| 561 | + return $this->file['status']; |
|
| 562 | + } |
|
| 563 | + /** |
|
| 564 | + * Check valid path |
|
| 565 | + * |
|
| 566 | + * @since 1.0 |
|
| 567 | + * @version 1.0.1 |
|
| 568 | + * @param string $filename |
|
| 569 | + * @return boolean |
|
| 570 | + * @method boolean isDirpath |
|
| 571 | + */ |
|
| 572 | + public function isDirpath($path) |
|
| 573 | + { |
|
| 574 | + if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 575 | + if (DIRECTORY_SEPARATOR == '/') { |
|
| 576 | + return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 577 | + } else { |
|
| 578 | + return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 579 | + } |
|
| 580 | + } |
|
| 581 | + return false; |
|
| 582 | + } |
|
| 583 | + /** |
|
| 584 | + * Allow overwriting files |
|
| 585 | + * |
|
| 586 | + * @since 1.0 |
|
| 587 | + * @version 1.0 |
|
| 588 | + * @return object |
|
| 589 | + * @method boolean allowOverwriting |
|
| 590 | + */ |
|
| 591 | + public function allowOverwriting() |
|
| 592 | + { |
|
| 593 | + $this->overwrite_file = true; |
|
| 594 | + return $this; |
|
| 595 | + } |
|
| 596 | + /** |
|
| 597 | + * File info |
|
| 598 | + * |
|
| 599 | + * @since 1.0 |
|
| 600 | + * @version 1.0 |
|
| 601 | + * @return object |
|
| 602 | + * @method object getInfo |
|
| 603 | + */ |
|
| 604 | + public function getInfo() |
|
| 605 | + { |
|
| 606 | + return (object)$this->file; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Check if the file is uploaded |
|
| 612 | + * @return boolean |
|
| 613 | + */ |
|
| 614 | + public function isUploaded(){ |
|
| 615 | + return isset($this->file_array[$this->input]) |
|
| 616 | + && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
| 617 | + } |
|
| 618 | 618 | |
| 619 | 619 | |
| 620 | - /** |
|
| 621 | - * Upload file |
|
| 622 | - * |
|
| 623 | - * @since 1.0 |
|
| 624 | - * @version 1.0.1 |
|
| 625 | - * @return boolean |
|
| 626 | - * @method boolean save |
|
| 627 | - */ |
|
| 628 | - public function save(){ |
|
| 629 | - if (count($this->file_array) > 0 && array_key_exists($this->input, $this->file_array)) { |
|
| 630 | - // set original filename if not have a new name |
|
| 631 | - if (empty($this->filename)) { |
|
| 632 | - $this->filename = $this->file_array[$this->input]['name']; |
|
| 633 | - } |
|
| 634 | - else{ |
|
| 635 | - // Replace %s for extension in filename |
|
| 636 | - // Before: /[\w\d]*(.[\d\w]+)$/i |
|
| 637 | - // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
| 638 | - // Support unicode(utf-8) characters |
|
| 639 | - // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
| 640 | - $extension = preg_replace( |
|
| 641 | - '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
| 642 | - '$1', |
|
| 643 | - $this->file_array[$this->input]['name'] |
|
| 644 | - ); |
|
| 645 | - $this->filename = $this->filename . '.' . $extension; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - // set file info |
|
| 649 | - $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
| 650 | - $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
| 651 | - $this->file['original'] = $this->file_array[$this->input]['name']; |
|
| 652 | - $this->file['size'] = $this->file_array[$this->input]['size']; |
|
| 653 | - $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
| 654 | - $this->file['destination'] = $this->destination_directory . $this->filename; |
|
| 655 | - $this->file['filename'] = $this->filename; |
|
| 656 | - $this->file['error'] = $this->file_array[$this->input]['error']; |
|
| 657 | - |
|
| 658 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 659 | - |
|
| 660 | - $error = $this->uploadHasError(); |
|
| 661 | - if($error){ |
|
| 662 | - return false; |
|
| 663 | - } |
|
| 664 | - // Execute input callback |
|
| 665 | - $this->runCallback('input'); |
|
| 666 | - |
|
| 667 | - $this->file['status'] = call_user_func_array( |
|
| 668 | - $this->upload_function, array( |
|
| 669 | - $this->file_array[$this->input]['tmp_name'], |
|
| 670 | - $this->destination_directory . $this->filename |
|
| 671 | - ) |
|
| 672 | - ); |
|
| 673 | - |
|
| 674 | - // Execute output callback |
|
| 675 | - $this->runCallback('output'); |
|
| 676 | - |
|
| 677 | - return $this->file['status']; |
|
| 678 | - } |
|
| 679 | - return false; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * File size for humans. |
|
| 685 | - * |
|
| 686 | - * @since 1.0 |
|
| 687 | - * @version 1.0 |
|
| 688 | - * @param integer $bytes |
|
| 689 | - * @param integer $precision |
|
| 690 | - * @return string |
|
| 691 | - * @method string sizeFormat |
|
| 692 | - */ |
|
| 693 | - public function sizeFormat($size, $precision = 2) |
|
| 694 | - { |
|
| 695 | - if($size > 0){ |
|
| 696 | - $base = log($size) / log(1024); |
|
| 697 | - $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
| 698 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 699 | - } |
|
| 700 | - return null; |
|
| 701 | - } |
|
| 620 | + /** |
|
| 621 | + * Upload file |
|
| 622 | + * |
|
| 623 | + * @since 1.0 |
|
| 624 | + * @version 1.0.1 |
|
| 625 | + * @return boolean |
|
| 626 | + * @method boolean save |
|
| 627 | + */ |
|
| 628 | + public function save(){ |
|
| 629 | + if (count($this->file_array) > 0 && array_key_exists($this->input, $this->file_array)) { |
|
| 630 | + // set original filename if not have a new name |
|
| 631 | + if (empty($this->filename)) { |
|
| 632 | + $this->filename = $this->file_array[$this->input]['name']; |
|
| 633 | + } |
|
| 634 | + else{ |
|
| 635 | + // Replace %s for extension in filename |
|
| 636 | + // Before: /[\w\d]*(.[\d\w]+)$/i |
|
| 637 | + // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
| 638 | + // Support unicode(utf-8) characters |
|
| 639 | + // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
| 640 | + $extension = preg_replace( |
|
| 641 | + '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
| 642 | + '$1', |
|
| 643 | + $this->file_array[$this->input]['name'] |
|
| 644 | + ); |
|
| 645 | + $this->filename = $this->filename . '.' . $extension; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + // set file info |
|
| 649 | + $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
| 650 | + $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
| 651 | + $this->file['original'] = $this->file_array[$this->input]['name']; |
|
| 652 | + $this->file['size'] = $this->file_array[$this->input]['size']; |
|
| 653 | + $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
| 654 | + $this->file['destination'] = $this->destination_directory . $this->filename; |
|
| 655 | + $this->file['filename'] = $this->filename; |
|
| 656 | + $this->file['error'] = $this->file_array[$this->input]['error']; |
|
| 657 | + |
|
| 658 | + $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 659 | + |
|
| 660 | + $error = $this->uploadHasError(); |
|
| 661 | + if($error){ |
|
| 662 | + return false; |
|
| 663 | + } |
|
| 664 | + // Execute input callback |
|
| 665 | + $this->runCallback('input'); |
|
| 666 | + |
|
| 667 | + $this->file['status'] = call_user_func_array( |
|
| 668 | + $this->upload_function, array( |
|
| 669 | + $this->file_array[$this->input]['tmp_name'], |
|
| 670 | + $this->destination_directory . $this->filename |
|
| 671 | + ) |
|
| 672 | + ); |
|
| 673 | + |
|
| 674 | + // Execute output callback |
|
| 675 | + $this->runCallback('output'); |
|
| 676 | + |
|
| 677 | + return $this->file['status']; |
|
| 678 | + } |
|
| 679 | + return false; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * File size for humans. |
|
| 685 | + * |
|
| 686 | + * @since 1.0 |
|
| 687 | + * @version 1.0 |
|
| 688 | + * @param integer $bytes |
|
| 689 | + * @param integer $precision |
|
| 690 | + * @return string |
|
| 691 | + * @method string sizeFormat |
|
| 692 | + */ |
|
| 693 | + public function sizeFormat($size, $precision = 2) |
|
| 694 | + { |
|
| 695 | + if($size > 0){ |
|
| 696 | + $base = log($size) / log(1024); |
|
| 697 | + $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
| 698 | + return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 699 | + } |
|
| 700 | + return null; |
|
| 701 | + } |
|
| 702 | 702 | |
| 703 | 703 | |
| 704 | - /** |
|
| 705 | - * Convert human file size to bytes |
|
| 706 | - * |
|
| 707 | - * @since 1.0 |
|
| 708 | - * @version 1.0.1 |
|
| 709 | - * @param integer|double $size |
|
| 710 | - * @return integer|double |
|
| 711 | - * @method string sizeInBytes |
|
| 712 | - */ |
|
| 713 | - public function sizeInBytes($size) |
|
| 714 | - { |
|
| 715 | - $unit = 'B'; |
|
| 716 | - $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
| 717 | - $matches = array(); |
|
| 718 | - preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
| 719 | - if (array_key_exists('unit', $matches)) { |
|
| 720 | - $unit = strtoupper($matches['unit']); |
|
| 721 | - } |
|
| 722 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - /** |
|
| 726 | - * Get the upload error message |
|
| 727 | - * @return string |
|
| 728 | - */ |
|
| 729 | - public function getError(){ |
|
| 730 | - return $this->error; |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - /** |
|
| 734 | - * Set the upload error message |
|
| 735 | - * @param string $message the upload error message to set |
|
| 736 | - */ |
|
| 737 | - public function setError($message){ |
|
| 738 | - $this->logger->info('The file upload got error : ' . $message); |
|
| 739 | - $this->error = $message; |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - /** |
|
| 743 | - * Run the callbacks in the file uploaded |
|
| 744 | - * @param string $type the type of callback "input" or "output" |
|
| 745 | - * @return void |
|
| 746 | - */ |
|
| 747 | - protected function runCallback($type){ |
|
| 748 | - if (!empty( $this->callbacks[$type])) { |
|
| 749 | - call_user_func($this->callbacks[$type], (object)$this->file); |
|
| 750 | - } |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * Check if file upload has error |
|
| 755 | - * @return boolean |
|
| 756 | - */ |
|
| 757 | - protected function uploadHasError(){ |
|
| 758 | - //check if file upload is allowed in the configuration |
|
| 759 | - if(! ini_get('file_uploads')){ |
|
| 760 | - $this->setError($this->error_messages['file_uploads']); |
|
| 761 | - return true; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - //check for php upload error |
|
| 765 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 766 | - $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
| 767 | - return true; |
|
| 768 | - } |
|
| 704 | + /** |
|
| 705 | + * Convert human file size to bytes |
|
| 706 | + * |
|
| 707 | + * @since 1.0 |
|
| 708 | + * @version 1.0.1 |
|
| 709 | + * @param integer|double $size |
|
| 710 | + * @return integer|double |
|
| 711 | + * @method string sizeInBytes |
|
| 712 | + */ |
|
| 713 | + public function sizeInBytes($size) |
|
| 714 | + { |
|
| 715 | + $unit = 'B'; |
|
| 716 | + $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
| 717 | + $matches = array(); |
|
| 718 | + preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
| 719 | + if (array_key_exists('unit', $matches)) { |
|
| 720 | + $unit = strtoupper($matches['unit']); |
|
| 721 | + } |
|
| 722 | + return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + /** |
|
| 726 | + * Get the upload error message |
|
| 727 | + * @return string |
|
| 728 | + */ |
|
| 729 | + public function getError(){ |
|
| 730 | + return $this->error; |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + /** |
|
| 734 | + * Set the upload error message |
|
| 735 | + * @param string $message the upload error message to set |
|
| 736 | + */ |
|
| 737 | + public function setError($message){ |
|
| 738 | + $this->logger->info('The file upload got error : ' . $message); |
|
| 739 | + $this->error = $message; |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + /** |
|
| 743 | + * Run the callbacks in the file uploaded |
|
| 744 | + * @param string $type the type of callback "input" or "output" |
|
| 745 | + * @return void |
|
| 746 | + */ |
|
| 747 | + protected function runCallback($type){ |
|
| 748 | + if (!empty( $this->callbacks[$type])) { |
|
| 749 | + call_user_func($this->callbacks[$type], (object)$this->file); |
|
| 750 | + } |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * Check if file upload has error |
|
| 755 | + * @return boolean |
|
| 756 | + */ |
|
| 757 | + protected function uploadHasError(){ |
|
| 758 | + //check if file upload is allowed in the configuration |
|
| 759 | + if(! ini_get('file_uploads')){ |
|
| 760 | + $this->setError($this->error_messages['file_uploads']); |
|
| 761 | + return true; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + //check for php upload error |
|
| 765 | + if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 766 | + $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
| 767 | + return true; |
|
| 768 | + } |
|
| 769 | 769 | |
| 770 | - //check for mime type |
|
| 771 | - if (! $this->checkMimeType($this->file['mime'])) { |
|
| 772 | - $this->setError($this->error_messages['accept_file_types']); |
|
| 773 | - return true; |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - // Check file size |
|
| 777 | - if ($this->max_file_size > 0 && $this->max_file_size < $this->file['size']) { |
|
| 778 | - $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
| 779 | - return true; |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - // Check if exists file |
|
| 783 | - if ($this->fileExists($this->destination_directory . $this->filename) && $this->overwrite_file === false) { |
|
| 784 | - $this->setError($this->error_messages['overwritten_not_allowed']); |
|
| 785 | - return true; |
|
| 786 | - } |
|
| 787 | - return false; |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * Get the PHP upload error message for the given code |
|
| 792 | - * @param int $code the error code |
|
| 793 | - * @return string the error message |
|
| 794 | - */ |
|
| 795 | - private function getPhpUploadErrorMessageByCode($code){ |
|
| 796 | - $codeMessageMaps = array( |
|
| 797 | - 1 => $this->error_messages['upload_err_ini_size'], |
|
| 798 | - 2 => $this->error_messages['upload_err_form_size'], |
|
| 799 | - 3 => $this->error_messages['upload_err_partial'], |
|
| 800 | - 4 => $this->error_messages['upload_err_no_file'], |
|
| 801 | - 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
| 802 | - 7 => $this->error_messages['upload_err_cant_write'], |
|
| 803 | - 8 => $this->error_messages['upload_err_extension'], |
|
| 804 | - ); |
|
| 805 | - return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
| 806 | - } |
|
| 807 | - } |
|
| 770 | + //check for mime type |
|
| 771 | + if (! $this->checkMimeType($this->file['mime'])) { |
|
| 772 | + $this->setError($this->error_messages['accept_file_types']); |
|
| 773 | + return true; |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + // Check file size |
|
| 777 | + if ($this->max_file_size > 0 && $this->max_file_size < $this->file['size']) { |
|
| 778 | + $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
| 779 | + return true; |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + // Check if exists file |
|
| 783 | + if ($this->fileExists($this->destination_directory . $this->filename) && $this->overwrite_file === false) { |
|
| 784 | + $this->setError($this->error_messages['overwritten_not_allowed']); |
|
| 785 | + return true; |
|
| 786 | + } |
|
| 787 | + return false; |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * Get the PHP upload error message for the given code |
|
| 792 | + * @param int $code the error code |
|
| 793 | + * @return string the error message |
|
| 794 | + */ |
|
| 795 | + private function getPhpUploadErrorMessageByCode($code){ |
|
| 796 | + $codeMessageMaps = array( |
|
| 797 | + 1 => $this->error_messages['upload_err_ini_size'], |
|
| 798 | + 2 => $this->error_messages['upload_err_form_size'], |
|
| 799 | + 3 => $this->error_messages['upload_err_partial'], |
|
| 800 | + 4 => $this->error_messages['upload_err_no_file'], |
|
| 801 | + 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
| 802 | + 7 => $this->error_messages['upload_err_cant_write'], |
|
| 803 | + 8 => $this->error_messages['upload_err_extension'], |
|
| 804 | + ); |
|
| 805 | + return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
| 806 | + } |
|
| 807 | + } |
|
@@ -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 |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | if (isset($_FILES) && is_array($_FILES)) { |
| 258 | 258 | $this->file_array = $_FILES; |
| 259 | 259 | } |
| 260 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 260 | + $this->logger->info('The upload file information are : ' . stringfy_vars($this->file_array)); |
|
| 261 | 261 | } |
| 262 | 262 | /** |
| 263 | 263 | * Set input. |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | */ |
| 274 | 274 | public function setInput($input) |
| 275 | 275 | { |
| 276 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 276 | + if (!empty($input) && (is_string($input) || is_numeric($input))) { |
|
| 277 | 277 | $this->input = $input; |
| 278 | 278 | } |
| 279 | 279 | return $this; |
@@ -309,7 +309,7 @@ discard block |
||
| 309 | 309 | */ |
| 310 | 310 | public function setAutoFilename() |
| 311 | 311 | { |
| 312 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 312 | + $this->filename = sha1(mt_rand(1, 9999) . uniqid()); |
|
| 313 | 313 | $this->filename .= time(); |
| 314 | 314 | return $this; |
| 315 | 315 | } |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
| 331 | 331 | // Calculate difference |
| 332 | 332 | if ($php_size < $file_size) { |
| 333 | - $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. ']'); |
|
| 333 | + $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 . ']'); |
|
| 334 | 334 | } |
| 335 | 335 | $this->max_file_size = $file_size; |
| 336 | 336 | } |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | public function setAllowedMimeTypes(array $mimes) |
| 349 | 349 | { |
| 350 | 350 | if (count($mimes) > 0) { |
| 351 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 351 | + array_map(array($this, 'setAllowMimeType'), $mimes); |
|
| 352 | 352 | } |
| 353 | 353 | return $this; |
| 354 | 354 | } |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | { |
| 414 | 414 | if (!empty($name) && is_string($name)) { |
| 415 | 415 | if (array_key_exists($name, $this->mime_helping)) { |
| 416 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 416 | + return $this->setAllowedMimeTypes($this->mime_helping[$name]); |
|
| 417 | 417 | } |
| 418 | 418 | } |
| 419 | 419 | return $this; |
@@ -432,8 +432,8 @@ discard block |
||
| 432 | 432 | */ |
| 433 | 433 | public function setUploadFunction($function) |
| 434 | 434 | { |
| 435 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 436 | - if (is_callable( $function)) { |
|
| 435 | + if (!empty($function) && (is_array($function) || is_string($function))) { |
|
| 436 | + if (is_callable($function)) { |
|
| 437 | 437 | $this->upload_function = $function; |
| 438 | 438 | } |
| 439 | 439 | } |
@@ -478,8 +478,8 @@ discard block |
||
| 478 | 478 | $this->destination_directory = $destination_directory; |
| 479 | 479 | chdir($destination_directory); |
| 480 | 480 | } |
| 481 | - else{ |
|
| 482 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 481 | + else { |
|
| 482 | + $this->logger->warning('Can not create the upload directory [' . $destination_directory . ']'); |
|
| 483 | 483 | } |
| 484 | 484 | } |
| 485 | 485 | } |
@@ -529,7 +529,7 @@ discard block |
||
| 529 | 529 | public function isFilename($filename) |
| 530 | 530 | { |
| 531 | 531 | $filename = basename($filename); |
| 532 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 532 | + return (!empty($filename) && (is_string($filename) || is_numeric($filename))); |
|
| 533 | 533 | } |
| 534 | 534 | /** |
| 535 | 535 | * Validate mime type with allowed mime types, |
@@ -571,11 +571,11 @@ discard block |
||
| 571 | 571 | */ |
| 572 | 572 | public function isDirpath($path) |
| 573 | 573 | { |
| 574 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 574 | + if (!empty($path) && (is_string($path) || is_numeric($path))) { |
|
| 575 | 575 | if (DIRECTORY_SEPARATOR == '/') { |
| 576 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 576 | + return (preg_match('/^[^*?"<>|:]*$/', $path) == 1); |
|
| 577 | 577 | } else { |
| 578 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 578 | + return (preg_match("/^[^*?\"<>|:]*$/", substr($path, 2)) == 1); |
|
| 579 | 579 | } |
| 580 | 580 | } |
| 581 | 581 | return false; |
@@ -603,7 +603,7 @@ discard block |
||
| 603 | 603 | */ |
| 604 | 604 | public function getInfo() |
| 605 | 605 | { |
| 606 | - return (object)$this->file; |
|
| 606 | + return (object) $this->file; |
|
| 607 | 607 | } |
| 608 | 608 | |
| 609 | 609 | |
@@ -611,7 +611,7 @@ discard block |
||
| 611 | 611 | * Check if the file is uploaded |
| 612 | 612 | * @return boolean |
| 613 | 613 | */ |
| 614 | - public function isUploaded(){ |
|
| 614 | + public function isUploaded() { |
|
| 615 | 615 | return isset($this->file_array[$this->input]) |
| 616 | 616 | && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
| 617 | 617 | } |
@@ -625,13 +625,13 @@ discard block |
||
| 625 | 625 | * @return boolean |
| 626 | 626 | * @method boolean save |
| 627 | 627 | */ |
| 628 | - public function save(){ |
|
| 628 | + public function save() { |
|
| 629 | 629 | if (count($this->file_array) > 0 && array_key_exists($this->input, $this->file_array)) { |
| 630 | 630 | // set original filename if not have a new name |
| 631 | 631 | if (empty($this->filename)) { |
| 632 | 632 | $this->filename = $this->file_array[$this->input]['name']; |
| 633 | 633 | } |
| 634 | - else{ |
|
| 634 | + else { |
|
| 635 | 635 | // Replace %s for extension in filename |
| 636 | 636 | // Before: /[\w\d]*(.[\d\w]+)$/i |
| 637 | 637 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
@@ -655,10 +655,10 @@ discard block |
||
| 655 | 655 | $this->file['filename'] = $this->filename; |
| 656 | 656 | $this->file['error'] = $this->file_array[$this->input]['error']; |
| 657 | 657 | |
| 658 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 658 | + $this->logger->info('The upload file information to process is : ' . stringfy_vars($this->file)); |
|
| 659 | 659 | |
| 660 | 660 | $error = $this->uploadHasError(); |
| 661 | - if($error){ |
|
| 661 | + if ($error) { |
|
| 662 | 662 | return false; |
| 663 | 663 | } |
| 664 | 664 | // Execute input callback |
@@ -692,10 +692,10 @@ discard block |
||
| 692 | 692 | */ |
| 693 | 693 | public function sizeFormat($size, $precision = 2) |
| 694 | 694 | { |
| 695 | - if($size > 0){ |
|
| 695 | + if ($size > 0) { |
|
| 696 | 696 | $base = log($size) / log(1024); |
| 697 | 697 | $suffixes = array('B', 'K', 'M', 'G', 'T'); |
| 698 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 698 | + return round(pow(1024, $base - floor($base)), $precision) . (isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 699 | 699 | } |
| 700 | 700 | return null; |
| 701 | 701 | } |
@@ -719,14 +719,14 @@ discard block |
||
| 719 | 719 | if (array_key_exists('unit', $matches)) { |
| 720 | 720 | $unit = strtoupper($matches['unit']); |
| 721 | 721 | } |
| 722 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 722 | + return (floatval($matches['size']) * pow(1024, $units[$unit])); |
|
| 723 | 723 | } |
| 724 | 724 | |
| 725 | 725 | /** |
| 726 | 726 | * Get the upload error message |
| 727 | 727 | * @return string |
| 728 | 728 | */ |
| 729 | - public function getError(){ |
|
| 729 | + public function getError() { |
|
| 730 | 730 | return $this->error; |
| 731 | 731 | } |
| 732 | 732 | |
@@ -734,7 +734,7 @@ discard block |
||
| 734 | 734 | * Set the upload error message |
| 735 | 735 | * @param string $message the upload error message to set |
| 736 | 736 | */ |
| 737 | - public function setError($message){ |
|
| 737 | + public function setError($message) { |
|
| 738 | 738 | $this->logger->info('The file upload got error : ' . $message); |
| 739 | 739 | $this->error = $message; |
| 740 | 740 | } |
@@ -744,9 +744,9 @@ discard block |
||
| 744 | 744 | * @param string $type the type of callback "input" or "output" |
| 745 | 745 | * @return void |
| 746 | 746 | */ |
| 747 | - protected function runCallback($type){ |
|
| 748 | - if (!empty( $this->callbacks[$type])) { |
|
| 749 | - call_user_func($this->callbacks[$type], (object)$this->file); |
|
| 747 | + protected function runCallback($type) { |
|
| 748 | + if (!empty($this->callbacks[$type])) { |
|
| 749 | + call_user_func($this->callbacks[$type], (object) $this->file); |
|
| 750 | 750 | } |
| 751 | 751 | } |
| 752 | 752 | |
@@ -754,21 +754,21 @@ discard block |
||
| 754 | 754 | * Check if file upload has error |
| 755 | 755 | * @return boolean |
| 756 | 756 | */ |
| 757 | - protected function uploadHasError(){ |
|
| 757 | + protected function uploadHasError() { |
|
| 758 | 758 | //check if file upload is allowed in the configuration |
| 759 | - if(! ini_get('file_uploads')){ |
|
| 759 | + if (!ini_get('file_uploads')) { |
|
| 760 | 760 | $this->setError($this->error_messages['file_uploads']); |
| 761 | 761 | return true; |
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | //check for php upload error |
| 765 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 765 | + if (is_numeric($this->file['error']) && $this->file['error'] > 0) { |
|
| 766 | 766 | $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
| 767 | 767 | return true; |
| 768 | 768 | } |
| 769 | 769 | |
| 770 | 770 | //check for mime type |
| 771 | - if (! $this->checkMimeType($this->file['mime'])) { |
|
| 771 | + if (!$this->checkMimeType($this->file['mime'])) { |
|
| 772 | 772 | $this->setError($this->error_messages['accept_file_types']); |
| 773 | 773 | return true; |
| 774 | 774 | } |
@@ -792,7 +792,7 @@ discard block |
||
| 792 | 792 | * @param int $code the error code |
| 793 | 793 | * @return string the error message |
| 794 | 794 | */ |
| 795 | - private function getPhpUploadErrorMessageByCode($code){ |
|
| 795 | + private function getPhpUploadErrorMessageByCode($code) { |
|
| 796 | 796 | $codeMessageMaps = array( |
| 797 | 797 | 1 => $this->error_messages['upload_err_ini_size'], |
| 798 | 798 | 2 => $this->error_messages['upload_err_form_size'], |
@@ -477,8 +477,7 @@ discard block |
||
| 477 | 477 | if (mkdir($destination_directory, 0775, true)) { |
| 478 | 478 | $this->destination_directory = $destination_directory; |
| 479 | 479 | chdir($destination_directory); |
| 480 | - } |
|
| 481 | - else{ |
|
| 480 | + } else{ |
|
| 482 | 481 | $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
| 483 | 482 | } |
| 484 | 483 | } |
@@ -630,8 +629,7 @@ discard block |
||
| 630 | 629 | // set original filename if not have a new name |
| 631 | 630 | if (empty($this->filename)) { |
| 632 | 631 | $this->filename = $this->file_array[$this->input]['name']; |
| 633 | - } |
|
| 634 | - else{ |
|
| 632 | + } else{ |
|
| 635 | 633 | // Replace %s for extension in filename |
| 636 | 634 | // Before: /[\w\d]*(.[\d\w]+)$/i |
| 637 | 635 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |