login(self::$user, self::$key)) { throw new \Exception('SSH Login failed'); } self::$sftp = new SFTP(self::$host); if (! self::$sftp->login(self::$user, self::$key)) { throw new \Exception('SFTP Login failed'); } } /** * @throws \Exception */ private static function ensureConnection() { if (! self::$ssh->isConnected() || ! self::$sftp->isConnected()) { self::connect(); } } /** * Calls SSH2 methods statically * * @param string $name * @param array $arguments * * @return mixed */ public static function __callStatic(string $name, array $arguments): mixed { self::ensureConnection(); 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 { self::ensureConnection(); $tmp = bin2hex(random_bytes(10)); # work around sftp sudo put restrictions return self::$sftp->put("/tmp/$tmp", $data, $mode, $start, $local_start, $progressCallback) && self::$ssh->exec("sudo mv /tmp/$tmp $remote_file"); } }