/**
  * 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;
 }
 /**
  * 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;
     }
 }
 /**
  * Creates a new stream object with appropriate prefix
  *
  * @param   boolean  $use_prefix   Prefix the connections for writing
  * @param   boolean  $use_network  Use network if available for writing; use false to disable (e.g. FTP, SCP)
  * @param   string   $ua           UA User agent to use
  * @param   boolean  $uamask       User agent masking (prefix Mozilla)
  *
  * @return  Stream
  *
  * @see Stream
  * @since   11.1
  */
 public static function getStream($use_prefix = true, $use_network = true, $ua = null, $uamask = false)
 {
     // Setup the context; Joomla! UA and overwrite
     $context = array();
     $version = new JVersion();
     // 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 = Helper::getCredentials('ftp');
         $SCPOptions = Helper::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 = JPATH_ROOT . '/';
         }
         $retval = new Stream($prefix, JPATH_ROOT, $context);
     } else {
         $retval = new Stream('', '', $context);
     }
     return $retval;
 }