/[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.4, Thu Jun 28 14:55:49 2007 UTC revision 1.5, Thu Jun 28 21:08:57 2007 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: drupal_ftp.module,v 1.3 2007/06/27 14:35:19 aaron Exp $  // $Id: drupal_ftp.module,v 1.4 2007/06/28 14:55:49 aaron Exp $
3    
4  // This is nearly a direct port from http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/  // Inspired by 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 most of the bugs from that example have been fixed.
6  // - winborn 2007-06-22  // - winborn 2007-06-22 - 2007-06-28
7    
8  define('DRUPAL_FTP_FT_DIRECTORY', 0);  define('DRUPAL_FTP_FT_DIRECTORY', 0);
9  define('DRUPAL_FTP_FT_FILE', 1);  define('DRUPAL_FTP_FT_FILE', 1);
# 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   *  creates a new ftp object. if any elements of ftp_map are missing, they'll be filled with the server defaults.
  *  This function connects to an FTP server and attempts to change into the directory specified by the fourth parameter, $directory.  
17   */   */
18  function drupal_ftp_connect($server, $ftp_user, $password, $directory) {  function drupal_ftp_ftp_object($ftp_map = array()) {
19    global $_DRUPAL_FTP;    $ftp = new stdClass();
   $_DRUPAL_FTP = new stdClass();  
   
   // Connect to the remote FTP server and then attempt  
   // to change into a remote directory. Returns false if failed  
20    
21    $_DRUPAL_FTP->__server = $server;    $ftp->__server = $ftp_map['#server'] ? $ftp_map['#server'] : variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER);
22    $_DRUPAL_FTP->__user = $ftp_user;    $ftp->__user = $ftp_map['#ftp_user'] ? $ftp_map['#ftp_user'] : variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME);
23    $_DRUPAL_FTP->__password = $password;    $ftp->__password = $ftp_map['#password'] ? $ftp_map['#password'] : variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD);
24    $_DRUPAL_FTP->__directory = $directory;    $ftp->__directory = $ftp_map['#directory'] ? $ftp_map['#directory'] : variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
25    
26    // Attempt to connect to the remote server    return $ftp;
27    $_DRUPAL_FTP->__conn = @ftp_connect($server);  }
28    
29    if (!$_DRUPAL_FTP->__conn) {  /**
30      drupal_ftp_error(t('Couldn\'t connect to server @server', array('@server' => $server)));   *  The drupal_ftp_connect function
31      return false;   *  This function connects to an FTP server and attempts to change into the directory specified by
32     *  the fourth parameter, $directory.
33     */
34    function drupal_ftp_connect(&$ftp) {
35      if (is_null($ftp)) {
36        $ftp = drupal_ftp_ftp_object();
37    }    }
38    
39    // Attempt to login to the remote server    if (!$ftp->__conn && !drupal_ftp_connected($ftp)) {
40    $_DRUPAL_FTP->__login = @ftp_login($_DRUPAL_FTP->__conn, $ftp_user, $password);      // Attempt to connect to the remote server
41        $ftp->__conn = @ftp_connect($ftp->__server);
42    
43    if (!$_DRUPAL_FTP->__login) {      if (!$ftp->__conn) {
44      drupal_ftp_error(t('Couldn\'t login as user @ftp_user to @server', array('@ftp_user' => $ftp_user, '@server' => $server)));        drupal_ftp_error(t('Couldn\'t connect to server @server', array('@server' => $ftp->__server)));
45      return false;        return false;
46    }      }
47    
48    // Attempt to change into the working directory      // Attempt to login to the remote server
49    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $directory);      $ftp->__login = @ftp_login($ftp->__conn, $ftp->__user, $ftp->__password);
50    
51    if (!$chDir) {      if (!$ftp->__login) {
52      drupal_ftp_error(t('Couldn\'t change into the @directory directory', array('@directory' => $directory)));        drupal_ftp_error(t('Couldn\'t login as user @ftp_user to @server', array('@ftp_user' => $ftp->__user, '@server' => $ftp->__server)));
53      return false;        return false;
54        }
55    
56        // Attempt to change into the working directory
57        $chDir = @ftp_chdir($ftp->__conn, $ftp->__directory);
58    
59        if (!$chDir) {
60          drupal_ftp_error(t('Couldn\'t change into the @directory directory', array('@directory' => $ftp->__directory)));
61          return false;
62        }
63    }    }
64    
65    // Everything worked OK, return true    // Everything worked OK, return true
# Line 62  function drupal_ftp_connect($server, $ft Line 72  function drupal_ftp_connect($server, $ft
72   * It returns true on success or false on disconnection.   * It returns true on success or false on disconnection.
73   */   */
74    
75  function drupal_ftp_connected() {  function drupal_ftp_connected(&$ftp) {
   global $_DRUPAL_FTP;  
76    // Attempt to call the ftp_systype to see if the connect    // Attempt to call the ftp_systype to see if the connect
77    // to the FTP server is still alive and kicking    // to the FTP server is still alive and kicking
78    
79    if (!@ftp_systype($_DRUPAL_FTP->__conn)) {    if (is_null($ftp)) {
80        $ftp = drupal_ftp_ftp_object();
81        return false;
82      }
83    
84      if (!@ftp_systype($ftp->__conn)) {
85      // The connection is dead      // The connection is dead
86      return false;      return false;
87    }    }
# Line 78  function drupal_ftp_connected() { Line 92  function drupal_ftp_connected() {
92  }  }
93    
94  /**  /**
95   *  The drupal_ftp_download_file Function   *  The drupal_ftp_ftp_to_data Function
96   *  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.
97   *  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.
98   *  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.
99   */   */
100  function drupal_ftp_download_file($filename, $directory) {  function drupal_ftp_ftp_to_data($filename, $directory, &$ftp) {
   global $_DRUPAL_FTP;  
101    // Change into the remote directory and retrieve the content    // Change into the remote directory and retrieve the content
102    // of a file. Once retrieve, return this value to the caller    // of a file. Once retrieve, return this value to the caller
103    
104    if (!drupal_ftp_connected()) {    if (!@drupal_ftp_connect($ftp)){
105      // Connect is dead, attempt to reconnect      return false;
     if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)){  
       return false;  
     }  
106    }    }
107    
108    // We are now connected, so let's retrieve the file contents.    // We are now connected, so let's retrieve the file contents.
109    // Firstly, we change into the directory    // Firstly, we change into the directory
110    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $directory);    $chDir = @ftp_chdir($ftp->__conn, $directory);
111    
112    if (!$chDir) {    if (!$chDir) {
113      drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory' => $directory)));      drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory' => $directory)));
# Line 107  function drupal_ftp_download_file($filen Line 117  function drupal_ftp_download_file($filen
117    // 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
118    $temp_file = file_create_filename($filename, file_directory_temp());    $temp_file = file_create_filename($filename, file_directory_temp());
119    $fp = @fopen($temp_file, 'wb');    $fp = @fopen($temp_file, 'wb');
120    $getFile = @ftp_fget($_DRUPAL_FTP->__conn, $fp, $filename, FTP_BINARY);    $getFile = @ftp_fget($ftp->__conn, $fp, $filename, FTP_BINARY);
121    fclose($fp);    fclose($fp);
122    
123    $fp = null;    $fp = null;
# Line 140  function drupal_ftp_download_file($filen Line 150  function drupal_ftp_download_file($filen
150    return $data;    return $data;
151  }  }
152    
153    function drupal_ftp_file_to_ftp($file, $ftp_filename, $ftp_directory, &$ftp) {
154      if (!@drupal_ftp_connect($ftp)) {
155        return false;
156      }
157    
158      if ($source = file_create_path($file)) {
159        // Now we can try to write to the remote file
160        $complete_filename = $ftp_directory . '/' . $ftp_filename;
161        $putFile = @ftp_put($ftp->__conn, $complete_filename, $source, FTP_BINARY);
162        if (!$putFile) {
163          drupal_ftp_error(t('Couldn\'t write to @complete_filename when trying to save file on the ftp server.', array('@complete_filename', $complete_filename)));
164          return false;
165        }
166    
167        // Everything worked OK
168        return true;
169      }
170      else {
171        drupal_ftp_error(t('Couldn\'t find @file.', array('@file')));
172        return false;
173      }
174    }
175    
176  /**  /**
177   *  The drupal_ftp_upload_file function   *  The drupal_ftp_data_to_ftp function
178   *  This function will attempt to create a file called $filename in the $directory folder on the FTP server   *  This function will attempt to create a file called $filename in the $directory folder on the FTP server
179   *  and will write $data to this file. If the file already exists then an error will be raised.   *  and will write $data to this file. If the file already exists then an error will be raised.
180   */   */
181  function drupal_ftp_upload_file($data, $filename, $directory) {  function drupal_ftp_data_to_ftp($data, $filename, $directory, &$ftp) {
   global $_DRUPAL_FTP;  
182    // Save HTML to a remote file on the FTP server    // Save HTML to a remote file on the FTP server
183    if (!drupal_ftp_connected()) {  
184      // Connect is dead, attempt to reconnect    if (!@drupal_ftp_connect($ftp)) {
185      if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {      return false;
       return false;  
     }  
186    }    }
187    
188    // We are now connected, so let's retrieve the file contents.    // We are now connected, so let's retrieve the file contents.
189    // Firstly, we change into the directory    // Firstly, we change into the directory
190    $chDir = @ftp_chdir($_DRUPAL_FTP->__conn, $directory);    $chDir = @ftp_chdir($ftp->__conn, $directory);
191    
192    if (!$chDir) {    if (!$chDir) {
193      drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory' => $directory)));      drupal_ftp_error(t('Couldn\'t change into directory: @directory.', array('@directory' => $directory)));
194      return false;      return false;
195    }    }
196    
# Line 169  function drupal_ftp_upload_file($data, $ Line 199  function drupal_ftp_upload_file($data, $
199    $fp = @fopen($local_filename, 'wb');    $fp = @fopen($local_filename, 'wb');
200    
201    if (!$fp) {    if (!$fp) {
202      drupal_ftp_error(t('Couldn\'t open a local file for temporary output'));      drupal_ftp_error(t('Couldn\'t open a local file for temporary output.'));
203      return false;      return false;
204    }    }
205    
# Line 177  function drupal_ftp_upload_file($data, $ Line 207  function drupal_ftp_upload_file($data, $
207    $filePut = @fputs($fp, $data, strlen($data));    $filePut = @fputs($fp, $data, strlen($data));
208    
209    if (!$filePut) {    if (!$filePut) {
210      drupal_ftp_error(t('Couldn\'t write to a local file for temporary output'));      drupal_ftp_error(t('Couldn\'t write to a local file for temporary output.'));
211      return false;      return false;
212    }    }
213    else {    else {
# Line 187  function drupal_ftp_upload_file($data, $ Line 217  function drupal_ftp_upload_file($data, $
217    // Now we can try to write to the remote file    // Now we can try to write to the remote file
218    $complete_filename = $directory . '/' . $filename;    $complete_filename = $directory . '/' . $filename;
219    
220    $putFile = @ftp_put($_DRUPAL_FTP->__conn, $complete_filename, $local_filename, FTP_BINARY);    $putFile = @ftp_put($ftp->__conn, $complete_filename, $local_filename, FTP_BINARY);
221    
222    if (!$putFile) {    if (!$putFile) {
223      drupal_ftp_error(t('Couldn\'t write to @complete_filename when trying to save file', array('@complete_filename', $complete_filename)));      drupal_ftp_error(t('Couldn\'t write to @complete_filename when trying to save file on the ftp server.', array('@complete_filename', $complete_filename)));
224      return false;      return false;
225    }    }
226    
# Line 203  function drupal_ftp_upload_file($data, $ Line 233  function drupal_ftp_upload_file($data, $
233   *  This function simply changes into the $directory folder on the FTP server.   *  This function simply changes into the $directory folder on the FTP server.
234   *  If a connection or permission error occurs then drupal_ftp_error() will contain the error message.   *  If a connection or permission error occurs then drupal_ftp_error() will contain the error message.
235   */   */
236  function drupal_ftp_change_directory($directory) {  function drupal_ftp_change_directory($directory, &$ftp) {
   global $_DRUPAL_FTP;  
237    // Switch to another directory on the web server. If we don't    // Switch to another directory on the web server. If we don't
238    // have permissions then an error will occur    // have permissions then an error will occur
239    
240    if (!drupal_ftp_connected()) {    if (!@drupal_ftp_connect($ftp)) {
241      // Connect is dead, attempt to reconnect      return false;
     if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {  
       return false;  
     }  
242    }    }
243    
244    // Try and change into another directory    // Try and change into another directory
245    $chDir = ftp_chdir($_DRUPAL_FTP->__conn, $directory);    $chDir = ftp_chdir($ftp->__conn, $directory);
246    
247    if (!$chDir) {    if (!$chDir) {
248      drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory', $directory)));      drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory', $directory)));
# Line 233  function drupal_ftp_change_directory($di Line 259  function drupal_ftp_change_directory($di
259   *  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.
260   *  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.
261   */   */
262  function drupal_ftp_file_list($directory) {  function drupal_ftp_file_list($directory, &$ftp) {
   global $_DRUPAL_FTP;  
263    // This function will attempt to change into the specified    // This function will attempt to change into the specified
264    // directory and retrieve a list of files as an associative    // directory and retrieve a list of files as an associative
265    // array. This list will include file name, size and date last modified    // array. This list will include file name, size and date last modified
# Line 242  function drupal_ftp_file_list($directory Line 267  function drupal_ftp_file_list($directory
267    $fileArray = array();    $fileArray = array();
268    
269    // Can we switch to the desired directory?    // Can we switch to the desired directory?
270    if (!drupal_ftp_change_directory($directory)) {    if (!drupal_ftp_change_directory($directory, $ftp)) {
271      return false;      return false;
272    }    }
273    
274    // We are in the directory, let's retrieve a list of files    // We are in the directory, let's retrieve a list of files
275    $fileList = ftp_rawlist($_DRUPAL_FTP->__conn, $directory);    $fileList = ftp_rawlist($ftp->__conn, $directory);
276    
277    // Save the list of files    // Save the list of files
278    if (@is_array($fileList)) {    if (@is_array($fileList)) {
# Line 297  function drupal_ftp_file_list($directory Line 322  function drupal_ftp_file_list($directory
322   *  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.
323   *  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.
324   */   */
325  function drupal_ftp_create_directory($folderName) {  function drupal_ftp_create_directory($folderName, &$ftp) {
   global $_DRUPAL_FTP;  
326    // Makes a new folder on the web server via FTP    // Makes a new folder on the web server via FTP
327    
328    if (!drupal_ftp_connected()) {    if (!@drupal_ftp_connect($ftp)) {
329      // Connect is dead, attempt to reconnect      return false;
     if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {  
       return false;  
     }  
330    }    }
331    
332    $createResult = @ftp_mkdir($_DRUPAL_FTP->__conn, $folderName);    $createResult = @ftp_mkdir($ftp->__conn, $folderName);
333    
334    if ($createResult == true) {    if ($createResult == true) {
335      // Can we change the files permissions?      // Can we change the files permissions?
336      $execResult = @ftp_site($_DRUPAL_FTP->__conn, 'chmod 0777 ' . $folderName . '/');      $execResult = @ftp_site($ftp->__conn, 'chmod 0777 ' . $folderName . '/');
337    
338      if ($execResult == true) {      if ($execResult == true) {
339        return true;        return true;
# Line 330  function drupal_ftp_create_directory($fo Line 351  function drupal_ftp_create_directory($fo
351    
352  /**  /**
353   *  The drupal_ftp_rename_file Function   *  The drupal_ftp_rename_file Function
354   *  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 $old_filename to $new_filename.
355   */   */
356  function drupal_ftp_rename_file($OldFileName, $NewFileName, $IsFolder) {  function drupal_ftp_rename_file($old_filename, $new_filename, $is_folder, &$ftp) {
   global $_DRUPAL_FTP;  
357    // Rename a file/directory on the FTP server    // Rename a file/directory on the FTP server
   global $php_errormsg;  
358    
359    if (!drupal_ftp_connected()) {    if (!@drupal_ftp_connect($ftp)) {
360      // Connect is dead, attempt to reconnect      return false;
     if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {  
       return false;  
     }  
361    }    }
362    
363    $renameResult = @ftp_rename($_DRUPAL_FTP->__conn, $OldFileName, $NewFileName);    $rename_result = @ftp_rename($ftp->__conn, $old_filename, $new_filename);
364    
365    if ($renameResult == true) {    if ($rename_result == true) {
366      // The file/folder was renamed successfully      // The file/folder was renamed successfully
367      return true;      return true;
368    }    }
369    else {    else {
370      // Couldn't rename the file/folder      // Couldn't rename the file/folder
371      if ($IsFolder == 0) {      if (!$is_folder) {
372        drupal_ftp_error(t('Couldn\'t rename the selected folder: @error', array('@error' => @$php_errormsg)));        drupal_ftp_error(t('Couldn\'t rename the selected folder: @filename', array('@filename' => $old_filename)));
373      }      }
374      else {      else {
375        drupal_ftp_error(t('Couldn\'t rename the selected file: @error', array('@error' => @$php_errormsg)));        drupal_ftp_error(t('Couldn\'t rename the selected file: @filename', array('@filename' => $old_filename)));
376      }      }
377    
378      return false;      return false;
# Line 367  function drupal_ftp_rename_file($OldFile Line 383  function drupal_ftp_rename_file($OldFile
383   *  The drupal_ftp_delete_file Function   *  The drupal_ftp_delete_file Function
384   *  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.
385   */   */
386  function drupal_ftp_delete_file($filename) {  function drupal_ftp_delete_file($filename, &$ftp) {
   global $_DRUPAL_FTP;  
387    // Remove the specified file from the FTP server    // Remove the specified file from the FTP server
388    if (!drupal_ftp_connected()) {    if (!@drupal_ftp_connect($ftp)) {
389      // Connect is dead, attempt to reconnect      return false;
     if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {  
       return false;  
     }  
390    }    }
391    
392    $deleteResult = @ftp_delete($_DRUPAL_FTP->__conn, $filename);    $deleteResult = @ftp_delete($ftp->__conn, $filename);
393    
394    if ($deleteResult == true) {    if ($deleteResult == true) {
395      // The file/folder was renamed successfully      // The file/folder was renamed successfully
# Line 385  function drupal_ftp_delete_file($filenam Line 397  function drupal_ftp_delete_file($filenam
397    }    }
398    else {    else {
399      // Couldn't delete the selected file      // Couldn't delete the selected file
400      drupal_ftp_error(t('Couldn\'t delete the selected file: @error', array('@error' => @$php_errormsg)));      drupal_ftp_error(t('Couldn\'t delete the selected file: @filename', array('@filename' => $filename)));
401      return false;      return false;
402    }    }
403  }  }
# Line 394  function drupal_ftp_delete_file($filenam Line 406  function drupal_ftp_delete_file($filenam
406   *  The drupal_ftp_delete_folder Function   *  The drupal_ftp_delete_folder Function
407   *  This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $folder_name.   *  This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $folder_name.
408   */   */
409  function drupal_ftp_delete_folder($folder_name) {  function drupal_ftp_delete_folder($folder_name, &$ftp) {
410    global $_DRUPAL_FTP;    if (!@drupal_ftp_connect($ftp)) {
411    // Remove the specified folder and all subdirectories/files from the FTP server      return false;
   global $php_errormsg;  
   
   if (!drupal_ftp_connected()) {  
     // Connect is dead, attempt to reconnect  
     if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {  
       return false;  
     }  
412    }    }
413    
414    @ftp_chdir($_DRUPAL_FTP->__conn, $folder_name);    @ftp_chdir($ftp->__conn, $folder_name);
415    $location = @ftp_pwd($_DRUPAL_FTP->__conn);    $location = @ftp_pwd($ftp->__conn);
416    
417    $directories = array();    $directories = array();
418    $files = array();    $files = array();
419    $dir_counter = 0;    $dir_counter = 0;
420    $file_counter = 0;    $file_counter = 0;
421    $content = @ftp_nlist($_DRUPAL_FTP->__conn, ".");    $content = @ftp_nlist($ftp->__conn, ".");
422    
423    for ($i = 0; $i < sizeof($content); $i++) {    for ($i = 0; $i < sizeof($content); $i++) {
424      // If we can change into this then it's a directory.      // If we can change into this then it's a directory.
425      // If not, it's a file      // If not, it's a file
426      if ($content[$i] != "." && $content[$i] != "..") {      if ($content[$i] != "." && $content[$i] != "..") {
427        if (@ftp_chdir($_DRUPAL_FTP->__conn, $content[$i])) {        if (@ftp_chdir($ftp->__conn, $content[$i])) {
428          // We have a directory          // We have a directory
429          $directories[] = $content[$i];          $directories[] = $content[$i];
430          $dir_counter++;          $dir_counter++;
431          @ftp_cdup($_DRUPAL_FTP->__conn);          @ftp_cdup($ftp->__conn);
432        }        }
433        else {        else {
434          // We have a file          // We have a file
# Line 434  function drupal_ftp_delete_folder($folde Line 439  function drupal_ftp_delete_folder($folde
439    }    }
440    
441    for ($j = 0; $j < $file_counter; $j++) {    for ($j = 0; $j < $file_counter; $j++) {
442      @ftp_delete($_DRUPAL_FTP->__conn, $files[$j]);      @ftp_delete($ftp->__conn, $files[$j]);
443    }    }
444    
445    for ($j = 0; $j < $dir_counter; $j++) {    for ($j = 0; $j < $dir_counter; $j++) {
446      if ($directories[$j] != "." OR $directories[$j] != "..") {      if ($directories[$j] != "." OR $directories[$j] != "..") {
447        $location = ftp_pwd ($_DRUPAL_FTP->__conn);        $location = ftp_pwd ($ftp->__conn);
448        drupal_ftp_delete_folder($directories[$j]);        drupal_ftp_delete_folder($directories[$j], $ftp);
449        @ftp_cdup ($_DRUPAL_FTP->__conn);        @ftp_cdup ($ftp->__conn);
450        @ftp_rmdir($_DRUPAL_FTP->__conn,$directories[$j]);        @ftp_rmdir($ftp->__conn,$directories[$j]);
451      }      }
452    }    }
453    
454    // 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
455    // if we can cdup. If we can, we delete it.    // if we can cdup. If we can, we delete it.
456    @ftp_chdir($_DRUPAL_FTP->__conn, $folder_name);    @ftp_chdir($ftp->__conn, $folder_name);
457    @ftp_cdup($_DRUPAL_FTP->__conn);    @ftp_cdup($ftp->__conn);
458    @ftp_rmdir($_DRUPAL_FTP->__conn, $folder_name);    @ftp_rmdir($ftp->__conn, $folder_name);
459    
460    // Did the recursive folder/file deletion work?    // Did the recursive folder/file deletion work?
461    return true;    return true;
# Line 473  function drupal_ftp_delete_folder($folde Line 478  function drupal_ftp_delete_folder($folde
478   *  This function checks if a file called $filename exists in the directory called $folder_name on the FTP server.   *  This function checks if a file called $filename exists in the directory called $folder_name on the FTP server.
479   *  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.
480   */   */
481  function drupal_ftp_file_exists($folder_name, $filename) {  function drupal_ftp_file_exists($folder_name, $filename, &$ftp) {
   global $_DRUPAL_FTP;  
482    // Does the specified file exist on the remote FTP server?    // Does the specified file exist on the remote FTP server?
483    // Returns false on error, true on file exists and 2 if it doesn't exist    // Returns NULL on error, true on file exists and false if it doesn't exist
   
   global $php_errormsg;  
484    
485    if (!drupal_ftp_connected()) {    if (!drupal_ftp_change_directory($folder_name, $ftp)) {
486      // Connect is dead, attempt to reconnect      return NULL;
     if (!@drupal_ftp_connect($_DRUPAL_FTP->__server, $_DRUPAL_FTP->__user, $_DRUPAL_FTP->__password, $_DRUPAL_FTP->__directory)) {  
       return false;  
     }  
487    }    }
488    
489    if (!@ftp_chdir($_DRUPAL_FTP->__conn, $folder_name)) {    // We have changed into the directory, let's get a list
490      drupal_ftp_error($php_errormsg);    // of files using ftp_nlist and compare it to see if it exists
491      return false;    $fileArray = @ftp_nlist($ftp->__conn, $folder_name);
492    
493      if (!is_array($fileArray)) {
494        drupal_ftp_error(t('Unable to retrieve an ftp directory listing in @folder.', array('@folder' => $folder_name)));
495        return NULL;
496    }    }
   else {  
     // We have changed into the directory, let's get a list  
     // of files using ftp_nlist and compare it to see if it exists  
     $fileArray = @ftp_nlist($_DRUPAL_FTP->__conn, $folder_name);  
497    
498      if (!is_array($fileArray)) {    $sep = $folder_name[strlen($folder_name)-1] == '/' ? '' : '/';
499        drupal_ftp_error($php_errormsg);    return in_array($folder_name . $sep . $filename, $fileArray);
500        return false;  }
501    
502    function drupal_ftp_create_filename($basename, $directory, &$ftp) {
503      $dest = $basename;
504    
505      if (drupal_ftp_file_exists($directory, $dest, $ftp)) {
506        // Destination file already exists, generate an alternative.
507        if ($pos = strrpos($basename, '.')) {
508          $name = substr($basename, 0, $pos);
509          $ext = substr($basename, $pos);
510      }      }
511      else {      else {
512        // Loop through each file and check it if exists        $name = $basename;
       for($i = 0; $i < sizeof($fileArray); $i++) {  
         if ($fileArray[$i] == "$folder_name/$filename") {  
           return true;  
         }  
       }  
   
       // The file wasn't found, return 2 for not found  
       return 2;  
513      }      }
514    
515        $counter = 0;
516        do {
517          $dest = $name .'_'. $counter++ . $ext;
518        } while (drupal_ftp_file_exists($directory, $dest, $ftp));
519    }    }
520    
521      return $dest;
522  }  }
523    
524  function drupal_ftp_error($message = NULL, $severity = WATCHDOG_ERROR, $notify = true) {  function drupal_ftp_error($message = NULL, $severity = WATCHDOG_ERROR, $notify = true, $type = 'error') {
525    static $errors;    static $errors;
526    
527    if (isset($message)) {    if (isset($message)) {
528      $errors[] = $message;      $errors[] = $message;
529      watchdog('drupal_ftp', $message, $severity);      watchdog('drupal_ftp', $message, $severity);
530      if ($notify) {      if ($notify) {
531        drupal_set_message($message, 'error');        drupal_set_message($message, $type);
532      }      }
533    }    }
534    return $errors;    return $errors;
# Line 602  function drupal_ftp_transfer_file() { Line 609  function drupal_ftp_transfer_file() {
609    
610  function drupal_ftp_transfer_file_submit($form, $form_values) {  function drupal_ftp_transfer_file_submit($form, $form_values) {
611    $dir = $form_values['directory'];    $dir = $form_values['directory'];
612      $home = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
   $server = variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER);  
   $username = variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME);  
   $password = variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD);  
   $home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);  
   
613    $url = check_url($form_values['url']);    $url = check_url($form_values['url']);
614    $result = drupal_http_request($url);    $result = drupal_http_request($url);
615    
616    if ($result->code == 200) {    if ($result->code == 200) {
617      $data = $result->data;      $data = $result->data;
618      if (drupal_ftp_connect($server, $username, $password, $home_dir)) {      if (drupal_ftp_data_to_ftp($data, check_plain($form_values['filename']), $home . $dir, $ftp)) {
619        if (drupal_ftp_upload_file($data, check_plain($form_values['filename']), $home_dir . $dir)) {        drupal_set_message(t('File transfered.'));
         drupal_set_message(t('File transfered.'));  
       }  
620      }      }
621    }    }
622    return 'ftp/browse' . $dir;    return 'ftp/browse' . $dir;
# Line 626  function drupal_ftp_browse_dl() { Line 626  function drupal_ftp_browse_dl() {
626    $args = func_get_args();    $args = func_get_args();
627    $directory = implode('/', $args);    $directory = implode('/', $args);
628    
629    $server = variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER);    $home = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
   $username = variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME);  
   $password = variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD);  
   $home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);  
630    
631    $file = $_GET['file'];    $file = $_GET['file'];
632    if (drupal_ftp_connect($server, $username, $password, $home_dir)) {    $data = drupal_ftp_ftp_to_data($file, $home . $directory, $ftp);
633      $data = drupal_ftp_download_file($file, $home_dir . $directory);    if ($data) {
634      if ($data) {      drupal_set_title($file);
       drupal_set_title($file);  
     }  
635    }    }
636    $parent = l(t('..'), 'ftp/browse/' . $directory);    $parent = l(t('..'), 'ftp/browse/' . $directory);
637    $output = theme('drupal_ftp_download', $parent, $data);    $output = theme('drupal_ftp_download', $parent, $data);
# Line 647  function drupal_ftp_browse_page() { Line 642  function drupal_ftp_browse_page() {
642    $args = func_get_args();    $args = func_get_args();
643    $fileList = array();    $fileList = array();
644    
   $server = variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER);  
   $username = variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME);  
   $password = variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD);  
645    $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);
646    
647    $directory = implode('/', $args);    $directory = implode('/', $args);
648    
649    if (drupal_ftp_connect($server, $username, $password, $home_dir)) {    $fileList = drupal_ftp_file_list($home_dir . $directory, $ftp);
650      $fileList = drupal_ftp_file_list($home_dir . $directory);    $browser = array();
651      $browser = array();    $links = array();
652      $links = array();  
653      if (!empty($args)) {
654      if (!empty($args)) {      $parents = $args;
655        $parents = $args;      array_pop($parents);
656        array_pop($parents);      $browser[] = l('..', 'ftp/browse' . (!empty($parents) ? '/' . implode('/', $parents) : ''));
657        $browser[] = l('..', 'ftp/browse' . (!empty($parents) ? '/' . implode('/', $parents) : ''));    }
658      }    if (is_array($fileList)) {
659      if (is_array($fileList)) {      for($i = 0; $i < sizeof($fileList); $i++) {
660        for($i = 0; $i < sizeof($fileList); $i++) {        if($fileList[$i]["type"] == 0) {
661          if($fileList[$i]["type"] == 0) {          // Folder
662            // Folder          $browser[] = l($fileList[$i]["filename"], 'ftp/browse/' . (!empty($args) ? $directory . '/' : '') . $fileList[$i]["filename"]) . " (directory)";
663            $browser[] = l($fileList[$i]["filename"], 'ftp/browse/' . (!empty($args) ? $directory . '/' : '') . $fileList[$i]["filename"]) . " (directory)";        }
664          }        else {
665          else {          // File
666            // File          $browser[] = l($fileList[$i]["filename"], 'ftp/dl/' . (!empty($args) ? $directory : ''), array(), 'file=' . $fileList[$i]["filename"]) . " (" . $fileList[$i]["filesize"] . " bytes)";
           $browser[] = l($fileList[$i]["filename"], 'ftp/dl/' . (!empty($args) ? $directory : ''), array(), 'file=' . $fileList[$i]["filename"]) . " (" . $fileList[$i]["filesize"] . " bytes)";  
         }  
667        }        }
668      }      }
     else {  
       $browser[] = theme('drupal_ftp_empty_directory_message', $directory);  
     }  
     if (user_access('write drupal ftp')) {  
       $links[] = l(t('transfer file'), 'ftp/transfer/' . $directory);  
     }  
     drupal_set_title(t('FTP Browse @directory', array('@directory' => $directory)));  
     $output .= theme('drupal_ftp_browser', $browser, $links);  
669    }    }
670      else {
671        $browser[] = theme('drupal_ftp_empty_directory_message', $directory);
672      }
673      if (user_access('write drupal ftp')) {
674        $links[] = l(t('transfer file'), 'ftp/transfer/' . $directory);
675      }
676      drupal_set_title(t('FTP Browse @directory', array('@directory' => $directory)));
677      $output .= theme('drupal_ftp_browser', $browser, $links);
678    
679    return $output;    return $output;
680  }  }
681    

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

  ViewVC Help
Powered by ViewVC 1.1.2