diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index ac50a49..4a9721c 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -716,8 +716,8 @@ function drupal_settings_initialize() { global $base_url, $base_path, $base_root; // Export the following settings.php variables to the global namespace - global $databases, $cookie_domain, $conf, $installed_profile, $update_free_access, $db_url, $db_prefix, $drupal_hash_salt, $is_https, $base_secure_url, $base_insecure_url, $drupal_config_directory_name, $drupal_config_key; - $conf = array(); + global $databases, $cookie_domain, $installed_profile, $update_free_access, $db_url, $db_prefix, $drupal_hash_salt, $is_https, $base_secure_url, $base_insecure_url, $drupal_config_directory_name, $drupal_config_key; + $conf = bootstrap_config('core.base'); if (file_exists(DRUPAL_ROOT . '/' . conf_path() . '/settings.php')) { include_once DRUPAL_ROOT . '/' . conf_path() . '/settings.php'; @@ -792,6 +792,7 @@ function drupal_settings_initialize() { ini_set('session.cookie_secure', TRUE); } $prefix = ini_get('session.cookie_secure') ? 'SSESS' : 'SESS'; + session_name($prefix . substr(hash('sha256', $session_name), 0, 32)); } @@ -892,43 +893,6 @@ function drupal_get_filename($type, $name, $filename = NULL) { } /** - * Load the persistent variable table. - * - * The variable table is composed of values that have been saved in the table - * with variable_set() as well as those explicitly specified in the configuration - * file. - */ -function variable_initialize($conf = array()) { - // NOTE: caching the variables improves performance by 20% when serving - // cached pages. - if ($cached = cache('bootstrap')->get('variables')) { - $variables = $cached->data; - } - else { - // Cache miss. Avoid a stampede. - $name = 'variable_init'; - if (!lock_acquire($name, 1)) { - // Another request is building the variable cache. - // Wait, then re-run this function. - lock_wait($name); - return variable_initialize($conf); - } - else { - // Proceed with variable rebuild. - $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed()); - cache('bootstrap')->set('variables', $variables); - lock_release($name); - } - } - - foreach ($conf as $name => $value) { - $variables[$name] = $value; - } - - return $variables; -} - -/** * Returns a persistent variable. * * Case-sensitivity of the variable_* functions depends on the database @@ -947,9 +911,18 @@ function variable_initialize($conf = array()) { * @see variable_set() */ function variable_get($name, $default = NULL) { - global $conf; - - return isset($conf[$name]) ? $conf[$name] : $default; + // If we try and get variables before the database is initialized we will + // get an error. Let's defend against this by checking our current bootstrap + // phase. + $phase = drupal_bootstrap(NULL, FALSE); + if ($phase > DRUPAL_BOOTSTRAP_DATABASE) { + $config = config('core.variable'); + } + // Fall back on settings from settings.php + else { + $config = config('core.base'); + } + return (isset($config->{$name})) ? $config->{$name} : $default; } /** @@ -969,13 +942,9 @@ function variable_get($name, $default = NULL) { * @see variable_get() */ function variable_set($name, $value) { - global $conf; - - db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute(); - - cache('bootstrap')->delete('variables'); - - $conf[$name] = $value; + $config = config('core.variable'); + $config->{$name} = $value; + $config->save(); } /** @@ -992,14 +961,8 @@ function variable_set($name, $value) { * @see variable_set() */ function variable_del($name) { - global $conf; - - db_delete('variable') - ->condition('name', $name) - ->execute(); - cache('bootstrap')->delete('variables'); - - unset($conf[$name]); + $config = config('core.variable'); + unset($config->{$name}); } /** @@ -2216,7 +2179,7 @@ function _drupal_bootstrap_page_cache() { require_once DRUPAL_ROOT . '/' . $include; } // Check for a cache mode force from settings.php. - if (variable_get('page_cache_without_database')) { + if (variable_get('page_cache_without_database', FALSE)) { $cache_enabled = TRUE; } else { @@ -2241,13 +2204,13 @@ function _drupal_bootstrap_page_cache() { date_default_timezone_set(drupal_get_user_timezone()); // If the skipping of the bootstrap hooks is not enforced, call // hook_boot. - if (variable_get('page_cache_invoke_hooks', TRUE)) { + if (variable_get('page_cache_without_database', FALSE)) { bootstrap_invoke_all('boot'); } drupal_serve_page_from_cache($cache); // If the skipping of the bootstrap hooks is not enforced, call // hook_exit. - if (variable_get('page_cache_invoke_hooks', TRUE)) { + if (variable_get('page_cache_invoke_hooks', FALSE)) { bootstrap_invoke_all('exit'); } // We are done. @@ -2315,14 +2278,10 @@ function _drupal_bootstrap_database() { * Bootstrap variables: Load system variables and all enabled bootstrap modules. */ function _drupal_bootstrap_variables() { - global $conf; - // Initialize the lock system. require_once DRUPAL_ROOT . '/' . variable_get('lock_inc', 'includes/lock.inc'); lock_initialize(); - // Load variables from the database, but do not overwrite variables set in settings.php. - $conf = variable_initialize(isset($conf) ? $conf : array()); // Load bootstrap modules. require_once DRUPAL_ROOT . '/includes/module.inc'; module_load_all(TRUE); diff --git a/includes/config.inc b/includes/config.inc index 64d2c46..ffeaa36 100755 --- a/includes/config.inc +++ b/includes/config.inc @@ -13,7 +13,7 @@ */ function config_get_config_directory() { global $drupal_config_directory_name; - + return conf_path() . '/files/' . $drupal_config_directory_name; } @@ -28,8 +28,8 @@ function config_install_default_config($module) { $drupal_config_dir = config_get_config_directory(); if (is_dir(drupal_get_path('module', $module) . '/config')) { $files = glob($module_config_dir . '/' . '*.xml'); - foreach ($files as $key => $file) { - // Load config data into the active store and write it out to the + foreach ($files as $key => $file) { + // Load config data into the active store and write it out to the // file system in the drupal config directory. Note the config name // needs to be the same as the file name WITHOUT the extension. // @@ -42,7 +42,7 @@ function config_install_default_config($module) { $verified_storage = new DrupalVerifiedStorageSQL($config_name); $verified_storage->write(file_get_contents($module_config_dir . '/' . $file)); } - } + } } /** @@ -53,7 +53,7 @@ function config_install_default_config($module) { * core.entity.node_type.page.xml * * You can pass a prefix 'core.entity.node_type' and get back an array of the - * filenames that match. This allows you to iterate through all files in a + * filenames that match. This allows you to iterate through all files in a * branch. Note that this will only work on the level above the tips, so * a prefix of 'core.entity' would return an empty array. * @@ -72,7 +72,7 @@ function config_get_signed_file_storage_names_with_prefix($prefix = '') { /** * Generate a hash of a config file's contents using our encryption key. - * + * * @param $data * The contents of a configuration file. * @return @@ -81,7 +81,7 @@ function config_get_signed_file_storage_names_with_prefix($prefix = '') { function config_sign_data($data) { // The configuration key is loaded from settings.php and imported into the global namespace global $drupal_config_key; - + // SHA-512 is both secure and very fast on 64 bit CPUs. return hash_hmac('sha512', $data, $drupal_config_key); } @@ -95,16 +95,53 @@ function config_get_names_with_prefix($prefix) { } function config($name, $class = 'DrupalConfig') { + $overrides = config_get_overrides($name); + // @TODO Replace this with the appropriate factory. + return new $class(config_get_verified_storage($name, 'DrupalVerifiedStorageSQL'), config_get_overrides($name)); +} + +/** + * Bootstrap the configuration. + * @param $name + * The name of the bin where we want to store data. + * @return DrupalConfig + * A DrupalConfig object. + */ +function bootstrap_config($name, $class = 'DrupalConfig') { + // @TODO Replace this with the appropriate factory. + return new $class(config_get_verified_storage($name, 'DrupalVerifiedStorageMemory'), config_get_overrides($name)); +} + +function config_get_overrides($name = NULL) { static $overrides; if (!isset($overrides)) { $storage = new SignedFileStorage('local'); $overrides = (array) config_decode($storage->read()); } - $key_overrides = isset($overrides[$name]) ? $overrides[$name] : array(); - // @TODO Replace this with the appropriate factory. - return new $class(new DrupalVerifiedStorageSQL($name), $key_overrides); + if (isset($name)) { + return isset($overrides[$name]) ? $overrides[$name] : array(); + } + return $overrides; +} + +/** + * Create instances of the VerifiedStorage interface. + * @param $name + * The name of the bin to initialize. + * @param $class + * The class to instantiate with that bin. + * @return DrupalVerifiedStorage $storage + * An instantiated storage class. + */ +function config_get_verified_storage($name, $class) { + static $storage_objects = array(); + if (!isset($storage_objects[$name])) { + $storage_objects[$name] = new $class($name); + } + return $storage_objects[$name]; } + /** * Decode configuration data from its native format to an associative array. * @@ -129,7 +166,7 @@ function config_decode($data) { * An associative array or an object * @return * A representation of this array or object in the native configuration - * format. + * format. * @todo * This needs to work for objects as well and currently doesn't */ @@ -145,22 +182,22 @@ function config_encode($data) { * An associative array or an object * @return * A representation of this array or object in the native configuration - * format. + * format. */ function config_array_to_xml($arr, $tab_count = 0) { $xml = ''; if (is_object($arr)) return ''; - foreach ($arr as $tag => $val) { - if (!is_array($val)) { - $xml .= PHP_EOL . '<' . $tag . '>' . htmlentities($val) . ''; - } else { - $tab_count++; - $xml .= PHP_EOL . '<' . $tag . '>' . config_array_to_xml($val, $tab_count); - $xml .= PHP_EOL . ''; - } - } - return $xml; + foreach ($arr as $tag => $val) { + if (!is_array($val)) { + $xml .= PHP_EOL . '<' . $tag . '>' . htmlentities($val) . ''; + } else { + $tab_count++; + $xml .= PHP_EOL . '<' . $tag . '>' . config_array_to_xml($val, $tab_count); + $xml .= PHP_EOL . ''; + } + } + return $xml; } class ConfigException extends Exception {} @@ -208,7 +245,7 @@ class SignedFileStorage { protected function exists() { return file_exists($this->getFilePath()); } - + public function getFilePath() { return config_get_config_directory() . '/' . $this->name . '.xml'; } @@ -361,6 +398,26 @@ class DrupalVerifiedStorageSQL extends DrupalConfigVerifiedStorage { } } +/** + * This Verified Storage does not use the database as it's backend, + * it just stores things in an array. + */ +class DrupalVerifiedStorageMemory extends DrupalConfigVerifiedStorage { + private $data; + public function read() { + return $this->data; + } + + public function writeToActive($data) { + $this->data = $data; + } + + static public function getNamesWithPrefix($prefix = '') { + // This is a naive version for now. I don't see this being used that much. + return array(); + } +} + class DrupalConfig { /** @@ -387,10 +444,10 @@ class DrupalConfig { } public function save() { - $obj = new stdClass(); + $obj = array(); foreach (get_object_vars($this) as $key => $val) { if ($key[0] != '_') { - $obj->$key = $val; + $obj[$key] = $val; } } $this->_verifiedStorage->write(config_encode($obj));