protected function resolvePath($remoteFile) { if (substr($remoteFile, 0, 1) != '/') { //resolve relative path $absolutePath = @ssh2_sftp_realpath($this->sftpSession, "."); $remoteFile = $absolutePath . '/' . $remoteFile; } return $remoteFile; }
/** * Upload File * * Uploads file with SCP * * @param string $localFile File to be uploaded * * @return void */ public function uploadFile($localFile) { $sftp = $this->sftp; $localFile = realpath($localFile); $localData = file_get_contents($localFile); if ($localData === False) { throw new \RuntimeException("Could not read local file {$localFile}"); } $remoteHome = ssh2_sftp_realpath($sftp, '.'); if ($remoteHome === false) { throw new \RuntimeException("Could not establish remote home directory"); } $remoteFile = "{$remoteHome}/" . basename($localFile); $stream = fopen("ssh2.sftp://{$sftp}{$remoteFile}", 'w'); if (!$stream) { throw new \RuntimeException("Could not open stream to {$remoteFile}"); } if (fwrite($stream, $localData) === false) { throw new \RuntimeException("Could not write to remote file {$remoteFile}"); } fclose($stream); return $remoteFile; }
public function getListing() { $dir = $this->directory; // Parse directory to parts $parsed_dir = trim($dir, '/'); $this->parts = empty($parsed_dir) ? array() : explode('/', $parsed_dir); // Find the path to the parent directory if (!empty($parts)) { $copy_of_parts = $parts; array_pop($copy_of_parts); if (!empty($copy_of_parts)) { $this->parent_directory = '/' . implode('/', $copy_of_parts); } else { $this->parent_directory = '/'; } } else { $this->parent_directory = ''; } // Initialise $connection = null; $sftphandle = null; // Open a connection if (!function_exists('ssh2_connect')) { $this->setError("Your web server does not have the SSH2 PHP module, therefore can not connect and upload archives to SFTP servers."); return false; } $connection = ssh2_connect($this->host, $this->port); if ($connection === false) { $this->setError("Invalid SFTP hostname or port ({$this->host}:{$this->port}) or the connection is blocked by your web server's firewall."); return false; } // Connect to the server if (!empty($this->pubkey) && !empty($this->privkey)) { if (!ssh2_auth_pubkey_file($connection, $this->username, $this->pubkey, $this->privkey, $this->password)) { $this->setError('Certificate error'); return false; } } else { if (!ssh2_auth_password($connection, $this->username, $this->password)) { $this->setError('Could not authenticate access to SFTP server; check your username and password.'); return false; } } $sftphandle = ssh2_sftp($connection); if ($sftphandle === false) { $this->setWarning("Your SSH server does not allow SFTP connections"); return false; } // Get a raw directory listing (hoping it's a UNIX server!) $list = array(); $dir = ltrim($dir, '/'); if (empty($dir)) { $dir = ssh2_sftp_realpath($sftphandle, "."); $this->directory = $dir; // Parse directory to parts $parsed_dir = trim($dir, '/'); $this->parts = empty($parsed_dir) ? array() : explode('/', $parsed_dir); // Find the path to the parent directory if (!empty($parts)) { $copy_of_parts = $parts; array_pop($copy_of_parts); if (!empty($copy_of_parts)) { $this->parent_directory = '/' . implode('/', $copy_of_parts); } else { $this->parent_directory = '/'; } } else { $this->parent_directory = ''; } } $handle = opendir("ssh2.sftp://{$sftphandle}/{$dir}"); if (!is_resource($handle)) { $this->setError(JText::_('SFTPBROWSER_ERROR_NOACCESS')); return false; } while (($entry = readdir($handle)) !== false) { if (substr($entry, 0, 1) == '.') { continue; } if (!is_dir("ssh2.sftp://{$sftphandle}/{$dir}/{$entry}")) { continue; } $list[] = $entry; } closedir($handle); if (!empty($list)) { asort($list); } return $list; }
/** * @access public * * @return bool */ public function cwd() { $cwd = ssh2_sftp_realpath($this->sftp_link, '.'); if ($cwd) { $cwd = trailingslashit(trim($cwd)); } return $cwd; }
/** * Remote realpath. * * @param string $remote_file * * @return string */ public function realpath($remote_file) { return ssh2_sftp_realpath($this->resource, $path); }
/** * @{inheritDoc} */ public function realpath($filename = '') { return $this->doRun(__METHOD__, func_get_args(), ssh2_sftp_realpath($this->sftp, $filename)); }
/** * Resolves the realpath of a provided path string * * @param string $filename The filename to resolve * * @return string The real path of the file */ public function realpath($filename) { // This function creates a not documented warning on failure. return @ssh2_sftp_realpath($this->getResource(), $filename); }
/** * Facade for ssh2_sftp_realpath. * * TODO: can probably get rid of file exists because realpath will fail if it doesnt. * * @param resource $sftp The sftp resource to use. * @param string $filename The filename to realpath. * * @return string|boolean String realpath on success, false on failure. */ public function realpath($sftp, $filename) { return ssh2_sftp_realpath($sftp, $filename); }
/** * Facade for ssh2_sftp_realpath. * * TODO: can probably get rid of file exists because realpath will fail if it doesnt. * * @param resource $sftp The sftp resource to use. * @param string $filename The filename to realpath. * * @return string|boolean String realpath on success, false on failure. */ public function realpath($sftp, $filename) { //@codeCoverageIgnoreStart return ssh2_sftp_realpath($sftp, $filename); //@codeCoverageIgnoreEnd }
/** * Get the current working directory * * @return string */ public function cwd() { return ssh2_sftp_realpath($this->sftpHandle, "."); }
/** * Resolves the realpath of a provided path string * * @param string $filename The filename to resolve * * @return string The real path of the file */ public function realpath($filename) { return ssh2_sftp_realpath($this->getResource(), $filename); }