Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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