| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | require_once 'phing/Task.php'; |
|---|
| 23 | require_once 'PEAR.php'; |
|---|
| 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 | 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 | 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 150 | $dir = substr($this->dir, -1) == '/' ? $this->dir : $this->dir.'/'; |
|---|
| 151 | |
|---|
| 152 | if($this->clearFirst) { |
|---|
| 153 | |
|---|
| 154 | $this->log('Clearing directory '.$dir, Project::MSG_INFO); |
|---|
| 155 | $ftp->rm($dir, true); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 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 | ?> |
|---|