public function extract($archive, $destination, $options = array()) { // Initialise variables. $this->_data = null; if (!extension_loaded('bz2')) { $this->set('error.message', MText::_('MLIB_FILESYSTEM_BZIP_NOT_SUPPORTED')); return MError::raiseWarning(100, $this->get('error.message')); } if (!isset($options['use_streams']) || $options['use_streams'] == false) { // Old style: read the whole file and then parse it if (!($this->_data = MFile::read($archive))) { $this->set('error.message', 'Unable to read archive'); return MError::raiseWarning(100, $this->get('error.message')); } $buffer = bzdecompress($this->_data); unset($this->_data); if (empty($buffer)) { $this->set('error.message', 'Unable to decompress data'); return MError::raiseWarning(100, $this->get('error.message')); } if (MFile::write($destination, $buffer) === false) { $this->set('error.message', 'Unable to write archive'); return MError::raiseWarning(100, $this->get('error.message')); } } else { // New style! streams! $input = MFactory::getStream(); $input->set('processingmethod', 'bz'); // use bzip if (!$input->open($archive)) { $this->set('error.message', MText::_('MLIB_FILESYSTEM_BZIP_UNABLE_TO_READ')); return MError::raiseWarning(100, $this->get('error.message')); } $output = MFactory::getStream(); if (!$output->open($destination, 'w')) { $this->set('error.message', MText::_('MLIB_FILESYSTEM_BZIP_UNABLE_TO_WRITE')); $input->close(); // close the previous file return MError::raiseWarning(100, $this->get('error.message')); } do { $this->_data = $input->read($input->get('chunksize', 8196)); if ($this->_data) { if (!$output->write($this->_data)) { $this->set('error.message', MText::_('MLIB_FILESYSTEM_BZIP_UNABLE_TO_WRITE_FILE')); return MError::raiseWarning(100, $this->get('error.message')); } } } while ($this->_data); $output->close(); $input->close(); } return true; }
public static function move($src, $dest, $path = '', $use_streams = false) { // Initialise variables. $FTPOptions = MClientHelper::getCredentials('ftp'); if ($path) { $src = MPath::clean($path . '/' . $src); $dest = MPath::clean($path . '/' . $dest); } if (!self::exists($src)) { return MText::_('MLIB_FILESYSTEM_ERROR_FIND_SOURCE_FOLDER'); } if (self::exists($dest)) { return MText::_('MLIB_FILESYSTEM_ERROR_FOLDER_EXISTS'); } if ($use_streams) { $stream = MFactory::getStream(); if (!$stream->move($src, $dest)) { return MText::sprintf('MLIB_FILESYSTEM_ERROR_FOLDER_RENAME', $stream->getError()); } $ret = true; } else { if ($FTPOptions['enabled'] == 1) { // Connect the FTP client mimport('framework.client.ftp'); $ftp = MFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); //Translate path for the FTP account $src = MPath::clean(str_replace(MPATH_ROOT, $FTPOptions['root'], $src), '/'); $dest = MPath::clean(str_replace(MPATH_ROOT, $FTPOptions['root'], $dest), '/'); // Use FTP rename to simulate move if (!$ftp->rename($src, $dest)) { return MText::_('Rename failed'); } $ret = true; } else { if (!@rename($src, $dest)) { return MText::_('Rename failed'); } $ret = true; } } return $ret; }
public static function upload($src, $dest, $use_streams = false) { // Ensure that the path is valid and clean $dest = MPath::clean($dest); // Create the destination directory if it does not exist $baseDir = dirname($dest); if (!file_exists($baseDir)) { mimport('framework.filesystem.folder'); MFolder::create($baseDir); } if ($use_streams) { $stream = MFactory::getStream(); if (!$stream->upload($src, $dest)) { MError::raiseWarning(21, MText::sprintf('MLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError())); return false; } return true; } else { // Initialise variables. $FTPOptions = MClientHelper::getCredentials('ftp'); $ret = false; if ($FTPOptions['enabled'] == 1) { // Connect the FTP client mimport('framework.client.ftp'); $ftp = MFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); // Translate path for the FTP account $dest = MPath::clean(str_replace(MPATH_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 { MError::raiseWarning(21, MText::_('MLIB_FILESYSTEM_ERROR_WARNFS_ERR02')); } } else { if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors if (MPath::setPermissions($dest)) { $ret = true; } else { MError::raiseWarning(21, MText::_('MLIB_FILESYSTEM_ERROR_WARNFS_ERR01')); } } else { MError::raiseWarning(21, MText::_('MLIB_FILESYSTEM_ERROR_WARNFS_ERR02')); } } return $ret; } }