| 1 |
<?php |
<?php |
| 2 |
// $Id: multisite_manager.module,v 1.8 2008/02/08 22:11:51 schuyler1d Exp $ |
// $Id: multisite_manager.module,v 1.8.4.1 2008/03/12 16:47:45 schuyler1d Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* author: Schuyler Duveen |
* author: Schuyler Duveen |
| 24 |
function multisite_manager_help($section) { |
function multisite_manager_help($section) { |
| 25 |
switch ($section) { |
switch ($section) { |
| 26 |
case 'admin/help#multisite_manager': |
case 'admin/help#multisite_manager': |
|
case 'admin/settings/multisite_manager': |
|
| 27 |
return t('Multisite Manager helps create new drupal sites from within a main drupal site. For documentation see <a href="http://drupal.org/project/multisite_manager">http://drupal.org/project/multisite_manager</a>.<p>This module is <b>NOT Plug-n-Play</b>. Before this module will work as desired follow these steps:<ol><li>Configure the Multisite Manager defaults for where new sites will be added to the database.</li><li>Make a special settings.php file in the /sites/ drupal directory. (This '. l( 'tool','admin/settings/multisite_manager/phpsettingfile') . ' will help)</li><li>Change your Apache config, probably in your Drupal .htaccess file.</li><li>For the default setup you need a symlink in your main Drupal directory. After changing to that directory, run <code>$ ln -s . site</code></li><li>Lastly, the DB account that runs the main site must have extra DB permissions to create databases and grant privileges</li></ol></p>'); |
return t('Multisite Manager helps create new drupal sites from within a main drupal site. For documentation see <a href="http://drupal.org/project/multisite_manager">http://drupal.org/project/multisite_manager</a>.<p>This module is <b>NOT Plug-n-Play</b>. Before this module will work as desired follow these steps:<ol><li>Configure the Multisite Manager defaults for where new sites will be added to the database.</li><li>Make a special settings.php file in the /sites/ drupal directory. (This '. l( 'tool','admin/settings/multisite_manager/phpsettingfile') . ' will help)</li><li>Change your Apache config, probably in your Drupal .htaccess file.</li><li>For the default setup you need a symlink in your main Drupal directory. After changing to that directory, run <code>$ ln -s . site</code></li><li>Lastly, the DB account that runs the main site must have extra DB permissions to create databases and grant privileges</li></ol></p>'); |
| 28 |
|
case 'admin/settings/multisite_manager': |
| 29 |
|
return t('These defaults will need corresponding changes in settings.php. Consult help for other required changes on the file system.'); |
| 30 |
|
|
| 31 |
} |
} |
| 32 |
} |
} |
| 33 |
|
|
| 127 |
$items = array(); |
$items = array(); |
| 128 |
$items['admin/settings/multisite_manager'] = array( |
$items['admin/settings/multisite_manager'] = array( |
| 129 |
'title' => t('Multisite Manager settings'), |
'title' => t('Multisite Manager settings'), |
| 130 |
|
'description' => t('Configure the database and url defaults for new sites.'), |
| 131 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 132 |
'page arguments' => array('multisite_manager_admin_settings'), |
'page arguments' => array('multisite_manager_admin_settings'), |
| 133 |
'access arguments' => array('administer multisite defaults'), |
'access arguments' => array('administer multisite defaults'), |
| 134 |
|
'file' => 'multisite_manager.admin.inc', |
| 135 |
); |
); |
| 136 |
$items['admin/settings/multisite_manager/defaults'] = array( |
$items['admin/settings/multisite_manager/defaults'] = array( |
| 137 |
'title' => t('Defaults'), |
'title' => t('Defaults'), |
| 140 |
'page arguments' => array('multisite_manager_admin_settings'), |
'page arguments' => array('multisite_manager_admin_settings'), |
| 141 |
'access arguments' => array('administer multisite defaults'), |
'access arguments' => array('administer multisite defaults'), |
| 142 |
'type' => MENU_DEFAULT_LOCAL_TASK, |
'type' => MENU_DEFAULT_LOCAL_TASK, |
| 143 |
|
'file' => 'multisite_manager.admin.inc', |
| 144 |
); |
); |
| 145 |
$items['admin/settings/multisite_manager/phpsettingfile'] = array( |
$items['admin/settings/multisite_manager/phpsettingfile'] = array( |
| 146 |
'title' => t('settings.php help'), |
'title' => t('settings.php help'), |
| 147 |
'type' => MENU_LOCAL_TASK, |
'type' => MENU_LOCAL_TASK, |
| 148 |
'description' => t('What to put in the settings.php file'), |
'description' => t('What to put in the settings.php file'), |
| 149 |
'page callback' => 'multisite_manager_admin_file_settingsdotphp', |
'page callback' => 'multisite_manager_admin_file_settingsdotphp', |
| 150 |
'access arguments' => array('administer multisite defaults') |
'access arguments' => array('administer multisite defaults'), |
| 151 |
|
'file' => 'multisite_manager.admin.inc', |
| 152 |
); |
); |
| 153 |
return $items; |
return $items; |
| 154 |
} |
} |
| 469 |
* Until this is in the db_* api..., return the error text from the last database query |
* Until this is in the db_* api..., return the error text from the last database query |
| 470 |
*/ |
*/ |
| 471 |
function _multisite_manager_db_error_message() { |
function _multisite_manager_db_error_message() { |
| 472 |
|
|
| 473 |
switch ($GLOBALS['db_type']) { |
switch ($GLOBALS['db_type']) { |
| 474 |
case 'mysql': |
case 'mysql': |
| 475 |
return mysql_error(); |
return mysql_error(); |
| 476 |
case 'mysqli': |
case 'mysqli': |
| 477 |
return mysqli_error(db_set_active()); |
global $active_db; |
| 478 |
|
return mysqli_error($active_db); |
| 479 |
case 'pgsql': |
case 'pgsql': |
| 480 |
return pg_last_error(); |
return pg_last_error(); |
| 481 |
} |
} |
| 733 |
/// go and rename the tables of the CURRENT DATABASE |
/// go and rename the tables of the CURRENT DATABASE |
| 734 |
$mid_cur_db = _multisite_manager_dbswitch($new_db); |
$mid_cur_db = _multisite_manager_dbswitch($new_db); |
| 735 |
|
|
| 736 |
drupal_set_message('Renaming tables in the new database.'); |
//drupal_set_message('Renaming tables in the new database.'); |
| 737 |
$result = _multisite_manager_tables_result($db_prefix); |
$result = _multisite_manager_tables_result($db_prefix); |
| 738 |
|
|
| 739 |
if ($cur_db['url'] != $mid_cur_db['url']) { |
if ($cur_db['url'] != $mid_cur_db['url']) { |
| 862 |
return $additions; |
return $additions; |
| 863 |
} |
} |
| 864 |
|
|
|
function multisite_manager_admin_settings_validate($form_id, $form_values) { |
|
|
if ($form_values['module'] == 'multisite_manager') { |
|
|
if (empty($form_values['multisite_manager_dbprefix_default']) && empty($form_values['multisite_manager_dbpath_default'])) { |
|
|
form_set_error('multisite_manager_dbprefix_default', t('You must, at least, enter a database or table prefix string. Both cannot be empty')); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
/* |
|
|
* multisite_manager_admin_file_settingsdotphp |
|
|
* @return string of PHP code that would be included based on the default settings for link/database name |
|
|
*/ |
|
|
function multisite_manager_admin_file_settingsdotphp() { |
|
|
global $base_url; |
|
|
$link = variable_get('multisite_manager_link_default', ''); |
|
|
/// |
|
|
$fakenode = new stdClass(); |
|
|
$fakenode->shortname = '". $my_site_base ."'; |
|
|
$fakenode->db_prefix = variable_get('multisite_manager_dbprefix_default', ''); |
|
|
$fakenode->db_user = ''; |
|
|
$fakenode->db_pass = '<b style="color:red">DATABASE_PASSWORD</b>'; |
|
|
$fakenode->db_path = variable_get('multisite_manager_dbpath_default', ''); |
|
|
$newdb = _multisite_manager_dbobj($fakenode); |
|
|
|
|
|
$recommended_directory = ''; |
|
|
|
|
|
if (!$link || !$fakenode->db_path) { |
|
|
return "ERROR: no link or db url"; |
|
|
} |
|
|
$link = str_replace('{base_url}', $base_url, $link); |
|
|
|
|
|
//$url_pieces[2] is hostname, $url_pieces[3] is path |
|
|
$url_pieces = explode('/', $link, 4); |
|
|
|
|
|
$rv = array('<p>Based on your configuration in '.l( 'Multisite Manager Defaults','admin/settings/multisite_manager/defaults').' The following code should be added to a special settings.php file which . Note, <b>This is an experimental feature</b> and you should look over this carefully before adding it in place of the database configuration (Also, please '.l('report suggestions/issues','http://drupal.org/project/multisite_manager') .').</p><blockquote><pre><code>', |
|
|
'$matches = FALSE;'); |
|
|
|
|
|
$host_regex = str_replace('{shortname}', '(\w+)', $url_pieces[2]); |
|
|
$path_regex = str_replace('{shortname}', '(\w+)', $url_pieces[3]); |
|
|
|
|
|
//assumes shortname will never be the TLD |
|
|
$recommended_directory = preg_replace('|\(\\w\+\)[^.]*\.|','',$host_regex); |
|
|
///shortname in hostname |
|
|
if (strpos($url_pieces[2],'{shortname}') !== FALSE) { |
|
|
$rv[] = 'if (preg_match("|'. $host_regex .'|",$_SERVER["HTTP_HOST"], $matches)'; |
|
|
$rv[] = ' && preg_match("|'. $path_regex .'|",request_uri())'; |
|
|
$rv[] = ' && $matches)'; |
|
|
$rv[] = '{'; |
|
|
}///shortname in path |
|
|
elseif (strpos($url_pieces[3],'{shortname}') !== FALSE) { |
|
|
$rv[] = 'if (preg_match("|'. $host_regex .'|",$_SERVER["HTTP_HOST"])'; |
|
|
$rv[] = ' && preg_match("|'. $path_regex .'|",request_uri(), $matches)'; |
|
|
$rv[] = ' && $matches)'; |
|
|
$rv[] = '{'; |
|
|
$recommended_directory .= '.' . str_replace('/','.',preg_replace('|(/[^/]*)?\(\\\w\+\)([^/]*)|','',$path_regex)); |
|
|
} |
|
|
else { |
|
|
///{shortname} has to appear in the link somewhere! |
|
|
return "ERROR: shortname must be in the link template " .$url_pieces[2] .'XX' . $url_pieces[3]; |
|
|
} |
|
|
$rv[] = ' $my_site_base = $matches[1];'; |
|
|
|
|
|
///prefix |
|
|
$rv[] = ' $db_prefix = "' . $newdb['prefix'] .'";'; |
|
|
///needs URL structure |
|
|
$rv[] = ' $base_url = "'. str_replace('{shortname}','". $my_site_base ."',$link) .'"; // NO trailing slash!'; |
|
|
|
|
|
///needs newDB URI structure |
|
|
$rv[] = ' $db_url = "' .$newdb['url'] .'";'; |
|
|
|
|
|
$rv[] = ' ///This assumes you have a ./files directory in your base drupal directory'; |
|
|
$rv[] = ' $conf = array('; |
|
|
$rv[] = ' "file_directory_path" => "files/".$my_site_base,'; |
|
|
$rv[] = ' );'; |
|
|
$rv[] = '}'; |
|
|
|
|
|
$rv[] = "</code></pre></blockquote>"; |
|
|
$rv[] = 'Suggested directory under sites/ to include settings.php file: <b>' . $recommended_directory . "</b></p>"; |
|
|
return implode("\n",$rv); |
|
|
} |
|
|
|
|
|
function multisite_manager_admin_settings() { |
|
|
$defaults = array('db_prefix' => '{shortname}_', |
|
|
'db_path' => '', |
|
|
'link' => '{base_url}/site/{shortname}', |
|
|
'admin_account' => 'copy creator', |
|
|
'profile' => 'default', |
|
|
'install_immediate' => TRUE, |
|
|
'run_cron' => 0, |
|
|
); |
|
|
$form['multisite_manager_run_cron'] = array( |
|
|
'#type' => 'checkbox', |
|
|
'#title' => t('Run drupal sites\' cron jobs'), |
|
|
'#required' => FALSE, |
|
|
'#default_value' => variable_get('multisite_manager_run_cron', $defaults['run_cron']), |
|
|
'#description' => t('This sets whether each drupal site\'s cron jobs will be run when this master site\'s cron is run. If this is unset no drupal site will have their cron jobs executed regardless of each individual sites\' settings for running cron jobs.') |
|
|
); |
|
|
$form['multisite_manager_install_immediate'] = array( |
|
|
'#type' => 'checkbox', |
|
|
'#title' => t('Install site upon node creation'), |
|
|
'#required' => FALSE, |
|
|
'#default_value' => variable_get('multisite_manager_install_immediate', $defaults['install_immediate']), |
|
|
'#description' => t('This sets whether Drupal site\'s creation is done upon node creation or wether it is delayed until the batch create script is run.') |
|
|
); |
|
|
$form['multisite_manager_dbprefix_default'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('Table prefix default'), |
|
|
'#required' => FALSE, |
|
|
'#default_value' => variable_get('multisite_manager_dbprefix_default', $defaults['db_prefix']), |
|
|
'#description' => t('This is the default prefix that will be forced on those without "advanced database setup" rights. {shortname} stands for the shortname field input when creating the site. If you make the default database different from the current one, you can reasonably make this empty.') |
|
|
); |
|
|
$form['multisite_manager_dbpath_default'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('Database name default'), |
|
|
'#required' => FALSE, |
|
|
'#default_value' => variable_get('multisite_manager_dbpath_default', $defaults['db_path']), |
|
|
'#description' => t('If left blank, it will use the current database. Otherwise, "{shortname}" will be replaced, so another common default might be "{shortname}_drupal". Do NOT set this unless the current database account has CREATE access in MySQL or CREATEDB access in Postgres. To ensure database data is not overwritten, if you install in another database, it is either required that your current database have some db_prefix OR that it will be a new database--i.e. that your entry here includes "{shortname}".'), |
|
|
); |
|
|
$form['multisite_manager_link_default'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('Link default'), |
|
|
'#required' => FALSE, |
|
|
'#default_value' => variable_get('multisite_manager_link_default', $defaults['link']), |
|
|
'#description' => t('This is where the site will be accessible by default. If you setup your ./sites/default/settings.php correctly along with your web server (e.g. apache/htaccess) config, you can anticipate where the new site will live and forward the user to the new site location upon creation. Here, there are two dynamic variables, {base_url} and {shortname}.') |
|
|
); |
|
|
$form['multisite_manager_admin_account'] = array( |
|
|
'#type' => 'radios', |
|
|
'#title' => t('Admin account on new sites'), |
|
|
'#required' => TRUE, |
|
|
'#options' => array( |
|
|
'copy admin' => t('Copy the admin account from this site'), |
|
|
'copy creator' => t('Copy the account of the creator'), |
|
|
'do nothing' => t('Do nothing (only choose this if all profiles available do something instead. the default profile does not)'), |
|
|
/*'prompt creator' => '',*/ |
|
|
), |
|
|
'#default_value' => variable_get('multisite_manager_admin_account', $defaults['admin_account']), |
|
|
); |
|
|
$form['multisite_manager_profile_default'] = array( |
|
|
'#type' => 'radios', |
|
|
'#title' => t('Profile default'), |
|
|
'#required' => FALSE, |
|
|
'#options' => _multisite_manager_profile_options(), |
|
|
'#default_value' => variable_get('multisite_manager_profile_default', $defaults['profile']), |
|
|
); |
|
|
|
|
|
return system_settings_form($form); |
|
|
} |
|
|
|
|
| 865 |
/** |
/** |
| 866 |
* Gets the node url from the link |
* Gets the node url from the link |
| 867 |
* |
* |