Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sftp.php
Go to the documentation of this file.
1 <?php
8 
14 class Sftp extends AbstractIo
15 {
16  const REMOTE_TIMEOUT = 10;
17  const SSH2_PORT = 22;
18 
22  protected $_connection = null;
23 
35  public function open(array $args = [])
36  {
37  if (!isset($args['timeout'])) {
38  $args['timeout'] = self::REMOTE_TIMEOUT;
39  }
40  if (strpos($args['host'], ':') !== false) {
41  list($host, $port) = explode(':', $args['host'], 2);
42  } else {
43  $host = $args['host'];
44  $port = self::SSH2_PORT;
45  }
46  $this->_connection = new \phpseclib\Net\SFTP($host, $port, $args['timeout']);
47  if (!$this->_connection->login($args['username'], $args['password'])) {
48  throw new \Exception(
49  sprintf("Unable to open SFTP connection as %s@%s", $args['username'], $args['host'])
50  );
51  }
52  }
53 
59  public function close()
60  {
61  $this->_connection->disconnect();
62  }
63 
78  public function mkdir($dir, $mode = 0777, $recursive = true)
79  {
80  if ($recursive) {
81  $no_errors = true;
82  $dirList = explode('/', $dir);
83  reset($dirList);
84  $currentWorkingDir = $this->_connection->pwd();
85  while ($no_errors && ($dir_item = next($dirList))) {
86  $no_errors = $this->_connection->mkdir($dir_item) && $this->_connection->chdir($dir_item);
87  }
88  $this->_connection->chdir($currentWorkingDir);
89  return $no_errors;
90  } else {
91  return $this->_connection->mkdir($dir);
92  }
93  }
94 
104  public function rmdir($dir, $recursive = false)
105  {
106  if ($recursive) {
107  $no_errors = true;
108  $currentWorkingDir = $this->pwd();
109  if (!$this->_connection->chdir($dir)) {
110  throw new \Exception("chdir(): {$dir}: Not a directory");
111  }
112  $list = $this->_connection->nlist();
113  if (!count($list)) {
114  // Go back
115  $this->_connection->chdir($currentWorkingDir);
116  return $this->rmdir($dir, false);
117  } else {
118  foreach ($list as $filename) {
119  if ($this->_connection->chdir($filename)) {
120  // This is a directory
121  $this->_connection->chdir('..');
122  $no_errors = $no_errors && $this->rmdir($filename, $recursive);
123  } else {
124  $no_errors = $no_errors && $this->rm($filename);
125  }
126  }
127  }
128  $no_errors = $no_errors && ($this->_connection->chdir(
129  $currentWorkingDir
130  ) && $this->_connection->rmdir(
131  $dir
132  ));
133  return $no_errors;
134  } else {
135  return $this->_connection->rmdir($dir);
136  }
137  }
138 
144  public function pwd()
145  {
146  return $this->_connection->pwd();
147  }
148 
156  public function cd($dir)
157  {
158  return $this->_connection->chdir($dir);
159  }
160 
168  public function read($filename, $destination = null)
169  {
170  if ($destination === null) {
171  $destination = false;
172  }
173  return $this->_connection->get($filename, $destination);
174  }
175 
184  public function write($filename, $source, $mode = null)
185  {
186  $mode = is_readable($source) ? \phpseclib\Net\SFTP::SOURCE_LOCAL_FILE : \phpseclib\Net\SFTP::SOURCE_STRING;
187  return $this->_connection->put($filename, $source, $mode);
188  }
189 
197  public function rm($filename)
198  {
199  return $this->_connection->delete($filename);
200  }
201 
210  public function mv($source, $destination)
211  {
212  return $this->_connection->rename($source, $destination);
213  }
214 
222  public function chmod($filename, $mode)
223  {
224  return $this->_connection->chmod($mode, $filename);
225  }
226 
235  public function ls($grep = null)
236  {
237  $list = $this->_connection->nlist();
238  $currentWorkingDir = $this->pwd();
239  $result = [];
240  foreach ($list as $name) {
241  $result[] = ['text' => $name, 'id' => "{$currentWorkingDir}/{$name}"];
242  }
243  return $result;
244  }
245 
251  public function rawls()
252  {
253  $list = $this->_connection->rawlist();
254  return $list;
255  }
256 }
if(!file_exists($installConfigFile)) $dirList
Definition: bootstrap.php:57
mkdir($dir, $mode=0777, $recursive=true)
Definition: Sftp.php:78
$source
Definition: source.php:23
read($filename, $destination=null)
Definition: Sftp.php:168
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
rmdir($dir, $recursive=false)
Definition: Sftp.php:104
mv($source, $destination)
Definition: Sftp.php:210
write($filename, $source, $mode=null)
Definition: Sftp.php:184
if(!isset($_GET['name'])) $name
Definition: log.php:14