Ticket #323: FtpDeployTask.php

File FtpDeployTask.php, 5.9 KB (added by joseph.chereshnovsky@…, 3 years ago)

FtpDeployTask.php file with the FTP passive mode support

Line 
1<?php
2/**
3 * $Id: FtpDeployTask.php 381 2008-07-29 16:22:26Z mrook $
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information please see
19 * <http://phing.info>.
20 */
21
22require_once 'phing/Task.php';
23require_once 'PEAR.php';
24
25/**
26 * FtpDeployTask
27 *
28 * Deploys a set of files to a remote FTP server.
29 *
30 *
31 * Example usage:
32 * <ftpdeploy host="host" port="21" username="user" password="password" dir="public_html" mode="ascii" clearfirst="true" passive="false">
33 *   <fileset dir=".">
34 *     <include name="**"/>
35 *     <exclude name="phing"/>
36 *     <exclude name="build.xml"/>
37 *     <exclude name="images/**.png"/>
38 *     <exclude name="images/**.gif"/>
39 *     <exclude name="images/**.jpg"/>
40 *   </fileset>
41 * </ftpdeploy>
42 *
43 *
44 * @todo Documentation
45 * @author Jorrit Schippers <jorrit at ncode dot nl>
46 * @since 2.3.1
47 */
48class FtpDeployTask extends Task
49{
50    private $host = null;
51    private $port = 0;
52    private $username = null;
53    private $password = null;
54    private $dir = null;
55    private $filesets;
56    private $completeDirMap;
57    private $mode = null;
58    private $clearFirst = false;
59   
60   
61    /**
62     * Indicates either to use pasive mode or not
63     * @var boolean $pasive
64     */
65    private $pasive = false;
66   
67    /**
68     * Sets the pasive mode
69     * @return void
70     */
71    public function setPassive($mode)
72    {
73        if (true == $mode) {
74            $this->pasive = true;
75        }
76    }
77
78
79    public function __construct() {
80        $this->filesets = array();
81        $this->completeDirMap = array();
82    }
83   
84    public function setHost($host) {
85        $this->host = $host;
86    }
87   
88    public function setPort($port) {
89        $this->port = (int) $port;
90    }
91   
92    public function setUsername($username) {
93        $this->username = $username;
94    }
95   
96    public function setPassword($password) {
97        $this->password = $password;
98    }
99   
100    public function setDir($dir) {
101        $this->dir = $dir;
102    }
103   
104    public function setMode($mode) {
105        switch(strtolower($mode)) {
106            case 'ascii':
107                $this->mode = FTP_ASCII;
108                break;
109            case 'binary':
110            case 'bin':
111                $this->mode = FTP_BINARY;
112                break;
113        }
114    }
115   
116    public function setClearFirst($clearFirst) {
117        $this->clearFirst = (bool) $clearFirst;
118    }
119   
120    function createFileSet() {
121        $num = array_push($this->filesets, new FileSet());
122        return $this->filesets[$num-1];
123    }
124   
125    /**
126     * The init method: check if Net_FTP is available
127     */
128    public function init() {
129        $paths = explode(PATH_SEPARATOR, get_include_path());
130        foreach($paths as $path) {
131            if(file_exists($path.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR.'FTP.php')) {
132                return true;
133            }
134        }
135        throw new BuildException('The FTP Deploy task requires the Net_FTP PEAR package.');
136    }
137   
138    /**
139     * The main entry point method.
140     */
141    public function main() {
142        $project = $this->getProject();
143       
144        require_once 'Net/FTP.php';
145        $ftp = new Net_FTP($this->host, $this->port);
146        $ret = $ftp->connect();
147        if(PEAR::isError($ret))
148            throw new BuildException('Could not connect to FTP server '.$this->host.' on port '.$this->port.': '.$ret->getMessage());
149
150        $ret = $ftp->login($this->username, $this->password);
151
152        // Switch to pasive mode
153        if (true == $this->pasive) {
154            $res = $ftp->setPassive();
155            $this->log('Passive mode enabled', Project::MSG_INFO);
156        }
157       
158        if(PEAR::isError($ret))
159            throw new BuildException('Could not login to FTP server '.$this->host.' on port '.$this->port.' with username '.$this->username.': '.$ret->getMessage());
160       
161        if($this->clearFirst) {
162            // TODO change to a loop through all files and directories within current directory
163            $this->log('Clearing directory '.$this->dir, Project::MSG_INFO);
164            $dir = substr($this->dir, -1) == '/' ? $this->dir : $this->dir.'/';
165            $ftp->rm($dir, true);
166            $ftp->mkdir($dir);
167        }
168       
169        $ret = $ftp->cd($this->dir);
170        if(PEAR::isError($ret))
171            throw new BuildException('Could not change to directory '.$this->dir.': '.$ret->getMessage());
172       
173        $fs = FileSystem::getFileSystem();
174        $convert = $fs->getSeparator() == '\\';
175       
176        foreach($this->filesets as $fs) {
177            $ds = $fs->getDirectoryScanner($project);
178            $fromDir  = $fs->getDir($project);
179            $srcFiles = $ds->getIncludedFiles();
180            $srcDirs  = $ds->getIncludedDirectories();
181            foreach($srcDirs as $dirname) {
182                if($convert)
183                    $dirname = str_replace('\\', '/', $dirname);
184                $this->log('Will create directory '.$dirname, Project::MSG_VERBOSE);
185                $ret = $ftp->mkdir($dirname, true);
186                if(PEAR::isError($ret))
187                    throw new BuildException('Could not create directory '.$dirname.': '.$ret->getMessage());
188            }
189            foreach($srcFiles as $filename) {
190                $file = new PhingFile($fromDir->getAbsolutePath(), $filename);
191                if($convert)
192                    $filename = str_replace('\\', '/', $filename);
193                $this->log('Will copy '.$file->getCanonicalPath().' to '.$filename, Project::MSG_VERBOSE);
194                $ret = $ftp->put($file->getCanonicalPath(), $filename, true, $this->mode);
195                if(PEAR::isError($ret))
196                    throw new BuildException('Could not deploy file '.$filename.': '.$ret->getMessage());
197            }
198        }
199       
200        $ftp->disconnect();
201    }
202}
203?>