| 1 |
<?php |
<?php |
| 2 |
// $Id: multisite_manager.module,v 1.7 2008/02/08 20:25:25 schuyler1d Exp $ |
// $Id: multisite_manager.module,v 1.8 2008/02/08 22:11:51 schuyler1d Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* author: Schuyler Duveen |
* author: Schuyler Duveen |
| 20 |
function multisite_manager_help($section) { |
function multisite_manager_help($section) { |
| 21 |
switch ($section) { |
switch ($section) { |
| 22 |
case 'admin/help#multisite_manager': |
case 'admin/help#multisite_manager': |
| 23 |
return t('Multisite Manager helps create new drupal sites from within a main drupal site. For documentation see http://code.google.com/p/drupal-multisite-manager/'); |
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 which are covered in more detail in the INSTALL.txt file:<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>'); |
| 24 |
|
case 'admin/settings/multisite_manager': |
| 25 |
|
return t('These defaults will need corresponding changes in settings.php. Consult help for other required changes on the file system.'); |
| 26 |
|
|
| 27 |
} |
} |
| 28 |
} |
} |
| 29 |
|
|
| 122 |
function multisite_manager_menu($may_cache) { |
function multisite_manager_menu($may_cache) { |
| 123 |
$items = array(); |
$items = array(); |
| 124 |
if ($may_cache) { |
if ($may_cache) { |
| 125 |
|
$items[] = array('path' => 'admin/settings/multisite_manager', |
| 126 |
|
'title' => t('Multisite Manager settings'), |
| 127 |
|
'description' => t('Configure the database and url defaults for new sites.'), |
| 128 |
|
'callback' => 'drupal_get_form', |
| 129 |
|
'callback arguments' => array('multisite_manager_admin_settings'), |
| 130 |
|
'access' => user_access('administer multisite defaults'), |
| 131 |
|
'type' => MENU_NORMAL_ITEM |
| 132 |
|
); |
| 133 |
|
$items[] = array('path' => 'admin/settings/multisite_manager/defaults', |
| 134 |
|
'title' => t('Defaults'), |
| 135 |
|
'description' => t('Configure the database and url defaults for new sites.'), |
| 136 |
|
'callback' => 'drupal_get_form', |
| 137 |
|
'callback arguments' => array('multisite_manager_admin_settings'), |
| 138 |
|
'access' => user_access('administer multisite defaults'), |
| 139 |
|
'type' => MENU_DEFAULT_LOCAL_TASK, |
| 140 |
|
); |
| 141 |
|
$items[] = array('path' => 'admin/settings/multisite_manager/phpsettingfile', |
| 142 |
|
'title' => t('settings.php help'), |
| 143 |
|
'type' => MENU_LOCAL_TASK, |
| 144 |
|
'description' => t('What to put in the settings.php file'), |
| 145 |
|
'callback' => 'multisite_manager_admin_file_settingsdotphp', |
| 146 |
|
'access' => user_access('administer multisite defaults') |
| 147 |
|
); |
| 148 |
|
/* |
| 149 |
$items[] = array('path' => 'admin/settings/multisite_manager', |
$items[] = array('path' => 'admin/settings/multisite_manager', |
| 150 |
'title' => t('Multisite Manager settings'), |
'title' => t('Multisite Manager settings'), |
| 151 |
'description' => t('Configure the database and url defaults for new sites.'), |
'description' => t('Configure the database and url defaults for new sites.'), |
| 153 |
'callback arguments' => array('multisite_manager_admin_settings'), |
'callback arguments' => array('multisite_manager_admin_settings'), |
| 154 |
'access' => user_access('administer multisite defaults'), |
'access' => user_access('administer multisite defaults'), |
| 155 |
'type' => MENU_NORMAL_ITEM); |
'type' => MENU_NORMAL_ITEM); |
| 156 |
|
*/ |
| 157 |
} |
} |
| 158 |
return $items; |
return $items; |
| 159 |
} |
} |
| 201 |
'db_user' => '', |
'db_user' => '', |
| 202 |
'db_pass' => '', |
'db_pass' => '', |
| 203 |
'db_path' => variable_get('multisite_manager_dbpath_default', ''), |
'db_path' => variable_get('multisite_manager_dbpath_default', ''), |
| 204 |
'link' => variable_get('multisite_manager_link_default', ''), |
'link' => variable_get('multisite_manager_link_default', '{base_url}/site/{shortname}'), |
| 205 |
'run_cron' => variable_get('multisite_manager_run_cron', 0), |
'run_cron' => variable_get('multisite_manager_run_cron', 0), |
| 206 |
|
'install_immediate' => variable_get('multisite_manager_install_immediate', TRUE), |
| 207 |
|
|
| 208 |
); |
); |
| 209 |
} |
} |
| 210 |
|
|
| 259 |
'#type' => 'radios', |
'#type' => 'radios', |
| 260 |
'#title' => t('Profile'), |
'#title' => t('Profile'), |
| 261 |
'#required' => TRUE, |
'#required' => TRUE, |
| 262 |
'#options' => _multisite_manager_profile_options(),//array_combine($profiles,$profiles), |
'#options' => _multisite_manager_profile_options(), |
| 263 |
'#default_value' => $defaults['profile'], |
'#default_value' => isset($node->profile)?$node->profile:$defaults['profile'], |
| 264 |
'#weight' => -2 |
'#weight' => -2 |
| 265 |
); |
); |
| 266 |
|
|
| 480 |
case 'mysql': |
case 'mysql': |
| 481 |
return mysql_error(); |
return mysql_error(); |
| 482 |
case 'mysqli': |
case 'mysqli': |
| 483 |
return mysqli_error(); |
return mysqli_error(db_set_active()); |
| 484 |
case 'pgsql': |
case 'pgsql': |
| 485 |
return pg_last_error(); |
return pg_last_error(); |
| 486 |
} |
} |
| 543 |
|
|
| 544 |
if ($errors === 1007) { |
if ($errors === 1007) { |
| 545 |
///This is ok, maybe we put all the new databases in one other db |
///This is ok, maybe we put all the new databases in one other db |
| 546 |
drupal_set_message("Database already existed"); |
drupal_set_message(t("Database already existed"),'error'); |
| 547 |
} |
} |
| 548 |
|
elseif ($errors === 1004) { |
| 549 |
|
drupal_set_message(t("The current database user does not have privileges to create a database."),'errror'); |
| 550 |
|
} |
| 551 |
elseif ($errors) { |
elseif ($errors) { |
| 552 |
drupal_set_message("Database error when creating database: $error", 'error'); |
drupal_set_message(t("Database error when creating database: %errors",array('%errors' => $errors)), 'error'); |
| 553 |
return FALSE; |
return FALSE; |
| 554 |
} |
} |
| 555 |
} |
} |
| 616 |
* 3. installs drupal profile in new place |
* 3. installs drupal profile in new place |
| 617 |
* 4. switches back to current context |
* 4. switches back to current context |
| 618 |
*/ |
*/ |
| 619 |
global $multisite_manager_installmodules, $db_prefix, $conf; |
global $multisite_manager_installmodules, $db_prefix, $conf, $locale; |
| 620 |
|
|
| 621 |
include_once './includes/install.inc'; |
include_once './includes/install.inc'; |
| 622 |
|
|
| 623 |
|
///in case we're running from admin/multisite_manager_batch_install.php |
| 624 |
|
///instead of from hook_insert() which came right after hook_validate() |
| 625 |
|
if (!$multisite_manager_installmodules) { |
| 626 |
|
$multisite_manager_installmodules = drupal_verify_profile($node->profile, $locale); |
| 627 |
|
if (!$multisite_manager_installmodules) { |
| 628 |
|
form_set_error('profile', t('One or more required modules are missing!'), 'error'); |
| 629 |
|
} |
| 630 |
|
} |
| 631 |
|
|
| 632 |
/** |
/** |
| 633 |
* creates database and user w/ permissions if necessary |
* creates database and user w/ permissions if necessary |
| 634 |
* _newdb returns the db_url for the new site based on |
* _newdb returns the db_url for the new site based on |
| 682 |
/// go and rename the tables of the CURRENT DATABASE |
/// go and rename the tables of the CURRENT DATABASE |
| 683 |
$mid_cur_db = _multisite_manager_dbswitch($new_db); |
$mid_cur_db = _multisite_manager_dbswitch($new_db); |
| 684 |
|
|
| 685 |
drupal_set_message('Renaming tables in the new database.'); |
drupal_set_message(t('Renaming tables in the new database.')); |
| 686 |
$result = _multisite_manager_tables_result($db_prefix); |
$result = _multisite_manager_tables_result($db_prefix); |
| 687 |
|
|
| 688 |
if ($cur_db['url'] != $mid_cur_db['url']) { |
if ($cur_db['url'] != $mid_cur_db['url']) { |
| 689 |
///This is a sanity check. We can still rename the tables, but without the RE-SWITCH, we would have clobbered our CUR_DB |
///This is a sanity check. We can still rename the tables, but without the RE-SWITCH, we would have clobbered our CUR_DB |
| 690 |
drupal_set_message('Something has gone horribly wrong. The database '. $mid_cur_db['url'] .' is NOT the new one. Therefore, the database was switched mysteriously some time during installation. Thus some features that were intended to be in your new database may have polluted your current database.','error'); |
drupal_set_message(t('Something has gone horribly wrong. The database %database is NOT the new one. Therefore, the database was switched mysteriously some time during installation. Thus some features that were intended to be in your new database may have polluted your current database.',array('%database' => $mid_cur_db['url'])),'error'); |
| 691 |
} |
} |
| 692 |
|
|
| 693 |
///special duty for {sequences} until it doesn't buggily store the prefix; BUG: http://drupal.org/node/168977 |
///special duty for {sequences} until it doesn't buggily store the prefix; BUG: http://drupal.org/node/168977 |
| 794 |
|
|
| 795 |
while ($table = @db_fetch_array($result)) { |
while ($table = @db_fetch_array($result)) { |
| 796 |
$table_name = array_pop($table); |
$table_name = array_pop($table); |
| 797 |
drupal_set_message("Dropping table ".$table_name); |
drupal_set_message(t("Dropping table %table",array('%table' => $table_name))); |
| 798 |
@db_query("DROP TABLE %s", $table_name); |
@db_query("DROP TABLE %s", $table_name); |
| 799 |
} |
} |
| 800 |
/* SWITCH BACK TO OLD DB */ |
/* SWITCH BACK TO OLD DB */ |
| 816 |
return $additions; |
return $additions; |
| 817 |
} |
} |
| 818 |
|
|
| 819 |
|
|
| 820 |
|
/* |
| 821 |
|
* multisite_manager_admin_file_settingsdotphp |
| 822 |
|
* @return string of PHP code that would be included based on the default settings for link/database name |
| 823 |
|
*/ |
| 824 |
|
function multisite_manager_admin_file_settingsdotphp() { |
| 825 |
|
global $base_url; |
| 826 |
|
$defaults = _multisite_manager_defaults(); |
| 827 |
|
$link = variable_get('multisite_manager_link_default', $defaults['link']); |
| 828 |
|
/// |
| 829 |
|
$fakenode = new stdClass(); |
| 830 |
|
$fakenode->shortname = '". $my_site_base ."'; |
| 831 |
|
$fakenode->db_prefix = variable_get('multisite_manager_dbprefix_default', ''); |
| 832 |
|
$fakenode->db_user = ''; |
| 833 |
|
$fakenode->db_pass = '<b style="color:red">DATABASE_PASSWORD</b>'; |
| 834 |
|
$fakenode->db_path = variable_get('multisite_manager_dbpath_default', ''); |
| 835 |
|
$newdb = _multisite_manager_dbobj($fakenode); |
| 836 |
|
|
| 837 |
|
$recommended_directory = ''; |
| 838 |
|
|
| 839 |
|
if (!$link || !$newdb['url']) { |
| 840 |
|
return "ERROR: no link or db url" . $link .$newdb['url'] ; |
| 841 |
|
} |
| 842 |
|
$link = str_replace('{base_url}', $base_url, $link); |
| 843 |
|
|
| 844 |
|
//$url_pieces[2] is hostname, $url_pieces[3] is path |
| 845 |
|
$url_pieces = explode('/', $link, 4); |
| 846 |
|
|
| 847 |
|
$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>', |
| 848 |
|
'$matches = FALSE;'); |
| 849 |
|
|
| 850 |
|
$host_regex = str_replace('{shortname}', '(\w+)', $url_pieces[2]); |
| 851 |
|
$path_regex = str_replace('{shortname}', '(\w+)', $url_pieces[3]); |
| 852 |
|
|
| 853 |
|
//assumes shortname will never be the TLD |
| 854 |
|
$recommended_directory = preg_replace('|\(\\w\+\)[^.]*\.|','',$host_regex); |
| 855 |
|
///shortname in hostname |
| 856 |
|
if (strpos($url_pieces[2],'{shortname}') !== FALSE) { |
| 857 |
|
$rv[] = 'if (preg_match("|'. $host_regex .'|",$_SERVER["HTTP_HOST"], $matches)'; |
| 858 |
|
$rv[] = ' && preg_match("|^/'. $path_regex .'|",request_uri())'; |
| 859 |
|
$rv[] = ' && $matches)'; |
| 860 |
|
$rv[] = '{'; |
| 861 |
|
}///shortname in path |
| 862 |
|
elseif (strpos($url_pieces[3],'{shortname}') !== FALSE) { |
| 863 |
|
$rv[] = 'if (preg_match("|'. $host_regex .'|",$_SERVER["HTTP_HOST"])'; |
| 864 |
|
$rv[] = ' && preg_match("|^/'. $path_regex .'|",request_uri(), $matches)'; |
| 865 |
|
$rv[] = ' && $matches)'; |
| 866 |
|
$rv[] = '{'; |
| 867 |
|
$recommended_directory .= '.' . str_replace('/','.',preg_replace('|(/[^/]*)?\(\\\w\+\)([^/]*)|','',$path_regex)); |
| 868 |
|
} |
| 869 |
|
else { |
| 870 |
|
///{shortname} has to appear in the link somewhere! |
| 871 |
|
return "ERROR: shortname must be in the link template " .$url_pieces[2] .'XX' . $url_pieces[3]; |
| 872 |
|
} |
| 873 |
|
$rv[] = ' $my_site_base = $matches[1];'; |
| 874 |
|
|
| 875 |
|
///prefix |
| 876 |
|
$rv[] = ' $db_prefix = "' . $newdb['prefix'] .'";'; |
| 877 |
|
///needs URL structure |
| 878 |
|
$rv[] = ' $base_url = "'. str_replace('{shortname}','". $my_site_base ."',$link) .'"; // NO trailing slash!'; |
| 879 |
|
|
| 880 |
|
///needs newDB URI structure |
| 881 |
|
$rv[] = ' $db_url = "' .$newdb['url'] .'";'; |
| 882 |
|
|
| 883 |
|
$rv[] = ' ///This assumes you have a ./files directory in your base drupal directory'; |
| 884 |
|
$rv[] = ' $conf = array('; |
| 885 |
|
$rv[] = ' "file_directory_path" => "files/".$my_site_base,'; |
| 886 |
|
$rv[] = ' );'; |
| 887 |
|
$rv[] = '}'; |
| 888 |
|
|
| 889 |
|
$rv[] = "</code></pre></blockquote>"; |
| 890 |
|
$rv[] = 'Suggested directory under sites/ to include settings.php file: <b>' . $recommended_directory . "</b></p>"; |
| 891 |
|
return implode("\n",$rv); |
| 892 |
|
} |
| 893 |
|
|
| 894 |
function multisite_manager_admin_settings_validate($form_id, $form_values) { |
function multisite_manager_admin_settings_validate($form_id, $form_values) { |
| 895 |
if ($form_values['module'] == 'multisite_manager') { |
if ($form_values['module'] == 'multisite_manager') { |
| 896 |
if (empty($form_values['multisite_manager_dbprefix_default']) && empty($form_values['multisite_manager_dbpath_default'])) { |
if (empty($form_values['multisite_manager_dbprefix_default']) && empty($form_values['multisite_manager_dbpath_default'])) { |
| 900 |
} |
} |
| 901 |
|
|
| 902 |
function multisite_manager_admin_settings() { |
function multisite_manager_admin_settings() { |
| 903 |
$defaults = array('db_prefix' => '{shortname}_', |
$defaults = _multisite_manager_defaults(); |
|
'db_path' => '', |
|
|
'link' => '{base_url}/site/{shortname}', |
|
|
'profile' => 'default', |
|
|
'install_immediate' => TRUE, |
|
|
'run_cron' => 0, |
|
|
); |
|
| 904 |
$form['multisite_manager_run_cron'] = array( |
$form['multisite_manager_run_cron'] = array( |
| 905 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 906 |
'#title' => t('Run drupal sites\' cron jobs'), |
'#title' => t('Run drupal sites\' cron jobs'), |
| 907 |
'#required' => FALSE, |
'#required' => FALSE, |
| 908 |
'#default_value' => variable_get('multisite_manager_run_cron', $defaults['run_cron']), |
'#default_value' => $defaults['run_cron'], |
| 909 |
'#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.') |
'#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.') |
| 910 |
); |
); |
| 911 |
$form['multisite_manager_install_immediate'] = array( |
$form['multisite_manager_install_immediate'] = array( |
| 912 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 913 |
'#title' => t('Install site upon node creation'), |
'#title' => t('Install site upon node creation'), |
| 914 |
'#required' => FALSE, |
'#required' => FALSE, |
| 915 |
'#default_value' => variable_get('multisite_manager_install_immediate', $defaults['install_immediate']), |
'#default_value' => $defaults['install_immediate'], |
| 916 |
'#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.') |
'#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.') |
| 917 |
); |
); |
| 918 |
$form['multisite_manager_dbprefix_default'] = array( |
$form['multisite_manager_dbprefix_default'] = array( |
| 919 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 920 |
'#title' => t('Table prefix default'), |
'#title' => t('Table prefix default'), |
| 921 |
'#required' => FALSE, |
'#required' => FALSE, |
| 922 |
'#default_value' => variable_get('multisite_manager_dbprefix_default', $defaults['db_prefix']), |
'#default_value' => $defaults['db_prefix'], |
| 923 |
'#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.') |
'#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.') |
| 924 |
); |
); |
| 925 |
$form['multisite_manager_dbpath_default'] = array( |
$form['multisite_manager_dbpath_default'] = array( |
| 926 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 927 |
'#title' => t('Database name default'), |
'#title' => t('Database name default'), |
| 928 |
'#required' => FALSE, |
'#required' => FALSE, |
| 929 |
'#default_value' => variable_get('multisite_manager_dbpath_default', $defaults['db_path']), |
'#default_value' => $defaults['db_path'], |
| 930 |
'#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}".'), |
'#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}".'), |
| 931 |
); |
); |
| 932 |
$form['multisite_manager_link_default'] = array( |
$form['multisite_manager_link_default'] = array( |
| 933 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 934 |
'#title' => t('Link default'), |
'#title' => t('Link default'), |
| 935 |
'#required' => FALSE, |
'#required' => FALSE, |
| 936 |
'#default_value' => variable_get('multisite_manager_link_default', $defaults['link']), |
'#default_value' => $defaults['link'], |
| 937 |
'#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}.') |
'#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}.') |
| 938 |
); |
); |
| 939 |
$form['multisite_manager_profile_default'] = array( |
$form['multisite_manager_profile_default'] = array( |
| 941 |
'#title' => t('Profile default'), |
'#title' => t('Profile default'), |
| 942 |
'#required' => FALSE, |
'#required' => FALSE, |
| 943 |
'#options' => _multisite_manager_profile_options(), |
'#options' => _multisite_manager_profile_options(), |
| 944 |
'#default_value' => variable_get('multisite_manager_profile_default', $defaults['profile']), |
'#default_value' => $defaults['profile'], |
| 945 |
); |
); |
| 946 |
|
|
| 947 |
return system_settings_form($form); |
return system_settings_form($form); |