@@ -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,27 +53,27 @@ 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($module != '.' && $module != '..' && 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 ($module != '.' && $module != '..' && 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 | - else{ |
|
| 76 | + else { |
|
| 77 | 77 | $logger->info('The application contains no module skipping'); |
| 78 | 78 | } |
| 79 | 79 | } |
@@ -82,9 +82,9 @@ discard block |
||
| 82 | 82 | * Get the list of the custom autoload configuration from module if exists |
| 83 | 83 | * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values |
| 84 | 84 | */ |
| 85 | - public static function getModulesAutoloadConfig(){ |
|
| 85 | + public static function getModulesAutoloadConfig() { |
|
| 86 | 86 | $logger = self::getLogger(); |
| 87 | - if(! self::hasModule()){ |
|
| 87 | + if (!self::hasModule()) { |
|
| 88 | 88 | $logger->info('No module was loaded skipping.'); |
| 89 | 89 | return false; |
| 90 | 90 | } |
@@ -97,27 +97,27 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | foreach (self::$list as $module) { |
| 99 | 99 | $file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php'; |
| 100 | - if(file_exists($file)){ |
|
| 100 | + if (file_exists($file)) { |
|
| 101 | 101 | require_once $file; |
| 102 | - if(! empty($autoload) && is_array($autoload)){ |
|
| 102 | + if (!empty($autoload) && is_array($autoload)) { |
|
| 103 | 103 | //libraries autoload |
| 104 | - if(! empty($autoload['libraries']) && is_array($autoload['libraries'])){ |
|
| 104 | + if (!empty($autoload['libraries']) && is_array($autoload['libraries'])) { |
|
| 105 | 105 | $autoloads['libraries'] = array_merge($autoloads['libraries'], $autoload['libraries']); |
| 106 | 106 | } |
| 107 | 107 | //config autoload |
| 108 | - if(! empty($autoload['config']) && is_array($autoload['config'])){ |
|
| 108 | + if (!empty($autoload['config']) && is_array($autoload['config'])) { |
|
| 109 | 109 | $autoloads['config'] = array_merge($autoloads['config'], $autoload['config']); |
| 110 | 110 | } |
| 111 | 111 | //models autoload |
| 112 | - if(! empty($autoload['models']) && is_array($autoload['models'])){ |
|
| 112 | + if (!empty($autoload['models']) && is_array($autoload['models'])) { |
|
| 113 | 113 | $autoloads['models'] = array_merge($autoloads['models'], $autoload['models']); |
| 114 | 114 | } |
| 115 | 115 | //functions autoload |
| 116 | - if(! empty($autoload['functions']) && is_array($autoload['functions'])){ |
|
| 116 | + if (!empty($autoload['functions']) && is_array($autoload['functions'])) { |
|
| 117 | 117 | $autoloads['functions'] = array_merge($autoloads['functions'], $autoload['functions']); |
| 118 | 118 | } |
| 119 | 119 | //languages autoload |
| 120 | - if(! empty($autoload['languages']) && is_array($autoload['languages'])){ |
|
| 120 | + if (!empty($autoload['languages']) && is_array($autoload['languages'])) { |
|
| 121 | 121 | $autoloads['languages'] = array_merge($autoloads['languages'], $autoload['languages']); |
| 122 | 122 | } |
| 123 | 123 | unset($autoload); |
@@ -131,23 +131,23 @@ discard block |
||
| 131 | 131 | * Get the list of the custom routes configuration from module if exists |
| 132 | 132 | * @return array|boolean the routes list or false if no module contains the routes configuration |
| 133 | 133 | */ |
| 134 | - public static function getModulesRoutes(){ |
|
| 134 | + public static function getModulesRoutes() { |
|
| 135 | 135 | $logger = self::getLogger(); |
| 136 | - if(! self::hasModule()){ |
|
| 136 | + if (!self::hasModule()) { |
|
| 137 | 137 | $logger->info('No module was loaded skipping.'); |
| 138 | 138 | return false; |
| 139 | 139 | } |
| 140 | 140 | $routes = array(); |
| 141 | 141 | foreach (self::$list as $module) { |
| 142 | 142 | $file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php'; |
| 143 | - if(file_exists($file)){ |
|
| 143 | + if (file_exists($file)) { |
|
| 144 | 144 | require_once $file; |
| 145 | - if(! empty($route) && is_array($route)){ |
|
| 145 | + if (!empty($route) && is_array($route)) { |
|
| 146 | 146 | $routes = array_merge($routes, $route); |
| 147 | 147 | unset($route); |
| 148 | 148 | } |
| 149 | - else{ |
|
| 150 | - show_error('No routing configuration found in [' .$file. '] for module [' . $module . ']'); |
|
| 149 | + else { |
|
| 150 | + show_error('No routing configuration found in [' . $file . '] for module [' . $module . ']'); |
|
| 151 | 151 | } |
| 152 | 152 | } |
| 153 | 153 | } |
@@ -161,23 +161,23 @@ discard block |
||
| 161 | 161 | * @param string $module the module name |
| 162 | 162 | * @return boolean|string false or null if no module have this controller, path the full path of the controller |
| 163 | 163 | */ |
| 164 | - public static function findControllerFullPath($class, $module = null){ |
|
| 164 | + public static function findControllerFullPath($class, $module = null) { |
|
| 165 | 165 | $logger = self::getLogger(); |
| 166 | - if(! self::hasModule()){ |
|
| 166 | + if (!self::hasModule()) { |
|
| 167 | 167 | $logger->info('No module was loaded skiping.'); |
| 168 | 168 | return false; |
| 169 | 169 | } |
| 170 | 170 | $class = str_ireplace('.php', '', $class); |
| 171 | 171 | $class = ucfirst($class); |
| 172 | - $classFile = $class.'.php'; |
|
| 173 | - $logger->debug('Checking the controller [' . $class . '] in module [' .$module. '] ...'); |
|
| 172 | + $classFile = $class . '.php'; |
|
| 173 | + $logger->debug('Checking the controller [' . $class . '] in module [' . $module . '] ...'); |
|
| 174 | 174 | $filePath = MODULE_PATH . $module . DS . 'controllers' . DS . $classFile; |
| 175 | - if(file_exists($filePath)){ |
|
| 176 | - $logger->info('Found controller [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 175 | + if (file_exists($filePath)) { |
|
| 176 | + $logger->info('Found controller [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 177 | 177 | return $filePath; |
| 178 | 178 | } |
| 179 | - else{ |
|
| 180 | - $logger->info('Controller [' . $class . '] does not exist in the module [' .$module. ']'); |
|
| 179 | + else { |
|
| 180 | + $logger->info('Controller [' . $class . '] does not exist in the module [' . $module . ']'); |
|
| 181 | 181 | return false; |
| 182 | 182 | } |
| 183 | 183 | } |
@@ -188,23 +188,23 @@ discard block |
||
| 188 | 188 | * @param string $module the module name |
| 189 | 189 | * @return boolean|string false or null if no module have this model, return the full path of this model |
| 190 | 190 | */ |
| 191 | - public static function findModelFullPath($class, $module = null){ |
|
| 191 | + public static function findModelFullPath($class, $module = null) { |
|
| 192 | 192 | $logger = self::getLogger(); |
| 193 | - if(! self::hasModule()){ |
|
| 193 | + if (!self::hasModule()) { |
|
| 194 | 194 | $logger->info('No module was loaded skiping.'); |
| 195 | 195 | return false; |
| 196 | 196 | } |
| 197 | 197 | $class = str_ireplace('.php', '', $class); |
| 198 | 198 | $class = ucfirst($class); |
| 199 | - $classFile = $class.'.php'; |
|
| 200 | - $logger->debug('Checking model [' . $class . '] in module [' .$module. '] ...'); |
|
| 199 | + $classFile = $class . '.php'; |
|
| 200 | + $logger->debug('Checking model [' . $class . '] in module [' . $module . '] ...'); |
|
| 201 | 201 | $filePath = MODULE_PATH . $module . DS . 'models' . DS . $classFile; |
| 202 | - if(file_exists($filePath)){ |
|
| 203 | - $logger->info('Found model [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 202 | + if (file_exists($filePath)) { |
|
| 203 | + $logger->info('Found model [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 204 | 204 | return $filePath; |
| 205 | 205 | } |
| 206 | - else{ |
|
| 207 | - $logger->info('Model [' . $class . '] does not exist in the module [' .$module. ']'); |
|
| 206 | + else { |
|
| 207 | + $logger->info('Model [' . $class . '] does not exist in the module [' . $module . ']'); |
|
| 208 | 208 | return false; |
| 209 | 209 | } |
| 210 | 210 | } |
@@ -215,22 +215,22 @@ discard block |
||
| 215 | 215 | * @param string $module the module name |
| 216 | 216 | * @return boolean|string false or null if no module have this configuration, return the full path of this configuration |
| 217 | 217 | */ |
| 218 | - public static function findConfigFullPath($configuration, $module = null){ |
|
| 218 | + public static function findConfigFullPath($configuration, $module = null) { |
|
| 219 | 219 | $logger = self::getLogger(); |
| 220 | - if(! self::hasModule()){ |
|
| 220 | + if (!self::hasModule()) { |
|
| 221 | 221 | $logger->info('No module was loaded skiping.'); |
| 222 | 222 | return false; |
| 223 | 223 | } |
| 224 | 224 | $configuration = str_ireplace('.php', '', $configuration); |
| 225 | - $file = $configuration.'.php'; |
|
| 226 | - $logger->debug('Checking configuration [' . $configuration . '] in module [' .$module. '] ...'); |
|
| 225 | + $file = $configuration . '.php'; |
|
| 226 | + $logger->debug('Checking configuration [' . $configuration . '] in module [' . $module . '] ...'); |
|
| 227 | 227 | $filePath = MODULE_PATH . $module . DS . 'config' . DS . $file; |
| 228 | - if(file_exists($filePath)){ |
|
| 229 | - $logger->info('Found configuration [' . $configuration . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 228 | + if (file_exists($filePath)) { |
|
| 229 | + $logger->info('Found configuration [' . $configuration . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 230 | 230 | return $filePath; |
| 231 | 231 | } |
| 232 | - else{ |
|
| 233 | - $logger->info('Configuration [' . $configuration . '] does not exist in the module [' .$module. ']'); |
|
| 232 | + else { |
|
| 233 | + $logger->info('Configuration [' . $configuration . '] does not exist in the module [' . $module . ']'); |
|
| 234 | 234 | return false; |
| 235 | 235 | } |
| 236 | 236 | } |
@@ -241,23 +241,23 @@ discard block |
||
| 241 | 241 | * @param string $module the module name |
| 242 | 242 | * @return boolean|string false or null if no module have this helper, return the full path of this helper |
| 243 | 243 | */ |
| 244 | - public static function findFunctionFullPath($helper, $module = null){ |
|
| 244 | + public static function findFunctionFullPath($helper, $module = null) { |
|
| 245 | 245 | $logger = self::getLogger(); |
| 246 | - if(! self::hasModule()){ |
|
| 246 | + if (!self::hasModule()) { |
|
| 247 | 247 | $logger->info('No module was loaded skiping.'); |
| 248 | 248 | return false; |
| 249 | 249 | } |
| 250 | 250 | $helper = str_ireplace('.php', '', $helper); |
| 251 | 251 | $helper = str_ireplace('function_', '', $helper); |
| 252 | - $file = 'function_'.$helper.'.php'; |
|
| 253 | - $logger->debug('Checking helper [' . $helper . '] in module [' .$module. '] ...'); |
|
| 252 | + $file = 'function_' . $helper . '.php'; |
|
| 253 | + $logger->debug('Checking helper [' . $helper . '] in module [' . $module . '] ...'); |
|
| 254 | 254 | $filePath = MODULE_PATH . $module . DS . 'functions' . DS . $file; |
| 255 | - if(file_exists($filePath)){ |
|
| 256 | - $logger->info('Found helper [' . $helper . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 255 | + if (file_exists($filePath)) { |
|
| 256 | + $logger->info('Found helper [' . $helper . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 257 | 257 | return $filePath; |
| 258 | 258 | } |
| 259 | - else{ |
|
| 260 | - $logger->info('Helper [' . $helper . '] does not exist in the module [' .$module. ']'); |
|
| 259 | + else { |
|
| 260 | + $logger->info('Helper [' . $helper . '] does not exist in the module [' . $module . ']'); |
|
| 261 | 261 | return false; |
| 262 | 262 | } |
| 263 | 263 | } |
@@ -269,22 +269,22 @@ discard block |
||
| 269 | 269 | * @param string $module the module name |
| 270 | 270 | * @return boolean|string false or null if no module have this library, return the full path of this library |
| 271 | 271 | */ |
| 272 | - public static function findLibraryFullPath($class, $module = null){ |
|
| 272 | + public static function findLibraryFullPath($class, $module = null) { |
|
| 273 | 273 | $logger = self::getLogger(); |
| 274 | - if(! self::hasModule()){ |
|
| 274 | + if (!self::hasModule()) { |
|
| 275 | 275 | $logger->info('No module was loaded skiping.'); |
| 276 | 276 | return false; |
| 277 | 277 | } |
| 278 | 278 | $class = str_ireplace('.php', '', $class); |
| 279 | - $file = $class.'.php'; |
|
| 280 | - $logger->debug('Checking library [' . $class . '] in module [' .$module. '] ...'); |
|
| 279 | + $file = $class . '.php'; |
|
| 280 | + $logger->debug('Checking library [' . $class . '] in module [' . $module . '] ...'); |
|
| 281 | 281 | $filePath = MODULE_PATH . $module . DS . 'libraries' . DS . $file; |
| 282 | - if(file_exists($filePath)){ |
|
| 283 | - $logger->info('Found library [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 282 | + if (file_exists($filePath)) { |
|
| 283 | + $logger->info('Found library [' . $class . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 284 | 284 | return $filePath; |
| 285 | 285 | } |
| 286 | - else{ |
|
| 287 | - $logger->info('Library [' . $class . '] does not exist in the module [' .$module. ']'); |
|
| 286 | + else { |
|
| 287 | + $logger->info('Library [' . $class . '] does not exist in the module [' . $module . ']'); |
|
| 288 | 288 | return false; |
| 289 | 289 | } |
| 290 | 290 | } |
@@ -296,9 +296,9 @@ discard block |
||
| 296 | 296 | * @param string $module the module name to check |
| 297 | 297 | * @return boolean|string false or null if no module have this view, path the full path of the view |
| 298 | 298 | */ |
| 299 | - public static function findViewFullPath($view, $module = null){ |
|
| 299 | + public static function findViewFullPath($view, $module = null) { |
|
| 300 | 300 | $logger = self::getLogger(); |
| 301 | - if(! self::hasModule()){ |
|
| 301 | + if (!self::hasModule()) { |
|
| 302 | 302 | $logger->info('No module was loaded skiping.'); |
| 303 | 303 | return false; |
| 304 | 304 | } |
@@ -306,14 +306,14 @@ discard block |
||
| 306 | 306 | $view = trim($view, '/\\'); |
| 307 | 307 | $view = str_ireplace('/', DS, $view); |
| 308 | 308 | $viewFile = $view . '.php'; |
| 309 | - $logger->debug('Checking view [' . $view . '] in module [' .$module. '] ...'); |
|
| 309 | + $logger->debug('Checking view [' . $view . '] in module [' . $module . '] ...'); |
|
| 310 | 310 | $filePath = MODULE_PATH . $module . DS . 'views' . DS . $viewFile; |
| 311 | - if(file_exists($filePath)){ |
|
| 312 | - $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 311 | + if (file_exists($filePath)) { |
|
| 312 | + $logger->info('Found view [' . $view . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 313 | 313 | return $filePath; |
| 314 | 314 | } |
| 315 | - else{ |
|
| 316 | - $logger->info('View [' . $view . '] does not exist in the module [' .$module. ']'); |
|
| 315 | + else { |
|
| 316 | + $logger->info('View [' . $view . '] does not exist in the module [' . $module . ']'); |
|
| 317 | 317 | return false; |
| 318 | 318 | } |
| 319 | 319 | } |
@@ -325,23 +325,23 @@ discard block |
||
| 325 | 325 | * @param string $appLang the application language like 'en', 'fr' |
| 326 | 326 | * @return boolean|string false or null if no module have this language, return the full path of this language |
| 327 | 327 | */ |
| 328 | - public static function findLanguageFullPath($language, $module = null, $appLang){ |
|
| 328 | + public static function findLanguageFullPath($language, $module = null, $appLang) { |
|
| 329 | 329 | $logger = self::getLogger(); |
| 330 | - if(! self::hasModule()){ |
|
| 330 | + if (!self::hasModule()) { |
|
| 331 | 331 | $logger->info('No module was loaded skiping.'); |
| 332 | 332 | return false; |
| 333 | 333 | } |
| 334 | 334 | $language = str_ireplace('.php', '', $language); |
| 335 | 335 | $language = str_ireplace('lang_', '', $language); |
| 336 | - $file = 'lang_'.$language.'.php'; |
|
| 337 | - $logger->debug('Checking language [' . $language . '] in module [' .$module. '] ...'); |
|
| 336 | + $file = 'lang_' . $language . '.php'; |
|
| 337 | + $logger->debug('Checking language [' . $language . '] in module [' . $module . '] ...'); |
|
| 338 | 338 | $filePath = MODULE_PATH . $module . DS . 'lang' . DS . $appLang . DS . $file; |
| 339 | - if(file_exists($filePath)){ |
|
| 340 | - $logger->info('Found language [' . $language . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
|
| 339 | + if (file_exists($filePath)) { |
|
| 340 | + $logger->info('Found language [' . $language . '] in module [' . $module . '], the file path is [' . $filePath . ']'); |
|
| 341 | 341 | return $filePath; |
| 342 | 342 | } |
| 343 | - else{ |
|
| 344 | - $logger->info('Language [' . $language . '] does not exist in the module [' .$module. ']'); |
|
| 343 | + else { |
|
| 344 | + $logger->info('Language [' . $language . '] does not exist in the module [' . $module . ']'); |
|
| 345 | 345 | return false; |
| 346 | 346 | } |
| 347 | 347 | } |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | * Get the list of module loaded |
| 351 | 351 | * @return array the module list |
| 352 | 352 | */ |
| 353 | - public static function getModuleList(){ |
|
| 353 | + public static function getModuleList() { |
|
| 354 | 354 | return self::$list; |
| 355 | 355 | } |
| 356 | 356 | |
@@ -358,7 +358,7 @@ discard block |
||
| 358 | 358 | * Check if the application has an module |
| 359 | 359 | * @return boolean |
| 360 | 360 | */ |
| 361 | - public static function hasModule(){ |
|
| 361 | + public static function hasModule() { |
|
| 362 | 362 | return !empty(self::$list); |
| 363 | 363 | } |
| 364 | 364 | |
@@ -61,8 +61,7 @@ discard block |
||
| 61 | 61 | while(($module = readdir($moduleDir)) !== false){ |
| 62 | 62 | if($module != '.' && $module != '..' && 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 | } |
@@ -72,8 +71,7 @@ discard block |
||
| 72 | 71 | |
| 73 | 72 | if(self::hasModule()){ |
| 74 | 73 | $logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']'); |
| 75 | - } |
|
| 76 | - else{ |
|
| 74 | + } else{ |
|
| 77 | 75 | $logger->info('The application contains no module skipping'); |
| 78 | 76 | } |
| 79 | 77 | } |
@@ -145,8 +143,7 @@ discard block |
||
| 145 | 143 | if(! empty($route) && is_array($route)){ |
| 146 | 144 | $routes = array_merge($routes, $route); |
| 147 | 145 | unset($route); |
| 148 | - } |
|
| 149 | - else{ |
|
| 146 | + } else{ |
|
| 150 | 147 | show_error('No routing configuration found in [' .$file. '] for module [' . $module . ']'); |
| 151 | 148 | } |
| 152 | 149 | } |
@@ -175,8 +172,7 @@ discard block |
||
| 175 | 172 | if(file_exists($filePath)){ |
| 176 | 173 | $logger->info('Found controller [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 177 | 174 | return $filePath; |
| 178 | - } |
|
| 179 | - else{ |
|
| 175 | + } else{ |
|
| 180 | 176 | $logger->info('Controller [' . $class . '] does not exist in the module [' .$module. ']'); |
| 181 | 177 | return false; |
| 182 | 178 | } |
@@ -202,8 +198,7 @@ discard block |
||
| 202 | 198 | if(file_exists($filePath)){ |
| 203 | 199 | $logger->info('Found model [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 204 | 200 | return $filePath; |
| 205 | - } |
|
| 206 | - else{ |
|
| 201 | + } else{ |
|
| 207 | 202 | $logger->info('Model [' . $class . '] does not exist in the module [' .$module. ']'); |
| 208 | 203 | return false; |
| 209 | 204 | } |
@@ -228,8 +223,7 @@ discard block |
||
| 228 | 223 | if(file_exists($filePath)){ |
| 229 | 224 | $logger->info('Found configuration [' . $configuration . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 230 | 225 | return $filePath; |
| 231 | - } |
|
| 232 | - else{ |
|
| 226 | + } else{ |
|
| 233 | 227 | $logger->info('Configuration [' . $configuration . '] does not exist in the module [' .$module. ']'); |
| 234 | 228 | return false; |
| 235 | 229 | } |
@@ -255,8 +249,7 @@ discard block |
||
| 255 | 249 | if(file_exists($filePath)){ |
| 256 | 250 | $logger->info('Found helper [' . $helper . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 257 | 251 | return $filePath; |
| 258 | - } |
|
| 259 | - else{ |
|
| 252 | + } else{ |
|
| 260 | 253 | $logger->info('Helper [' . $helper . '] does not exist in the module [' .$module. ']'); |
| 261 | 254 | return false; |
| 262 | 255 | } |
@@ -282,8 +275,7 @@ discard block |
||
| 282 | 275 | if(file_exists($filePath)){ |
| 283 | 276 | $logger->info('Found library [' . $class . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 284 | 277 | return $filePath; |
| 285 | - } |
|
| 286 | - else{ |
|
| 278 | + } else{ |
|
| 287 | 279 | $logger->info('Library [' . $class . '] does not exist in the module [' .$module. ']'); |
| 288 | 280 | return false; |
| 289 | 281 | } |
@@ -311,8 +303,7 @@ discard block |
||
| 311 | 303 | if(file_exists($filePath)){ |
| 312 | 304 | $logger->info('Found view [' . $view . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 313 | 305 | return $filePath; |
| 314 | - } |
|
| 315 | - else{ |
|
| 306 | + } else{ |
|
| 316 | 307 | $logger->info('View [' . $view . '] does not exist in the module [' .$module. ']'); |
| 317 | 308 | return false; |
| 318 | 309 | } |
@@ -339,8 +330,7 @@ discard block |
||
| 339 | 330 | if(file_exists($filePath)){ |
| 340 | 331 | $logger->info('Found language [' . $language . '] in module [' .$module. '], the file path is [' .$filePath. ']'); |
| 341 | 332 | return $filePath; |
| 342 | - } |
|
| 343 | - else{ |
|
| 333 | + } else{ |
|
| 344 | 334 | $logger->info('Language [' . $language . '] does not exist in the module [' .$module. ']'); |
| 345 | 335 | return false; |
| 346 | 336 | } |
@@ -1,803 +1,803 @@ |
||
| 1 | 1 | <?php |
| 2 | - defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | - /** |
|
| 4 | - * TNH Framework |
|
| 5 | - * |
|
| 6 | - * A simple PHP framework using HMVC architecture |
|
| 7 | - * |
|
| 8 | - * This content is released under the GNU GPL License (GPL) |
|
| 9 | - * |
|
| 10 | - * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | - * |
|
| 12 | - * This program is free software; you can redistribute it and/or |
|
| 13 | - * modify it under the terms of the GNU General Public License |
|
| 14 | - * as published by the Free Software Foundation; either version 3 |
|
| 15 | - * of the License, or (at your option) any later version. |
|
| 16 | - * |
|
| 17 | - * This program is distributed in the hope that it will be useful, |
|
| 18 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | - * GNU General Public License for more details. |
|
| 21 | - * |
|
| 22 | - * You should have received a copy of the GNU General Public License |
|
| 23 | - * along with this program; if not, write to the Free Software |
|
| 24 | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | - */ |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Upload |
|
| 31 | - * |
|
| 32 | - * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
| 33 | - * |
|
| 34 | - * @author Olaf Erlandsen <[email protected]> |
|
| 35 | - * @author http://www.webdevfreelance.com/ |
|
| 36 | - * |
|
| 37 | - * @package FileUpload |
|
| 38 | - * @version 1.5 |
|
| 39 | - */ |
|
| 40 | - class Upload{ |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Version |
|
| 44 | - * |
|
| 45 | - * @since 1.5 |
|
| 46 | - * @version 1.0 |
|
| 47 | - */ |
|
| 48 | - const VERSION = '1.5'; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Upload function name |
|
| 52 | - * Remember: |
|
| 53 | - * Default function: move_uploaded_file |
|
| 54 | - * Native options: |
|
| 55 | - * - move_uploaded_file (Default and best option) |
|
| 56 | - * - copy |
|
| 57 | - * |
|
| 58 | - * @since 1.0 |
|
| 59 | - * @version 1.0 |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - private $upload_function = 'move_uploaded_file'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Array with the information obtained from the |
|
| 66 | - * variable $_FILES or $HTTP_POST_FILES. |
|
| 67 | - * |
|
| 68 | - * @since 1.0 |
|
| 69 | - * @version 1.0 |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - private $file_array = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * If the file you are trying to upload already exists it will |
|
| 76 | - * be overwritten if you set the variable to true. |
|
| 77 | - * |
|
| 78 | - * @since 1.0 |
|
| 79 | - * @version 1.0 |
|
| 80 | - * @var boolean |
|
| 81 | - */ |
|
| 82 | - private $overwrite_file = false; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Input element |
|
| 86 | - * Example: |
|
| 87 | - * <input type="file" name="file" /> |
|
| 88 | - * Result: |
|
| 89 | - * FileUpload::$input = file |
|
| 90 | - * |
|
| 91 | - * @since 1.0 |
|
| 92 | - * @version 1.0 |
|
| 93 | - * @var string |
|
| 94 | - */ |
|
| 95 | - private $input; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Path output |
|
| 99 | - * |
|
| 100 | - * @since 1.0 |
|
| 101 | - * @version 1.0 |
|
| 102 | - * @var string |
|
| 103 | - */ |
|
| 104 | - private $destination_directory; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Output filename |
|
| 108 | - * |
|
| 109 | - * @since 1.0 |
|
| 110 | - * @version 1.0 |
|
| 111 | - * @var string |
|
| 112 | - */ |
|
| 113 | - private $filename; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Max file size |
|
| 117 | - * |
|
| 118 | - * @since 1.0 |
|
| 119 | - * @version 1.0 |
|
| 120 | - * @var float |
|
| 121 | - */ |
|
| 122 | - private $max_file_size= 0.0; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * List of allowed mime types |
|
| 126 | - * |
|
| 127 | - * @since 1.0 |
|
| 128 | - * @version 1.0 |
|
| 129 | - * @var array |
|
| 130 | - */ |
|
| 131 | - private $allowed_mime_types = array(); |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Callbacks |
|
| 135 | - * |
|
| 136 | - * @since 1.0 |
|
| 137 | - * @version 1.0 |
|
| 138 | - * @var array |
|
| 139 | - */ |
|
| 140 | - private $callbacks = array('before' => null, 'after' => null); |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * File object |
|
| 144 | - * |
|
| 145 | - * @since 1.0 |
|
| 146 | - * @version 1.0 |
|
| 147 | - * @var object |
|
| 148 | - */ |
|
| 149 | - private $file; |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Helping mime types |
|
| 153 | - * |
|
| 154 | - * @since 1.0 |
|
| 155 | - * @version 1.0 |
|
| 156 | - * @var array |
|
| 157 | - */ |
|
| 158 | - private $mime_helping = array( |
|
| 159 | - 'text' => array('text/plain',), |
|
| 160 | - 'image' => array( |
|
| 161 | - 'image/jpeg', |
|
| 162 | - 'image/jpg', |
|
| 163 | - 'image/pjpeg', |
|
| 164 | - 'image/png', |
|
| 165 | - 'image/gif', |
|
| 166 | - ), |
|
| 167 | - 'document' => array( |
|
| 168 | - 'application/pdf', |
|
| 169 | - 'application/msword', |
|
| 170 | - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
| 171 | - 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
| 172 | - 'application/vnd.ms-powerpoint', |
|
| 173 | - 'application/vnd.ms-excel', |
|
| 174 | - 'application/vnd.oasis.opendocument.spreadsheet', |
|
| 175 | - 'application/vnd.oasis.opendocument.presentation', |
|
| 176 | - ), |
|
| 177 | - 'video' => array( |
|
| 178 | - 'video/3gpp', |
|
| 179 | - 'video/3gpp', |
|
| 180 | - 'video/x-msvideo', |
|
| 181 | - 'video/avi', |
|
| 182 | - 'video/mpeg4', |
|
| 183 | - 'video/mp4', |
|
| 184 | - 'video/mpeg', |
|
| 185 | - 'video/mpg', |
|
| 186 | - 'video/quicktime', |
|
| 187 | - 'video/x-sgi-movie', |
|
| 188 | - 'video/x-ms-wmv', |
|
| 189 | - 'video/x-flv', |
|
| 190 | - ), |
|
| 191 | - ); |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * The upload error message |
|
| 195 | - * @var array |
|
| 196 | - */ |
|
| 197 | - public $error_messages = array(); |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * The upload error message |
|
| 201 | - * @var string |
|
| 202 | - */ |
|
| 203 | - protected $error = null; |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * The logger instance |
|
| 207 | - * @var Log |
|
| 208 | - */ |
|
| 209 | - private $logger; |
|
| 210 | - |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Construct |
|
| 214 | - * |
|
| 215 | - * @since 0.1 |
|
| 216 | - * @version 1.0.1 |
|
| 217 | - * @return object |
|
| 218 | - * @method object __construct |
|
| 219 | - */ |
|
| 220 | - public function __construct(){ |
|
| 221 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 222 | - $this->logger->setLogger('Library::Upload'); |
|
| 223 | - |
|
| 224 | - Loader::lang('file_upload'); |
|
| 225 | - $obj =& get_instance(); |
|
| 226 | - |
|
| 227 | - $this->error_messages = array( |
|
| 228 | - 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
| 229 | - 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
| 230 | - 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
| 231 | - 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
| 232 | - 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
| 233 | - 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
| 234 | - 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
| 235 | - 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
| 236 | - 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
| 237 | - 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
| 238 | - 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
| 239 | - ); |
|
| 240 | - |
|
| 241 | - $this->file = array( |
|
| 242 | - 'status' => false, // True: success upload |
|
| 243 | - 'mime' => '', // Empty string |
|
| 244 | - 'filename' => '', // Empty string |
|
| 245 | - 'original' => '', // Empty string |
|
| 246 | - 'size' => 0, // 0 Bytes |
|
| 247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
| 248 | - 'destination' => './', // Default: ./ |
|
| 249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
| 250 | - 'error' => null, // File error |
|
| 251 | - ); |
|
| 252 | - |
|
| 253 | - // Change dir to current dir |
|
| 254 | - $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
| 255 | - |
|
| 256 | - // Set file array |
|
| 257 | - if (isset($_FILES) && is_array($_FILES)) { |
|
| 258 | - $this->file_array = $_FILES; |
|
| 259 | - } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
|
| 260 | - $this->file_array = $HTTP_POST_FILES; |
|
| 261 | - } |
|
| 262 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 263 | - } |
|
| 264 | - /** |
|
| 265 | - * Set input. |
|
| 266 | - * If you have $_FILES["file"], you must use the key "file" |
|
| 267 | - * Example: |
|
| 268 | - * $object->setInput("file"); |
|
| 269 | - * |
|
| 270 | - * @since 1.0 |
|
| 271 | - * @version 1.0 |
|
| 272 | - * @param string $input |
|
| 273 | - * @return object |
|
| 274 | - * @method boolean setInput |
|
| 275 | - */ |
|
| 276 | - public function setInput($input) |
|
| 277 | - { |
|
| 278 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 279 | - $this->input = $input; |
|
| 280 | - } |
|
| 281 | - return $this; |
|
| 282 | - } |
|
| 283 | - /** |
|
| 284 | - * Set new filename |
|
| 285 | - * Example: |
|
| 286 | - * FileUpload::setFilename("new file.txt") |
|
| 287 | - * Remember: |
|
| 288 | - * Use %s to retrive file extension |
|
| 289 | - * |
|
| 290 | - * @since 1.0 |
|
| 291 | - * @version 1.0 |
|
| 292 | - * @param string $filename |
|
| 293 | - * @return object |
|
| 294 | - * @method boolean setFilename |
|
| 295 | - */ |
|
| 296 | - public function setFilename($filename) |
|
| 297 | - { |
|
| 298 | - if ($this->isFilename($filename)) { |
|
| 299 | - $this->filename = $filename; |
|
| 300 | - } |
|
| 301 | - return $this; |
|
| 302 | - } |
|
| 303 | - /** |
|
| 304 | - * Set automatic filename |
|
| 305 | - * |
|
| 306 | - * @since 1.0 |
|
| 307 | - * @version 1.5 |
|
| 308 | - * @param string $extension |
|
| 309 | - * @return object |
|
| 310 | - * @method boolean setAutoFilename |
|
| 311 | - */ |
|
| 312 | - public function setAutoFilename() |
|
| 313 | - { |
|
| 314 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 315 | - $this->filename .= time(); |
|
| 316 | - return $this; |
|
| 317 | - } |
|
| 318 | - /** |
|
| 319 | - * Set file size limit |
|
| 320 | - * |
|
| 321 | - * @since 1.0 |
|
| 322 | - * @version 1.0 |
|
| 323 | - * @param double $file_size |
|
| 324 | - * @return object |
|
| 325 | - * @method boolean setMaxFileSize |
|
| 326 | - */ |
|
| 327 | - public function setMaxFileSize($file_size) |
|
| 328 | - { |
|
| 329 | - $file_size = $this->sizeInBytes($file_size); |
|
| 330 | - if (is_numeric($file_size) && $file_size > -1) { |
|
| 331 | - // Get php config |
|
| 332 | - $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
|
| 333 | - // Calculate difference |
|
| 334 | - if ($php_size < $file_size) { |
|
| 335 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
| 336 | - } |
|
| 337 | - $this->max_file_size = $file_size; |
|
| 338 | - } |
|
| 339 | - return $this; |
|
| 340 | - } |
|
| 341 | - /** |
|
| 342 | - * Set array mime types |
|
| 343 | - * |
|
| 344 | - * @since 1.0 |
|
| 345 | - * @version 1.0 |
|
| 346 | - * @param array $mimes |
|
| 347 | - * @return object |
|
| 348 | - * @method boolean setAllowedMimeTypes |
|
| 349 | - */ |
|
| 350 | - public function setAllowedMimeTypes(array $mimes) |
|
| 351 | - { |
|
| 352 | - if (count($mimes) > 0) { |
|
| 353 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 354 | - } |
|
| 355 | - return $this; |
|
| 356 | - } |
|
| 357 | - /** |
|
| 358 | - * Set input callback |
|
| 359 | - * |
|
| 360 | - * @since 1.0 |
|
| 361 | - * @version 1.0 |
|
| 362 | - * @param mixed $callback |
|
| 363 | - * @return object |
|
| 364 | - * @method boolean setCallbackInput |
|
| 365 | - */ |
|
| 366 | - public function setCallbackInput($callback) |
|
| 367 | - { |
|
| 368 | - if (is_callable($callback, false)) { |
|
| 369 | - $this->callbacks['input'] = $callback; |
|
| 370 | - } |
|
| 371 | - return $this; |
|
| 372 | - } |
|
| 373 | - /** |
|
| 374 | - * Set output callback |
|
| 375 | - * |
|
| 376 | - * @since 1.0 |
|
| 377 | - * @version 1.0 |
|
| 378 | - * @param mixed $callback |
|
| 379 | - * @return object |
|
| 380 | - * @method boolean setCallbackOutput |
|
| 381 | - */ |
|
| 382 | - public function setCallbackOutput($callback) |
|
| 383 | - { |
|
| 384 | - if (is_callable($callback, false)) { |
|
| 385 | - $this->callbacks['output'] = $callback; |
|
| 386 | - } |
|
| 387 | - return $this; |
|
| 388 | - } |
|
| 389 | - /** |
|
| 390 | - * Append a mime type to allowed mime types |
|
| 391 | - * |
|
| 392 | - * @since 1.0 |
|
| 393 | - * @version 1.0.1 |
|
| 394 | - * @param string $mime |
|
| 395 | - * @return object |
|
| 396 | - * @method boolean setAllowMimeType |
|
| 397 | - */ |
|
| 398 | - public function setAllowMimeType($mime) |
|
| 399 | - { |
|
| 400 | - if (!empty($mime) && is_string($mime)) { |
|
| 401 | - $this->allowed_mime_types[] = strtolower($mime); |
|
| 402 | - $this->file['allowed_mime_types'][] = strtolower($mime); |
|
| 403 | - } |
|
| 404 | - return $this; |
|
| 405 | - } |
|
| 406 | - /** |
|
| 407 | - * Set allowed mime types from mime helping |
|
| 408 | - * |
|
| 409 | - * @since 1.0.1 |
|
| 410 | - * @version 1.0.1 |
|
| 411 | - * @return object |
|
| 412 | - * @method boolean setMimeHelping |
|
| 413 | - */ |
|
| 414 | - public function setMimeHelping($name) |
|
| 415 | - { |
|
| 416 | - if (!empty($name) && is_string($name)) { |
|
| 417 | - if (array_key_exists($name, $this->mime_helping)) { |
|
| 418 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - return $this; |
|
| 422 | - } |
|
| 423 | - /** |
|
| 424 | - * Set function to upload file |
|
| 425 | - * Examples: |
|
| 426 | - * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
| 427 | - * 2.- FileUpload::setUploadFunction("copy"); |
|
| 428 | - * |
|
| 429 | - * @since 1.0 |
|
| 430 | - * @version 1.0 |
|
| 431 | - * @param string $function |
|
| 432 | - * @return object |
|
| 433 | - * @method boolean setUploadFunction |
|
| 434 | - */ |
|
| 435 | - public function setUploadFunction($function) |
|
| 436 | - { |
|
| 437 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 438 | - if (is_callable( $function)) { |
|
| 439 | - $this->upload_function = $function; |
|
| 440 | - } |
|
| 441 | - } |
|
| 442 | - return $this; |
|
| 443 | - } |
|
| 444 | - /** |
|
| 445 | - * Clear allowed mime types cache |
|
| 446 | - * |
|
| 447 | - * @since 1.0 |
|
| 448 | - * @version 1.0 |
|
| 449 | - * @return object |
|
| 450 | - * @method boolean clearAllowedMimeTypes |
|
| 451 | - */ |
|
| 452 | - public function clearAllowedMimeTypes() |
|
| 453 | - { |
|
| 454 | - $this->allowed_mime_types = array(); |
|
| 455 | - $this->file['allowed_mime_types'] = array(); |
|
| 456 | - return $this; |
|
| 457 | - } |
|
| 458 | - /** |
|
| 459 | - * Set destination output |
|
| 460 | - * |
|
| 461 | - * @since 1.0 |
|
| 462 | - * @version 1.0 |
|
| 463 | - * @param string $destination_directory Destination path |
|
| 464 | - * @param boolean $create_if_not_exist |
|
| 465 | - * @return object |
|
| 466 | - * @method boolean setDestinationDirectory |
|
| 467 | - */ |
|
| 468 | - public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
| 469 | - $destination_directory = realpath($destination_directory); |
|
| 470 | - if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
| 471 | - $destination_directory .= DIRECTORY_SEPARATOR; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - if ($this->isDirpath($destination_directory)) { |
|
| 475 | - if ($this->dirExists($destination_directory)) { |
|
| 476 | - $this->destination_directory = $destination_directory; |
|
| 477 | - chdir($destination_directory); |
|
| 478 | - } else if ($create_if_not_exist === true) { |
|
| 479 | - if (mkdir($destination_directory, 0775, true)) { |
|
| 480 | - $this->destination_directory = $destination_directory; |
|
| 481 | - chdir($destination_directory); |
|
| 482 | - } |
|
| 483 | - else{ |
|
| 484 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 485 | - } |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - return $this; |
|
| 489 | - } |
|
| 490 | - /** |
|
| 491 | - * Check file exists |
|
| 492 | - * |
|
| 493 | - * @since 1.0 |
|
| 494 | - * @version 1.0.1 |
|
| 495 | - * @param string $file_destination |
|
| 496 | - * @return boolean |
|
| 497 | - * @method boolean fileExists |
|
| 498 | - */ |
|
| 499 | - public function fileExists($file_destination) |
|
| 500 | - { |
|
| 501 | - if ($this->isFilename($file_destination)) { |
|
| 502 | - return (file_exists($file_destination) && is_file($file_destination)); |
|
| 503 | - } |
|
| 504 | - return false; |
|
| 505 | - } |
|
| 506 | - /** |
|
| 507 | - * Check dir exists |
|
| 508 | - * |
|
| 509 | - * @since 1.0 |
|
| 510 | - * @version 1.0.1 |
|
| 511 | - * @param string $path |
|
| 512 | - * @return boolean |
|
| 513 | - * @method boolean dirExists |
|
| 514 | - */ |
|
| 515 | - public function dirExists($path) |
|
| 516 | - { |
|
| 517 | - if ($this->isDirpath($path)) { |
|
| 518 | - return (file_exists($path) && is_dir($path)); |
|
| 519 | - } |
|
| 520 | - return false; |
|
| 521 | - } |
|
| 522 | - /** |
|
| 523 | - * Check valid filename |
|
| 524 | - * |
|
| 525 | - * @since 1.0 |
|
| 526 | - * @version 1.0.1 |
|
| 527 | - * @param string $filename |
|
| 528 | - * @return boolean |
|
| 529 | - * @method boolean isFilename |
|
| 530 | - */ |
|
| 531 | - public function isFilename($filename) |
|
| 532 | - { |
|
| 533 | - $filename = basename($filename); |
|
| 534 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 535 | - } |
|
| 536 | - /** |
|
| 537 | - * Validate mime type with allowed mime types, |
|
| 538 | - * but if allowed mime types is empty, this method return true |
|
| 539 | - * |
|
| 540 | - * @since 1.0 |
|
| 541 | - * @version 1.0 |
|
| 542 | - * @param string $mime |
|
| 543 | - * @return boolean |
|
| 544 | - * @method boolean checkMimeType |
|
| 545 | - */ |
|
| 546 | - public function checkMimeType($mime) |
|
| 547 | - { |
|
| 548 | - if (count($this->allowed_mime_types) == 0) { |
|
| 549 | - return true; |
|
| 550 | - } |
|
| 551 | - return in_array(strtolower($mime), $this->allowed_mime_types); |
|
| 552 | - } |
|
| 553 | - /** |
|
| 554 | - * Retrive status of upload |
|
| 555 | - * |
|
| 556 | - * @since 1.0 |
|
| 557 | - * @version 1.0 |
|
| 558 | - * @return boolean |
|
| 559 | - * @method boolean getStatus |
|
| 560 | - */ |
|
| 561 | - public function getStatus() |
|
| 562 | - { |
|
| 563 | - return $this->file['status']; |
|
| 564 | - } |
|
| 565 | - /** |
|
| 566 | - * Check valid path |
|
| 567 | - * |
|
| 568 | - * @since 1.0 |
|
| 569 | - * @version 1.0.1 |
|
| 570 | - * @param string $filename |
|
| 571 | - * @return boolean |
|
| 572 | - * @method boolean isDirpath |
|
| 573 | - */ |
|
| 574 | - public function isDirpath($path) |
|
| 575 | - { |
|
| 576 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 577 | - if (DIRECTORY_SEPARATOR == '/') { |
|
| 578 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 579 | - } else { |
|
| 580 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 581 | - } |
|
| 582 | - } |
|
| 583 | - return false; |
|
| 584 | - } |
|
| 585 | - /** |
|
| 586 | - * Allow overwriting files |
|
| 587 | - * |
|
| 588 | - * @since 1.0 |
|
| 589 | - * @version 1.0 |
|
| 590 | - * @return object |
|
| 591 | - * @method boolean allowOverwriting |
|
| 592 | - */ |
|
| 593 | - public function allowOverwriting() |
|
| 594 | - { |
|
| 595 | - $this->overwrite_file = true; |
|
| 596 | - return $this; |
|
| 597 | - } |
|
| 598 | - /** |
|
| 599 | - * File info |
|
| 600 | - * |
|
| 601 | - * @since 1.0 |
|
| 602 | - * @version 1.0 |
|
| 603 | - * @return object |
|
| 604 | - * @method object getInfo |
|
| 605 | - */ |
|
| 606 | - public function getInfo() |
|
| 607 | - { |
|
| 608 | - return (object)$this->file; |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - |
|
| 612 | - /** |
|
| 613 | - * Check if the file is uploaded |
|
| 614 | - * @return boolean |
|
| 615 | - */ |
|
| 616 | - public function isUploaded(){ |
|
| 617 | - return isset($this->file_array[$this->input]) |
|
| 618 | - && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - /** |
|
| 622 | - * Check if file upload has error |
|
| 623 | - * @return boolean |
|
| 624 | - */ |
|
| 625 | - protected function uploadHasError(){ |
|
| 626 | - //check if file upload is allowed in the configuration |
|
| 627 | - if(! ini_get('file_uploads')){ |
|
| 628 | - $this->setError($this->error_messages['file_uploads']); |
|
| 629 | - return true; |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - //check for php upload error |
|
| 633 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 634 | - $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
| 635 | - return true; |
|
| 636 | - } |
|
| 2 | + defined('ROOT_PATH') or exit('Access denied'); |
|
| 3 | + /** |
|
| 4 | + * TNH Framework |
|
| 5 | + * |
|
| 6 | + * A simple PHP framework using HMVC architecture |
|
| 7 | + * |
|
| 8 | + * This content is released under the GNU GPL License (GPL) |
|
| 9 | + * |
|
| 10 | + * Copyright (C) 2017 Tony NGUEREZA |
|
| 11 | + * |
|
| 12 | + * This program is free software; you can redistribute it and/or |
|
| 13 | + * modify it under the terms of the GNU General Public License |
|
| 14 | + * as published by the Free Software Foundation; either version 3 |
|
| 15 | + * of the License, or (at your option) any later version. |
|
| 16 | + * |
|
| 17 | + * This program is distributed in the hope that it will be useful, |
|
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 | + * GNU General Public License for more details. |
|
| 21 | + * |
|
| 22 | + * You should have received a copy of the GNU General Public License |
|
| 23 | + * along with this program; if not, write to the Free Software |
|
| 24 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
| 25 | + */ |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Upload |
|
| 31 | + * |
|
| 32 | + * A complete class to upload files with php 5 or higher, but the best: very simple to use. |
|
| 33 | + * |
|
| 34 | + * @author Olaf Erlandsen <[email protected]> |
|
| 35 | + * @author http://www.webdevfreelance.com/ |
|
| 36 | + * |
|
| 37 | + * @package FileUpload |
|
| 38 | + * @version 1.5 |
|
| 39 | + */ |
|
| 40 | + class Upload{ |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Version |
|
| 44 | + * |
|
| 45 | + * @since 1.5 |
|
| 46 | + * @version 1.0 |
|
| 47 | + */ |
|
| 48 | + const VERSION = '1.5'; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Upload function name |
|
| 52 | + * Remember: |
|
| 53 | + * Default function: move_uploaded_file |
|
| 54 | + * Native options: |
|
| 55 | + * - move_uploaded_file (Default and best option) |
|
| 56 | + * - copy |
|
| 57 | + * |
|
| 58 | + * @since 1.0 |
|
| 59 | + * @version 1.0 |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + private $upload_function = 'move_uploaded_file'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Array with the information obtained from the |
|
| 66 | + * variable $_FILES or $HTTP_POST_FILES. |
|
| 67 | + * |
|
| 68 | + * @since 1.0 |
|
| 69 | + * @version 1.0 |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + private $file_array = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * If the file you are trying to upload already exists it will |
|
| 76 | + * be overwritten if you set the variable to true. |
|
| 77 | + * |
|
| 78 | + * @since 1.0 |
|
| 79 | + * @version 1.0 |
|
| 80 | + * @var boolean |
|
| 81 | + */ |
|
| 82 | + private $overwrite_file = false; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Input element |
|
| 86 | + * Example: |
|
| 87 | + * <input type="file" name="file" /> |
|
| 88 | + * Result: |
|
| 89 | + * FileUpload::$input = file |
|
| 90 | + * |
|
| 91 | + * @since 1.0 |
|
| 92 | + * @version 1.0 |
|
| 93 | + * @var string |
|
| 94 | + */ |
|
| 95 | + private $input; |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Path output |
|
| 99 | + * |
|
| 100 | + * @since 1.0 |
|
| 101 | + * @version 1.0 |
|
| 102 | + * @var string |
|
| 103 | + */ |
|
| 104 | + private $destination_directory; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Output filename |
|
| 108 | + * |
|
| 109 | + * @since 1.0 |
|
| 110 | + * @version 1.0 |
|
| 111 | + * @var string |
|
| 112 | + */ |
|
| 113 | + private $filename; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Max file size |
|
| 117 | + * |
|
| 118 | + * @since 1.0 |
|
| 119 | + * @version 1.0 |
|
| 120 | + * @var float |
|
| 121 | + */ |
|
| 122 | + private $max_file_size= 0.0; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * List of allowed mime types |
|
| 126 | + * |
|
| 127 | + * @since 1.0 |
|
| 128 | + * @version 1.0 |
|
| 129 | + * @var array |
|
| 130 | + */ |
|
| 131 | + private $allowed_mime_types = array(); |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Callbacks |
|
| 135 | + * |
|
| 136 | + * @since 1.0 |
|
| 137 | + * @version 1.0 |
|
| 138 | + * @var array |
|
| 139 | + */ |
|
| 140 | + private $callbacks = array('before' => null, 'after' => null); |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * File object |
|
| 144 | + * |
|
| 145 | + * @since 1.0 |
|
| 146 | + * @version 1.0 |
|
| 147 | + * @var object |
|
| 148 | + */ |
|
| 149 | + private $file; |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Helping mime types |
|
| 153 | + * |
|
| 154 | + * @since 1.0 |
|
| 155 | + * @version 1.0 |
|
| 156 | + * @var array |
|
| 157 | + */ |
|
| 158 | + private $mime_helping = array( |
|
| 159 | + 'text' => array('text/plain',), |
|
| 160 | + 'image' => array( |
|
| 161 | + 'image/jpeg', |
|
| 162 | + 'image/jpg', |
|
| 163 | + 'image/pjpeg', |
|
| 164 | + 'image/png', |
|
| 165 | + 'image/gif', |
|
| 166 | + ), |
|
| 167 | + 'document' => array( |
|
| 168 | + 'application/pdf', |
|
| 169 | + 'application/msword', |
|
| 170 | + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
| 171 | + 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
| 172 | + 'application/vnd.ms-powerpoint', |
|
| 173 | + 'application/vnd.ms-excel', |
|
| 174 | + 'application/vnd.oasis.opendocument.spreadsheet', |
|
| 175 | + 'application/vnd.oasis.opendocument.presentation', |
|
| 176 | + ), |
|
| 177 | + 'video' => array( |
|
| 178 | + 'video/3gpp', |
|
| 179 | + 'video/3gpp', |
|
| 180 | + 'video/x-msvideo', |
|
| 181 | + 'video/avi', |
|
| 182 | + 'video/mpeg4', |
|
| 183 | + 'video/mp4', |
|
| 184 | + 'video/mpeg', |
|
| 185 | + 'video/mpg', |
|
| 186 | + 'video/quicktime', |
|
| 187 | + 'video/x-sgi-movie', |
|
| 188 | + 'video/x-ms-wmv', |
|
| 189 | + 'video/x-flv', |
|
| 190 | + ), |
|
| 191 | + ); |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * The upload error message |
|
| 195 | + * @var array |
|
| 196 | + */ |
|
| 197 | + public $error_messages = array(); |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * The upload error message |
|
| 201 | + * @var string |
|
| 202 | + */ |
|
| 203 | + protected $error = null; |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * The logger instance |
|
| 207 | + * @var Log |
|
| 208 | + */ |
|
| 209 | + private $logger; |
|
| 210 | + |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Construct |
|
| 214 | + * |
|
| 215 | + * @since 0.1 |
|
| 216 | + * @version 1.0.1 |
|
| 217 | + * @return object |
|
| 218 | + * @method object __construct |
|
| 219 | + */ |
|
| 220 | + public function __construct(){ |
|
| 221 | + $this->logger =& class_loader('Log', 'classes'); |
|
| 222 | + $this->logger->setLogger('Library::Upload'); |
|
| 223 | + |
|
| 224 | + Loader::lang('file_upload'); |
|
| 225 | + $obj =& get_instance(); |
|
| 226 | + |
|
| 227 | + $this->error_messages = array( |
|
| 228 | + 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
|
| 229 | + 'upload_err_form_size' => $obj->lang->get('fu_upload_err_form_size'), |
|
| 230 | + 'upload_err_partial' => $obj->lang->get('fu_upload_err_partial'), |
|
| 231 | + 'upload_err_no_file' => $obj->lang->get('fu_upload_err_no_file'), |
|
| 232 | + 'upload_err_no_tmp_dir' => $obj->lang->get('fu_upload_err_no_tmp_dir'), |
|
| 233 | + 'upload_err_cant_write' => $obj->lang->get('fu_upload_err_cant_write'), |
|
| 234 | + 'upload_err_extension' => $obj->lang->get('fu_upload_err_extension'), |
|
| 235 | + 'accept_file_types' => $obj->lang->get('fu_accept_file_types'), |
|
| 236 | + 'file_uploads' => $obj->lang->get('fu_file_uploads_disabled'), |
|
| 237 | + 'max_file_size' => $obj->lang->get('fu_max_file_size'), |
|
| 238 | + 'overwritten_not_allowed' => $obj->lang->get('fu_overwritten_not_allowed'), |
|
| 239 | + ); |
|
| 240 | + |
|
| 241 | + $this->file = array( |
|
| 242 | + 'status' => false, // True: success upload |
|
| 243 | + 'mime' => '', // Empty string |
|
| 244 | + 'filename' => '', // Empty string |
|
| 245 | + 'original' => '', // Empty string |
|
| 246 | + 'size' => 0, // 0 Bytes |
|
| 247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
| 248 | + 'destination' => './', // Default: ./ |
|
| 249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
| 250 | + 'error' => null, // File error |
|
| 251 | + ); |
|
| 252 | + |
|
| 253 | + // Change dir to current dir |
|
| 254 | + $this->destination_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
| 255 | + |
|
| 256 | + // Set file array |
|
| 257 | + if (isset($_FILES) && is_array($_FILES)) { |
|
| 258 | + $this->file_array = $_FILES; |
|
| 259 | + } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
|
| 260 | + $this->file_array = $HTTP_POST_FILES; |
|
| 261 | + } |
|
| 262 | + $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 263 | + } |
|
| 264 | + /** |
|
| 265 | + * Set input. |
|
| 266 | + * If you have $_FILES["file"], you must use the key "file" |
|
| 267 | + * Example: |
|
| 268 | + * $object->setInput("file"); |
|
| 269 | + * |
|
| 270 | + * @since 1.0 |
|
| 271 | + * @version 1.0 |
|
| 272 | + * @param string $input |
|
| 273 | + * @return object |
|
| 274 | + * @method boolean setInput |
|
| 275 | + */ |
|
| 276 | + public function setInput($input) |
|
| 277 | + { |
|
| 278 | + if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 279 | + $this->input = $input; |
|
| 280 | + } |
|
| 281 | + return $this; |
|
| 282 | + } |
|
| 283 | + /** |
|
| 284 | + * Set new filename |
|
| 285 | + * Example: |
|
| 286 | + * FileUpload::setFilename("new file.txt") |
|
| 287 | + * Remember: |
|
| 288 | + * Use %s to retrive file extension |
|
| 289 | + * |
|
| 290 | + * @since 1.0 |
|
| 291 | + * @version 1.0 |
|
| 292 | + * @param string $filename |
|
| 293 | + * @return object |
|
| 294 | + * @method boolean setFilename |
|
| 295 | + */ |
|
| 296 | + public function setFilename($filename) |
|
| 297 | + { |
|
| 298 | + if ($this->isFilename($filename)) { |
|
| 299 | + $this->filename = $filename; |
|
| 300 | + } |
|
| 301 | + return $this; |
|
| 302 | + } |
|
| 303 | + /** |
|
| 304 | + * Set automatic filename |
|
| 305 | + * |
|
| 306 | + * @since 1.0 |
|
| 307 | + * @version 1.5 |
|
| 308 | + * @param string $extension |
|
| 309 | + * @return object |
|
| 310 | + * @method boolean setAutoFilename |
|
| 311 | + */ |
|
| 312 | + public function setAutoFilename() |
|
| 313 | + { |
|
| 314 | + $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 315 | + $this->filename .= time(); |
|
| 316 | + return $this; |
|
| 317 | + } |
|
| 318 | + /** |
|
| 319 | + * Set file size limit |
|
| 320 | + * |
|
| 321 | + * @since 1.0 |
|
| 322 | + * @version 1.0 |
|
| 323 | + * @param double $file_size |
|
| 324 | + * @return object |
|
| 325 | + * @method boolean setMaxFileSize |
|
| 326 | + */ |
|
| 327 | + public function setMaxFileSize($file_size) |
|
| 328 | + { |
|
| 329 | + $file_size = $this->sizeInBytes($file_size); |
|
| 330 | + if (is_numeric($file_size) && $file_size > -1) { |
|
| 331 | + // Get php config |
|
| 332 | + $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
|
| 333 | + // Calculate difference |
|
| 334 | + if ($php_size < $file_size) { |
|
| 335 | + $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
| 336 | + } |
|
| 337 | + $this->max_file_size = $file_size; |
|
| 338 | + } |
|
| 339 | + return $this; |
|
| 340 | + } |
|
| 341 | + /** |
|
| 342 | + * Set array mime types |
|
| 343 | + * |
|
| 344 | + * @since 1.0 |
|
| 345 | + * @version 1.0 |
|
| 346 | + * @param array $mimes |
|
| 347 | + * @return object |
|
| 348 | + * @method boolean setAllowedMimeTypes |
|
| 349 | + */ |
|
| 350 | + public function setAllowedMimeTypes(array $mimes) |
|
| 351 | + { |
|
| 352 | + if (count($mimes) > 0) { |
|
| 353 | + array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 354 | + } |
|
| 355 | + return $this; |
|
| 356 | + } |
|
| 357 | + /** |
|
| 358 | + * Set input callback |
|
| 359 | + * |
|
| 360 | + * @since 1.0 |
|
| 361 | + * @version 1.0 |
|
| 362 | + * @param mixed $callback |
|
| 363 | + * @return object |
|
| 364 | + * @method boolean setCallbackInput |
|
| 365 | + */ |
|
| 366 | + public function setCallbackInput($callback) |
|
| 367 | + { |
|
| 368 | + if (is_callable($callback, false)) { |
|
| 369 | + $this->callbacks['input'] = $callback; |
|
| 370 | + } |
|
| 371 | + return $this; |
|
| 372 | + } |
|
| 373 | + /** |
|
| 374 | + * Set output callback |
|
| 375 | + * |
|
| 376 | + * @since 1.0 |
|
| 377 | + * @version 1.0 |
|
| 378 | + * @param mixed $callback |
|
| 379 | + * @return object |
|
| 380 | + * @method boolean setCallbackOutput |
|
| 381 | + */ |
|
| 382 | + public function setCallbackOutput($callback) |
|
| 383 | + { |
|
| 384 | + if (is_callable($callback, false)) { |
|
| 385 | + $this->callbacks['output'] = $callback; |
|
| 386 | + } |
|
| 387 | + return $this; |
|
| 388 | + } |
|
| 389 | + /** |
|
| 390 | + * Append a mime type to allowed mime types |
|
| 391 | + * |
|
| 392 | + * @since 1.0 |
|
| 393 | + * @version 1.0.1 |
|
| 394 | + * @param string $mime |
|
| 395 | + * @return object |
|
| 396 | + * @method boolean setAllowMimeType |
|
| 397 | + */ |
|
| 398 | + public function setAllowMimeType($mime) |
|
| 399 | + { |
|
| 400 | + if (!empty($mime) && is_string($mime)) { |
|
| 401 | + $this->allowed_mime_types[] = strtolower($mime); |
|
| 402 | + $this->file['allowed_mime_types'][] = strtolower($mime); |
|
| 403 | + } |
|
| 404 | + return $this; |
|
| 405 | + } |
|
| 406 | + /** |
|
| 407 | + * Set allowed mime types from mime helping |
|
| 408 | + * |
|
| 409 | + * @since 1.0.1 |
|
| 410 | + * @version 1.0.1 |
|
| 411 | + * @return object |
|
| 412 | + * @method boolean setMimeHelping |
|
| 413 | + */ |
|
| 414 | + public function setMimeHelping($name) |
|
| 415 | + { |
|
| 416 | + if (!empty($name) && is_string($name)) { |
|
| 417 | + if (array_key_exists($name, $this->mime_helping)) { |
|
| 418 | + return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + return $this; |
|
| 422 | + } |
|
| 423 | + /** |
|
| 424 | + * Set function to upload file |
|
| 425 | + * Examples: |
|
| 426 | + * 1.- FileUpload::setUploadFunction("move_uploaded_file"); |
|
| 427 | + * 2.- FileUpload::setUploadFunction("copy"); |
|
| 428 | + * |
|
| 429 | + * @since 1.0 |
|
| 430 | + * @version 1.0 |
|
| 431 | + * @param string $function |
|
| 432 | + * @return object |
|
| 433 | + * @method boolean setUploadFunction |
|
| 434 | + */ |
|
| 435 | + public function setUploadFunction($function) |
|
| 436 | + { |
|
| 437 | + if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 438 | + if (is_callable( $function)) { |
|
| 439 | + $this->upload_function = $function; |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | + return $this; |
|
| 443 | + } |
|
| 444 | + /** |
|
| 445 | + * Clear allowed mime types cache |
|
| 446 | + * |
|
| 447 | + * @since 1.0 |
|
| 448 | + * @version 1.0 |
|
| 449 | + * @return object |
|
| 450 | + * @method boolean clearAllowedMimeTypes |
|
| 451 | + */ |
|
| 452 | + public function clearAllowedMimeTypes() |
|
| 453 | + { |
|
| 454 | + $this->allowed_mime_types = array(); |
|
| 455 | + $this->file['allowed_mime_types'] = array(); |
|
| 456 | + return $this; |
|
| 457 | + } |
|
| 458 | + /** |
|
| 459 | + * Set destination output |
|
| 460 | + * |
|
| 461 | + * @since 1.0 |
|
| 462 | + * @version 1.0 |
|
| 463 | + * @param string $destination_directory Destination path |
|
| 464 | + * @param boolean $create_if_not_exist |
|
| 465 | + * @return object |
|
| 466 | + * @method boolean setDestinationDirectory |
|
| 467 | + */ |
|
| 468 | + public function setDestinationDirectory($destination_directory, $create_if_not_exist = false) { |
|
| 469 | + $destination_directory = realpath($destination_directory); |
|
| 470 | + if (substr($destination_directory, -1) != DIRECTORY_SEPARATOR) { |
|
| 471 | + $destination_directory .= DIRECTORY_SEPARATOR; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + if ($this->isDirpath($destination_directory)) { |
|
| 475 | + if ($this->dirExists($destination_directory)) { |
|
| 476 | + $this->destination_directory = $destination_directory; |
|
| 477 | + chdir($destination_directory); |
|
| 478 | + } else if ($create_if_not_exist === true) { |
|
| 479 | + if (mkdir($destination_directory, 0775, true)) { |
|
| 480 | + $this->destination_directory = $destination_directory; |
|
| 481 | + chdir($destination_directory); |
|
| 482 | + } |
|
| 483 | + else{ |
|
| 484 | + $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 485 | + } |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + return $this; |
|
| 489 | + } |
|
| 490 | + /** |
|
| 491 | + * Check file exists |
|
| 492 | + * |
|
| 493 | + * @since 1.0 |
|
| 494 | + * @version 1.0.1 |
|
| 495 | + * @param string $file_destination |
|
| 496 | + * @return boolean |
|
| 497 | + * @method boolean fileExists |
|
| 498 | + */ |
|
| 499 | + public function fileExists($file_destination) |
|
| 500 | + { |
|
| 501 | + if ($this->isFilename($file_destination)) { |
|
| 502 | + return (file_exists($file_destination) && is_file($file_destination)); |
|
| 503 | + } |
|
| 504 | + return false; |
|
| 505 | + } |
|
| 506 | + /** |
|
| 507 | + * Check dir exists |
|
| 508 | + * |
|
| 509 | + * @since 1.0 |
|
| 510 | + * @version 1.0.1 |
|
| 511 | + * @param string $path |
|
| 512 | + * @return boolean |
|
| 513 | + * @method boolean dirExists |
|
| 514 | + */ |
|
| 515 | + public function dirExists($path) |
|
| 516 | + { |
|
| 517 | + if ($this->isDirpath($path)) { |
|
| 518 | + return (file_exists($path) && is_dir($path)); |
|
| 519 | + } |
|
| 520 | + return false; |
|
| 521 | + } |
|
| 522 | + /** |
|
| 523 | + * Check valid filename |
|
| 524 | + * |
|
| 525 | + * @since 1.0 |
|
| 526 | + * @version 1.0.1 |
|
| 527 | + * @param string $filename |
|
| 528 | + * @return boolean |
|
| 529 | + * @method boolean isFilename |
|
| 530 | + */ |
|
| 531 | + public function isFilename($filename) |
|
| 532 | + { |
|
| 533 | + $filename = basename($filename); |
|
| 534 | + return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 535 | + } |
|
| 536 | + /** |
|
| 537 | + * Validate mime type with allowed mime types, |
|
| 538 | + * but if allowed mime types is empty, this method return true |
|
| 539 | + * |
|
| 540 | + * @since 1.0 |
|
| 541 | + * @version 1.0 |
|
| 542 | + * @param string $mime |
|
| 543 | + * @return boolean |
|
| 544 | + * @method boolean checkMimeType |
|
| 545 | + */ |
|
| 546 | + public function checkMimeType($mime) |
|
| 547 | + { |
|
| 548 | + if (count($this->allowed_mime_types) == 0) { |
|
| 549 | + return true; |
|
| 550 | + } |
|
| 551 | + return in_array(strtolower($mime), $this->allowed_mime_types); |
|
| 552 | + } |
|
| 553 | + /** |
|
| 554 | + * Retrive status of upload |
|
| 555 | + * |
|
| 556 | + * @since 1.0 |
|
| 557 | + * @version 1.0 |
|
| 558 | + * @return boolean |
|
| 559 | + * @method boolean getStatus |
|
| 560 | + */ |
|
| 561 | + public function getStatus() |
|
| 562 | + { |
|
| 563 | + return $this->file['status']; |
|
| 564 | + } |
|
| 565 | + /** |
|
| 566 | + * Check valid path |
|
| 567 | + * |
|
| 568 | + * @since 1.0 |
|
| 569 | + * @version 1.0.1 |
|
| 570 | + * @param string $filename |
|
| 571 | + * @return boolean |
|
| 572 | + * @method boolean isDirpath |
|
| 573 | + */ |
|
| 574 | + public function isDirpath($path) |
|
| 575 | + { |
|
| 576 | + if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 577 | + if (DIRECTORY_SEPARATOR == '/') { |
|
| 578 | + return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 579 | + } else { |
|
| 580 | + return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 581 | + } |
|
| 582 | + } |
|
| 583 | + return false; |
|
| 584 | + } |
|
| 585 | + /** |
|
| 586 | + * Allow overwriting files |
|
| 587 | + * |
|
| 588 | + * @since 1.0 |
|
| 589 | + * @version 1.0 |
|
| 590 | + * @return object |
|
| 591 | + * @method boolean allowOverwriting |
|
| 592 | + */ |
|
| 593 | + public function allowOverwriting() |
|
| 594 | + { |
|
| 595 | + $this->overwrite_file = true; |
|
| 596 | + return $this; |
|
| 597 | + } |
|
| 598 | + /** |
|
| 599 | + * File info |
|
| 600 | + * |
|
| 601 | + * @since 1.0 |
|
| 602 | + * @version 1.0 |
|
| 603 | + * @return object |
|
| 604 | + * @method object getInfo |
|
| 605 | + */ |
|
| 606 | + public function getInfo() |
|
| 607 | + { |
|
| 608 | + return (object)$this->file; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + |
|
| 612 | + /** |
|
| 613 | + * Check if the file is uploaded |
|
| 614 | + * @return boolean |
|
| 615 | + */ |
|
| 616 | + public function isUploaded(){ |
|
| 617 | + return isset($this->file_array[$this->input]) |
|
| 618 | + && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + /** |
|
| 622 | + * Check if file upload has error |
|
| 623 | + * @return boolean |
|
| 624 | + */ |
|
| 625 | + protected function uploadHasError(){ |
|
| 626 | + //check if file upload is allowed in the configuration |
|
| 627 | + if(! ini_get('file_uploads')){ |
|
| 628 | + $this->setError($this->error_messages['file_uploads']); |
|
| 629 | + return true; |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + //check for php upload error |
|
| 633 | + if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 634 | + $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
|
| 635 | + return true; |
|
| 636 | + } |
|
| 637 | 637 | |
| 638 | - //check for mime type |
|
| 639 | - if (!$this->checkMimeType($this->file['mime'])) { |
|
| 640 | - $this->setError($this->error_messages['accept_file_types']); |
|
| 641 | - return true; |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - // Check file size |
|
| 645 | - if ($this->max_file_size > 0) { |
|
| 646 | - if ($this->max_file_size < $this->file['size']) { |
|
| 647 | - $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
| 648 | - return true; |
|
| 649 | - } |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - // Check if exists file |
|
| 653 | - if ($this->fileExists($this->destination_directory . $this->filename) && $this->overwrite_file === false) { |
|
| 654 | - $this->setError($this->error_messages['overwritten_not_allowed']); |
|
| 655 | - return true; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - return false; |
|
| 659 | - } |
|
| 660 | - /** |
|
| 661 | - * Upload file |
|
| 662 | - * |
|
| 663 | - * @since 1.0 |
|
| 664 | - * @version 1.0.1 |
|
| 665 | - * @return boolean |
|
| 666 | - * @method boolean save |
|
| 667 | - */ |
|
| 668 | - public function save(){ |
|
| 669 | - if (count($this->file_array) > 0) { |
|
| 670 | - if (array_key_exists($this->input, $this->file_array)) { |
|
| 671 | - // set original filename if not have a new name |
|
| 672 | - if (empty($this->filename)) { |
|
| 673 | - $this->filename = $this->file_array[$this->input]['name']; |
|
| 674 | - } |
|
| 675 | - else{ |
|
| 676 | - // Replace %s for extension in filename |
|
| 677 | - // Before: /[\w\d]*(.[\d\w]+)$/i |
|
| 678 | - // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
| 679 | - // Support unicode(utf-8) characters |
|
| 680 | - // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
| 681 | - $extension = preg_replace( |
|
| 682 | - '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
| 683 | - '$1', |
|
| 684 | - $this->file_array[$this->input]['name'] |
|
| 685 | - ); |
|
| 686 | - $this->filename = $this->filename . '.' . $extension; |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - // set file info |
|
| 690 | - $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
| 691 | - $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
| 692 | - $this->file['original'] = $this->file_array[$this->input]['name']; |
|
| 693 | - $this->file['size'] = $this->file_array[$this->input]['size']; |
|
| 694 | - $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
| 695 | - $this->file['destination'] = $this->destination_directory . $this->filename; |
|
| 696 | - $this->file['filename'] = $this->filename; |
|
| 697 | - $this->file['error'] = $this->file_array[$this->input]['error']; |
|
| 698 | - |
|
| 699 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 700 | - |
|
| 701 | - $error = $this->uploadHasError(); |
|
| 702 | - if($error){ |
|
| 703 | - return false; |
|
| 704 | - } |
|
| 705 | - // Execute input callback |
|
| 706 | - if (!empty( $this->callbacks['input'])) { |
|
| 707 | - call_user_func($this->callbacks['input'], (object)$this->file); |
|
| 708 | - } |
|
| 638 | + //check for mime type |
|
| 639 | + if (!$this->checkMimeType($this->file['mime'])) { |
|
| 640 | + $this->setError($this->error_messages['accept_file_types']); |
|
| 641 | + return true; |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + // Check file size |
|
| 645 | + if ($this->max_file_size > 0) { |
|
| 646 | + if ($this->max_file_size < $this->file['size']) { |
|
| 647 | + $this->setError(sprintf($this->error_messages['max_file_size'], $this->sizeFormat($this->max_file_size))); |
|
| 648 | + return true; |
|
| 649 | + } |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + // Check if exists file |
|
| 653 | + if ($this->fileExists($this->destination_directory . $this->filename) && $this->overwrite_file === false) { |
|
| 654 | + $this->setError($this->error_messages['overwritten_not_allowed']); |
|
| 655 | + return true; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + return false; |
|
| 659 | + } |
|
| 660 | + /** |
|
| 661 | + * Upload file |
|
| 662 | + * |
|
| 663 | + * @since 1.0 |
|
| 664 | + * @version 1.0.1 |
|
| 665 | + * @return boolean |
|
| 666 | + * @method boolean save |
|
| 667 | + */ |
|
| 668 | + public function save(){ |
|
| 669 | + if (count($this->file_array) > 0) { |
|
| 670 | + if (array_key_exists($this->input, $this->file_array)) { |
|
| 671 | + // set original filename if not have a new name |
|
| 672 | + if (empty($this->filename)) { |
|
| 673 | + $this->filename = $this->file_array[$this->input]['name']; |
|
| 674 | + } |
|
| 675 | + else{ |
|
| 676 | + // Replace %s for extension in filename |
|
| 677 | + // Before: /[\w\d]*(.[\d\w]+)$/i |
|
| 678 | + // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
|
| 679 | + // Support unicode(utf-8) characters |
|
| 680 | + // Example: "русские.jpeg" is valid; "Zhōngguó.jpeg" is valid; "Tønsberg.jpeg" is valid |
|
| 681 | + $extension = preg_replace( |
|
| 682 | + '/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu', |
|
| 683 | + '$1', |
|
| 684 | + $this->file_array[$this->input]['name'] |
|
| 685 | + ); |
|
| 686 | + $this->filename = $this->filename . '.' . $extension; |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + // set file info |
|
| 690 | + $this->file['mime'] = $this->file_array[$this->input]['type']; |
|
| 691 | + $this->file['tmp'] = $this->file_array[$this->input]['tmp_name']; |
|
| 692 | + $this->file['original'] = $this->file_array[$this->input]['name']; |
|
| 693 | + $this->file['size'] = $this->file_array[$this->input]['size']; |
|
| 694 | + $this->file['sizeFormated'] = $this->sizeFormat($this->file['size']); |
|
| 695 | + $this->file['destination'] = $this->destination_directory . $this->filename; |
|
| 696 | + $this->file['filename'] = $this->filename; |
|
| 697 | + $this->file['error'] = $this->file_array[$this->input]['error']; |
|
| 698 | + |
|
| 699 | + $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 700 | + |
|
| 701 | + $error = $this->uploadHasError(); |
|
| 702 | + if($error){ |
|
| 703 | + return false; |
|
| 704 | + } |
|
| 705 | + // Execute input callback |
|
| 706 | + if (!empty( $this->callbacks['input'])) { |
|
| 707 | + call_user_func($this->callbacks['input'], (object)$this->file); |
|
| 708 | + } |
|
| 709 | 709 | |
| 710 | 710 | |
| 711 | - $this->file['status'] = call_user_func_array( |
|
| 712 | - $this->upload_function, array( |
|
| 713 | - $this->file_array[$this->input]['tmp_name'], |
|
| 714 | - $this->destination_directory . $this->filename |
|
| 715 | - ) |
|
| 716 | - ); |
|
| 717 | - |
|
| 718 | - // Execute output callback |
|
| 719 | - if (!empty( $this->callbacks['output'])) { |
|
| 720 | - call_user_func($this->callbacks['output'], (object)$this->file); |
|
| 721 | - } |
|
| 722 | - return $this->file['status']; |
|
| 723 | - } |
|
| 724 | - } |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * File size for humans. |
|
| 729 | - * |
|
| 730 | - * @since 1.0 |
|
| 731 | - * @version 1.0 |
|
| 732 | - * @param integer $bytes |
|
| 733 | - * @param integer $precision |
|
| 734 | - * @return string |
|
| 735 | - * @method string sizeFormat |
|
| 736 | - */ |
|
| 737 | - public function sizeFormat($size, $precision = 2) |
|
| 738 | - { |
|
| 739 | - if($size > 0){ |
|
| 740 | - $base = log($size) / log(1024); |
|
| 741 | - $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
| 742 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 743 | - } |
|
| 744 | - return null; |
|
| 745 | - } |
|
| 711 | + $this->file['status'] = call_user_func_array( |
|
| 712 | + $this->upload_function, array( |
|
| 713 | + $this->file_array[$this->input]['tmp_name'], |
|
| 714 | + $this->destination_directory . $this->filename |
|
| 715 | + ) |
|
| 716 | + ); |
|
| 717 | + |
|
| 718 | + // Execute output callback |
|
| 719 | + if (!empty( $this->callbacks['output'])) { |
|
| 720 | + call_user_func($this->callbacks['output'], (object)$this->file); |
|
| 721 | + } |
|
| 722 | + return $this->file['status']; |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * File size for humans. |
|
| 729 | + * |
|
| 730 | + * @since 1.0 |
|
| 731 | + * @version 1.0 |
|
| 732 | + * @param integer $bytes |
|
| 733 | + * @param integer $precision |
|
| 734 | + * @return string |
|
| 735 | + * @method string sizeFormat |
|
| 736 | + */ |
|
| 737 | + public function sizeFormat($size, $precision = 2) |
|
| 738 | + { |
|
| 739 | + if($size > 0){ |
|
| 740 | + $base = log($size) / log(1024); |
|
| 741 | + $suffixes = array('B', 'K', 'M', 'G', 'T'); |
|
| 742 | + return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 743 | + } |
|
| 744 | + return null; |
|
| 745 | + } |
|
| 746 | 746 | |
| 747 | 747 | |
| 748 | - /** |
|
| 749 | - * Convert human file size to bytes |
|
| 750 | - * |
|
| 751 | - * @since 1.0 |
|
| 752 | - * @version 1.0.1 |
|
| 753 | - * @param integer|double $size |
|
| 754 | - * @return integer|double |
|
| 755 | - * @method string sizeInBytes |
|
| 756 | - */ |
|
| 757 | - public function sizeInBytes($size) |
|
| 758 | - { |
|
| 759 | - $unit = 'B'; |
|
| 760 | - $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
| 761 | - $matches = array(); |
|
| 762 | - preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
| 763 | - if (array_key_exists('unit', $matches)) { |
|
| 764 | - $unit = strtoupper($matches['unit']); |
|
| 765 | - } |
|
| 766 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - /** |
|
| 770 | - * Get the upload error message |
|
| 771 | - * @return string |
|
| 772 | - */ |
|
| 773 | - public function getError(){ |
|
| 774 | - return $this->error; |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - /** |
|
| 778 | - * Set the upload error message |
|
| 779 | - * @param string $message the upload error message to set |
|
| 780 | - */ |
|
| 781 | - public function setError($message){ |
|
| 782 | - $this->logger->info('The file upload got error : ' . $message); |
|
| 783 | - $this->error = $message; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Get the PHP upload error message for the given code |
|
| 788 | - * @param int $code the error code |
|
| 789 | - * @return string the error message |
|
| 790 | - */ |
|
| 791 | - private function getPhpUploadErrorMessageByCode($code){ |
|
| 792 | - $codeMessageMaps = array( |
|
| 793 | - 1 => $this->error_messages['upload_err_ini_size'], |
|
| 794 | - 2 => $this->error_messages['upload_err_form_size'], |
|
| 795 | - 3 => $this->error_messages['upload_err_partial'], |
|
| 796 | - 4 => $this->error_messages['upload_err_no_file'], |
|
| 797 | - 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
| 798 | - 7 => $this->error_messages['upload_err_cant_write'], |
|
| 799 | - 8 => $this->error_messages['upload_err_extension'], |
|
| 800 | - ); |
|
| 801 | - return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
| 802 | - } |
|
| 803 | - } |
|
| 748 | + /** |
|
| 749 | + * Convert human file size to bytes |
|
| 750 | + * |
|
| 751 | + * @since 1.0 |
|
| 752 | + * @version 1.0.1 |
|
| 753 | + * @param integer|double $size |
|
| 754 | + * @return integer|double |
|
| 755 | + * @method string sizeInBytes |
|
| 756 | + */ |
|
| 757 | + public function sizeInBytes($size) |
|
| 758 | + { |
|
| 759 | + $unit = 'B'; |
|
| 760 | + $units = array('B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4); |
|
| 761 | + $matches = array(); |
|
| 762 | + preg_match('/(?<size>[\d\.]+)\s*(?<unit>b|k|m|g|t)?/i', $size, $matches); |
|
| 763 | + if (array_key_exists('unit', $matches)) { |
|
| 764 | + $unit = strtoupper($matches['unit']); |
|
| 765 | + } |
|
| 766 | + return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + /** |
|
| 770 | + * Get the upload error message |
|
| 771 | + * @return string |
|
| 772 | + */ |
|
| 773 | + public function getError(){ |
|
| 774 | + return $this->error; |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + /** |
|
| 778 | + * Set the upload error message |
|
| 779 | + * @param string $message the upload error message to set |
|
| 780 | + */ |
|
| 781 | + public function setError($message){ |
|
| 782 | + $this->logger->info('The file upload got error : ' . $message); |
|
| 783 | + $this->error = $message; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Get the PHP upload error message for the given code |
|
| 788 | + * @param int $code the error code |
|
| 789 | + * @return string the error message |
|
| 790 | + */ |
|
| 791 | + private function getPhpUploadErrorMessageByCode($code){ |
|
| 792 | + $codeMessageMaps = array( |
|
| 793 | + 1 => $this->error_messages['upload_err_ini_size'], |
|
| 794 | + 2 => $this->error_messages['upload_err_form_size'], |
|
| 795 | + 3 => $this->error_messages['upload_err_partial'], |
|
| 796 | + 4 => $this->error_messages['upload_err_no_file'], |
|
| 797 | + 6 => $this->error_messages['upload_err_no_tmp_dir'], |
|
| 798 | + 7 => $this->error_messages['upload_err_cant_write'], |
|
| 799 | + 8 => $this->error_messages['upload_err_extension'], |
|
| 800 | + ); |
|
| 801 | + return isset($codeMessageMaps[$code]) ? $codeMessageMaps[$code] : null; |
|
| 802 | + } |
|
| 803 | + } |
|
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * @package FileUpload |
| 38 | 38 | * @version 1.5 |
| 39 | 39 | */ |
| 40 | - class Upload{ |
|
| 40 | + class Upload { |
|
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * Version |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | * @version 1.0 |
| 70 | 70 | * @var array |
| 71 | 71 | */ |
| 72 | - private $file_array = array(); |
|
| 72 | + private $file_array = array(); |
|
| 73 | 73 | |
| 74 | 74 | /** |
| 75 | 75 | * If the file you are trying to upload already exists it will |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @version 1.0 |
| 120 | 120 | * @var float |
| 121 | 121 | */ |
| 122 | - private $max_file_size= 0.0; |
|
| 122 | + private $max_file_size = 0.0; |
|
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | 125 | * List of allowed mime types |
@@ -217,12 +217,12 @@ discard block |
||
| 217 | 217 | * @return object |
| 218 | 218 | * @method object __construct |
| 219 | 219 | */ |
| 220 | - public function __construct(){ |
|
| 221 | - $this->logger =& class_loader('Log', 'classes'); |
|
| 220 | + public function __construct() { |
|
| 221 | + $this->logger = & class_loader('Log', 'classes'); |
|
| 222 | 222 | $this->logger->setLogger('Library::Upload'); |
| 223 | 223 | |
| 224 | 224 | Loader::lang('file_upload'); |
| 225 | - $obj =& get_instance(); |
|
| 225 | + $obj = & get_instance(); |
|
| 226 | 226 | |
| 227 | 227 | $this->error_messages = array( |
| 228 | 228 | 'upload_err_ini_size' => $obj->lang->get('fu_upload_err_ini_size'), |
@@ -239,15 +239,15 @@ discard block |
||
| 239 | 239 | ); |
| 240 | 240 | |
| 241 | 241 | $this->file = array( |
| 242 | - 'status' => false, // True: success upload |
|
| 243 | - 'mime' => '', // Empty string |
|
| 244 | - 'filename' => '', // Empty string |
|
| 245 | - 'original' => '', // Empty string |
|
| 246 | - 'size' => 0, // 0 Bytes |
|
| 247 | - 'sizeFormated' => '0B', // 0 Bytes |
|
| 248 | - 'destination' => './', // Default: ./ |
|
| 249 | - 'allowed_mime_types' => array(), // Allowed mime types |
|
| 250 | - 'error' => null, // File error |
|
| 242 | + 'status' => false, // True: success upload |
|
| 243 | + 'mime' => '', // Empty string |
|
| 244 | + 'filename' => '', // Empty string |
|
| 245 | + 'original' => '', // Empty string |
|
| 246 | + 'size' => 0, // 0 Bytes |
|
| 247 | + 'sizeFormated' => '0B', // 0 Bytes |
|
| 248 | + 'destination' => './', // Default: ./ |
|
| 249 | + 'allowed_mime_types' => array(), // Allowed mime types |
|
| 250 | + 'error' => null, // File error |
|
| 251 | 251 | ); |
| 252 | 252 | |
| 253 | 253 | // Change dir to current dir |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | } elseif (isset($HTTP_POST_FILES) && is_array($HTTP_POST_FILES)) { |
| 260 | 260 | $this->file_array = $HTTP_POST_FILES; |
| 261 | 261 | } |
| 262 | - $this->logger->info('The upload file information are : ' .stringfy_vars($this->file_array)); |
|
| 262 | + $this->logger->info('The upload file information are : ' . stringfy_vars($this->file_array)); |
|
| 263 | 263 | } |
| 264 | 264 | /** |
| 265 | 265 | * Set input. |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | */ |
| 276 | 276 | public function setInput($input) |
| 277 | 277 | { |
| 278 | - if (!empty($input) && (is_string($input) || is_numeric($input) )) { |
|
| 278 | + if (!empty($input) && (is_string($input) || is_numeric($input))) { |
|
| 279 | 279 | $this->input = $input; |
| 280 | 280 | } |
| 281 | 281 | return $this; |
@@ -311,7 +311,7 @@ discard block |
||
| 311 | 311 | */ |
| 312 | 312 | public function setAutoFilename() |
| 313 | 313 | { |
| 314 | - $this->filename = sha1(mt_rand(1, 9999).uniqid()); |
|
| 314 | + $this->filename = sha1(mt_rand(1, 9999) . uniqid()); |
|
| 315 | 315 | $this->filename .= time(); |
| 316 | 316 | return $this; |
| 317 | 317 | } |
@@ -332,7 +332,7 @@ discard block |
||
| 332 | 332 | $php_size = $this->sizeInBytes((int) ini_get('upload_max_filesize')); |
| 333 | 333 | // Calculate difference |
| 334 | 334 | if ($php_size < $file_size) { |
| 335 | - $this->logger->warning('The upload max file size you set [' .$file_size. '] is greather than the PHP configuration for upload max file size [' .$php_size. ']'); |
|
| 335 | + $this->logger->warning('The upload max file size you set [' . $file_size . '] is greather than the PHP configuration for upload max file size [' . $php_size . ']'); |
|
| 336 | 336 | } |
| 337 | 337 | $this->max_file_size = $file_size; |
| 338 | 338 | } |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | public function setAllowedMimeTypes(array $mimes) |
| 351 | 351 | { |
| 352 | 352 | if (count($mimes) > 0) { |
| 353 | - array_map(array($this , 'setAllowMimeType'), $mimes); |
|
| 353 | + array_map(array($this, 'setAllowMimeType'), $mimes); |
|
| 354 | 354 | } |
| 355 | 355 | return $this; |
| 356 | 356 | } |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | { |
| 416 | 416 | if (!empty($name) && is_string($name)) { |
| 417 | 417 | if (array_key_exists($name, $this->mime_helping)) { |
| 418 | - return $this->setAllowedMimeTypes($this->mime_helping[ $name ]); |
|
| 418 | + return $this->setAllowedMimeTypes($this->mime_helping[$name]); |
|
| 419 | 419 | } |
| 420 | 420 | } |
| 421 | 421 | return $this; |
@@ -434,8 +434,8 @@ discard block |
||
| 434 | 434 | */ |
| 435 | 435 | public function setUploadFunction($function) |
| 436 | 436 | { |
| 437 | - if (!empty($function) && (is_array($function) || is_string($function) )) { |
|
| 438 | - if (is_callable( $function)) { |
|
| 437 | + if (!empty($function) && (is_array($function) || is_string($function))) { |
|
| 438 | + if (is_callable($function)) { |
|
| 439 | 439 | $this->upload_function = $function; |
| 440 | 440 | } |
| 441 | 441 | } |
@@ -480,8 +480,8 @@ discard block |
||
| 480 | 480 | $this->destination_directory = $destination_directory; |
| 481 | 481 | chdir($destination_directory); |
| 482 | 482 | } |
| 483 | - else{ |
|
| 484 | - $this->logger->warning('Can not create the upload directory [' .$destination_directory. ']'); |
|
| 483 | + else { |
|
| 484 | + $this->logger->warning('Can not create the upload directory [' . $destination_directory . ']'); |
|
| 485 | 485 | } |
| 486 | 486 | } |
| 487 | 487 | } |
@@ -531,7 +531,7 @@ discard block |
||
| 531 | 531 | public function isFilename($filename) |
| 532 | 532 | { |
| 533 | 533 | $filename = basename($filename); |
| 534 | - return (!empty($filename) && (is_string( $filename) || is_numeric($filename))); |
|
| 534 | + return (!empty($filename) && (is_string($filename) || is_numeric($filename))); |
|
| 535 | 535 | } |
| 536 | 536 | /** |
| 537 | 537 | * Validate mime type with allowed mime types, |
@@ -573,11 +573,11 @@ discard block |
||
| 573 | 573 | */ |
| 574 | 574 | public function isDirpath($path) |
| 575 | 575 | { |
| 576 | - if (!empty( $path) && (is_string( $path) || is_numeric($path) )) { |
|
| 576 | + if (!empty($path) && (is_string($path) || is_numeric($path))) { |
|
| 577 | 577 | if (DIRECTORY_SEPARATOR == '/') { |
| 578 | - return (preg_match( '/^[^*?"<>|:]*$/' , $path) == 1 ); |
|
| 578 | + return (preg_match('/^[^*?"<>|:]*$/', $path) == 1); |
|
| 579 | 579 | } else { |
| 580 | - return (preg_match( "/^[^*?\"<>|:]*$/" , substr($path,2) ) == 1); |
|
| 580 | + return (preg_match("/^[^*?\"<>|:]*$/", substr($path, 2)) == 1); |
|
| 581 | 581 | } |
| 582 | 582 | } |
| 583 | 583 | return false; |
@@ -605,7 +605,7 @@ discard block |
||
| 605 | 605 | */ |
| 606 | 606 | public function getInfo() |
| 607 | 607 | { |
| 608 | - return (object)$this->file; |
|
| 608 | + return (object) $this->file; |
|
| 609 | 609 | } |
| 610 | 610 | |
| 611 | 611 | |
@@ -613,7 +613,7 @@ discard block |
||
| 613 | 613 | * Check if the file is uploaded |
| 614 | 614 | * @return boolean |
| 615 | 615 | */ |
| 616 | - public function isUploaded(){ |
|
| 616 | + public function isUploaded() { |
|
| 617 | 617 | return isset($this->file_array[$this->input]) |
| 618 | 618 | && is_uploaded_file($this->file_array[$this->input]['tmp_name']); |
| 619 | 619 | } |
@@ -622,15 +622,15 @@ discard block |
||
| 622 | 622 | * Check if file upload has error |
| 623 | 623 | * @return boolean |
| 624 | 624 | */ |
| 625 | - protected function uploadHasError(){ |
|
| 625 | + protected function uploadHasError() { |
|
| 626 | 626 | //check if file upload is allowed in the configuration |
| 627 | - if(! ini_get('file_uploads')){ |
|
| 627 | + if (!ini_get('file_uploads')) { |
|
| 628 | 628 | $this->setError($this->error_messages['file_uploads']); |
| 629 | 629 | return true; |
| 630 | 630 | } |
| 631 | 631 | |
| 632 | 632 | //check for php upload error |
| 633 | - if(is_numeric($this->file['error']) && $this->file['error'] > 0){ |
|
| 633 | + if (is_numeric($this->file['error']) && $this->file['error'] > 0) { |
|
| 634 | 634 | $this->setError($this->getPhpUploadErrorMessageByCode($this->file['error'])); |
| 635 | 635 | return true; |
| 636 | 636 | } |
@@ -665,14 +665,14 @@ discard block |
||
| 665 | 665 | * @return boolean |
| 666 | 666 | * @method boolean save |
| 667 | 667 | */ |
| 668 | - public function save(){ |
|
| 668 | + public function save() { |
|
| 669 | 669 | if (count($this->file_array) > 0) { |
| 670 | 670 | if (array_key_exists($this->input, $this->file_array)) { |
| 671 | 671 | // set original filename if not have a new name |
| 672 | 672 | if (empty($this->filename)) { |
| 673 | 673 | $this->filename = $this->file_array[$this->input]['name']; |
| 674 | 674 | } |
| 675 | - else{ |
|
| 675 | + else { |
|
| 676 | 676 | // Replace %s for extension in filename |
| 677 | 677 | // Before: /[\w\d]*(.[\d\w]+)$/i |
| 678 | 678 | // After: /^[\s[:alnum:]\-\_\.]*\.([\d\w]+)$/iu |
@@ -696,15 +696,15 @@ discard block |
||
| 696 | 696 | $this->file['filename'] = $this->filename; |
| 697 | 697 | $this->file['error'] = $this->file_array[$this->input]['error']; |
| 698 | 698 | |
| 699 | - $this->logger->info('The upload file information to process is : ' .stringfy_vars($this->file)); |
|
| 699 | + $this->logger->info('The upload file information to process is : ' . stringfy_vars($this->file)); |
|
| 700 | 700 | |
| 701 | 701 | $error = $this->uploadHasError(); |
| 702 | - if($error){ |
|
| 702 | + if ($error) { |
|
| 703 | 703 | return false; |
| 704 | 704 | } |
| 705 | 705 | // Execute input callback |
| 706 | - if (!empty( $this->callbacks['input'])) { |
|
| 707 | - call_user_func($this->callbacks['input'], (object)$this->file); |
|
| 706 | + if (!empty($this->callbacks['input'])) { |
|
| 707 | + call_user_func($this->callbacks['input'], (object) $this->file); |
|
| 708 | 708 | } |
| 709 | 709 | |
| 710 | 710 | |
@@ -716,8 +716,8 @@ discard block |
||
| 716 | 716 | ); |
| 717 | 717 | |
| 718 | 718 | // Execute output callback |
| 719 | - if (!empty( $this->callbacks['output'])) { |
|
| 720 | - call_user_func($this->callbacks['output'], (object)$this->file); |
|
| 719 | + if (!empty($this->callbacks['output'])) { |
|
| 720 | + call_user_func($this->callbacks['output'], (object) $this->file); |
|
| 721 | 721 | } |
| 722 | 722 | return $this->file['status']; |
| 723 | 723 | } |
@@ -736,10 +736,10 @@ discard block |
||
| 736 | 736 | */ |
| 737 | 737 | public function sizeFormat($size, $precision = 2) |
| 738 | 738 | { |
| 739 | - if($size > 0){ |
|
| 739 | + if ($size > 0) { |
|
| 740 | 740 | $base = log($size) / log(1024); |
| 741 | 741 | $suffixes = array('B', 'K', 'M', 'G', 'T'); |
| 742 | - return round(pow(1024, $base - floor($base)), $precision) . ( isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 742 | + return round(pow(1024, $base - floor($base)), $precision) . (isset($suffixes[floor($base)]) ? $suffixes[floor($base)] : ''); |
|
| 743 | 743 | } |
| 744 | 744 | return null; |
| 745 | 745 | } |
@@ -763,14 +763,14 @@ discard block |
||
| 763 | 763 | if (array_key_exists('unit', $matches)) { |
| 764 | 764 | $unit = strtoupper($matches['unit']); |
| 765 | 765 | } |
| 766 | - return (floatval($matches['size']) * pow(1024, $units[$unit]) ) ; |
|
| 766 | + return (floatval($matches['size']) * pow(1024, $units[$unit])); |
|
| 767 | 767 | } |
| 768 | 768 | |
| 769 | 769 | /** |
| 770 | 770 | * Get the upload error message |
| 771 | 771 | * @return string |
| 772 | 772 | */ |
| 773 | - public function getError(){ |
|
| 773 | + public function getError() { |
|
| 774 | 774 | return $this->error; |
| 775 | 775 | } |
| 776 | 776 | |
@@ -778,7 +778,7 @@ discard block |
||
| 778 | 778 | * Set the upload error message |
| 779 | 779 | * @param string $message the upload error message to set |
| 780 | 780 | */ |
| 781 | - public function setError($message){ |
|
| 781 | + public function setError($message) { |
|
| 782 | 782 | $this->logger->info('The file upload got error : ' . $message); |
| 783 | 783 | $this->error = $message; |
| 784 | 784 | } |
@@ -788,7 +788,7 @@ discard block |
||
| 788 | 788 | * @param int $code the error code |
| 789 | 789 | * @return string the error message |
| 790 | 790 | */ |
| 791 | - private function getPhpUploadErrorMessageByCode($code){ |
|
| 791 | + private function getPhpUploadErrorMessageByCode($code) { |
|
| 792 | 792 | $codeMessageMaps = array( |
| 793 | 793 | 1 => $this->error_messages['upload_err_ini_size'], |
| 794 | 794 | 2 => $this->error_messages['upload_err_form_size'], |
@@ -1,914 +1,914 @@ |
||
| 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 | - * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
| 214 | - * each rule against the data value. Sets _success to true if there are no errors |
|
| 215 | - * afterwards. |
|
| 216 | - */ |
|
| 217 | - protected function _run() { |
|
| 218 | - if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 219 | - $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
| 220 | - //first check for CSRF |
|
| 221 | - if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 222 | - show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 223 | - } |
|
| 224 | - else{ |
|
| 225 | - $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - ///////////////////////////////////////////// |
|
| 229 | - $this->_forceFail = false; |
|
| 230 | - |
|
| 231 | - foreach ($this->getData() as $inputName => $inputVal) { |
|
| 232 | - if(is_array($this->data[$inputName])){ |
|
| 233 | - $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 234 | - } |
|
| 235 | - else{ |
|
| 236 | - $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - if (array_key_exists($inputName, $this->_rules)) { |
|
| 240 | - foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 241 | - $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 247 | - $this->_success = true; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Adds a rule to a form data validation field. |
|
| 253 | - * |
|
| 254 | - * @param string $inputField Name of the field or the data key to add a rule to |
|
| 255 | - * @param string $ruleSets PIPE seperated string of rules |
|
| 256 | - * |
|
| 257 | - * @return FormValidation Current instance of object. |
|
| 258 | - */ |
|
| 259 | - public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 260 | - $this->_rules[$inputField] = $ruleSets; |
|
| 261 | - $this->_labels[$inputField] = $inputLabel; |
|
| 262 | - $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 263 | - return $this; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 268 | - * of rule names rather than a pipe-delimited string as well. |
|
| 269 | - * @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 | + * Takes and trims each data, if it has any rules, we parse the rule string and run |
|
| 214 | + * each rule against the data value. Sets _success to true if there are no errors |
|
| 215 | + * afterwards. |
|
| 216 | + */ |
|
| 217 | + protected function _run() { |
|
| 218 | + if(get_instance()->request->method() == 'POST' || $this->enableCsrfCheck){ |
|
| 219 | + $this->logger->debug('Check if CSRF is enabled in configuration'); |
|
| 220 | + //first check for CSRF |
|
| 221 | + if ((get_config('csrf_enable', false) || $this->enableCsrfCheck) && ! Security::validateCSRF()){ |
|
| 222 | + show_error('Invalide data, Cross Site Request Forgery do his job, the data to validate is corrupted.'); |
|
| 223 | + } |
|
| 224 | + else{ |
|
| 225 | + $this->logger->info('CSRF is not enabled in configuration or not set manully, no need to check it'); |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + ///////////////////////////////////////////// |
|
| 229 | + $this->_forceFail = false; |
|
| 230 | + |
|
| 231 | + foreach ($this->getData() as $inputName => $inputVal) { |
|
| 232 | + if(is_array($this->data[$inputName])){ |
|
| 233 | + $this->data[$inputName] = array_map('trim', $this->data[$inputName]); |
|
| 234 | + } |
|
| 235 | + else{ |
|
| 236 | + $this->data[$inputName] = trim($this->data[$inputName]); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + if (array_key_exists($inputName, $this->_rules)) { |
|
| 240 | + foreach ($this->_parseRuleString($this->_rules[$inputName]) as $eachRule) { |
|
| 241 | + $this->_validateRule($inputName, $this->data[$inputName], $eachRule); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + if (empty($this->_errors) && $this->_forceFail === false) { |
|
| 247 | + $this->_success = true; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Adds a rule to a form data validation field. |
|
| 253 | + * |
|
| 254 | + * @param string $inputField Name of the field or the data key to add a rule to |
|
| 255 | + * @param string $ruleSets PIPE seperated string of rules |
|
| 270 | 256 | * |
| 271 | 257 | * @return FormValidation Current instance of object. |
| 272 | - */ |
|
| 273 | - public function setRules(array $ruleSets) { |
|
| 274 | - foreach ($ruleSets as $ruleSet) { |
|
| 275 | - $pipeDelimitedRules = null; |
|
| 276 | - if (is_array($ruleSet['rules'])) { |
|
| 277 | - $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 278 | - } else { |
|
| 279 | - $pipeDelimitedRules = $ruleSet['rules']; |
|
| 280 | - } |
|
| 281 | - $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 282 | - } |
|
| 283 | - return $this; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 288 | - * end of the errors block respectively. |
|
| 289 | - * |
|
| 290 | - * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 291 | - * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 292 | - * |
|
| 258 | + */ |
|
| 259 | + public function setRule($inputField, $inputLabel, $ruleSets) { |
|
| 260 | + $this->_rules[$inputField] = $ruleSets; |
|
| 261 | + $this->_labels[$inputField] = $inputLabel; |
|
| 262 | + $this->logger->info('Set the field rule: name [' .$inputField. '], label [' .$inputLabel. '], rules [' .$ruleSets. ']'); |
|
| 263 | + return $this; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Takes an array of rules and uses setRule() to set them, accepts an array |
|
| 268 | + * of rule names rather than a pipe-delimited string as well. |
|
| 269 | + * @param array $ruleSets |
|
| 270 | + * |
|
| 293 | 271 | * @return FormValidation Current instance of object. |
| 294 | - */ |
|
| 295 | - public function setErrorsDelimiter($start, $end) { |
|
| 296 | - $this->_allErrorsDelimiter[0] = $start; |
|
| 297 | - $this->_allErrorsDelimiter[1] = $end; |
|
| 298 | - return $this; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * This is the individual error delimiter, each argument occurs once before and after |
|
| 303 | - * each individual error listed. |
|
| 304 | - * |
|
| 305 | - * @param string $start Displayed before each error. |
|
| 306 | - * @param string $end Displayed after each error. |
|
| 307 | - * |
|
| 272 | + */ |
|
| 273 | + public function setRules(array $ruleSets) { |
|
| 274 | + foreach ($ruleSets as $ruleSet) { |
|
| 275 | + $pipeDelimitedRules = null; |
|
| 276 | + if (is_array($ruleSet['rules'])) { |
|
| 277 | + $pipeDelimitedRules = implode('|', $ruleSet['rules']); |
|
| 278 | + } else { |
|
| 279 | + $pipeDelimitedRules = $ruleSet['rules']; |
|
| 280 | + } |
|
| 281 | + $this->setRule($ruleSet['name'], $ruleSet['label'], $pipeDelimitedRules); |
|
| 282 | + } |
|
| 283 | + return $this; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * This method creates the global errors delimiter, each argument occurs once, at the beginning, and |
|
| 288 | + * end of the errors block respectively. |
|
| 289 | + * |
|
| 290 | + * @param string $start Before block of errors gets displayed, HTML allowed. |
|
| 291 | + * @param string $end After the block of errors gets displayed, HTML allowed. |
|
| 292 | + * |
|
| 308 | 293 | * @return FormValidation Current instance of object. |
| 309 | - */ |
|
| 310 | - public function setErrorDelimiter($start, $end) { |
|
| 311 | - $this->_eachErrorDelimiter[0] = $start; |
|
| 312 | - $this->_eachErrorDelimiter[1] = $end; |
|
| 313 | - return $this; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Get the each errors delimiters |
|
| 318 | - * |
|
| 319 | - * @return array |
|
| 320 | - */ |
|
| 321 | - public function getErrorDelimiter() { |
|
| 322 | - return $this->_eachErrorDelimiter; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Get the all errors delimiters |
|
| 327 | - * |
|
| 328 | - * @return array |
|
| 329 | - */ |
|
| 330 | - public function getErrorsDelimiter() { |
|
| 331 | - return $this->_allErrorsDelimiter; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * This sets a custom error message that can override the default error phrase provided |
|
| 336 | - * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 337 | - * which will globally change the error phrase of that rule, or in the format of: |
|
| 338 | - * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 339 | - * that rule, applied on that field. |
|
| 340 | - * |
|
| 341 | - * @return boolean True on success, false on failure. |
|
| 342 | - */ |
|
| 343 | - public function setMessage() { |
|
| 344 | - $numArgs = func_num_args(); |
|
| 345 | - switch ($numArgs) { |
|
| 346 | - default: |
|
| 347 | - return false; |
|
| 348 | - // A global rule error message |
|
| 349 | - case 2: |
|
| 350 | - foreach ($this->post(null) as $key => $val) { |
|
| 351 | - $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 352 | - } |
|
| 353 | - break; |
|
| 354 | - // Field specific rule error message |
|
| 355 | - case 3: |
|
| 356 | - $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 357 | - break; |
|
| 358 | - } |
|
| 359 | - return true; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Adds a custom error message in the errorSet array, that will |
|
| 364 | - * forcibly display it. |
|
| 365 | - * |
|
| 366 | - * @param string $inputName The form input name or data key |
|
| 367 | - * @param string $errorMessage Error to display |
|
| 368 | - * |
|
| 369 | - * @return formValidation Current instance of the object |
|
| 370 | - */ |
|
| 371 | - public function setCustomError($inputName, $errorMessage) { |
|
| 372 | - $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 373 | - $this->_errors[$inputName] = $errorMessage; |
|
| 374 | - return $this; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 379 | - * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 380 | - * all values. |
|
| 381 | - * |
|
| 382 | - * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 383 | - * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 384 | - * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 385 | - */ |
|
| 386 | - public function post($key = null, $trim = true) { |
|
| 387 | - $returnValue = null; |
|
| 388 | - if (is_null($key)) { |
|
| 389 | - $returnValue = array(); |
|
| 390 | - foreach ($this->getData() as $key => $val) { |
|
| 391 | - $returnValue[$key] = $this->post($key, $trim); |
|
| 392 | - } |
|
| 393 | - } else { |
|
| 394 | - $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 395 | - } |
|
| 396 | - return $returnValue; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 401 | - * function or just returned. |
|
| 402 | - * |
|
| 403 | - * @param boolean $limit number of error to display or return |
|
| 404 | - * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 405 | - * |
|
| 406 | - * @return string Errors formatted for output |
|
| 407 | - */ |
|
| 408 | - public function displayErrors($limit = null, $echo = true) { |
|
| 409 | - list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 410 | - list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 411 | - $errorOutput = $errorsStart; |
|
| 412 | - $i = 0; |
|
| 413 | - if (!empty($this->_errors)) { |
|
| 414 | - foreach ($this->_errors as $fieldName => $error) { |
|
| 415 | - if ($i === $limit) { |
|
| 416 | - break; |
|
| 417 | - } |
|
| 418 | - $errorOutput .= $errorStart; |
|
| 419 | - $errorOutput .= $error; |
|
| 420 | - $errorOutput .= $errorEnd; |
|
| 421 | - $i++; |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - $errorOutput .= $errorsEnd; |
|
| 425 | - echo ($echo) ? $errorOutput : ''; |
|
| 426 | - return (! $echo) ? $errorOutput : null; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Returns raw array of errors in no format instead of displaying them |
|
| 431 | - * formatted. |
|
| 432 | - * |
|
| 433 | - * @return array |
|
| 434 | - */ |
|
| 435 | - public function returnErrors() { |
|
| 436 | - return $this->_errors; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 441 | - * |
|
| 442 | - * @param string $ruleString String to be parsed. |
|
| 443 | - * |
|
| 444 | - * @return array Array of each value in original string. |
|
| 445 | - */ |
|
| 446 | - protected function _parseRuleString($ruleString) { |
|
| 447 | - $ruleSets = array(); |
|
| 448 | - /* |
|
| 294 | + */ |
|
| 295 | + public function setErrorsDelimiter($start, $end) { |
|
| 296 | + $this->_allErrorsDelimiter[0] = $start; |
|
| 297 | + $this->_allErrorsDelimiter[1] = $end; |
|
| 298 | + return $this; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * This is the individual error delimiter, each argument occurs once before and after |
|
| 303 | + * each individual error listed. |
|
| 304 | + * |
|
| 305 | + * @param string $start Displayed before each error. |
|
| 306 | + * @param string $end Displayed after each error. |
|
| 307 | + * |
|
| 308 | + * @return FormValidation Current instance of object. |
|
| 309 | + */ |
|
| 310 | + public function setErrorDelimiter($start, $end) { |
|
| 311 | + $this->_eachErrorDelimiter[0] = $start; |
|
| 312 | + $this->_eachErrorDelimiter[1] = $end; |
|
| 313 | + return $this; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Get the each errors delimiters |
|
| 318 | + * |
|
| 319 | + * @return array |
|
| 320 | + */ |
|
| 321 | + public function getErrorDelimiter() { |
|
| 322 | + return $this->_eachErrorDelimiter; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Get the all errors delimiters |
|
| 327 | + * |
|
| 328 | + * @return array |
|
| 329 | + */ |
|
| 330 | + public function getErrorsDelimiter() { |
|
| 331 | + return $this->_allErrorsDelimiter; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * This sets a custom error message that can override the default error phrase provided |
|
| 336 | + * by FormValidation, it can be used in the format of setMessage('rule', 'error phrase') |
|
| 337 | + * which will globally change the error phrase of that rule, or in the format of: |
|
| 338 | + * setMessage('rule', 'fieldname', 'error phrase') - which will only change the error phrase for |
|
| 339 | + * that rule, applied on that field. |
|
| 340 | + * |
|
| 341 | + * @return boolean True on success, false on failure. |
|
| 342 | + */ |
|
| 343 | + public function setMessage() { |
|
| 344 | + $numArgs = func_num_args(); |
|
| 345 | + switch ($numArgs) { |
|
| 346 | + default: |
|
| 347 | + return false; |
|
| 348 | + // A global rule error message |
|
| 349 | + case 2: |
|
| 350 | + foreach ($this->post(null) as $key => $val) { |
|
| 351 | + $this->_errorPhraseOverrides[$key][func_get_arg(0)] = func_get_arg(1); |
|
| 352 | + } |
|
| 353 | + break; |
|
| 354 | + // Field specific rule error message |
|
| 355 | + case 3: |
|
| 356 | + $this->_errorPhraseOverrides[func_get_arg(1)][func_get_arg(0)] = func_get_arg(2); |
|
| 357 | + break; |
|
| 358 | + } |
|
| 359 | + return true; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Adds a custom error message in the errorSet array, that will |
|
| 364 | + * forcibly display it. |
|
| 365 | + * |
|
| 366 | + * @param string $inputName The form input name or data key |
|
| 367 | + * @param string $errorMessage Error to display |
|
| 368 | + * |
|
| 369 | + * @return formValidation Current instance of the object |
|
| 370 | + */ |
|
| 371 | + public function setCustomError($inputName, $errorMessage) { |
|
| 372 | + $errorMessage = str_replace('%1', $this->_labels[$inputName], $errorMessage); |
|
| 373 | + $this->_errors[$inputName] = $errorMessage; |
|
| 374 | + return $this; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Allows for an accesor to any/all post values, if a value of null is passed as the key, it |
|
| 379 | + * will recursively find all keys/values of the $_POST array or data array. It also automatically trims |
|
| 380 | + * all values. |
|
| 381 | + * |
|
| 382 | + * @param string $key Key of $this->data to be found, pass null for all Key => Val pairs. |
|
| 383 | + * @param boolean $trim Defaults to true, trims all $this->data values. |
|
| 384 | + * @return string|array Array of post or data values if null is passed as key, string if only one key is desired. |
|
| 385 | + */ |
|
| 386 | + public function post($key = null, $trim = true) { |
|
| 387 | + $returnValue = null; |
|
| 388 | + if (is_null($key)) { |
|
| 389 | + $returnValue = array(); |
|
| 390 | + foreach ($this->getData() as $key => $val) { |
|
| 391 | + $returnValue[$key] = $this->post($key, $trim); |
|
| 392 | + } |
|
| 393 | + } else { |
|
| 394 | + $returnValue = (array_key_exists($key, $this->getData())) ? (($trim) ? trim($this->data[$key]) : $this->data[$key]) : null; |
|
| 395 | + } |
|
| 396 | + return $returnValue; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Gets all errors from errorSet and displays them, can be echo out from the |
|
| 401 | + * function or just returned. |
|
| 402 | + * |
|
| 403 | + * @param boolean $limit number of error to display or return |
|
| 404 | + * @param boolean $echo Whether or not the values are to be returned or displayed |
|
| 405 | + * |
|
| 406 | + * @return string Errors formatted for output |
|
| 407 | + */ |
|
| 408 | + public function displayErrors($limit = null, $echo = true) { |
|
| 409 | + list($errorsStart, $errorsEnd) = $this->_allErrorsDelimiter; |
|
| 410 | + list($errorStart, $errorEnd) = $this->_eachErrorDelimiter; |
|
| 411 | + $errorOutput = $errorsStart; |
|
| 412 | + $i = 0; |
|
| 413 | + if (!empty($this->_errors)) { |
|
| 414 | + foreach ($this->_errors as $fieldName => $error) { |
|
| 415 | + if ($i === $limit) { |
|
| 416 | + break; |
|
| 417 | + } |
|
| 418 | + $errorOutput .= $errorStart; |
|
| 419 | + $errorOutput .= $error; |
|
| 420 | + $errorOutput .= $errorEnd; |
|
| 421 | + $i++; |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + $errorOutput .= $errorsEnd; |
|
| 425 | + echo ($echo) ? $errorOutput : ''; |
|
| 426 | + return (! $echo) ? $errorOutput : null; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Returns raw array of errors in no format instead of displaying them |
|
| 431 | + * formatted. |
|
| 432 | + * |
|
| 433 | + * @return array |
|
| 434 | + */ |
|
| 435 | + public function returnErrors() { |
|
| 436 | + return $this->_errors; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Breaks up a PIPE seperated string of rules, and puts them into an array. |
|
| 441 | + * |
|
| 442 | + * @param string $ruleString String to be parsed. |
|
| 443 | + * |
|
| 444 | + * @return array Array of each value in original string. |
|
| 445 | + */ |
|
| 446 | + protected function _parseRuleString($ruleString) { |
|
| 447 | + $ruleSets = array(); |
|
| 448 | + /* |
|
| 449 | 449 | //////////////// hack for regex rule that can contain "|" |
| 450 | 450 | */ |
| 451 | - if(strpos($ruleString, 'regex') !== false){ |
|
| 452 | - $regexRule = array(); |
|
| 453 | - $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 454 | - preg_match($rule, $ruleString, $regexRule); |
|
| 455 | - $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 456 | - if(!empty($regexRule[0])){ |
|
| 457 | - $ruleSets[] = $regexRule[0]; |
|
| 458 | - } |
|
| 459 | - $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 460 | - foreach ($ruleStringRegex as $rule) { |
|
| 461 | - $rule = trim($rule); |
|
| 462 | - if($rule){ |
|
| 463 | - $ruleSets[] = $rule; |
|
| 464 | - } |
|
| 465 | - } |
|
| 451 | + if(strpos($ruleString, 'regex') !== false){ |
|
| 452 | + $regexRule = array(); |
|
| 453 | + $rule = '#regex\[\/(.*)\/([a-zA-Z0-9]?)\]#'; |
|
| 454 | + preg_match($rule, $ruleString, $regexRule); |
|
| 455 | + $ruleStringTemp = preg_replace($rule, '', $ruleString); |
|
| 456 | + if(!empty($regexRule[0])){ |
|
| 457 | + $ruleSets[] = $regexRule[0]; |
|
| 458 | + } |
|
| 459 | + $ruleStringRegex = explode('|', $ruleStringTemp); |
|
| 460 | + foreach ($ruleStringRegex as $rule) { |
|
| 461 | + $rule = trim($rule); |
|
| 462 | + if($rule){ |
|
| 463 | + $ruleSets[] = $rule; |
|
| 464 | + } |
|
| 465 | + } |
|
| 466 | 466 | |
| 467 | - } |
|
| 468 | - /***********************************/ |
|
| 469 | - else{ |
|
| 470 | - if (strpos($ruleString, '|') !== FALSE) { |
|
| 471 | - $ruleSets = explode('|', $ruleString); |
|
| 472 | - } else { |
|
| 473 | - $ruleSets[] = $ruleString; |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - return $ruleSets; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Returns whether or not a field obtains the rule "required". |
|
| 481 | - * |
|
| 482 | - * @param string $fieldName Field to check if required. |
|
| 483 | - * |
|
| 484 | - * @return boolean Whether or not the field is required. |
|
| 485 | - */ |
|
| 486 | - protected function _fieldIsRequired($fieldName) { |
|
| 487 | - $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 488 | - return (in_array('required', $rules)); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 493 | - * and adds an error to the errorSet if it fails validation of the rule. |
|
| 494 | - * |
|
| 495 | - * @param string $inputName Name or key of the validation data |
|
| 496 | - * @param string $inputVal Value of the validation data |
|
| 497 | - * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 498 | - * @return void |
|
| 499 | - */ |
|
| 500 | - protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 501 | - $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 502 | - // Array to store args |
|
| 503 | - $ruleArgs = array(); |
|
| 504 | - |
|
| 505 | - preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 506 | - |
|
| 507 | - // Get the rule arguments, realRule is just the base rule name |
|
| 508 | - // Like min_length instead of min_length[3] |
|
| 509 | - $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 467 | + } |
|
| 468 | + /***********************************/ |
|
| 469 | + else{ |
|
| 470 | + if (strpos($ruleString, '|') !== FALSE) { |
|
| 471 | + $ruleSets = explode('|', $ruleString); |
|
| 472 | + } else { |
|
| 473 | + $ruleSets[] = $ruleString; |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + return $ruleSets; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Returns whether or not a field obtains the rule "required". |
|
| 481 | + * |
|
| 482 | + * @param string $fieldName Field to check if required. |
|
| 483 | + * |
|
| 484 | + * @return boolean Whether or not the field is required. |
|
| 485 | + */ |
|
| 486 | + protected function _fieldIsRequired($fieldName) { |
|
| 487 | + $rules = $this->_parseRuleString($this->_rules[$fieldName]); |
|
| 488 | + return (in_array('required', $rules)); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Takes a data input name, it's value, and the rule it's being validated against (ex: max_length[16]) |
|
| 493 | + * and adds an error to the errorSet if it fails validation of the rule. |
|
| 494 | + * |
|
| 495 | + * @param string $inputName Name or key of the validation data |
|
| 496 | + * @param string $inputVal Value of the validation data |
|
| 497 | + * @param string $ruleName Rule to be validated against, including args (exact_length[5]) |
|
| 498 | + * @return void |
|
| 499 | + */ |
|
| 500 | + protected function _validateRule($inputName, $inputVal, $ruleName) { |
|
| 501 | + $this->logger->debug('Rule validation of field [' .$inputName. '], value [' .$inputVal. '], rule [' .$ruleName. ']'); |
|
| 502 | + // Array to store args |
|
| 503 | + $ruleArgs = array(); |
|
| 504 | + |
|
| 505 | + preg_match('/\[(.*)\]/', $ruleName, $ruleArgs); |
|
| 506 | + |
|
| 507 | + // Get the rule arguments, realRule is just the base rule name |
|
| 508 | + // Like min_length instead of min_length[3] |
|
| 509 | + $ruleName = preg_replace('/\[(.*)\]/', '', $ruleName); |
|
| 510 | 510 | |
| 511 | - if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 512 | - $methodToCall = $this->_toCallCase($ruleName); |
|
| 513 | - call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 514 | - } |
|
| 515 | - return; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - /** |
|
| 519 | - * Set error for the given field or key |
|
| 520 | - * |
|
| 521 | - * @param string $inputName the input or key name |
|
| 522 | - * @param string $ruleName the rule name |
|
| 523 | - * @param array|string $replacements |
|
| 524 | - */ |
|
| 525 | - protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 526 | - $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 527 | - $rulePhrase = null; |
|
| 528 | - foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 529 | - if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 530 | - $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 531 | - } else { |
|
| 532 | - $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 533 | - } |
|
| 534 | - } |
|
| 535 | - // Any overrides? |
|
| 536 | - if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 537 | - $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 538 | - } |
|
| 539 | - // Type cast to array in case it's a string |
|
| 540 | - $replacements = (array) $replacements; |
|
| 511 | + if (method_exists($this, $this->_toCallCase($ruleName))) { |
|
| 512 | + $methodToCall = $this->_toCallCase($ruleName); |
|
| 513 | + call_user_func(array($this, $methodToCall), $inputName, $ruleName, $ruleArgs); |
|
| 514 | + } |
|
| 515 | + return; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + /** |
|
| 519 | + * Set error for the given field or key |
|
| 520 | + * |
|
| 521 | + * @param string $inputName the input or key name |
|
| 522 | + * @param string $ruleName the rule name |
|
| 523 | + * @param array|string $replacements |
|
| 524 | + */ |
|
| 525 | + protected function _setError($inputName, $ruleName, $replacements = array()) { |
|
| 526 | + $rulePhraseKeyParts = explode(',', $ruleName); |
|
| 527 | + $rulePhrase = null; |
|
| 528 | + foreach ($rulePhraseKeyParts as $rulePhraseKeyPart) { |
|
| 529 | + if (array_key_exists($rulePhraseKeyPart, $this->_errorsMessages)) { |
|
| 530 | + $rulePhrase = $this->_errorsMessages[$rulePhraseKeyPart]; |
|
| 531 | + } else { |
|
| 532 | + $rulePhrase = $rulePhrase[$rulePhraseKeyPart]; |
|
| 533 | + } |
|
| 534 | + } |
|
| 535 | + // Any overrides? |
|
| 536 | + if (array_key_exists($inputName, $this->_errorPhraseOverrides) && array_key_exists($ruleName, $this->_errorPhraseOverrides[$inputName])) { |
|
| 537 | + $rulePhrase = $this->_errorPhraseOverrides[$inputName][$ruleName]; |
|
| 538 | + } |
|
| 539 | + // Type cast to array in case it's a string |
|
| 540 | + $replacements = (array) $replacements; |
|
| 541 | 541 | $replacementCount = count($replacements); |
| 542 | - for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 543 | - $key = $i - 1; |
|
| 544 | - $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 545 | - } |
|
| 546 | - if (! array_key_exists($inputName, $this->_errors)) { |
|
| 547 | - $this->_errors[$inputName] = $rulePhrase; |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Used to run a callback for the callback rule, as well as pass in a default |
|
| 553 | - * argument of the post value. For example the username field having a rule: |
|
| 554 | - * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 555 | - * of eval over call_user_func is in case the function is not user defined. |
|
| 556 | - * |
|
| 557 | - * @param type $inputArg |
|
| 558 | - * @param string $callbackFunc |
|
| 559 | - * |
|
| 560 | - * @return mixed |
|
| 561 | - */ |
|
| 562 | - protected function _runCallback($inputArg, $callbackFunc) { |
|
| 542 | + for ($i = 1; $i <= $replacementCount; $i++) { |
|
| 543 | + $key = $i - 1; |
|
| 544 | + $rulePhrase = str_replace('%' . $i, $replacements[$key], $rulePhrase); |
|
| 545 | + } |
|
| 546 | + if (! array_key_exists($inputName, $this->_errors)) { |
|
| 547 | + $this->_errors[$inputName] = $rulePhrase; |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Used to run a callback for the callback rule, as well as pass in a default |
|
| 553 | + * argument of the post value. For example the username field having a rule: |
|
| 554 | + * callback[userExists] will eval userExists(data[username]) - Note the use |
|
| 555 | + * of eval over call_user_func is in case the function is not user defined. |
|
| 556 | + * |
|
| 557 | + * @param type $inputArg |
|
| 558 | + * @param string $callbackFunc |
|
| 559 | + * |
|
| 560 | + * @return mixed |
|
| 561 | + */ |
|
| 562 | + protected function _runCallback($inputArg, $callbackFunc) { |
|
| 563 | 563 | return eval('return ' . $callbackFunc . '("' . $inputArg . '");'); |
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Used for applying a rule only if the empty callback evaluates to true, |
|
| 568 | - * for example required[funcName] - This runs funcName without passing any |
|
| 569 | - * arguments. |
|
| 570 | - * |
|
| 571 | - * @param string $callbackFunc |
|
| 572 | - * |
|
| 573 | - * @return mixed |
|
| 574 | - */ |
|
| 575 | - protected function _runEmptyCallback($callbackFunc) { |
|
| 576 | - return eval('return ' . $callbackFunc . '();'); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * Gets a specific label of a specific field input name. |
|
| 581 | - * |
|
| 582 | - * @param string $inputName |
|
| 583 | - * |
|
| 584 | - * @return string |
|
| 585 | - */ |
|
| 586 | - protected function _getLabel($inputName) { |
|
| 587 | - return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 588 | - } |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Used for applying a rule only if the empty callback evaluates to true, |
|
| 568 | + * for example required[funcName] - This runs funcName without passing any |
|
| 569 | + * arguments. |
|
| 570 | + * |
|
| 571 | + * @param string $callbackFunc |
|
| 572 | + * |
|
| 573 | + * @return mixed |
|
| 574 | + */ |
|
| 575 | + protected function _runEmptyCallback($callbackFunc) { |
|
| 576 | + return eval('return ' . $callbackFunc . '();'); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + /** |
|
| 580 | + * Gets a specific label of a specific field input name. |
|
| 581 | + * |
|
| 582 | + * @param string $inputName |
|
| 583 | + * |
|
| 584 | + * @return string |
|
| 585 | + */ |
|
| 586 | + protected function _getLabel($inputName) { |
|
| 587 | + return (array_key_exists($inputName, $this->_labels)) ? $this->_labels[$inputName] : $inputName; |
|
| 588 | + } |
|
| 589 | 589 | |
| 590 | - /** |
|
| 591 | - * Peform validation for the rule "required" |
|
| 592 | - * @param string $inputName the form field or data key name used |
|
| 593 | - * @param string $ruleName the rule name for this validation ("required") |
|
| 594 | - * @param array $ruleArgs the rules argument |
|
| 595 | - */ |
|
| 590 | + /** |
|
| 591 | + * Peform validation for the rule "required" |
|
| 592 | + * @param string $inputName the form field or data key name used |
|
| 593 | + * @param string $ruleName the rule name for this validation ("required") |
|
| 594 | + * @param array $ruleArgs the rules argument |
|
| 595 | + */ |
|
| 596 | 596 | protected function _validateRequired($inputName, $ruleName, array $ruleArgs) { |
| 597 | - $inputVal = $this->post($inputName); |
|
| 598 | - if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 599 | - $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 600 | - if ($inputVal == '' && $callbackReturn == true) { |
|
| 601 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 602 | - } |
|
| 603 | - } |
|
| 597 | + $inputVal = $this->post($inputName); |
|
| 598 | + if(array_key_exists(1, $ruleArgs) && function_exists($ruleArgs[1])) { |
|
| 599 | + $callbackReturn = $this->_runEmptyCallback($ruleArgs[1]); |
|
| 600 | + if ($inputVal == '' && $callbackReturn == true) { |
|
| 601 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | 604 | else if($inputVal == '') { |
| 605 | 605 | $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
| 606 | - } |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * Perform validation for the honey pot so means for the validation to be failed |
|
| 611 | - * @param string $inputName the form field or data key name used |
|
| 612 | - * @param string $ruleName the rule name for this validation |
|
| 613 | - * @param array $ruleArgs the rules argument |
|
| 614 | - */ |
|
| 615 | - protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 616 | - if ($this->data[$inputName] != '') { |
|
| 617 | - $this->_forceFail = true; |
|
| 618 | - } |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - /** |
|
| 622 | - * Peform validation for the rule "callback" |
|
| 623 | - * @param string $inputName the form field or data key name used |
|
| 624 | - * @param string $ruleName the rule name for this validation ("callback") |
|
| 625 | - * @param array $ruleArgs the rules argument |
|
| 626 | - */ |
|
| 627 | - protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 628 | - if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 606 | + } |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * Perform validation for the honey pot so means for the validation to be failed |
|
| 611 | + * @param string $inputName the form field or data key name used |
|
| 612 | + * @param string $ruleName the rule name for this validation |
|
| 613 | + * @param array $ruleArgs the rules argument |
|
| 614 | + */ |
|
| 615 | + protected function _validateHoneypot($inputName, $ruleName, array $ruleArgs) { |
|
| 616 | + if ($this->data[$inputName] != '') { |
|
| 617 | + $this->_forceFail = true; |
|
| 618 | + } |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + /** |
|
| 622 | + * Peform validation for the rule "callback" |
|
| 623 | + * @param string $inputName the form field or data key name used |
|
| 624 | + * @param string $ruleName the rule name for this validation ("callback") |
|
| 625 | + * @param array $ruleArgs the rules argument |
|
| 626 | + */ |
|
| 627 | + protected function _validateCallback($inputName, $ruleName, array $ruleArgs) { |
|
| 628 | + if (function_exists($ruleArgs[1]) && !empty($this->data[$inputName])) { |
|
| 629 | 629 | $result = $this->_runCallback($this->data[$inputName], $ruleArgs[1]); |
| 630 | 630 | if(! $result){ |
| 631 | 631 | $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
| 632 | 632 | } |
| 633 | - } |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * Peform validation for the rule "depends" |
|
| 638 | - * @param string $inputName the form field or data key name used |
|
| 639 | - * @param string $ruleName the rule name for this validation ("depends") |
|
| 640 | - * @param array $ruleArgs the rules argument |
|
| 641 | - */ |
|
| 642 | - protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 643 | - if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 644 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 645 | - } |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * Peform validation for the rule "not_equal" |
|
| 650 | - * @param string $inputName the form field or data key name used |
|
| 651 | - * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 652 | - * @param array $ruleArgs the rules argument |
|
| 653 | - */ |
|
| 654 | - protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 655 | - $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 656 | - foreach ($canNotEqual as $doNotEqual) { |
|
| 657 | - $inputVal = $this->post($inputName); |
|
| 658 | - if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 659 | - if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 660 | - $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 661 | - continue; |
|
| 662 | - } |
|
| 663 | - } |
|
| 633 | + } |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * Peform validation for the rule "depends" |
|
| 638 | + * @param string $inputName the form field or data key name used |
|
| 639 | + * @param string $ruleName the rule name for this validation ("depends") |
|
| 640 | + * @param array $ruleArgs the rules argument |
|
| 641 | + */ |
|
| 642 | + protected function _validateDepends($inputName, $ruleName, array $ruleArgs) { |
|
| 643 | + if (array_key_exists($ruleArgs[1], $this->_errors)) { |
|
| 644 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 645 | + } |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * Peform validation for the rule "not_equal" |
|
| 650 | + * @param string $inputName the form field or data key name used |
|
| 651 | + * @param string $ruleName the rule name for this validation ("not_equal") |
|
| 652 | + * @param array $ruleArgs the rules argument |
|
| 653 | + */ |
|
| 654 | + protected function _validateNotEqual($inputName, $ruleName, array $ruleArgs) { |
|
| 655 | + $canNotEqual = explode(',', $ruleArgs[1]); |
|
| 656 | + foreach ($canNotEqual as $doNotEqual) { |
|
| 657 | + $inputVal = $this->post($inputName); |
|
| 658 | + if (preg_match('/post:(.*)/', $doNotEqual)) { |
|
| 659 | + if ($inputVal == $this->data[str_replace('post:', '', $doNotEqual)]) { |
|
| 660 | + $this->_setError($inputName, $ruleName . ',post:key', array($this->_getLabel($inputName), $this->_getLabel(str_replace('post:', '', $doNotEqual)))); |
|
| 661 | + continue; |
|
| 662 | + } |
|
| 663 | + } |
|
| 664 | 664 | else{ |
| 665 | - if ($inputVal == $doNotEqual) { |
|
| 666 | - $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 667 | - continue; |
|
| 668 | - } |
|
| 669 | - } |
|
| 670 | - } |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Peform validation for the rule "matches" |
|
| 675 | - * @param string $inputName the form field or data key name used |
|
| 676 | - * @param string $ruleName the rule name for this validation ("matches") |
|
| 677 | - * @param array $ruleArgs the rules argument |
|
| 678 | - */ |
|
| 679 | - protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 680 | - $inputVal = $this->post($inputName); |
|
| 681 | - if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 682 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 683 | - } |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - /** |
|
| 687 | - * Peform validation for the rule "valid_email" |
|
| 688 | - * @param string $inputName the form field or data key name used |
|
| 689 | - * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 690 | - * @param array $ruleArgs the rules argument |
|
| 691 | - */ |
|
| 692 | - protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 693 | - $inputVal = $this->post($inputName); |
|
| 694 | - 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)) { |
|
| 695 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 696 | - return; |
|
| 697 | - } |
|
| 698 | - $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 699 | - } |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * Peform validation for the rule "exact_length" |
|
| 704 | - * @param string $inputName the form field or data key name used |
|
| 705 | - * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 706 | - * @param array $ruleArgs the rules argument |
|
| 707 | - */ |
|
| 708 | - protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 709 | - $inputVal = $this->post($inputName); |
|
| 710 | - if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 711 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 712 | - return; |
|
| 713 | - } |
|
| 714 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 715 | - } |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * Peform validation for the rule "max_length" |
|
| 720 | - * @param string $inputName the form field or data key name used |
|
| 721 | - * @param string $ruleName the rule name for this validation ("max_length") |
|
| 722 | - * @param array $ruleArgs the rules argument |
|
| 723 | - */ |
|
| 724 | - protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 725 | - $inputVal = $this->post($inputName); |
|
| 726 | - if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 727 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 728 | - return; |
|
| 729 | - } |
|
| 730 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 731 | - } |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * Peform validation for the rule "min_length" |
|
| 736 | - * @param string $inputName the form field or data key name used |
|
| 737 | - * @param string $ruleName the rule name for this validation ("min_length") |
|
| 738 | - * @param array $ruleArgs the rules argument |
|
| 739 | - */ |
|
| 740 | - protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 741 | - $inputVal = $this->post($inputName); |
|
| 742 | - if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 743 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 744 | - return; |
|
| 745 | - } |
|
| 746 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 747 | - } |
|
| 748 | - } |
|
| 665 | + if ($inputVal == $doNotEqual) { |
|
| 666 | + $this->_setError($inputName, $ruleName . ',string', array($this->_getLabel($inputName), $doNotEqual)); |
|
| 667 | + continue; |
|
| 668 | + } |
|
| 669 | + } |
|
| 670 | + } |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * Peform validation for the rule "matches" |
|
| 675 | + * @param string $inputName the form field or data key name used |
|
| 676 | + * @param string $ruleName the rule name for this validation ("matches") |
|
| 677 | + * @param array $ruleArgs the rules argument |
|
| 678 | + */ |
|
| 679 | + protected function _validateMatches($inputName, $ruleName, array $ruleArgs) { |
|
| 680 | + $inputVal = $this->post($inputName); |
|
| 681 | + if ($inputVal != $this->data[$ruleArgs[1]]) { |
|
| 682 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 683 | + } |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + /** |
|
| 687 | + * Peform validation for the rule "valid_email" |
|
| 688 | + * @param string $inputName the form field or data key name used |
|
| 689 | + * @param string $ruleName the rule name for this validation ("valid_email") |
|
| 690 | + * @param array $ruleArgs the rules argument |
|
| 691 | + */ |
|
| 692 | + protected function _validateValidEmail($inputName, $ruleName, array $ruleArgs) { |
|
| 693 | + $inputVal = $this->post($inputName); |
|
| 694 | + 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)) { |
|
| 695 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 696 | + return; |
|
| 697 | + } |
|
| 698 | + $this->_setError($inputName, $ruleName, $this->_getLabel($inputName)); |
|
| 699 | + } |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * Peform validation for the rule "exact_length" |
|
| 704 | + * @param string $inputName the form field or data key name used |
|
| 705 | + * @param string $ruleName the rule name for this validation ("exact_length") |
|
| 706 | + * @param array $ruleArgs the rules argument |
|
| 707 | + */ |
|
| 708 | + protected function _validateExactLength($inputName, $ruleName, array $ruleArgs) { |
|
| 709 | + $inputVal = $this->post($inputName); |
|
| 710 | + if (strlen($inputVal) != $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 711 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 712 | + return; |
|
| 713 | + } |
|
| 714 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 715 | + } |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * Peform validation for the rule "max_length" |
|
| 720 | + * @param string $inputName the form field or data key name used |
|
| 721 | + * @param string $ruleName the rule name for this validation ("max_length") |
|
| 722 | + * @param array $ruleArgs the rules argument |
|
| 723 | + */ |
|
| 724 | + protected function _validateMaxLength($inputName, $ruleName, array $ruleArgs) { |
|
| 725 | + $inputVal = $this->post($inputName); |
|
| 726 | + if (strlen($inputVal) > $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 727 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 728 | + return; |
|
| 729 | + } |
|
| 730 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 731 | + } |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * Peform validation for the rule "min_length" |
|
| 736 | + * @param string $inputName the form field or data key name used |
|
| 737 | + * @param string $ruleName the rule name for this validation ("min_length") |
|
| 738 | + * @param array $ruleArgs the rules argument |
|
| 739 | + */ |
|
| 740 | + protected function _validateMinLength($inputName, $ruleName, array $ruleArgs) { |
|
| 741 | + $inputVal = $this->post($inputName); |
|
| 742 | + if (strlen($inputVal) < $ruleArgs[1]) { // $ruleArgs[0] is [length] $rulesArgs[1] is just length |
|
| 743 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 744 | + return; |
|
| 745 | + } |
|
| 746 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | 749 | |
| 750 | - /** |
|
| 751 | - * Peform validation for the rule "less_than" |
|
| 752 | - * @param string $inputName the form field or data key name used |
|
| 753 | - * @param string $ruleName the rule name for this validation ("less_than") |
|
| 754 | - * @param array $ruleArgs the rules argument |
|
| 755 | - */ |
|
| 756 | - protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 757 | - $inputVal = $this->post($inputName); |
|
| 758 | - if ($inputVal >= $ruleArgs[1]) { |
|
| 759 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 760 | - return; |
|
| 761 | - } |
|
| 762 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 763 | - } |
|
| 764 | - } |
|
| 750 | + /** |
|
| 751 | + * Peform validation for the rule "less_than" |
|
| 752 | + * @param string $inputName the form field or data key name used |
|
| 753 | + * @param string $ruleName the rule name for this validation ("less_than") |
|
| 754 | + * @param array $ruleArgs the rules argument |
|
| 755 | + */ |
|
| 756 | + protected function _validateLessThan($inputName, $ruleName, array $ruleArgs) { |
|
| 757 | + $inputVal = $this->post($inputName); |
|
| 758 | + if ($inputVal >= $ruleArgs[1]) { |
|
| 759 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 760 | + return; |
|
| 761 | + } |
|
| 762 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 763 | + } |
|
| 764 | + } |
|
| 765 | 765 | |
| 766 | - /** |
|
| 767 | - * Peform validation for the rule "greater_than" |
|
| 768 | - * @param string $inputName the form field or data key name used |
|
| 769 | - * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 770 | - * @param array $ruleArgs the rules argument |
|
| 771 | - */ |
|
| 772 | - protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 773 | - $inputVal = $this->post($inputName); |
|
| 774 | - if ($inputVal <= $ruleArgs[1]) { |
|
| 775 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 776 | - return; |
|
| 777 | - } |
|
| 778 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 779 | - } |
|
| 780 | - } |
|
| 766 | + /** |
|
| 767 | + * Peform validation for the rule "greater_than" |
|
| 768 | + * @param string $inputName the form field or data key name used |
|
| 769 | + * @param string $ruleName the rule name for this validation ("greater_than") |
|
| 770 | + * @param array $ruleArgs the rules argument |
|
| 771 | + */ |
|
| 772 | + protected function _validateGreaterThan($inputName, $ruleName, array $ruleArgs) { |
|
| 773 | + $inputVal = $this->post($inputName); |
|
| 774 | + if ($inputVal <= $ruleArgs[1]) { |
|
| 775 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 776 | + return; |
|
| 777 | + } |
|
| 778 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 779 | + } |
|
| 780 | + } |
|
| 781 | 781 | |
| 782 | - /** |
|
| 783 | - * Peform validation for the rule "numeric" |
|
| 784 | - * @param string $inputName the form field or data key name used |
|
| 785 | - * @param string $ruleName the rule name for this validation ("numeric") |
|
| 786 | - * @param array $ruleArgs the rules argument |
|
| 787 | - */ |
|
| 788 | - protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 789 | - $inputVal = $this->post($inputName); |
|
| 790 | - if (! is_numeric($inputVal)) { |
|
| 791 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 792 | - return; |
|
| 793 | - } |
|
| 794 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 795 | - } |
|
| 796 | - } |
|
| 782 | + /** |
|
| 783 | + * Peform validation for the rule "numeric" |
|
| 784 | + * @param string $inputName the form field or data key name used |
|
| 785 | + * @param string $ruleName the rule name for this validation ("numeric") |
|
| 786 | + * @param array $ruleArgs the rules argument |
|
| 787 | + */ |
|
| 788 | + protected function _validateNumeric($inputName, $ruleName, array $ruleArgs) { |
|
| 789 | + $inputVal = $this->post($inputName); |
|
| 790 | + if (! is_numeric($inputVal)) { |
|
| 791 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 792 | + return; |
|
| 793 | + } |
|
| 794 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 795 | + } |
|
| 796 | + } |
|
| 797 | 797 | |
| 798 | - /** |
|
| 799 | - * Peform validation for the rule "exists" |
|
| 800 | - * @param string $inputName the form field or data key name used |
|
| 801 | - * @param string $ruleName the rule name for this validation ("exists") |
|
| 802 | - * @param array $ruleArgs the rules argument |
|
| 803 | - */ |
|
| 798 | + /** |
|
| 799 | + * Peform validation for the rule "exists" |
|
| 800 | + * @param string $inputName the form field or data key name used |
|
| 801 | + * @param string $ruleName the rule name for this validation ("exists") |
|
| 802 | + * @param array $ruleArgs the rules argument |
|
| 803 | + */ |
|
| 804 | 804 | protected function _validateExists($inputName, $ruleName, array $ruleArgs) { |
| 805 | - $inputVal = $this->post($inputName); |
|
| 806 | - $obj = & get_instance(); |
|
| 807 | - if(! isset($obj->database)){ |
|
| 808 | - return; |
|
| 809 | - } |
|
| 810 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 811 | - $obj->database->from($table) |
|
| 812 | - ->where($column, $inputVal) |
|
| 813 | - ->get(); |
|
| 814 | - $nb = $obj->database->numRows(); |
|
| 815 | - if ($nb == 0) { |
|
| 816 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 817 | - return; |
|
| 818 | - } |
|
| 819 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 820 | - } |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * Peform validation for the rule "is_unique" |
|
| 825 | - * @param string $inputName the form field or data key name used |
|
| 826 | - * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 827 | - * @param array $ruleArgs the rules argument |
|
| 828 | - */ |
|
| 829 | - protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 830 | - $inputVal = $this->post($inputName); |
|
| 831 | - $obj = & get_instance(); |
|
| 832 | - if(! isset($obj->database)){ |
|
| 833 | - return; |
|
| 834 | - } |
|
| 835 | - list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 836 | - $obj->database->from($table) |
|
| 837 | - ->where($column, $inputVal) |
|
| 838 | - ->get(); |
|
| 839 | - $nb = $obj->database->numRows(); |
|
| 840 | - if ($nb != 0) { |
|
| 841 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 842 | - return; |
|
| 843 | - } |
|
| 844 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 845 | - } |
|
| 846 | - } |
|
| 805 | + $inputVal = $this->post($inputName); |
|
| 806 | + $obj = & get_instance(); |
|
| 807 | + if(! isset($obj->database)){ |
|
| 808 | + return; |
|
| 809 | + } |
|
| 810 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 811 | + $obj->database->from($table) |
|
| 812 | + ->where($column, $inputVal) |
|
| 813 | + ->get(); |
|
| 814 | + $nb = $obj->database->numRows(); |
|
| 815 | + if ($nb == 0) { |
|
| 816 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 817 | + return; |
|
| 818 | + } |
|
| 819 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 820 | + } |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * Peform validation for the rule "is_unique" |
|
| 825 | + * @param string $inputName the form field or data key name used |
|
| 826 | + * @param string $ruleName the rule name for this validation ("is_unique") |
|
| 827 | + * @param array $ruleArgs the rules argument |
|
| 828 | + */ |
|
| 829 | + protected function _validateIsUnique($inputName, $ruleName, array $ruleArgs) { |
|
| 830 | + $inputVal = $this->post($inputName); |
|
| 831 | + $obj = & get_instance(); |
|
| 832 | + if(! isset($obj->database)){ |
|
| 833 | + return; |
|
| 834 | + } |
|
| 835 | + list($table, $column) = explode('.', $ruleArgs[1]); |
|
| 836 | + $obj->database->from($table) |
|
| 837 | + ->where($column, $inputVal) |
|
| 838 | + ->get(); |
|
| 839 | + $nb = $obj->database->numRows(); |
|
| 840 | + if ($nb != 0) { |
|
| 841 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 842 | + return; |
|
| 843 | + } |
|
| 844 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | 847 | |
| 848 | - /** |
|
| 849 | - * Peform validation for the rule "is_unique_update" |
|
| 850 | - * @param string $inputName the form field or data key name used |
|
| 851 | - * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 852 | - * @param array $ruleArgs the rules argument |
|
| 853 | - */ |
|
| 854 | - protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 855 | - $inputVal = $this->post($inputName); |
|
| 856 | - $obj = & get_instance(); |
|
| 857 | - if(! isset($obj->database)){ |
|
| 858 | - return; |
|
| 859 | - } |
|
| 860 | - $data = explode(',', $ruleArgs[1]); |
|
| 861 | - if(count($data) < 2){ |
|
| 862 | - return; |
|
| 863 | - } |
|
| 864 | - list($table, $column) = explode('.', $data[0]); |
|
| 865 | - list($field, $val) = explode('=', $data[1]); |
|
| 866 | - $obj->database->from($table) |
|
| 867 | - ->where($column, $inputVal) |
|
| 868 | - ->where($field, '!=', trim($val)) |
|
| 869 | - ->get(); |
|
| 870 | - $nb = $obj->database->numRows(); |
|
| 871 | - if ($nb != 0) { |
|
| 872 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 873 | - return; |
|
| 874 | - } |
|
| 875 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 876 | - } |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - /** |
|
| 880 | - * Peform validation for the rule "in_list" |
|
| 881 | - * @param string $inputName the form field or data key name used |
|
| 882 | - * @param string $ruleName the rule name for this validation ("in_list") |
|
| 883 | - * @param array $ruleArgs the rules argument |
|
| 884 | - */ |
|
| 885 | - protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 886 | - $inputVal = $this->post($inputName); |
|
| 887 | - $list = explode(',', $ruleArgs[1]); |
|
| 888 | - $list = array_map('trim', $list); |
|
| 889 | - if (! in_array($inputVal, $list)) { |
|
| 890 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 891 | - return; |
|
| 892 | - } |
|
| 893 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 894 | - } |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - /** |
|
| 898 | - * Peform validation for the rule "regex" |
|
| 899 | - * @param string $inputName the form field or data key name used |
|
| 900 | - * @param string $ruleName the rule name for this validation ("regex") |
|
| 901 | - * @param array $ruleArgs the rules argument |
|
| 902 | - */ |
|
| 903 | - protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 904 | - $inputVal = $this->post($inputName); |
|
| 905 | - $regex = $ruleArgs[1]; |
|
| 906 | - if (! preg_match($regex, $inputVal)) { |
|
| 907 | - if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 908 | - return; |
|
| 909 | - } |
|
| 910 | - $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 911 | - } |
|
| 912 | - } |
|
| 848 | + /** |
|
| 849 | + * Peform validation for the rule "is_unique_update" |
|
| 850 | + * @param string $inputName the form field or data key name used |
|
| 851 | + * @param string $ruleName the rule name for this validation ("is_unique_update") |
|
| 852 | + * @param array $ruleArgs the rules argument |
|
| 853 | + */ |
|
| 854 | + protected function _validateIsUniqueUpdate($inputName, $ruleName, array $ruleArgs) { |
|
| 855 | + $inputVal = $this->post($inputName); |
|
| 856 | + $obj = & get_instance(); |
|
| 857 | + if(! isset($obj->database)){ |
|
| 858 | + return; |
|
| 859 | + } |
|
| 860 | + $data = explode(',', $ruleArgs[1]); |
|
| 861 | + if(count($data) < 2){ |
|
| 862 | + return; |
|
| 863 | + } |
|
| 864 | + list($table, $column) = explode('.', $data[0]); |
|
| 865 | + list($field, $val) = explode('=', $data[1]); |
|
| 866 | + $obj->database->from($table) |
|
| 867 | + ->where($column, $inputVal) |
|
| 868 | + ->where($field, '!=', trim($val)) |
|
| 869 | + ->get(); |
|
| 870 | + $nb = $obj->database->numRows(); |
|
| 871 | + if ($nb != 0) { |
|
| 872 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 873 | + return; |
|
| 874 | + } |
|
| 875 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 876 | + } |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + /** |
|
| 880 | + * Peform validation for the rule "in_list" |
|
| 881 | + * @param string $inputName the form field or data key name used |
|
| 882 | + * @param string $ruleName the rule name for this validation ("in_list") |
|
| 883 | + * @param array $ruleArgs the rules argument |
|
| 884 | + */ |
|
| 885 | + protected function _validateInList($inputName, $ruleName, array $ruleArgs) { |
|
| 886 | + $inputVal = $this->post($inputName); |
|
| 887 | + $list = explode(',', $ruleArgs[1]); |
|
| 888 | + $list = array_map('trim', $list); |
|
| 889 | + if (! in_array($inputVal, $list)) { |
|
| 890 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 891 | + return; |
|
| 892 | + } |
|
| 893 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName), $this->_getLabel($ruleArgs[1]))); |
|
| 894 | + } |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + /** |
|
| 898 | + * Peform validation for the rule "regex" |
|
| 899 | + * @param string $inputName the form field or data key name used |
|
| 900 | + * @param string $ruleName the rule name for this validation ("regex") |
|
| 901 | + * @param array $ruleArgs the rules argument |
|
| 902 | + */ |
|
| 903 | + protected function _validateRegex($inputName, $ruleName, array $ruleArgs) { |
|
| 904 | + $inputVal = $this->post($inputName); |
|
| 905 | + $regex = $ruleArgs[1]; |
|
| 906 | + if (! preg_match($regex, $inputVal)) { |
|
| 907 | + if (! $this->_fieldIsRequired($inputName) && empty($this->data[$inputName])) { |
|
| 908 | + return; |
|
| 909 | + } |
|
| 910 | + $this->_setError($inputName, $ruleName, array($this->_getLabel($inputName))); |
|
| 911 | + } |
|
| 912 | + } |
|
| 913 | 913 | |
| 914 | - } |
|
| 914 | + } |
|
@@ -53,14 +53,14 @@ discard block |
||
| 53 | 53 | //put the first letter of class to upper case |
| 54 | 54 | $class = ucfirst($class); |
| 55 | 55 | static $classes = array(); |
| 56 | - if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log'){ |
|
| 56 | + if (isset($classes[$class]) /*hack for duplicate log Logger name*/ && $class != 'Log') { |
|
| 57 | 57 | return $classes[$class]; |
| 58 | 58 | } |
| 59 | 59 | $found = false; |
| 60 | 60 | foreach (array(ROOT_PATH, CORE_PATH) as $path) { |
| 61 | 61 | $file = $path . $dir . '/' . $class . '.php'; |
| 62 | - if (file_exists($file)){ |
|
| 63 | - if (class_exists($class, false) === false){ |
|
| 62 | + if (file_exists($file)) { |
|
| 63 | + if (class_exists($class, false) === false) { |
|
| 64 | 64 | require_once $file; |
| 65 | 65 | } |
| 66 | 66 | //already found |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | break; |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | - if (! $found){ |
|
| 71 | + if (!$found) { |
|
| 72 | 72 | //can't use show_error() at this time because some dependencies not yet loaded |
| 73 | 73 | set_http_status_header(503); |
| 74 | 74 | echo 'Cannot find the class [' . $class . ']'; |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | /* |
| 79 | 79 | TODO use the best method to get the Log instance |
| 80 | 80 | */ |
| 81 | - if ($class == 'Log'){ |
|
| 81 | + if ($class == 'Log') { |
|
| 82 | 82 | //can't use the instruction like "return new Log()" |
| 83 | 83 | //because we need return the reference instance of the loaded class. |
| 84 | 84 | $log = new Log(); |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | */ |
| 103 | 103 | function & class_loaded($class = null){ |
| 104 | 104 | static $list = array(); |
| 105 | - if ($class !== null){ |
|
| 105 | + if ($class !== null) { |
|
| 106 | 106 | $list[strtolower($class)] = $class; |
| 107 | 107 | } |
| 108 | 108 | return $list; |
@@ -117,14 +117,14 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | function & load_configurations(array $overwrite_values = array()){ |
| 119 | 119 | static $config; |
| 120 | - if (empty($config)){ |
|
| 120 | + if (empty($config)) { |
|
| 121 | 121 | $file = CONFIG_PATH . 'config.php'; |
| 122 | 122 | $found = false; |
| 123 | - if (file_exists($file)){ |
|
| 123 | + if (file_exists($file)) { |
|
| 124 | 124 | require_once $file; |
| 125 | 125 | $found = true; |
| 126 | 126 | } |
| 127 | - if (! $found){ |
|
| 127 | + if (!$found) { |
|
| 128 | 128 | set_http_status_header(503); |
| 129 | 129 | echo 'Unable to find the configuration file [' . $file . ']'; |
| 130 | 130 | die(); |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @return mixed the config value |
| 146 | 146 | */ |
| 147 | - function get_config($key, $default = null){ |
|
| 147 | + function get_config($key, $default = null) { |
|
| 148 | 148 | static $cfg; |
| 149 | - if (empty($cfg)){ |
|
| 149 | + if (empty($cfg)) { |
|
| 150 | 150 | $cfg[0] = & load_configurations(); |
| 151 | 151 | } |
| 152 | 152 | return array_key_exists($key, $cfg[0]) ? $cfg[0][$key] : $default; |
@@ -160,9 +160,9 @@ discard block |
||
| 160 | 160 | * |
| 161 | 161 | * @codeCoverageIgnore |
| 162 | 162 | */ |
| 163 | - function save_to_log($level, $message, $logger = null){ |
|
| 164 | - $log =& class_loader('Log', 'classes'); |
|
| 165 | - if ($logger){ |
|
| 163 | + function save_to_log($level, $message, $logger = null) { |
|
| 164 | + $log = & class_loader('Log', 'classes'); |
|
| 165 | + if ($logger) { |
|
| 166 | 166 | $log->setLogger($logger); |
| 167 | 167 | } |
| 168 | 168 | $log->writeLog($message, $level); |
@@ -175,11 +175,11 @@ discard block |
||
| 175 | 175 | * |
| 176 | 176 | * @codeCoverageIgnore |
| 177 | 177 | */ |
| 178 | - function set_http_status_header($code = 200, $text = null){ |
|
| 179 | - if (!is_int($code) || $code < 0 ){ |
|
| 178 | + function set_http_status_header($code = 200, $text = null) { |
|
| 179 | + if (!is_int($code) || $code < 0) { |
|
| 180 | 180 | show_error('HTTP status code must be an integer'); |
| 181 | 181 | } |
| 182 | - if (empty($text)){ |
|
| 182 | + if (empty($text)) { |
|
| 183 | 183 | $code = abs($code); |
| 184 | 184 | $http_status = array( |
| 185 | 185 | 100 => 'Continue', |
@@ -228,18 +228,18 @@ discard block |
||
| 228 | 228 | 504 => 'Gateway Timeout', |
| 229 | 229 | 505 => 'HTTP Version Not Supported', |
| 230 | 230 | ); |
| 231 | - if (isset($http_status[$code])){ |
|
| 231 | + if (isset($http_status[$code])) { |
|
| 232 | 232 | $text = $http_status[$code]; |
| 233 | 233 | } |
| 234 | - else{ |
|
| 234 | + else { |
|
| 235 | 235 | show_error('No HTTP status text found for your code please check it.'); |
| 236 | 236 | } |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - if (strpos(php_sapi_name(), 'cgi') === 0){ |
|
| 239 | + if (strpos(php_sapi_name(), 'cgi') === 0) { |
|
| 240 | 240 | header('Status: ' . $code . ' ' . $text, TRUE); |
| 241 | 241 | } |
| 242 | - else{ |
|
| 242 | + else { |
|
| 243 | 243 | $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
| 244 | 244 | header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
| 245 | 245 | } |
@@ -254,13 +254,13 @@ discard block |
||
| 254 | 254 | * |
| 255 | 255 | * @codeCoverageIgnore |
| 256 | 256 | */ |
| 257 | - function show_error($msg, $title = 'error', $logging = true){ |
|
| 257 | + function show_error($msg, $title = 'error', $logging = true) { |
|
| 258 | 258 | $title = strtoupper($title); |
| 259 | 259 | $data = array(); |
| 260 | 260 | $data['error'] = $msg; |
| 261 | 261 | $data['title'] = $title; |
| 262 | - if ($logging){ |
|
| 263 | - save_to_log('error', '['.$title.'] '.strip_tags($msg), 'GLOBAL::ERROR'); |
|
| 262 | + if ($logging) { |
|
| 263 | + save_to_log('error', '[' . $title . '] ' . strip_tags($msg), 'GLOBAL::ERROR'); |
|
| 264 | 264 | } |
| 265 | 265 | $response = & class_loader('Response', 'classes'); |
| 266 | 266 | $response->sendError($data); |
@@ -274,18 +274,18 @@ discard block |
||
| 274 | 274 | * |
| 275 | 275 | * @return boolean true if the web server uses the https protocol, false if not. |
| 276 | 276 | */ |
| 277 | - function is_https(){ |
|
| 277 | + function is_https() { |
|
| 278 | 278 | /* |
| 279 | 279 | * some servers pass the "HTTPS" parameter in the server variable, |
| 280 | 280 | * if is the case, check if the value is "on", "true", "1". |
| 281 | 281 | */ |
| 282 | - if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
|
| 282 | + if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') { |
|
| 283 | 283 | return true; |
| 284 | 284 | } |
| 285 | - else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
| 285 | + else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
|
| 286 | 286 | return true; |
| 287 | 287 | } |
| 288 | - else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
| 288 | + else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') { |
|
| 289 | 289 | return true; |
| 290 | 290 | } |
| 291 | 291 | return false; |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | * |
| 301 | 301 | * @return boolean true if is a valid URL address or false. |
| 302 | 302 | */ |
| 303 | - function is_url($url){ |
|
| 303 | + function is_url($url) { |
|
| 304 | 304 | return preg_match('/^(http|https|ftp):\/\/(.*)/', $url) == 1; |
| 305 | 305 | } |
| 306 | 306 | |
@@ -310,8 +310,8 @@ discard block |
||
| 310 | 310 | * @param string $controllerClass the controller class name to be loaded |
| 311 | 311 | * @codeCoverageIgnore |
| 312 | 312 | */ |
| 313 | - function autoload_controller($controllerClass){ |
|
| 314 | - if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')){ |
|
| 313 | + function autoload_controller($controllerClass) { |
|
| 314 | + if (file_exists($path = APPS_CONTROLLER_PATH . $controllerClass . '.php')) { |
|
| 315 | 315 | require_once $path; |
| 316 | 316 | } |
| 317 | 317 | } |
@@ -325,11 +325,11 @@ discard block |
||
| 325 | 325 | * |
| 326 | 326 | * @return boolean |
| 327 | 327 | */ |
| 328 | - function php_exception_handler($ex){ |
|
| 329 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 330 | - show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
| 328 | + function php_exception_handler($ex) { |
|
| 329 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) { |
|
| 330 | + show_error('An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
|
| 331 | 331 | } |
| 332 | - else{ |
|
| 332 | + else { |
|
| 333 | 333 | save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
| 334 | 334 | } |
| 335 | 335 | return true; |
@@ -346,16 +346,16 @@ discard block |
||
| 346 | 346 | * |
| 347 | 347 | * @return boolean |
| 348 | 348 | */ |
| 349 | - function php_error_handler($errno , $errstr, $errfile , $errline){ |
|
| 349 | + function php_error_handler($errno, $errstr, $errfile, $errline) { |
|
| 350 | 350 | $isError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errno) === $errno); |
| 351 | - if ($isError){ |
|
| 351 | + if ($isError) { |
|
| 352 | 352 | set_http_status_header(500); |
| 353 | 353 | } |
| 354 | - if (! (error_reporting() & $errno)) { |
|
| 354 | + if (!(error_reporting() & $errno)) { |
|
| 355 | 355 | save_to_log('error', 'An error is occurred in the file ' . $errfile . ' at line ' . $errline . ' raison : ' . $errstr, 'PHP ERROR'); |
| 356 | 356 | return; |
| 357 | 357 | } |
| 358 | - if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
|
| 358 | + if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))) { |
|
| 359 | 359 | $errorType = 'error'; |
| 360 | 360 | switch ($errno) { |
| 361 | 361 | case E_USER_ERROR: |
@@ -371,9 +371,9 @@ discard block |
||
| 371 | 371 | $errorType = 'error'; |
| 372 | 372 | break; |
| 373 | 373 | } |
| 374 | - show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline .'</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
| 374 | + show_error('An error is occurred in the file <b>' . $errfile . '</b> at line <b>' . $errline . '</b> raison : ' . $errstr, 'PHP ' . $errorType); |
|
| 375 | 375 | } |
| 376 | - if ($isError){ |
|
| 376 | + if ($isError) { |
|
| 377 | 377 | die(); |
| 378 | 378 | } |
| 379 | 379 | return true; |
@@ -383,10 +383,10 @@ discard block |
||
| 383 | 383 | * This function is used to run in shutdown situation of the script |
| 384 | 384 | * @codeCoverageIgnore |
| 385 | 385 | */ |
| 386 | - function php_shudown_handler(){ |
|
| 386 | + function php_shudown_handler() { |
|
| 387 | 387 | $lastError = error_get_last(); |
| 388 | 388 | if (isset($lastError) && |
| 389 | - ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))){ |
|
| 389 | + ($lastError['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) { |
|
| 390 | 390 | php_error_handler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']); |
| 391 | 391 | } |
| 392 | 392 | } |
@@ -404,11 +404,11 @@ discard block |
||
| 404 | 404 | * |
| 405 | 405 | * @return string string of the HTML attribute. |
| 406 | 406 | */ |
| 407 | - function attributes_to_string(array $attributes){ |
|
| 407 | + function attributes_to_string(array $attributes) { |
|
| 408 | 408 | $str = ' '; |
| 409 | 409 | //we check that the array passed as an argument is not empty. |
| 410 | - if (! empty($attributes)){ |
|
| 411 | - foreach($attributes as $key => $value){ |
|
| 410 | + if (!empty($attributes)) { |
|
| 411 | + foreach ($attributes as $key => $value) { |
|
| 412 | 412 | $key = trim(htmlspecialchars($key)); |
| 413 | 413 | $value = trim(htmlspecialchars($value)); |
| 414 | 414 | /* |
@@ -418,10 +418,10 @@ discard block |
||
| 418 | 418 | * $attr = array('placeholder' => 'I am a "puple"') |
| 419 | 419 | * $str = attributes_to_string($attr); => placeholder = "I am a \"puple\"" |
| 420 | 420 | */ |
| 421 | - if ($value && strpos('"', $value) !== false){ |
|
| 421 | + if ($value && strpos('"', $value) !== false) { |
|
| 422 | 422 | $value = addslashes($value); |
| 423 | 423 | } |
| 424 | - $str .= $key.' = "'.$value.'" '; |
|
| 424 | + $str .= $key . ' = "' . $value . '" '; |
|
| 425 | 425 | } |
| 426 | 426 | } |
| 427 | 427 | //remove the space after using rtrim() |
@@ -437,7 +437,7 @@ discard block |
||
| 437 | 437 | * |
| 438 | 438 | * @return string the stringfy value |
| 439 | 439 | */ |
| 440 | - function stringfy_vars($var){ |
|
| 440 | + function stringfy_vars($var) { |
|
| 441 | 441 | return print_r($var, true); |
| 442 | 442 | } |
| 443 | 443 | |
@@ -448,18 +448,18 @@ discard block |
||
| 448 | 448 | * |
| 449 | 449 | * @return mixed the sanitize value |
| 450 | 450 | */ |
| 451 | - function clean_input($str){ |
|
| 452 | - if (is_array($str)){ |
|
| 451 | + function clean_input($str) { |
|
| 452 | + if (is_array($str)) { |
|
| 453 | 453 | $str = array_map('clean_input', $str); |
| 454 | 454 | } |
| 455 | - else if (is_object($str)){ |
|
| 455 | + else if (is_object($str)) { |
|
| 456 | 456 | $obj = $str; |
| 457 | 457 | foreach ($str as $var => $value) { |
| 458 | 458 | $obj->$var = clean_input($value); |
| 459 | 459 | } |
| 460 | 460 | $str = $obj; |
| 461 | 461 | } |
| 462 | - else{ |
|
| 462 | + else { |
|
| 463 | 463 | $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
| 464 | 464 | } |
| 465 | 465 | return $str; |
@@ -477,11 +477,11 @@ discard block |
||
| 477 | 477 | * |
| 478 | 478 | * @return string the string with the hidden part. |
| 479 | 479 | */ |
| 480 | - function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*'){ |
|
| 480 | + function string_hidden($str, $startCount = 0, $endCount = 0, $hiddenChar = '*') { |
|
| 481 | 481 | //get the string length |
| 482 | 482 | $len = strlen($str); |
| 483 | 483 | //if str is empty |
| 484 | - if ($len <= 0){ |
|
| 484 | + if ($len <= 0) { |
|
| 485 | 485 | return str_repeat($hiddenChar, 6); |
| 486 | 486 | } |
| 487 | 487 | //if the length is less than startCount and endCount |
@@ -489,14 +489,14 @@ discard block |
||
| 489 | 489 | //or startCount is negative or endCount is negative |
| 490 | 490 | //return the full string hidden |
| 491 | 491 | |
| 492 | - if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)){ |
|
| 492 | + if ((($startCount + $endCount) > $len) || ($startCount == 0 && $endCount == 0) || ($startCount < 0 || $endCount < 0)) { |
|
| 493 | 493 | return str_repeat($hiddenChar, $len); |
| 494 | 494 | } |
| 495 | 495 | //the start non hidden string |
| 496 | 496 | $startNonHiddenStr = substr($str, 0, $startCount); |
| 497 | 497 | //the end non hidden string |
| 498 | 498 | $endNonHiddenStr = null; |
| 499 | - if ($endCount > 0){ |
|
| 499 | + if ($endCount > 0) { |
|
| 500 | 500 | $endNonHiddenStr = substr($str, - $endCount); |
| 501 | 501 | } |
| 502 | 502 | //the hidden string |
@@ -509,31 +509,31 @@ discard block |
||
| 509 | 509 | * This function is used to set the initial session config regarding the configuration |
| 510 | 510 | * @codeCoverageIgnore |
| 511 | 511 | */ |
| 512 | - function set_session_config(){ |
|
| 512 | + function set_session_config() { |
|
| 513 | 513 | //$_SESSION is not available on cli mode |
| 514 | - if (! IS_CLI){ |
|
| 515 | - $logger =& class_loader('Log', 'classes'); |
|
| 514 | + if (!IS_CLI) { |
|
| 515 | + $logger = & class_loader('Log', 'classes'); |
|
| 516 | 516 | $logger->setLogger('PHPSession'); |
| 517 | 517 | //set session params |
| 518 | 518 | $sessionHandler = get_config('session_handler', 'files'); //the default is to store in the files |
| 519 | 519 | $sessionName = get_config('session_name'); |
| 520 | - if ($sessionName){ |
|
| 520 | + if ($sessionName) { |
|
| 521 | 521 | session_name($sessionName); |
| 522 | 522 | } |
| 523 | 523 | $logger->info('Session handler: ' . $sessionHandler); |
| 524 | 524 | $logger->info('Session name: ' . $sessionName); |
| 525 | 525 | |
| 526 | - if ($sessionHandler == 'files'){ |
|
| 526 | + if ($sessionHandler == 'files') { |
|
| 527 | 527 | $sessionSavePath = get_config('session_save_path'); |
| 528 | - if ($sessionSavePath){ |
|
| 529 | - if (! is_dir($sessionSavePath)){ |
|
| 528 | + if ($sessionSavePath) { |
|
| 529 | + if (!is_dir($sessionSavePath)) { |
|
| 530 | 530 | mkdir($sessionSavePath, 1773); |
| 531 | 531 | } |
| 532 | 532 | session_save_path($sessionSavePath); |
| 533 | 533 | $logger->info('Session save path: ' . $sessionSavePath); |
| 534 | 534 | } |
| 535 | 535 | } |
| 536 | - else if ($sessionHandler == 'database'){ |
|
| 536 | + else if ($sessionHandler == 'database') { |
|
| 537 | 537 | //load database session handle library |
| 538 | 538 | //Model |
| 539 | 539 | require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
@@ -541,11 +541,11 @@ discard block |
||
| 541 | 541 | //Database Session handler Model |
| 542 | 542 | require_once CORE_CLASSES_MODEL_PATH . 'DBSessionHandlerModel.php'; |
| 543 | 543 | |
| 544 | - $DBS =& class_loader('DBSessionHandler', 'classes'); |
|
| 544 | + $DBS = & class_loader('DBSessionHandler', 'classes'); |
|
| 545 | 545 | session_set_save_handler($DBS, true); |
| 546 | 546 | $logger->info('session save path: ' . get_config('session_save_path')); |
| 547 | 547 | } |
| 548 | - else{ |
|
| 548 | + else { |
|
| 549 | 549 | show_error('Invalid session handler configuration'); |
| 550 | 550 | } |
| 551 | 551 | $lifetime = get_config('session_cookie_lifetime', 0); |
@@ -568,9 +568,9 @@ discard block |
||
| 568 | 568 | $logger->info('Session lifetime: ' . $lifetime); |
| 569 | 569 | $logger->info('Session cookie path: ' . $path); |
| 570 | 570 | $logger->info('Session domain: ' . $domain); |
| 571 | - $logger->info('Session is secure: ' . ($secure ? 'TRUE':'FALSE')); |
|
| 571 | + $logger->info('Session is secure: ' . ($secure ? 'TRUE' : 'FALSE')); |
|
| 572 | 572 | |
| 573 | - if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()){ |
|
| 573 | + if ((function_exists('session_status') && session_status() !== PHP_SESSION_ACTIVE) || !session_id()) { |
|
| 574 | 574 | $logger->info('Session not yet start, start it now'); |
| 575 | 575 | session_start(); |
| 576 | 576 | } |
@@ -230,16 +230,14 @@ discard block |
||
| 230 | 230 | ); |
| 231 | 231 | if (isset($http_status[$code])){ |
| 232 | 232 | $text = $http_status[$code]; |
| 233 | - } |
|
| 234 | - else{ |
|
| 233 | + } else{ |
|
| 235 | 234 | show_error('No HTTP status text found for your code please check it.'); |
| 236 | 235 | } |
| 237 | 236 | } |
| 238 | 237 | |
| 239 | 238 | if (strpos(php_sapi_name(), 'cgi') === 0){ |
| 240 | 239 | header('Status: ' . $code . ' ' . $text, TRUE); |
| 241 | - } |
|
| 242 | - else{ |
|
| 240 | + } else{ |
|
| 243 | 241 | $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
| 244 | 242 | header($proto . ' ' . $code . ' ' . $text, TRUE, $code); |
| 245 | 243 | } |
@@ -281,11 +279,9 @@ discard block |
||
| 281 | 279 | */ |
| 282 | 280 | if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off'){ |
| 283 | 281 | return true; |
| 284 | - } |
|
| 285 | - else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
| 282 | + } else if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'){ |
|
| 286 | 283 | return true; |
| 287 | - } |
|
| 288 | - else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
| 284 | + } else if (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off'){ |
|
| 289 | 285 | return true; |
| 290 | 286 | } |
| 291 | 287 | return false; |
@@ -328,8 +324,7 @@ discard block |
||
| 328 | 324 | function php_exception_handler($ex){ |
| 329 | 325 | if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors'))){ |
| 330 | 326 | show_error('An exception is occured in file '. $ex->getFile() .' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception #' . $ex->getCode()); |
| 331 | - } |
|
| 332 | - else{ |
|
| 327 | + } else{ |
|
| 333 | 328 | save_to_log('error', 'An exception is occured in file ' . $ex->getFile() . ' at line ' . $ex->getLine() . ' raison : ' . $ex->getMessage(), 'PHP Exception'); |
| 334 | 329 | } |
| 335 | 330 | return true; |
@@ -451,15 +446,13 @@ discard block |
||
| 451 | 446 | function clean_input($str){ |
| 452 | 447 | if (is_array($str)){ |
| 453 | 448 | $str = array_map('clean_input', $str); |
| 454 | - } |
|
| 455 | - else if (is_object($str)){ |
|
| 449 | + } else if (is_object($str)){ |
|
| 456 | 450 | $obj = $str; |
| 457 | 451 | foreach ($str as $var => $value) { |
| 458 | 452 | $obj->$var = clean_input($value); |
| 459 | 453 | } |
| 460 | 454 | $str = $obj; |
| 461 | - } |
|
| 462 | - else{ |
|
| 455 | + } else{ |
|
| 463 | 456 | $str = htmlspecialchars(strip_tags($str), ENT_QUOTES, 'UTF-8'); |
| 464 | 457 | } |
| 465 | 458 | return $str; |
@@ -532,8 +525,7 @@ discard block |
||
| 532 | 525 | session_save_path($sessionSavePath); |
| 533 | 526 | $logger->info('Session save path: ' . $sessionSavePath); |
| 534 | 527 | } |
| 535 | - } |
|
| 536 | - else if ($sessionHandler == 'database'){ |
|
| 528 | + } else if ($sessionHandler == 'database'){ |
|
| 537 | 529 | //load database session handle library |
| 538 | 530 | //Model |
| 539 | 531 | require_once CORE_CLASSES_MODEL_PATH . 'Model.php'; |
@@ -544,8 +536,7 @@ discard block |
||
| 544 | 536 | $DBS =& class_loader('DBSessionHandler', 'classes'); |
| 545 | 537 | session_set_save_handler($DBS, true); |
| 546 | 538 | $logger->info('session save path: ' . get_config('session_save_path')); |
| 547 | - } |
|
| 548 | - else{ |
|
| 539 | + } else{ |
|
| 549 | 540 | show_error('Invalid session handler configuration'); |
| 550 | 541 | } |
| 551 | 542 | $lifetime = get_config('session_cookie_lifetime', 0); |