degagne /
libssh2
| 1 | <?php |
||
| 2 | namespace LibSSH2; |
||
| 3 | |||
| 4 | use LibSSH2\Authentication\Authentication; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Connection class. |
||
| 8 | * |
||
| 9 | * Remote resource connection setter class. |
||
| 10 | * |
||
| 11 | * @package LibSSH2 |
||
| 12 | */ |
||
| 13 | class Connection extends ConsoleOutput |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Remote subsystem connection resource. |
||
| 17 | * |
||
| 18 | * @var resource |
||
| 19 | */ |
||
| 20 | protected $connection; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Constructor. |
||
| 24 | * |
||
| 25 | * @param instance $configuration Configuration class instance |
||
| 26 | * @param instance $authentication Authentication class instance |
||
| 27 | * @param boolean $tunnel require SSH tunnel |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | public function __construct(Configuration $configuration, Authentication $authentication) |
||
| 31 | { |
||
| 32 | if (extension_loaded('ssh2') == false) |
||
| 33 | { |
||
| 34 | throw new \RuntimeException('The libssh2 extension is not loaded.'); |
||
| 35 | } |
||
| 36 | |||
| 37 | $this->connect($configuration); |
||
| 38 | $this->authenticate($authentication); |
||
| 39 | |||
| 40 | if ($configuration->get_tunnel()) |
||
| 41 | { |
||
| 42 | $this->tunnel($configuration); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Destructor. |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | public function __destruct() |
||
| 52 | { |
||
| 53 | $this->disconnect(); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create remote connection resource. |
||
| 58 | * |
||
| 59 | * @param resource $configuration \LibSSH2\Configuration object |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | final public function connect(Configuration $configuration) |
||
| 63 | { |
||
| 64 | $this->connection = @ssh2_connect($configuration->get_host(), $configuration->get_port(), $configuration->get_methods()); |
||
| 65 | if ($this->connection === false || !is_resource($this->connection)) |
||
| 66 | { |
||
| 67 | throw new \RuntimeException($this->get_error_message()); |
||
| 68 | } |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Create remote tunnel connection resource. |
||
| 74 | * |
||
| 75 | * @param string $host hostname |
||
| 76 | * @param int $port port (default=22) |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | final public function tunnel(Configuration $configuration) |
||
| 80 | { |
||
| 81 | $tunnel = @ssh2_tunnel($this->connection, $configuration->get_tunnel_host(), $configuration->get_tunnel_port()); |
||
| 82 | if ($tunnel === false) |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 83 | { |
||
| 84 | throw new \RuntimeException($this->get_error_message()); |
||
| 85 | } |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Authenticate remote connection resource. |
||
| 91 | * |
||
| 92 | * @param resource $authentication \LibSSH2\Authentication\Authentication interface object |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | final public function authenticate(Authentication $authentication) |
||
| 96 | { |
||
| 97 | $authentication->authenticate($this->connection); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | final public function disconnect() |
||
| 104 | { |
||
| 105 | @ssh2_exec($this->connection, 'exit'); |
||
| 106 | unset($this->connection); |
||
| 107 | } |
||
| 108 | } |
||
| 109 |