summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/tasks/ext/FtpDeployTask.php
blob: 136c0ac7ab233c2ceea36ac4c5b63e00effbf588 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
 * $Id: 87063ecf88b18eae74c2bca3918a1b4ac9f52807 $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
 */

require_once 'phing/Task.php';

/**
 * FtpDeployTask
 * 
 * Deploys a set of files to a remote FTP server.
 * 
 * 
 * Example usage:
 * <ftpdeploy host="host" port="21" username="user" password="password" dir="public_html" mode="ascii" clearfirst="true">
 *   <fileset dir=".">
 *     <include name="**"/>
 *     <exclude name="phing"/>
 *     <exclude name="build.xml"/>
 *     <exclude name="images/**.png"/>
 *     <exclude name="images/**.gif"/>
 *     <exclude name="images/**.jpg"/>
 *   </fileset>
 * </ftpdeploy>
 *
 * @author Jorrit Schippers <jorrit at ncode dot nl>
 * @version $Id: 87063ecf88b18eae74c2bca3918a1b4ac9f52807 $
 * @since 2.3.1
 * @package  phing.tasks.ext
 */
class FtpDeployTask extends Task
{
    private $host = null;
    private $port = 21;
    private $username = null;
    private $password = null;
    private $dir = null;
    private $filesets;
    private $completeDirMap;
    private $mode = FTP_BINARY;
    private $clearFirst = false;
    private $passive = false;

    protected $logLevel = Project::MSG_VERBOSE;
    
    public function __construct() {
        $this->filesets = array();
        $this->completeDirMap = array();
    }
    
    public function setHost($host) {
        $this->host = $host;
    }
    
    public function setPort($port) {
        $this->port = (int) $port;
    }
    
    public function setUsername($username) {
        $this->username = $username;
    }
    
    public function setPassword($password) {
        $this->password = $password;
    }
    
    public function setDir($dir) {
        $this->dir = $dir;
    }
    
    public function setMode($mode) {
        switch(strtolower($mode)) {
            case 'ascii':
                $this->mode = FTP_ASCII;
                break;
            case 'binary':
            case 'bin':
                $this->mode = FTP_BINARY;
                break;
        }
    }
    
    public function setPassive($passive)
    {
        $this->passive = (bool) $passive;
    }
    
    public function setClearFirst($clearFirst) {
        $this->clearFirst = (bool) $clearFirst;
    }
    
    public function createFileSet() {
        $num = array_push($this->filesets, new FileSet());
        return $this->filesets[$num-1];
    }
    
    /**
     * Set level of log messages generated (default = info)
     * @param string $level
     */
    public function setLevel($level)
    {
        switch ($level)
        {
            case "error": $this->logLevel = Project::MSG_ERR; break;
            case "warning": $this->logLevel = Project::MSG_WARN; break;
            case "info": $this->logLevel = Project::MSG_INFO; break;
            case "verbose": $this->logLevel = Project::MSG_VERBOSE; break;
            case "debug": $this->logLevel = Project::MSG_DEBUG; break;
        }
    }

    /**
     * The init method: check if Net_FTP is available
     */
    public function init() {
        require_once 'PEAR.php';

        $paths = explode(PATH_SEPARATOR, get_include_path());
        foreach($paths as $path) {
            if(file_exists($path.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR.'FTP.php')) {
                return true;
            }
        }
        throw new BuildException('The FTP Deploy task requires the Net_FTP PEAR package.');
    }
    
    /**
     * The main entry point method.
     */
    public function main() {
        $project = $this->getProject();
        
        require_once 'Net/FTP.php';
        $ftp = new Net_FTP($this->host, $this->port);
        $ret = $ftp->connect();
        if(@PEAR::isError($ret)) {
            throw new BuildException('Could not connect to FTP server '.$this->host.' on port '.$this->port.': '.$ret->getMessage());
        } else {
            $this->log('Connected to FTP server ' . $this->host . ' on port ' . $this->port, $this->logLevel);
        }
        
        $ret = $ftp->login($this->username, $this->password);
        if(@PEAR::isError($ret)) {
            throw new BuildException('Could not login to FTP server '.$this->host.' on port '.$this->port.' with username '.$this->username.': '.$ret->getMessage());
        } else {
            $this->log('Logged in to FTP server with username ' . $this->username, $this->logLevel);
        }
        
        if ($this->passive) {
            $this->log('Setting passive mode', $this->logLevel);
            $ret = $ftp->setPassive();
            if(@PEAR::isError($ret)) {
                $ftp->disconnect();
                throw new BuildException('Could not set PASSIVE mode: '.$ret->getMessage());
            }
        }

        // append '/' to the end if necessary
        $dir = substr($this->dir, -1) == '/' ? $this->dir : $this->dir.'/';
        
        if($this->clearFirst) {
            // TODO change to a loop through all files and directories within current directory
            $this->log('Clearing directory '.$dir, $this->logLevel);
            $ftp->rm($dir, true);
        }
        
        // Create directory just in case
        $ret = $ftp->mkdir($dir, true);
        if(@PEAR::isError($ret)) {
            $ftp->disconnect();
            throw new BuildException('Could not create directory '.$dir.': '.$ret->getMessage());
        }
        
        $ret = $ftp->cd($dir);
        if(@PEAR::isError($ret)) {
            $ftp->disconnect();
            throw new BuildException('Could not change to directory '.$dir.': '.$ret->getMessage());
        } else {
            $this->log('Changed directory ' . $dir, $this->logLevel);
        }
        
        $fs = FileSystem::getFileSystem();
        $convert = $fs->getSeparator() == '\\';
        
        foreach($this->filesets as $fs) {
            $ds = $fs->getDirectoryScanner($project);
            $fromDir  = $fs->getDir($project);
            $srcFiles = $ds->getIncludedFiles();
            $srcDirs  = $ds->getIncludedDirectories();
            foreach($srcDirs as $dirname) {
                if($convert)
                    $dirname = str_replace('\\', '/', $dirname);
                $this->log('Will create directory '.$dirname, $this->logLevel);
                $ret = $ftp->mkdir($dirname, true);
                if(@PEAR::isError($ret)) {
                    $ftp->disconnect();
                    throw new BuildException('Could not create directory '.$dirname.': '.$ret->getMessage());
                }
            }
            foreach($srcFiles as $filename) {
                $file = new PhingFile($fromDir->getAbsolutePath(), $filename);
                if($convert)
                    $filename = str_replace('\\', '/', $filename);
                $this->log('Will copy '.$file->getCanonicalPath().' to '.$filename, $this->logLevel);
                $ret = $ftp->put($file->getCanonicalPath(), $filename, true, $this->mode);
                if(@PEAR::isError($ret)) {
                    $ftp->disconnect();
                    throw new BuildException('Could not deploy file '.$filename.': '.$ret->getMessage());
                }
            }
        }
        
        $ftp->disconnect();
        $this->log('Disconnected from FTP server', $this->logLevel);
    }
}