123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- require_once 'phing/Task.php';
- 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;
-
- 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;
- }
-
- function createFileSet() {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
-
- 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.');
- }
-
-
- 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, Project::MSG_VERBOSE);
- }
-
- $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, Project::MSG_VERBOSE);
- }
-
- if ($this->passive) {
- $this->log('Setting passive mode', Project::MSG_INFO);
- $ret = $ftp->setPassive();
- if(@PEAR::isError($ret)) {
- $ftp->disconnect();
- throw new BuildException('Could not set PASSIVE mode: '.$ret->getMessage());
- }
- }
-
- $dir = substr($this->dir, -1) == '/' ? $this->dir : $this->dir.'/';
-
- if($this->clearFirst) {
-
- $this->log('Clearing directory '.$dir, Project::MSG_INFO);
- $ftp->rm($dir, true);
- }
-
-
- $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, Project::MSG_VERBOSE);
- }
-
- $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, Project::MSG_VERBOSE);
- $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, Project::MSG_VERBOSE);
- $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', Project::MSG_VERBOSE);
- }
- }
- ?>
|