/[drupal]/contributions/modules/drupal_ftp/drupal_ftp.module
ViewVC logotype

Diff of /contributions/modules/drupal_ftp/drupal_ftp.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.3, Wed Jun 27 14:35:19 2007 UTC revision 1.4, Thu Jun 28 14:55:49 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: drupal_ftp.module,v 1.2 2007/06/22 21:08:44 aaron Exp $  // $Id: drupal_ftp.module,v 1.3 2007/06/27 14:35:19 aaron Exp $
3    
4  // This is nearly a direct port from http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/  // This is nearly a direct port from http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/
5  // it's been drupalized, however, and there's more work to make it complete. however, looks good so far...  // it's been drupalized, however, and there's more work to make it complete. however, looks good so far...
# Line 13  define('DRUPAL_FTP_DEFAULT_PASSWORD', 's Line 13  define('DRUPAL_FTP_DEFAULT_PASSWORD', 's
13  define('DRUPAL_FTP_DEFAULT_HOME_DIRECTORY', '/pub/drupal/');  define('DRUPAL_FTP_DEFAULT_HOME_DIRECTORY', '/pub/drupal/');
14    
15  /**  /**
16   *  The _drupal_ftp_connect function   *  The drupal_ftp_connect function
17   *  This function connects to an FTP server and attempts to change into the directory specified by the fourth parameter, $Directory.   *  This function connects to an FTP server and attempts to change into the directory specified by the fourth parameter, $directory.
  *  The fifth parameter, $Err is a reference variable and will contain the error message if an error occurs.  
18   */   */
19  function _drupal_ftp_connect($Server, $User, $Password, $Directory, &$Err) {  function drupal_ftp_connect($server, $ftp_user, $password, $directory) {
20    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
21    $_DRUPAL_FTP = new stdClass();    $_DRUPAL_FTP = new stdClass();
22    
23    // Connect to the remote FTP server and then attempt    // Connect to the remote FTP server and then attempt
24    // to change into a remote directory. Returns false    // to change into a remote directory. Returns false if failed
   // as well as a string in the $err reference if failed  
25    
26    $_DRUPAL_FTP->__server = $Server;    $_DRUPAL_FTP->__server = $server;
27    $_DRUPAL_FTP->__user = $User;    $_DRUPAL_FTP->__user = $ftp_user;
28    $_DRUPAL_FTP->__password = $Password;    $_DRUPAL_FTP->__password = $password;
29    $_DRUPAL_FTP->__directory = $Directory;    $_DRUPAL_FTP->__directory = $directory;
30    
31    // Attempt to connect to the remote server    // Attempt to connect to the remote server
32    $_DRUPAL_FTP->__conn = @ftp_connect($Server);    $_DRUPAL_FTP->__conn = @ftp_connect($server);
33    
34    if (!$_DRUPAL_FTP->__conn) {    if (!$_DRUPAL_FTP->__conn) {
35      $Err = "Couldn't connect to server $Server";      drupal_ftp_error(t('Couldn\'t connect to server @server', array('@server' => $server)));
36      return false;      return false;
37    }    }
38    
39    // Attempt to login to the remote server    // Attempt to login to the remote server
40    $_DRUPAL_FTP->__login = @ftp_login($_DRUPAL_FTP->__conn, $User, $Password);    $_DRUPAL_FTP->__login = @ftp_login($_DRUPAL_FTP->__conn, $ftp_user, $password);
41    
42    if (!$_DRUPAL_FTP->__login) {    if (!$_DRUPAL_FTP->__login) {
43      $Err = "Couldn't login as user $User to $Server";      drupal_ftp_error(t('Couldn\'t login as user @ftp_user to @server', array('@ftp_user' => $ftp_user, '@server' => $server)));
44      return false;      return false;
45    }    }
46    
47    // Attempt to change into the working directory    // Attempt to change into the working directory
48    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $Directory);    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $directory);
49    
50    if (!$chDir) {    if (!$chDir) {
51      $Err = "Couldn't change into the $Directory directory";      drupal_ftp_error(t('Couldn\'t change into the @directory directory', array('@directory' => $directory)));
52      return false;      return false;
53    }    }
54    
# Line 59  function _drupal_ftp_connect($Server, $U Line 57  function _drupal_ftp_connect($Server, $U
57  }  }
58    
59  /**  /**
60   * The _drupal_ftp_connected function   * The drupal_ftp_connected function
61   * This function queries the FTP server with the ftp_systype command to check if the connection is still alive.   * This function queries the FTP server with the ftp_systype command to check if the connection is still alive.
62   * It returns true on success or false on disconnection.   * It returns true on success or false on disconnection.
63   */   */
64    
65  function _drupal_ftp_connected() {  function drupal_ftp_connected() {
66    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
67    // Attempt to call the ftp_systype to see if the connect    // Attempt to call the ftp_systype to see if the connect
68    // to the FTP server is still alive and kicking    // to the FTP server is still alive and kicking
# Line 80  function _drupal_ftp_connected() { Line 78  function _drupal_ftp_connected() {
78  }  }
79    
80  /**  /**
81   *  The _drupal_ftp_download_file Function   *  The drupal_ftp_download_file Function
82   *  This function tries to retrieve the contents of a file from the FTP server.   *  This function tries to retrieve the contents of a file from the FTP server.
83   *  Firstly it changes into the $Directory directory, and then attempts to download the file $FileName.   *  Firstly it changes into the $directory directory, and then attempts to download the file $filename.
84   *  The file is saved locally and its contents are returned to the caller of the function.   *  The file is saved locally and its contents are returned to the caller of the function.
85   */   */
86  function _drupal_ftp_download_file($FileName, $Directory, &$Err) {  function drupal_ftp_download_file($filename, $directory) {
87    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
88    // Change into the remote directory and retrieve the content    // Change into the remote directory and retrieve the content
89    // of a file. Once retrieve, return this value to the caller    // of a file. Once retrieve, return this value to the caller
90    
91    if (!_drupal_ftp_connected()) {    if (!drupal_ftp_connected()) {
92      // Connect is dead, attempt to reconnect      // Connect is dead, attempt to reconnect
93      $err = "";      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)){
94      @_drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory, $err);        return false;
   
     if ($err != "") {  
       _drupal_ftp_error($err);  
       die();  
     }  
     else {  
       return true;  
95      }      }
96    }    }
97    
98    // We are now connected, so let's retrieve the file contents.    // We are now connected, so let's retrieve the file contents.
99    // Firstly, we change into the directory    // Firstly, we change into the directory
100    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $Directory);    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $directory);
101    
102    if (!$chDir) {    if (!$chDir) {
103      $Err = "Couldn't change into directory: $Directory";      drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory' => $directory)));
104      return false;      return false;
105    }    }
106    
107    // We have changed into the directory, let's attempt to get the file    // We have changed into the directory, let's attempt to get the file
108    $fp = @fopen("localfile", "wb");    $temp_file = file_create_filename($filename, file_directory_temp());
109    $getFile = @ftp_fget($_DRUPAL_FTP->__conn, $fp, $FileName, FTP_BINARY);    $fp = @fopen($temp_file, 'wb');
110      $getFile = @ftp_fget($_DRUPAL_FTP->__conn, $fp, $filename, FTP_BINARY);
111    fclose($fp);    fclose($fp);
112    
113    $fp = null;    $fp = null;
114    
115    if (!$getFile) {    if (!$getFile) {
116      $Err = "Unable to download file: $FileName from $Directory";      drupal_ftp_error(t('Unable to download file: @filename from @directory', array('@filename' => $filename, '@directory' => $directory)));
117      return false;      return false;
118    }    }
119    
120    // The file was downloaded successfully. Let's open it, read in its    // The file was downloaded successfully. Let's open it, read in its
121    // contents and return it to the calling function    // contents and return it to the calling function
122    
123    $fp = @fopen("localfile", "rb");    $fp = @fopen($temp_file, 'rb');
124    
125    if (!$fp) {    if (!$fp) {
126      $Err = "Unable to open $FileName after it was downloaded from {$_DRUPAL_FTP->__server}";      drupal_ftp_error(t('Unable to open @filename after it was downloaded from {@server}', array('@filename' => $filename, '@server' => $_DRUPAL_FTP->__server)));
127      return false;      return false;
128    }    }
129    
130    // Read in the contents of the file to a variable    // Read in the contents of the file to a variable
131    $data = "";    $data = '';
132    
133    while(!feof($fp)) {    while(!feof($fp)) {
134      $data.= fread($fp, 4096);      $data.= fread($fp, 4096);
# Line 149  function _drupal_ftp_download_file($File Line 141  function _drupal_ftp_download_file($File
141  }  }
142    
143  /**  /**
144   *  The _drupal_ftp_upload_file function   *  The drupal_ftp_upload_file function
145   *  This function will attempt to create a file called $FileName in the $Directory folder on the FTP server and will write $Data to this file. If the file already exists then an error will be raised:   *  This function will attempt to create a file called $filename in the $directory folder on the FTP server
146     *  and will write $data to this file. If the file already exists then an error will be raised.
147   */   */
148  function _drupal_ftp_upload_file($Data, $FileName, $Directory, &$Err) {  function drupal_ftp_upload_file($data, $filename, $directory) {
149    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
150    // Save HTML to a remote file on the FTP server    // Save HTML to a remote file on the FTP server
151    if (!_drupal_ftp_connected()) {    if (!drupal_ftp_connected()) {
152      // Connect is dead, attempt to reconnect      // Connect is dead, attempt to reconnect
153      $err = "";      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {
154      @_drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory, $err);        return false;
   
     if ($err != "") {  
       _drupal_ftp_error($err);  
       die();  
     }  
     else {  
       return true;  
155      }      }
156    }    }
157    
158    // We are now connected, so let's retrieve the file contents.    // We are now connected, so let's retrieve the file contents.
159    // Firstly, we change into the directory    // Firstly, we change into the directory
160    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $Directory);    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $directory);
161    
162    if (!$chDir) {    if (!$chDir) {
163      $Err = "Couldn't change into directory: $Directory";      drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory' => $directory)));
164      return false;      return false;
165    }    }
166    
167      $local_filename = file_create_filename($filename, file_directory_temp());
168    // Save the HTML to a file which we then upload    // Save the HTML to a file which we then upload
169    $fp = @fopen("localfile", "wb");    $fp = @fopen($local_filename, 'wb');
170    
171    if (!$fp) {    if (!$fp) {
172      $Err = "Couldn't open a local file for temporary output";      drupal_ftp_error(t('Couldn\'t open a local file for temporary output'));
173      return false;      return false;
174    }    }
175    
176    // The file was opened OK, let's write to it    // The file was opened OK, let's write to it
177    $filePut = @fputs($fp, $Data, strlen($Data));    $filePut = @fputs($fp, $data, strlen($data));
178    
179    if (!$filePut) {    if (!$filePut) {
180      $Err = "Couldn't write to a local file for temporary output";      drupal_ftp_error(t('Couldn\'t write to a local file for temporary output'));
181      return false;      return false;
182    }    }
183    else {    else {
# Line 198  function _drupal_ftp_upload_file($Data, Line 185  function _drupal_ftp_upload_file($Data,
185    }    }
186    
187    // Now we can try to write to the remote file    // Now we can try to write to the remote file
188    $CompleteFileName = $Directory . "/" . $FileName;    $complete_filename = $directory . '/' . $filename;
   $LocalFileName = "localfile";  
189    
190    $putFile = @ftp_put($_DRUPAL_FTP->__conn, $CompleteFileName, $LocalFileName, FTP_BINARY);    $putFile = @ftp_put($_DRUPAL_FTP->__conn, $complete_filename, $local_filename, FTP_BINARY);
191    
192    if (!$putFile) {    if (!$putFile) {
193      $Err = "Couldn't write to $CompleteFileName when trying to save file";      drupal_ftp_error(t('Couldn\'t write to @complete_filename when trying to save file', array('@complete_filename', $complete_filename)));
194      return false;      return false;
195    }    }
196    
# Line 213  function _drupal_ftp_upload_file($Data, Line 199  function _drupal_ftp_upload_file($Data,
199  }  }
200    
201  /**  /**
202   *  The _drupal_ftp_change_directory Function   *  The drupal_ftp_change_directory Function
203   *  This function simply changes into the $Directory folder on the FTP server.   *  This function simply changes into the $directory folder on the FTP server.
204   *  If a connection or permission error occurs then $Err will contain the error message.   *  If a connection or permission error occurs then drupal_ftp_error() will contain the error message.
205   */   */
206  function _drupal_ftp_change_directory($Directory, &$Err) {  function drupal_ftp_change_directory($directory) {
207    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
208    // Switch to another directory on the web server. If we don't    // Switch to another directory on the web server. If we don't
209    // have permissions then an error will occur    // have permissions then an error will occur
210    
211    if (!_drupal_ftp_connected()) {    if (!drupal_ftp_connected()) {
212      // Connect is dead, attempt to reconnect      // Connect is dead, attempt to reconnect
213      $err = "";      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {
214      @_drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory, $err);        return false;
   
     if ($err != "") {  
       _drupal_ftp_error($err);  
       die();  
     }  
     else {  
       return true;  
215      }      }
216    }    }
217    
218    // Try and change into another directory    // Try and change into another directory
219    $chDir = ftp_chdir($_DRUPAL_FTP->__conn, $Directory);    $chDir = ftp_chdir($_DRUPAL_FTP->__conn, $directory);
220    
221    if (!$chDir) {    if (!$chDir) {
222      $Err = "Couldn't change into directory: $Directory";      drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory', $directory)));
223      return false;      return false;
224    }    }
225    else {    else {
# Line 250  function _drupal_ftp_change_directory($D Line 229  function _drupal_ftp_change_directory($D
229  }  }
230    
231  /**  /**
232   *  The _drupal_ftp_file_list Function   *  The drupal_ftp_file_list Function
233   *  This function will change into the $Directory folder and get a list of files and directories contained in that folder.   *  This function will change into the $directory folder and get a list of files and directories contained in that folder.
234   *  This function still needs a lot of work, but should work in most cases.   *  This function still needs a lot of work, but should work in most cases.
235   */   */
236  function _drupal_ftp_file_list($Directory, &$Err) {  function drupal_ftp_file_list($directory) {
237    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
238    // This function will attempt to change into the specified    // This function will attempt to change into the specified
239    // directory and retrieve a list of files as an associative    // directory and retrieve a list of files as an associative
240    // array. This list will include file name, size and date last modified    // array. This list will include file name, size and date last modified
241    
   $err = "";  
242    $fileArray = array();    $fileArray = array();
243    
244    // Can we switch to the desired directory?    // Can we switch to the desired directory?
245    if (!_drupal_ftp_change_directory($Directory, $err)) {    if (!drupal_ftp_change_directory($directory)) {
246      _drupal_ftp_error($err);      return false;
247    }    }
248    
249    // We are in the directory, let's retrieve a list of files    // We are in the directory, let's retrieve a list of files
250    $fileList = ftp_rawlist($_DRUPAL_FTP->__conn, $Directory);    $fileList = ftp_rawlist($_DRUPAL_FTP->__conn, $directory);
251    
252    // Save the list of files    // Save the list of files
253    if (@is_array($fileList)) {    if (@is_array($fileList)) {
254      // Interate through the array      // Interate through the array
255      for($i = 0; $i < sizeof($fileList); $i++) {      for($i = 0; $i < sizeof($fileList); $i++) {
256        $itemArray = explode(" ", $fileList[$i]);        $itemArray = explode(' ', $fileList[$i]);
257    
258        // Are we dealing with a file or directory?        // Are we dealing with a file or directory?
259        // If the first letter of the attributes is        // If the first letter of the attributes is
260        // "d" then we are dealing with a directory        // 'd' then we are dealing with a directory
261        $attributes = $itemArray[0];        $attributes = $itemArray[0];
262    
263        if (substr($attributes, 0, 1) == "d") {        if (substr($attributes, 0, 1) == 'd') {
264          $fileType = DRUPAL_FTP_FT_DIRECTORY;          $fileType = DRUPAL_FTP_FT_DIRECTORY;
265        }        }
266        else {        else {
# Line 300  function _drupal_ftp_file_list($Director Line 278  function _drupal_ftp_file_list($Director
278        }        }
279    
280        // Get the date last modified        // Get the date last modified
281        $fileTimeStamp = $itemArray[sizeof($itemArray)-4] . " " . $itemArray[sizeof($itemArray)-3] . " " . $itemArray[sizeof($itemArray)-2];        $fileTimeStamp = $itemArray[sizeof($itemArray)-4] . ' ' . $itemArray[sizeof($itemArray)-3] . ' ' . $itemArray[sizeof($itemArray)-2];
282    
283        $fileArray[] = array("type" => $fileType,        $fileArray[] = array(
284        "filename" => $fileName,          'type' => $fileType,
285        "filesize" => $fileSize,          'filename' => $fileName,
286        "filetime" => $fileTimeStamp);          'filesize' => $fileSize,
287            'filetime' => $fileTimeStamp
288          );
289      }      }
   
     sort($fileArray);  
     return $fileArray;  
   }  
   else {  
     $Err = "No files in $Directory directory";  
     return -1;  
290    }    }
291      sort($fileArray);
292      return $fileArray;
293  }  }
294    
295  /**  /**
296   *  The _drupal_ftp_create_directory Function   *  The drupal_ftp_create_directory Function
297   *  This function tries to make a new directory called $folderName on the FTP server.   *  This function tries to make a new directory called $folderName on the FTP server.
298   *  If it can create the folder, then the folder is given appropriate rights with the CHMOD command.   *  If it can create the folder, then the folder is given appropriate rights with the CHMOD command.
299   */   */
300  function _drupal_ftp_create_directory($folderName, &$Err) {  function drupal_ftp_create_directory($folderName) {
301    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
302    // Makes a new folder on the web server via FTP    // Makes a new folder on the web server via FTP
303    
304    if (!_drupal_ftp_connected()) {    if (!drupal_ftp_connected()) {
305      // Connect is dead, attempt to reconnect      // Connect is dead, attempt to reconnect
306      $err = "";      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {
307      @_drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory, $err);        return false;
   
     if ($err != "") {  
       _drupal_ftp_error($err);  
       die();  
     }  
     else {  
       return true;  
308      }      }
309    }    }
310    
# Line 350  function _drupal_ftp_create_directory($f Line 318  function _drupal_ftp_create_directory($f
318        return true;        return true;
319      }      }
320      else {      else {
321        $Err = "Couldn't set owner permissions on $folderName";        drupal_ftp_error(t('Couldn\'t set owner permissions on @folder.', array('@folder', $folderName)));
322        return false;        return false;
323      }      }
324    }    }
325    else {    else {
326      $Err = "Couldn't create new folder $folderName";      drupal_ftp_error(t('Couldn\'t create new folder @folder.', array('@folder', $folderName)));
327      return false;      return false;
328    }    }
329  }  }
330    
331  /**  /**
332   *  The _drupal_ftp_rename_file Function   *  The drupal_ftp_rename_file Function
333   *  This function attempts to rename a file on the FTP server from $OldFileName to $NewFileName.   *  This function attempts to rename a file on the FTP server from $OldFileName to $NewFileName.
334   */   */
335  function _drupal_ftp_rename_file($OldFileName, $NewFileName, $IsFolder, &$Err) {  function drupal_ftp_rename_file($OldFileName, $NewFileName, $IsFolder) {
336    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
337    // Rename a file/directory on the FTP server    // Rename a file/directory on the FTP server
338    global $php_errormsg;    global $php_errormsg;
339    
340    if (!_drupal_ftp_connected()) {    if (!drupal_ftp_connected()) {
341      // Connect is dead, attempt to reconnect      // Connect is dead, attempt to reconnect
342      $err = "";      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {
343      @_drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory, $err);        return false;
   
     if ($err != "") {  
       _drupal_ftp_error($err);  
       die();  
     }  
     else {  
       return true;  
344      }      }
345    }    }
346    
# Line 392  function _drupal_ftp_rename_file($OldFil Line 353  function _drupal_ftp_rename_file($OldFil
353    else {    else {
354      // Couldn't rename the file/folder      // Couldn't rename the file/folder
355      if ($IsFolder == 0) {      if ($IsFolder == 0) {
356        $Err = "Couldn't rename the selected folder: " . @$php_errormsg;        drupal_ftp_error(t('Couldn\'t rename the selected folder: @error', array('@error' => @$php_errormsg)));
357      }      }
358      else {      else {
359        $Err = "Couldn't rename the selected file: " . @$php_errormsg;        drupal_ftp_error(t('Couldn\'t rename the selected file: @error', array('@error' => @$php_errormsg)));
360      }      }
361    
362      return false;      return false;
# Line 403  function _drupal_ftp_rename_file($OldFil Line 364  function _drupal_ftp_rename_file($OldFil
364  }  }
365    
366  /**  /**
367   *  The _drupal_ftp_delete_file Function   *  The drupal_ftp_delete_file Function
368   *  This function attempts to delete a file called $FileName from the FTP server.   *  This function attempts to delete a file called $filename from the FTP server.
369   */   */
370  function _drupal_ftp_delete_file($FileName, &$Err) {  function drupal_ftp_delete_file($filename) {
371    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
372    // Remove the specified file from the FTP server    // Remove the specified file from the FTP server
373    if (!_drupal_ftp_connected()) {    if (!drupal_ftp_connected()) {
374      // Connect is dead, attempt to reconnect      // Connect is dead, attempt to reconnect
375      $err = "";      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {
376      @_drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory, $err);        return false;
   
     if ($err != "") {  
       _drupal_ftp_error($err);  
       die();  
     }  
     else {  
       return true;  
377      }      }
378    }    }
379    
380    $deleteResult = @ftp_delete($_DRUPAL_FTP->__conn, $FileName);    $deleteResult = @ftp_delete($_DRUPAL_FTP->__conn, $filename);
381    
382    if ($deleteResult == true) {    if ($deleteResult == true) {
383      // The file/folder was renamed successfully      // The file/folder was renamed successfully
# Line 431  function _drupal_ftp_delete_file($FileNa Line 385  function _drupal_ftp_delete_file($FileNa
385    }    }
386    else {    else {
387      // Couldn't delete the selected file      // Couldn't delete the selected file
388      $Err = "Couldn't delete the selected file: " . @$php_errormsg;      drupal_ftp_error(t('Couldn\'t delete the selected file: @error', array('@error' => @$php_errormsg)));
389      return false;      return false;
390    }    }
391  }  }
392    
393  /**  /**
394   *  The _drupal_ftp_delete_folder Function   *  The drupal_ftp_delete_folder Function
395   *  This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $FolderName.   *  This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $folder_name.
396   */   */
397  function _drupal_ftp_delete_folder($FolderName, &$Err) {  function drupal_ftp_delete_folder($folder_name) {
398    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
399    // Remove the specified folder and all subdirectories/files from the FTP server    // Remove the specified folder and all subdirectories/files from the FTP server
400    global $php_errormsg;    global $php_errormsg;
401    
402    if (!_drupal_ftp_connected()) {    if (!drupal_ftp_connected()) {
403      // Connect is dead, attempt to reconnect      // Connect is dead, attempt to reconnect
404      $err = "";      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {
405      @_drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory, $err);        return false;
   
     if ($err != "") {  
       _drupal_ftp_error($err);  
       die();  
     }  
     else {  
       return true;  
406      }      }
407    }    }
408    
409    @ftp_chdir($_DRUPAL_FTP->__conn, $FolderName);    @ftp_chdir($_DRUPAL_FTP->__conn, $folder_name);
410    $location = @ftp_pwd($_DRUPAL_FTP->__conn);    $location = @ftp_pwd($_DRUPAL_FTP->__conn);
411    
412    $directories = array();    $directories = array();
# Line 493  function _drupal_ftp_delete_folder($Fold Line 440  function _drupal_ftp_delete_folder($Fold
440    for ($j = 0; $j < $dir_counter; $j++) {    for ($j = 0; $j < $dir_counter; $j++) {
441      if ($directories[$j] != "." OR $directories[$j] != "..") {      if ($directories[$j] != "." OR $directories[$j] != "..") {
442        $location = ftp_pwd ($_DRUPAL_FTP->__conn);        $location = ftp_pwd ($_DRUPAL_FTP->__conn);
443        $_DRUPAL_FTP->_drupal_ftp_delete_folder($directories[$j], $_DRUPAL_FTP->__dummyError);        drupal_ftp_delete_folder($directories[$j]);
444        @ftp_cdup ($_DRUPAL_FTP->__conn);        @ftp_cdup ($_DRUPAL_FTP->__conn);
445        @ftp_rmdir($_DRUPAL_FTP->__conn,$directories[$j]);        @ftp_rmdir($_DRUPAL_FTP->__conn,$directories[$j]);
446      }      }
# Line 501  function _drupal_ftp_delete_folder($Fold Line 448  function _drupal_ftp_delete_folder($Fold
448    
449    // Lastly, we change into the directory that we want to delete and see    // Lastly, we change into the directory that we want to delete and see
450    // if we can cdup. If we can, we delete it.    // if we can cdup. If we can, we delete it.
451    @ftp_chdir($_DRUPAL_FTP->__conn, $FolderName);    @ftp_chdir($_DRUPAL_FTP->__conn, $folder_name);
452    @ftp_cdup($_DRUPAL_FTP->__conn);    @ftp_cdup($_DRUPAL_FTP->__conn);
453    @ftp_rmdir($_DRUPAL_FTP->__conn, $FolderName);    @ftp_rmdir($_DRUPAL_FTP->__conn, $folder_name);
454    
455    // Did the recursive folder/file deletion work?    // Did the recursive folder/file deletion work?
456    return true;    return true;
457    
458    /*    /*
459    if (@$php_errormsg == "")    if (@$php_errormsg == '')
460    {    {
461    return true;    return true;
462    }    }
463    else    else
464    {    {
465    $Err = "Couldn't recursive delete folder: " . @$php_errormsg;    $err = "Couldn't recursive delete folder: " . @$php_errormsg;
466    return false;    return false;
467    }    }
468    */    */
469  }  }
470    
471  /**  /**
472   *  The _drupal_ftp_file_exists Function   *  The drupal_ftp_file_exists Function
473   *  This function checks if a file called $FileName exists in the directory called $FolderName on the FTP server.   *  This function checks if a file called $filename exists in the directory called $folder_name on the FTP server.
474   *  It returns true if the file exists, and false if it doesn�t.   *  It returns true if the file exists, and false if it doesn�t.
475   */   */
476  function _drupal_ftp_file_exists($FolderName, $FileName, &$Err) {  function drupal_ftp_file_exists($folder_name, $filename) {
477    global $_DRUPAL_FTP;    global $_DRUPAL_FTP;
478    // Does the specified file exist on the remote FTP server?    // Does the specified file exist on the remote FTP server?
479    // Returns false on error, true on file exists and 2 if it doesn't exist    // Returns false on error, true on file exists and 2 if it doesn't exist
480    
481    global $php_errormsg;    global $php_errormsg;
482    
483    if (!_drupal_ftp_connected()) {    if (!drupal_ftp_connected()) {
484      // Connect is dead, attempt to reconnect      // Connect is dead, attempt to reconnect
485      $err = "";      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {
486      @_drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory, $err);        return false;
   
     if ($err != "") {  
       _drupal_ftp_error($err);  
       die();  
     }  
     else {  
       return true;  
487      }      }
488    }    }
489    
490    if (!@ftp_chdir($_DRUPAL_FTP->__conn, $FolderName)) {    if (!@ftp_chdir($_DRUPAL_FTP->__conn, $folder_name)) {
491      $Err = $php_errormsg;      drupal_ftp_error($php_errormsg);
492      return false;      return false;
493    }    }
494    else {    else {
495      // We have changed into the directory, let's get a list      // We have changed into the directory, let's get a list
496      // of files using ftp_nlist and compare it to see if it exists      // of files using ftp_nlist and compare it to see if it exists
497      $fileArray = @ftp_nlist($_DRUPAL_FTP->__conn, $FolderName);      $fileArray = @ftp_nlist($_DRUPAL_FTP->__conn, $folder_name);
498    
499      if (!is_array($fileArray)) {      if (!is_array($fileArray)) {
500        $Err = $php_errormsg;        drupal_ftp_error($php_errormsg);
501        return false;        return false;
502      }      }
503      else {      else {
504        // Loop through each file and check it if exists        // Loop through each file and check it if exists
505        for($i = 0; $i < sizeof($fileArray); $i++) {        for($i = 0; $i < sizeof($fileArray); $i++) {
506          if ($fileArray[$i] == "$FolderName/$FileName") {          if ($fileArray[$i] == "$folder_name/$filename") {
507            return true;            return true;
508          }          }
509        }        }
# Line 574  function _drupal_ftp_file_exists($Folder Line 514  function _drupal_ftp_file_exists($Folder
514    }    }
515  }  }
516    
517  function _drupal_ftp_error($err) {  function drupal_ftp_error($message = NULL, $severity = WATCHDOG_ERROR, $notify = true) {
518    drupal_set_message($err, 'error');    static $errors;
519    
520      if (isset($message)) {
521        $errors[] = $message;
522        watchdog('drupal_ftp', $message, $severity);
523        if ($notify) {
524          drupal_set_message($message, 'error');
525        }
526      }
527      return $errors;
528  }  }
529    
530  function drupal_ftp_perm() {  function drupal_ftp_perm() {
# Line 664  function drupal_ftp_transfer_file_submit Line 613  function drupal_ftp_transfer_file_submit
613    
614    if ($result->code == 200) {    if ($result->code == 200) {
615      $data = $result->data;      $data = $result->data;
616      _drupal_ftp_connect($server, $username, $password, $home_dir, $err);      if (drupal_ftp_connect($server, $username, $password, $home_dir)) {
617      if (!$err) {        if (drupal_ftp_upload_file($data, check_plain($form_values['filename']), $home_dir . $dir)) {
618        _drupal_ftp_upload_file($data, check_plain($form_values['filename']), $home_dir . $dir, $err);          drupal_set_message(t('File transfered.'));
619      }        }
     if ($err) {  
       drupal_set_message($err, 'error');  
     }  
     else {  
       drupal_set_message(t('File transfered.'));  
620      }      }
621    }    }
   else {  
     drupal_set_message(t('Unable to retrieve file.'), 'error');  
   }  
622    return 'ftp/browse' . $dir;    return 'ftp/browse' . $dir;
623  }  }
624    
625  function drupal_ftp_browse_dl() {  function drupal_ftp_browse_dl() {
626    $args = func_get_args();    $args = func_get_args();
627    $directory = implode('/', $args);    $directory = implode('/', $args);
   $err = '';  
628    
629    $server = variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER);    $server = variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER);
630    $username = variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME);    $username = variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME);
# Line 692  function drupal_ftp_browse_dl() { Line 632  function drupal_ftp_browse_dl() {
632    $home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);    $home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
633    
634    $file = $_GET['file'];    $file = $_GET['file'];
635    _drupal_ftp_connect($server, $username, $password, $home_dir, $err);    if (drupal_ftp_connect($server, $username, $password, $home_dir)) {
636    $output = _drupal_ftp_download_file($file, $home_dir . $directory, $err);      $data = drupal_ftp_download_file($file, $home_dir . $directory);
637    if (!$err) {      if ($data) {
638      drupal_set_title($file);        drupal_set_title($file);
639    }      }
   else {  
     drupal_set_message($err, 'error');  
640    }    }
641      $parent = l(t('..'), 'ftp/browse/' . $directory);
642      $output = theme('drupal_ftp_download', $parent, $data);
643    return $output;    return $output;
644  }  }
645    
# Line 713  function drupal_ftp_browse_page() { Line 653  function drupal_ftp_browse_page() {
653    $home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);    $home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
654    
655    $directory = implode('/', $args);    $directory = implode('/', $args);
   $err = "";  
656    
657    _drupal_ftp_connect($server, $username, $password, $home_dir, $err);    if (drupal_ftp_connect($server, $username, $password, $home_dir)) {
658    $fileList = _drupal_ftp_file_list($home_dir . $directory, $err);      $fileList = drupal_ftp_file_list($home_dir . $directory);
659    $browser = array();      $browser = array();
660    $links = array();      $links = array();
661    
   if($err == '') {  
662      if (!empty($args)) {      if (!empty($args)) {
663        $parents = $args;        $parents = $args;
664        array_pop($parents);        array_pop($parents);
665        $browser[] = l('..', 'ftp/browse' . (!empty($parents) ? '/' . implode('/', $parents) : ''));        $browser[] = l('..', 'ftp/browse' . (!empty($parents) ? '/' . implode('/', $parents) : ''));
666      }      }
667      for($i = 0; $i < sizeof($fileList); $i++) {      if (is_array($fileList)) {
668        if($fileList[$i]["type"] == 0) {        for($i = 0; $i < sizeof($fileList); $i++) {
669          // Folder          if($fileList[$i]["type"] == 0) {
670          $browser[] = l($fileList[$i]["filename"], 'ftp/browse/' . (!empty($args) ? $directory . '/' : '') . $fileList[$i]["filename"]) . " (directory)";            // Folder
671        }            $browser[] = l($fileList[$i]["filename"], 'ftp/browse/' . (!empty($args) ? $directory . '/' : '') . $fileList[$i]["filename"]) . " (directory)";
672        else {          }
673          // File          else {
674          $browser[] = l($fileList[$i]["filename"], 'ftp/dl/' . (!empty($args) ? $directory : ''), array(), 'file=' . $fileList[$i]["filename"]) . " (" . $fileList[$i]["filesize"] . " bytes)";            // File
675              $browser[] = l($fileList[$i]["filename"], 'ftp/dl/' . (!empty($args) ? $directory : ''), array(), 'file=' . $fileList[$i]["filename"]) . " (" . $fileList[$i]["filesize"] . " bytes)";
676            }
677        }        }
678      }      }
679        else {
680          $browser[] = theme('drupal_ftp_empty_directory_message', $directory);
681        }
682      if (user_access('write drupal ftp')) {      if (user_access('write drupal ftp')) {
683        $links[] = array(        $links[] = l(t('transfer file'), 'ftp/transfer/' . $directory);
         'title' => t('transfer file'),  
         'href' => 'ftp/transfer/' . $directory,  
       );  
684      }      }
685        drupal_set_title(t('FTP Browse @directory', array('@directory' => $directory)));
686      $output .= theme('drupal_ftp_browser', $browser, $links);      $output .= theme('drupal_ftp_browser', $browser, $links);
687    }    }
   else {  
     $output .= $err;  
   }  
688    return $output;    return $output;
689  }  }
690    
691    function theme_drupal_ftp_empty_directory_message($directory) {
692      return t('No files in @directory directory.', array('@directory' => $directory));
693    }
694    
695  function theme_drupal_ftp_browser($browser = array(), $links = array()) {  function theme_drupal_ftp_browser($browser = array(), $links = array()) {
696      $output .= '<div class="drupal_ftp_browser">' . "\n";
697    $output .= theme('item_list', $browser);    $output .= theme('item_list', $browser);
698    $output .= theme('links', $links);    $output .= "</div>\n";
699      if (!empty($links)) {
700        $output .= "<div class=\"drupal_ftp_browser_links\">\n";
701        $output .= '<strong>' . t('Available actions:') . '</strong><br />';
702        $output .= theme('item_list', $links);
703        $output .= "</div>\n\n";
704      }
705      return $output;
706    }
707    
708    function theme_drupal_ftp_download($directory_link, $data) {
709      $output = '<div class="drupal_ftp_parent_link">' . $directory_link . '</div><div class="drupal_ftp_data">' . $data  . '</div>' . "\n\n";
710    return $output;    return $output;
711  }  }
712    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.2