webmaster@1: 0; $i--) { webmaster@1: for ($j = count($server); $j > 0; $j--) { webmaster@1: $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i)); webmaster@1: if (file_exists("$confdir/$dir/settings.php") || (!$require_settings && file_exists("$confdir/$dir"))) { webmaster@1: $conf = "$confdir/$dir"; webmaster@1: return $conf; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: $conf = "$confdir/default"; webmaster@1: return $conf; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Unsets all disallowed global variables. See $allowed for what's allowed. webmaster@1: */ webmaster@1: function drupal_unset_globals() { webmaster@1: if (ini_get('register_globals')) { webmaster@1: $allowed = array('_ENV' => 1, '_GET' => 1, '_POST' => 1, '_COOKIE' => 1, '_FILES' => 1, '_SERVER' => 1, '_REQUEST' => 1, 'GLOBALS' => 1); webmaster@1: foreach ($GLOBALS as $key => $value) { webmaster@1: if (!isset($allowed[$key])) { webmaster@1: unset($GLOBALS[$key]); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Loads the configuration and sets the base URL, cookie domain, and webmaster@1: * session name correctly. webmaster@1: */ webmaster@1: function conf_init() { webmaster@1: global $base_url, $base_path, $base_root; webmaster@1: webmaster@1: // Export the following settings.php variables to the global namespace webmaster@1: global $db_url, $db_prefix, $cookie_domain, $conf, $installed_profile, $update_free_access; webmaster@1: $conf = array(); webmaster@1: webmaster@1: if (file_exists('./'. conf_path() .'/settings.php')) { webmaster@1: include_once './'. conf_path() .'/settings.php'; webmaster@1: } webmaster@1: webmaster@7: // Ignore the placeholder url from default.settings.php. webmaster@7: if (isset($db_url) && $db_url == 'mysql://username:password@localhost/databasename') { webmaster@7: $db_url = ''; webmaster@7: } webmaster@7: webmaster@1: if (isset($base_url)) { webmaster@1: // Parse fixed base URL from settings.php. webmaster@1: $parts = parse_url($base_url); webmaster@1: if (!isset($parts['path'])) { webmaster@1: $parts['path'] = ''; webmaster@1: } webmaster@1: $base_path = $parts['path'] .'/'; webmaster@1: // Build $base_root (everything until first slash after "scheme://"). webmaster@1: $base_root = substr($base_url, 0, strlen($base_url) - strlen($parts['path'])); webmaster@1: } webmaster@1: else { webmaster@1: // Create base URL webmaster@1: $base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http'; webmaster@1: webmaster@1: // As $_SERVER['HTTP_HOST'] is user input, ensure it only contains webmaster@1: // characters allowed in hostnames. webmaster@1: $base_url = $base_root .= '://'. preg_replace('/[^a-z0-9-:._]/i', '', $_SERVER['HTTP_HOST']); webmaster@1: webmaster@1: // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'], not webmaster@1: // be modified by a visitor. webmaster@1: if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) { webmaster@1: $base_path = "/$dir"; webmaster@1: $base_url .= $base_path; webmaster@1: $base_path .= '/'; webmaster@1: } webmaster@1: else { webmaster@1: $base_path = '/'; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: if ($cookie_domain) { webmaster@1: // If the user specifies the cookie domain, also use it for session name. webmaster@1: $session_name = $cookie_domain; webmaster@1: } webmaster@1: else { webmaster@1: // Otherwise use $base_url as session name, without the protocol webmaster@1: // to use the same session identifiers across http and https. webmaster@1: list( , $session_name) = explode('://', $base_url, 2); webmaster@1: // We escape the hostname because it can be modified by a visitor. webmaster@1: if (!empty($_SERVER['HTTP_HOST'])) { webmaster@1: $cookie_domain = check_plain($_SERVER['HTTP_HOST']); webmaster@1: } webmaster@1: } webmaster@1: // Strip leading periods, www., and port numbers from cookie domain. webmaster@1: $cookie_domain = ltrim($cookie_domain, '.'); webmaster@1: if (strpos($cookie_domain, 'www.') === 0) { webmaster@1: $cookie_domain = substr($cookie_domain, 4); webmaster@1: } webmaster@1: $cookie_domain = explode(':', $cookie_domain); webmaster@1: $cookie_domain = '.'. $cookie_domain[0]; webmaster@1: // Per RFC 2109, cookie domains must contain at least one dot other than the webmaster@1: // first. For hosts such as 'localhost' or IP Addresses we don't set a cookie domain. webmaster@1: if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) { webmaster@1: ini_set('session.cookie_domain', $cookie_domain); webmaster@1: } webmaster@1: session_name('SESS'. md5($session_name)); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Returns and optionally sets the filename for a system item (module, webmaster@1: * theme, etc.). The filename, whether provided, cached, or retrieved webmaster@1: * from the database, is only returned if the file exists. webmaster@1: * webmaster@1: * This function plays a key role in allowing Drupal's resources (modules webmaster@1: * and themes) to be located in different places depending on a site's webmaster@1: * configuration. For example, a module 'foo' may legally be be located webmaster@1: * in any of these three places: webmaster@1: * webmaster@1: * modules/foo/foo.module webmaster@1: * sites/all/modules/foo/foo.module webmaster@1: * sites/example.com/modules/foo/foo.module webmaster@1: * webmaster@1: * Calling drupal_get_filename('module', 'foo') will give you one of webmaster@1: * the above, depending on where the module is located. webmaster@1: * webmaster@1: * @param $type webmaster@1: * The type of the item (i.e. theme, theme_engine, module). webmaster@1: * @param $name webmaster@1: * The name of the item for which the filename is requested. webmaster@1: * @param $filename webmaster@1: * The filename of the item if it is to be set explicitly rather webmaster@1: * than by consulting the database. webmaster@1: * webmaster@1: * @return webmaster@1: * The filename of the requested item. webmaster@1: */ webmaster@1: function drupal_get_filename($type, $name, $filename = NULL) { webmaster@1: static $files = array(); webmaster@1: webmaster@1: if (!isset($files[$type])) { webmaster@1: $files[$type] = array(); webmaster@1: } webmaster@1: webmaster@1: if (!empty($filename) && file_exists($filename)) { webmaster@1: $files[$type][$name] = $filename; webmaster@1: } webmaster@1: elseif (isset($files[$type][$name])) { webmaster@1: // nothing webmaster@1: } webmaster@1: // Verify that we have an active database connection, before querying webmaster@1: // the database. This is required because this function is called both webmaster@1: // before we have a database connection (i.e. during installation) and webmaster@1: // when a database connection fails. webmaster@1: elseif (db_is_active() && (($file = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file))) { webmaster@1: $files[$type][$name] = $file; webmaster@1: } webmaster@1: else { webmaster@1: // Fallback to searching the filesystem if the database connection is webmaster@1: // not established or the requested file is not found. webmaster@1: $config = conf_path(); webmaster@1: $dir = (($type == 'theme_engine') ? 'themes/engines' : "${type}s"); webmaster@1: $file = (($type == 'theme_engine') ? "$name.engine" : "$name.$type"); webmaster@1: webmaster@1: foreach (array("$config/$dir/$file", "$config/$dir/$name/$file", "$dir/$file", "$dir/$name/$file") as $file) { webmaster@1: if (file_exists($file)) { webmaster@1: $files[$type][$name] = $file; webmaster@1: break; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: if (isset($files[$type][$name])) { webmaster@1: return $files[$type][$name]; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Load the persistent variable table. webmaster@1: * webmaster@1: * The variable table is composed of values that have been saved in the table webmaster@1: * with variable_set() as well as those explicitly specified in the configuration webmaster@1: * file. webmaster@1: */ webmaster@1: function variable_init($conf = array()) { webmaster@1: // NOTE: caching the variables improves performance by 20% when serving cached pages. webmaster@1: if ($cached = cache_get('variables', 'cache')) { webmaster@1: $variables = $cached->data; webmaster@1: } webmaster@1: else { webmaster@1: $result = db_query('SELECT * FROM {variable}'); webmaster@1: while ($variable = db_fetch_object($result)) { webmaster@1: $variables[$variable->name] = unserialize($variable->value); webmaster@1: } webmaster@1: cache_set('variables', $variables); webmaster@1: } webmaster@1: webmaster@1: foreach ($conf as $name => $value) { webmaster@1: $variables[$name] = $value; webmaster@1: } webmaster@1: webmaster@1: return $variables; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Return a persistent variable. webmaster@1: * webmaster@1: * @param $name webmaster@1: * The name of the variable to return. webmaster@1: * @param $default webmaster@1: * The default value to use if this variable has never been set. webmaster@1: * @return webmaster@1: * The value of the variable. webmaster@1: */ webmaster@1: function variable_get($name, $default) { webmaster@1: global $conf; webmaster@1: webmaster@1: return isset($conf[$name]) ? $conf[$name] : $default; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Set a persistent variable. webmaster@1: * webmaster@1: * @param $name webmaster@1: * The name of the variable to set. webmaster@1: * @param $value webmaster@1: * The value to set. This can be any PHP data type; these functions take care webmaster@1: * of serialization as necessary. webmaster@1: */ webmaster@1: function variable_set($name, $value) { webmaster@1: global $conf; webmaster@1: webmaster@1: $serialized_value = serialize($value); webmaster@1: db_query("UPDATE {variable} SET value = '%s' WHERE name = '%s'", $serialized_value, $name); webmaster@1: if (!db_affected_rows()) { webmaster@1: @db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $serialized_value); webmaster@1: } webmaster@1: webmaster@1: cache_clear_all('variables', 'cache'); webmaster@1: webmaster@1: $conf[$name] = $value; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Unset a persistent variable. webmaster@1: * webmaster@1: * @param $name webmaster@1: * The name of the variable to undefine. webmaster@1: */ webmaster@1: function variable_del($name) { webmaster@1: global $conf; webmaster@1: webmaster@1: db_query("DELETE FROM {variable} WHERE name = '%s'", $name); webmaster@1: cache_clear_all('variables', 'cache'); webmaster@1: webmaster@1: unset($conf[$name]); webmaster@1: } webmaster@1: webmaster@1: webmaster@1: /** webmaster@1: * Retrieve the current page from the cache. webmaster@1: * webmaster@1: * Note: we do not serve cached pages when status messages are waiting (from webmaster@1: * a redirected form submission which was completed). webmaster@1: * webmaster@1: * @param $status_only webmaster@1: * When set to TRUE, retrieve the status of the page cache only webmaster@1: * (whether it was started in this request or not). webmaster@1: */ webmaster@1: function page_get_cache($status_only = FALSE) { webmaster@1: static $status = FALSE; webmaster@1: global $user, $base_root; webmaster@1: webmaster@1: if ($status_only) { webmaster@1: return $status; webmaster@1: } webmaster@1: $cache = NULL; webmaster@1: webmaster@1: if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && count(drupal_set_message()) == 0) { webmaster@1: $cache = cache_get($base_root . request_uri(), 'cache_page'); webmaster@1: webmaster@1: if (empty($cache)) { webmaster@1: ob_start(); webmaster@1: $status = TRUE; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return $cache; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Call all init or exit hooks without including all modules. webmaster@1: * webmaster@1: * @param $hook webmaster@1: * The name of the bootstrap hook we wish to invoke. webmaster@1: */ webmaster@1: function bootstrap_invoke_all($hook) { webmaster@1: foreach (module_list(TRUE, TRUE) as $module) { webmaster@1: drupal_load('module', $module); webmaster@1: module_invoke($module, $hook); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Includes a file with the provided type and name. This prevents webmaster@1: * including a theme, engine, module, etc., more than once. webmaster@1: * webmaster@1: * @param $type webmaster@1: * The type of item to load (i.e. theme, theme_engine, module). webmaster@1: * @param $name webmaster@1: * The name of the item to load. webmaster@1: * webmaster@1: * @return webmaster@1: * TRUE if the item is loaded or has already been loaded. webmaster@1: */ webmaster@1: function drupal_load($type, $name) { webmaster@1: static $files = array(); webmaster@1: webmaster@1: if (isset($files[$type][$name])) { webmaster@1: return TRUE; webmaster@1: } webmaster@1: webmaster@1: $filename = drupal_get_filename($type, $name); webmaster@1: webmaster@1: if ($filename) { webmaster@1: include_once "./$filename"; webmaster@1: $files[$type][$name] = TRUE; webmaster@1: webmaster@1: return TRUE; webmaster@1: } webmaster@1: webmaster@1: return FALSE; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Set HTTP headers in preparation for a page response. webmaster@1: * webmaster@1: * Authenticated users are always given a 'no-cache' header, and will webmaster@1: * fetch a fresh page on every request. This prevents authenticated webmaster@1: * users seeing locally cached pages that show them as logged out. webmaster@1: * webmaster@1: * @see page_set_cache() webmaster@1: */ webmaster@1: function drupal_page_header() { webmaster@1: header("Expires: Sun, 19 Nov 1978 05:00:00 GMT"); webmaster@1: header("Last-Modified: ". gmdate("D, d M Y H:i:s") ." GMT"); webmaster@1: header("Cache-Control: store, no-cache, must-revalidate"); webmaster@1: header("Cache-Control: post-check=0, pre-check=0", FALSE); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Set HTTP headers in preparation for a cached page response. webmaster@1: * webmaster@1: * The general approach here is that anonymous users can keep a local webmaster@1: * cache of the page, but must revalidate it on every request. Then, webmaster@1: * they are given a '304 Not Modified' response as long as they stay webmaster@1: * logged out and the page has not been modified. webmaster@1: * webmaster@1: */ webmaster@1: function drupal_page_cache_header($cache) { webmaster@1: // Set default values: webmaster@1: $last_modified = gmdate('D, d M Y H:i:s', $cache->created) .' GMT'; webmaster@1: $etag = '"'. md5($last_modified) .'"'; webmaster@1: webmaster@1: // See if the client has provided the required HTTP headers: webmaster@1: $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE; webmaster@1: $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE; webmaster@1: webmaster@1: if ($if_modified_since && $if_none_match webmaster@1: && $if_none_match == $etag // etag must match webmaster@1: && $if_modified_since == $last_modified) { // if-modified-since must match webmaster@1: header('HTTP/1.1 304 Not Modified'); webmaster@1: // All 304 responses must send an etag if the 200 response for the same object contained an etag webmaster@1: header("Etag: $etag"); webmaster@1: exit(); webmaster@1: } webmaster@1: webmaster@1: // Send appropriate response: webmaster@1: header("Last-Modified: $last_modified"); webmaster@1: header("ETag: $etag"); webmaster@1: webmaster@1: // The following headers force validation of cache: webmaster@1: header("Expires: Sun, 19 Nov 1978 05:00:00 GMT"); webmaster@1: header("Cache-Control: must-revalidate"); webmaster@1: webmaster@1: if (variable_get('page_compression', TRUE)) { webmaster@1: // Determine if the browser accepts gzipped data. webmaster@1: if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === FALSE && function_exists('gzencode')) { webmaster@1: // Strip the gzip header and run uncompress. webmaster@1: $cache->data = gzinflate(substr(substr($cache->data, 10), 0, -8)); webmaster@1: } webmaster@1: elseif (function_exists('gzencode')) { webmaster@1: header('Content-Encoding: gzip'); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Send the original request's headers. We send them one after webmaster@1: // another so PHP's header() function can deal with duplicate webmaster@1: // headers. webmaster@1: $headers = explode("\n", $cache->headers); webmaster@1: foreach ($headers as $header) { webmaster@1: header($header); webmaster@1: } webmaster@1: webmaster@1: print $cache->data; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Define the critical hooks that force modules to always be loaded. webmaster@1: */ webmaster@1: function bootstrap_hooks() { webmaster@1: return array('boot', 'exit'); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Unserializes and appends elements from a serialized string. webmaster@1: * webmaster@1: * @param $obj webmaster@1: * The object to which the elements are appended. webmaster@1: * @param $field webmaster@1: * The attribute of $obj whose value should be unserialized. webmaster@1: */ webmaster@1: function drupal_unpack($obj, $field = 'data') { webmaster@1: if ($obj->$field && $data = unserialize($obj->$field)) { webmaster@1: foreach ($data as $key => $value) { webmaster@1: if (!isset($obj->$key)) { webmaster@1: $obj->$key = $value; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: return $obj; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Return the URI of the referring page. webmaster@1: */ webmaster@1: function referer_uri() { webmaster@1: if (isset($_SERVER['HTTP_REFERER'])) { webmaster@1: return $_SERVER['HTTP_REFERER']; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Encode special characters in a plain-text string for display as HTML. webmaster@1: * webmaster@1: * Uses drupal_validate_utf8 to prevent cross site scripting attacks on webmaster@1: * Internet Explorer 6. webmaster@1: */ webmaster@1: function check_plain($text) { webmaster@1: return drupal_validate_utf8($text) ? htmlspecialchars($text, ENT_QUOTES) : ''; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Checks whether a string is valid UTF-8. webmaster@1: * webmaster@1: * All functions designed to filter input should use drupal_validate_utf8 webmaster@1: * to ensure they operate on valid UTF-8 strings to prevent bypass of the webmaster@1: * filter. webmaster@1: * webmaster@1: * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented webmaster@1: * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent webmaster@1: * bytes. When these subsequent bytes are HTML control characters such as webmaster@1: * quotes or angle brackets, parts of the text that were deemed safe by filters webmaster@1: * end up in locations that are potentially unsafe; An onerror attribute that webmaster@1: * is outside of a tag, and thus deemed safe by a filter, can be interpreted webmaster@1: * by the browser as if it were inside the tag. webmaster@1: * webmaster@1: * This function exploits preg_match behaviour (since PHP 4.3.5) when used webmaster@1: * with the u modifier, as a fast way to find invalid UTF-8. When the matched webmaster@1: * string contains an invalid byte sequence, it will fail silently. webmaster@1: * webmaster@1: * preg_match may not fail on 4 and 5 octet sequences, even though they webmaster@1: * are not supported by the specification. webmaster@1: * webmaster@1: * The specific preg_match behaviour is present since PHP 4.3.5. webmaster@1: * webmaster@1: * @param $text webmaster@1: * The text to check. webmaster@1: * @return webmaster@1: * TRUE if the text is valid UTF-8, FALSE if not. webmaster@1: */ webmaster@1: function drupal_validate_utf8($text) { webmaster@1: if (strlen($text) == 0) { webmaster@1: return TRUE; webmaster@1: } webmaster@1: return (preg_match('/^./us', $text) == 1); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Since $_SERVER['REQUEST_URI'] is only available on Apache, we webmaster@1: * generate an equivalent using other environment variables. webmaster@1: */ webmaster@1: function request_uri() { webmaster@1: webmaster@1: if (isset($_SERVER['REQUEST_URI'])) { webmaster@1: $uri = $_SERVER['REQUEST_URI']; webmaster@1: } webmaster@1: else { webmaster@1: if (isset($_SERVER['argv'])) { webmaster@1: $uri = $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['argv'][0]; webmaster@1: } webmaster@1: elseif (isset($_SERVER['QUERY_STRING'])) { webmaster@1: $uri = $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING']; webmaster@1: } webmaster@1: else { webmaster@1: $uri = $_SERVER['SCRIPT_NAME']; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return $uri; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Log a system message. webmaster@1: * webmaster@1: * @param $type webmaster@1: * The category to which this message belongs. webmaster@1: * @param $message webmaster@1: * The message to store in the log. See t() for documentation webmaster@1: * on how $message and $variables interact. Keep $message webmaster@1: * translatable by not concatenating dynamic values into it! webmaster@1: * @param $variables webmaster@1: * Array of variables to replace in the message on display or webmaster@1: * NULL if message is already translated or not possible to webmaster@1: * translate. webmaster@1: * @param $severity webmaster@1: * The severity of the message, as per RFC 3164 webmaster@1: * @param $link webmaster@1: * A link to associate with the message. webmaster@1: * webmaster@1: * @see watchdog_severity_levels() webmaster@1: */ webmaster@1: function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) { webmaster@1: global $user, $base_root; webmaster@1: webmaster@1: // Prepare the fields to be logged webmaster@1: $log_message = array( webmaster@1: 'type' => $type, webmaster@1: 'message' => $message, webmaster@1: 'variables' => $variables, webmaster@1: 'severity' => $severity, webmaster@1: 'link' => $link, webmaster@1: 'user' => $user, webmaster@1: 'request_uri' => $base_root . request_uri(), webmaster@1: 'referer' => referer_uri(), webmaster@1: 'ip' => ip_address(), webmaster@1: 'timestamp' => time(), webmaster@1: ); webmaster@1: webmaster@1: // Call the logging hooks to log/process the message webmaster@1: foreach (module_implements('watchdog', TRUE) as $module) { webmaster@1: module_invoke($module, 'watchdog', $log_message); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Set a message which reflects the status of the performed operation. webmaster@1: * webmaster@1: * If the function is called with no arguments, this function returns all set webmaster@1: * messages without clearing them. webmaster@1: * webmaster@1: * @param $message webmaster@1: * The message should begin with a capital letter and always ends with a webmaster@1: * period '.'. webmaster@1: * @param $type webmaster@1: * The type of the message. One of the following values are possible: webmaster@1: * - 'status' webmaster@1: * - 'warning' webmaster@1: * - 'error' webmaster@1: * @param $repeat webmaster@1: * If this is FALSE and the message is already set, then the message won't webmaster@1: * be repeated. webmaster@1: */ webmaster@1: function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) { webmaster@1: if ($message) { webmaster@1: if (!isset($_SESSION['messages'])) { webmaster@1: $_SESSION['messages'] = array(); webmaster@1: } webmaster@1: webmaster@1: if (!isset($_SESSION['messages'][$type])) { webmaster@1: $_SESSION['messages'][$type] = array(); webmaster@1: } webmaster@1: webmaster@1: if ($repeat || !in_array($message, $_SESSION['messages'][$type])) { webmaster@1: $_SESSION['messages'][$type][] = $message; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // messages not set when DB connection fails webmaster@1: return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Return all messages that have been set. webmaster@1: * webmaster@1: * @param $type webmaster@1: * (optional) Only return messages of this type. webmaster@1: * @param $clear_queue webmaster@1: * (optional) Set to FALSE if you do not want to clear the messages queue webmaster@1: * @return webmaster@1: * An associative array, the key is the message type, the value an array webmaster@1: * of messages. If the $type parameter is passed, you get only that type, webmaster@1: * or an empty array if there are no such messages. If $type is not passed, webmaster@1: * all message types are returned, or an empty array if none exist. webmaster@1: */ webmaster@1: function drupal_get_messages($type = NULL, $clear_queue = TRUE) { webmaster@1: if ($messages = drupal_set_message()) { webmaster@1: if ($type) { webmaster@1: if ($clear_queue) { webmaster@1: unset($_SESSION['messages'][$type]); webmaster@1: } webmaster@1: if (isset($messages[$type])) { webmaster@1: return array($type => $messages[$type]); webmaster@1: } webmaster@1: } webmaster@1: else { webmaster@1: if ($clear_queue) { webmaster@1: unset($_SESSION['messages']); webmaster@1: } webmaster@1: return $messages; webmaster@1: } webmaster@1: } webmaster@1: return array(); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Perform an access check for a given mask and rule type. Rules are usually webmaster@1: * created via admin/user/rules page. webmaster@1: * webmaster@1: * If any allow rule matches, access is allowed. Otherwise, if any deny rule webmaster@1: * matches, access is denied. If no rule matches, access is allowed. webmaster@1: * webmaster@1: * @param $type string webmaster@1: * Type of access to check: Allowed values are: webmaster@1: * - 'host': host name or IP address webmaster@1: * - 'mail': e-mail address webmaster@1: * - 'user': username webmaster@1: * @param $mask string webmaster@1: * String or mask to test: '_' matches any character, '%' matches any webmaster@1: * number of characters. webmaster@1: * @return bool webmaster@1: * TRUE if access is denied, FALSE if access is allowed. webmaster@1: */ webmaster@1: function drupal_is_denied($type, $mask) { webmaster@1: // Because this function is called for every page request, both cached webmaster@1: // and non-cached pages, we tried to optimize it as much as possible. webmaster@1: // We deny access if the only matching records in the {access} table have webmaster@1: // status 0 (deny). If any have status 1 (allow), or if there are no webmaster@1: // matching records, we allow access. webmaster@1: $sql = "SELECT 1 FROM {access} WHERE type = '%s' AND LOWER('%s') LIKE LOWER(mask) AND status = %d"; webmaster@1: return db_result(db_query_range($sql, $type, $mask, 0, 0, 1)) && !db_result(db_query_range($sql, $type, $mask, 1, 0, 1)); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Generates a default anonymous $user object. webmaster@1: * webmaster@1: * @return Object - the user object. webmaster@1: */ webmaster@1: function drupal_anonymous_user($session = '') { webmaster@1: $user = new stdClass(); webmaster@1: $user->uid = 0; webmaster@1: $user->hostname = ip_address(); webmaster@1: $user->roles = array(); webmaster@1: $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; webmaster@1: $user->session = $session; webmaster@1: $user->cache = 0; webmaster@1: return $user; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * A string describing a phase of Drupal to load. Each phase adds to the webmaster@1: * previous one, so invoking a later phase automatically runs the earlier webmaster@1: * phases too. The most important usage is that if you want to access the webmaster@1: * Drupal database from a script without loading anything else, you can webmaster@1: * include bootstrap.inc, and call drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE). webmaster@1: * webmaster@1: * @param $phase webmaster@1: * A constant. Allowed values are: webmaster@1: * DRUPAL_BOOTSTRAP_CONFIGURATION: initialize configuration. webmaster@1: * DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE: try to call a non-database cache fetch routine. webmaster@1: * DRUPAL_BOOTSTRAP_DATABASE: initialize database layer. webmaster@1: * DRUPAL_BOOTSTRAP_ACCESS: identify and reject banned hosts. webmaster@1: * DRUPAL_BOOTSTRAP_SESSION: initialize session handling. webmaster@1: * DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: load bootstrap.inc and module.inc, start webmaster@1: * the variable system and try to serve a page from the cache. webmaster@1: * DRUPAL_BOOTSTRAP_LANGUAGE: identify the language used on the page. webmaster@1: * DRUPAL_BOOTSTRAP_PATH: set $_GET['q'] to Drupal path of request. webmaster@1: * DRUPAL_BOOTSTRAP_FULL: Drupal is fully loaded, validate and fix input data. webmaster@1: */ webmaster@1: function drupal_bootstrap($phase) { webmaster@1: static $phases = array(DRUPAL_BOOTSTRAP_CONFIGURATION, DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE, DRUPAL_BOOTSTRAP_DATABASE, DRUPAL_BOOTSTRAP_ACCESS, DRUPAL_BOOTSTRAP_SESSION, DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE, DRUPAL_BOOTSTRAP_LANGUAGE, DRUPAL_BOOTSTRAP_PATH, DRUPAL_BOOTSTRAP_FULL), $phase_index = 0; webmaster@1: webmaster@1: while ($phase >= $phase_index && isset($phases[$phase_index])) { webmaster@1: $current_phase = $phases[$phase_index]; webmaster@1: unset($phases[$phase_index++]); webmaster@1: _drupal_bootstrap($current_phase); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: function _drupal_bootstrap($phase) { webmaster@1: global $conf; webmaster@1: webmaster@1: switch ($phase) { webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_CONFIGURATION: webmaster@1: drupal_unset_globals(); webmaster@1: // Start a page timer: webmaster@1: timer_start('page'); webmaster@1: // Initialize the configuration webmaster@1: conf_init(); webmaster@1: break; webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE: webmaster@1: // Allow specifying special cache handlers in settings.php, like webmaster@1: // using memcached or files for storing cache information. webmaster@1: require_once variable_get('cache_inc', './includes/cache.inc'); webmaster@1: // If the page_cache_fastpath is set to TRUE in settings.php and webmaster@1: // page_cache_fastpath (implemented in the special implementation of webmaster@1: // cache.inc) printed the page and indicated this with a returned TRUE webmaster@1: // then we are done. webmaster@1: if (variable_get('page_cache_fastpath', FALSE) && page_cache_fastpath()) { webmaster@1: exit; webmaster@1: } webmaster@1: break; webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_DATABASE: webmaster@1: // Initialize the default database. webmaster@1: require_once './includes/database.inc'; webmaster@1: db_set_active(); webmaster@1: break; webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_ACCESS: webmaster@1: // Deny access to hosts which were banned - t() is not yet available. webmaster@1: if (drupal_is_denied('host', ip_address())) { webmaster@1: header('HTTP/1.1 403 Forbidden'); webmaster@1: print 'Sorry, '. check_plain(ip_address()) .' has been banned.'; webmaster@1: exit(); webmaster@1: } webmaster@1: break; webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_SESSION: webmaster@1: require_once variable_get('session_inc', './includes/session.inc'); webmaster@1: session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy_sid', 'sess_gc'); webmaster@1: session_start(); webmaster@1: break; webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: webmaster@1: // Initialize configuration variables, using values from settings.php if available. webmaster@1: $conf = variable_init(isset($conf) ? $conf : array()); webmaster@1: // Load module handling. webmaster@1: require_once './includes/module.inc'; webmaster@1: $cache_mode = variable_get('cache', CACHE_DISABLED); webmaster@1: // Get the page from the cache. webmaster@1: $cache = $cache_mode == CACHE_DISABLED ? '' : page_get_cache(); webmaster@1: // If the skipping of the bootstrap hooks is not enforced, call hook_boot. webmaster@1: if ($cache_mode != CACHE_AGGRESSIVE) { webmaster@1: bootstrap_invoke_all('boot'); webmaster@1: } webmaster@1: // If there is a cached page, display it. webmaster@1: if ($cache) { webmaster@1: drupal_page_cache_header($cache); webmaster@1: // If the skipping of the bootstrap hooks is not enforced, call hook_exit. webmaster@1: if ($cache_mode != CACHE_AGGRESSIVE) { webmaster@1: bootstrap_invoke_all('exit'); webmaster@1: } webmaster@1: // We are done. webmaster@1: exit; webmaster@1: } webmaster@1: // Prepare for non-cached page workflow. webmaster@1: drupal_page_header(); webmaster@1: break; webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_LANGUAGE: webmaster@1: drupal_init_language(); webmaster@1: break; webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_PATH: webmaster@1: require_once './includes/path.inc'; webmaster@1: // Initialize $_GET['q'] prior to loading modules and invoking hook_init(). webmaster@1: drupal_init_path(); webmaster@1: break; webmaster@1: webmaster@1: case DRUPAL_BOOTSTRAP_FULL: webmaster@1: require_once './includes/common.inc'; webmaster@1: _drupal_bootstrap_full(); webmaster@1: break; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Enables use of the theme system without requiring database access. webmaster@1: * webmaster@1: * Loads and initializes the theme system for site installs, updates and when webmaster@1: * the site is in off-line mode. This also applies when the database fails. webmaster@1: * webmaster@1: * @see _drupal_maintenance_theme() webmaster@1: */ webmaster@1: function drupal_maintenance_theme() { webmaster@1: require_once './includes/theme.maintenance.inc'; webmaster@1: _drupal_maintenance_theme(); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Return the name of the localisation function. Use in code that needs to webmaster@1: * run both during installation and normal operation. webmaster@1: */ webmaster@1: function get_t() { webmaster@1: static $t; webmaster@1: if (is_null($t)) { webmaster@1: $t = function_exists('install_main') ? 'st' : 't'; webmaster@1: } webmaster@1: return $t; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Choose a language for the current page, based on site and user preferences. webmaster@1: */ webmaster@1: function drupal_init_language() { webmaster@1: global $language, $user; webmaster@1: webmaster@1: // Ensure the language is correctly returned, even without multilanguage support. webmaster@1: // Useful for eg. XML/HTML 'lang' attributes. webmaster@1: if (variable_get('language_count', 1) == 1) { webmaster@1: $language = language_default(); webmaster@1: } webmaster@1: else { webmaster@1: include_once './includes/language.inc'; webmaster@1: $language = language_initialize(); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Get a list of languages set up indexed by the specified key webmaster@1: * webmaster@1: * @param $field The field to index the list with. webmaster@1: * @param $reset Boolean to request a reset of the list. webmaster@1: */ webmaster@1: function language_list($field = 'language', $reset = FALSE) { webmaster@1: static $languages = NULL; webmaster@1: webmaster@1: // Reset language list webmaster@1: if ($reset) { webmaster@1: $languages = NULL; webmaster@1: } webmaster@1: webmaster@1: // Init language list webmaster@1: if (!isset($languages)) { webmaster@1: if (variable_get('language_count', 1) > 1 || module_exists('locale')) { webmaster@1: $result = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC'); webmaster@1: while ($row = db_fetch_object($result)) { webmaster@1: $languages['language'][$row->language] = $row; webmaster@1: } webmaster@1: } webmaster@1: else { webmaster@1: // No locale module, so use the default language only. webmaster@1: $default = language_default(); webmaster@1: $languages['language'][$default->language] = $default; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Return the array indexed by the right field webmaster@1: if (!isset($languages[$field])) { webmaster@1: $languages[$field] = array(); webmaster@1: foreach ($languages['language'] as $lang) { webmaster@1: // Some values should be collected into an array webmaster@1: if (in_array($field, array('enabled', 'weight'))) { webmaster@1: $languages[$field][$lang->$field][$lang->language] = $lang; webmaster@1: } webmaster@1: else { webmaster@1: $languages[$field][$lang->$field] = $lang; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: return $languages[$field]; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Default language used on the site webmaster@1: * webmaster@1: * @param $property webmaster@1: * Optional property of the language object to return webmaster@1: */ webmaster@1: function language_default($property = NULL) { webmaster@1: $language = variable_get('language_default', (object) array('language' => 'en', 'name' => 'English', 'native' => 'English', 'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => '', 'weight' => 0, 'javascript' => '')); webmaster@1: return $property ? $language->$property : $language; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * If Drupal is behind a reverse proxy, we use the X-Forwarded-For header webmaster@1: * instead of $_SERVER['REMOTE_ADDR'], which would be the IP address webmaster@1: * of the proxy server, and not the client's. webmaster@1: * webmaster@1: * @return webmaster@1: * IP address of client machine, adjusted for reverse proxy. webmaster@1: */ webmaster@1: function ip_address() { webmaster@1: static $ip_address = NULL; webmaster@1: webmaster@1: if (!isset($ip_address)) { webmaster@1: $ip_address = $_SERVER['REMOTE_ADDR']; webmaster@1: if (variable_get('reverse_proxy', 0) && array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) { webmaster@1: // If an array of known reverse proxy IPs is provided, then trust webmaster@1: // the XFF header if request really comes from one of them. webmaster@1: $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array()); webmaster@1: if (!empty($reverse_proxy_addresses) && in_array($ip_address, $reverse_proxy_addresses, TRUE)) { webmaster@1: // If there are several arguments, we need to check the most webmaster@1: // recently added one, i.e. the last one. webmaster@1: $ip_address = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return $ip_address; webmaster@1: }