public static function getStream($use_prefix = true, $use_network = true, $ua = null, $uamask = false) { mimport('framework.filesystem.stream'); // Setup the context; $context = array(); $version = new MVersion(); // set the UA for HTTP and overwrite for FTP $context['http']['user_agent'] = $version->getUserAgent($ua, $uamask); $context['ftp']['overwrite'] = true; if ($use_prefix) { $FTPOptions = MClientHelper::getCredentials('ftp'); $SCPOptions = MClientHelper::getCredentials('scp'); if ($FTPOptions['enabled'] == 1 && $use_network) { $prefix = 'ftp://' . $FTPOptions['user'] . ':' . $FTPOptions['pass'] . '@' . $FTPOptions['host']; $prefix .= $FTPOptions['port'] ? ':' . $FTPOptions['port'] : ''; $prefix .= $FTPOptions['root']; } elseif ($SCPOptions['enabled'] == 1 && $use_network) { $prefix = 'ssh2.sftp://' . $SCPOptions['user'] . ':' . $SCPOptions['pass'] . '@' . $SCPOptions['host']; $prefix .= $SCPOptions['port'] ? ':' . $SCPOptions['port'] : ''; $prefix .= $SCPOptions['root']; } else { $prefix = MPATH_ROOT . '/'; } $retval = new MStream($prefix, MPATH_ROOT, $context); } else { $retval = new MStream('', '', $context); } return $retval; }
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 setCredentialsFromRequest($client) { // Determine whether FTP credentials have been passed along with the current request $user = MRequest::getString('username', null, 'POST', MREQUEST_ALLOWRAW); $pass = MRequest::getString('password', null, 'POST', MREQUEST_ALLOWRAW); if ($user != '' && $pass != '') { // Add credentials to the session if (MClientHelper::setCredentials($client, $user, $pass)) { $return = false; } else { $return = MError::raiseWarning('SOME_ERROR_CODE', MText::_('MLIB_CLIENT_ERROR_HELPER_SETCREDENTIALSFROMREQUEST_FAILED')); } } else { // Just determine if the FTP input fields need to be shown $return = !MClientHelper::hasCredentials('ftp'); } return $return; }
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; } }