SFTPClient::connect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the ftp-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\Core\Security\Authenticator;
15
use WBW\Library\FTP\Exception\FTPException;
16
17
/**
18
 * SFTP client.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\FTP\Client
22
 */
23
class SFTPClient extends AbstractFTPClient {
24
25
    /**
26
     * SFTP resource.
27
     *
28
     * @var mixed
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
        $this->getAuthenticator()->setScheme("sftp");
40
        if (null === $this->getAuthenticator()->getPort()) {
41
            $this->getAuthenticator()->setPort(22);
42
        }
43
    }
44
45
    /**
46
     * Closes this SFTP connection.
47
     *
48
     * @return SFTPClient Returns this SFTP client.
49
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
50
     */
51
    public function close() {
52
        if (false === ssh2_exec($this->getConnection(), "exit")) {
53
            throw $this->newFTPException("close failed");
54
        }
55
        return $this;
56
    }
57
58
    /**
59
     * Opens this SFTP connection.
60
     *
61
     * @return SFTPClient Returns this SFTP client.
62
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
63
     */
64
    public function connect() {
65
        $host = $this->getAuthenticator()->getHost();
66
        $port = $this->getAuthenticator()->getPort();
67
        $this->setConnection(ssh2_connect($host, $port));
68
        if (false === $this->getConnection()) {
69
            throw $this->newFTPException("connection failed");
70
        }
71
        return $this;
72
    }
73
74
    /**
75
     * Deletes a file on the SFTP server.
76
     *
77
     * @param string $path The file to delete.
78
     * @return SFTPClient Returns this SFTP client.
79
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
80
     */
81
    public function delete($path) {
82
        if (false === ssh2_sftp_unlink($this->getSFTP(), $path)) {
83
            throw $this->newFTPException(sprintf("delete %s failed", $path));
84
        }
85
        return $this;
86
    }
87
88
    /**
89
     * Get the SFTP resource.
90
     *
91
     * @return mixed Returns the SFTP resource.
92
     */
93
    private function getSFTP() {
94
        if (null === $this->sftp) {
95
            $this->sftp = ssh2_sftp($this->getConnection());
96
        }
97
        return $this->sftp;
98
    }
99
100
    /**
101
     * Logs in to this SFTP connection.
102
     *
103
     * @return SFTPClient Returns this SFTP client.
104
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
105
     */
106
    public function login() {
107
        $username = $this->getAuthenticator()->getPasswordAuthentication()->getUsername();
108
        $password = $this->getAuthenticator()->getPasswordAuthentication()->getPassword();
109
        if (false === ssh2_auth_password($this->getConnection(), $username, $password)) {
110
            throw $this->newFTPException("login failed");
111
        }
112
        return $this;
113
    }
114
115
    /**
116
     * Creates a directory.
117
     *
118
     * @param string $directory The directory.
119
     * @param integer $mode The mode.
120
     * @param boolean $recursive Recursively ?.
121
     * @return SFTPClient Returns this SFTP client.
122
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
123
     */
124
    public function mkdir($directory, $mode = 0777, $recursive = false) {
125
        if (false === ssh2_sftp_mkdir($this->getSFTP(), $directory, $mode, $recursive)) {
126
            throw $this->newFTPException(sprintf("mkdir %s failed", $directory));
127
        }
128
        return $this;
129
    }
130
131
    /**
132
     * Uploads a file to the SFTP server.
133
     *
134
     * @param string $localFile The local file.
135
     * @param string $remoteFile The remote file.
136
     * @return SFTPClient Returns this SFTP client.
137
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
138
     */
139
    public function put($localFile, $remoteFile) {
140
        $stream = fopen("ssh2.sftp://" . intval($this->getSFTP()) . $remoteFile, "w");
141
        fwrite($stream, file_get_contents($localFile));
142
        fclose($stream);
143
        return $this;
144
    }
145
146
    /**
147
     * Renames a file or a directory on the SFTP server.
148
     *
149
     * @param string $oldName The old file/directory name.
150
     * @param string $newName The new name.
151
     * @return SFTPClient Returns this SFTP client.
152
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
153
     */
154
    public function rename($oldName, $newName) {
155
        if (false === ssh2_sftp_rename($this->getSFTP(), $oldName, $newName)) {
156
            throw $this->newFTPException(sprintf("rename %s into %s failed", $oldName, $newName));
157
        }
158
        return $this;
159
    }
160
161
    /**
162
     * Removes a directory.
163
     *
164
     * @param string $directory The directory.
165
     * @return SFTPClient Returns this SFTP client.
166
     * @throws FTPException Throws a FTP exception if an I/O error occurs.
167
     */
168
    public function rmdir($directory) {
169
        if (false === ssh2_sftp_rmdir($this->getSFTP(), $directory)) {
170
            throw $this->newFTPException(sprintf("rmdir %s failed", $directory));
171
        }
172
        return $this;
173
    }
174
175
}
176