| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @abstract
|
| 6 |
* Provides common functions for working with multisites.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* @category HOOKS
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_help
|
| 15 |
*/
|
| 16 |
function multisite_api_help($section) {
|
| 17 |
switch ($section) {
|
| 18 |
case 'admin/help#multisite_api':
|
| 19 |
return '<p>'. t('A collection of common functions useful for administering multisites. ' .
|
| 20 |
'You don\'t need to install this unless required by another module.') .'</p>';
|
| 21 |
case 'admin/multisite/multisite_api':
|
| 22 |
return '<p>'. t('Most large sites will have a 3 stage workflow. ' .
|
| 23 |
'A developer writes code on a development server, ' .
|
| 24 |
'changes are moved to a staging server for QA, ' .
|
| 25 |
'and finaly on to the production server. ' .
|
| 26 |
'Sites on each server will likely have something common in the site name (drupal/sites/$site_name).') .'</p>';
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* implementation of hook_menu
|
| 32 |
*/
|
| 33 |
function multisite_api_menu($may_cache) {
|
| 34 |
$items = array();
|
| 35 |
|
| 36 |
if ($may_cache) {
|
| 37 |
$access = user_access('administer site configuration');
|
| 38 |
$items[] = array(
|
| 39 |
'path' => 'admin/multisite/multisite_api',
|
| 40 |
'title' => t('Multisite API'),
|
| 41 |
'callback' => 'drupal_get_form',
|
| 42 |
'callback arguments' => 'multisite_api_form',
|
| 43 |
'access' => $access,
|
| 44 |
);
|
| 45 |
$items[] = array(
|
| 46 |
'path' => 'admin/multisite',
|
| 47 |
'title' => t('Multisite'),
|
| 48 |
'description' => t('Multisite administration.'),
|
| 49 |
'position' => 'right',
|
| 50 |
'weight' => 5,
|
| 51 |
'callback' => 'multisite_api_overview',
|
| 52 |
'access' => $access,
|
| 53 |
);
|
| 54 |
}
|
| 55 |
return $items;
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* @category PAGES
|
| 60 |
*/
|
| 61 |
function multisite_api_overview() {
|
| 62 |
|
| 63 |
// Check database setup if necessary
|
| 64 |
if (function_exists('db_check_setup') && empty($_POST)) {
|
| 65 |
db_check_setup();
|
| 66 |
}
|
| 67 |
|
| 68 |
$menu = menu_get_item(NULL, 'admin/multisite');
|
| 69 |
$content = system_admin_menu_block($menu);
|
| 70 |
|
| 71 |
$output = theme('admin_block_content', $content);
|
| 72 |
|
| 73 |
return $output;
|
| 74 |
}
|
| 75 |
|
| 76 |
function multisite_api_form() {
|
| 77 |
$form['types'] = array(
|
| 78 |
'#type' => 'fieldset',
|
| 79 |
'#title' => t('Site types'),
|
| 80 |
'#collapsible' => false,
|
| 81 |
'#description' => t('To determine what type a site is, enter the common string below.'),
|
| 82 |
);
|
| 83 |
$form['types']['multisite_api_development'] = array(
|
| 84 |
'#type' => 'textfield',
|
| 85 |
'#default_value' => variable_get('multisite_api_development', 'localhost'),
|
| 86 |
'#title' => t('Development'),
|
| 87 |
);
|
| 88 |
$form['types']['multisite_api_staging'] = array(
|
| 89 |
'#type' => 'textfield',
|
| 90 |
'#default_value' => variable_get('multisite_api_staging', 'staging'),
|
| 91 |
'#title' => t('Staging'),
|
| 92 |
);
|
| 93 |
$form['types']['multisite_api_production'] = array(
|
| 94 |
'#title' => t('Production'),
|
| 95 |
'#type' => 'item',
|
| 96 |
'#value' => t('Any site not meeting the above criteria is considered a production site'),
|
| 97 |
);
|
| 98 |
$form['multisite_api_exclude'] = array(
|
| 99 |
'#title' => t('Exclude'),
|
| 100 |
'#type' => 'textfield',
|
| 101 |
'#default_value' => variable_get('multisite_api_exclude', 'intra'),
|
| 102 |
'#description' => t('Any site that contains a string from this list should always be excluded from ' .
|
| 103 |
'lists returned by the API functions. Enter a comma separated list (no spaces) of strings. ' .
|
| 104 |
'You could use this for an intranet site, or any other site that should be dealt with manually and separately.'),
|
| 105 |
);
|
| 106 |
return system_settings_form($form);
|
| 107 |
}
|
| 108 |
|
| 109 |
function multisite_api_form_submit($form_id, $form_values) {
|
| 110 |
variable_set('multisite_api_development', $form_values['multisite_api_development']);
|
| 111 |
variable_set('multisite_api_staging', $form_values['multisite_api_staging']);
|
| 112 |
variable_set('multisite_api_exclude', $form_values['multisite_api_exclude']);
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* @category API
|
| 117 |
*/
|
| 118 |
|
| 119 |
/**
|
| 120 |
* Gets the type of the given site name
|
| 121 |
* @param string $site
|
| 122 |
* The site (drupal/sites/$site) to get, leave blank to get the type of the current site
|
| 123 |
* @return string
|
| 124 |
* one of: production, staging, development, or null if the site is in the excludes list
|
| 125 |
*
|
| 126 |
*/
|
| 127 |
function multisite_api_get_site_type($site = null) {
|
| 128 |
|
| 129 |
// get site
|
| 130 |
if (!$site) {
|
| 131 |
$site = substr(conf_path(), 6);
|
| 132 |
}
|
| 133 |
|
| 134 |
// deal with the excludes list
|
| 135 |
$excludes = _multisite_api_excludes();
|
| 136 |
foreach ($excludes as $e) {
|
| 137 |
if (strstr($site, $e) !== false ) {
|
| 138 |
return null;
|
| 139 |
}
|
| 140 |
}
|
| 141 |
|
| 142 |
// what type is it?
|
| 143 |
if (strpos($site, variable_get('multisite_api_staging', 'staging')) !== false) {
|
| 144 |
return 'staging';
|
| 145 |
}
|
| 146 |
elseif (strpos($site, variable_get('multisite_api_development', 'localhost')) !== false) {
|
| 147 |
return 'development';
|
| 148 |
}
|
| 149 |
else {
|
| 150 |
return 'production';
|
| 151 |
}
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
| 155 |
* Get the names of all sites of the given type, sites in the excludes list are not included
|
| 156 |
* @param string $type
|
| 157 |
* production, staging, or development
|
| 158 |
* or 'current' to return only sites matching the type of the current site
|
| 159 |
* @param simple array
|
| 160 |
* list of all sites, not including the current site
|
| 161 |
*/
|
| 162 |
function multisite_api_get_all_site_names_of_type($type) {
|
| 163 |
if ($type == 'current'){
|
| 164 |
$type = multisite_api_get_site_type($current_site);
|
| 165 |
}
|
| 166 |
$sites = multisite_api_site_list($type);
|
| 167 |
foreach ($sites as $site) {
|
| 168 |
$list[$site['dir']] = $site['dir'];
|
| 169 |
}
|
| 170 |
ksort($list);
|
| 171 |
return $list;
|
| 172 |
}
|
| 173 |
|
| 174 |
/**
|
| 175 |
* Gets info on all multisites
|
| 176 |
* @param string $type
|
| 177 |
* only return sites of this type (production, staging, or development)
|
| 178 |
* leave null to return all sites
|
| 179 |
* excluded sites are never returned
|
| 180 |
* @return
|
| 181 |
* array of associative arrays,
|
| 182 |
* Each sub array represents a multisite with elements for:
|
| 183 |
* dir => directory in sites, the site name
|
| 184 |
* type => development, staging, or production
|
| 185 |
* db_url => $db_url
|
| 186 |
* db_dsn => parsed version of $db_url
|
| 187 |
* db_prefix => $db_prefixed
|
| 188 |
* base_url => only returned if $base_url is defined
|
| 189 |
* db_default_url => default database url, taking into account $db_prefix['default']
|
| 190 |
* db_default_dsn => parsed version of $db_default_url
|
| 191 |
* + anything else that is defined in settings.php
|
| 192 |
* @todo cache this mutha
|
| 193 |
*/
|
| 194 |
function multisite_api_site_list($type = null) {
|
| 195 |
static $sites;
|
| 196 |
if (isset($sites)) {
|
| 197 |
return $sites;
|
| 198 |
}
|
| 199 |
|
| 200 |
$sites = array();
|
| 201 |
if (is_dir('sites') && $handle = opendir('sites')) {
|
| 202 |
while ($dir = readdir($handle)) {
|
| 203 |
if (file_exists("sites/$dir/settings.php")) {
|
| 204 |
if (!$site_type = multisite_api_get_site_type($dir)) {
|
| 205 |
// the site is part of the excludes list
|
| 206 |
continue;
|
| 207 |
}
|
| 208 |
if ($type && $type != $site_type) {
|
| 209 |
continue;
|
| 210 |
}
|
| 211 |
|
| 212 |
$vars = _multisite_api_eval_file("sites/$dir/settings.php");
|
| 213 |
$vars['type'] = $site_type;
|
| 214 |
|
| 215 |
// db_url is an array, assume we only need the default
|
| 216 |
if (is_array($vars['db_url'])) {
|
| 217 |
$vars['db_url'] = $vars['db_url']['default'];
|
| 218 |
}
|
| 219 |
$vars['db_dsn'] = parse_url($vars['db_url']);
|
| 220 |
$vars['dir'] = $dir;
|
| 221 |
|
| 222 |
// build default DSN
|
| 223 |
if (is_array($vars['db_prefix']) && substr($vars['db_prefix']['default'], -1, 1) == '.'){
|
| 224 |
$vars['db_default_dsn'] = $vars['db_dsn'];
|
| 225 |
$vars['db_default_dsn']['path'] = '/'. strrev(substr(strrev($vars['db_prefix']['default']), 1));
|
| 226 |
}
|
| 227 |
elseif(is_string($vars['db_prefix']) && substr($vars['db_prefix'], -1, 1) == '.') {
|
| 228 |
$vars['db_default_dsn'] = $vars['db_dsn'];
|
| 229 |
$vars['db_default_dsn']['path'] = '/'. strrev(substr(strrev($vars['db_prefix']), 1));
|
| 230 |
}
|
| 231 |
else {
|
| 232 |
$vars['db_default_dsn'] = $vars['db_dsn'];
|
| 233 |
}
|
| 234 |
$vars['db_default_url'] = $vars['db_default_dsn']['scheme'] .'://'. $vars['db_default_dsn']['user'] .':'. $vars['db_default_dsn']['pass'] .'@'. $vars['db_default_dsn']['host']. $vars['db_default_dsn']['path'];
|
| 235 |
|
| 236 |
// add to main array
|
| 237 |
$sites[$dir] = $vars;
|
| 238 |
}
|
| 239 |
}
|
| 240 |
}
|
| 241 |
//$sites = ksort($sites); // WTF? doing this makes an empty array ?!?!?!
|
| 242 |
return $sites;
|
| 243 |
}
|
| 244 |
|
| 245 |
/**
|
| 246 |
* @category INTERNAL FUNCTIONS
|
| 247 |
*/
|
| 248 |
|
| 249 |
/**
|
| 250 |
* evaluates the php file and returns all defined variables
|
| 251 |
* ONLY USE THIS ON SETTINGS.PHP ANYTHING ELSE IS RISKY
|
| 252 |
* @param string $file_path the path to the file
|
| 253 |
* @return associative array of all evaluated variables
|
| 254 |
*/
|
| 255 |
function _multisite_api_eval_file($file_path) {
|
| 256 |
|
| 257 |
// get file
|
| 258 |
$data = file($file_path);
|
| 259 |
|
| 260 |
// remove dangerous things
|
| 261 |
foreach($data as $n => $line) {
|
| 262 |
if (substr($line , 0, 7) == 'ini_set') {
|
| 263 |
$data[$n] = '';
|
| 264 |
}
|
| 265 |
}
|
| 266 |
$data = implode($data);
|
| 267 |
|
| 268 |
// trim open php tag
|
| 269 |
if (substr($data, 0, 2) == '<?') {
|
| 270 |
$data = substr($data, 2);
|
| 271 |
}
|
| 272 |
if (substr($data, 0, 3) == 'php') {
|
| 273 |
$data = substr($data, 3);
|
| 274 |
}
|
| 275 |
|
| 276 |
//trim close php tag
|
| 277 |
if (substr($data, -2) == '?>') {
|
| 278 |
$data = substr($data, 0, strlen($data) -2);
|
| 279 |
}
|
| 280 |
if (substr($data, -3) == 'php') {
|
| 281 |
$data = substr($data, 0, strlen($data) -3);
|
| 282 |
}
|
| 283 |
|
| 284 |
// get the vars
|
| 285 |
eval($data);
|
| 286 |
$vars = get_defined_vars();
|
| 287 |
unset($vars['file_path'], $vars['data'], $vars['n'], $vars['line']);
|
| 288 |
return $vars;
|
| 289 |
}
|
| 290 |
|
| 291 |
function _multisite_api_excludes() {
|
| 292 |
$excludes = variable_get('multisite_api_exclude', 'intra');
|
| 293 |
return explode(',', $excludes);
|
| 294 |
}
|