Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Ftp.php
Go to the documentation of this file.
1 <?php
8 
12 class Ftp
13 {
19  protected $_conn = false;
20 
27  protected function checkConnected()
28  {
29  if (!$this->_conn) {
30  throw new \Exception(__CLASS__ . " - no connection established with server");
31  }
32  }
33 
40  public function mdkir($name)
41  {
42  $this->checkConnected();
43  return @ftp_mkdir($this->_conn, $name);
44  }
45 
53  public function mkdirRecursive($path, $mode = 0777)
54  {
55  $this->checkConnected();
56  $dir = explode("/", $path);
57  $path = "";
58  $ret = true;
59  for ($i = 0; $i < count($dir); $i++) {
60  $path .= "/" . $dir[$i];
61  if (!@ftp_chdir($this->_conn, $path)) {
62  @ftp_chdir($this->_conn, "/");
63  if (!@ftp_mkdir($this->_conn, $path)) {
64  $ret = false;
65  break;
66  } else {
67  @ftp_chmod($this->_conn, $mode, $path);
68  }
69  }
70  }
71  return $ret;
72  }
73 
82  public function login($login = "anonymous", $password = "[email protected]")
83  {
84  $this->checkConnected();
85  $res = @ftp_login($this->_conn, $login, $password);
86  if (!$res) {
87  throw new \Exception("Invalid login credentials");
88  }
89  return $res;
90  }
91 
99  public function validateConnectionString($string)
100  {
101  $data = parse_url($string);
102  if (false === $data) {
103  throw new \Exception("Connection string invalid: '{$string}'");
104  }
105  if ($data['scheme'] != 'ftp') {
106  throw new \Exception("Support for scheme unsupported: '{$data['scheme']}'");
107  }
108 
109  // Decode user & password strings from URL
110  foreach (array_intersect(array_keys($data), ['user','pass']) as $key) {
111  $data[$key] = urldecode($data[$key]);
112  }
113 
114  return $data;
115  }
116 
127  public function connect($string, $timeout = 900)
128  {
129  $params = $this->validateConnectionString($string);
130  $port = isset($params['port']) ? intval($params['port']) : 21;
131 
132  $this->_conn = ftp_connect($params['host'], $port, $timeout);
133 
134  if (!$this->_conn) {
135  throw new \Exception("Cannot connect to host: {$params['host']}");
136  }
137  if (isset($params['user']) && isset($params['pass'])) {
138  $this->login($params['user'], $params['pass']);
139  } else {
140  $this->login();
141  }
142  if (isset($params['path'])) {
143  if (!$this->chdir($params['path'])) {
144  throw new \Exception("Cannot chdir after login to: {$params['path']}");
145  }
146  }
147  }
148 
158  public function fput($remoteFile, $handle, $mode = FTP_BINARY, $startPos = 0)
159  {
160  $this->checkConnected();
161  return @ftp_fput($this->_conn, $remoteFile, $handle, $mode, $startPos);
162  }
163 
173  public function put($remoteFile, $localFile, $mode = FTP_BINARY, $startPos = 0)
174  {
175  $this->checkConnected();
176  return ftp_put($this->_conn, $remoteFile, $localFile, $mode, $startPos);
177  }
178 
184  public function getcwd()
185  {
186  $d = $this->raw("pwd");
187  $data = explode(" ", $d[0], 3);
188  if (empty($data[1])) {
189  return false;
190  }
191  if (intval($data[0]) != 257) {
192  return false;
193  }
194  $out = trim($data[1], '"');
195  if ($out !== "/") {
196  $out = rtrim($out, "/");
197  }
198  return $out;
199  }
200 
207  public function raw($cmd)
208  {
209  $this->checkConnected();
210  return @ftp_raw($this->_conn, $cmd);
211  }
212 
226  public function upload($remote, $local, $dirMode = 0777, $ftpMode = FTP_BINARY)
227  {
228  $this->checkConnected();
229 
230  if (!file_exists($local)) {
231  throw new \Exception("Local file doesn't exist: {$local}");
232  }
233  if (!is_readable($local)) {
234  throw new \Exception("Local file is not readable: {$local}");
235  }
236  if (is_dir($local)) {
237  throw new \Exception("Directory given instead of file: {$local}");
238  }
239 
240  $globalPathMode = substr($remote, 0, 1) == "/";
241  $dirname = dirname($remote);
242  $cwd = $this->getcwd();
243  if (false === $cwd) {
244  throw new \Exception("Server returns something awful on PWD command");
245  }
246 
247  if (!$globalPathMode) {
248  $dirname = $cwd . "/" . $dirname;
249  $remote = $cwd . "/" . $remote;
250  }
251  $res = $this->mkdirRecursive($dirname, $dirMode);
252  $this->chdir($cwd);
253 
254  if (!$res) {
255  return false;
256  }
257  return $this->put($remote, $local, $ftpMode);
258  }
259 
268  public function download($remote, $local, $ftpMode = FTP_BINARY)
269  {
270  $this->checkConnected();
271  return $this->get($local, $remote, $ftpMode);
272  }
273 
280  public function pasv($pasv)
281  {
282  $this->checkConnected();
283  return @ftp_pasv($this->_conn, (bool)$pasv);
284  }
285 
291  public function close()
292  {
293  if ($this->_conn) {
294  @ftp_close($this->_conn);
295  }
296  }
297 
305  public function chmod($mode, $remoteFile)
306  {
307  $this->checkConnected();
308  return @ftp_chmod($this->_conn, $mode, $remoteFile);
309  }
310 
317  public function chdir($dir)
318  {
319  $this->checkConnected();
320  return @ftp_chdir($this->_conn, $dir);
321  }
322 
328  public function cdup()
329  {
330  $this->checkConnected();
331  return @ftp_cdup($this->_conn);
332  }
333 
344  public function get($localFile, $remoteFile, $fileMode = FTP_BINARY, $resumeOffset = 0)
345  {
346  $remoteFile = $this->correctFilePath($remoteFile);
347  $this->checkConnected();
348  return @ftp_get($this->_conn, $localFile, $remoteFile, $fileMode, $resumeOffset);
349  }
350 
357  public function nlist($dir = "/")
358  {
359  $this->checkConnected();
360  $dir = $this->correctFilePath($dir);
361  return @ftp_nlist($this->_conn, $dir);
362  }
363 
371  public function rawlist($dir = "/", $recursive = false)
372  {
373  $this->checkConnected();
374  $dir = $this->correctFilePath($dir);
375  return @ftp_rawlist($this->_conn, $dir, $recursive);
376  }
377 
384  public static function byteconvert($bytes)
385  {
386  $symbol = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
387  $exp = floor(log($bytes) / log(1024));
388  return sprintf('%.2f ' . $symbol[$exp], $bytes / pow(1024, floor($exp)));
389  }
390 
397  public static function chmodnum($chmod)
398  {
399  $trans = ['-' => '0', 'r' => '4', 'w' => '2', 'x' => '1'];
400  $chmod = substr(strtr($chmod, $trans), 1);
401  $array = str_split($chmod, 3);
402  return array_sum(str_split($array[0])) . array_sum(str_split($array[1])) . array_sum(str_split($array[2]));
403  }
404 
412  public function fileExists($path, $excludeIfIsDir = true)
413  {
414  $path = $this->correctFilePath($path);
415  $globalPathMode = substr($path, 0, 1) == "/";
416 
417  $file = basename($path);
418  $dir = $globalPathMode ? dirname($path) : $this->getcwd() . "/" . $path;
419  $data = $this->ls($dir);
420  foreach ($data as $row) {
421  if ($file == $row['name']) {
422  if ($excludeIfIsDir && $row['dir']) {
423  continue;
424  }
425  return true;
426  }
427  }
428  return false;
429  }
430 
439  public function ls($dir = "/", $recursive = false)
440  {
441  $dir = $this->correctFilePath($dir);
442  $rawfiles = (array)$this->rawlist($dir, $recursive);
443  $structure = [];
444  $arraypointer = & $structure;
445  foreach ($rawfiles as $rawfile) {
446  if ($rawfile[0] == '/') {
447  $paths = array_slice(explode('/', str_replace(':', '', $rawfile)), 1);
448  $arraypointer = & $structure;
449  foreach ($paths as $path) {
450  foreach ($arraypointer as $i => $file) {
451  if ($file['name'] == $path) {
452  $arraypointer = & $arraypointer[$i]['children'];
453  break;
454  }
455  }
456  }
457  } elseif (!empty($rawfile)) {
458  $info = preg_split("/[\s]+/", $rawfile, 9);
459  $arraypointer[] = [
460  'name' => $info[8],
461  'dir' => $info[0][0] == 'd',
462  'size' => (int)$info[4],
463  'chmod' => self::chmodnum($info[0]),
464  'rawdata' => $info,
465  'raw' => $rawfile,
466  ];
467  }
468  }
469  return $structure;
470  }
471 
478  public function correctFilePath($str)
479  {
480  $str = str_replace("\\", "/", $str);
481  $str = preg_replace("/^.\//", "", $str);
482  return $str;
483  }
484 
491  public function delete($file)
492  {
493  $this->checkConnected();
494  $file = $this->correctFilePath($file);
495  return @ftp_delete($this->_conn, $file);
496  }
497 }
mkdirRecursive($path, $mode=0777)
Definition: Ftp.php:53
static byteconvert($bytes)
Definition: Ftp.php:384
rawlist($dir="/", $recursive=false)
Definition: Ftp.php:371
validateConnectionString($string)
Definition: Ftp.php:99
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
upload($remote, $local, $dirMode=0777, $ftpMode=FTP_BINARY)
Definition: Ftp.php:226
static chmodnum($chmod)
Definition: Ftp.php:397
fput($remoteFile, $handle, $mode=FTP_BINARY, $startPos=0)
Definition: Ftp.php:158
login($login="anonymous", $password="[email protected]")
Definition: Ftp.php:82
download($remote, $local, $ftpMode=FTP_BINARY)
Definition: Ftp.php:268
chmod($mode, $remoteFile)
Definition: Ftp.php:305
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
fileExists($path, $excludeIfIsDir=true)
Definition: Ftp.php:412
ls($dir="/", $recursive=false)
Definition: Ftp.php:439
connect($string, $timeout=900)
Definition: Ftp.php:127
put($remoteFile, $localFile, $mode=FTP_BINARY, $startPos=0)
Definition: Ftp.php:173
$paths
Definition: _bootstrap.php:83
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$handle
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14