/** * Test of procerss directory path */ public function testProcessDirectoryPath() { $this->assertEquals('home', FileManagerBaseModel::processDirectoryPath('home/')); $this->assertEquals('home', FileManagerBaseModel::processDirectoryPath('../home/')); $this->assertEquals('_home90_', FileManagerBaseModel::processDirectoryPath('@@!!...\\\\////_home90-MMMM(((**&&&_')); $this->assertEquals('home/test', FileManagerBaseModel::processDirectoryPath('../home/....&&&/test')); $this->assertEquals('', FileManagerBaseModel::processDirectoryPath('....////\\\\\\')); $this->assertEquals('', FileManagerBaseModel::processDirectoryPath('//////////////////////////////')); }
/** * Validate existing directory * * @param $value * @param array $context * @return boolean */ public function validateExistingDirectory($value, array $context = []) { // get a full path if (false !== ($fullPath = $this->model->getUserDirectory($this->path))) { if (file_exists($fullPath . '/' . $value)) { return !is_dir($fullPath . '/' . $value); } return true; } return false; }
/** * Generate a file url * * @param string $fileName * @param array $options * string path * array filters * @return string */ public function __invoke($fileName, array $options) { $currentPath = $options['path'] . '/' . $fileName; // generate a directory navigation link if (is_dir(FileManagerBaseModel::getUserBaseFilesDir() . '/' . $options['path'] . '/' . $fileName)) { // get the directory url $directoryUrl = $this->getView()->url('application/page', ['controller' => $this->getView()->applicationRoute()->getParam('controller'), 'action' => $this->getView()->applicationRoute()->getParam('action')], ['force_canonical' => true, 'query' => ['path' => $currentPath] + $options['filters']]); return $this->getView()->partial('file-manager/patrial/directory-url', ['name' => $fileName, 'url' => $directoryUrl]); } // generate a file link return $this->getView()->partial('file-manager/patrial/file-url', ['file_extension' => FileSystemUtility::getFileExtension($fileName), 'name' => $fileName, 'url' => $this->view->serverUrl() . $this->view->basePath() . '/' . FileManagerBaseModel::getUserBaseFilesUrl() . '/' . $currentPath]); }
/** * Test delete user home directory */ public function testDeleteUserHomeDirectory() { // test user data $data = ['nick_name' => Rand::getString(32), 'email' => Rand::getString(32), 'api_key' => Rand::getString(32), 'role' => AclModelBase::DEFAULT_ROLE_MEMBER, 'language' => null]; // create a test user $query = $this->userModel->insert()->into('user_list')->values($data); $statement = $this->userModel->prepareStatementForSqlObject($query); $statement->execute(); $testUserId = $this->userModel->getAdapter()->getDriver()->getLastGeneratedValue(); // create a test user's home directory $homeUserDirectory = FileManagerBaseModel::getUserBaseFilesDir($testUserId) . '/' . FileManagerBaseModel::getHomeDirectoryName(); FileSystemUtility::createDir($homeUserDirectory); // fire the delete user event UserEvent::fireUserDeleteEvent($testUserId, $data); // delete the created user $query = $this->userModel->delete()->from('user_list')->where(['user_id' => $testUserId]); $statement = $this->userModel->prepareStatementForSqlObject($query); $statement->execute(); // home directory must be deleted $this->assertFalse(file_exists($homeUserDirectory)); }
/** * Edit a file or a directory * * @retun array */ protected function editFile() { $editForm = null; $isDirectory = false; // get a path $userPath = $this->getUserPath(); $filePath = $this->getRequest()->getQuery('file_path', null); $fileName = null != $this->getRequest()->getQuery('slug', null) ? FileManagerBaseModel::slugifyFileName($this->getRequest()->getQuery('slug'), false, true) : null; // get current user directories structure $userDirectories = $this->getModel()->getUserDirectories(); // get absolute paths $userDirectory = $this->getModel()->getUserDirectory($userPath); $currentDirectory = $this->getModel()->getUserDirectory($filePath); // check the paths if (false !== $userDirectory && false !== $currentDirectory && $fileName) { // check the file name $fullFilePath = $currentDirectory . $fileName; $isDirectory = is_dir($fullFilePath); if ($fileName != '.' && $fileName != '..' && file_exists($fullFilePath)) { // get a form $editForm = $this->getServiceLocator()->get('Application\\Form\\FormManager')->getInstance('FileManager\\Form\\FileManagerEdit')->setFileName($fileName)->setFullFilePath($currentDirectory)->setFullUserPath($userDirectory)->isDirectory($isDirectory)->setUserPath($userPath); $request = $this->getRequest(); // validate the form if ($request->isPost()) { // fill the form with received values $editForm->getForm()->setData($request->getPost(), false); // save data if ($editForm->getForm()->isValid()) { // check the permission and increase permission's actions track if (true !== ($result = $this->aclCheckPermission())) { return $result; } // edit the file if (false === ($newFileName = $this->getModel()->editFile($editForm->getForm()->getData()['name'], $fullFilePath, $userDirectory, $isDirectory))) { $this->flashMessenger()->setNamespace('error')->addMessage($this->getTranslator()->translate(!$isDirectory ? 'Impossible edit selected file' : 'Impossible edit selected directory')); } else { $this->flashMessenger()->setNamespace('success')->addMessage($this->getTranslator()->translate(!$isDirectory ? 'The file has been edited' : 'The directory has been edited')); } return $this->redirectTo($this->params('controller'), 'edit', [], false, ['path' => $userPath, 'file_path' => $userPath, 'slug' => $newFileName ? $newFileName : $fileName]); } } } } return ['is_directory' => $isDirectory, 'edit_form' => $editForm ? $editForm->getForm() : null, 'path' => $userPath, 'file_path' => $filePath, 'file_name' => $fileName, 'user_directories' => $userDirectories]; }
/** * Generate a tree * * @param array|boolean $userDirectories * @param string $treeId * @param string $currentPath * @param array $filters * @param string $treeClass * @return string */ public function __invoke($userDirectories = [], $treeId, $currentPath, array $filters = [], $treeClass = 'filetree') { $currentPath = $currentPath ? FileManagerBaseModel::processDirectoryPath($currentPath) : FileManagerBaseModel::getHomeDirectoryName(); return $this->getView()->partial('file-manager/patrial/directory-tree', ['id' => $treeId, 'class' => $treeClass, 'items' => $this->processDirectories($userDirectories, $currentPath, $filters), 'cookie_lifetime' => $this->treeCookieLifetimeDays]); }
/** * Init file settings */ protected function initFileSettings() { // add descriptions params $this->formElements['name']['description'] = 'File edit name description'; $this->formElements['name']['description_params'] = [$this->maxFileNameLength]; // add extra validators for the file $this->formElements['name']['validators'] = [['name' => 'regex', 'options' => ['pattern' => '/^[' . FileManagerBaseModel::getFileNamePattern() . ']+$/', 'message' => 'You can only use the following characters: Latin, numeric, underscore, dot, dash, bracket']], ['name' => 'callback', 'options' => ['callback' => [$this, 'validateExistingFile'], 'message' => 'File already exists in selected directory']]]; $this->formElements['name']['max_length'] = (int) $this->maxFileNameLength; }
/** * Generate a base file url * * @param string $currentPath * @return string */ public function __invoke($currentPath = null) { return FileManagerBaseModel::getUserBaseFilesUrl() . '/' . ($currentPath ? $currentPath . '/' : null); }
/** * Set current path * * @param string $path * @return object fluent interface */ public function setPath($path) { $this->path = FileManagerBaseModel::processDirectoryPath($path); return $this; }