RSI helpers  0.1
RSI helpers
Ftp.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Wrapper;
4 
5 /**
6  * FTP wrapper.
7  */
8 class Ftp{
9 
10  protected $_connection = null;
11 
12  public function __construct($host,$port = 21,$secure = false,$timeout = 90){
13  $this->_connection = $secure ? ftp_ssl_connect($host,$port,$timeout) : ftp_connect($host,$port,$timeout);
14  if(!$this->_connection) throw new \Exception("Could not connect to '$host:$port'");
15  }
16 
17  public function __destruct(){
18  $this->close();
19  }
20 
21  public function close(){
22  if($this->_connection) ftp_close($this->_connection);
23  $this->_connection = null;
24  }
25 
26  public function putContents($remote,$data,$mode = FTP_BINARY){
27  $file = fopen('php://memory','r+');
28  fputs($file,$data);
29  rewind($file);
30  $result = $this->fput($remote,$file,$mode);
31  fclose($file);
32  return $result;
33  }
34 
35  public function getContents($remote,$mode = FTP_BINARY){
36  $file = fopen('php://memory','r+');
37  $data = false;
38  if($this->fget($file,$remote,$mode)){
39  rewind($file);
40  while(!feof($file)) $data .= fread($file,1024);
41  fclose($file);
42  }
43  return $data;
44  }
45 
46  public function mkdir($path,$mode = 0777){
47  $result = true;
48  if($this->nlist($path) === false){
49  $this->mkdir(dirname($path));
50  $result = ftp_mkdir($this->_connection,$path);
51  if($mode) $this->chmod($mode,$path);
52  }
53  return $result;
54  }
55 
56  public function isDir($path){
57  $current = $this->pwd();
58  $result = false;
59  try{
60  if($result = $this->chdir($path)) $this->chdir($current);
61  }
62  catch(\Exception $e){}
63  return $result;
64  }
65  /**
66  * Find and filter files.
67  * @see \\Rsi\\File::find
68  */
69  public function find($path,$filters = null,$recursive = false){
70  if(!$this->isDir($path)) return false;
71  if(!$filters) $filters = [];
72  if(!array_key_exists(\Rsi\File::FIND_FILTER_TYPE,$filters)) $filters[\Rsi\File::FIND_FILTER_TYPE] = \Rsi\File::FIND_TYPE_FILE;
73  $files = [];
74  if($dir = $this->nlist($path)) foreach($dir as $entry) if(trim(basename($entry),'.')) try{
75  $info = ['dir' => $is_dir = $this->isDir($entry)];
76  if($is_dir && $recursive) $files += $this->find($entry,$filters,$recursive);
77  foreach($filters as $key => $value){
78  if($operator = preg_match('/^(\\w+)(\\W+)$/',$key,$match) ? $match[2] : null) $key = $match[1];
79  else $operator = '==';
80  switch($key){
81  case \Rsi\File::FIND_FILTER_TYPE:
82  if($value == \Rsi\File::FIND_TYPE_ALL) break;
83  if(($value & \Rsi\File::FIND_TYPE_DIR) && !$is_dir) continue 3;
84  if(($value & \Rsi\File::FIND_TYPE_FILE) && $is_dir) continue 3;
85  break;
86  case \Rsi\File::FIND_FILTER_NAME:
87  if(!\Rsi\Str::operator($info[$key] = $entry,$operator,$value)) continue 3;
88  break;
89  case \Rsi\File::FIND_FILTER_TIME:
90  if($is_dir || !\Rsi\Str::operator($info[$key] = $this->mdtm($entry),$operator,$value)) continue 3;
91  break;
92  case \Rsi\File::FIND_FILTER_SIZE:
93  if($is_dir || !\Rsi\Str::operator($info[$key] = $this->size($entry),$operator,\Rsi\Number::shorthandBytes($value))) continue 3;
94  break;
95  case \Rsi\File::FIND_FILTER_FUNC:
96  if(!($info[$key] = call_user_func($value,$full,$info))) continue 3;
97  break;
98  }
99  }
100  $files[$entry] = $info;
101  }
102  catch(\Exception $e){}
103  return $files;
104  }
105  /**
106  * Find files.
107  * @see \\Rsi\\File::dir
108  */
109  public function dir($path,$pattern = null,$recursive = false){
110  $filters = [\Rsi\File::FIND_FILTER_TYPE => \Rsi\File::FIND_TYPE_FILE];
111  if($pattern) $filters[\Rsi\File::FIND_FILTER_NAME . '//'] = '/^' . strtr(preg_quote($pattern,'/') . '$/i',['\\*' => '.*','\\?' => '.']);
112  return ($files = $this->find($path,$filters,$recursive)) ? array_keys($files) : $files;
113  }
114 
115  public function __call($func_name,$params){
116  return call_user_func_array('ftp_' . \Rsi\Str::snake($func_name),array_merge([$this->_connection],$params));
117  }
118 
119 }
putContents($remote, $data, $mode=FTP_BINARY)
Definition: Ftp.php:26
const FIND_TYPE_DIR
Definition: File.php:17
const FIND_FILTER_TYPE
Specify the type (see FIND_TYPE_* constants).
Definition: File.php:10
find($path, $filters=null, $recursive=false)
Find and filter files.
Definition: Ftp.php:69
getContents($remote, $mode=FTP_BINARY)
Definition: Ftp.php:35
dir($path, $pattern=null, $recursive=false)
Find files.
Definition: Ftp.php:109
static shorthandBytes($size)
Returns the size in bytes for shorthand notations (e.g.
Definition: Number.php:32
const FIND_TYPE_ALL
Definition: File.php:19
mkdir($path, $mode=0777)
Definition: Ftp.php:46
const FIND_TYPE_FILE
Definition: File.php:18
Definition: Color.php:3
static operator($ref, $operator,$value, $default=false)
Evaluate an operator between a value and a reference value.
Definition: Str.php:385
__call($func_name, $params)
Definition: Ftp.php:115
static snake($str, $delimiter='_')
Converts a CamelCased string to snake_case.
Definition: Str.php:180
FTP wrapper.
Definition: Ftp.php:8
__destruct()
Definition: Ftp.php:17
__construct($host, $port=21, $secure=false, $timeout=90)
Definition: Ftp.php:12
isDir($path)
Definition: Ftp.php:56