1 | <?php |
||
14 | class SshExtension implements ServerInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var Configuration |
||
18 | */ |
||
19 | private $configuration; |
||
20 | |||
21 | /** |
||
22 | * SSH session. |
||
23 | * @var Ssh\Session |
||
24 | */ |
||
25 | private $session; |
||
26 | |||
27 | /** |
||
28 | * Array of created directories during upload. |
||
29 | * @var array |
||
30 | */ |
||
31 | private $directories = []; |
||
32 | |||
33 | /** |
||
34 | * @param Configuration $configuration |
||
35 | */ |
||
36 | public function __construct(Configuration $configuration) |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function connect() |
||
45 | { |
||
46 | $serverConfig = $this->getConfiguration(); |
||
47 | $configuration = new Ssh\Configuration($serverConfig->getHost(), $serverConfig->getPort()); |
||
48 | |||
49 | switch ($serverConfig->getAuthenticationMethod()) { |
||
50 | case Configuration::AUTH_BY_PASSWORD: |
||
51 | $authentication = new Ssh\Authentication\Password( |
||
52 | $serverConfig->getUser(), |
||
53 | $serverConfig->getPassword() |
||
54 | ); |
||
55 | break; |
||
56 | |||
57 | case Configuration::AUTH_BY_CONFIG: |
||
58 | $configuration = new Ssh\SshConfigFileConfiguration( |
||
59 | $serverConfig->getConfigFile(), |
||
60 | $serverConfig->getHost(), |
||
61 | $serverConfig->getPort() |
||
62 | ); |
||
63 | $authentication = $configuration->getAuthentication( |
||
64 | $serverConfig->getPassword(), |
||
65 | $serverConfig->getUser() |
||
66 | ); |
||
67 | |||
68 | break; |
||
69 | |||
70 | case Configuration::AUTH_BY_IDENTITY_FILE: |
||
71 | $authentication = new Ssh\Authentication\PublicKeyFile( |
||
72 | $serverConfig->getUser(), |
||
73 | $serverConfig->getPublicKey(), |
||
74 | $serverConfig->getPrivateKey(), |
||
75 | $serverConfig->getPassPhrase() |
||
76 | ); |
||
77 | |||
78 | break; |
||
79 | |||
80 | case Configuration::AUTH_BY_PEM_FILE: |
||
81 | throw new \RuntimeException('If you want to use pem file, switch to using PhpSecLib.'); |
||
82 | |||
83 | case Configuration::AUTH_BY_AGENT: |
||
84 | $authentication = new \Ssh\Authentication\Agent( |
||
85 | $serverConfig->getUser() |
||
86 | ); |
||
87 | break; |
||
88 | |||
89 | default: |
||
90 | throw new \RuntimeException('You need to specify authentication method.'); |
||
91 | } |
||
92 | |||
93 | $this->session = new Ssh\Session($configuration, $authentication); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Check if not connected and connect. |
||
98 | */ |
||
99 | public function checkConnection() |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function run($command) |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function upload($local, $remote) |
||
121 | { |
||
122 | $this->checkConnection(); |
||
123 | |||
124 | $remote = str_replace(DIRECTORY_SEPARATOR, '/', $remote); |
||
125 | $dir = str_replace(DIRECTORY_SEPARATOR, '/', dirname($remote)); |
||
126 | |||
127 | if (!isset($this->directories[$dir])) { |
||
128 | $this->session->getSftp()->mkdir($dir, -1, true); |
||
129 | $this->directories[$dir] = true; |
||
130 | } |
||
131 | |||
132 | if ($this->session->getSftp()->send($local, $remote) === false) { |
||
133 | throw new \RuntimeException('Can not upload file.'); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function download($local, $remote) |
||
148 | |||
149 | /** |
||
150 | * @return Configuration |
||
151 | */ |
||
152 | public function getConfiguration() |
||
156 | } |
||
157 |