| 1 |
<?php
|
| 2 |
// $Id: drupal_ftp.module,v 1.4 2007/06/28 14:55:49 aaron Exp $
|
| 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.
|
| 6 |
// - winborn 2007-06-22 - 2007-06-28
|
| 7 |
|
| 8 |
define('DRUPAL_FTP_FT_DIRECTORY', 0);
|
| 9 |
define('DRUPAL_FTP_FT_FILE', 1);
|
| 10 |
define('DRUPAL_FTP_DEFAULT_SERVER', 'ftp.drupal.org');
|
| 11 |
define('DRUPAL_FTP_DEFAULT_USERNAME', 'anonymous');
|
| 12 |
define('DRUPAL_FTP_DEFAULT_PASSWORD', 'someone@somewhere.com');
|
| 13 |
define('DRUPAL_FTP_DEFAULT_HOME_DIRECTORY', '/pub/drupal/');
|
| 14 |
|
| 15 |
/**
|
| 16 |
* creates a new ftp object. if any elements of ftp_map are missing, they'll be filled with the server defaults.
|
| 17 |
*/
|
| 18 |
function drupal_ftp_ftp_object($ftp_map = array()) {
|
| 19 |
$ftp = new stdClass();
|
| 20 |
|
| 21 |
$ftp->__server = $ftp_map['#server'] ? $ftp_map['#server'] : variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER);
|
| 22 |
$ftp->__user = $ftp_map['#ftp_user'] ? $ftp_map['#ftp_user'] : variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME);
|
| 23 |
$ftp->__password = $ftp_map['#password'] ? $ftp_map['#password'] : variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD);
|
| 24 |
$ftp->__directory = $ftp_map['#directory'] ? $ftp_map['#directory'] : variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
|
| 25 |
|
| 26 |
return $ftp;
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* The drupal_ftp_connect function
|
| 31 |
* 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 |
if (!$ftp->__conn && !drupal_ftp_connected($ftp)) {
|
| 40 |
// Attempt to connect to the remote server
|
| 41 |
$ftp->__conn = @ftp_connect($ftp->__server);
|
| 42 |
|
| 43 |
if (!$ftp->__conn) {
|
| 44 |
drupal_ftp_error(t('Couldn\'t connect to server @server', array('@server' => $ftp->__server)));
|
| 45 |
return false;
|
| 46 |
}
|
| 47 |
|
| 48 |
// Attempt to login to the remote server
|
| 49 |
$ftp->__login = @ftp_login($ftp->__conn, $ftp->__user, $ftp->__password);
|
| 50 |
|
| 51 |
if (!$ftp->__login) {
|
| 52 |
drupal_ftp_error(t('Couldn\'t login as user @ftp_user to @server', array('@ftp_user' => $ftp->__user, '@server' => $ftp->__server)));
|
| 53 |
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
|
| 66 |
return true;
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* The drupal_ftp_connected function
|
| 71 |
* This function queries the FTP server with the ftp_systype command to check if the connection is still alive.
|
| 72 |
* It returns true on success or false on disconnection.
|
| 73 |
*/
|
| 74 |
|
| 75 |
function drupal_ftp_connected(&$ftp) {
|
| 76 |
// Attempt to call the ftp_systype to see if the connect
|
| 77 |
// to the FTP server is still alive and kicking
|
| 78 |
|
| 79 |
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
|
| 86 |
return false;
|
| 87 |
}
|
| 88 |
else {
|
| 89 |
// The connection is still alive
|
| 90 |
return true;
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* The drupal_ftp_ftp_to_data Function
|
| 96 |
* 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.
|
| 98 |
* The file is saved locally and its contents are returned to the caller of the function.
|
| 99 |
*/
|
| 100 |
function drupal_ftp_ftp_to_data($filename, $directory, &$ftp) {
|
| 101 |
// Change into the remote directory and retrieve the content
|
| 102 |
// of a file. Once retrieve, return this value to the caller
|
| 103 |
|
| 104 |
if (!@drupal_ftp_connect($ftp)){
|
| 105 |
return false;
|
| 106 |
}
|
| 107 |
|
| 108 |
// We are now connected, so let's retrieve the file contents.
|
| 109 |
// Firstly, we change into the directory
|
| 110 |
$chDir = @ftp_chdir($ftp->__conn, $directory);
|
| 111 |
|
| 112 |
if (!$chDir) {
|
| 113 |
drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory' => $directory)));
|
| 114 |
return false;
|
| 115 |
}
|
| 116 |
|
| 117 |
// We have changed into the directory, let's attempt to get the file
|
| 118 |
$temp_file = file_create_filename($filename, file_directory_temp());
|
| 119 |
$fp = @fopen($temp_file, 'wb');
|
| 120 |
$getFile = @ftp_fget($ftp->__conn, $fp, $filename, FTP_BINARY);
|
| 121 |
fclose($fp);
|
| 122 |
|
| 123 |
$fp = null;
|
| 124 |
|
| 125 |
if (!$getFile) {
|
| 126 |
drupal_ftp_error(t('Unable to download file: @filename from @directory', array('@filename' => $filename, '@directory' => $directory)));
|
| 127 |
return false;
|
| 128 |
}
|
| 129 |
|
| 130 |
// The file was downloaded successfully. Let's open it, read in its
|
| 131 |
// contents and return it to the calling function
|
| 132 |
|
| 133 |
$fp = @fopen($temp_file, 'rb');
|
| 134 |
|
| 135 |
if (!$fp) {
|
| 136 |
drupal_ftp_error(t('Unable to open @filename after it was downloaded from {@server}', array('@filename' => $filename, '@server' => $_DRUPAL_FTP->__server)));
|
| 137 |
return false;
|
| 138 |
}
|
| 139 |
|
| 140 |
// Read in the contents of the file to a variable
|
| 141 |
$data = '';
|
| 142 |
|
| 143 |
while(!feof($fp)) {
|
| 144 |
$data.= fread($fp, 4096);
|
| 145 |
}
|
| 146 |
|
| 147 |
@fclose($fp);
|
| 148 |
|
| 149 |
// Return the HTML from the file
|
| 150 |
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_data_to_ftp function
|
| 178 |
* 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.
|
| 180 |
*/
|
| 181 |
function drupal_ftp_data_to_ftp($data, $filename, $directory, &$ftp) {
|
| 182 |
// Save HTML to a remote file on the FTP server
|
| 183 |
|
| 184 |
if (!@drupal_ftp_connect($ftp)) {
|
| 185 |
return false;
|
| 186 |
}
|
| 187 |
|
| 188 |
// We are now connected, so let's retrieve the file contents.
|
| 189 |
// Firstly, we change into the directory
|
| 190 |
$chDir = @ftp_chdir($ftp->__conn, $directory);
|
| 191 |
|
| 192 |
if (!$chDir) {
|
| 193 |
drupal_ftp_error(t('Couldn\'t change into directory: @directory.', array('@directory' => $directory)));
|
| 194 |
return false;
|
| 195 |
}
|
| 196 |
|
| 197 |
$local_filename = file_create_filename($filename, file_directory_temp());
|
| 198 |
// Save the HTML to a file which we then upload
|
| 199 |
$fp = @fopen($local_filename, 'wb');
|
| 200 |
|
| 201 |
if (!$fp) {
|
| 202 |
drupal_ftp_error(t('Couldn\'t open a local file for temporary output.'));
|
| 203 |
return false;
|
| 204 |
}
|
| 205 |
|
| 206 |
// The file was opened OK, let's write to it
|
| 207 |
$filePut = @fputs($fp, $data, strlen($data));
|
| 208 |
|
| 209 |
if (!$filePut) {
|
| 210 |
drupal_ftp_error(t('Couldn\'t write to a local file for temporary output.'));
|
| 211 |
return false;
|
| 212 |
}
|
| 213 |
else {
|
| 214 |
@fclose($fp);
|
| 215 |
}
|
| 216 |
|
| 217 |
// Now we can try to write to the remote file
|
| 218 |
$complete_filename = $directory . '/' . $filename;
|
| 219 |
|
| 220 |
$putFile = @ftp_put($ftp->__conn, $complete_filename, $local_filename, FTP_BINARY);
|
| 221 |
|
| 222 |
if (!$putFile) {
|
| 223 |
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;
|
| 225 |
}
|
| 226 |
|
| 227 |
// Everything worked OK
|
| 228 |
return true;
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* The drupal_ftp_change_directory Function
|
| 233 |
* 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.
|
| 235 |
*/
|
| 236 |
function drupal_ftp_change_directory($directory, &$ftp) {
|
| 237 |
// Switch to another directory on the web server. If we don't
|
| 238 |
// have permissions then an error will occur
|
| 239 |
|
| 240 |
if (!@drupal_ftp_connect($ftp)) {
|
| 241 |
return false;
|
| 242 |
}
|
| 243 |
|
| 244 |
// Try and change into another directory
|
| 245 |
$chDir = ftp_chdir($ftp->__conn, $directory);
|
| 246 |
|
| 247 |
if (!$chDir) {
|
| 248 |
drupal_ftp_error(t('Couldn\'t change into directory: @directory', array('@directory', $directory)));
|
| 249 |
return false;
|
| 250 |
}
|
| 251 |
else {
|
| 252 |
// Changing directories worked OK
|
| 253 |
return true;
|
| 254 |
}
|
| 255 |
}
|
| 256 |
|
| 257 |
/**
|
| 258 |
* The drupal_ftp_file_list Function
|
| 259 |
* 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.
|
| 261 |
*/
|
| 262 |
function drupal_ftp_file_list($directory, &$ftp) {
|
| 263 |
// This function will attempt to change into the specified
|
| 264 |
// directory and retrieve a list of files as an associative
|
| 265 |
// array. This list will include file name, size and date last modified
|
| 266 |
|
| 267 |
$fileArray = array();
|
| 268 |
|
| 269 |
// Can we switch to the desired directory?
|
| 270 |
if (!drupal_ftp_change_directory($directory, $ftp)) {
|
| 271 |
return false;
|
| 272 |
}
|
| 273 |
|
| 274 |
// We are in the directory, let's retrieve a list of files
|
| 275 |
$fileList = ftp_rawlist($ftp->__conn, $directory);
|
| 276 |
|
| 277 |
// Save the list of files
|
| 278 |
if (@is_array($fileList)) {
|
| 279 |
// Interate through the array
|
| 280 |
for($i = 0; $i < sizeof($fileList); $i++) {
|
| 281 |
$itemArray = explode(' ', $fileList[$i]);
|
| 282 |
|
| 283 |
// Are we dealing with a file or directory?
|
| 284 |
// If the first letter of the attributes is
|
| 285 |
// 'd' then we are dealing with a directory
|
| 286 |
$attributes = $itemArray[0];
|
| 287 |
|
| 288 |
if (substr($attributes, 0, 1) == 'd') {
|
| 289 |
$fileType = DRUPAL_FTP_FT_DIRECTORY;
|
| 290 |
}
|
| 291 |
else {
|
| 292 |
$fileType = DRUPAL_FTP_FT_FILE;
|
| 293 |
}
|
| 294 |
|
| 295 |
// Get the file/directory name
|
| 296 |
$fileName = $itemArray[sizeof($itemArray)-1];
|
| 297 |
|
| 298 |
// Get the size of the file
|
| 299 |
$fileSize = $itemArray[sizeof($itemArray)-5];
|
| 300 |
|
| 301 |
if (!is_numeric($fileSize)) {
|
| 302 |
$fileSize = $itemArray[sizeof($itemArray)-6];
|
| 303 |
}
|
| 304 |
|
| 305 |
// Get the date last modified
|
| 306 |
$fileTimeStamp = $itemArray[sizeof($itemArray)-4] . ' ' . $itemArray[sizeof($itemArray)-3] . ' ' . $itemArray[sizeof($itemArray)-2];
|
| 307 |
|
| 308 |
$fileArray[] = array(
|
| 309 |
'type' => $fileType,
|
| 310 |
'filename' => $fileName,
|
| 311 |
'filesize' => $fileSize,
|
| 312 |
'filetime' => $fileTimeStamp
|
| 313 |
);
|
| 314 |
}
|
| 315 |
}
|
| 316 |
sort($fileArray);
|
| 317 |
return $fileArray;
|
| 318 |
}
|
| 319 |
|
| 320 |
/**
|
| 321 |
* The drupal_ftp_create_directory Function
|
| 322 |
* 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.
|
| 324 |
*/
|
| 325 |
function drupal_ftp_create_directory($folderName, &$ftp) {
|
| 326 |
// Makes a new folder on the web server via FTP
|
| 327 |
|
| 328 |
if (!@drupal_ftp_connect($ftp)) {
|
| 329 |
return false;
|
| 330 |
}
|
| 331 |
|
| 332 |
$createResult = @ftp_mkdir($ftp->__conn, $folderName);
|
| 333 |
|
| 334 |
if ($createResult == true) {
|
| 335 |
// Can we change the files permissions?
|
| 336 |
$execResult = @ftp_site($ftp->__conn, 'chmod 0777 ' . $folderName . '/');
|
| 337 |
|
| 338 |
if ($execResult == true) {
|
| 339 |
return true;
|
| 340 |
}
|
| 341 |
else {
|
| 342 |
drupal_ftp_error(t('Couldn\'t set owner permissions on @folder.', array('@folder', $folderName)));
|
| 343 |
return false;
|
| 344 |
}
|
| 345 |
}
|
| 346 |
else {
|
| 347 |
drupal_ftp_error(t('Couldn\'t create new folder @folder.', array('@folder', $folderName)));
|
| 348 |
return false;
|
| 349 |
}
|
| 350 |
}
|
| 351 |
|
| 352 |
/**
|
| 353 |
* The drupal_ftp_rename_file Function
|
| 354 |
* This function attempts to rename a file on the FTP server from $old_filename to $new_filename.
|
| 355 |
*/
|
| 356 |
function drupal_ftp_rename_file($old_filename, $new_filename, $is_folder, &$ftp) {
|
| 357 |
// Rename a file/directory on the FTP server
|
| 358 |
|
| 359 |
if (!@drupal_ftp_connect($ftp)) {
|
| 360 |
return false;
|
| 361 |
}
|
| 362 |
|
| 363 |
$rename_result = @ftp_rename($ftp->__conn, $old_filename, $new_filename);
|
| 364 |
|
| 365 |
if ($rename_result == true) {
|
| 366 |
// The file/folder was renamed successfully
|
| 367 |
return true;
|
| 368 |
}
|
| 369 |
else {
|
| 370 |
// Couldn't rename the file/folder
|
| 371 |
if (!$is_folder) {
|
| 372 |
drupal_ftp_error(t('Couldn\'t rename the selected folder: @filename', array('@filename' => $old_filename)));
|
| 373 |
}
|
| 374 |
else {
|
| 375 |
drupal_ftp_error(t('Couldn\'t rename the selected file: @filename', array('@filename' => $old_filename)));
|
| 376 |
}
|
| 377 |
|
| 378 |
return false;
|
| 379 |
}
|
| 380 |
}
|
| 381 |
|
| 382 |
/**
|
| 383 |
* The drupal_ftp_delete_file Function
|
| 384 |
* This function attempts to delete a file called $filename from the FTP server.
|
| 385 |
*/
|
| 386 |
function drupal_ftp_delete_file($filename, &$ftp) {
|
| 387 |
// Remove the specified file from the FTP server
|
| 388 |
if (!@drupal_ftp_connect($ftp)) {
|
| 389 |
return false;
|
| 390 |
}
|
| 391 |
|
| 392 |
$deleteResult = @ftp_delete($ftp->__conn, $filename);
|
| 393 |
|
| 394 |
if ($deleteResult == true) {
|
| 395 |
// The file/folder was renamed successfully
|
| 396 |
return true;
|
| 397 |
}
|
| 398 |
else {
|
| 399 |
// Couldn't delete the selected file
|
| 400 |
drupal_ftp_error(t('Couldn\'t delete the selected file: @filename', array('@filename' => $filename)));
|
| 401 |
return false;
|
| 402 |
}
|
| 403 |
}
|
| 404 |
|
| 405 |
/**
|
| 406 |
* 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.
|
| 408 |
*/
|
| 409 |
function drupal_ftp_delete_folder($folder_name, &$ftp) {
|
| 410 |
if (!@drupal_ftp_connect($ftp)) {
|
| 411 |
return false;
|
| 412 |
}
|
| 413 |
|
| 414 |
@ftp_chdir($ftp->__conn, $folder_name);
|
| 415 |
$location = @ftp_pwd($ftp->__conn);
|
| 416 |
|
| 417 |
$directories = array();
|
| 418 |
$files = array();
|
| 419 |
$dir_counter = 0;
|
| 420 |
$file_counter = 0;
|
| 421 |
$content = @ftp_nlist($ftp->__conn, ".");
|
| 422 |
|
| 423 |
for ($i = 0; $i < sizeof($content); $i++) {
|
| 424 |
// If we can change into this then it's a directory.
|
| 425 |
// If not, it's a file
|
| 426 |
if ($content[$i] != "." && $content[$i] != "..") {
|
| 427 |
if (@ftp_chdir($ftp->__conn, $content[$i])) {
|
| 428 |
// We have a directory
|
| 429 |
$directories[] = $content[$i];
|
| 430 |
$dir_counter++;
|
| 431 |
@ftp_cdup($ftp->__conn);
|
| 432 |
}
|
| 433 |
else {
|
| 434 |
// We have a file
|
| 435 |
$files[] = $content[$i];
|
| 436 |
$file_counter++;
|
| 437 |
}
|
| 438 |
}
|
| 439 |
}
|
| 440 |
|
| 441 |
for ($j = 0; $j < $file_counter; $j++) {
|
| 442 |
@ftp_delete($ftp->__conn, $files[$j]);
|
| 443 |
}
|
| 444 |
|
| 445 |
for ($j = 0; $j < $dir_counter; $j++) {
|
| 446 |
if ($directories[$j] != "." OR $directories[$j] != "..") {
|
| 447 |
$location = ftp_pwd ($ftp->__conn);
|
| 448 |
drupal_ftp_delete_folder($directories[$j], $ftp);
|
| 449 |
@ftp_cdup ($ftp->__conn);
|
| 450 |
@ftp_rmdir($ftp->__conn,$directories[$j]);
|
| 451 |
}
|
| 452 |
}
|
| 453 |
|
| 454 |
// Lastly, we change into the directory that we want to delete and see
|
| 455 |
// if we can cdup. If we can, we delete it.
|
| 456 |
@ftp_chdir($ftp->__conn, $folder_name);
|
| 457 |
@ftp_cdup($ftp->__conn);
|
| 458 |
@ftp_rmdir($ftp->__conn, $folder_name);
|
| 459 |
|
| 460 |
// Did the recursive folder/file deletion work?
|
| 461 |
return true;
|
| 462 |
|
| 463 |
/*
|
| 464 |
if (@$php_errormsg == '')
|
| 465 |
{
|
| 466 |
return true;
|
| 467 |
}
|
| 468 |
else
|
| 469 |
{
|
| 470 |
$err = "Couldn't recursive delete folder: " . @$php_errormsg;
|
| 471 |
return false;
|
| 472 |
}
|
| 473 |
*/
|
| 474 |
}
|
| 475 |
|
| 476 |
/**
|
| 477 |
* The drupal_ftp_file_exists Function
|
| 478 |
* 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.
|
| 480 |
*/
|
| 481 |
function drupal_ftp_file_exists($folder_name, $filename, &$ftp) {
|
| 482 |
// Does the specified file exist on the remote FTP server?
|
| 483 |
// Returns NULL on error, true on file exists and false if it doesn't exist
|
| 484 |
|
| 485 |
if (!drupal_ftp_change_directory($folder_name, $ftp)) {
|
| 486 |
return NULL;
|
| 487 |
}
|
| 488 |
|
| 489 |
// We have changed into the directory, let's get a list
|
| 490 |
// of files using ftp_nlist and compare it to see if it exists
|
| 491 |
$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 |
}
|
| 497 |
|
| 498 |
$sep = $folder_name[strlen($folder_name)-1] == '/' ? '' : '/';
|
| 499 |
return in_array($folder_name . $sep . $filename, $fileArray);
|
| 500 |
}
|
| 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 {
|
| 512 |
$name = $basename;
|
| 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, $type = 'error') {
|
| 525 |
static $errors;
|
| 526 |
|
| 527 |
if (isset($message)) {
|
| 528 |
$errors[] = $message;
|
| 529 |
watchdog('drupal_ftp', $message, $severity);
|
| 530 |
if ($notify) {
|
| 531 |
drupal_set_message($message, $type);
|
| 532 |
}
|
| 533 |
}
|
| 534 |
return $errors;
|
| 535 |
}
|
| 536 |
|
| 537 |
function drupal_ftp_perm() {
|
| 538 |
return array(
|
| 539 |
'administer drupal ftp',
|
| 540 |
'browse drupal ftp',
|
| 541 |
'write drupal ftp',
|
| 542 |
);
|
| 543 |
}
|
| 544 |
|
| 545 |
function drupal_ftp_menu($may_cache) {
|
| 546 |
$items = array();
|
| 547 |
if ($may_cache) {
|
| 548 |
$items[] = array(
|
| 549 |
'path' => 'ftp/browse',
|
| 550 |
'title' => t('FTP Browse'),
|
| 551 |
'callback' => 'drupal_ftp_browse_page',
|
| 552 |
'type' => MENU_CALLBACK,
|
| 553 |
'access' => user_access('browse drupal ftp'),
|
| 554 |
);
|
| 555 |
$items[] = array(
|
| 556 |
'path' => 'ftp/dl',
|
| 557 |
'title' => t('FTP Download'),
|
| 558 |
'callback' => 'drupal_ftp_browse_dl',
|
| 559 |
'type' => MENU_CALLBACK,
|
| 560 |
'access' => user_access('browse drupal ftp'),
|
| 561 |
);
|
| 562 |
$items[] = array(
|
| 563 |
'path' => 'admin/settings/drupal_ftp',
|
| 564 |
'title' => t('Drupal FTP configuration'),
|
| 565 |
'description' => t('Configure Drupal FTP information, such as default server, username & password.'),
|
| 566 |
'callback' => 'drupal_get_form',
|
| 567 |
'callback arguments' => 'drupal_ftp_settings',
|
| 568 |
'access' => user_access('administer drupal ftp'),
|
| 569 |
);
|
| 570 |
$items[] = array(
|
| 571 |
'path' => 'ftp/transfer',
|
| 572 |
'title' => t('Drupal FTP transfer file'),
|
| 573 |
'description' => t('Transfer a file through ftp.'),
|
| 574 |
'callback' => 'drupal_get_form',
|
| 575 |
'callback arguments' => 'drupal_ftp_transfer_file',
|
| 576 |
'access' => user_access('write drupal ftp'),
|
| 577 |
'type' => MENU_CALLBACK,
|
| 578 |
);
|
| 579 |
}
|
| 580 |
return $items;
|
| 581 |
}
|
| 582 |
|
| 583 |
function drupal_ftp_transfer_file() {
|
| 584 |
$args = func_get_args();
|
| 585 |
$dir = '/' . implode('/', $args);
|
| 586 |
$form = array();
|
| 587 |
$form['url'] = array(
|
| 588 |
'#type' => 'textfield',
|
| 589 |
'#title' => t('URL to transfer'),
|
| 590 |
'#description' => t('Enter the complete URL, including http://, of the file to transfer to the %dir directory.', array('%dir' => $dir)),
|
| 591 |
'#required' => true,
|
| 592 |
);
|
| 593 |
$form['filename'] = array(
|
| 594 |
'#type' => 'textfield',
|
| 595 |
'#title' => t('Filename to assign'),
|
| 596 |
'#description' => t('Please give the filename you wish to assign to this file.'),
|
| 597 |
'#required' => true,
|
| 598 |
);
|
| 599 |
$form['directory'] = array(
|
| 600 |
'#type' => 'value',
|
| 601 |
'#value' => $dir,
|
| 602 |
);
|
| 603 |
$form['submit'] = array(
|
| 604 |
'#type' => 'submit',
|
| 605 |
'#value' => t('Transfer'),
|
| 606 |
);
|
| 607 |
return $form;
|
| 608 |
}
|
| 609 |
|
| 610 |
function drupal_ftp_transfer_file_submit($form, $form_values) {
|
| 611 |
$dir = $form_values['directory'];
|
| 612 |
$home = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
|
| 613 |
$url = check_url($form_values['url']);
|
| 614 |
$result = drupal_http_request($url);
|
| 615 |
|
| 616 |
if ($result->code == 200) {
|
| 617 |
$data = $result->data;
|
| 618 |
if (drupal_ftp_data_to_ftp($data, check_plain($form_values['filename']), $home . $dir, $ftp)) {
|
| 619 |
drupal_set_message(t('File transfered.'));
|
| 620 |
}
|
| 621 |
}
|
| 622 |
return 'ftp/browse' . $dir;
|
| 623 |
}
|
| 624 |
|
| 625 |
function drupal_ftp_browse_dl() {
|
| 626 |
$args = func_get_args();
|
| 627 |
$directory = implode('/', $args);
|
| 628 |
|
| 629 |
$home = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
|
| 630 |
|
| 631 |
$file = $_GET['file'];
|
| 632 |
$data = drupal_ftp_ftp_to_data($file, $home . $directory, $ftp);
|
| 633 |
if ($data) {
|
| 634 |
drupal_set_title($file);
|
| 635 |
}
|
| 636 |
$parent = l(t('..'), 'ftp/browse/' . $directory);
|
| 637 |
$output = theme('drupal_ftp_download', $parent, $data);
|
| 638 |
return $output;
|
| 639 |
}
|
| 640 |
|
| 641 |
function drupal_ftp_browse_page() {
|
| 642 |
$args = func_get_args();
|
| 643 |
$fileList = array();
|
| 644 |
|
| 645 |
$home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY);
|
| 646 |
|
| 647 |
$directory = implode('/', $args);
|
| 648 |
|
| 649 |
$fileList = drupal_ftp_file_list($home_dir . $directory, $ftp);
|
| 650 |
$browser = array();
|
| 651 |
$links = array();
|
| 652 |
|
| 653 |
if (!empty($args)) {
|
| 654 |
$parents = $args;
|
| 655 |
array_pop($parents);
|
| 656 |
$browser[] = l('..', 'ftp/browse' . (!empty($parents) ? '/' . implode('/', $parents) : ''));
|
| 657 |
}
|
| 658 |
if (is_array($fileList)) {
|
| 659 |
for($i = 0; $i < sizeof($fileList); $i++) {
|
| 660 |
if($fileList[$i]["type"] == 0) {
|
| 661 |
// Folder
|
| 662 |
$browser[] = l($fileList[$i]["filename"], 'ftp/browse/' . (!empty($args) ? $directory . '/' : '') . $fileList[$i]["filename"]) . " (directory)";
|
| 663 |
}
|
| 664 |
else {
|
| 665 |
// File
|
| 666 |
$browser[] = l($fileList[$i]["filename"], 'ftp/dl/' . (!empty($args) ? $directory : ''), array(), 'file=' . $fileList[$i]["filename"]) . " (" . $fileList[$i]["filesize"] . " bytes)";
|
| 667 |
}
|
| 668 |
}
|
| 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;
|
| 680 |
}
|
| 681 |
|
| 682 |
function theme_drupal_ftp_empty_directory_message($directory) {
|
| 683 |
return t('No files in @directory directory.', array('@directory' => $directory));
|
| 684 |
}
|
| 685 |
|
| 686 |
function theme_drupal_ftp_browser($browser = array(), $links = array()) {
|
| 687 |
$output .= '<div class="drupal_ftp_browser">' . "\n";
|
| 688 |
$output .= theme('item_list', $browser);
|
| 689 |
$output .= "</div>\n";
|
| 690 |
if (!empty($links)) {
|
| 691 |
$output .= "<div class=\"drupal_ftp_browser_links\">\n";
|
| 692 |
$output .= '<strong>' . t('Available actions:') . '</strong><br />';
|
| 693 |
$output .= theme('item_list', $links);
|
| 694 |
$output .= "</div>\n\n";
|
| 695 |
}
|
| 696 |
return $output;
|
| 697 |
}
|
| 698 |
|
| 699 |
function theme_drupal_ftp_download($directory_link, $data) {
|
| 700 |
$output = '<div class="drupal_ftp_parent_link">' . $directory_link . '</div><div class="drupal_ftp_data">' . $data . '</div>' . "\n\n";
|
| 701 |
return $output;
|
| 702 |
}
|
| 703 |
|
| 704 |
function drupal_ftp_settings() {
|
| 705 |
$form = array();
|
| 706 |
$form['drupal_ftp_default_server'] = array(
|
| 707 |
'#type' => 'textfield',
|
| 708 |
'#title' => t('Default FTP Server'),
|
| 709 |
'#default_value' => variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER),
|
| 710 |
'#description' => t('This is the default server that may be browsed. Note that other modules using the provided API may override this value.'),
|
| 711 |
);
|
| 712 |
$form['drupal_ftp_default_username'] = array(
|
| 713 |
'#type' => 'textfield',
|
| 714 |
'#title' => t('Default FTP Username'),
|
| 715 |
'#default_value' => variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME),
|
| 716 |
'#description' => t('This is the default username that will be used when browsing. Note that other modules using the provided API may override this value.'),
|
| 717 |
);
|
| 718 |
$form['drupal_ftp_default_password'] = array(
|
| 719 |
'#type' => 'textfield',
|
| 720 |
'#title' => t('Default FTP Password'),
|
| 721 |
'#default_value' => variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD),
|
| 722 |
'#description' => t('This is the default password that will be used when browsing. Note that other modules using the provided API may override this value.'),
|
| 723 |
);
|
| 724 |
$form['drupal_ftp_default_home_directory'] = array(
|
| 725 |
'#type' => 'textfield',
|
| 726 |
'#title' => t('Default FTP Home Directory'),
|
| 727 |
'#default_value' => variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY),
|
| 728 |
'#description' => t('This is the default directory that will be used as the home directory when browsing. Make sure that the directory begins and ends with a forward slash, such as \'/www/\', \'/home/public_ftp/\', or \'/\'. Note that other modules using the provided API may override this value.'),
|
| 729 |
);
|
| 730 |
return system_settings_form($form);
|
| 731 |
}
|
| 732 |
|