/** * Moves an uploaded file to a destination folder * * @param string $src The name of the php (temporary) uploaded file * @param string $dest The path (including filename) to move the uploaded file to * @param boolean $use_streams True to use streams * * @return boolean True on success * * @since 11.1 */ public static function upload($src, $dest, $use_streams = false) { // Ensure that the path is valid and clean $dest = Path::clean($dest); // Create the destination directory if it does not exist $baseDir = dirname($dest); if (!file_exists($baseDir)) { Folder::create($baseDir); } if ($use_streams) { $stream = Factory::getStream(); if (!$stream->upload($src, $dest)) { Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError()), Log::WARNING, 'jerror'); return false; } return true; } else { $FTPOptions = ClientHelper::getCredentials('ftp'); $ret = false; if ($FTPOptions['enabled'] == 1) { // Connect the FTP client $ftp = ClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']); // Translate path for the FTP account $dest = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/'); // Copy the file to the destination directory if (is_uploaded_file($src) && $ftp->store($src, $dest)) { unlink($src); $ret = true; } else { Log::add(Text::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR02'), Log::WARNING, 'jerror'); } } else { if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors if (Path::setPermissions($dest)) { $ret = true; } else { Log::add(Text::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR01'), Log::WARNING, 'jerror'); } } else { Log::add(Text::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR02'), Log::WARNING, 'jerror'); } } return $ret; } }
/** * Moves a folder. * * @param string $src The path to the source folder. * @param string $dest The path to the destination folder. * @param string $path An optional base path to prefix to the file names. * @param boolean $use_streams Optionally use streams. * * @return mixed Error message on false or boolean true on success. * * @since 11.1 */ public static function move($src, $dest, $path = '', $use_streams = false) { $FTPOptions = ClientHelper::getCredentials('ftp'); if ($path) { $src = Path::clean($path . '/' . $src); $dest = Path::clean($path . '/' . $dest); } if (!self::exists($src)) { return Text::_('JLIB_FILESYSTEM_ERROR_FIND_SOURCE_FOLDER'); } if (self::exists($dest)) { return Text::_('JLIB_FILESYSTEM_ERROR_FOLDER_EXISTS'); } if ($use_streams) { $stream = Factory::getStream(); if (!$stream->move($src, $dest)) { return Text::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_RENAME', $stream->getError()); } $ret = true; } else { if ($FTPOptions['enabled'] == 1) { // Connect the FTP client $ftp = Ftp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']); // Translate path for the FTP account $src = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/'); $dest = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/'); // Use FTP rename to simulate move if (!$ftp->rename($src, $dest)) { return Text::_('Rename failed'); } $ret = true; } else { if (!@rename($src, $dest)) { return Text::_('Rename failed'); } $ret = true; } } return $ret; }
/** * Extract a Bzip2 compressed file to a given path * * @param string $archive Path to Bzip2 archive to extract * @param string $destination Path to extract archive to * @param array $options Extraction options [unused] * * @return boolean True if successful * * @since 11.1 * @throws RuntimeException */ public function extract($archive, $destination, array $options = array()) { $this->_data = null; if (!extension_loaded('bz2')) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'The bz2 extension is not available.'); } else { throw new RuntimeException('The bz2 extension is not available.'); } } if (!isset($options['use_streams']) || $options['use_streams'] == false) { // Old style: read the whole file and then parse it $this->_data = file_get_contents($archive); if (!$this->_data) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive'); } else { throw new RuntimeException('Unable to read archive'); } } $buffer = bzdecompress($this->_data); unset($this->_data); if (empty($buffer)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to decompress data'); } else { throw new RuntimeException('Unable to decompress data'); } } if (File::write($destination, $buffer) === false) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive'); } else { throw new RuntimeException('Unable to write archive'); } } } else { // New style! streams! $input = Factory::getStream(); // Use bzip $input->set('processingmethod', 'bz'); if (!$input->open($archive)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive (bz2)'); } else { throw new RuntimeException('Unable to read archive (bz2)'); } } $output = Factory::getStream(); if (!$output->open($destination, 'w')) { $input->close(); if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive (bz2)'); } else { throw new RuntimeException('Unable to write archive (bz2)'); } } do { $this->_data = $input->read($input->get('chunksize', 8196)); if ($this->_data) { if (!$output->write($this->_data)) { $input->close(); if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive (bz2)'); } else { throw new RuntimeException('Unable to write archive (bz2)'); } } } } while ($this->_data); $output->close(); $input->close(); } return true; }
/** * Extract a Gzip compressed file to a given path * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive to * @param array $options Extraction options [unused] * * @return boolean True if successful * * @since 11.1 * @throws RuntimeException */ public function extract($archive, $destination, array $options = array()) { $this->_data = null; if (!extension_loaded('zlib')) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'The zlib extension is not available.'); } else { throw new RuntimeException('The zlib extension is not available.'); } } if (!isset($options['use_streams']) || $options['use_streams'] == false) { $this->_data = file_get_contents($archive); if (!$this->_data) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive'); } else { throw new RuntimeException('Unable to read archive'); } } $position = $this->_getFilePosition(); $buffer = gzinflate(substr($this->_data, $position, strlen($this->_data) - $position)); if (empty($buffer)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to decompress data'); } else { throw new RuntimeException('Unable to decompress data'); } } if (File::write($destination, $buffer) === false) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive'); } else { throw new RuntimeException('Unable to write archive'); } } } else { // New style! streams! $input = Factory::getStream(); // Use gz $input->set('processingmethod', 'gz'); if (!$input->open($archive)) { if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to read archive (gz)'); } else { throw new RuntimeException('Unable to read archive (gz)'); } } $output = Factory::getStream(); if (!$output->open($destination, 'w')) { $input->close(); if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write archive (gz)'); } else { throw new RuntimeException('Unable to write archive (gz)'); } } do { $this->_data = $input->read($input->get('chunksize', 8196)); if ($this->_data) { if (!$output->write($this->_data)) { $input->close(); if (class_exists('\\JError')) { return JError::raiseWarning(100, 'Unable to write file (gz)'); } else { throw new RuntimeException('Unable to write file (gz)'); } } } } while ($this->_data); $output->close(); $input->close(); } return true; }