Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sftp.php
1<?php
2
4
6use \Bitrix\Main\SystemException;
7
8Loc::loadMessages(__FILE__);
9
15class Sftp
16{
17 protected $login;
18 protected $pass;
19 protected $host;
20 protected $port;
21 protected $fingerprint;
22
23 protected $connection;
24 protected $sftp;
25
35 {
36 $this->host = $host;
37 $this->login = $login;
38 $this->pass = $pass;
39 $this->port = $port;
41 }
42
48 public function connect()
49 {
50 if(!extension_loaded("ssh2"))
51 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_SSH2_EXT"));
52
53 $this->connection = @ssh2_connect($this->host, $this->port);
54
55 if(!$this->connection)
56 {
57 throw new SystemException(
59 "TRADING_PLATFORM_SFTP_ERROR_CONNECT",
60 array(
61 "#HOST#" => $this->host,
62 "#PORT#" => $this->port
63 )
64 )
65 );
66 }
67
68 if($this->fingerprint != "")
69 {
71
72 if ($fingerprint != $this->fingerprint)
73 {
75 "TRADING_PLATFORM_SFTP_ERROR_FINGERPRINT",
76 array(
77 "#HOST#" => $this->host,
78 "#FINGERPRINT1#" => $fingerprint,
79 "#FINGERPRINT2#" => $this->fingerprint,
80 )
81 ));
82 }
83 }
84
85 if(!@ssh2_auth_password($this->connection, $this->login, $this->pass))
86 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_PASS"));
87
88 $this->sftp = ssh2_sftp($this->connection);
89
90 if(!$this->sftp)
91 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_INIT"));
92
93 return true;
94 }
95
103 {
104 $remotePath = "sftp://".intval($this->sftp).$remoteFile;
105 $stream = @fopen("ssh2.".$remotePath, 'w');
106
107 if (!$stream)
108 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_OPEN_FILE", array("#FILE#" => $remotePath)));
109
111
112 if ($data === false)
113 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_READ_FILE", array("#FILE#" => $localFile)));
114
115 if (fwrite($stream, $data) === false)
116 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_WRITE_FILE", array("#FILE#" => $remotePath)));
117
118 @fclose($stream);
119
120 return true;
121 }
122
130 {
131 $remotePath = "sftp://".intval($this->sftp).$remoteFile;
132 $stream = @fopen("ssh2.".$remotePath, 'r');
133
134 if (!$stream)
135 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_OPEN_FILE", array("#FILE#" => $remotePath)));
136
137 $contents = stream_get_contents($stream);
138
139 if(file_put_contents($localFile, $contents) === false)
140 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_WRITE_FILE", array("#FILE#" => $localFile)));
141
142 @fclose($stream);
143 return true;
144 }
145
151 public function getFilesList($remotePath)
152 {
153 $result = array();
154 $fullPath = "sftp://".intval($this->sftp).$remotePath;
155 $dirHandle = @opendir("ssh2.".$fullPath);
156
157 if($dirHandle === false)
158 throw new SystemException(Loc::getMessage("TRADING_PLATFORM_SFTP_ERROR_OPEN_PATH", array("#PATH#" => $fullPath)));
159
160 while (false !== ($file = readdir($dirHandle)))
161 if(is_file("ssh2.".$fullPath."/".$file))
162 $result[] = $file;
163
164 return $result;
165 }
166
171 public function getFileSize($remoteFile)
172 {
173 return filesize("ssh2.sftp://".intval($this->sftp).$remoteFile);
174 }
175}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
uploadFile($localFile, $remoteFile)
Definition sftp.php:102
downloadFile($remoteFile, $localFile)
Definition sftp.php:129
__construct($login, $pass, $host, $port, $fingerprint="")
Definition sftp.php:34