wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.FTPUtil.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.FTPUtil.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 
20 /**
21  * @class FTPUtil
22  * @ingroup Util
23  * @brief FTPUtil provides support for ftp functionality.
24  *
25  * @author ingo herwig <ingo@wemove.com>
26  */
27 class FTPUtil
28 {
29  var $_errorMsg = '';
30  var $_ftpConnId = null;
31  var $_ftpRootDir = '';
32 
33  /**
34  * Get last error message.
35  * @return The error string
36  */
37  function getErrorMsg()
38  {
39  return $this->_errorMsg;
40  }
41  /**
42  * Open a FTP connection to a given server.
43  * @param params Initialization data given in an assoziative array with the following keys:
44  * server, port, login, password
45  * @return True on success, False else / error string provided by getErrorMsg()
46  */
47  function open($params)
48  {
49  // check ftp support
50  if (!function_exists(ftp_connect))
51  {
52  $this->_errorMsg = 'ftp extension missing';
53  return null;
54  }
55 
56  $this->_errorMsg = '';
57  // suppress warnings
58  $oldErrorLevel = error_reporting (E_ERROR | E_PARSE);
59  // set up basic connection
60  $conn_id = ftp_connect($params['server'], $params['port'], 10);
61  if ($conn_id)
62  {
63  // login with username and password
64  $login_result = ftp_login($conn_id, $params['login'], $params['password']);
65  // check connection
66  if ($login_result)
67  {
68  $this->_ftpConnId = $conn_id;
69  // remember the root dir, which is needed if a relative path is given in copy()
70  $this->_ftpRootDir = ftp_pwd($this->_ftpConnId);
71  // use passive mode
72  //ftp_pasv($this->_ftpConnId, true);
73 
74  return true;
75  }
76  else {
77  $this->_errorMsg = 'login failed';
78  }
79  }
80  else
81  $this->_errorMsg = 'connection to server failed';
82  // reset error level
83  error_reporting ($oldErrorLevel);
84  return false;
85  }
86  /*
87  * Get the files in a remote directory that match a pattern
88  * @param dir The directory to search in [default: .]
89  * @param pattern The pattern (regexp) to match [default: /./]
90  * @param prependDirectoryName True/False whether to prepend the directory name to each file [default: false]
91  * @param recursive True/False whether to recurse into subdirectories [default: false]
92  * @result An array containing the filenames or null if failed, error string provided by getErrorMsg()
93  */
94  function getFiles($directory='.', $pattern='/./', $prependDirectoryName=false, $recursive=false)
95  {
96  if ($this->_ftpConnId == null)
97  {
98  $this->_errorMsg = 'connect to server first';
99  return null;
100  }
101 
102  $result = null;
103  $isfile = ftp_size($this->_ftpConnId, urldecode($directory));
104  if ($isfile == "-1")
105  {
106  $result = array();
107  $fileList = ftp_nlist($this->_ftpConnId, $directory);
108  foreach ($fileList as $file)
109  {
110  $isfile = ftp_size($this->_ftpConnId, urldecode($file));
111  if($recursive && $isfile == "-1")
112  {
113  $files = $this->getFiles($file , $pattern, $prependDirectoryName, $recursive);
114  $result = array_merge($result, $files);
115  }
116  else if($isfile != "-1" && preg_match($pattern, $file))
117  {
118  if (!$prependDirectoryName)
119  $file = substr($file, strrpos($file, '/')+1);
120  array_push($result, $file);
121  }
122  }
123  }
124  else
125  $this->_errorMsg = "The directory '".$directory."' does not exist.";
126  return $result;
127  }
128  /**
129  * Get size an modification date of a remote file.
130  * @param file The name of the file to get the info for
131  * @result An assoziative array with keys 'size' and 'mtime' (values -1 indicate an error)
132  */
133  function getFileInfo($file)
134  {
135  if ($this->_ftpConnId == null)
136  {
137  $this->_errorMsg = 'connect to server first';
138  return null;
139  }
140 
141  ftp_chdir($this->_ftpConnId, $this->_ftpRootDir);
142  $fileInfo = array();
143  $file = urldecode($file);
144  $fileInfo['size'] = ftp_size($this->_ftpConnId, $file);
145  $fileInfo['mtime'] = ftp_mdtm($this->_ftpConnId, $file);
146 
147  return $fileInfo;
148  }
149  /**
150  * Transfer a file to a given server via FTP.
151  * @param file The name of the file to transfer (path relative to the script)
152  * @param transferMode The transfer mode [FTP_ASCII | FTP_BINARY]
153  * @param toDir The server upload directory (relative to serverroot) [default: './']
154  * @param createDir True/False wether to create the directory if not existing [default: true]
155  * @param destName The destination file / the same as the source file if null [default: null]
156  * @return True on success, False else / error string provided by getLastError()
157  */
158  function copy($file, $transferMode, $toDir='./', $createDir=true, $destName=null)
159  {
160  if ($this->_ftpConnId == null)
161  {
162  $this->_errorMsg = 'connect to server first';
163  return null;
164  }
165 
166  $this->_errorMsg = '';
167  // suppress warnings
168  $oldErrorLevel = error_reporting (E_ERROR | E_PARSE);
169  if ($this->_ftpConnId != null && file_exists($file))
170  {
171  // upload the file
172  if ($destName == null)
173  $destName = basename($file);
174 
175  // change dir (if we don't have an absolute path change dir to root dir)
176  if ($toDir[0] != '/')
177  ftp_chdir($this->_ftpConnId, $this->_ftpRootDir);
178  if (!ftp_chdir($this->_ftpConnId, $toDir))
179  {
180  if ($createDir)
181  {
182  ftp_mkdir($this->_ftpConnId, $toDir);
183  ftp_chdir($this->_ftpConnId, $toDir);
184  }
185  else
186  {
187  $this->_errorMsg = 'change dir failed: '.$toDir;
188  return false;
189  }
190  }
191 
192  // upload file
193  $upload = ftp_put($this->_ftpConnId, $destName, $file, $transferMode);
194  // check upload status
195  if (!$upload)
196  $this->_errorMsg = 'file upload failed';
197  else
198  return true;
199  }
200  // reset error level
201  error_reporting ($oldErrorLevel);
202  return false;
203  }
204  /**
205  * Synchronize a file on the server. The method copies the local file to
206  * the server if the remote file does not exist, differs in size or
207  * is older than the local file.
208  * @param localFile The name of the local file (path relative to the script)
209  * @param remoteFile The name of the remote file (path relative to serverroot)
210  * @result True/False wether the file was transfered or not
211  */
212  function synchronize($localFile, $remoteFile)
213  {
214  if ($this->_ftpConnId == null)
215  {
216  $this->_errorMsg = 'connect to server first';
217  return null;
218  }
219 
220  $localInfo = stat($localFile);
221  $remoteInfo = $this->getFileInfo($remoteFile);
222 
223  // transfer file only if it is newer or has another size or something is wrong with the
224  // remote file
225  if ($remoteInfo['size'] == -1 || $remoteInfo['size'] != $localInfo['size'] ||
226  $remoteInfo['mtime'] == -1 || $remoteInfo['mtime'] < $localInfo['mtime'])
227  {
228  $pathInfo = pathinfo($remoteFile);
229  $transfered = $this->copy($localFile, FTP_BINARY, $pathInfo['dirname'], true, $pathInfo['basename']);
230  if ($transfered)
231  return true;
232  }
233  return false;
234  }
235  /**
236  * Delete a remote file.
237  * @param file The name of the file to delete
238  * @return True on success, False else / error string provided by getLastError()
239  */
240  function delete($file)
241  {
242  if ($this->_ftpConnId == null)
243  {
244  $this->_errorMsg = 'connect to server first';
245  return null;
246  }
247 
248  return ftp_delete($this->_ftpConnId, $file);
249  }
250  /**
251  * Close a FTP connection to a given server.
252  */
253  function close()
254  {
255  $this->_errorMsg = '';
256  // suppress warnings
257  $oldErrorLevel = error_reporting (E_ERROR | E_PARSE);
258  // close the FTP stream
259  if ($this->_ftpConnId != null)
260  ftp_quit($this->_ftpConnId);
261  $this->_ftpConnId = null;
262  // reset error level
263  error_reporting ($oldErrorLevel);
264  }
265 }
266 ?>
getFileInfo($file)
FTPUtil provides support for ftp functionality.
synchronize($localFile, $remoteFile)
open($params)
getFiles($directory='.', $pattern='/./', $prependDirectoryName=false, $recursive=false)
copy($file, $transferMode, $toDir='./', $createDir=true, $destName=null)