/[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.5, Thu Jun 28 21:08:57 2007 UTC revision 1.5.4.1, Tue Jul 28 13:47:54 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: drupal_ftp.module,v 1.4 2007/06/28 14:55:49 aaron Exp $  // $Id$
3    
4  // Inspired by http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/  /**
5  // It's been drupalized, however, and most of the bugs from that example have been fixed.   * @file
6  // - winborn 2007-06-22 - 2007-06-28   * Inspired by http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/
7     * It's been drupalized, however, and most of the bugs from that example have
8     * been fixed.
9     * - winborn 2007-06-22 - 2007-06-28
10     *
11     * Converted to 6.x by alexkb on 28/07/09
12     */
13    
14  define('DRUPAL_FTP_FT_DIRECTORY', 0);  define('DRUPAL_FTP_FT_DIRECTORY', 0);
15  define('DRUPAL_FTP_FT_FILE', 1);  define('DRUPAL_FTP_FT_FILE', 1);
# Line 13  define('DRUPAL_FTP_DEFAULT_PASSWORD', 's Line 19  define('DRUPAL_FTP_DEFAULT_PASSWORD', 's
19  define('DRUPAL_FTP_DEFAULT_HOME_DIRECTORY', '/pub/drupal/');  define('DRUPAL_FTP_DEFAULT_HOME_DIRECTORY', '/pub/drupal/');
20    
21  /**  /**
22   *  creates a new ftp object. if any elements of ftp_map are missing, they'll be filled with the server defaults.   * Creates a new ftp object. if any elements of ftp_map are missing,
23     * they'll be filled with the server defaults.
24   */   */
25  function drupal_ftp_ftp_object($ftp_map = array()) {  function drupal_ftp_ftp_object($ftp_map = array()) {
26    $ftp = new stdClass();    $ftp = new stdClass();
# Line 28  function drupal_ftp_ftp_object($ftp_map Line 35  function drupal_ftp_ftp_object($ftp_map
35    
36  /**  /**
37   *  The drupal_ftp_connect function   *  The drupal_ftp_connect function
38   *  This function connects to an FTP server and attempts to change into the directory specified by   *  This function connects to an FTP server and attempts to change into the
39   *  the fourth parameter, $directory.   *  directory specified by the fourth parameter, $directory.
40   */   */
41  function drupal_ftp_connect(&$ftp) {  function drupal_ftp_connect(&$ftp) {
42    if (is_null($ftp)) {    if (is_null($ftp)) {
# Line 42  function drupal_ftp_connect(&$ftp) { Line 49  function drupal_ftp_connect(&$ftp) {
49    
50      if (!$ftp->__conn) {      if (!$ftp->__conn) {
51        drupal_ftp_error(t('Couldn\'t connect to server @server', array('@server' => $ftp->__server)));        drupal_ftp_error(t('Couldn\'t connect to server @server', array('@server' => $ftp->__server)));
52        return false;        return FALSE;
53      }      }
54    
55      // Attempt to login to the remote server      // Attempt to login to the remote server
# Line 50  function drupal_ftp_connect(&$ftp) { Line 57  function drupal_ftp_connect(&$ftp) {
57    
58      if (!$ftp->__login) {      if (!$ftp->__login) {
59        drupal_ftp_error(t('Couldn\'t login as user @ftp_user to @server', array('@ftp_user' => $ftp->__user, '@server' => $ftp->__server)));        drupal_ftp_error(t('Couldn\'t login as user @ftp_user to @server', array('@ftp_user' => $ftp->__user, '@server' => $ftp->__server)));
60        return false;        return FALSE;
61      }      }
62    
63      // Attempt to change into the working directory      // Attempt to change into the working directory
64      $chDir = @ftp_chdir($ftp->__conn, $ftp->__directory);      $ch_dir = @ftp_chdir($ftp->__conn, $ftp->__directory);
65    
66      if (!$chDir) {      if (!$ch_dir) {
67        drupal_ftp_error(t('Couldn\'t change into the @directory directory', array('@directory' => $ftp->__directory)));        drupal_ftp_error(t('Couldn\'t change into the @directory directory', array('@directory' => $ftp->__directory)));
68        return false;        return FALSE;
69      }      }
70    }    }
71    
72    // Everything worked OK, return true    // Everything worked OK, return TRUE
73    return true;    return TRUE;
74  }  }
75    
76  /**  /**
77   * The drupal_ftp_connected function   * The drupal_ftp_connected function
78   * 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
79   * It returns true on success or false on disconnection.   * if the connection is still alive. It returns TRUE on success or FALSE on
80     * disconnection.
81   */   */
82    
83  function drupal_ftp_connected(&$ftp) {  function drupal_ftp_connected(&$ftp) {
# Line 78  function drupal_ftp_connected(&$ftp) { Line 86  function drupal_ftp_connected(&$ftp) {
86    
87    if (is_null($ftp)) {    if (is_null($ftp)) {
88      $ftp = drupal_ftp_ftp_object();      $ftp = drupal_ftp_ftp_object();
89      return false;      return FALSE;
90    }    }
91    
92    if (!@ftp_systype($ftp->__conn)) {    if (!@ftp_systype($ftp->__conn)) {
93      // The connection is dead      // The connection is dead
94      return false;      return FALSE;
95    }    }
96    else {    else {
97      // The connection is still alive      // The connection is still alive
98      return true;      return TRUE;
99    }    }
100  }  }
101    
102  /**  /**
103   *  The drupal_ftp_ftp_to_data Function   *  The drupal_ftp_ftp_to_data Function
104   *  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.
105   *  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
106   *  The file is saved locally and its contents are returned to the caller of the function.   *  download the file $file_name. The file is saved locally and its contents
107     *  are returned to the caller of the function.
108   */   */
109  function drupal_ftp_ftp_to_data($filename, $directory, &$ftp) {  function drupal_ftp_ftp_to_data($file_name, $directory, &$ftp) {
110    // Change into the remote directory and retrieve the content    // Change into the remote directory and retrieve the content
111    // of a file. Once retrieve, return this value to the caller    // of a file. Once retrieve, return this value to the caller
112    
113    if (!@drupal_ftp_connect($ftp)){    if (!@drupal_ftp_connect($ftp)) {
114      return false;      return FALSE;
115    }    }
116    
117    // We are now connected, so let's retrieve the file contents.    // We are now connected, so let's retrieve the file contents.
118    // Firstly, we change into the directory    // Firstly, we change into the directory
119    $chDir = @ftp_chdir($ftp->__conn, $directory);    $ch_dir = @ftp_chdir($ftp->__conn, $directory);
120    
121    if (!$chDir) {    if (!$ch_dir) {
122      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)));
123      return false;      return FALSE;
124    }    }
125    
126    // 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
127    $temp_file = file_create_filename($filename, file_directory_temp());    $temp_file = file_create_filename($file_name, file_directory_temp());
128    $fp = @fopen($temp_file, 'wb');    $fp = @fopen($temp_file, 'wb');
129    $getFile = @ftp_fget($ftp->__conn, $fp, $filename, FTP_BINARY);    $get_file = @ftp_fget($ftp->__conn, $fp, $file_name, FTP_BINARY);
130    fclose($fp);    fclose($fp);
131    
132    $fp = null;    $fp = NULL;
133    
134    if (!$getFile) {    if (!$get_file) {
135      drupal_ftp_error(t('Unable to download file: @filename from @directory', array('@filename' => $filename, '@directory' => $directory)));      drupal_ftp_error(t('Unable to download file: @filename from @directory', array('@filename' => $file_name, '@directory' => $directory)));
136      return false;      return FALSE;
137    }    }
138    
139    // The file was downloaded successfully. Let's open it, read in its    // The file was downloaded successfully. Let's open it, read in its
# Line 133  function drupal_ftp_ftp_to_data($filenam Line 142  function drupal_ftp_ftp_to_data($filenam
142    $fp = @fopen($temp_file, 'rb');    $fp = @fopen($temp_file, 'rb');
143    
144    if (!$fp) {    if (!$fp) {
145      drupal_ftp_error(t('Unable to open @filename after it was downloaded from {@server}', array('@filename' => $filename, '@server' => $_DRUPAL_FTP->__server)));      drupal_ftp_error(t('Unable to open @filename after it was downloaded from {@server}', array('@filename' => $file_name, '@server' => $_DRUPAL_FTP->__server)));
146      return false;      return FALSE;
147    }    }
148    
149    // Read in the contents of the file to a variable    // Read in the contents of the file to a variable
150    $data = '';    $data = '';
151    
152    while(!feof($fp)) {    while (!feof($fp)) {
153      $data.= fread($fp, 4096);      $data .= fread($fp, 4096);
154    }    }
155    
156    @fclose($fp);    @fclose($fp);
# Line 152  function drupal_ftp_ftp_to_data($filenam Line 161  function drupal_ftp_ftp_to_data($filenam
161    
162  function drupal_ftp_file_to_ftp($file, $ftp_filename, $ftp_directory, &$ftp) {  function drupal_ftp_file_to_ftp($file, $ftp_filename, $ftp_directory, &$ftp) {
163    if (!@drupal_ftp_connect($ftp)) {    if (!@drupal_ftp_connect($ftp)) {
164      return false;      return FALSE;
165    }    }
166    
167    if ($source = file_create_path($file)) {    if ($source = file_create_path($file)) {
168      // Now we can try to write to the remote file      // Now we can try to write to the remote file
169      $complete_filename = $ftp_directory . '/' . $ftp_filename;      $complete_filename = $ftp_directory .'/'. $ftp_filename;
170      $putFile = @ftp_put($ftp->__conn, $complete_filename, $source, FTP_BINARY);      $put_file = @ftp_put($ftp->__conn, $complete_filename, $source, FTP_BINARY);
171      if (!$putFile) {      if (!$put_file) {
172        drupal_ftp_error(t('Couldn\'t write to @complete_filename when trying to save file on the ftp server.', 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)));
173        return false;        return FALSE;
174      }      }
175    
176      // Everything worked OK      // Everything worked OK
177      return true;      return TRUE;
178    }    }
179    else {    else {
180      drupal_ftp_error(t('Couldn\'t find @file.', array('@file')));      drupal_ftp_error(t('Couldn\'t find @file.', array('@file')));
181      return false;      return FALSE;
182    }    }
183  }  }
184    
185  /**  /**
186   *  The drupal_ftp_data_to_ftp function   *  The drupal_ftp_data_to_ftp function
187   *  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 $file_name in the
188   *  and will write $data to this file. If the file already exists then an error will be raised.   *  $directory folder on the FTP server and will write $data to this file.
189     *  If the file already exists then an error will be raised.
190   */   */
191  function drupal_ftp_data_to_ftp($data, $filename, $directory, &$ftp) {  function drupal_ftp_data_to_ftp($data, $file_name, $directory, &$ftp) {
192    // Save HTML to a remote file on the FTP server    // Save HTML to a remote file on the FTP server
193    
194    if (!@drupal_ftp_connect($ftp)) {    if (!@drupal_ftp_connect($ftp)) {
195      return false;      return FALSE;
196    }    }
197    
198    // We are now connected, so let's retrieve the file contents.    // We are now connected, so let's retrieve the file contents.
199    // Firstly, we change into the directory    // Firstly, we change into the directory
200    $chDir = @ftp_chdir($ftp->__conn, $directory);    $ch_dir = @ftp_chdir($ftp->__conn, $directory);
201    
202    if (!$chDir) {    if (!$ch_dir) {
203      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)));
204      return false;      return FALSE;
205    }    }
206    
207    $local_filename = file_create_filename($filename, file_directory_temp());    $local_filename = file_create_filename($file_name, file_directory_temp());
208    // Save the HTML to a file which we then upload    // Save the HTML to a file which we then upload
209    $fp = @fopen($local_filename, 'wb');    $fp = @fopen($local_filename, 'wb');
210    
211    if (!$fp) {    if (!$fp) {
212      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.'));
213      return false;      return FALSE;
214    }    }
215    
216    // The file was opened OK, let's write to it    // The file was opened OK, let's write to it
217    $filePut = @fputs($fp, $data, strlen($data));    $file_put = @fputs($fp, $data, strlen($data));
218    
219    if (!$filePut) {    if (!$file_put) {
220      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.'));
221      return false;      return FALSE;
222    }    }
223    else {    else {
224      @fclose($fp);      @fclose($fp);
225    }    }
226    
227    // Now we can try to write to the remote file    // Now we can try to write to the remote file
228    $complete_filename = $directory . '/' . $filename;    $complete_filename = $directory .'/'. $file_name;
229    
230    $putFile = @ftp_put($ftp->__conn, $complete_filename, $local_filename, FTP_BINARY);    $put_file = @ftp_put($ftp->__conn, $complete_filename, $local_filename, FTP_BINARY);
231    
232    if (!$putFile) {    if (!$put_file) {
233      drupal_ftp_error(t('Couldn\'t write to @complete_filename when trying to save file on the ftp server.', 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)));
234      return false;      return FALSE;
235    }    }
236    
237    // Everything worked OK    // Everything worked OK
238    return true;    return TRUE;
239  }  }
240    
241  /**  /**
242   *  The drupal_ftp_change_directory Function   *  The drupal_ftp_change_directory Function
243   *  This function simply changes into the $directory folder on the FTP server.   *  This function simply changes into the $directory folder on the FTP server.
244   *  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
245     *  contain the error message.
246   */   */
247  function drupal_ftp_change_directory($directory, &$ftp) {  function drupal_ftp_change_directory($directory, &$ftp) {
248    // Switch to another directory on the web server. If we don't    // Switch to another directory on the web server. If we don't
249    // have permissions then an error will occur    // have permissions then an error will occur
250    
251    if (!@drupal_ftp_connect($ftp)) {    if (!@drupal_ftp_connect($ftp)) {
252      return false;      return FALSE;
253    }    }
254    
255    // Try and change into another directory    // Try and change into another directory
256    $chDir = ftp_chdir($ftp->__conn, $directory);    $ch_dir = ftp_chdir($ftp->__conn, $directory);
257    
258    if (!$chDir) {    if (!$ch_dir) {
259      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)));
260      return false;      return FALSE;
261    }    }
262    else {    else {
263      // Changing directories worked OK      // Changing directories worked OK
264      return true;      return TRUE;
265    }    }
266  }  }
267    
268  /**  /**
269   *  The drupal_ftp_file_list Function   *  The drupal_ftp_file_list Function
270   *  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
271   *  This function still needs a lot of work, but should work in most cases.   *  files and directories contained in that folder. This function still needs a
272     *  lot of work, but should work in most cases.
273   */   */
274  function drupal_ftp_file_list($directory, &$ftp) {  function drupal_ftp_file_list($directory, &$ftp) {
275    // This function will attempt to change into the specified    // This function will attempt to change into the specified
276    // directory and retrieve a list of files as an associative    // directory and retrieve a list of files as an associative
277    // array. This list will include file name, size and date last modified    // array. This list will include file name, size and date last modified
278    
279    $fileArray = array();    $file_array = array();
280    
281    // Can we switch to the desired directory?    // Can we switch to the desired directory?
282    if (!drupal_ftp_change_directory($directory, $ftp)) {    if (!drupal_ftp_change_directory($directory, $ftp)) {
283      return false;      return FALSE;
284    }    }
285    
286    // We are in the directory, let's retrieve a list of files    // We are in the directory, let's retrieve a list of files
287    $fileList = ftp_rawlist($ftp->__conn, $directory);    $file_list = ftp_rawlist($ftp->__conn, $directory);
288    
289    // Save the list of files    // Save the list of files
290    if (@is_array($fileList)) {    if (@is_array($file_list)) {
291      // Interate through the array      // Interate through the array
292      for($i = 0; $i < sizeof($fileList); $i++) {      for ($i = 0; $i < sizeof($file_list); $i++) {
293        $itemArray = explode(' ', $fileList[$i]);        $item_array = explode(' ', $file_list[$i]);
294    
295        // Are we dealing with a file or directory?        // Are we dealing with a file or directory?
296        // If the first letter of the attributes is        // If the first letter of the attributes is
297        // 'd' then we are dealing with a directory        // 'd' then we are dealing with a directory
298        $attributes = $itemArray[0];        $attributes = $item_array[0];
299    
300        if (substr($attributes, 0, 1) == 'd') {        if (substr($attributes, 0, 1) == 'd') {
301          $fileType = DRUPAL_FTP_FT_DIRECTORY;          $file_type = DRUPAL_FTP_FT_DIRECTORY;
302        }        }
303        else {        else {
304          $fileType = DRUPAL_FTP_FT_FILE;          $file_type = DRUPAL_FTP_FT_FILE;
305        }        }
306    
307        // Get the file/directory name        // Get the file/directory name
308        $fileName = $itemArray[sizeof($itemArray)-1];        $file_name = $item_array[sizeof($item_array)-1];
309    
310        // Get the size of the file        // Get the size of the file
311        $fileSize = $itemArray[sizeof($itemArray)-5];        $file_size = $item_array[sizeof($item_array)-5];
312    
313        if (!is_numeric($fileSize)) {        if (!is_numeric($file_size)) {
314          $fileSize = $itemArray[sizeof($itemArray)-6];          $file_size = $item_array[sizeof($item_array)-6];
315        }        }
316    
317        // Get the date last modified        // Get the date last modified
318        $fileTimeStamp = $itemArray[sizeof($itemArray)-4] . ' ' . $itemArray[sizeof($itemArray)-3] . ' ' . $itemArray[sizeof($itemArray)-2];        $file_time_stamp = $item_array[sizeof($item_array)-4] .' '. $item_array[sizeof($item_array)-3] .' '. $item_array[sizeof($item_array)-2];
319    
320        $fileArray[] = array(        $file_array[] = array(
321          'type' => $fileType,          'type' => $file_type,
322          'filename' => $fileName,          'filename' => $file_name,
323          'filesize' => $fileSize,          'filesize' => $file_size,
324          'filetime' => $fileTimeStamp          'filetime' => $file_time_stamp
325        );        );
326      }      }
327    }    }
328    sort($fileArray);    sort($file_array);
329    return $fileArray;    return $file_array;
330  }  }
331    
332  /**  /**
333   *  The drupal_ftp_create_directory Function   *  The drupal_ftp_create_directory Function
334   *  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
335   *  If it can create the folder, then the folder is given appropriate rights with the CHMOD command.   *  server. If it can create the folder, then the folder is given appropriate
336     *  rights with the CHMOD command.
337   */   */
338  function drupal_ftp_create_directory($folderName, &$ftp) {  function drupal_ftp_create_directory($folder_name, &$ftp) {
339    // Makes a new folder on the web server via FTP    // Makes a new folder on the web server via FTP
340    
341    if (!@drupal_ftp_connect($ftp)) {    if (!@drupal_ftp_connect($ftp)) {
342      return false;      return FALSE;
343    }    }
344    
345    $createResult = @ftp_mkdir($ftp->__conn, $folderName);    $create_result = @ftp_mkdir($ftp->__conn, $folder_name);
346    
347    if ($createResult == true) {    if ($create_result == TRUE) {
348      // Can we change the files permissions?      // Can we change the files permissions?
349      $execResult = @ftp_site($ftp->__conn, 'chmod 0777 ' . $folderName . '/');      $exec_result = @ftp_site($ftp->__conn, 'chmod 0777 '. $folder_name .'/');
350    
351      if ($execResult == true) {      if ($exec_result == TRUE) {
352        return true;        return TRUE;
353      }      }
354      else {      else {
355        drupal_ftp_error(t('Couldn\'t set owner permissions on @folder.', array('@folder', $folderName)));        drupal_ftp_error(t('Couldn\'t set owner permissions on @folder.', array('@folder', $folder_name)));
356        return false;        return FALSE;
357      }      }
358    }    }
359    else {    else {
360      drupal_ftp_error(t('Couldn\'t create new folder @folder.', array('@folder', $folderName)));      drupal_ftp_error(t('Couldn\'t create new folder @folder.', array('@folder', $folder_name)));
361      return false;      return FALSE;
362    }    }
363  }  }
364    
365  /**  /**
366   *  The drupal_ftp_rename_file Function   *  The drupal_ftp_rename_file Function
367   *  This function attempts to rename a file on the FTP server from $old_filename to $new_filename.   *  This function attempts to rename a file on the FTP server from
368     *  $old_filename to $new_filename.
369   */   */
370  function drupal_ftp_rename_file($old_filename, $new_filename, $is_folder, &$ftp) {  function drupal_ftp_rename_file($old_filename, $new_filename, $is_folder, &$ftp) {
371    // Rename a file/directory on the FTP server    // Rename a file/directory on the FTP server
372    
373    if (!@drupal_ftp_connect($ftp)) {    if (!@drupal_ftp_connect($ftp)) {
374      return false;      return FALSE;
375    }    }
376    
377    $rename_result = @ftp_rename($ftp->__conn, $old_filename, $new_filename);    $rename_result = @ftp_rename($ftp->__conn, $old_filename, $new_filename);
378    
379    if ($rename_result == true) {    if ($rename_result == TRUE) {
380      // The file/folder was renamed successfully      // The file/folder was renamed successfully
381      return true;      return TRUE;
382    }    }
383    else {    else {
384      // Couldn't rename the file/folder      // Couldn't rename the file/folder
# Line 375  function drupal_ftp_rename_file($old_fil Line 389  function drupal_ftp_rename_file($old_fil
389        drupal_ftp_error(t('Couldn\'t rename the selected file: @filename', array('@filename' => $old_filename)));        drupal_ftp_error(t('Couldn\'t rename the selected file: @filename', array('@filename' => $old_filename)));
390      }      }
391    
392      return false;      return FALSE;
393    }    }
394  }  }
395    
396  /**  /**
397   *  The drupal_ftp_delete_file Function   *  The drupal_ftp_delete_file Function
398   *  This function attempts to delete a file called $filename from the FTP server.   *  This function attempts to delete a file called $file_name from the FTP
399     *  server.
400   */   */
401  function drupal_ftp_delete_file($filename, &$ftp) {  function drupal_ftp_delete_file($file_name, &$ftp) {
402    // Remove the specified file from the FTP server    // Remove the specified file from the FTP server
403    if (!@drupal_ftp_connect($ftp)) {    if (!@drupal_ftp_connect($ftp)) {
404      return false;      return FALSE;
405    }    }
406    
407    $deleteResult = @ftp_delete($ftp->__conn, $filename);    $delete_result = @ftp_delete($ftp->__conn, $file_name);
408    
409    if ($deleteResult == true) {    if ($delete_result == TRUE) {
410      // The file/folder was renamed successfully      // The file/folder was renamed successfully
411      return true;      return TRUE;
412    }    }
413    else {    else {
414      // Couldn't delete the selected file      // Couldn't delete the selected file
415      drupal_ftp_error(t('Couldn\'t delete the selected file: @filename', array('@filename' => $filename)));      drupal_ftp_error(t('Couldn\'t delete the selected file: @filename', array('@filename' => $file_name)));
416      return false;      return FALSE;
417    }    }
418  }  }
419    
420  /**  /**
421   *  The drupal_ftp_delete_folder Function   *  The drupal_ftp_delete_folder Function
422   *  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
423     *  files and folders from a directory called $folder_name.
424   */   */
425  function drupal_ftp_delete_folder($folder_name, &$ftp) {  function drupal_ftp_delete_folder($folder_name, &$ftp) {
426    if (!@drupal_ftp_connect($ftp)) {    if (!@drupal_ftp_connect($ftp)) {
427      return false;      return FALSE;
428    }    }
429    
430    @ftp_chdir($ftp->__conn, $folder_name);    @ftp_chdir($ftp->__conn, $folder_name);
# Line 444  function drupal_ftp_delete_folder($folde Line 460  function drupal_ftp_delete_folder($folde
460    
461    for ($j = 0; $j < $dir_counter; $j++) {    for ($j = 0; $j < $dir_counter; $j++) {
462      if ($directories[$j] != "." OR $directories[$j] != "..") {      if ($directories[$j] != "." OR $directories[$j] != "..") {
463        $location = ftp_pwd ($ftp->__conn);        $location = ftp_pwd($ftp->__conn);
464        drupal_ftp_delete_folder($directories[$j], $ftp);        drupal_ftp_delete_folder($directories[$j], $ftp);
465        @ftp_cdup ($ftp->__conn);        @ftp_cdup($ftp->__conn);
466        @ftp_rmdir($ftp->__conn,$directories[$j]);        @ftp_rmdir($ftp->__conn, $directories[$j]);
467      }      }
468    }    }
469    
# Line 458  function drupal_ftp_delete_folder($folde Line 474  function drupal_ftp_delete_folder($folde
474    @ftp_rmdir($ftp->__conn, $folder_name);    @ftp_rmdir($ftp->__conn, $folder_name);
475    
476    // Did the recursive folder/file deletion work?    // Did the recursive folder/file deletion work?
477    return true;    return TRUE;
478    
479    /*    /*
480    if (@$php_errormsg == '')    if (@$php_errormsg == '')
481    {    {
482    return true;    return TRUE;
483    }    }
484    else    else
485    {    {
486    $err = "Couldn't recursive delete folder: " . @$php_errormsg;    $err = "Couldn't recursive delete folder: " . @$php_errormsg;
487    return false;    return FALSE;
488    }    }
489    */    */
490  }  }
491    
492  /**  /**
493   *  The drupal_ftp_file_exists Function   *  The drupal_ftp_file_exists Function
494   *  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 $file_name exists in the directory
495   *  It returns true if the file exists, and false if it doesn�t.   *  called $folder_name on the FTP server.
496     *  It returns TRUE if the file exists, and FALSE if it doesn�t.
497   */   */
498  function drupal_ftp_file_exists($folder_name, $filename, &$ftp) {  function drupal_ftp_file_exists($folder_name, $file_name, &$ftp) {
499    // Does the specified file exist on the remote FTP server?    // Does the specified file exist on the remote FTP server?
500    // Returns NULL on error, true on file exists and false if it doesn't exist    // Returns NULL on error, TRUE on file exists and FALSE if it doesn't exist
501    
502    if (!drupal_ftp_change_directory($folder_name, $ftp)) {    if (!drupal_ftp_change_directory($folder_name, $ftp)) {
503      return NULL;      return NULL;
# Line 488  function drupal_ftp_file_exists($folder_ Line 505  function drupal_ftp_file_exists($folder_
505    
506    // We have changed into the directory, let's get a list    // We have changed into the directory, let's get a list
507    // 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
508    $fileArray = @ftp_nlist($ftp->__conn, $folder_name);    $file_array = @ftp_nlist($ftp->__conn, $folder_name);
509    
510    if (!is_array($fileArray)) {    if (!is_array($file_array)) {
511      drupal_ftp_error(t('Unable to retrieve an ftp directory listing in @folder.', array('@folder' => $folder_name)));      drupal_ftp_error(t('Unable to retrieve an ftp directory listing in @folder.', array('@folder' => $folder_name)));
512      return NULL;      return NULL;
513    }    }
514    
515    $sep = $folder_name[strlen($folder_name)-1] == '/' ? '' : '/';    $sep = $folder_name[strlen($folder_name)-1] == '/' ? '' : '/';
516    return in_array($folder_name . $sep . $filename, $fileArray);    return in_array($folder_name . $sep . $file_name, $file_array);
517  }  }
518    
519  function drupal_ftp_create_filename($basename, $directory, &$ftp) {  function drupal_ftp_create_filename($basename, $directory, &$ftp) {
# Line 521  function drupal_ftp_create_filename($bas Line 538  function drupal_ftp_create_filename($bas
538    return $dest;    return $dest;
539  }  }
540    
541  function drupal_ftp_error($message = NULL, $severity = WATCHDOG_ERROR, $notify = true, $type = 'error') {  function drupal_ftp_error($message = NULL, $severity = WATCHDOG_ERROR, $notify = TRUE, $type = 'error') {
542    static $errors;    static $errors;
543    
544    if (isset($message)) {    if (isset($message)) {
# Line 542  function drupal_ftp_perm() { Line 559  function drupal_ftp_perm() {
559    );    );
560  }  }
561    
562  function drupal_ftp_menu($may_cache) {  function drupal_ftp_menu() {
563    $items = array();    $items = array();
564    if ($may_cache) {  
565      $items[] = array(    $items['ftp/browse'] = array(
566        'path' => 'ftp/browse',      'title' => 'FTP Browse',
567        'title' => t('FTP Browse'),      'page callback' => 'drupal_ftp_browse_page',
568        'callback' => 'drupal_ftp_browse_page',      'type' => MENU_LOCAL_TASK,
569        'type' => MENU_CALLBACK,      'access callback' => 'user_access',
570        'access' => user_access('browse drupal ftp'),      'access arguments' => array('browse drupal ftp'),
571      );    );
572      $items[] = array(    $items['ftp/dl'] = array(
573        'path' => 'ftp/dl',      'title' => 'FTP Download',
574        'title' => t('FTP Download'),      'page callback' => 'drupal_ftp_browse_dl',
575        'callback' => 'drupal_ftp_browse_dl',      'type' => MENU_LOCAL_TASK,
576        'type' => MENU_CALLBACK,      'access callback' => 'user_access',
577        'access' => user_access('browse drupal ftp'),      'access arguments' => array('browse drupal ftp'),
578      );    );
579      $items[] = array(    $items['admin/settings/drupal_ftp'] = array(
580        'path' => 'admin/settings/drupal_ftp',      'title' => 'Drupal FTP configuration',
581        'title' => t('Drupal FTP configuration'),      'description' => 'Configure Drupal FTP information, such as default server, username & password.',
582        'description' => t('Configure Drupal FTP information, such as default server, username & password.'),      'page callback' => 'drupal_get_form',
583        'callback' => 'drupal_get_form',      'page arguments' => array('drupal_ftp_settings'),
584        'callback arguments' => 'drupal_ftp_settings',      'access callback' => 'user_access',
585        'access' => user_access('administer drupal ftp'),      'access arguments' => array('administer drupal ftp'),
586      );    );
587      $items[] = array(    $items['ftp/transfer'] = array(
588        'path' => 'ftp/transfer',      'title' => 'Drupal FTP transfer file',
589        'title' => t('Drupal FTP transfer file'),      'description' => 'Transfer a file through ftp.',
590        'description' => t('Transfer a file through ftp.'),      'page callback' => 'drupal_get_form',
591        'callback' => 'drupal_get_form',      'page arguments' => array('drupal_ftp_transfer_file'),
592        'callback arguments' => 'drupal_ftp_transfer_file',      'access callback' => 'user_access',
593        'access' => user_access('write drupal ftp'),      'access arguments' => array('write drupal ftp'),
594        'type' => MENU_CALLBACK,      'type' => MENU_LOCAL_TASK,
595      );    );
596    }  
597    return $items;    return $items;
598  }  }
599    
600  function drupal_ftp_transfer_file() {  function drupal_ftp_transfer_file() {
601    $args = func_get_args();    $args = func_get_args();
602    $dir = '/' . implode('/', $args);    array_shift($args); // gets rid of the 1st element used by drupal.
603    
604      if (is_array($args)) {
605        $dir = '/'. implode('/', $args);
606      }
607      else {
608        $dir = '/';
609      }
610    
611    $form = array();    $form = array();
612    $form['url'] = array(    $form['url'] = array(
613      '#type' => 'textfield',      '#type' => 'textfield',
614      '#title' => t('URL to transfer'),      '#title' => t('URL to transfer'),
615      '#description' => t('Enter the complete URL, including http://, of the file to transfer to the %dir directory.', array('%dir' => $dir)),      '#description' => t('Enter the complete URL, including http://, of the file to transfer to the %dir directory.', array('%dir' => $dir)),
616      '#required' => true,      '#required' => TRUE,
617    );    );
618    $form['filename'] = array(    $form['filename'] = array(
619      '#type' => 'textfield',      '#type' => 'textfield',
620      '#title' => t('Filename to assign'),      '#title' => t('Filename to assign'),
621      '#description' => t('Please give the filename you wish to assign to this file.'),      '#description' => t('Please give the filename you wish to assign to this file.'),
622      '#required' => true,      '#required' => TRUE,
623    );    );
624    $form['directory'] = array(    $form['directory'] = array(
625      '#type' => 'value',      '#type' => 'value',
# Line 607  function drupal_ftp_transfer_file() { Line 632  function drupal_ftp_transfer_file() {
632    return $form;    return $form;
633  }  }
634    
635  function drupal_ftp_transfer_file_submit($form, $form_values) {  function drupal_ftp_transfer_file_submit($form, $form_state) {
636    $dir = $form_values['directory'];    $dir = $form_state['values']['directory'];
637    $home = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);    $home = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
638    $url = check_url($form_values['url']);    $url = check_url($form_state['values']['url']);
639    $result = drupal_http_request($url);    $result = drupal_http_request($url);
640    
641    if ($result->code == 200) {    if ($result->code == 200) {
642      $data = $result->data;      $data = $result->data;
643      if (drupal_ftp_data_to_ftp($data, check_plain($form_values['filename']), $home . $dir, $ftp)) {      if (drupal_ftp_data_to_ftp($data, check_plain($form_state['values']['filename']), $home . $dir, $ftp)) {
644        drupal_set_message(t('File transfered.'));        drupal_set_message(t('File transfered.'));
645      }      }
646    }    }
647    return 'ftp/browse' . $dir;    return 'ftp/browse'. $dir;
648  }  }
649    
650  function drupal_ftp_browse_dl() {  function drupal_ftp_browse_dl() {
# Line 633  function drupal_ftp_browse_dl() { Line 658  function drupal_ftp_browse_dl() {
658    if ($data) {    if ($data) {
659      drupal_set_title($file);      drupal_set_title($file);
660    }    }
661    $parent = l(t('..'), 'ftp/browse/' . $directory);    $parent = l(t('..'), 'ftp/browse/'. $directory);
662    $output = theme('drupal_ftp_download', $parent, $data);    $output = theme('drupal_ftp_download', $parent, $data);
663    return $output;    return $output;
664  }  }
665    
666  function drupal_ftp_browse_page() {  function drupal_ftp_browse_page() {
667    $args = func_get_args();    $args = func_get_args();
668    $fileList = array();    $file_list = array();
669    
670    $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);
671    
672    $directory = implode('/', $args);    $directory = implode('/', $args);
673    
674    $fileList = drupal_ftp_file_list($home_dir . $directory, $ftp);    $file_list = drupal_ftp_file_list($home_dir . $directory, $ftp);
675    $browser = array();    $browser = array();
676    $links = array();    $links = array();
677    
678    if (!empty($args)) {    if (!empty($args)) {
679      $parents = $args;      $parents = $args;
680      array_pop($parents);      array_pop($parents);
681      $browser[] = l('..', 'ftp/browse' . (!empty($parents) ? '/' . implode('/', $parents) : ''));      $browser[] = l('..', 'ftp/browse'. (!empty($parents) ? '/'. implode('/', $parents) : ''));
682    }    }
683    if (is_array($fileList)) {    if (is_array($file_list)) {
684      for($i = 0; $i < sizeof($fileList); $i++) {      for ($i = 0; $i < sizeof($file_list); $i++) {
685        if($fileList[$i]["type"] == 0) {        if ($file_list[$i]["type"] == 0) {
686          // Folder          // Folder
687          $browser[] = l($fileList[$i]["filename"], 'ftp/browse/' . (!empty($args) ? $directory . '/' : '') . $fileList[$i]["filename"]) . " (directory)";          $browser[] = l($file_list[$i]["filename"], 'ftp/browse/'. (!empty($args) ? $directory .'/' : '') . $file_list[$i]["filename"]) ." (directory)";
688        }        }
689        else {        else {
690          // File          // File
691          $browser[] = l($fileList[$i]["filename"], 'ftp/dl/' . (!empty($args) ? $directory : ''), array(), 'file=' . $fileList[$i]["filename"]) . " (" . $fileList[$i]["filesize"] . " bytes)";          $browser[] = l($file_list[$i]["filename"], 'ftp/dl/'. (!empty($args) ? $directory : ''),
692              array('query' => 'file='. $file_list[$i]["filename"])) ." (" . $file_list[$i]["filesize"] ." bytes)";
693        }        }
694      }      }
695    }    }
# Line 671  function drupal_ftp_browse_page() { Line 697  function drupal_ftp_browse_page() {
697      $browser[] = theme('drupal_ftp_empty_directory_message', $directory);      $browser[] = theme('drupal_ftp_empty_directory_message', $directory);
698    }    }
699    if (user_access('write drupal ftp')) {    if (user_access('write drupal ftp')) {
700      $links[] = l(t('transfer file'), 'ftp/transfer/' . $directory);      $links[] = l(t('transfer file'), 'ftp/transfer/'. $directory);
701    }    }
702    drupal_set_title(t('FTP Browse @directory', array('@directory' => $directory)));    drupal_set_title(t('FTP Browse @directory', array('@directory' => $directory)));
703    $output .= theme('drupal_ftp_browser', $browser, $links);    $output .= theme('drupal_ftp_browser', $browser, $links);
# Line 679  function drupal_ftp_browse_page() { Line 705  function drupal_ftp_browse_page() {
705    return $output;    return $output;
706  }  }
707    
708    function drupal_ftp_theme() {
709      return array(
710        'drupal_ftp_browser' => array(
711          'arguments' => array('browser' => NULL, 'links' => NULL),
712        ),
713        'drupal_ftp_download' => array(
714          'arguments' => array('directory_link' => NULL, 'data' => NULL),
715        ),
716        'drupal_ftp_empty_directory_message' => array(
717          'arguments' => array('directory' => NULL),
718        ),
719      );
720    }
721    
722  function theme_drupal_ftp_empty_directory_message($directory) {  function theme_drupal_ftp_empty_directory_message($directory) {
723    return t('No files in @directory directory.', array('@directory' => $directory));    return t('No files in @directory directory.', array('@directory' => $directory));
724  }  }
725    
726  function theme_drupal_ftp_browser($browser = array(), $links = array()) {  function theme_drupal_ftp_browser($browser = array(), $links = array()) {
727    $output .= '<div class="drupal_ftp_browser">' . "\n";    $output .= '<div class="drupal_ftp_browser">'."\n";
728    $output .= theme('item_list', $browser);    $output .= theme('item_list', $browser);
729    $output .= "</div>\n";    $output .= "</div>\n";
730    if (!empty($links)) {    if (!empty($links)) {
731      $output .= "<div class=\"drupal_ftp_browser_links\">\n";      $output .= "<div class=\"drupal_ftp_browser_links\">\n";
732      $output .= '<strong>' . t('Available actions:') . '</strong><br />';      $output .= '<strong>'. t('Available actions:') .'</strong><br />';
733      $output .= theme('item_list', $links);      $output .= theme('item_list', $links);
734      $output .= "</div>\n\n";      $output .= "</div>\n\n";
735    }    }
# Line 697  function theme_drupal_ftp_browser($brows Line 737  function theme_drupal_ftp_browser($brows
737  }  }
738    
739  function theme_drupal_ftp_download($directory_link, $data) {  function theme_drupal_ftp_download($directory_link, $data) {
740    $output = '<div class="drupal_ftp_parent_link">' . $directory_link . '</div><div class="drupal_ftp_data">' . $data  . '</div>' . "\n\n";    $output = '<div class="drupal_ftp_parent_link">'. $directory_link .'</div><div class="drupal_ftp_data">'. $data  .'</div>'."\n\n";
741    return $output;    return $output;
742  }  }
743    
# Line 730  function drupal_ftp_settings() { Line 770  function drupal_ftp_settings() {
770    return system_settings_form($form);    return system_settings_form($form);
771  }  }
772    
773    
774    

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

  ViewVC Help
Powered by ViewVC 1.1.2