/demo/scalr_newui/app/src/LibWebta/library/IO/Transports/Transports/class.SSHTransport.php

https://github.com/kennethjiang/Wolke · PHP · 248 lines · 102 code · 32 blank · 114 comment · 15 complexity · 88a5007b3a929816687d11b8c354d9de MD5 · raw file

  1. <?
  2. /**
  3. * This file is a part of LibWebta, PHP class library.
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 2 of the GPL license,
  8. * that is bundled with this package in the file license.txt and is
  9. * available through the world-wide-web at the following url:
  10. * http://www.gnu.org/copyleft/gpl.html
  11. *
  12. * @category LibWebta
  13. * @package IO
  14. * @subpackage Transports
  15. * @copyright Copyright (c) 2003-2007 Webta Inc, http://www.gnu.org/licenses/gpl.html
  16. * @license http://www.gnu.org/licenses/gpl.html
  17. */
  18. Core::Load("NET/SSH");
  19. /**
  20. * SSH Transport
  21. *
  22. * @name SSHTransport
  23. * @category LibWebta
  24. * @package IO
  25. * @subpackage Transports
  26. * @version 1.0
  27. * @author Igor Savchenko <http://webta.net/company.html>
  28. */
  29. class SSHTransport implements ITransport
  30. {
  31. /**
  32. * SSH Connection
  33. *
  34. * @var SSH2
  35. */
  36. private $SSHConnection;
  37. /**
  38. * SSH Transport constructor
  39. *
  40. * @param string $ssh_host SSH Host
  41. * @param string $ssh_port SSH Port
  42. * @param string $ssh_login SSH Login
  43. * @param string $ssh_password SSH Password
  44. */
  45. public function __construct($ssh_host, $ssh_port, $ssh_login = false, $ssh_password = false)
  46. {
  47. $this->SSHConnection = Core::GetInstance("SSH2");
  48. $this->SSHHost = $ssh_host;
  49. $this->SSHPort = $ssh_port;
  50. if ($ssh_login && $ssh_password)
  51. $this->Auth("password", $ssh_login, $ssh_password);
  52. if ($this->SSHConnection instanceof SSH2)
  53. $this->Connect();
  54. else
  55. Core::RaiseError("Transports/SSHTransport: Cannot get SSH2 instance.");
  56. }
  57. /**
  58. * Authenticate user
  59. *
  60. * @param string $authmethod
  61. * @param variable-length argument lists
  62. */
  63. public function Auth($authmethod)
  64. {
  65. switch($authmethod)
  66. {
  67. case "password":
  68. $this->SSHConnection->AddPassword(func_get_arg(1), func_get_arg(2));
  69. break;
  70. case "pubkey":
  71. $this->SSHConnection->AddPubkey(func_get_arg(1), func_get_arg(2), func_get_arg(3), func_get_arg(4));
  72. break;
  73. }
  74. }
  75. /**
  76. * Return avaiable auth methods for this transport
  77. *
  78. * @return unknown
  79. */
  80. public function GetAuthMethods()
  81. {
  82. return array("password" => "Authenticate using login and password", "pubkey" => "Authenticate using a public key");
  83. }
  84. /**
  85. * @ignore
  86. *
  87. */
  88. public function __destruct()
  89. {
  90. unset($this->SSHConnection);
  91. }
  92. /**
  93. * Connect to SSH Server
  94. *
  95. * @access private
  96. * @return bool
  97. * @return bool
  98. */
  99. private function Connect()
  100. {
  101. if (!$this->SSHConnection->Connect($this->SSHHost, $this->SSHPort))
  102. Core::RaiseError(sprintf(_("Transports/SSHTransport: Cannot connect to %s:%s."), $this->SSHHost, $this->SSHPort));
  103. else
  104. return true;
  105. }
  106. /**
  107. * Read file
  108. *
  109. * @param string $filename
  110. * @return bool
  111. */
  112. public function Read($filename)
  113. {
  114. if (!$this->SSHConnection->IsConnected())
  115. $this->Connect();
  116. return $this->SSHConnection->GetFile($filename);
  117. }
  118. /**
  119. * Write file
  120. *
  121. * @param string $filename
  122. * @param string $content
  123. * @param bool $overwrite
  124. * @return bool
  125. */
  126. public function Write($filename, $content, $overwrite = true)
  127. {
  128. if (!$this->SSHConnection->IsConnected())
  129. $this->Connect();
  130. $tp = ($overwrite) ? "w+" : "a+";
  131. return $this->SSHConnection->SendFile($filename, $content, $tp, false);
  132. }
  133. /**
  134. * Copy file or folder
  135. *
  136. * @param string $old_path
  137. * @param string $new_path
  138. * @return bool
  139. */
  140. public function Copy($old_path, $new_path, $recursive = true)
  141. {
  142. if (!$this->SSHConnection->IsConnected())
  143. $this->Connect();
  144. $params = ($recursive) ? "-R" : "";
  145. return $this->SSHConnection->Exec("cp {$params} {$old_path} {$new_path}");
  146. }
  147. /**
  148. * Remove file or folder
  149. *
  150. * @param string $path
  151. * @return bool
  152. */
  153. public function Remove($path, $recursive = true)
  154. {
  155. if (!$this->SSHConnection->IsConnected())
  156. $this->Connect();
  157. $params = ($recursive) ? "-rf" : "-f";
  158. return $this->SSHConnection->Exec("rm {$params} {$path}");
  159. }
  160. /**
  161. * Chmod file or folder
  162. *
  163. * @param string $path
  164. * @param integer $perms
  165. * @return bool
  166. */
  167. public function Chmod($path, $perms, $recursive = true)
  168. {
  169. if (!$this->SSHConnection->IsConnected())
  170. $this->Connect();
  171. $params = ($recursive) ? "-R" : "";
  172. return $this->SSHConnection->Exec("chmod {$params} {$perms} {$path}");
  173. }
  174. /**
  175. * Move file or folder (rename)
  176. *
  177. * @param string $old_path
  178. * @param string $new_path
  179. * @return bool
  180. */
  181. public function Move($old_path, $new_path, $recursive = true)
  182. {
  183. if (!$this->SSHConnection->IsConnected())
  184. $this->Connect();
  185. $params = ($recursive) ? "-R" : "";
  186. return $this->SSHConnection->Exec("mv {$params} {$old_path} {$new_path}");
  187. }
  188. /**
  189. * Create net directory
  190. *
  191. * @param string $path
  192. * @return bool
  193. */
  194. public function MkDir($path)
  195. {
  196. if (!$this->SSHConnection->IsConnected())
  197. $this->Connect();
  198. return $this->SSHConnection->Exec("mkdir {$path}");
  199. }
  200. /**
  201. * Execute command
  202. *
  203. * @param string $command
  204. * @return bool
  205. */
  206. public function Execute($command)
  207. {
  208. if (!$this->SSHConnection->IsConnected())
  209. $this->Connect();
  210. $res = $this->SSHConnection->Exec($command);
  211. if ($res === true)
  212. $res = $this->SSHConnection->StdErr;
  213. return $res;
  214. }
  215. }
  216. ?>