-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTPCServerSFTP.php
89 lines (73 loc) · 1.81 KB
/
TPCServerSFTP.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* SFTP storage back-end, using phpseclib (http://phpseclib.sourceforge.net).
*
* @file
* @author Nmlgc
*/
class TPCServerSFTP extends TPCServer {
// SFTP connection
protected $sftp = null;
// Current directory
protected $dir = null;
/// ===================
/// TPCServer functions
/// ===================
/**
* Initializes an SFTP session.
*/
function __construct( array &$serverInfo ) {
global $wgTPCServerRSAPrivKey;
global $wgTPCServerRSAPass;
// Required $serverInfo elements
$sftp_host = &$serverInfo['sftp_host'];
$sftp_user = &$serverInfo['sftp_user'];
$sftp_pass = ( $serverInfo['sftp_pass'] ?? '' );
$local_path = ( $serverInfo['local_path'] ?? '' );
if ( !$sftp_pass and $wgTPCServerRSAPrivKey ) {
$key = new Crypt_RSA();
$key->setPassword( $wgTPCServerRSAPass );
$key->loadKey( file_get_contents( $wgTPCServerRSAPrivKey ) );
} else {
$key = null;
}
$this->sftp = new Net_SFTP( $sftp_host );
if ( $sftp_pass ) {
$ret = $this->sftp->login( $sftp_user, $sftp_pass );
} else {
$ret = $this->sftp->login( $sftp_user, $key );
}
if ( $local_path ) {
$this->sftp->chdir( $local_path );
}
}
function wipe() {
$files = $this->sftp->nlist();
foreach ( $files as $i ) {
if ( $i != '.' and $i != '..' ) {
$this->sftp->delete( $i, true );
}
}
}
function mkdir( &$dir ) {
if ( $dir !== "" ) {
$this->sftp->mkdir( $dir, -1, true );
}
}
function chdir( &$dir ) {
if ( $dir ) {
$this->dir = $dir . '/';
} else {
$this->dir = null;
}
}
function put( &$fn, &$data ) {
$this->sftp->put( $this->dir . $fn, $data );
}
function get( &$fn ) {
return $this->sftp->get( $this->dir . $fn );
}
function copy( &$target, &$source ) {
$this->sftp->put( $this->dir . $target, $source, NET_SFTP_LOCAL_FILE );
}
};