Ticket #338: FtpDeployTask.php

File FtpDeployTask.php, 6.1 KB (added by mi.olszewski@…, 3 years ago)

improved version of FtpDeployTask

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">
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    private $passive = false;
60   
61    public function __construct() {
62        $this->filesets = array();
63        $this->completeDirMap = array();
64    }
65   
66    public function setHost($host) {
67        $this->host = $host;
68    }
69   
70    public function setPort($port) {
71        $this->port = (int) $port;
72    }
73   
74    public function setUsername($username) {
75        $this->username = $username;
76    }
77   
78    public function setPassword($password) {
79        $this->password = $password;
80    }
81   
82    public function setDir($dir) {
83        $this->dir = $dir;
84    }
85   
86    public function setMode($mode) {
87        switch(strtolower($mode)) {
88            case 'ascii':
89                $this->mode = FTP_ASCII;
90                break;
91            case 'binary':
92            case 'bin':
93                $this->mode = FTP_BINARY;
94                break;
95        }
96    }
97   
98    public function setPassive($passive)
99    {
100        $this->passive = (bool) $passive;
101    }
102   
103    public function setClearFirst($clearFirst) {
104        $this->clearFirst = (bool) $clearFirst;
105    }
106   
107    function createFileSet() {
108        $num = array_push($this->filesets, new FileSet());
109        return $this->filesets[$num-1];
110    }
111   
112    /**
113     * The init method: check if Net_FTP is available
114     */
115    public function init() {
116        $paths = explode(PATH_SEPARATOR, get_include_path());
117        foreach($paths as $path) {
118            if(file_exists($path.DIRECTORY_SEPARATOR.'Net'.DIRECTORY_SEPARATOR.'FTP.php')) {
119                return true;
120            }
121        }
122        throw new BuildException('The FTP Deploy task requires the Net_FTP PEAR package.');
123    }
124   
125    /**
126     * The main entry point method.
127     */
128    public function main() {
129        $project = $this->getProject();
130       
131        require_once 'Net/FTP.php';
132        $ftp = new Net_FTP($this->host, $this->port);
133        $ret = $ftp->connect();
134        if(PEAR::isError($ret))
135            throw new BuildException('Could not connect to FTP server '.$this->host.' on port '.$this->port.': '.$ret->getMessage());
136        $ret = $ftp->login($this->username, $this->password);
137        if(PEAR::isError($ret))
138            throw new BuildException('Could not login to FTP server '.$this->host.' on port '.$this->port.' with username '.$this->username.': '.$ret->getMessage());
139       
140        if ($this->passive) {
141            $this->log('Setting passive mode', Project::MSG_INFO);
142            $ret = $ftp->setPassive();
143            if(PEAR::isError($ret)) {
144                $ftp->disconnect();
145                throw new BuildException('Could not set PASSIVE mode: '.$ret->getMessage());
146            }
147        }
148
149        // append '/' to the end if necessary
150        $dir = substr($this->dir, -1) == '/' ? $this->dir : $this->dir.'/';
151       
152        if($this->clearFirst) {
153            // TODO change to a loop through all files and directories within current directory
154            $this->log('Clearing directory '.$dir, Project::MSG_INFO);
155            $ftp->rm($dir, true);
156        }
157       
158        // Create directory just in case
159        $ret = $ftp->mkdir($dir, true);
160        if(PEAR::isError($ret)) {
161            $ftp->disconnect();
162            throw new BuildException('Could not create directory '.$dir.': '.$ret->getMessage());
163        }
164       
165        $ret = $ftp->cd($dir);
166        if(PEAR::isError($ret)) {
167            $ftp->disconnect();
168            throw new BuildException('Could not change to directory '.$dir.': '.$ret->getMessage());
169        }
170       
171        $fs = FileSystem::getFileSystem();
172        $convert = $fs->getSeparator() == '\\';
173       
174        foreach($this->filesets as $fs) {
175            $ds = $fs->getDirectoryScanner($project);
176            $fromDir  = $fs->getDir($project);
177            $srcFiles = $ds->getIncludedFiles();
178            $srcDirs  = $ds->getIncludedDirectories();
179            foreach($srcDirs as $dirname) {
180                if($convert)
181                    $dirname = str_replace('\\', '/', $dirname);
182                $this->log('Will create directory '.$dirname, Project::MSG_VERBOSE);
183                $ret = $ftp->mkdir($dirname, true);
184                if(PEAR::isError($ret))    {
185                    $ftp->disconnect();
186                    throw new BuildException('Could not create directory '.$dirname.': '.$ret->getMessage());
187                }
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                    $ftp->disconnect();
197                    throw new BuildException('Could not deploy file '.$filename.': '.$ret->getMessage());
198                }
199            }
200        }
201       
202        $ftp->disconnect();
203    }
204}
205?>