From f597f5c7b5296d80da24d44b06e9ed2f0d2bcc84 Mon Sep 17 00:00:00 2001 From: Tim Steiner <tsteiner2@unl.edu> Date: Wed, 13 Oct 2010 21:46:29 +0000 Subject: [PATCH] Added a slight hack to drupal's bootstrap to not require symlinks to create additional sites. Modified site creation tool to use new method: add records to .htaccess. THE CHANGES TO .htaccess.sample MUST BE COPIED TO YOUR .HTACCESS FILE! git-svn-id: file:///tmp/wdn_thm_drupal/trunk@239 20a16fea-79d4-4915-8869-1ea9d5ebf173 --- .htaccess.sample | 7 +++ includes/unl_bootstrap.inc | 32 ++++++++++++ index.php | 2 + sites/all/modules/unl/cron.php | 93 ++++++++++++++++------------------ 4 files changed, 85 insertions(+), 49 deletions(-) create mode 100644 includes/unl_bootstrap.inc diff --git a/.htaccess.sample b/.htaccess.sample index c7b4df00f..f8135d45a 100644 --- a/.htaccess.sample +++ b/.htaccess.sample @@ -62,6 +62,13 @@ DirectoryIndex index.php index.html index.htm <IfModule mod_rewrite.c> RewriteEngine on + # THIS SECTION IS AUTOMATICALY GENERATED. + # DO NOT EDIT!!!! + + # %UNL_CREATION_TOOL_STUB% + + # END AUTOMATICALLY GENERATED AREA. + # Block access to "hidden" directories whose names begin with a period. This # includes directories used by version control systems such as Subversion or # Git to store control files. Files whose names begin with a period, as well diff --git a/includes/unl_bootstrap.inc b/includes/unl_bootstrap.inc new file mode 100644 index 000000000..4f6d1a2d3 --- /dev/null +++ b/includes/unl_bootstrap.inc @@ -0,0 +1,32 @@ +<?php + +function unl_bootstrap() { + $original_script_name = $_SERVER['SCRIPT_NAME']; + + $path_parts = explode('/', $_SERVER['SCRIPT_URL']); + foreach ($path_parts as $path_index => $path_part) { + if (!$path_part) { + unset($path_parts[$path_index]); + } + } + + $previous_conf_path = ''; + $previous_script_name = ''; + for ($i = count($path_parts); $i > 0; $i--) { + $_SERVER['SCRIPT_NAME'] = '/' . implode('/', array_slice($path_parts, 0, $i)) . '/index.php'; + $conf_path = conf_path(TRUE, TRUE); + + if ($previous_conf_path && ($conf_path != $previous_conf_path)) { + $_SERVER['SCRIPT_NAME'] = $previous_script_name; + break; + } + + if ($_SERVER['SCRIPT_NAME'] == $original_script_name) { + break; + } + $previous_conf_path = $conf_path; + $previous_script_name = $_SERVER['SCRIPT_NAME']; + } + + conf_path(TRUE, TRUE); +} diff --git a/index.php b/index.php index c7d323b49..ea72099b4 100644 --- a/index.php +++ b/index.php @@ -17,6 +17,8 @@ */ define('DRUPAL_ROOT', getcwd()); +require_once DRUPAL_ROOT . '/includes/unl_bootstrap.inc'; require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; +unl_bootstrap(); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); menu_execute_active_handler(); diff --git a/sites/all/modules/unl/cron.php b/sites/all/modules/unl/cron.php index 671318c25..956b5436a 100644 --- a/sites/all/modules/unl/cron.php +++ b/sites/all/modules/unl/cron.php @@ -18,11 +18,18 @@ while ($row = $query->fetchAssoc()) { ->fields(array('installed' => 1)) ->condition('site_id', $row['site_id']) ->execute(); - unl_add_site($row['site_path'], $row['uri'], $row['clean_url'], $row['db_prefix']); - db_update('unl_sites') - ->fields(array('installed' => 2)) - ->condition('site_id', $row['site_id']) - ->execute(); + if (unl_add_site($row['site_path'], $row['uri'], $row['clean_url'], $row['db_prefix'], $row['site_id'])) { + db_update('unl_sites') + ->fields(array('installed' => 2)) + ->condition('site_id', $row['site_id']) + ->execute(); + } + else { + db_update('unl_sites') + ->fields(array('installed' => 5)) + ->condition('site_id', $row['site_id']) + ->execute(); + } } $query = db_query('SELECT * FROM {unl_sites} WHERE installed=3'); @@ -31,7 +38,7 @@ while ($row = $query->fetchAssoc()) { ->fields(array('installed' => 4)) ->condition('site_id', $row['site_id']) ->execute(); - if (unl_remove_site($row['site_path'], $row['uri'], $row['db_prefix'])) { + if (unl_remove_site($row['site_path'], $row['uri'], $row['db_prefix'], $row['site_id'])) { db_delete('unl_sites') ->condition('site_id', $row['site_id']) ->execute(); @@ -46,7 +53,7 @@ while ($row = $query->fetchAssoc()) { } -function unl_add_site($site_path, $uri, $clean_url, $db_prefix) { +function unl_add_site($site_path, $uri, $clean_url, $db_prefix, $site_id) { if (substr($site_path, 0, 1) == '/') { $site_path = substr($site_path, 1); } @@ -74,37 +81,28 @@ function unl_add_site($site_path, $uri, $clean_url, $db_prefix) { $db_prefix = escapeshellarg($db_prefix); $command = "$php_path sites/all/modules/drush/drush.php -y --uri=$uri site-install unl_profile --sites-subdir=$sites_subdir --db-url=$db_url --db-prefix=$db_prefix --clean-url=$clean_url"; - - $subdir = explode('/', $site_path); - $symlink_name = array_pop($subdir); - $subdir = implode('/', $subdir); - - if ($subdir) { - mkdir($subdir, 0755, TRUE); - } - - $subdir = realpath($subdir); - $subdir_levels = count(explode('/', $subdir)) - count(explode('/', DRUPAL_ROOT)); - - $symlink_target = array(); - for ($i = 0; $i < $subdir_levels; $i++) { - $symlink_target[] = '..'; - } - $symlink_target = implode('/', $symlink_target); + shell_exec($command); - if (!$symlink_target) { - $symlink_target = '.'; + $stub_token = ' # %UNL_CREATION_TOOL_STUB%'; + $htaccess = file_get_contents(DRUPAL_ROOT . '/.htaccess'); + $stub_pos = strpos($htaccess, $stub_token); + if ($stub_pos === FALSE) { + return FALSE; } - - if ($subdir) { - mkdir($subdir, 0755, TRUE); + $new_htaccess = substr($htaccess, 0, $stub_pos) + . " # %UNL_START_SITE_ID_$site_id%\n"; + foreach (array('misc', 'modules', 'sites', 'themes') as $drupal_dir) { + $new_htaccess .= " RewriteRule $site_path/$drupal_dir/(.*) $drupal_dir/$1\n"; } + $new_htaccess .= " # %UNL_END_SITE_ID_$site_id%\n\n" + . $stub_token + . substr($htaccess, $stub_pos + strlen($stub_token)); - symlink($symlink_target, $subdir . '/' . $symlink_name); - shell_exec($command); + file_put_contents(DRUPAL_ROOT . '/.htaccess', $new_htaccess); + return TRUE; } -function unl_remove_site($site_path, $uri, $db_prefix) { +function unl_remove_site($site_path, $uri, $db_prefix, $site_id) { $schema = drupal_get_schema(); $tables = array_keys($schema); sort($tables); @@ -138,24 +136,21 @@ function unl_remove_site($site_path, $uri, $db_prefix) { shell_exec('chmod -R u+w ' . escapeshellarg($sites_subdir)); shell_exec('rm -rf ' . escapeshellarg($sites_subdir)); - // Check to see if this site had any sub-sites. - // If so, we don't want to remove any symlinks. - // TODO: instead, iterate over all sites and rebuild directories/symlinks if needed. - $results = db_select('unl_sites', 's') - ->fields('s', array('uri')) - ->condition('uri', $uri . '/%', 'LIKE') - ->execute() - ->fetchAll(); - if (count($results) > 0) { - return TRUE; - } + // Remove the rewrite rules from .htaccess for this site. + $htaccess = file_get_contents(DRUPAL_ROOT . '/.htaccess'); + $site_start_token = "\n # %UNL_START_SITE_ID_$site_id%"; + $site_end_token = " # %UNL_END_SITE_ID_$site_id%\n"; - // Remove the symlink to the drupal root for this site. - $subdir = explode('/', $site_path); - $symlink_name = array_pop($subdir); - $subdir_levels = count($subdir); - $subdir = implode('/', $subdir); - unlink(DRUPAL_ROOT . '/' . $subdir . '/' . $symlink_name); + $start_pos = strpos($htaccess, $site_start_token); + $end_pos = strpos($htaccess, $site_end_token); + + if ($start_pos === FALSE || $end_pos === FALSE) { + return FALSE; + } + $new_htaccess = substr($htaccess, 0, $start_pos) + . substr($htaccess, $end_pos + strlen($site_end_token)) + ; + file_put_contents(DRUPAL_ROOT . '/.htaccess', $new_htaccess); return TRUE; } -- GitLab