/** * Sets the root path * @param File $rootPath The root path * @throws FileSystemException */ private function setRootPath(File $rootPath) { if (!$rootPath->isDirectory()) { throw new FileSystemException('Could not build the index: ' . $this->rootPath->getPath() . ' is not a directory.'); } $this->rootPath = $rootPath; }
/** * Constructs a new file view * @param zibo\library\filesystem\File $file File to render * @return null */ public function __construct(File $file) { if (!$file->exists() || $file->isDirectory()) { throw new ZiboException($file . ' does not exists or is a directory.'); } $this->file = $file; }
/** * Checks if the provided file is allowed by this filter * @param zibo\library\filesystem\File $file File to check * @return boolean True if the file is allowed, false otherwise */ public function isAllowed(File $file) { $result = !$this->include; if ($file->isDirectory()) { $result = !$result; } return $result; }
/** * Constructs a new repository * @param zibo\library\filesystem\File $directory The directory of the repository * @return null * @throws zibo\ZiboException when the provided directory does not exist or when it's not writable */ public function __construct(File $directory) { $directory->create(); if (!$directory->isDirectory()) { throw new ZiboException($directory->getAbsolutePath() . ' is not a directory'); } if (!$directory->isWritable()) { throw new ZiboException($directory->getAbsolutePath() . ' is not writable'); } $this->directory = $directory; $this->isIndexDirty = false; $this->readIndexFile(); }
/** * Decorates the cell with the path of the file * @param zibo\library\html\table\Cell $cell Cell to decorate * @param zibo\library\html\table\Row $row Row of the cell to decorate * @param integer $rowNumber Number of the current row * @param array $remainingValues Array containing the values of the remaining rows of the table * @return null */ public function decorate(Cell $cell, Row $row, $rowNumber, array $remainingValues) { $file = $cell->getValue(); $absoluteFile = new File($this->root, $file); if (!$absoluteFile->exists()) { $cell->setValue('---'); return; } if ($absoluteFile->isDirectory()) { $class = self::CLASS_DIRECTORY; } else { $class = self::CLASS_FILE; } $html = $this->getNameHtml($file->getPath(), $class); $cell->setValue($html); }
/** * Compresses a file into the archive * @param \Phar $archive Phar object of PHP * @param zibo\library\filesystem\File $file The file to compress in the archive * @param zibo\library\filesystem\File $prefix The path for the file in the archive * @return null */ private function compressFile(PhpPhar $archive, File $file, File $prefix = null) { if ($prefix == null) { $prefix = new File($file->getName()); } else { $prefix = new File($prefix, $file->getName()); } if ($file->exists()) { if ($file->isDirectory()) { $this->compressDirectory($archive, $file, $prefix); } else { $archive->addFile($file->getAbsolutePath(), $prefix->getPath()); } } else { throw new ArchiveException("Source does not exist: {$file}"); } }
/** * Constructs a new file view * @param zibo\library\filesystem\File $file File to render * @return null */ public function __construct(File $file) { $response = Zibo::getInstance()->getResponse(); if (!$file->exists() || $file->isDirectory()) { $response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); return; } $maxAge = 60 * 60; $mime = Mime::getMimeType($file); $lastModified = gmdate('r', $file->getModificationTime()); $expires = gmdate('r', time() + $maxAge); $response->setHeader('Pragma', 'public'); $response->setHeader('Cache-Control', 'max-age=' . $maxAge); $response->setHeader('Expires', $expires); $response->setHeader('Last-Modified', $lastModified); $response->setHeader('ETag', md5($lastModified)); $response->setHeader('Content-Type', $mime); $response->setHeader('Content-Length', $file->getSize()); $this->file = $file; }
/** * Compresses a file into the archive * @param ZipArchive $archive ZipArchive object of PHP * @param zibo\library\filesystem\File $file The file to compress in the archive * @param zibo\library\filesystem\File $prefix The path for the file in the archive * @return null */ private function compressFile(ZipArchive $archive, File $file, File $prefix = null) { if ($prefix == null) { $prefix = new File($file->getName()); } else { $prefix = new File($prefix, $file->getName()); } $children = null; if ($file->exists()) { if ($file->isDirectory()) { $children = $file->read(); } else { $archive->addFile($file->getPath(), $prefix->getPath()); return; } } if (empty($children)) { $archive->addEmptyDir($prefix->getPath()); } else { foreach ($children as $file) { $this->compressFile($archive, $file, $prefix); } } }
/** * Uploads a file to the FTP server * @param zibo\library\filesystem\File $localFile The file to upload * @param string $remoteFile The destination file on the FTP server * @param integer $mode ASCII or binary (constants FTP_ASCII or FTP_BINARY) * @return null * @throws zibo\library\ftp\exception\FtpException when not connected to the FTP server * @throws zibo\library\ftp\exception\FtpException when the file could not be uploaded */ public function put(File $localFile, $remoteFile, $mode = null) { $this->checkConnected(); if (!$localFile->exists()) { throw new FtpException('Could not upload ' . $localFile->getName() . ': Source does not exist'); } elseif ($localFile->isDirectory()) { throw new FtpException('Could not upload ' . $localFile->getName() . ': Source is a directory'); } if (!$mode) { $mode = FTP_BINARY; } if (!@ftp_put($this->handle, $remoteFile, $localFile->getAbsolutePath(), $mode)) { throw new FtpException('Could not upload ' . $localFile->getName() . ': A problem occured while uploading'); } }
/** * Action to edit or create a file * * Every argument to this method is a part of the file to edit. eg. * $fb->editAction('application', 'data', 'test.txt') would show the editor for application/data/test.txt * * To create a new file in a directory, give the arguments to a directory instead of a file. * @return null */ public function editAction() { $pieces = func_get_args(); $path = $this->getFileFromPieces($pieces, false); $absolutePath = new File($this->fileBrowser->getRoot(), $path); $basePath = $this->request->getBasePath() . '/'; $saveAction = $basePath . self::ACTION_EDIT . ($path ? '/' . $path : ''); $name = null; $content = null; if ($path) { if (!$absolutePath->exists()) { $this->addError(self::TRANSLATION_ERROR_EXIST_NOT, array('path' => $this->fileBrowser->getPath($path))); $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); $this->response->setView(new BaseView()); return; } if (!$absolutePath->isDirectory()) { $name = $absolutePath->getName(); $path = $absolutePath->getParent(); $content = $absolutePath->read(); } else { $path = $absolutePath; } } else { $path = $absolutePath; } $isWritable = $absolutePath->isWritable(); $path = $this->fileBrowser->getPath($path, false); $form = new EditorForm($saveAction, $path, $name, $content); if ($form->isSubmitted()) { $path = $path->getPath(); $redirectUrl = $basePath . self::ACTION_PATH . ($path != '.' ? '/' . $path : ''); if ($form->isCancelled()) { $this->response->setRedirect($redirectUrl); return; } try { $form->validate(); $content = $form->getFileContent(); $name = $form->getFileName(); $path = $form->getFilePath(); $file = new File($this->fileBrowser->getRoot(), $path . '/' . $name); if ($file->isWritable()) { $file->write($content); $this->addInformation(self::TRANSLATION_INFORMATION_SAVED, array('path' => $this->fileBrowser->getPath($file, false))); $this->response->setRedirect($redirectUrl); } else { $this->addError(self::TRANSLATION_ERROR_WRITABLE, array('path' => $this->fileBrowser->getPath($file, false))); $form->setIsDisabled(true, EditorForm::BUTTON_SUBMIT); $isWritable = true; } } catch (ValidationException $exception) { } catch (Exception $exception) { Zibo::getInstance()->runEvent(Zibo::EVENT_LOG, $exception->getMessage(), $exception->getTraceAsString(), 1); $this->addError(self::TRANSLATION_ERROR, array('error' => $exception->getMessage())); $this->response->setStatusCode(Response::STATUS_CODE_SERVER_ERROR); } } if (!$isWritable) { $form->setIsDisabled(true, EditorForm::BUTTON_SUBMIT); $this->addWarning(self::TRANSLATION_ERROR_WRITABLE, array('path' => $path . ($name ? '/' . $name : ''))); } $view = new EditorView($form, $path); $view->setPageTitle(Module::TRANSLATION_FILE_BROWSER, true); $this->response->setView($view); }
/** * Read a directory * @param File $dir directory to read * @param boolean $recursive true to read the subdirectories, false (default) to only read the given directory * @return array Array with a File object as value and it's path as key * @throws zibo\library\filesystem\exception\FileSystemException when the directory could not be read */ private function readDirectory(File $dir, $recursive = false) { $path = $this->getAbsolutePath($dir); if ($dir->isPhar() && !$dir->hasPharProtocol($path)) { $path = 'phar://' . $path; } if (!($handle = @opendir($path))) { throw new FileSystemException('Could not read ' . $path); } $files = array(); while (($f = readdir($handle)) !== false) { if (in_array($f, $this->ignoredFileNames)) { continue; } $file = new File($dir, $f); $files[$file->getPath()] = $file; if ($recursive && $file->isDirectory()) { $tmp = $this->readDirectory($file, true); foreach ($tmp as $k => $v) { $files[$k] = $v; } } } return $files; }
/** * Checks if the provided path is a directory in the root of this browser * @param zibo\library\filesystem\File $path Path to check * @return boolean True if the path is a directory in the root of this browser, false otherwise */ public function isDirectory(File $path) { $path = new File($this->root, $path); return $path->isDirectory(); }
/** * Get the available themes based on the existance of the theme directory * @param string $themesDirectory name of the theme directory in the view directory (optional) * @return array Array with the names of the available themes */ public static function getThemes($themesDirectory = null) { if ($themesDirectory === null) { $themesDirectory = self::DEFAULT_THEMES_DIRECTORY; } $themes = array(); $includePaths = Zibo::getInstance()->getIncludePaths(); $viewPath = new File(Zibo::DIRECTORY_VIEW, $themesDirectory); foreach ($includePaths as $includePath) { $themesPath = new File($includePath, $viewPath); if ($themesPath->exists() && $themesPath->isDirectory() && $themesPath->isReadable()) { $themesFiles = $themesPath->read(); foreach ($themesFiles as $themesFile) { if ($themesFile->isDirectory() && $themesFile->isReadable()) { $name = $themesFile->getName(); $themes[$name] = $name; } } } } return $themes; }