1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
sftp.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\TradingPlatform;
4
5use Bitrix\Main\Localization\Loc;
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;
40 $this->fingerprint = $fingerprint;
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(
58 Loc::getMessage(
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 {
70 $fingerprint = ssh2_fingerprint($this->connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
71
72 if ($fingerprint != $this->fingerprint)
73 {
74 throw new SystemException(Loc::getMessage(
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
102 public function uploadFile($localFile, $remoteFile)
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
110 $data = file_get_contents($localFile);
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
129 public function downloadFile($remoteFile, $localFile)
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}
getFilesList($remotePath)
Определения sftp.php:151
uploadFile($localFile, $remoteFile)
Определения sftp.php:102
downloadFile($remoteFile, $localFile)
Определения sftp.php:129
getFileSize($remoteFile)
Определения sftp.php:171
__construct($login, $pass, $host, $port, $fingerprint="")
Определения sftp.php:34
$contents
Определения commerceml2.php:57
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14