1
1
<?php
2
2
3
3
namespace Brunodebarros \Gitdeploy ;
4
+
4
5
use Brunodebarros \Gitdeploy \Helpers ;
5
6
6
7
class Sftp extends Server {
7
8
8
- public function connect ($ server ) {
9
- include ('Crypt/RSA.php ' );
10
- include ('Net/SFTP.php ' );
11
-
12
- $ this ->connection = new \Net_SFTP ($ server ['host ' ], $ server ['port ' ], 10 );
13
- $ logged_in = false ;
14
-
15
- if (isset ($ server ['sftp_key ' ])) {
16
- $ key = new \Crypt_RSA ();
17
- if (isset ($ server ['pass ' ]) and ! empty ($ server ['pass ' ])) {
18
- $ key ->setPassword ($ server ['pass ' ]);
19
- }
20
- $ key ->loadKey (file_get_contents ($ server ['sftp_key ' ]));
21
- $ logged_in = $ this ->connection ->login ($ server ['user ' ], $ key );
9
+ public function connect ($ test = false ) {
10
+ if (!$ this ->connection or $ test ) {
11
+ $ server = $ this ->server ;
12
+ require_once ('Crypt/RSA.php ' );
13
+ require_once ('Net/SFTP.php ' );
14
+
15
+ $ this ->connection = new \Net_SFTP ($ server ['host ' ], $ server ['port ' ], 10 );
16
+ $ logged_in = false ;
17
+
18
+ if (isset ($ server ['sftp_key ' ])) {
19
+ $ key = new \Crypt_RSA ();
20
+ if (isset ($ server ['pass ' ]) and ! empty ($ server ['pass ' ])) {
21
+ $ key ->setPassword ($ server ['pass ' ]);
22
+ }
23
+ $ key ->loadKey (file_get_contents ($ server ['sftp_key ' ]));
24
+ $ logged_in = $ this ->connection ->login ($ server ['user ' ], $ key );
22
25
23
- if (!$ logged_in ) {
24
- Helpers::error ("Could not login to {$ this ->host }. It may be because the key requires a passphrase, which you need to specify it as the 'pass' attribute. " );
26
+ if (!$ logged_in ) {
27
+ Helpers::error ("Could not login to {$ this ->host }. It may be because the key requires a passphrase, which you need to specify it as the 'pass' attribute. " );
28
+ }
29
+ } else {
30
+ $ logged_in = $ this ->connection ->login ($ server ['user ' ], $ server ['pass ' ]);
31
+
32
+ if (!$ logged_in ) {
33
+ Helpers::error ("Could not login to {$ this ->host }" );
34
+ }
25
35
}
26
- } else {
27
- $ logged_in = $ this ->connection ->login ($ server ['user ' ], $ server ['pass ' ]);
28
36
29
- if (!$ logged_in ) {
30
- Helpers::error ("Could not login to {$ this ->host }" );
37
+ if (!$ this -> connection -> chdir ( $ server [ ' path ' ]) ) {
38
+ Helpers::error ("Could not change the directory to { $ server [ ' path ' ]} on {$ this ->host }" );
31
39
}
40
+
41
+ Helpers::logmessage ("Connected to: {$ this ->host }" );
42
+ $ this ->current_commit = $ this ->get_file ('REVISION ' , true );
32
43
}
33
44
34
- if (! $ this -> connection -> chdir ( $ server [ ' path ' ]) ) {
35
- Helpers:: error ( " Could not change the directory to { $ server [ ' path ' ]} on { $ this ->host }" );
45
+ if ($ test ) {
46
+ $ this ->disconnect ( );
36
47
}
48
+ }
37
49
38
- Helpers::logmessage ("Connected to: {$ this ->host }" );
50
+ public function disconnect () {
51
+ $ this ->connection ->disconnect ();
52
+ $ this ->connection = null ;
53
+ Helpers::logmessage ("Disconnected from: {$ this ->host }" );
39
54
}
40
55
41
56
public function get_file ($ file , $ ignore_if_error = false ) {
57
+ $ this ->connect ();
58
+
42
59
$ contents = $ this ->connection ->get ($ file );
43
60
if ($ contents ) {
44
61
return $ contents ;
@@ -53,6 +70,8 @@ public function get_file($file, $ignore_if_error = false) {
53
70
}
54
71
55
72
public function set_file ($ file , $ contents ) {
73
+ $ this ->connect ();
74
+
56
75
$ file = $ file ;
57
76
$ dir = explode ("/ " , dirname ($ file ));
58
77
$ path = "" ;
@@ -87,6 +106,8 @@ public function set_file($file, $contents) {
87
106
}
88
107
89
108
protected function recursive_remove ($ file_or_directory , $ if_dir = false ) {
109
+ $ this ->connect ();
110
+
90
111
$ parent = dirname ($ file_or_directory );
91
112
if ($ this ->connection ->delete ($ file_or_directory , $ if_dir )) {
92
113
$ filelist = $ this ->connection ->nlist ($ parent );
@@ -101,11 +122,15 @@ protected function recursive_remove($file_or_directory, $if_dir = false) {
101
122
}
102
123
103
124
public function mkdir ($ file ) {
125
+ $ this ->connect ();
126
+
104
127
$ this ->connection ->mkdir ($ file );
105
128
Helpers::logmessage ("Created directory: $ file " );
106
129
}
107
130
108
131
public function unset_file ($ file ) {
132
+ $ this ->connect ();
133
+
109
134
$ this ->recursive_remove ($ file , false );
110
135
Helpers::logmessage ("Deleted: $ file " );
111
136
}
0 commit comments