initial commit
[namibia] / module / Utility / src / Utility / Remote / CurlFtpUploader.php
1 <?php
2
3 namespace Utility\Remote;
4
5 class CurlFtpUploader 
6 {
7         const PROTOCOL_FTP      = 'ftp';
8         const PROTOCOL_SFTP     = 'sftp';
9         
10         protected $_protocol = self::PROTOCOL_FTP;
11         protected $_port = 21;
12         
13         protected $_serverAddress;
14         protected $_username;
15         protected $_password;
16         
17         protected $_sslVerifyPeer = false;
18         protected $_sslVerifyHost = 0;
19         
20         protected $_ftpSsl              = CURLFTPSSL_TRY;
21         
22         public $_errorNo = 0;
23     public $_error = '';
24         
25         function __construct($serverAddress, $username, $password)
26         {
27                 $this->_serverAddress   = $serverAddress;
28                 $this->_username                = $username;
29                 $this->_password                = $password;
30         }
31         
32         /**
33          * Set the protocol, default is ftp
34          * @example setProtoCol(CurlFtpUploader::PROTOCOL_SFTP);
35          * @example setProtoCol(CurlFtpUploader::PROTOCOL_FTP);
36          * @param string $protocol
37          * @throws \Exception
38          * @return \Utility\Remote\CurlFtpUploader
39          */
40         public function setProtoCol($protocol)
41         {
42                 if(self::PROTOCOL_FTP != $protocol && self::PROTOCOL_SFTP != $protocol)
43                 {
44                         throw new \Exception('Protocol must be CurlFtpUploader::PROTOCOL_FTP or CurlFtpUploader::PROTOCOL_SFTP');
45                 }       
46                 $this->_protocol = $protocol;
47                 
48                 if($protocol == self::PROTOCOL_SFTP)
49                 {
50                         $this->setPort(22);
51                 }
52                 return $this;           
53         }
54         
55         /**
56          * Set the port, default is 21
57          * @example setPort(22);
58          * @param int $port
59          * @throws \Exception
60          * @return \Utility\Remote\CurlFtpUploader
61          */
62         public function setPort($port)
63         {
64                 $port = (int) $port;
65                 if($port == 0)
66                 {
67                         throw new \Exception('Port must be an int and bigger than 0');
68                 }
69                 $this->_port = $port;
70                 return $this;
71         }
72         
73         /**
74          * Set CURLOPT_SSL_VERIFYPEER, default is false
75          * @param bool $value
76          * @return \Utility\Remote\CurlFtpUploader
77          */
78         public function setSslVerifyPeer($value)
79         {
80                 $this->_sslVerifyPeer = (bool) $value;
81                 return $this;
82         }
83         
84         /**
85          * Set CURLOPT_SSL_VERIFYHOST, default is false
86          * @param bool $value
87          * @return \Utility\Remote\CurlFtpUploader
88          */
89         public function setSslVerifyHost($value)
90         {
91                 $this->_sslVerifyHost = (int) $value;
92                 return $this;
93         }
94         /**
95          * Set CURLOPT_FTP_SSL, default value is CURLFTPSSL_TRY
96          * @example setFtpSsl(CURLFTPSSL_TRY);
97          * @param int $value
98          * @return \Utility\Remote\CurlFtpUploader
99          */
100         public function setFtpSsl($value)
101         {
102                 $this->_ftpSsl = $value;
103                 return $this;
104         }
105
106         /**
107          * Upload the file
108          * [The upload directory has to exist, otherwise upload will fail with an error 9 (CURLE_REMOTE_ACCESS_DENIED (9) 
109          *      We were denied access to the resource given in the URL. For FTP, this occurs while trying to change to the remote directory)]
110          * @example uploadFile('/var/tmp/pic1.jpg', 'some_dir/dest_pic.jpg');
111          * @param string $sourceFile
112          * @param string $destFile
113          * @return boolean
114          */
115         public function uploadFile($sourceFile, $destFile)
116         {
117                 if('/' == substr($this->_serverAddress, -1))
118                 {
119                         $this->_serverAddress = substr($this->_serverAddress, 0, ( (strlen($this->_serverAddress) - 1 )));
120                 }
121                 
122                 if('/' == substr($destFile, 0, 1))
123                 {
124                         $destFile = substr($destFile, 1);
125                 }
126                 
127                 $uploadDestination = $this->_protocol . '://' . $this->_username . ':' . $this->_password . '@' . $this->_serverAddress . '/' . $destFile;
128
129                 $fp = fopen($sourceFile, 'r');
130                 
131                 $ch = curl_init();
132                 curl_setopt($ch, CURLOPT_PORT, $this->_port);
133                 curl_setopt($ch, CURLOPT_URL, $uploadDestination);
134                 curl_setopt($ch, CURLOPT_UPLOAD, 1);
135                 curl_setopt($ch, CURLOPT_INFILE, $fp);
136                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_sslVerifyPeer);
137                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->_sslVerifyHost);
138                 curl_setopt($ch, CURLOPT_FTP_SSL, $this->_ftpSsl);
139                 curl_setopt($ch, CURLOPT_INFILESIZE, filesize($sourceFile));
140                 
141                 curl_exec ($ch);
142                 $this->_errorNo = curl_errno($ch);
143         $this->_error = curl_error($ch);
144                 curl_close ($ch);
145                 
146                 if (0 == $this->_errorNo) 
147                 {
148                         return true;
149                 } 
150                 else 
151                 {
152                         return false;
153                 }
154         }
155 }