1
+ <?php
2
+ /**
3
+ * FTP with Implicit SSL/TLS Class
4
+ *
5
+ * Simple wrapper for cURL functions to transfer an ASCII file over FTP with implicit SSL/TLS
6
+ *
7
+ * @category Class
8
+ * @author Max Rice
9
+ * @since 1.0
10
+ */
11
+
12
+ class FTP_Implicit_SSL {
13
+
14
+ /** @var resource cURL resource handle */
15
+ private $ curl_handle ;
16
+
17
+ /** @var string cURL URL for upload */
18
+ private $ url ;
19
+
20
+ /**
21
+ * Connect to FTP server over Implicit SSL/TLS
22
+ *
23
+ *
24
+ * @access public
25
+ * @since 1.0
26
+ * @param string $username
27
+ * @param string $password
28
+ * @param string $server
29
+ * @param int $port
30
+ * @param string $initial_path
31
+ * @param bool $passive_mode
32
+ * @throws Exception - blank username / password / port
33
+ * @return \FTP_Implicit_SSL
34
+ */
35
+ public function __construct ( $ username , $ password , $ server , $ port = 990 , $ initial_path = '' , $ passive_mode = false ) {
36
+
37
+ // check for blank username
38
+ if ( ! $ username )
39
+ throw new Exception ( 'FTP Username is blank. ' );
40
+
41
+ // don't check for blank password (highly-questionable use case, but still)
42
+
43
+ // check for blank server
44
+ if ( ! $ server )
45
+ throw new Exception ( 'FTP Server is blank. ' );
46
+
47
+ // check for blank port
48
+ if ( ! $ port )
49
+ throw new Exception ( 'FTP Port is blank. ' , WC_XML_Suite::$ text_domain );
50
+
51
+ // set host/initial path
52
+ $ this ->url = "ftps:// {$ server }/ {$ initial_path }" ;
53
+
54
+ // setup connection
55
+ $ this ->curl_handle = curl_init ();
56
+
57
+ // check for successful connection
58
+ if ( ! $ this ->curl_handle )
59
+ throw new Exception ( 'Could not initialize cURL. ' );
60
+
61
+ // connection options
62
+ $ options = array (
63
+ CURLOPT_USERPWD => $ username . ': ' . $ password ,
64
+ CURLOPT_SSL_VERIFYPEER => false , // don't verify SSL
65
+ CURLOPT_SSL_VERIFYHOST => false ,
66
+ CURLOPT_FTP_SSL => CURLFTPSSL_ALL , // require SSL For both control and data connections
67
+ CURLOPT_FTPSSLAUTH => CURLFTPAUTH_DEFAULT , // let cURL choose the FTP authentication method (either SSL or TLS)
68
+ CURLOPT_UPLOAD => true ,
69
+ CURLOPT_PORT => $ port ,
70
+ CURLOPT_TIMEOUT => 30 ,
71
+ );
72
+
73
+ // cURL FTP enables passive mode by default, so disable it by enabling the PORT command and allowing cURL to select the IP address for the data connection
74
+ if ( ! $ passive_mode )
75
+ $ options [ CURLOPT_FTPPORT ] = '- ' ;
76
+
77
+ // set connection options, use foreach so useful errors can be caught instead of a generic "cannot set options" error with curl_setopt_array()
78
+ foreach ( $ options as $ option_name => $ option_value ) {
79
+
80
+ if ( ! curl_setopt ( $ this ->curl_handle , $ option_name , $ option_value ) )
81
+ throw new Exception ( sprintf ( 'Could not set cURL option: %s ' , $ option_name ) );
82
+ }
83
+
84
+ }
85
+
86
+ /**
87
+ * Write file into temporary memory and upload stream to remote file
88
+ *
89
+ * @access public
90
+ * @since 1.0
91
+ * @param string $file_name - remote file name to create
92
+ * @param string $file - file content to upload
93
+ * @throws Exception - Open remote file failure or write data failure
94
+ */
95
+ public function upload ( $ file_name , $ file ) {
96
+
97
+ // set file name
98
+ if ( ! curl_setopt ( $ this ->curl_handle , CURLOPT_URL , $ this ->url . $ file_name ))
99
+ throw new Exception ( "Could not set cURL file name: $ file_name " );
100
+
101
+ // open memory stream for writing
102
+ $ stream = fopen ( 'php://temp ' , 'w+ ' );
103
+
104
+ // check for valid stream handle
105
+ if ( ! $ stream )
106
+ throw new Exception ( 'Could not open php://temp for writing. ' );
107
+
108
+ // write file into the temporary stream
109
+ fwrite ( $ stream , $ file );
110
+
111
+ // rewind the stream pointer
112
+ rewind ( $ stream );
113
+
114
+ // set the file to be uploaded
115
+ if ( ! curl_setopt ( $ this ->curl_handle , CURLOPT_INFILE , $ stream ) )
116
+ throw new Exception ( "Could not load file $ file_name " );
117
+
118
+ // upload file
119
+ if ( ! curl_exec ( $ this ->curl_handle ) )
120
+ throw new Exception ( sprintf ( 'Could not upload file. cURL Error: [%s] - %s ' , curl_errno ( $ this ->curl_handle ), curl_error ( $ this ->curl_handle ) ) );
121
+
122
+ // close the stream handle
123
+ fclose ( $ stream );
124
+ }
125
+
126
+ /**
127
+ * Attempt to close cURL handle
128
+ * Note - errors suppressed here as they are not useful
129
+ *
130
+ * @access public
131
+ * @since 1.0
132
+ */
133
+ public function __destruct () {
134
+
135
+ @curl_close ( $ this ->curl_handle );
136
+ }
137
+
138
+ }
0 commit comments