| 1 |
<?php |
<?php |
| 2 |
// $Id:$ |
// $Id: drupal_ftp.module,v 1.2 2007/06/22 21:08:44 aaron Exp $ |
| 3 |
|
|
| 4 |
// This is nearly a direct port from http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/ |
// This is nearly a direct port from http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/ |
| 5 |
// it's been drupalized, however, and there's more work to make it complete. however, looks good so far... |
// it's been drupalized, however, and there's more work to make it complete. however, looks good so far... |
| 6 |
// - winborn 2007-06-22 |
// - winborn 2007-06-22 |
| 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); |
| 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 |
* The _drupal_ftp_connect function |
* The _drupal_ftp_connect function |
| 578 |
drupal_set_message($err, 'error'); |
drupal_set_message($err, 'error'); |
| 579 |
} |
} |
| 580 |
|
|
| 581 |
|
function drupal_ftp_perm() { |
| 582 |
|
return array( |
| 583 |
|
'administer drupal ftp', |
| 584 |
|
'browse drupal ftp', |
| 585 |
|
'write drupal ftp', |
| 586 |
|
); |
| 587 |
|
} |
| 588 |
|
|
| 589 |
function drupal_ftp_menu($may_cache) { |
function drupal_ftp_menu($may_cache) { |
| 590 |
$items = array(); |
$items = array(); |
| 591 |
if ($may_cache) { |
if ($may_cache) { |
| 592 |
$items[] = array( |
$items[] = array( |
| 593 |
'path' => 'ftp/test', |
'path' => 'ftp/browse', |
| 594 |
'title' => t('FTP Test'), |
'title' => t('FTP Browse'), |
| 595 |
'callback' => 'drupal_ftp_test_page', |
'callback' => 'drupal_ftp_browse_page', |
| 596 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 597 |
'access' => user_access('access content'), |
'access' => user_access('browse drupal ftp'), |
| 598 |
); |
); |
| 599 |
$items[] = array( |
$items[] = array( |
| 600 |
'path' => 'ftp/dl', |
'path' => 'ftp/dl', |
| 601 |
'title' => t('FTP Download'), |
'title' => t('FTP Download'), |
| 602 |
'callback' => 'drupal_ftp_test_dl', |
'callback' => 'drupal_ftp_browse_dl', |
| 603 |
|
'type' => MENU_CALLBACK, |
| 604 |
|
'access' => user_access('browse drupal ftp'), |
| 605 |
|
); |
| 606 |
|
$items[] = array( |
| 607 |
|
'path' => 'admin/settings/drupal_ftp', |
| 608 |
|
'title' => t('Drupal FTP configuration'), |
| 609 |
|
'description' => t('Configure Drupal FTP information, such as default server, username & password.'), |
| 610 |
|
'callback' => 'drupal_get_form', |
| 611 |
|
'callback arguments' => 'drupal_ftp_settings', |
| 612 |
|
'access' => user_access('administer drupal ftp'), |
| 613 |
|
); |
| 614 |
|
$items[] = array( |
| 615 |
|
'path' => 'ftp/transfer', |
| 616 |
|
'title' => t('Drupal FTP transfer file'), |
| 617 |
|
'description' => t('Transfer a file through ftp.'), |
| 618 |
|
'callback' => 'drupal_get_form', |
| 619 |
|
'callback arguments' => 'drupal_ftp_transfer_file', |
| 620 |
|
'access' => user_access('write drupal ftp'), |
| 621 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
|
'access' => user_access('access content'), |
|
| 622 |
); |
); |
| 623 |
} |
} |
| 624 |
return $items; |
return $items; |
| 625 |
} |
} |
| 626 |
|
|
| 627 |
function drupal_ftp_test_dl() { |
function drupal_ftp_transfer_file() { |
| 628 |
// _drupal_ftp_download_file($FileName, $Directory, &$Err) |
$args = func_get_args(); |
| 629 |
|
$dir = '/' . implode('/', $args); |
| 630 |
|
$form = array(); |
| 631 |
|
$form['url'] = array( |
| 632 |
|
'#type' => 'textfield', |
| 633 |
|
'#title' => t('URL to transfer'), |
| 634 |
|
'#description' => t('Enter the complete URL, including http://, of the file to transfer to the %dir directory.', array('%dir' => $dir)), |
| 635 |
|
'#required' => true, |
| 636 |
|
); |
| 637 |
|
$form['filename'] = array( |
| 638 |
|
'#type' => 'textfield', |
| 639 |
|
'#title' => t('Filename to assign'), |
| 640 |
|
'#description' => t('Please give the filename you wish to assign to this file.'), |
| 641 |
|
'#required' => true, |
| 642 |
|
); |
| 643 |
|
$form['directory'] = array( |
| 644 |
|
'#type' => 'value', |
| 645 |
|
'#value' => $dir, |
| 646 |
|
); |
| 647 |
|
$form['submit'] = array( |
| 648 |
|
'#type' => 'submit', |
| 649 |
|
'#value' => t('Transfer'), |
| 650 |
|
); |
| 651 |
|
return $form; |
| 652 |
|
} |
| 653 |
|
|
| 654 |
|
function drupal_ftp_transfer_file_submit($form, $form_values) { |
| 655 |
|
$dir = $form_values['directory']; |
| 656 |
|
|
| 657 |
|
$server = variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER); |
| 658 |
|
$username = variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME); |
| 659 |
|
$password = variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD); |
| 660 |
|
$home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY); |
| 661 |
|
|
| 662 |
|
$url = check_url($form_values['url']); |
| 663 |
|
$result = drupal_http_request($url); |
| 664 |
|
|
| 665 |
|
if ($result->code == 200) { |
| 666 |
|
$data = $result->data; |
| 667 |
|
_drupal_ftp_connect($server, $username, $password, $home_dir, $err); |
| 668 |
|
if (!$err) { |
| 669 |
|
_drupal_ftp_upload_file($data, check_plain($form_values['filename']), $home_dir . $dir, $err); |
| 670 |
|
} |
| 671 |
|
if ($err) { |
| 672 |
|
drupal_set_message($err, 'error'); |
| 673 |
|
} |
| 674 |
|
else { |
| 675 |
|
drupal_set_message(t('File transfered.')); |
| 676 |
|
} |
| 677 |
|
} |
| 678 |
|
else { |
| 679 |
|
drupal_set_message(t('Unable to retrieve file.'), 'error'); |
| 680 |
|
} |
| 681 |
|
return 'ftp/browse' . $dir; |
| 682 |
|
} |
| 683 |
|
|
| 684 |
|
function drupal_ftp_browse_dl() { |
| 685 |
$args = func_get_args(); |
$args = func_get_args(); |
| 686 |
$directory = '/' . implode('/', $args); |
$directory = implode('/', $args); |
| 687 |
$err = ''; |
$err = ''; |
| 688 |
|
|
| 689 |
|
$server = variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER); |
| 690 |
|
$username = variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME); |
| 691 |
|
$password = variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD); |
| 692 |
|
$home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY); |
| 693 |
|
|
| 694 |
$file = $_GET['file']; |
$file = $_GET['file']; |
| 695 |
_drupal_ftp_connect("ftp.drupal.org", "anonymous", "someone@somewhere.com", "/", $err); |
_drupal_ftp_connect($server, $username, $password, $home_dir, $err); |
| 696 |
$output = _drupal_ftp_download_file($file, $directory, $err); |
$output = _drupal_ftp_download_file($file, $home_dir . $directory, $err); |
| 697 |
if (!$err) { |
if (!$err) { |
| 698 |
drupal_set_title($file); |
drupal_set_title($file); |
| 699 |
} |
} |
| 703 |
return $output; |
return $output; |
| 704 |
} |
} |
| 705 |
|
|
| 706 |
function drupal_ftp_test_page() { |
function drupal_ftp_browse_page() { |
| 707 |
$args = func_get_args(); |
$args = func_get_args(); |
|
$directory = '/' . implode('/', $args); |
|
|
$err = ""; |
|
| 708 |
$fileList = array(); |
$fileList = array(); |
| 709 |
|
|
| 710 |
_drupal_ftp_connect("ftp.drupal.org", "anonymous", "someone@somewhere.com", "/", $err); |
$server = variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER); |
| 711 |
$fileList = _drupal_ftp_file_list($directory, $err); |
$username = variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME); |
| 712 |
|
$password = variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD); |
| 713 |
|
$home_dir = variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY); |
| 714 |
|
|
| 715 |
|
$directory = implode('/', $args); |
| 716 |
|
$err = ""; |
| 717 |
|
|
| 718 |
|
_drupal_ftp_connect($server, $username, $password, $home_dir, $err); |
| 719 |
|
$fileList = _drupal_ftp_file_list($home_dir . $directory, $err); |
| 720 |
|
$browser = array(); |
| 721 |
|
$links = array(); |
| 722 |
|
|
| 723 |
if($err == "") { |
if($err == '') { |
| 724 |
if (!empty($args)) { |
if (!empty($args)) { |
| 725 |
$parents = $args; |
$parents = $args; |
| 726 |
array_pop($parents); |
array_pop($parents); |
| 727 |
$output .= l('..', 'ftp/test' . (!empty($parents) ? '/' . implode('/', $parents) : '')) . '<br />'; |
$browser[] = l('..', 'ftp/browse' . (!empty($parents) ? '/' . implode('/', $parents) : '')); |
| 728 |
} |
} |
| 729 |
for($i = 0; $i < sizeof($fileList); $i++) { |
for($i = 0; $i < sizeof($fileList); $i++) { |
| 730 |
if($fileList[$i]["type"] == 0) { |
if($fileList[$i]["type"] == 0) { |
| 731 |
// Folder |
// Folder |
| 732 |
$output .= l($fileList[$i]["filename"], 'ftp/test' . (!empty($args) ? $directory : '') . '/' . $fileList[$i]["filename"]) . " (directory)<br>"; |
$browser[] = l($fileList[$i]["filename"], 'ftp/browse/' . (!empty($args) ? $directory . '/' : '') . $fileList[$i]["filename"]) . " (directory)"; |
| 733 |
} |
} |
| 734 |
else { |
else { |
| 735 |
// File |
// File |
| 736 |
$output .= l($fileList[$i]["filename"], 'ftp/dl' . (!empty($args) ? $directory : ''), array(), 'file=' . $fileList[$i]["filename"]) . " (" . $fileList[$i]["filesize"] . " bytes)<br>"; |
$browser[] = l($fileList[$i]["filename"], 'ftp/dl/' . (!empty($args) ? $directory : ''), array(), 'file=' . $fileList[$i]["filename"]) . " (" . $fileList[$i]["filesize"] . " bytes)"; |
| 737 |
} |
} |
| 738 |
} |
} |
| 739 |
|
if (user_access('write drupal ftp')) { |
| 740 |
|
$links[] = array( |
| 741 |
|
'title' => t('transfer file'), |
| 742 |
|
'href' => 'ftp/transfer/' . $directory, |
| 743 |
|
); |
| 744 |
|
} |
| 745 |
|
$output .= theme('drupal_ftp_browser', $browser, $links); |
| 746 |
} |
} |
| 747 |
else { |
else { |
| 748 |
$output .= $err; |
$output .= $err; |
| 749 |
} |
} |
| 750 |
return $output; |
return $output; |
| 751 |
} |
} |
| 752 |
|
|
| 753 |
|
function theme_drupal_ftp_browser($browser = array(), $links = array()) { |
| 754 |
|
$output .= theme('item_list', $browser); |
| 755 |
|
$output .= theme('links', $links); |
| 756 |
|
return $output; |
| 757 |
|
} |
| 758 |
|
|
| 759 |
|
function drupal_ftp_settings() { |
| 760 |
|
$form = array(); |
| 761 |
|
$form['drupal_ftp_default_server'] = array( |
| 762 |
|
'#type' => 'textfield', |
| 763 |
|
'#title' => t('Default FTP Server'), |
| 764 |
|
'#default_value' => variable_get('drupal_ftp_default_server', DRUPAL_FTP_DEFAULT_SERVER), |
| 765 |
|
'#description' => t('This is the default server that may be browsed. Note that other modules using the provided API may override this value.'), |
| 766 |
|
); |
| 767 |
|
$form['drupal_ftp_default_username'] = array( |
| 768 |
|
'#type' => 'textfield', |
| 769 |
|
'#title' => t('Default FTP Username'), |
| 770 |
|
'#default_value' => variable_get('drupal_ftp_default_username', DRUPAL_FTP_DEFAULT_USERNAME), |
| 771 |
|
'#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.'), |
| 772 |
|
); |
| 773 |
|
$form['drupal_ftp_default_password'] = array( |
| 774 |
|
'#type' => 'textfield', |
| 775 |
|
'#title' => t('Default FTP Password'), |
| 776 |
|
'#default_value' => variable_get('drupal_ftp_default_password', DRUPAL_FTP_DEFAULT_PASSWORD), |
| 777 |
|
'#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.'), |
| 778 |
|
); |
| 779 |
|
$form['drupal_ftp_default_home_directory'] = array( |
| 780 |
|
'#type' => 'textfield', |
| 781 |
|
'#title' => t('Default FTP Home Directory'), |
| 782 |
|
'#default_value' => variable_get('drupal_ftp_default_home_directory', DRUPAL_FTP_DEFAULT_HOME_DIRECTORY), |
| 783 |
|
'#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.'), |
| 784 |
|
); |
| 785 |
|
return system_settings_form($form); |
| 786 |
|
} |
| 787 |
|
|