ssh = new SSH2($host); $key = PublicKeyLoader::load(file_get_contents($key)); if (! $this->ssh->login($user, $key)) { throw new \Exception('SSH Login failed'); } $this->sftp = new SFTP($host); if (! $this->sftp->login($user, $key)) { throw new \Exception('SFTP Login failed'); } } /** * Calls SSH2 methods statically * * @param string $name * @param array $arguments * * @return mixed */ public static function __callStatic(string $name, array $arguments): mixed { $self = self::getInstance(); if (! method_exists($self->ssh, $name)) { return $self->sftp->$name(...$arguments); } return $self->ssh->$name(...$arguments); } /** * @see SFTP::put */ public static function put($remote_file, $data, $mode = SFTP::SOURCE_STRING, $start = -1, $local_start = -1, $progressCallback = null): bool { $tmp = bin2hex(random_bytes(10)); # work around sftp sudo put restrictions return self::getInstance()->sftp->put("/tmp/$tmp", $data, $mode, $start, $local_start, $progressCallback) && self::getInstance()->ssh->exec("sudo mv /tmp/$tmp $remote_file"); } }