123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- require_once 'phing/Task.php';
- class ScpTask extends Task
- {
- protected $file = "";
- protected $filesets = array();
- protected $todir = "";
- protected $mode = null;
- protected $host = "";
- protected $port = 22;
- protected $username = "";
- protected $password = "";
- protected $autocreate = true;
- protected $fetch = false;
- protected $localEndpoint = "";
- protected $remoteEndpoint = "";
- protected $pubkeyfile = '';
- protected $privkeyfile = '';
- protected $privkeyfilepassphrase = '';
-
- protected $connection = null;
- protected $sftp = null;
-
- protected $count = 0;
-
- public function setHost($h)
- {
- $this->host = $h;
- }
-
- public function getHost()
- {
- return $this->host;
- }
-
- public function setPort($p)
- {
- $this->port = $p;
- }
-
- public function getPort()
- {
- return $this->port;
- }
-
- public function setMode($value)
- {
- $this->mode = $value;
- }
-
- public function getMode()
- {
- return $this->mode;
- }
-
- public function setUsername($username)
- {
- $this->username = $username;
- }
-
- public function getUsername()
- {
- return $this->username;
- }
-
- public function setPassword($password)
- {
- $this->password = $password;
- }
-
- public function getPassword()
- {
- return $this->password;
- }
-
-
- public function setPubkeyfile($pubkeyfile)
- {
- $this->pubkeyfile = $pubkeyfile;
- }
-
- public function getPubkeyfile()
- {
- return $this->pubkeyfile;
- }
-
-
- public function setPrivkeyfile($privkeyfile)
- {
- $this->privkeyfile = $privkeyfile;
- }
-
- public function getPrivkeyfile()
- {
- return $this->privkeyfile;
- }
-
-
- public function setPrivkeyfilepassphrase($privkeyfilepassphrase)
- {
- $this->privkeyfilepassphrase = $privkeyfilepassphrase;
- }
-
- public function getPrivkeyfilepassphrase($privkeyfilepassphrase)
- {
- return $this->privkeyfilepassphrase;
- }
-
-
- public function setAutocreate($autocreate)
- {
- $this->autocreate = (bool) $autocreate;
- }
-
-
- public function getAutocreate()
- {
- return $this->autocreate;
- }
-
-
- public function setTodir($todir)
- {
- $this->todir = $todir;
- }
-
- public function getTodir()
- {
- return $this->todir;
- }
-
- public function setFile($file)
- {
- $this->file = $file;
- }
-
- public function getFile()
- {
- return $this->file;
- }
-
-
- public function setFetch($fetch)
- {
- $this->fetch = (bool) $fetch;
- }
-
-
- public function getFetch()
- {
- return $this->fetch;
- }
-
-
- public function createFileSet() {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
- public function init()
- {
- if (!function_exists('ssh2_connect')) {
- throw new BuildException("To use ScpTask, you need to install the SSH extension.");
- }
- return true;
- }
- public function main()
- {
- if ($this->file == "" && empty($this->filesets)) {
- throw new BuildException("Missing either a nested fileset or attribute 'file'");
- }
-
- if ($this->host == "" || $this->username == "") {
- throw new BuildException("Attribute 'hostname' and 'username' must be set");
- }
- $this->connection = ssh2_connect($this->host, $this->port);
- if (is_null($this->connection)) {
- throw new BuildException("Could not establish connection to " . $this->host . ":" . $this->port . "!");
- }
- $could_auth = null;
- if ( $this->pubkeyfile ) {
- $could_auth = ssh2_auth_pubkey_file($this->connection, $this->username, $this->pubkeyfile, $this->privkeyfile, $this->privkeyfilepassphrase);
- } else {
- $could_auth = ssh2_auth_password($this->connection, $this->username, $this->password);
- }
- if (!$could_auth) {
- throw new BuildException("Could not authenticate connection!");
- }
-
-
- if ($this->autocreate) {
- $this->sftp = ssh2_sftp($this->connection);
- }
-
- if ($this->file != "") {
- $this->copyFile($this->file, basename($this->file));
- } else {
- if ($this->fetch) {
- throw new BuildException("Unable to use filesets to retrieve files from remote server");
- }
-
- foreach($this->filesets as $fs) {
- $ds = $fs->getDirectoryScanner($this->project);
- $files = $ds->getIncludedFiles();
- $dir = $fs->getDir($this->project)->getPath();
- foreach($files as $file) {
- $path = $dir.DIRECTORY_SEPARATOR.$file;
- $this->copyFile($path, $file);
- }
- }
- }
-
- $this->log("Copied " . $this->counter . " file(s) " . ($this->fetch ? "from" : "to") . " '" . $this->host . "'");
- }
-
- protected function copyFile($local, $remote)
- {
- $path = rtrim($this->todir, "/") . "/";
-
- if ($this->fetch) {
- $localEndpoint = $path . $remote;
- $remoteEndpoint = $local;
- $ret = @ssh2_scp_recv($this->connection, $remoteEndpoint, $localEndpoint);
-
- if ($ret === false) {
- throw new BuildException("Could not fetch remote file '" . $remoteEndpoint . "'");
- }
- } else {
- $localEndpoint = $local;
- $remoteEndpoint = $path . $remote;
- if ($this->autocreate) {
- ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), (is_null($this->mode) ? 0777 : $this->mode), true);
- }
-
- if (!is_null($this->mode)) {
- $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint, $this->mode);
- } else {
- $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint);
- }
- if ($ret === false) {
- throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'");
- }
- }
- $this->counter++;
- }
- }
- ?>
|