ScpTask.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /*
  3. * $Id: ScpTask.php 905 2010-10-05 16:28:03Z 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. require_once 'phing/Task.php';
  22. /**
  23. * Copy files to and from a remote host using scp.
  24. *
  25. * @author Michiel Rook <mrook@php.net>
  26. * @author Johan Van den Brande <johan@vandenbrande.com>
  27. * @version $Id: ScpTask.php 905 2010-10-05 16:28:03Z mrook $
  28. * @package phing.tasks.ext
  29. */
  30. class ScpTask extends Task
  31. {
  32. protected $file = "";
  33. protected $filesets = array(); // all fileset objects assigned to this task
  34. protected $todir = "";
  35. protected $mode = null;
  36. protected $host = "";
  37. protected $port = 22;
  38. protected $username = "";
  39. protected $password = "";
  40. protected $autocreate = true;
  41. protected $fetch = false;
  42. protected $localEndpoint = "";
  43. protected $remoteEndpoint = "";
  44. protected $pubkeyfile = '';
  45. protected $privkeyfile = '';
  46. protected $privkeyfilepassphrase = '';
  47. protected $connection = null;
  48. protected $sftp = null;
  49. protected $count = 0;
  50. /**
  51. * Sets the remote host
  52. */
  53. public function setHost($h)
  54. {
  55. $this->host = $h;
  56. }
  57. /**
  58. * Returns the remote host
  59. */
  60. public function getHost()
  61. {
  62. return $this->host;
  63. }
  64. /**
  65. * Sets the remote host port
  66. */
  67. public function setPort($p)
  68. {
  69. $this->port = $p;
  70. }
  71. /**
  72. * Returns the remote host port
  73. */
  74. public function getPort()
  75. {
  76. return $this->port;
  77. }
  78. /**
  79. * Sets the mode value
  80. */
  81. public function setMode($value)
  82. {
  83. $this->mode = $value;
  84. }
  85. /**
  86. * Returns the mode value
  87. */
  88. public function getMode()
  89. {
  90. return $this->mode;
  91. }
  92. /**
  93. * Sets the username of the user to scp
  94. */
  95. public function setUsername($username)
  96. {
  97. $this->username = $username;
  98. }
  99. /**
  100. * Returns the username
  101. */
  102. public function getUsername()
  103. {
  104. return $this->username;
  105. }
  106. /**
  107. * Sets the password of the user to scp
  108. */
  109. public function setPassword($password)
  110. {
  111. $this->password = $password;
  112. }
  113. /**
  114. * Returns the password
  115. */
  116. public function getPassword()
  117. {
  118. return $this->password;
  119. }
  120. /**
  121. * Sets the public key file of the user to scp
  122. */
  123. public function setPubkeyfile($pubkeyfile)
  124. {
  125. $this->pubkeyfile = $pubkeyfile;
  126. }
  127. /**
  128. * Returns the pubkeyfile
  129. */
  130. public function getPubkeyfile()
  131. {
  132. return $this->pubkeyfile;
  133. }
  134. /**
  135. * Sets the private key file of the user to scp
  136. */
  137. public function setPrivkeyfile($privkeyfile)
  138. {
  139. $this->privkeyfile = $privkeyfile;
  140. }
  141. /**
  142. * Returns the private keyfile
  143. */
  144. public function getPrivkeyfile()
  145. {
  146. return $this->privkeyfile;
  147. }
  148. /**
  149. * Sets the private key file passphrase of the user to scp
  150. */
  151. public function setPrivkeyfilepassphrase($privkeyfilepassphrase)
  152. {
  153. $this->privkeyfilepassphrase = $privkeyfilepassphrase;
  154. }
  155. /**
  156. * Returns the private keyfile passphrase
  157. */
  158. public function getPrivkeyfilepassphrase($privkeyfilepassphrase)
  159. {
  160. return $this->privkeyfilepassphrase;
  161. }
  162. /**
  163. * Sets whether to autocreate remote directories
  164. */
  165. public function setAutocreate($autocreate)
  166. {
  167. $this->autocreate = (bool) $autocreate;
  168. }
  169. /**
  170. * Returns whether to autocreate remote directories
  171. */
  172. public function getAutocreate()
  173. {
  174. return $this->autocreate;
  175. }
  176. /**
  177. * Set destination directory
  178. */
  179. public function setTodir($todir)
  180. {
  181. $this->todir = $todir;
  182. }
  183. /**
  184. * Returns the destination directory
  185. */
  186. public function getTodir()
  187. {
  188. return $this->todir;
  189. }
  190. /**
  191. * Sets local filename
  192. */
  193. public function setFile($file)
  194. {
  195. $this->file = $file;
  196. }
  197. /**
  198. * Returns local filename
  199. */
  200. public function getFile()
  201. {
  202. return $this->file;
  203. }
  204. /**
  205. * Sets whether to send (default) or fetch files
  206. */
  207. public function setFetch($fetch)
  208. {
  209. $this->fetch = (bool) $fetch;
  210. }
  211. /**
  212. * Returns whether to send (default) or fetch files
  213. */
  214. public function getFetch()
  215. {
  216. return $this->fetch;
  217. }
  218. /**
  219. * Nested creator, creates a FileSet for this task
  220. *
  221. * @return FileSet The created fileset object
  222. */
  223. public function createFileSet() {
  224. $num = array_push($this->filesets, new FileSet());
  225. return $this->filesets[$num-1];
  226. }
  227. public function init()
  228. {
  229. if (!function_exists('ssh2_connect')) {
  230. throw new BuildException("To use ScpTask, you need to install the SSH extension.");
  231. }
  232. return true;
  233. }
  234. public function main()
  235. {
  236. if ($this->file == "" && empty($this->filesets)) {
  237. throw new BuildException("Missing either a nested fileset or attribute 'file'");
  238. }
  239. if ($this->host == "" || $this->username == "") {
  240. throw new BuildException("Attribute 'hostname' and 'username' must be set");
  241. }
  242. $this->connection = ssh2_connect($this->host, $this->port);
  243. if (is_null($this->connection)) {
  244. throw new BuildException("Could not establish connection to " . $this->host . ":" . $this->port . "!");
  245. }
  246. $could_auth = null;
  247. if ( $this->pubkeyfile ) {
  248. $could_auth = ssh2_auth_pubkey_file($this->connection, $this->username, $this->pubkeyfile, $this->privkeyfile, $this->privkeyfilepassphrase);
  249. } else {
  250. $could_auth = ssh2_auth_password($this->connection, $this->username, $this->password);
  251. }
  252. if (!$could_auth) {
  253. throw new BuildException("Could not authenticate connection!");
  254. }
  255. // prepare sftp resource
  256. if ($this->autocreate) {
  257. $this->sftp = ssh2_sftp($this->connection);
  258. }
  259. if ($this->file != "") {
  260. $this->copyFile($this->file, basename($this->file));
  261. } else {
  262. if ($this->fetch) {
  263. throw new BuildException("Unable to use filesets to retrieve files from remote server");
  264. }
  265. foreach($this->filesets as $fs) {
  266. $ds = $fs->getDirectoryScanner($this->project);
  267. $files = $ds->getIncludedFiles();
  268. $dir = $fs->getDir($this->project)->getPath();
  269. foreach($files as $file) {
  270. $path = $dir.DIRECTORY_SEPARATOR.$file;
  271. $this->copyFile($path, $file);
  272. }
  273. }
  274. }
  275. $this->log("Copied " . $this->counter . " file(s) " . ($this->fetch ? "from" : "to") . " '" . $this->host . "'");
  276. }
  277. protected function copyFile($local, $remote)
  278. {
  279. $path = rtrim($this->todir, "/") . "/";
  280. if ($this->fetch) {
  281. $localEndpoint = $path . $remote;
  282. $remoteEndpoint = $local;
  283. $ret = @ssh2_scp_recv($this->connection, $remoteEndpoint, $localEndpoint);
  284. if ($ret === false) {
  285. throw new BuildException("Could not fetch remote file '" . $remoteEndpoint . "'");
  286. }
  287. } else {
  288. $localEndpoint = $local;
  289. $remoteEndpoint = $path . $remote;
  290. if ($this->autocreate) {
  291. ssh2_sftp_mkdir($this->sftp, dirname($remoteEndpoint), (is_null($this->mode) ? 0777 : $this->mode), true);
  292. }
  293. if (!is_null($this->mode)) {
  294. $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint, $this->mode);
  295. } else {
  296. $ret = @ssh2_scp_send($this->connection, $localEndpoint, $remoteEndpoint);
  297. }
  298. if ($ret === false) {
  299. throw new BuildException("Could not create remote file '" . $remoteEndpoint . "'");
  300. }
  301. }
  302. $this->counter++;
  303. }
  304. }
  305. ?>