/** * 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; }
/** * Description: Setting and render of the layout file * Example: View::instance('viewFile', $data)->layout('layoutFile'); * @param null $file * @throws ConfigException * @throws ViewException */ public function layout($file = null) { //if the layout file is not specified if (is_null($file)) { if (!($path = File::find(APPDIR . 'views', Config::get('layoutFile')))) { throw new ViewException('The layout file ' . Config::get('layoutFile') . ' is not found'); } extract($this->data); ob_start(); require APPDIR . 'views' . DIRECTORY_SEPARATOR . $this->view . EXT; $content = ob_get_clean(); require $path; } else { //if the layout file is specified if (!($path = File::find('views' . DIRECTORY_SEPARATOR . 'layouts', $file))) { throw new ViewException('The layout file ' . $file . ' is not found'); } extract($this->data); ob_start(); //the connection of the view file require $this->view; $content = ob_get_clean(); //the connection of the layout file require $path; } }
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); }
/** * 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; }
/** * Description: return the html a tag * Example: HTML::a(['link' => 'site.ru', 'title' => 'link', 'class' => 'some-class']); * @param array $config * @return the generated HTML a tag * available params: title, class, id, name, link * @throws HelperException */ public static function a(array $config) { if (!is_array($config)) { throw new HelperException('The config param must be array'); } $title = self::setAttribute($config, 'title'); $class = self::setAttribute($config, 'class'); $id = self::setAttribute($config, 'id'); $tag = "<a href=\"" . Config::get('baseUrl') . $config['link'] . "\" {$title} {$class} {$id}>{$config['name']}</a>"; return $tag; }
/** * Description: The verification of the session existence * Example: Identity::check(['off' => ['view' => '404'], 'stop' => true]); * Identity::check(['off' => ['url' => 'http://www.site.ru']]); * Available params: on - if exists a session * off - if not exists a session * view - a view file * url - URL for a redirection * stop - a stop of the script * @param array $params */ public static function check(array $params = null) { if (is_null($params)) { $params = Config::get('identityParams'); } //if the session exists, if (Session::exists()) { //the execution of the rules self::executeRules('on', $params); } else { //the execution of the rules self::executeRules('off', $params); } }
/** * Description: the data preparation after the check * @param array $data * @throws ConfigException */ private function prepareData(array $data) { foreach ($data as $fieldName => $fieldVal) { //the data encryption if (in_array('crypt', $this->rules[$fieldName])) { $this->preparedData[$fieldName] = crypt($fieldVal, Config::get('salt')); } //repeatable field is not added to the array if ($fieldName == $this->repeatedFields) { continue; } $this->preparedData[$fieldName] = in_array('crypt', $this->rules[$fieldName]) ? crypt(trim($fieldVal), Config::get('salt')) : trim($fieldVal); } $this->repeatedFields = null; }