/** * 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(); }
public function providerGetFiles() { $systemPath = realpath(__DIR__ . '/../../../../../'); $file = 'src/zibo/core/filesystem/GenericFileBrowser.php'; $resultFile = new File($systemPath, $file); return array(array(array(), 'unexistantFile'), array(array($resultFile->getAbsolutePath() => $resultFile), $file)); }
/** * Process the upload and update the value of this field * @return null */ public function processRequest() { if (!isset($_FILES[$this->name])) { $this->setValue($this->getDefaultValue()); return; } $this->uploadPath->create(); if (!$this->uploadPath->isWritable()) { throw new ZiboException('Upload path ' . $this->uploadPath->getAbsolutePath() . ' is not writable'); } if (!$this->isMultiple()) { $formFile = $this->uploadFile($_FILES[$this->name]); $this->setValue($formFile); return; } $values = array(); $files = $this->getMultipleFiles($_FILES[$this->name]); foreach ($files as $file) { if ($file['error'] == UPLOAD_ERR_NO_FILE) { continue; } $formFile = $this->uploadFile($file); $values[] = $formFile; } $this->setValue($values); }
public function providerGetFiles() { $systemPath = realpath(dirname(__FILE__) . '/../../../../../../'); $file = 'test/src/zibo/library/filesystem/browser/GenericBrowserTest.php'; $resultFile = new File($systemPath, $file); return array(array(array(), 'unexistantFile'), array(array($resultFile->getAbsolutePath() => $resultFile), $file)); }
/** * Connects this connection * @return null * @throws zibo\library\database\mysql\exception\MysqlException when no connection could be made with the host * @throws zibo\library\database\mysql\exception\MysqlException when the database could not be selected */ public function connect() { $file = new File($this->dsn->getPath()); if (!$file->exists()) { $parent = $file->getParent(); $parent->create(); } try { $this->connection = new SQLite3($file->getAbsolutePath()); } catch (Exception $exception) { $this->connection = null; throw new SqliteException($exception->getMessage(), 0, $exception); } }
/** * Get the relative file in the Zibo file structure for a given absolute file. * @param zibo\library\filesystem\File $file absolute file to get the relative file from * @return zibo\library\filesystem\File relative file in the Zibo file structure if located in the root of the Zibo installation * @throws zibo\ZiboException when the provided file is not in the root path * @throws zibo\ZiboException when the provided file is part of the Zibo file system structure */ public function getRelativeFile(File $file) { $absoluteFile = $file->getAbsolutePath(); $rootPath = $this->getRootPath(); $isPhar = $file->hasPharProtocol(); $file = str_replace($rootPath->getPath() . File::DIRECTORY_SEPARATOR, '', $absoluteFile); if ($file == $absoluteFile) { throw new ZiboException($file . ' is not in the root path'); } if ($isPhar) { $file = substr($file, 7); } $tokens = explode(File::DIRECTORY_SEPARATOR, $file); $token = array_shift($tokens); if ($token == self::DIRECTORY_APPLICATION || $token == self::DIRECTORY_SYSTEM) { $token = array_pop($tokens); return new File(implode(File::DIRECTORY_SEPARATOR, $tokens), $token); } if ($token !== self::DIRECTORY_MODULES || count($tokens) < 2) { throw new ZiboException($file . ' is not in the Zibo file system structure (' . $token . ')'); } array_shift($tokens); $token = array_pop($tokens); return new File(implode(File::DIRECTORY_SEPARATOR, $tokens), $token); }
/** * Gets the base64 encoded string of the contents of the provided file * @param zibo\library\filesystem\File $file The file to get the base64 string of * @return string * @throws zibo\library\filesystem\exception\FileSystemException when the file could not be read */ private function getBase64FromFile(File $file) { $path = $file->getAbsolutePath(); $contents = file_get_contents($path); if ($contents === false) { $error = error_get_last(); throw new FileSystemException('Could not read ' . $path . ': ' . $error['message']); } return base64_encode($contents); }
/** * Uncompresses the archive to the provided destination * @param zibo\library\filesystem\File $destination Destination of the uncompressed files * @return null */ public function uncompress(File $destination) { $path = $this->file->getAbsolutePath(); $zip = new ZipArchive(); if ($zip->open($path) !== true) { throw new ArchiveException('Could not open ' . $path); } $destination->create(); $zip->extractTo($destination->getAbsolutePath()); $zip->close(); }
/** * Gets all the files from the user directory * @param zibo\library\security\SecurityManager $securityManager The security manager * @param array $filters Filters for the file browser when reading the contents of the user directory * @return array Array with the URL of the file as key and the label as value */ private function getTinyMCEFiles(SecurityManager $securityManager, array $filters = array()) { // read the files $userDirectory = $this->getUserDirectory(); $fileBrowser = new FileBrowser($userDirectory); $files = $fileBrowser->readDirectory(null, $filters, true); // initializes variables needed to generate the tinymce list $zibo = Zibo::getInstance(); $rootPath = $zibo->getRootPath(); $applicationPath = new File($rootPath, Zibo::DIRECTORY_APPLICATION); $applicationPath = $applicationPath->getAbsolutePath(); $request = $zibo->getRequest(); $baseUrl = $request->getBaseUrl(); // generate the tinymce list $list = array(); foreach ($files as $path => $file) { $file = new File($userDirectory, $file); $link = str_replace($applicationPath, $baseUrl, $file->getAbsolutePath()); if (str_pos($baseUrl, $link) === false) { // the file is not in the root of the installation, skip it continue; } $label = $fileBrowser->getPath($file); $list[$link] = $label; } // we're done here return $list; }
/** * 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 rename a file or directory * * Every argument to this method is a part of the rename path. eg. * $fb->renameAction('application', 'data', 'test.txt') would rename to application/data/test.txt * @return null */ public function renameAction() { $pieces = func_get_args(); $file = $this->getFileFromPieces($pieces, false); if (!$file) { $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); } else { $absoluteFile = new File($this->fileBrowser->getRoot(), $file); if (!$absoluteFile->exists()) { $this->addError(self::TRANSLATION_ERROR_EXIST_NOT, array('path' => $file)); $this->response->setStatusCode(Response::STATUS_CODE_NOT_FOUND); } } if ($this->response->getStatusCode() == Response::STATUS_CODE_NOT_FOUND) { $this->response->setView(new BaseView()); return; } $basePath = $this->request->getBasePath(); $parent = $absoluteFile->getParent(); $redirectUrl = $basePath . '/' . self::ACTION_PATH . '/' . $this->fileBrowser->getPath($parent, false); $form = new RenameForm($basePath . '/' . self::ACTION_RENAME . '/' . $file, $file); if ($form->isSubmitted()) { if ($form->isCancelled()) { $this->response->setRedirect($redirectUrl); return; } try { $form->validate(); $name = $form->getFileName(); $name = String::safeString($name); $destination = new File($parent, $name); if ($destination->getAbsolutePath() != $absoluteFile->getPath() && $destination->exists()) { $error = new ValidationError(self::TRANSLATION_ERROR_EXIST, '%path% exists already', array('path' => $this->fileBrowser->getPath($destination, false))); $exception = new ValidationException(); $exception->addErrors(RenameForm::FIELD_NAME, array($error)); throw $exception; } $absoluteFile->move($destination); $this->addInformation(self::TRANSLATION_INFORMATION_RENAMED, array('old' => $file->getName(), 'new' => $name)); $this->response->setRedirect($redirectUrl); return; } catch (ValidationException $exception) { $form->setValidationException($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 (!$absoluteFile->isWritable() || !$absoluteFile->getParent()->isWritable()) { $this->addWarning(self::TRANSLATION_ERROR_WRITABLE, array('path' => $file)); $form->setIsDisabled(true, RenameForm::BUTTON_SUBMIT); } $view = new RenameView($form, $file); $view->setPageTitle(Module::TRANSLATION_FILE_BROWSER, true); $this->response->setView($view); }
/** * Copy a directory to another directory * @param File $source * @param File $destination * @return null * @throws zibo\library\filesystem\exception\FileSystemException when the source could not be read */ private function copyDirectory(File $source, File $destination) { $sourcePath = $source->getAbsolutePath(); if (!($handle = opendir($sourcePath))) { throw new FileSystemException('Could not read ' . $sourcePath); } $files = false; while (($file = readdir($handle)) !== false) { if ($file != '.' && $file != '..') { $files = true; $this->copy(new File($source, $file), new File($destination, $file)); } } if (!$files) { // since copying an empty directory does nothing, we create the destination $this->create($destination); } }
/** * Gets the relative path of the provided file to the root path of the browser * @param zibo\library\filesystem\File $file File to get the path for * @param boolean $addRoot Set to false if the root of the browser is already added to the provided file * @return zibo\library\filesystem\File The relative path of the provided file */ public function getPath(File $file = null, $addRoot = true) { if (!$file) { return new File(self::DEFAULT_PATH); } if ($addRoot) { $file = new File($this->root, $file); } $fileAbsolutePath = $file->getAbsolutePath(); $file = str_replace($this->rootAbsolutePath, '', $fileAbsolutePath); if (!$file) { $file = self::DEFAULT_PATH; } else { if ($file[0] === File::DIRECTORY_SEPARATOR) { $file = substr($file, 1); } } return new File($file); }
/** * Copies a file into the modules directory * @param zibo\library\filesystem\File $file * @return zibo\library\filesystem\File the destination file */ private function copyFile(File $file) { if ($file->getExtension() == 'phar' && class_exists(self::CLASS_ARCHIVE_FACTORY)) { $module = new File(Zibo::DIRECTORY_MODULES, substr($file->getName(), 0, -5)); $archive = ArchiveFactory::getInstance()->getArchive($file); $archive->uncompress($module); } else { $module = new File(Zibo::DIRECTORY_MODULES, $file->getName()); if ($module->getAbsolutePath() != $file->getAbsolutePath()) { $file->copy($module); } } return $module; }
/** * Send the generated workbook to the browser * @param string filename filename proposed to the user * @return null */ public function send($filename, $type = 'Excel5') { header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Cache-Control: max-age=0'); $writer = PHPExcel_IOFactory::createWriter($this->workbook, $type); if (method_exists($writer, 'setTempDir')) { $tempDir = new File(self::DIRECTORY_TEMP); $writer->setTempDir($tempDir->getAbsolutePath()); } $writer->save('php://output'); }
/** * Exports the database to a file * @param zibo\library\filesystem\File $file * @return null * @throws zibo\ZiboException when an error eoccured */ public function export(File $file) { $extension = $file->getExtension(); if ($extension != 'sql') { throw new MysqlException('Provided file needs to have an sql extension'); } $dsn = $this->getDsn(); $username = $dsn->getUsername(); $password = $dsn->getPassword(); $database = $dsn->getDatabase(); $command = 'mysqldump --user='******' --password='******' ' . $database; $command .= ' > ' . $file->getAbsolutePath(); System::execute($command); }