Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
sftp.php
1
<?php
2
3
namespace
Bitrix\Sale\TradingPlatform
;
4
5
use
Bitrix\Main\Localization\Loc
;
6
use \Bitrix\Main\SystemException;
7
8
Loc::loadMessages
(__FILE__);
9
15
class
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
34
public
function
__construct
(
$login
,
$pass
,
$host
,
$port
,
$fingerprint
=
""
)
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
}
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Sale\TradingPlatform\Sftp
Definition
sftp.php:16
Bitrix\Sale\TradingPlatform\Sftp\getFilesList
getFilesList($remotePath)
Definition
sftp.php:151
Bitrix\Sale\TradingPlatform\Sftp\$connection
$connection
Definition
sftp.php:23
Bitrix\Sale\TradingPlatform\Sftp\$pass
$pass
Definition
sftp.php:18
Bitrix\Sale\TradingPlatform\Sftp\uploadFile
uploadFile($localFile, $remoteFile)
Definition
sftp.php:102
Bitrix\Sale\TradingPlatform\Sftp\$fingerprint
$fingerprint
Definition
sftp.php:21
Bitrix\Sale\TradingPlatform\Sftp\$host
$host
Definition
sftp.php:19
Bitrix\Sale\TradingPlatform\Sftp\connect
connect()
Definition
sftp.php:48
Bitrix\Sale\TradingPlatform\Sftp\downloadFile
downloadFile($remoteFile, $localFile)
Definition
sftp.php:129
Bitrix\Sale\TradingPlatform\Sftp\getFileSize
getFileSize($remoteFile)
Definition
sftp.php:171
Bitrix\Sale\TradingPlatform\Sftp\$port
$port
Definition
sftp.php:20
Bitrix\Sale\TradingPlatform\Sftp\$sftp
$sftp
Definition
sftp.php:24
Bitrix\Sale\TradingPlatform\Sftp\__construct
__construct($login, $pass, $host, $port, $fingerprint="")
Definition
sftp.php:34
Bitrix\Sale\TradingPlatform\Sftp\$login
$login
Definition
sftp.php:17
Bitrix\Sale\TradingPlatform
Definition
catalogsectiontab.php:3
modules
sale
lib
tradingplatform
sftp.php
Создано системой
1.10.0