/** * Upload files from ckeditor * @param string $type * @return string * @throws NativeException * @throws \Ffcms\Core\Exception\SyntaxException */ public function actionUpload($type) { /** @var $loadFile \Symfony\Component\HttpFoundation\File\UploadedFile */ $loadFile = App::$Request->files->get('upload'); if ($loadFile === null || $loadFile->getError() !== 0) { return $this->errorResponse(__('File upload failed')); } // get file extension $fileExt = '.' . $loadFile->guessExtension(); // check if this request type is allowed if ($this->allowedExt[$type] === null || !Obj::isArray($this->allowedExt[$type])) { throw new NativeException('Hack attempt'); } // check if this file extension is allowed to upload if (!Arr::in($fileExt, $this->allowedExt[$type])) { return $this->errorResponse(__('This file type is not allowed to upload')); } $date = Date::convertToDatetime(time(), 'd-m-Y'); // create file hash based on name-size $fileNewName = App::$Security->simpleHash($loadFile->getFilename() . $loadFile->getSize()) . $fileExt; $savePath = Normalize::diskFullPath('/upload/' . $type . '/' . $date); // save file from tmp to regular $loadFile->move($savePath, $fileNewName); // generate URI of uploaded file $url = '/upload/' . $type . '/' . $date . '/' . $fileNewName; return App::$View->render('editor/load_success', ['callbackId' => (int) App::$Request->query->get('CKEditorFuncNum'), 'url' => $url], __DIR__); }
/** * Get directory total size (in bytes) * @param string $path * @return int */ public static function getSize($path) { $path = Normalize::diskFullPath($path); $size = 0; foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file) { if ($file->getFileName() !== '..') { $size += $file->getSize(); } } return $size; }
/** * Try to find exist viewer full path * @param string $path * @param string|null $source * @return null|string * @throws NativeException */ private function findViewer($path, $source = null) { $tmpPath = null; // sounds like a relative path for current view theme if (Str::contains('/', $path)) { // lets try to get full path for current theme $tmpPath = $path; if (!Str::startsWith($this->themePath, $path)) { $tmpPath = Normalize::diskPath($this->themePath . '/' . $path . '.php'); } } else { // sounds like a object-depended view call from controller or etc // get stack trace of callbacks $calledLog = debug_backtrace(); $calledController = null; // lets try to find controller in backtrace foreach ($calledLog as $caller) { if (isset($caller['class']) && Str::startsWith('Apps\\Controller\\', $caller['class'])) { $calledController = (string) $caller['class']; } } // depended controller is not founded? Let finish if ($calledController === null) { throw new NativeException('View render is failed: callback controller not founded! Call with relative path: ' . $path); } // get controller name $controllerName = Str::sub($calledController, Str::length('Apps\\Controller\\' . env_name . '\\')); $controllerName = Str::lowerCase($controllerName); // get full path $tmpPath = $this->themePath . DIRECTORY_SEPARATOR . $controllerName . DIRECTORY_SEPARATOR . $path . '.php'; } // check if builded view full path is exist if (File::exist($tmpPath)) { return $tmpPath; } // hmm, not founded. Lets try to find in caller directory (for widgets, apps packages and other) if ($source !== null) { $tmpPath = Normalize::diskPath($source . DIRECTORY_SEPARATOR . $path . '.php'); if (File::exist($tmpPath)) { // add notify for native views if (App::$Debug !== null) { App::$Debug->addMessage('Render native viewer: ' . Str::replace(root, null, $tmpPath), 'info'); } return $tmpPath; } } if (App::$Debug !== null) { App::$Debug->addMessage('Viewer not founded on rendering: ' . $path, 'warning'); } return null; }
/** * Append translation data from exist full path * @param string $path * @return bool */ public function append($path) { $path = Normalize::diskFullPath($path); // check if file exist if (!File::exist($path)) { return false; } // load file translations $addTranslation = (require $path); if (!Obj::isArray($addTranslation)) { return false; } // merge data $this->cached = Arr::merge($this->cached, $addTranslation); return true; }
/** * Save input data to database */ public function make() { // save data to db $this->_record->title = $this->title; $this->_record->text = $this->text; $this->_record->path = $this->path; $this->_record->category_id = (int) $this->categoryId; $this->_record->display = 0; // set to premoderation $this->_record->author_id = (int) $this->authorId; if ($this->_new === true) { $this->_record->comment_hash = $this->generateCommentHash(); } $this->_record->save(); // work with poster data if ($this->poster !== null) { // lets move poster from tmp to gallery $originDir = '/upload/gallery/' . $this->_record->id . '/orig/'; $thumbDir = '/upload/gallery/' . $this->_record->id . '/thumb/'; if (!Directory::exist($originDir)) { Directory::create($originDir); } if (!Directory::exist($thumbDir)) { Directory::create($thumbDir); } $fileName = App::$Security->simpleHash($this->poster->getClientOriginalName() . $this->poster->getSize()); $newFullName = $fileName . '.' . $this->poster->guessExtension(); // move poster to upload gallery directory $this->poster->move(Normalize::diskFullPath($originDir), $newFullName); // initialize image resizer $thumb = new Image(); $thumb->setCacheDir(root . '/Private/Cache/images'); // open original file, resize it and save $thumbSaveName = Normalize::diskFullPath($thumbDir) . '/' . $fileName . '.jpg'; $thumb->open(Normalize::diskFullPath($originDir) . DIRECTORY_SEPARATOR . $newFullName)->cropResize($this->_configs['galleryResize'])->save($thumbSaveName, 'jpg', 90); $thumb = null; // update poster in database $this->_record->poster = $newFullName; $this->_record->save(); } }
/** * Download file from $url and save it into $path * @param string $url * @param string $path * @return bool */ public static function saveFromUrl($url, $path) { if (!filter_var($url, FILTER_VALIDATE_URL) || !function_exists('curl_init')) { return false; } $path = Normalize::diskFullPath($path); // check if upload directory is exists $dir = dirname($path); if (!Directory::exist($dir)) { Directory::create($dir); } // initialize stream resource $stream = @fopen($path, 'w'); // initialize curl & set required options, target url, destination save stream $curl = \curl_init(); \curl_setopt($curl, CURLOPT_URL, $url); if (Str::startsWith('https', $url)) { \curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); \curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); } \curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); \curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); \curl_setopt($curl, CURLOPT_HEADER, 0); \curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322'); \curl_setopt($curl, CURLOPT_FAILONERROR, true); \curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); \curl_setopt($curl, CURLOPT_AUTOREFERER, true); \curl_setopt($curl, CURLOPT_TIMEOUT, 10); // set destination file path \curl_setopt($curl, CURLOPT_FILE, $stream); \curl_exec($curl); \curl_close($curl); fclose($stream); return true; }
/** * Show gallery images from upload directory * @param int $id * @return string * @throws NotFoundException * @throws NativeException */ public function actionGallerylist($id) { // check if id is passed if (Str::likeEmpty($id)) { throw new NativeException('Wrong input data'); } // check if user have permission to access there if (!App::$User->isAuth() || !App::$User->identity()->getRole()->can('global/file')) { throw new NativeException('Permission denied'); } $thumbDir = Normalize::diskFullPath('/upload/gallery/' . $id . '/orig/'); if (!Directory::exist($thumbDir)) { throw new NotFoundException('Nothing found'); } $files = Directory::scan($thumbDir, null, true); if ($files === false || !Obj::isArray($files) || count($files) < 1) { throw new NotFoundException('Nothing found'); } $output = []; foreach ($files as $file) { $fileExt = Str::lastIn($file, '.'); $fileName = Str::sub($file, 0, -Str::length($fileExt)); $output[] = ['thumbnailUrl' => '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg', 'url' => '/upload/gallery/' . $id . '/orig/' . $file, 'name' => $file, 'size' => File::size('/upload/gallery/' . $id . '/orig/' . $file)]; } $this->setJsonHeader(); return json_encode(['status' => 1, 'files' => $output]); }