PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Varien/Io/Sftp.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 236 lines | 121 code | 19 blank | 96 comment | 21 complexity | 23b11045f52a68745126f8db9ddacb03 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Io
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. require_once('phpseclib/Net/SFTP.php');
  27. /**
  28. * Sftp client interface
  29. *
  30. * @category Varien
  31. * @package Varien_Io
  32. * @author Magento Core Team <core@magentocommerce.com>
  33. * @link http://www.php.net/manual/en/function.ssh2-connect.php
  34. */
  35. class Varien_Io_Sftp extends Varien_Io_Abstract implements Varien_Io_Interface
  36. {
  37. const REMOTE_TIMEOUT = 10;
  38. const SSH2_PORT = 22;
  39. /**
  40. * @var Net_SFTP $_connection
  41. */
  42. protected $_connection = null;
  43. /**
  44. * Open a SFTP connection to a remote site.
  45. *
  46. * @param array $args Connection arguments
  47. * @param string $args[host] Remote hostname
  48. * @param string $args[username] Remote username
  49. * @param string $args[password] Connection password
  50. * @param int $args[timeout] Connection timeout [=10]
  51. *
  52. */
  53. public function open(array $args = array())
  54. {
  55. if (!isset($args['timeout'])) {
  56. $args['timeout'] = self::REMOTE_TIMEOUT;
  57. }
  58. if (strpos($args['host'], ':') !== false) {
  59. list($host, $port) = explode(':', $args['host'], 2);
  60. } else {
  61. $host = $args['host'];
  62. $port = self::SSH2_PORT;
  63. }
  64. $this->_connection = new Net_SFTP($host, $port, $args['timeout']);
  65. if (!$this->_connection->login($args['username'], $args['password'])) {
  66. throw new Exception(sprintf("Unable to open SFTP connection as %s@%s", $args['username'], $args['host']));
  67. }
  68. }
  69. /**
  70. * Close a connection
  71. *
  72. */
  73. public function close()
  74. {
  75. return $this->_connection->disconnect();
  76. }
  77. /**
  78. * Create a directory
  79. *
  80. * @param $mode Ignored here; uses logged-in user's umask
  81. * @param $recursive Analogous to mkdir -p
  82. *
  83. * Note: if $recursive is true and an error occurs mid-execution,
  84. * false is returned and some part of the hierarchy might be created.
  85. * No rollback is performed.
  86. */
  87. public function mkdir($dir, $mode=0777, $recursive=true)
  88. {
  89. if ($recursive) {
  90. $no_errors = true;
  91. $dirlist = explode('/', $dir);
  92. reset($dirlist);
  93. $cwd = $this->_connection->pwd();
  94. while ($no_errors && ($dir_item = next($dirlist))) {
  95. $no_errors = ($this->_connection->mkdir($dir_item) && $this->_connection->chdir($dir_item));
  96. }
  97. $this->_connection->chdir($cwd);
  98. return $no_errors;
  99. } else {
  100. return $this->_connection->mkdir($dir);
  101. }
  102. }
  103. /**
  104. * Delete a directory
  105. *
  106. */
  107. public function rmdir($dir, $recursive=false)
  108. {
  109. if ($recursive) {
  110. $no_errors = true;
  111. $cwd = $this->_connection->pwd();
  112. if(!$this->_connection->chdir($dir)) {
  113. throw new Exception("chdir(): $dir: Not a directory");
  114. }
  115. $list = $this->_connection->nlist();
  116. if (!count($list)) {
  117. // Go back
  118. $this->_connection->chdir($pwd);
  119. return $this->rmdir($dir, false);
  120. } else {
  121. foreach ($list as $filename) {
  122. if($this->_connection->chdir($filename)) { // This is a directory
  123. $this->_connection->chdir('..');
  124. $no_errors = $no_errors && $this->rmdir($filename, $recursive);
  125. } else {
  126. $no_errors = $no_errors && $this->rm($filename);
  127. }
  128. }
  129. }
  130. $no_errors = $no_errors && ($this->_connection->chdir($pwd) && $this->_connection->rmdir($dir));
  131. return $no_errors;
  132. } else {
  133. return $this->_connection->rmdir($dir);
  134. }
  135. }
  136. /**
  137. * Get current working directory
  138. *
  139. */
  140. public function pwd()
  141. {
  142. return $this->_connection->pwd();
  143. }
  144. /**
  145. * Change current working directory
  146. *
  147. */
  148. public function cd($dir)
  149. {
  150. return $this->_connection->chdir($dir);
  151. }
  152. /**
  153. * Read a file
  154. *
  155. */
  156. public function read($filename, $dest=null)
  157. {
  158. if (is_null($dest)) {
  159. $dest = false;
  160. }
  161. return $this->_connection->get($filename, $dest);
  162. }
  163. /**
  164. * Write a file
  165. * @param $src Must be a local file name
  166. */
  167. public function write($filename, $src, $mode=null)
  168. {
  169. return $this->_connection->put($filename, $src);
  170. }
  171. /**
  172. * Delete a file
  173. *
  174. */
  175. public function rm($filename)
  176. {
  177. return $this->_connection->delete($filename);
  178. }
  179. /**
  180. * Rename or move a directory or a file
  181. *
  182. */
  183. public function mv($src, $dest)
  184. {
  185. return $this->_connection->rename($src, $dest);
  186. }
  187. /**
  188. * Chamge mode of a directory or a file
  189. *
  190. */
  191. public function chmod($filename, $mode)
  192. {
  193. return $this->_connection->chmod($mode, $filename);
  194. }
  195. /**
  196. * Get list of cwd subdirectories and files
  197. *
  198. */
  199. public function ls($grep=null)
  200. {
  201. $list = $this->_connection->nlist();
  202. $pwd = $this->pwd();
  203. $result = array();
  204. foreach($list as $name) {
  205. $result[] = array(
  206. 'text' => $name,
  207. 'id' => "{$pwd}{$name}",
  208. );
  209. }
  210. return $result;
  211. }
  212. public function rawls()
  213. {
  214. $list = $this->_connection->rawlist();
  215. return $list;
  216. }
  217. }