• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / core-library / 5185700055

pending completion
5185700055

push

github

webeweb
Update CHANGELOG

48909 of 49052 relevant lines covered (99.71%)

48.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ftp/Client/SftpClient.php
1
<?php
2

3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WBW\Library\Ftp\Client;
13

14
use WBW\Library\Ftp\Exception\FtpException;
15
use WBW\Library\Security\Authenticator;
16

17
/**
18
 * SFTP client.
19
 *
20
 * @author webeweb <https://github.com/webeweb>
21
 * @package WBW\Library\Ftp\Client
22
 */
23
class SftpClient extends AbstractClient {
24

25
    /**
26
     * SFTP resource.
27
     *
28
     * @var resource
29
     */
30
    private $sftp;
31

32
    /**
33
     * Constructor.
34
     *
35
     * @param Authenticator $authenticator The authenticator.
36
     */
37
    public function __construct(Authenticator $authenticator) {
38
        parent::__construct($authenticator);
×
39

40
        $this->getAuthenticator()->setScheme("sftp");
×
41
        if (null === $this->getAuthenticator()->getPort()) {
×
42
            $this->getAuthenticator()->setPort(22);
×
43
        }
44
    }
45

46
    /**
47
     * Close.
48
     *
49
     * @return SftpClient Returns this SFTP client.
50
     * @throws FtpException Throws an FTP exception if an I/O error occurs.
51
     */
52
    public function close(): SftpClient {
53

54
        if (null === $this->getConnection() || false === $this->getConnection()) {
×
55
            return $this;
×
56
        }
57

58
        if (false === @ssh2_exec($this->getConnection(), "exit")) {
×
59
            throw $this->newFtpException("ssh2_exec failed: [exit]");
×
60
        }
61

62
        return $this;
×
63
    }
64

65
    /**
66
     * Connect.
67
     *
68
     * @return SftpClient Returns this SFTP client.
69
     * @throws FtpException Throws an FTP exception if an I/O error occurs.
70
     */
71
    public function connect(): SftpClient {
72

73
        $host = $this->getAuthenticator()->getHostname();
×
74
        $port = $this->getAuthenticator()->getPort();
×
75

76
        $this->setConnection(@ssh2_connect($host, $port));
×
77
        if (false === $this->getConnection()) {
×
78
            throw $this->newFtpException("ssh2_connect failed: [$host, $port]");
×
79
        }
80

81
        return $this;
×
82
    }
83

84
    /**
85
     * Delete.
86
     *
87
     * @param string $path The path.
88
     * @return SftpClient Returns this SFTP client.
89
     * @throws FtpException Throws an FTP exception if an I/O error occurs.
90
     */
91
    public function delete(string $path): SftpClient {
92

93
        if (false === ssh2_sftp_unlink($this->getSftp(), $path)) {
×
94
            throw $this->newFtpException("ssh2_sftp_unlink failed: [$path]");
×
95
        }
96

97
        return $this;
×
98
    }
99

100
    /**
101
     * Get a file.
102
     *
103
     * @param string $localFile The local file.
104
     * @param string $remoteFile The remote file.
105
     * @return SftpClient Returns this SFTP client.
106
     */
107
    public function get(string $localFile, string $remoteFile): SftpClient {
108

109
        $input  = fopen("ssh2.sftp://" . intval($this->getSftp()) . $remoteFile, "r");
×
110
        $output = fopen($localFile, "w");
×
111

112
        stream_copy_to_stream($input, $output);
×
113

114
        fclose($input);
×
115
        fclose($output);
×
116

117
        return $this;
×
118
    }
119

120
    /**
121
     * Get the SFTP resource.
122
     *
123
     * @return resource Returns the SFTP resource.
124
     */
125
    private function getSftp() {
126

127
        if (null === $this->sftp) {
×
128
            $this->sftp = ssh2_sftp($this->getConnection());
×
129
        }
130

131
        return $this->sftp;
×
132
    }
133

134
    /**
135
     * Login.
136
     *
137
     * @return SftpClient Returns this SFTP client.
138
     * @throws FtpException Throws an FTP exception if an I/O error occurs.
139
     */
140
    public function login(): SftpClient {
141

142
        $username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername();
×
143
        $password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword();
×
144

145
        if (false === @ssh2_auth_password($this->getConnection(), $username, $password)) {
×
146
            throw $this->newFtpException("ssh2_auth_password failed: [$username]");
×
147
        }
148

149
        return $this;
×
150
    }
151

152
    /**
153
     * Make a directory.
154
     *
155
     * @param string $directory The directory.
156
     * @param int $mode The mode.
157
     * @param bool $recursive Recursively ?
158
     * @return SftpClient Returns this SFTP client.
159
     * @throws FtpException Throws an FTP exception if an I/O error occurs.
160
     */
161
    public function mkdir(string $directory, int $mode = 0777, bool $recursive = false): SftpClient {
162

163
        if (false === ssh2_sftp_mkdir($this->getSftp(), $directory, $mode, $recursive)) {
×
164
            throw $this->newFtpException("ssh2_sftp_mkdir failed: [$directory]");
×
165
        }
166

167
        return $this;
×
168
    }
169

170
    /**
171
     * Put a file.
172
     *
173
     * @param string $localFile The local file.
174
     * @param string $remoteFile The remote file.
175
     * @return SftpClient Returns this SFTP client.
176
     * @throws FtpException Throws an FTP exception if an I/O error occurs.
177
     */
178
    public function put(string $localFile, string $remoteFile): SftpClient {
179

180
        $filename = "ssh2.sftp://" . intval($this->getSftp()) . $remoteFile;
×
181

182
        $input  = @fopen($localFile, "r");
×
183
        $output = @fopen($filename, "w");
×
184

185
        if (false === $input) {
×
186
            throw $this->newFtpException("fopen failed: [$localFile]");
×
187
        }
188

189
        if (false === $output) {
×
190
            throw $this->newFtpException("fopen failed: [$filename]");
×
191
        }
192

193
        stream_copy_to_stream($input, $output);
×
194

195
        fclose($input);
×
196
        fclose($output);
×
197

198
        return $this;
×
199
    }
200

201
    /**
202
     * Rename.
203
     *
204
     * @param string $oldName The old name.
205
     * @param string $newName The new name.
206
     * @return SftpClient Returns this SFTP client.
207
     * @throws FtpException Throws an FTP exception if an I/O error occurs.
208
     */
209
    public function rename(string $oldName, string $newName): SftpClient {
210

211
        if (false === ssh2_sftp_rename($this->getSftp(), $oldName, $newName)) {
×
212
            throw $this->newFtpException("ssh2_sftp_rename failed: [$oldName, $newName]");
×
213
        }
214

215
        return $this;
×
216
    }
217

218
    /**
219
     * Remove a directory.
220
     *
221
     * @param string $directory The directory.
222
     * @return SftpClient Returns this SFTP client.
223
     * @throws FtpException Throws a FTP exception if an I/O error occurs.
224
     */
225
    public function rmdir(string $directory): SftpClient {
226

227
        if (false === ssh2_sftp_rmdir($this->getSftp(), $directory)) {
×
228
            throw $this->newFtpException("ssh2_sftp_rmdir failed: [$directory]");
×
229
        }
230

231
        return $this;
×
232
    }
233
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc