Exemplo n.º 1
0
 /**
  * Description:      if the file upload is done function returns the uploaded file array, else returns false
  * Example:          $imageConfig = [
  *                       'file'    => Request::files('image'),
  *                       'maxSize' => 6.21,
  *                       'path'    => 'files/images/',
  *                       'name'    => 'myImage'
  *                   ];
  *                   File::upload($imageConfig);
  * Available params: file    (the input file)
  *                   maxSize (Mb)
  *                   path    (the path to save image)
  *                   name    (the new name of the uploaded file)
  * @param            $config
  * @return           the uploaded file data [size]     => 2 (mb)
  *                                          [origName] => default.png
  *                                          [name]     => myImage.png (the changed file name)
  *                                          [type]     => png (the file extension)
  *                                          [path]     => files/images
  *                                          [fullPath] => files/images/myImage.png (the path of the saved file)
  * @throws HelperException
  * @throws \simple\exceptions\ConfigException
  */
 public static function upload(array $config)
 {
     //the connection of the file messages
     if (!($path = \simple\utilities\File::find(APPDIR . 'messages', Config::get('language')))) {
         throw new HelperException("The messages file is not found");
     }
     require $path;
     $inputFileData = $config['file'];
     $maxSizeB = ceil($config['maxSize'] * 1024 * 1024);
     //if the size of the input file is over that a specified file max size return false
     if ($inputFileData['size'] > $maxSizeB) {
         $difference = $inputFileData['size'] - $maxSizeB;
         $msg = preg_replace(['/{difference}/'], [$difference], $message['fileMaxSize']);
         self::$errors['maxSize'] = $msg;
     }
     //the path to save the file
     $destinationPath = $config['path'] . '/';
     $destinationPath = str_ireplace('//', '/', $destinationPath);
     //the file extension
     $fileExt = end(explode('.', $inputFileData['name']));
     //the original file name
     $origName = $inputFileData['name'];
     //the new file name
     $newName = !empty($config['name']) ? $config['name'] . '.' . $fileExt : $origName;
     $moveUploadedFile = move_uploaded_file($config['file']['tmp_name'], $destinationPath . $newName);
     //if failed to move file return the errors array
     if (!$moveUploadedFile) {
         self::$errors['move'] = $message['moveUploadedFile'];
     }
     //if the file is uploaded return the uploaded file data array
     $uploadedFileData = ['size' => $inputFileData['size'], 'origName' => $inputFileData['name'], 'name' => $newName, 'type' => $inputFileData['type'], 'ext' => $fileExt, 'path' => $destinationPath, 'fullPath' => $destinationPath . $newName];
     return $uploadedFileData;
 }
Exemplo n.º 2
0
 public function run()
 {
     $request = $_SERVER['REQUEST_URI'];
     $splits = explode('/', trim($request, '/'));
     $this->controller = !empty($splits[1]) ? ucfirst($splits[1]) : Config::get('defaultController');
     $this->action = !empty($splits[2]) ? $splits[2] : Config::get('defaultAction');
     if (!empty($splits[3])) {
         $this->param = $splits[3];
     }
     if (!($path = File::find(APPDIR . 'controllers', $this->controller))) {
         throw new RouteException("The requested controller file {$this->controller} in " . APPDIR . 'controllers' . DIRECTORY_SEPARATOR . $this->controller . EXT . " is not found");
     }
     require $path;
     if (!class_exists($this->controller)) {
         throw new RouteException('The requested controller class ' . $this->controller . ' is not found');
     }
     $rc = new ReflectionClass($this->controller);
     if (!$rc->isSubclassOf(Controller::class)) {
         throw new RouteException("The {$this->controller} controller must inherit a base controller");
     }
     if (!$rc->hasMethod($this->action)) {
         throw new RouteException('The requested action ' . $this->action . ' is not found');
     }
     $controller = $rc->newInstance();
     $action = $rc->getMethod($this->action);
     $action->invoke($controller, $this->param);
 }
Exemplo n.º 3
0
 /**
  * Description: return a config value
  * Example:     Config::get('param');
  * @param       $param
  * @return      mixed
  * @throws      ConfigException
  */
 public static function get($param)
 {
     if (!($path = File::find(APPDIR . 'configs', 'config'))) {
         throw new ConfigException('The configuration file configs is not found');
     }
     require $path;
     return $config[$param];
 }
Exemplo n.º 4
0
 /**
  * Description: An open of the db connection
  * @return      mixed
  * @throws      DataBaseException
  * @throws      \simple\utilities\DataBaseException
  */
 public function open()
 {
     $driver = Config::get('dbDriver') . 'Driver';
     if (!($path = File::find(VENDIR . 'simple' . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR . 'drivers', $driver))) {
         throw new DataBaseException('The db driver file is not found');
     }
     require_once $path;
     $driverInstance = new $driver(Config::get('dbHostname'), Config::get('dbUsername'), Config::get('dbPassword'), Config::get('dbName'), Config::get('dbCharset'));
     return $driverInstance->connection;
 }
Exemplo n.º 5
0
 /**
  * Description: Creating an object model
  * Example:     ORM::instance('tableName');
  * @param       $model
  * @param       null $condition
  * @return      object of the selected model class
  * @throws      DataBaseException
  */
 public static function instance($model, $condition = null)
 {
     if (!($path = File::find(APPDIR . 'models', $model))) {
         throw new DataBaseException('The ' . $model . ' model not found');
     }
     require $path;
     if (!class_exists($model)) {
         throw new DataBaseException('The ' . $model . ' class not found');
     }
     return new $model($condition);
 }
Exemplo n.º 6
0
 /**
  * Description: return a model class example
  * @param       $model
  * @return      mixed
  * @throws      DataBaseException
  */
 public static function instance($model)
 {
     if (!($path = File::find(APPDIR . 'models' . DIRECTORY_SEPARATOR, $model))) {
         throw new DataBaseException("File {$model} is not found");
     }
     require $path;
     if (!class_exists($model)) {
         throw new DataBaseException("Model class {$model} is not found");
     }
     return new $model();
 }
Exemplo n.º 7
0
 protected function model($model, $name = null)
 {
     if (!($path = File::find(APPDIR . 'models', $model))) {
         throw new ControllerException('The model class ' . ucfirst($model) . ' is not found');
     }
     require_once $path;
     if (!is_null($name)) {
         $modelName = $name;
     } else {
         $modelName = $model;
     }
     $this->{$modelName} = new $model();
 }
Exemplo n.º 8
0
 /**
  * Description: The showing of the widget
  * Example:     $this->widget('viewFile', $data); (within the view file)
  * @param       $file
  * @param       array $data
  * @throws      ViewException
  */
 public function view(string $file, array $data = null)
 {
     if (!($path = File::find(APPDIR . 'views', $file))) {
         throw new ViewException('The widget view file ' . $file . ' not found');
     }
     if (!is_null($data)) {
         extract($data);
     }
     require $path;
 }
Exemplo n.º 9
0
 /**
  * Description: If exist errors, this function returns false, else returns true
  * @return bool
  * @throws HelperException
  * @throws \simple\utilities\HelperException
  */
 public function check()
 {
     //the connection of the messages file
     if (!($path = File::find(APPDIR . 'messages', Config::get('language')))) {
         throw new HelperException("The message file is not found");
     }
     require $path;
     unset($this->errors);
     foreach ($this->data as $fieldName => $fieldValue) {
         if (!isset($this->rules[$fieldName])) {
             continue;
         }
         array_push($this->fields, $fieldName);
         //an array of error names
         $errors = array();
         //an array of the names of errors
         $errorMessages = array();
         foreach ($this->rules[$fieldName] as $rule) {
             if (preg_match('(min\\[[0-9]+\\])', $rule)) {
                 $min = substr($rule, 4, strlen($rule) - 5);
             }
             if (preg_match('(max\\[[0-9]+\\])', $rule)) {
                 $max = substr($rule, 4, strlen($rule) - 5);
             }
             if (preg_match('[required]', $rule)) {
                 $required = true;
             }
             if (preg_match('[email]', $rule)) {
                 $email = true;
             }
             if (preg_match('[unique]', $rule)) {
                 $unique = substr($rule, 7, strlen($rule) - 8);
             }
             if (preg_match('[number]', $rule)) {
                 $number = true;
             }
             if (preg_match('[letter]', $rule)) {
                 $letter = true;
             }
             if (preg_match('[en]', $rule)) {
                 $en = true;
             }
             if (preg_match('[ru]', $rule)) {
                 $ru = true;
             }
             if (preg_match('[decimal]', $rule)) {
                 $decimal = true;
             }
             if (preg_match('[integer]', $rule)) {
                 $integer = true;
             }
             if (preg_match('[lower]', $rule)) {
                 $lower = true;
             }
             if (preg_match('[upper]', $rule)) {
                 $upper = true;
             }
             if (preg_match('[repeat]', $rule)) {
                 $repeat = substr($rule, 7, strlen($rule) - 8);
                 $this->repeatedFields = $fieldName;
             }
             if (preg_match('[auth]', $rule)) {
                 $auth = substr($rule, 5, strlen($rule) - 6);
             }
         }
         if (isset($min)) {
             //if the string length is smaller than specified in rule
             if (iconv_strlen($this->data[$fieldName]) < $min) {
                 array_push($errors, 'min');
                 $fieldTitle = $this->setFieldName($fieldName);
                 $msg = preg_replace(['/{field}/', '/{num}/'], [$fieldTitle, $min], $message['min']);
                 //add a message to array
                 array_push($errorMessages, ['min' => $msg]);
                 unset($min);
             }
         }
         if (isset($max)) {
             //if the string length is more than specified in rule
             if (iconv_strlen($this->data[$fieldName]) > $max) {
                 array_push($errors, 'max');
                 $fieldTitle = $this->setFieldName($fieldName);
                 $msg = preg_replace(['/{field}/', '/{num}/'], [$fieldTitle, $max], $message['max']);
                 //add a message to array
                 array_push($errorMessages, ['max' => $msg]);
                 unset($max);
             }
         }
         if ($required) {
             //if exists rule for the field
             if (in_array('required', $this->rules[$fieldName])) {
                 //if the field is not empty, check the rules
                 if (empty($this->data[trim($fieldName)])) {
                     array_push($errors, 'required');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['required']);
                     //add a message to array
                     array_push($errorMessages, ['required' => $msg]);
                 }
             }
         }
         if ($email) {
             //if exists rule for the field
             if (in_array('email', $this->rules[$fieldName])) {
                 //if the field is not empty, check the rules
                 if (!empty($this->data[$fieldName]) && !filter_var($this->data[$fieldName], FILTER_VALIDATE_EMAIL)) {
                     array_push($errors, 'email');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['email']);
                     //add a message to array
                     array_push($errorMessages, ['email' => $msg]);
                 }
             }
         }
         if ($number) {
             //if exists rule for the field
             if (in_array('number', $this->rules[$fieldName])) {
                 //if the field is not empty, check the rules
                 if (!empty($this->data[$fieldName]) && preg_match('([^+0-9])', $this->data[$fieldName])) {
                     array_push($errors, 'number');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['number']);
                     //add a message to array
                     array_push($errorMessages, ['number' => $msg]);
                 }
             }
         }
         if ($letter) {
             //if exists rule for the field
             if (in_array('letter', $this->rules[$fieldName])) {
                 //if the field is not empty, check the rules
                 if (!empty($this->data[$fieldName]) && preg_match('([^A-zÀ-ÿ])', $this->data[$fieldName])) {
                     array_push($errors, 'letter');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['letter']);
                     //add a message to array
                     array_push($errorMessages, ['letter' => $msg]);
                 }
             }
         }
         if ($en) {
             //if exists rule for the field
             if (in_array('en', $this->rules[$fieldName])) {
                 //if the field is not empty, check the rules
                 if (!empty($this->data[$fieldName]) && preg_match('([^A-z])', $this->data[$fieldName])) {
                     array_push($errors, 'en');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['en']);
                     //add a message to array
                     array_push($errorMessages, ['en' => $msg]);
                 }
             }
         }
         if ($ru) {
             //if exists rule for the field
             if (in_array('ru', $this->rules[$fieldName])) {
                 if (!empty($this->data[$fieldName]) && preg_match('([^À-ÿ])', $this->data[$fieldName])) {
                     array_push($errors, 'ru');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['ru']);
                     //add a message to array
                     array_push($errorMessages, ['ru' => $msg]);
                 }
             }
         }
         if ($decimal) {
             //if exists rule for the field
             if (in_array('decimal', $this->rules[$fieldName])) {
                 //if value is not empty, execute the rule
                 if (!empty($this->data[$fieldName]) && !preg_match('(^[\\-+]?[0-9]+\\.[0-9]+$)', $this->data[$fieldName])) {
                     array_push($errors, 'decimal');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['decimal']);
                     //add a message to array
                     array_push($errorMessages, ['decimal' => $msg]);
                 }
             }
         }
         if ($integer) {
             //if exists rule for the field
             if (in_array('integer', $this->rules[$fieldName])) {
                 //if value is not empty, execute the rule
                 if (!empty($this->data[$fieldName]) && !preg_match('(^[\\-+]?[0-9]+$)', $this->data[$fieldName])) {
                     array_push($errors, 'integer');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['integer']);
                     //add a message to array
                     array_push($errorMessages, ['integer' => $msg]);
                 }
             }
         }
         if ($lower) {
             //if exists rule for the field
             if (in_array('lower', $this->rules[$fieldName])) {
                 //if value is not empty, execute the rule
                 if (!empty($this->data[$fieldName]) && preg_match('([^a-zà-ÿ])', $this->data[$fieldName])) {
                     array_push($errors, 'lower');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['lower']);
                     //add a message to array
                     array_push($errorMessages, ['lower' => $msg]);
                 }
             }
         }
         if ($upper) {
             //if exists rule for the field
             if (in_array('upper', $this->rules[$fieldName])) {
                 //if value is not empty, execute the rule
                 if (!empty($this->data[$fieldName]) && preg_match('([^A-ZÀ-ß])', $this->data[$fieldName])) {
                     array_push($errors, 'upper');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['lower']);
                     //add a message to array
                     array_push($errorMessages, ['lower' => $msg]);
                 }
             }
         }
         if (isset($repeat)) {
             if (in_array('repeat' . "[{$repeat}]", $this->rules[$fieldName])) {
                 if ($this->data[$fieldName] !== $this->data[$repeat]) {
                     array_push($errors, 'repeat');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/', '/{repeat}/'], [$fieldTitle, $repeat], $message['repeat']);
                     //add a message to array
                     array_push($errorMessages, ['repeat' => $msg]);
                 }
                 unset($repeat);
             }
         }
         if (isset($unique)) {
             //getting of the table and field name
             $tblData = explode('.', $unique);
             list($table, $field) = $tblData;
             $fldValue = $this->data[$fieldName];
             //if the field value is not empty
             if (!empty($fldValue)) {
                 $connection = Connection::instance()->open();
                 $execute = $connection->query("SELECT * FROM `{$table}` WHERE `{$field}` = '{$fldValue}'");
                 if ($execute->num_rows > 0) {
                     array_push($errors, 'unique');
                     $fieldTitle = $this->setFieldName($fieldName);
                     $msg = preg_replace(['/{field}/'], [$fieldTitle], $message['unique']);
                     //add a message to array
                     array_push($errorMessages, ['unique' => $msg]);
                 }
             }
             //clear variable $unique
             unset($unique);
         }
         //a validation of the user data during authorization
         if (isset($auth)) {
             if (!$this->auth($auth)) {
                 array_push($errors, 'auth');
                 $msg = preg_replace(['/{field}/'], [$auth], $message['auth']);
                 //add a message to array
                 array_push($errorMessages, ['auth' => $msg]);
             }
         }
         //if the errors array is not empty
         if (!empty($errors)) {
             $this->errors[$fieldName] = $errors;
             $this->errorMessages[$fieldName] = $errorMessages;
         }
     }
     //if there are errors, return false
     if (!empty($this->errors) && !empty($this->data)) {
         return false;
     } elseif (empty($this->errors) && !empty($this->data)) {
         $this->prepareData($this->data);
         //if there are no errors, return true
         return true;
     }
 }