bootstrap.inc
<?php
define('CACHE_PERMANENT', 0);
define('CACHE_TEMPORARY', -1);
define('WATCHDOG_NOTICE', 0);
define('WATCHDOG_WARNING', 1);
define('WATCHDOG_ERROR', 2);
function conf_init() {
static $conf = '';
if ($conf) {
return $conf;
}
$confdir = 'sites';
$uri = explode('/', $_SERVER['PHP_SELF']);
$server = explode('.', rtrim($_SERVER['HTTP_HOST'], '.'));
for ($i = count($uri) - 1; $i > 0; $i--) {
for ($j = count($server); $j > 0; $j--) {
$dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
if (file_exists("$confdir/$dir/settings.php")) {
$conf = "$confdir/$dir";
return $conf;
}
}
}
$conf = "$confdir/default";
return $conf;
}
function drupal_get_filename($type, $name, $filename = NULL) {
static $files = array();
if (!$files[$type]) {
$files[$type] = array();
}
if ($filename && file_exists($filename)) {
$files[$type][$name] = $filename;
}
elseif ($files[$type][$name]) {
}
elseif (($file = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file)) {
$files[$type][$name] = $file;
}
else {
$config = conf_init();
$dir = (($type == 'theme_engine') ? 'themes/engines' : "${type}s");
$file = (($type == 'theme_engine') ? "$name.engine" : "$name.$type");
foreach (array("$config/$dir/$file", "$config/$dir/$name/$file", "$dir/$file", "$dir/$name/$file") as $file) {
if (file_exists($file)) {
$files[$type][$name] = $file;
break;
}
}
}
return $files[$type][$name];
}
function variable_init($conf = array()) {
if ($cached = cache_get('variables')) {
$variables = unserialize($cached->data);
}
else {
$result = db_query('SELECT * FROM {variable}');
while ($variable = db_fetch_object($result)) {
$variables[$variable->name] = unserialize($variable->value);
}
cache_set('variables', serialize($variables));
}
foreach ($conf as $name => $value) {
$variables[$name] = $value;
}
return $variables;
}
function variable_get($name, $default) {
global $conf;
return isset($conf[$name]) ? $conf[$name] : $default;
}
function variable_set($name, $value) {
global $conf;
db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, serialize($value));
cache_clear_all('variables');
$conf[$name] = $value;
}
function variable_del($name) {
global $conf;
db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
cache_clear_all('variables');
unset($conf[$name]);
}
function cache_get($key) {
$cache = db_fetch_object(db_query("SELECT data, created, headers FROM {cache} WHERE cid = '%s'", $key));
if (isset($cache->data)) {
$cache->data = db_decode_blob($cache->data);
return $cache;
}
return 0;
}
function cache_set($cid, $data, $expire = CACHE_PERMANENT, $headers = NULL) {
$data = db_encode_blob($data);
db_query("UPDATE {cache} SET data = '%s', created = %d, expire = %d, headers = '%s' WHERE cid = '%s'", $data, time(), $expire, $headers, $cid);
if (!db_affected_rows()) {
@db_query("INSERT INTO {cache} (cid, data, created, expire, headers) VALUES ('%s', '%s', %d, %d, '%s')", $cid, $data, time(), $expire, $headers);
}
}
function cache_clear_all($cid = NULL, $wildcard = false) {
if (empty($cid)) {
db_query("DELETE FROM {cache} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
}
else {
if ($wildcard) {
db_query("DELETE FROM {cache} WHERE cid LIKE '%%%s%%'", $cid);
}
else {
db_query("DELETE FROM {cache} WHERE cid = '%s'", $cid);
}
}
}
function page_set_cache() {
global $user, $base_url;
if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET') {
if ($data = ob_get_contents()) {
if (function_exists('gzencode')) {
if (version_compare(phpversion(), '4.2', '>=')) {
$data = gzencode($data, 9, FORCE_GZIP);
}
else {
$data = gzencode($data, FORCE_GZIP);
}
}
ob_end_flush();
cache_set($base_url . request_uri(), $data, CACHE_TEMPORARY, drupal_get_headers());
}
}
}
function page_get_cache() {
global $user, $base_url;
$cache = NULL;
if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && count(drupal_set_message()) == 0) {
$cache = cache_get($base_url . request_uri());
if (empty($cache)) {
ob_start();
}
}
return $cache;
}
function bootstrap_invoke_all($op) {
foreach (module_list(FALSE, TRUE) as $module) {
drupal_load('module', $module);
module_invoke($module, $op);
}
}
function drupal_load($type, $name) {
static $files = array();
if ($files[$type][$name]) {
return TRUE;
}
$filename = drupal_get_filename($type, $name);
if ($filename) {
include_once($filename);
$files[$type][$name] = TRUE;
return TRUE;
}
return FALSE;
}
function drupal_get_path_map($action = '') {
static $map = NULL;
if ($action == 'rebuild') {
$map = NULL;
}
if (is_null($map)) {
$map = array(); $result = db_query('SELECT * FROM {url_alias}');
while ($data = db_fetch_object($result)) {
$map[$data->dst] = $data->src;
}
}
return $map;
}
function drupal_get_path_alias($path) {
if (($map = drupal_get_path_map()) && ($newpath = array_search($path, $map))) {
return $newpath;
}
elseif (function_exists('conf_url_rewrite')) {
return conf_url_rewrite($path, 'outgoing');
}
else {
return $path;
}
}
function drupal_get_title() {
$title = drupal_set_title();
if (!isset($title)) {
if (function_exists('menu_get_active_title')) {
$title = check_plain(menu_get_active_title());
}
}
return $title;
}
function drupal_set_title($title = NULL) {
static $stored_title;
if (isset($title)) {
$stored_title = $title;
}
return $stored_title;
}
function drupal_page_header() {
if (variable_get('dev_timer', 0)) {
timer_start();
}
if (variable_get('cache', 0)) {
if ($cache = page_get_cache()) {
bootstrap_invoke_all('init');
$date = gmdate('D, d M Y H:i:s', $cache->created) .' GMT';
$etag = '"'. md5($date) .'"';
$modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $date : NULL;
if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && ($timestamp = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) != -1) {
$modified_since = $cache->created <= $timestamp;
}
else {
$modified_since = NULL;
}
$none_match = !empty($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] == $etag : NULL;
if (($modified_since !== NULL || $none_match !== NULL) && $modified_since !== false && $none_match !== false) {
header('HTTP/1.0 304 Not Modified');
exit();
}
header("Last-Modified: $date");
header("ETag: $etag");
if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false && function_exists('gzencode')) {
$cache->data = gzinflate(substr(substr($cache->data, 10), 0, -8));
}
elseif (function_exists('gzencode')) {
header('Content-Encoding: gzip');
}
$headers = explode("\n", $cache->headers);
foreach ($headers as $header) {
header($header);
}
print $cache->data;
bootstrap_invoke_all('exit');
exit();
}
else {
header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
}
}
function bootstrap_hooks() {
return array('init', 'exit');
}
function drupal_unpack($obj, $field = 'data') {
if ($obj->$field && $data = unserialize($obj->$field)) {
foreach ($data as $key => $value) {
if (!isset($obj->$key)) {
$obj->$key = $value;
}
}
}
return $obj;
}
function referer_uri() {
if (isset($_SERVER['HTTP_REFERER'])) {
return $_SERVER['HTTP_REFERER'];
}
}
function arg($index) {
static $arguments, $q;
if (empty($arguments) || $q != $_GET['q']) {
$arguments = explode('/', $_GET['q']);
}
if (array_key_exists($index, $arguments)) {
return $arguments[$index];
}
}
function request_uri() {
if (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
}
else {
if (isset($_SERVER['argv'])) {
$uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
}
else {
$uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
}
}
return $uri;
}
function timer_start() {
global $timer;
list($usec, $sec) = explode(' ', microtime());
$timer = (float)$usec + (float)$sec;
}
function watchdog($type, $message, $severity = WATCHDOG_NOTICE, $link = NULL) {
global $user;
db_query("INSERT INTO {watchdog} (uid, type, message, severity, link, location, hostname, timestamp) VALUES (%d, '%s', '%s', %d, '%s', '%s', '%s', %d)", $user->uid, $type, $message, $severity, $link, request_uri(), $_SERVER['REMOTE_ADDR'], time());
}
function drupal_set_message($message = NULL, $type = 'status') {
if (isset($message)) {
if (!isset($_SESSION['messages'])) {
$_SESSION['messages'] = array();
}
if (!isset($_SESSION['messages'][$type])) {
$_SESSION['messages'][$type] = array();
}
$_SESSION['messages'][$type][] = $message;
}
return $_SESSION['messages'];
}
function drupal_get_messages() {
$messages = drupal_set_message();
$_SESSION['messages'] = array();
return $messages;
}
function check_plain($text) {
return htmlspecialchars($text, ENT_QUOTES);
}
unset($conf);
$config = conf_init();
include_once "$config/settings.php";
include_once 'includes/database.inc';
include_once 'includes/session.inc';
include_once 'includes/module.inc';
$conf = variable_init(isset($conf) ? $conf : array());
?>