| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* The FTP Backend for the plugin manager.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of user hook plugin_manager_backend. Checks
|
| 11 |
* if the requirements for the ftp backend are enabled.
|
| 12 |
*
|
| 13 |
* @return
|
| 14 |
* Return 'ftp' if the requirements are available.
|
| 15 |
*/
|
| 16 |
function ftp_plugin_manager_backend() {
|
| 17 |
if (function_exists('ftp_connect') OR ini_get('allow_url_fopen')) {
|
| 18 |
return 'ftp';
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Install the supplied files to the appropriate locations.
|
| 24 |
*
|
| 25 |
* @param $files
|
| 26 |
* The files to install.
|
| 27 |
* @param $type
|
| 28 |
* Either 'module' or 'theme' to indicate where to install to.
|
| 29 |
* @param $username
|
| 30 |
* Local FTP Username
|
| 31 |
* @param $password
|
| 32 |
* Local FTP Password
|
| 33 |
* @return
|
| 34 |
* TRUE if all the files were installed, FALSE otherwise.
|
| 35 |
*/
|
| 36 |
function ftp_plugin_manager_copy($files, $type, $username, $password) {
|
| 37 |
if (function_exists('ftp_connect')) {
|
| 38 |
return ftp_plugin_manager_copy_library($files, $type, $username, $password);
|
| 39 |
}
|
| 40 |
return ftp_plugin_manager_copy_wrapper($files, $type, $username, $password);
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Install the supplied files to the appropriate locations using ftp without
|
| 45 |
* the stream wrapper.
|
| 46 |
*
|
| 47 |
* @param $files
|
| 48 |
* The files to install.
|
| 49 |
* @param $type
|
| 50 |
* Either 'module' or 'theme' to indicate where to install to.
|
| 51 |
* @param $username
|
| 52 |
* Local FTP Username
|
| 53 |
* @param $password
|
| 54 |
* Local FTP Password
|
| 55 |
* @return
|
| 56 |
* TRUE if all the files were installed, FALSE otherwise.
|
| 57 |
*/
|
| 58 |
function ftp_plugin_manager_copy_library($files, $type, $username, $password) {
|
| 59 |
// Figure out the type of files.
|
| 60 |
if (!in_array($type, array('modules', 'themes'))) {
|
| 61 |
return FALSE;
|
| 62 |
}
|
| 63 |
$dir = 'sites/all/'. drupal_strtolower($type);
|
| 64 |
|
| 65 |
// Connect to the local ftp server.
|
| 66 |
$connect = ftp_connect('localhost');
|
| 67 |
if (!$connect) {
|
| 68 |
drupal_set_message(t('No ftp server could be found.'), 'error');
|
| 69 |
return FALSE;
|
| 70 |
}
|
| 71 |
|
| 72 |
// Login to the local ftp server.
|
| 73 |
if (!@ftp_login($connect, $username, $password)) {
|
| 74 |
drupal_set_message(t('Could not login to the ftp server.'), 'error');
|
| 75 |
return FALSE;
|
| 76 |
}
|
| 77 |
|
| 78 |
// Try to guess the ftp address for the drupal install.
|
| 79 |
// Start by putting the entire address together and replacing \ with /.
|
| 80 |
$local_path = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT'] . base_path());
|
| 81 |
|
| 82 |
// Grab the last on off the end and explode it.
|
| 83 |
$local_path = explode('/', rtrim($local_path, '/'));
|
| 84 |
|
| 85 |
// Try to guess which how far in we are chrooted...
|
| 86 |
foreach ($local_path AS $index => $value) {
|
| 87 |
unset($local_path[$index]);
|
| 88 |
if (ftp_nlist($connect, implode('/', $local_path) .'/'. $dir)) {
|
| 89 |
$ftp_path = implode('/', $local_path) .'/'. $dir;
|
| 90 |
break;
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
// If we couldn't guess it, then quit.
|
| 95 |
if (!isset($ftp_path)) {
|
| 96 |
drupal_set_message(t('Could not guess the ftp directory for drupal.'), 'error');
|
| 97 |
return FALSE;
|
| 98 |
}
|
| 99 |
|
| 100 |
// Prepare the directories to use later.
|
| 101 |
$extract_dir = file_directory_path() .'/plugin_manager_extraction/';
|
| 102 |
|
| 103 |
// Process each of the files.
|
| 104 |
foreach ($files AS $index => $file) {
|
| 105 |
if (trim($file, "\\/") != $file) {
|
| 106 |
@ftp_mkdir($connect, "$ftp_path/$file");
|
| 107 |
}
|
| 108 |
else {
|
| 109 |
@ftp_put($connect, "$ftp_path/$file", $extract_dir . $file, FTP_BINARY);
|
| 110 |
}
|
| 111 |
}
|
| 112 |
return TRUE;
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Install the supplied files to the appropriate locations using the ftp
|
| 117 |
* stream wrapper.
|
| 118 |
*
|
| 119 |
* @param $files
|
| 120 |
* The files to install.
|
| 121 |
* @param $type
|
| 122 |
* Either 'module' or 'theme' to indicate where to install to.
|
| 123 |
* @param $username
|
| 124 |
* Local FTP Username
|
| 125 |
* @param $password
|
| 126 |
* Local FTP Password
|
| 127 |
* @return
|
| 128 |
* TRUE if all the files were installed, FALSE otherwise.
|
| 129 |
*/
|
| 130 |
function ftp_plugin_manager_copy_wrapper($files, $type, $username, $password) {
|
| 131 |
// Figure out the type of files.
|
| 132 |
if (!in_array($type, array('modules', 'themes'))) {
|
| 133 |
return FALSE;
|
| 134 |
}
|
| 135 |
$dir = 'sites/all/'. drupal_strtolower($type);
|
| 136 |
|
| 137 |
// Write the common part of the url
|
| 138 |
$ftp_base_dir = "ftp://". urlencode($username) .":". urlencode($password) ."@localhost/";
|
| 139 |
|
| 140 |
if (!@is_dir($ftp_base_dir)) {
|
| 141 |
drupal_set_message(t('The supplied username/password combination '
|
| 142 |
.'was not accepted, or no local ftp server is running.'),
|
| 143 |
'error');
|
| 144 |
return FALSE;
|
| 145 |
}
|
| 146 |
|
| 147 |
// Try to guess the ftp address for the drupal install.
|
| 148 |
// Start by putting the entire address together and replacing \ with /.
|
| 149 |
$local_path = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT'] . base_path());
|
| 150 |
|
| 151 |
// Grab the last on off the end and explode it.
|
| 152 |
$local_path = explode('/', rtrim($local_path, '/'));
|
| 153 |
|
| 154 |
// Try to guess which how far in we are chrooted...
|
| 155 |
foreach ($local_path AS $index => $value) {
|
| 156 |
unset($local_path[$index]);
|
| 157 |
if (is_dir($ftp_base_dir . implode('/', $local_path) .'/'. $dir)) {
|
| 158 |
$ftp_path = $ftp_base_dir . implode('/', $local_path) .'/'. $dir;
|
| 159 |
break;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
// If we couldn't guess it, then quit.
|
| 164 |
if (!isset($ftp_path)) {
|
| 165 |
drupal_set_message(t('Could not guess the ftp directory for drupal.'), 'error');
|
| 166 |
return FALSE;
|
| 167 |
}
|
| 168 |
|
| 169 |
// Prepare the directories to use later.
|
| 170 |
$extract_dir = file_directory_path() .'/plugin_manager_extraction/';
|
| 171 |
|
| 172 |
// Process each of the files.
|
| 173 |
foreach ($files AS $index => $file) {
|
| 174 |
if (trim($file, "\\/") != $file) {
|
| 175 |
@mkdir("$ftp_path/$file");
|
| 176 |
}
|
| 177 |
else {
|
| 178 |
@copy($extract_dir . $file, "$ftp_path/$file");
|
| 179 |
}
|
| 180 |
}
|
| 181 |
return TRUE;
|
| 182 |
}
|