Advertisement
Faguss

UserSpice Validate.php - custom version

Jun 13th, 2017
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.72 KB | None | 0 0
  1. <?php
  2. /*
  3. UserSpice 4
  4. An Open Source PHP User Management System
  5. by the UserSpice Team at http://UserSpice.com
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. class Validate
  21. {
  22.     public
  23.             $_errors = [],
  24.             $_db     = null;
  25.  
  26.     public function __construct()  {
  27.         $this->_db = DB::getInstance();
  28.     }
  29.  
  30.     public function check($source, $items=[], $sanitize=true) {
  31.         $this->_errors = [];
  32.        
  33.         foreach ($items as $item => $rules) {
  34.             $item    = sanitize($item);
  35.             $display = $rules['display'];
  36.            
  37.             foreach ($rules as $rule => $rule_value) {
  38.                 $value = $source[$item];
  39.                
  40.                 if ($sanitize)
  41.                     $value = sanitize(trim($value));
  42.                
  43.                 $length = is_array($value) ? count($value) : strlen($value);
  44.                 $verb   = is_array($value) ? "are"         : "is";
  45.  
  46.                 if ($rule=='required'  &&  $length==0) {
  47.                     if ($rule_value)
  48.                         $this->addError(["{$display} {$verb} required",$item]);
  49.                 }
  50.                 else
  51.                 if ($length != 0) {
  52.                     switch ($rule) {
  53.                         case 'min':
  54.                             if (is_array($rule_value))
  55.                                 $rule_value = max($rule_value);
  56.  
  57.                             if ($length < $rule_value)
  58.                                 $this->addError(["{$display} must be a minimum of {$rule_value} characters",$item]);
  59.                             break;
  60.  
  61.                         case 'max':
  62.                             if (is_array($rule_value))
  63.                                 $rule_value = min($rule_value);
  64.                            
  65.                             if ($length > $rule_value)
  66.                                 $this->addError(["{$display} must be a maximum of {$rule_value} characters",$item]);
  67.                             break;
  68.  
  69.                         case 'matches':
  70.                             if (!is_array($rule_value))
  71.                                 $rule_value = [$rule_value];
  72.                            
  73.                             foreach ($rule_value as $current_item)
  74.                                 if ($value != $source[$current_item])
  75.                                     $this->addError(["{$items[$current_item]['display']} and {$display} must match",$item]);
  76.                             break;
  77.  
  78.                         case 'unique':
  79.                             $table  = is_array($rule_value) ? $rule_value[0] : $rule_value;
  80.                             $fields = is_array($rule_value) ? $rule_value[1] : [$item, '=', $value];
  81.                            
  82.                             if ($this->_db->get($table, $fields)) {
  83.                                 if ($this->_db->count())
  84.                                     $this->addError(["{$display} already exists. Please choose another {$display}",$item]);
  85.                             } else
  86.                                 $this->addError(["Cannot verify {$display}. Database error",$item]);
  87.                             break;
  88.  
  89.                         case 'unique_update':
  90.                             $t     = explode(',', $rule_value);
  91.                             $table = $t[0];
  92.                             $id    = $t[1];
  93.                             $query = "SELECT * FROM {$table} WHERE id != {$id} AND {$item} = '{$value}'";
  94.                             $check = $this->_db->query($query);
  95.                            
  96.                             if ($check->count())
  97.                                 $this->addError(["{$display} already exists. Please choose another {$display}",$item]);
  98.                             break;
  99.  
  100.                         case 'is_numeric': case 'is_num':
  101.                             if ($rule_value  &&  !is_numeric($value))
  102.                                 $this->addError(["{$display} has to be a number. Please use a numeric value",$item]);
  103.                             break;
  104.  
  105.                         case 'valid_email':
  106.                             if(!filter_var($value,FILTER_VALIDATE_EMAIL))
  107.                                 $this->addError(["{$display} must be a valid email address",$item]);
  108.                             break;
  109.                            
  110.                         case '<'  :
  111.                         case '>'  :
  112.                         case '<=' :
  113.                         case '>=' :
  114.                         case '!=' :
  115.                         case '==' :
  116.                             $array = is_array($rule_value) ? $rule_value : [$rule_value];
  117.  
  118.                             foreach ($array as $rule_value)
  119.                                 if (is_numeric($value)) {
  120.                                     $rule_value_display = $rule_value;
  121.                                    
  122.                                     if (!is_numeric($rule_value)  &&  isset($source[$rule_value])) {
  123.                                         $rule_value_display = $items[$rule_value]["display"];
  124.                                         $rule_value         = $source[$rule_value];
  125.                                     }
  126.  
  127.                                     if ($rule=="<"  &&  $value>=$rule_value)
  128.                                         $this->addError(["{$display} must be smaller than {$rule_value_display}",$item]);
  129.                                    
  130.                                     if ($rule==">"  &&  $value<=$rule_value)
  131.                                         $this->addError(["{$display} must be larger than {$rule_value_display}",$item]);
  132.                                    
  133.                                     if ($rule=="<="  &&  $value>$rule_value)
  134.                                         $this->addError(["{$display} must be equal {$rule_value_display} or smaller",$item]);
  135.                                    
  136.                                     if ($rule==">="  &&  $value<$rule_value)
  137.                                         $this->addError(["{$display} must be equal {$rule_value_display} or larger",$item]);
  138.                                    
  139.                                     if ($rule=="!="  &&  $value==$rule_value)
  140.                                         $this->addError(["{$display} must be different from {$rule_value_display}",$item]);
  141.                                    
  142.                                     if ($rule=="=="  &&  $value!=$rule_value)
  143.                                         $this->addError(["{$display} must equal {$rule_value_display}",$item]);
  144.                                 }
  145.                                 else
  146.                                     $this->addError(["{$display} has to be a number. Please use a numeric value",$item]);
  147.                             break;
  148.                            
  149.                         case 'is_integer': case 'is_int':
  150.                             if ($rule_value  &&  filter_var($value, FILTER_VALIDATE_INT)===false)
  151.                                 $this->addError(["{$display} has to be an integer",$item]);
  152.                             break;
  153.                            
  154.                         case 'is_timezone':
  155.                             if ($rule_value)
  156.                                 if (array_search($value, DateTimeZone::listIdentifiers(DateTimeZone::ALL)) === FALSE)
  157.                                     $this->addError(["{$display} has to be a valid time zone name",$item]);
  158.                         break;
  159.                        
  160.                         case 'in':
  161.                             $verb           = "have to be";
  162.                             $list_of_names  = [];   // if doesn't match then display these in an error message
  163.                             $list_of_values = [];   // to compare it against
  164.                            
  165.                             if (!is_array($rule_value))
  166.                                 $rule_value = [$rule_value];
  167.                            
  168.                             foreach($rule_value as $val)
  169.                                 if (!is_array($val)) {
  170.                                     $list_of_names[]  = $val;
  171.                                     $list_of_values[] = strtolower($val);
  172.                                 } else
  173.                                     if (count($val) > 0) {
  174.                                         $list_of_names[]  = $val[0];
  175.                                         $list_of_values[] = strtolower((count($val)>1 ? $val[1] : $val[0]));
  176.                                     }
  177.                            
  178.                             if (!is_array($value)) {
  179.                                 $verb  = "has to be one of the following";
  180.                                 $value = [$value];
  181.                             }
  182.                            
  183.                             foreach ($value as $val) {
  184.                                 if (array_search(strtolower($val), $list_of_values) === FALSE) {
  185.                                     $this->addError(["{$display} {$verb}: ".implode(', ',$list_of_names),$item]);
  186.                                     break;
  187.                                 }
  188.                             }
  189.                         break;
  190.                        
  191.                         case 'is_datetime':
  192.                         if ($rule_value !== false) {
  193.                             $object = DateTime::createFromFormat((empty($rule_value) || is_bool($rule_value) ? "Y-m-d H:i:s" : $rule_value), $value);
  194.                        
  195.                             if (!$object  ||  DateTime::getLastErrors()["warning_count"]>0  ||  DateTime::getLastErrors()["error_count"]>0)
  196.                                 $this->addError(["{$display} has to be a valid time",$item]);
  197.                         }
  198.                         break;
  199.                     }
  200.                 }
  201.             }
  202.  
  203.         }
  204.        
  205.         return $this;
  206.     }
  207.  
  208.     public function addError($error) {
  209.         if (array_search($error, $this->_errors) === FALSE)
  210.             $this->_errors[] = $error;
  211.     }
  212.  
  213.     public function display_errors() {
  214.         $html = "<UL CLASS='bg-danger'>";
  215.        
  216.         foreach($this->_errors as $error) {
  217.             if (is_array($error))
  218.                 $html    .= "<LI CLASS='text-danger'>{$error[0]}</LI>
  219.                              <SCRIPT>jQuery('document').ready(function(){jQuery('#{$error[1]}').parent().closest('div').addClass('has-error');});</SCRIPT>";
  220.             else
  221.                 $html .= "<LI CLASS='text-danger'>{$error}</LI>";
  222.         }
  223.        
  224.         $html .= "</UL>";
  225.         return $html;
  226.     }
  227.  
  228.     public function errors(){
  229.         return $this->_errors;
  230.     }
  231.  
  232.     public function passed(){
  233.         return empty($this->_errors);
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement