| 1 |
<?php
|
| 2 |
// $Id: domain.bootstrap.inc,v 1.9 2009/06/28 15:23:20 agentken Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Domain bootstrap file.
|
| 6 |
*
|
| 7 |
* The domain bootstrap process is initiated in domain/settings.inc which is
|
| 8 |
* (supposed to be) included in the user's settings.php and therefore initiated
|
| 9 |
* during drupal's first bootstrap phase (DRUPAL_BOOTSTRAP_CONFIGURATION).
|
| 10 |
*
|
| 11 |
* The purpose of this is to allow domain-based modules to modify basic drupal
|
| 12 |
* systems like the variable or database/prefix system - before they are used by
|
| 13 |
* any other modules.
|
| 14 |
*
|
| 15 |
* Note that all functions in this file are _private, by design. You should never
|
| 16 |
* call these functions directly. Public functions are declared in domain.module.
|
| 17 |
*
|
| 18 |
* @ingroup domain
|
| 19 |
*/
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Domain bootstrap phase 1: makes sure that database is initialized and
|
| 23 |
* loads all necessary module files.
|
| 24 |
*/
|
| 25 |
define('DOMAIN_BOOTSTRAP_INIT', 0);
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Domain bootstrap phase 2: resolves host and does a lookup in the {domain}
|
| 29 |
* table. Also invokes hook "hook_domain_bootstrap_lookup".
|
| 30 |
*/
|
| 31 |
define('DOMAIN_BOOTSTRAP_NAME_RESOLVE', 1);
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Domain bootstrap phase 3: invokes bootstrap hook "hook_domain_bootstrap_full".
|
| 35 |
*/
|
| 36 |
define('DOMAIN_BOOTSTRAP_FULL', 2);
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Domain module bootstrap: calls all bootstrap phases.
|
| 40 |
*/
|
| 41 |
function domain_bootstrap() {
|
| 42 |
global $_domain;
|
| 43 |
// Initialize our global variable.
|
| 44 |
$_domain = array();
|
| 45 |
|
| 46 |
$phases = array(DOMAIN_BOOTSTRAP_INIT, DOMAIN_BOOTSTRAP_NAME_RESOLVE, DOMAIN_BOOTSTRAP_FULL);
|
| 47 |
foreach ($phases as $phase) {
|
| 48 |
// We return NULL if the domain module is not enabled. This prevents
|
| 49 |
// load errors during module enable / disable.
|
| 50 |
$error = _domain_bootstrap($phase);
|
| 51 |
if ($error === NULL) {
|
| 52 |
$_domain['error'] = -1;
|
| 53 |
break;
|
| 54 |
}
|
| 55 |
else if ($error === FALSE) {
|
| 56 |
// watchdog() is not available here, and there may be other logging.
|
| 57 |
// So we have to write an error message global variable and pick them up in settings,inc.
|
| 58 |
$_domain['error'] = $phase;
|
| 59 |
break;
|
| 60 |
}
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Calls individual bootstrap phases.
|
| 66 |
*
|
| 67 |
* @param $phase
|
| 68 |
* The domain bootstrap phase to call.
|
| 69 |
*
|
| 70 |
* @return
|
| 71 |
* Returns TRUE if the bootstrap phase was successful and FALSE otherwise.
|
| 72 |
*/
|
| 73 |
function _domain_bootstrap($phase) {
|
| 74 |
global $_domain;
|
| 75 |
switch ($phase) {
|
| 76 |
case DOMAIN_BOOTSTRAP_INIT:
|
| 77 |
// Make sure database is loaded
|
| 78 |
_drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
|
| 79 |
// If the module has been disabled, stop loading.
|
| 80 |
$table = domain_get_primary_table('system');
|
| 81 |
$check = db_result(db_query("SELECT status FROM $table WHERE name = 'domain' AND type = 'module'"));
|
| 82 |
if (empty($check)) {
|
| 83 |
return NULL;
|
| 84 |
}
|
| 85 |
|
| 86 |
// Load bootstrap modules registered by Domain Access.
|
| 87 |
_domain_bootstrap_modules_load();
|
| 88 |
|
| 89 |
// If essential core module file has not been loaded, bootstrap fails.
|
| 90 |
if (!function_exists('domain_load')) {
|
| 91 |
return FALSE;
|
| 92 |
}
|
| 93 |
break;
|
| 94 |
|
| 95 |
case DOMAIN_BOOTSTRAP_NAME_RESOLVE:
|
| 96 |
// Get the domain_id of the request.
|
| 97 |
$_domain = domain_resolve_host();
|
| 98 |
// If we don't have a valid domain id now, we can't really go on, bootstrap fails.
|
| 99 |
if (empty($_domain) || !is_numeric($_domain['domain_id'])) {
|
| 100 |
return FALSE;
|
| 101 |
}
|
| 102 |
break;
|
| 103 |
|
| 104 |
case DOMAIN_BOOTSTRAP_FULL:
|
| 105 |
// Make sure the load process worked correctly before invoking the hook.
|
| 106 |
if (!domain_settings_setup_ok()) {
|
| 107 |
return FALSE;
|
| 108 |
}
|
| 109 |
_domain_bootstrap_invoke_all('full', $_domain);
|
| 110 |
break;
|
| 111 |
}
|
| 112 |
return TRUE;
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Returns a list of modules which are loaded during domain_bootstrap phases.
|
| 117 |
*
|
| 118 |
* The domain module is always in the list of modules and has weight -99 so it
|
| 119 |
* should usually be first one as well.
|
| 120 |
*
|
| 121 |
* @param $reset
|
| 122 |
* If set to TRUE the cached list of modules is updated with the value from the
|
| 123 |
* {variable} table again. Default value is FALSE. Optional.
|
| 124 |
*
|
| 125 |
* @return
|
| 126 |
* An array of module names.
|
| 127 |
*/
|
| 128 |
function _domain_bootstrap_modules($reset = FALSE) {
|
| 129 |
static $modules = NULL;
|
| 130 |
if ($reset || is_null($modules)) {
|
| 131 |
$modules = _domain_bootstrap_modules_get();
|
| 132 |
if (!is_array($modules)) {
|
| 133 |
$modules = array();
|
| 134 |
}
|
| 135 |
if (!in_array('domain', $modules)) {
|
| 136 |
$modules['-99'] = 'domain';
|
| 137 |
}
|
| 138 |
ksort($modules);
|
| 139 |
}
|
| 140 |
return $modules;
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* Tries to load all domain bootstrap modules.
|
| 145 |
* @see _domain_bootstrap_modules()
|
| 146 |
*/
|
| 147 |
function _domain_bootstrap_modules_load() {
|
| 148 |
$modules = _domain_bootstrap_modules();
|
| 149 |
foreach ($modules as $module) {
|
| 150 |
drupal_load('module', $module);
|
| 151 |
}
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
| 155 |
* Retrieves the value of the variable 'domain_bootstrap_modules' from the
|
| 156 |
* {variable} table. This function does not use Drupal's variable system.
|
| 157 |
*
|
| 158 |
* @return
|
| 159 |
* An array containing module names.
|
| 160 |
*/
|
| 161 |
function _domain_bootstrap_modules_get() {
|
| 162 |
global $conf;
|
| 163 |
$key = 'domain_bootstrap_modules';
|
| 164 |
$table = domain_get_primary_table('variable');
|
| 165 |
$conf[$key] = unserialize(db_result(db_query("SELECT value FROM $table WHERE name = '%s'", $key)));
|
| 166 |
return $conf[$key];
|
| 167 |
}
|
| 168 |
|
| 169 |
/**
|
| 170 |
* Tries to call specified hook on all domain_bootstrap modules.
|
| 171 |
*
|
| 172 |
* The hook function names are of the following format:
|
| 173 |
* {$module}_domain_bootstrap_{$hook} where {$module}
|
| 174 |
* is the name of the module implementing the hook and {$hook}
|
| 175 |
* is the identifier for the concrete domain bootstrap hook.
|
| 176 |
*
|
| 177 |
* This function is basically a copy of module_invoke_all() adjusted to our
|
| 178 |
* needs.
|
| 179 |
*
|
| 180 |
* @param $hook
|
| 181 |
* The name of the bootstrap hook to invoke.
|
| 182 |
*
|
| 183 |
* ... Arguments to pass to the hook.
|
| 184 |
*
|
| 185 |
* @link http://api.drupal.org/api/function/module_invoke_all/6
|
| 186 |
*/
|
| 187 |
function _domain_bootstrap_invoke_all() {
|
| 188 |
$args = func_get_args();
|
| 189 |
$hook = $args[0];
|
| 190 |
unset($args[0]);
|
| 191 |
$return = array();
|
| 192 |
foreach (_domain_bootstrap_modules() as $module) {
|
| 193 |
$function = $module .'_domain_bootstrap_'. $hook;
|
| 194 |
if (function_exists($function)) {
|
| 195 |
$result = call_user_func_array($function, $args);
|
| 196 |
if (isset($result) && is_array($result)) {
|
| 197 |
$return = array_merge_recursive($return, $result);
|
| 198 |
}
|
| 199 |
else if (isset($result)) {
|
| 200 |
$return[] = $result;
|
| 201 |
}
|
| 202 |
}
|
| 203 |
}
|
| 204 |
return $return;
|
| 205 |
}
|
| 206 |
|
| 207 |
/**
|
| 208 |
* Escape the names of the default tables for variable lookups.
|
| 209 |
*
|
| 210 |
* There are a few cases where we must directly query data from the
|
| 211 |
* primary site's database. Due to table prefixing and Domain Prefix, we
|
| 212 |
* must ensure that we are querying the correct table.
|
| 213 |
*
|
| 214 |
* When using this function, you should insert the $table result directly
|
| 215 |
* into your query string, or use token replacement '%s' syntax. Do not
|
| 216 |
* enclose the table name in brackets {}, as that defeats the purpose of
|
| 217 |
* this function.
|
| 218 |
*
|
| 219 |
* @see _domain_conf_load_primary()
|
| 220 |
*
|
| 221 |
* @param $table
|
| 222 |
* The name of the base table to lookup.
|
| 223 |
* @return
|
| 224 |
* A query-safe table name pointing to the primary domain.
|
| 225 |
*/
|
| 226 |
function domain_get_primary_table($table) {
|
| 227 |
global $db_prefix;
|
| 228 |
if (is_string($db_prefix)) {
|
| 229 |
$table = $db_prefix . $table;
|
| 230 |
}
|
| 231 |
else if (is_array($db_prefix)) {
|
| 232 |
$table = $db_prefix['default'] . $table;
|
| 233 |
}
|
| 234 |
return db_escape_table($table);
|
| 235 |
}
|
| 236 |
|