public static function setCredentials($client, $user, $pass) { $return = false; $client = strtolower($client); // Test if the given credentials are valid switch ($client) { case 'ftp': $config = MFactory::getConfig(); $options = array('enabled' => $config->get('ftp_enable'), 'host' => $config->get('ftp_host'), 'port' => $config->get('ftp_port')); if ($options['enabled']) { mimport('framework.client.ftp'); $ftp = MFTP::getInstance($options['host'], $options['port']); // Test the connection and try to log in if ($ftp->isConnected()) { if ($ftp->login($user, $pass)) { $return = true; } $ftp->quit(); } } break; default: break; } if ($return) { // Save valid credentials to the session $session = MFactory::getSession(); $session->set($client . '.user', $user, 'MClientHelper'); $session->set($client . '.pass', $pass, 'MClientHelper'); // Force re-creation of the data saved within MClientHelper::getCredentials() MClientHelper::getCredentials($client, true); } return $return; }
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; } }