| 1 | | <?php |
| 2 | | // $Id: bootstrap.inc,v 1.243 2008/10/31 02:18:22 dries Exp $ |
| 3 | | |
| 4 | | /** |
| 5 | | * @file |
| 6 | | * Functions that need to be loaded on every Drupal request. |
| 7 | | */ |
| 8 | | |
| 9 | | /** |
| 10 | | * Indicates that the item should never be removed unless explicitly told
to |
| 11 | | * using cache_clear_all() with a cache ID. |
| 12 | | */ |
| 13 | 2367 | define('CACHE_PERMANENT', 0); |
| 14 | | |
| 15 | | /** |
| 16 | | * Indicates that the item should be removed at the next general cache
wipe. |
| 17 | | */ |
| 18 | 2367 | define('CACHE_TEMPORARY', -1); |
| 19 | | |
| 20 | | /** |
| 21 | | * Indicates that page caching is disabled. |
| 22 | | */ |
| 23 | 2367 | define('CACHE_DISABLED', 0); |
| 24 | | |
| 25 | | /** |
| 26 | | * Indicates that page caching is enabled, using "normal" mode. |
| 27 | | */ |
| 28 | 2367 | define('CACHE_NORMAL', 1); |
| 29 | | |
| 30 | | /** |
| 31 | | * Indicates that page caching is using "aggressive" mode. This bypasses |
| 32 | | * loading any modules for additional speed, which may break functionality
in |
| 33 | | * modules that expect to be run on each page load. |
| 34 | | */ |
| 35 | 2367 | define('CACHE_AGGRESSIVE', 2); |
| 36 | | |
| 37 | | /** |
| 38 | | * Log message severity -- Emergency: system is unusable. |
| 39 | | * |
| 40 | | * @see watchdog() |
| 41 | | * @see watchdog_severity_levels() |
| 42 | | */ |
| 43 | 2367 | define('WATCHDOG_EMERG', 0); |
| 44 | | |
| 45 | | /** |
| 46 | | * Log message severity -- Alert: action must be taken immediately. |
| 47 | | * |
| 48 | | * @see watchdog() |
| 49 | | * @see watchdog_severity_levels() |
| 50 | | */ |
| 51 | 2367 | define('WATCHDOG_ALERT', 1); |
| 52 | | |
| 53 | | /** |
| 54 | | * Log message severity -- Critical: critical conditions. |
| 55 | | * |
| 56 | | * @see watchdog() |
| 57 | | * @see watchdog_severity_levels() |
| 58 | | */ |
| 59 | 2367 | define('WATCHDOG_CRITICAL', 2); |
| 60 | | |
| 61 | | /** |
| 62 | | * Log message severity -- Error: error conditions. |
| 63 | | * |
| 64 | | * @see watchdog() |
| 65 | | * @see watchdog_severity_levels() |
| 66 | | */ |
| 67 | 2367 | define('WATCHDOG_ERROR', 3); |
| 68 | | |
| 69 | | /** |
| 70 | | * Log message severity -- Warning: warning conditions. |
| 71 | | * |
| 72 | | * @see watchdog() |
| 73 | | * @see watchdog_severity_levels() |
| 74 | | */ |
| 75 | 2367 | define('WATCHDOG_WARNING', 4); |
| 76 | | |
| 77 | | /** |
| 78 | | * Log message severity -- Notice: normal but significant condition. |
| 79 | | * |
| 80 | | * @see watchdog() |
| 81 | | * @see watchdog_severity_levels() |
| 82 | | */ |
| 83 | 2367 | define('WATCHDOG_NOTICE', 5); |
| 84 | | |
| 85 | | /** |
| 86 | | * Log message severity -- Informational: informational messages. |
| 87 | | * |
| 88 | | * @see watchdog() |
| 89 | | * @see watchdog_severity_levels() |
| 90 | | */ |
| 91 | 2367 | define('WATCHDOG_INFO', 6); |
| 92 | | |
| 93 | | /** |
| 94 | | * Log message severity -- Debug: debug-level messages. |
| 95 | | * |
| 96 | | * @see watchdog() |
| 97 | | * @see watchdog_severity_levels() |
| 98 | | */ |
| 99 | 2367 | define('WATCHDOG_DEBUG', 7); |
| 100 | | |
| 101 | | /** |
| 102 | | * First bootstrap phase: initialize configuration. |
| 103 | | */ |
| 104 | 2367 | define('DRUPAL_BOOTSTRAP_CONFIGURATION', 0); |
| 105 | | |
| 106 | | /** |
| 107 | | * Second bootstrap phase: try to call a non-database cache |
| 108 | | * fetch routine. |
| 109 | | */ |
| 110 | 2367 | define('DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE', 1); |
| 111 | | |
| 112 | | /** |
| 113 | | * Third bootstrap phase: initialize database layer. |
| 114 | | */ |
| 115 | 2367 | define('DRUPAL_BOOTSTRAP_DATABASE', 2); |
| 116 | | |
| 117 | | /** |
| 118 | | * Fourth bootstrap phase: identify and reject banned hosts. |
| 119 | | */ |
| 120 | 2367 | define('DRUPAL_BOOTSTRAP_ACCESS', 3); |
| 121 | | |
| 122 | | /** |
| 123 | | * Fifth bootstrap phase: initialize session handling. |
| 124 | | */ |
| 125 | 2367 | define('DRUPAL_BOOTSTRAP_SESSION', 4); |
| 126 | | |
| 127 | | /** |
| 128 | | * Sixth bootstrap phase: initialize the variable system. |
| 129 | | */ |
| 130 | 2367 | define('DRUPAL_BOOTSTRAP_VARIABLES', 5); |
| 131 | | |
| 132 | | /** |
| 133 | | * Seventh bootstrap phase: load bootstrap.inc and module.inc, start |
| 134 | | * the variable system and try to serve a page from the cache. |
| 135 | | */ |
| 136 | 2367 | define('DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE', 6); |
| 137 | | |
| 138 | | /** |
| 139 | | * Eighth bootstrap phase: find out language of the page. |
| 140 | | */ |
| 141 | 2367 | define('DRUPAL_BOOTSTRAP_LANGUAGE', 7); |
| 142 | | |
| 143 | | /** |
| 144 | | * Nineth bootstrap phase: set $_GET['q'] to Drupal path of request. |
| 145 | | */ |
| 146 | 2367 | define('DRUPAL_BOOTSTRAP_PATH', 8); |
| 147 | | |
| 148 | | /** |
| 149 | | * Final bootstrap phase: Drupal is fully loaded; validate and fix |
| 150 | | * input data. |
| 151 | | */ |
| 152 | 2367 | define('DRUPAL_BOOTSTRAP_FULL', 9); |
| 153 | | |
| 154 | | /** |
| 155 | | * Role ID for anonymous users; should match what's in the "role" table. |
| 156 | | */ |
| 157 | 2367 | define('DRUPAL_ANONYMOUS_RID', 1); |
| 158 | | |
| 159 | | /** |
| 160 | | * Role ID for authenticated users; should match what's in the "role"
table. |
| 161 | | */ |
| 162 | 2367 | define('DRUPAL_AUTHENTICATED_RID', 2); |
| 163 | | |
| 164 | | /** |
| 165 | | * No language negotiation. The default language is used. |
| 166 | | */ |
| 167 | 2367 | define('LANGUAGE_NEGOTIATION_NONE', 0); |
| 168 | | |
| 169 | | /** |
| 170 | | * Path based negotiation with fallback to default language |
| 171 | | * if no defined path prefix identified. |
| 172 | | */ |
| 173 | 2367 | define('LANGUAGE_NEGOTIATION_PATH_DEFAULT', 1); |
| 174 | | |
| 175 | | /** |
| 176 | | * Path based negotiation with fallback to user preferences |
| 177 | | * and browser language detection if no defined path prefix |
| 178 | | * identified. |
| 179 | | */ |
| 180 | 2367 | define('LANGUAGE_NEGOTIATION_PATH', 2); |
| 181 | | |
| 182 | | /** |
| 183 | | * Domain based negotiation with fallback to default language |
| 184 | | * if no language identified by domain. |
| 185 | | */ |
| 186 | 2367 | define('LANGUAGE_NEGOTIATION_DOMAIN', 3); |
| 187 | | |
| 188 | | /** |
| 189 | | * For convenience, define a short form of the request time global. |
| 190 | | */ |
| 191 | 2367 | define('REQUEST_TIME', $_SERVER['REQUEST_TIME']); |
| 192 | | |
| 193 | | /** |
| 194 | | * @name Title text filtering flags |
| 195 | | * @{ |
| 196 | | * Flags for use in drupal_set_title(). |
| 197 | | */ |
| 198 | | |
| 199 | | /** |
| 200 | | * Flag for drupal_set_title(); text is not sanitized, so run
check_plain(). |
| 201 | | */ |
| 202 | 2367 | define('CHECK_PLAIN', 0); |
| 203 | | |
| 204 | | /** |
| 205 | | * Flag for drupal_set_title(); text has already been sanitized. |
| 206 | | */ |
| 207 | 2367 | define('PASS_THROUGH', -1); |
| 208 | | |
| 209 | | /** |
| 210 | | * @} End of "Title text filtering flags". |
| 211 | | */ |
| 212 | | |
| 213 | | |
| 214 | | /** |
| 215 | | * Start the timer with the specified name. If you start and stop |
| 216 | | * the same timer multiple times, the measured intervals will be |
| 217 | | * accumulated. |
| 218 | | * |
| 219 | | * @param name |
| 220 | | * The name of the timer. |
| 221 | | */ |
| 222 | 2367 | function timer_start($name) { |
| 223 | 2367 | global $timers; |
| 224 | | |
| 225 | 2367 | $timers[$name]['start'] = microtime(TRUE); |
| 226 | 2367 | $timers[$name]['count'] = isset($timers[$name]['count']) ?
++$timers[$name]['count'] : 1; |
| 227 | 2367 | } |
| 228 | | |
| 229 | | /** |
| 230 | | * Read the current timer value without stopping the timer. |
| 231 | | * |
| 232 | | * @param name |
| 233 | | * The name of the timer. |
| 234 | | * @return |
| 235 | | * The current timer value in ms. |
| 236 | | */ |
| 237 | 2367 | function timer_read($name) { |
| 238 | 16 | global $timers; |
| 239 | | |
| 240 | 16 | if (isset($timers[$name]['start'])) { |
| 241 | 16 | $stop = microtime(TRUE); |
| 242 | 16 | $diff = round(($stop - $timers[$name]['start']) * 1000, 2); |
| 243 | | |
| 244 | 16 | if (isset($timers[$name]['time'])) { |
| 245 | 0 | $diff += $timers[$name]['time']; |
| 246 | 0 | } |
| 247 | 16 | return $diff; |
| 248 | 0 | } |
| 249 | 0 | } |
| 250 | | |
| 251 | | /** |
| 252 | | * Stop the timer with the specified name. |
| 253 | | * |
| 254 | | * @param name |
| 255 | | * The name of the timer. |
| 256 | | * @return |
| 257 | | * A timer array. The array contains the number of times the |
| 258 | | * timer has been started and stopped (count) and the accumulated |
| 259 | | * timer value in ms (time). |
| 260 | | */ |
| 261 | 2367 | function timer_stop($name) { |
| 262 | 0 | global $timers; |
| 263 | | |
| 264 | 0 | $timers[$name]['time'] = timer_read($name); |
| 265 | 0 | unset($timers[$name]['start']); |
| 266 | | |
| 267 | 0 | return $timers[$name]; |
| 268 | 0 | } |
| 269 | | |
| 270 | | /** |
| 271 | | * Find the appropriate configuration directory. |
| 272 | | * |
| 273 | | * Try finding a matching configuration directory by stripping the
website's |
| 274 | | * hostname from left to right and pathname from right to left. The first |
| 275 | | * configuration file found will be used; the remaining will ignored. If no |
| 276 | | * configuration file is found, return a default value '$confdir/default'. |
| 277 | | * |
| 278 | | * Example for a fictitious site installed at |
| 279 | | * http://www.drupal.org:8080/mysite/test/ the 'settings.php' is searched
in |
| 280 | | * the following directories: |
| 281 | | * |
| 282 | | * 1. $confdir/8080.www.drupal.org.mysite.test |
| 283 | | * 2. $confdir/www.drupal.org.mysite.test |
| 284 | | * 3. $confdir/drupal.org.mysite.test |
| 285 | | * 4. $confdir/org.mysite.test |
| 286 | | * |
| 287 | | * 5. $confdir/8080.www.drupal.org.mysite |
| 288 | | * 6. $confdir/www.drupal.org.mysite |
| 289 | | * 7. $confdir/drupal.org.mysite |
| 290 | | * 8. $confdir/org.mysite |
| 291 | | * |
| 292 | | * 9. $confdir/8080.www.drupal.org |
| 293 | | * 10. $confdir/www.drupal.org |
| 294 | | * 11. $confdir/drupal.org |
| 295 | | * 12. $confdir/org |
| 296 | | * |
| 297 | | * 13. $confdir/default |
| 298 | | * |
| 299 | | * If a file named sites.php is present in the $confdir, it will be loaded |
| 300 | | * prior to scanning for directories. It should define an associative
array |
| 301 | | * named $sites, which maps domains to directories. It should be in the
form |
| 302 | | * of: |
| 303 | | * |
| 304 | | * $sites = array( |
| 305 | | * 'The url to alias' => 'A directory within the sites directory' |
| 306 | | * ); |
| 307 | | * |
| 308 | | * For example: |
| 309 | | * |
| 310 | | * $sites = array( |
| 311 | | * 'devexample.com' => 'example.com', |
| 312 | | * 'localhost/example' => 'example.com', |
| 313 | | * ); |
| 314 | | * |
| 315 | | * The above array will cause Drupal to look for a directory named |
| 316 | | * "example.com" in the sites directory whenever a request comes from |
| 317 | | * "example.com", "devexample.com", or "localhost/example". That is useful |
| 318 | | * on development servers, where the domain name may not be the same as the |
| 319 | | * domain of the live server. Since Drupal stores file paths into the
database |
| 320 | | * (files, system table, etc.) this will ensure the paths are correct while |
| 321 | | * accessed on development servers. |
| 322 | | * |
| 323 | | * @param $require_settings |
| 324 | | * Only configuration directories with an existing settings.php file |
| 325 | | * will be recognized. Defaults to TRUE. During initial installation, |
| 326 | | * this is set to FALSE so that Drupal can detect a matching directory, |
| 327 | | * then create a new settings.php file in it. |
| 328 | | * @param reset |
| 329 | | * Force a full search for matching directories even if one had been |
| 330 | | * found previously. |
| 331 | | * @return |
| 332 | | * The path of the matching directory. |
| 333 | | */ |
| 334 | 2367 | function conf_path($require_settings = TRUE, $reset = FALSE) { |
| 335 | 2498 | static $conf = ''; |
| 336 | | |
| 337 | 2498 | if ($conf && !$reset) { |
| 338 | 2498 | return $conf; |
| 339 | 0 | } |
| 340 | | |
| 341 | 2367 | $confdir = 'sites'; |
| 342 | | |
| 343 | 2367 | $sites = array(); |
| 344 | 2367 | if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/sites.php')) { |
| 345 | | // This will overwrite $sites with the desired mappings. |
| 346 | 0 | include(DRUPAL_ROOT . '/' . $confdir . '/sites.php'); |
| 347 | 0 | } |
| 348 | | |
| 349 | 2367 | $uri = explode('/', $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] :
$_SERVER['SCRIPT_FILENAME']); |
| 350 | 2367 | if (strpos($_SERVER['HTTP_HOST'], '/') !== FALSE ||
strpos($_SERVER['HTTP_HOST'], '\\') !== FALSE) { |
| 351 | | // A HTTP_HOST containing slashes may be an attack and is invalid. |
| 352 | 0 | header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request'); |
| 353 | 0 | exit; |
| 354 | 0 | } |
| 355 | 2367 | $server = explode('.', implode('.', array_reverse(explode(':',
rtrim($_SERVER['HTTP_HOST'], '.'))))); |
| 356 | 2367 | for ($i = count($uri) - 1; $i > 0; $i--) { |
| 357 | 2367 | for ($j = count($server); $j > 0; $j--) { |
| 358 | 2367 | $dir = implode('.', array_slice($server, -$j)) . implode('.',
array_slice($uri, 0, $i)); |
| 359 | 2367 | if (isset($sites[$dir]) && file_exists(DRUPAL_ROOT . '/' . $confdir .
'/' . $sites[$dir])) { |
| 360 | 0 | $dir = $sites[$dir]; |
| 361 | 0 | } |
| 362 | 2367 | if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir .
'/settings.php') || (!$require_settings && file_exists(DRUPAL_ROOT . '/' .
$confdir . '/' . $dir))) { |
| 363 | 0 | $conf = "$confdir/$dir"; |
| 364 | 0 | return $conf; |
| 365 | 0 | } |
| 366 | 2367 | } |
| 367 | 2367 | } |
| 368 | 2367 | $conf = "$confdir/default"; |
| 369 | 2367 | return $conf; |
| 370 | 0 | } |
| 371 | | |
| 372 | | /** |
| 373 | | * Initialize variables needed for the rest of the execution. |
| 374 | | */ |
| 375 | 2367 | function drupal_initialize_variables() { |
| 376 | 2367 | if (!isset($_SERVER['HTTP_REFERER'])) { |
| 377 | 2367 | $_SERVER['HTTP_REFERER'] = ''; |
| 378 | 2367 | } |
| 379 | 2367 | if (!isset($_SERVER['SERVER_PROTOCOL']) || ($_SERVER['SERVER_PROTOCOL']
!= 'HTTP/1.0' && $_SERVER['SERVER_PROTOCOL'] != 'HTTP/1.1')) { |
| 380 | 0 | $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0'; |
| 381 | 0 | } |
| 382 | | // Enforce E_ALL, but allow users to set levels not part of E_ALL. |
| 383 | 2367 | error_reporting(E_ALL | error_reporting()); |
| 384 | | // Prevent PHP from generating HTML errors messages. |
| 385 | 2367 | ini_set('html_errors', 0); |
| 386 | 2367 | } |
| 387 | | |
| 388 | | /** |
| 389 | | * Loads the configuration and sets the base URL, cookie domain, and |
| 390 | | * session name correctly. |
| 391 | | */ |
| 392 | 2367 | function conf_init() { |
| 393 | 2367 | global $base_url, $base_path, $base_root; |
| 394 | | |
| 395 | | // Export the following settings.php variables to the global namespace |
| 396 | 2367 | global $databases, $db_prefix, $cookie_domain, $conf, $installed_profile,
$update_free_access; |
| 397 | 2367 | $conf = array(); |
| 398 | | |
| 399 | 2367 | if (file_exists(DRUPAL_ROOT . '/' . conf_path() . '/settings.php')) { |
| 400 | 2367 | include_once DRUPAL_ROOT . '/' . conf_path() . '/settings.php'; |
| 401 | 2367 | } |
| 402 | | |
| 403 | 2367 | if (isset($base_url)) { |
| 404 | | // Parse fixed base URL from settings.php. |
| 405 | 0 | $parts = parse_url($base_url); |
| 406 | 0 | if (!isset($parts['path'])) { |
| 407 | 0 | $parts['path'] = ''; |
| 408 | 0 | } |
| 409 | 0 | $base_path = $parts['path'] . '/'; |
| 410 | | // Build $base_root (everything until first slash after "scheme://"). |
| 411 | 0 | $base_root = substr($base_url, 0, strlen($base_url) -
strlen($parts['path'])); |
| 412 | 0 | } |
| 413 | | else { |
| 414 | | // Create base URL |
| 415 | 2367 | $base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ?
'https' : 'http'; |
| 416 | | |
| 417 | | // As $_SERVER['HTTP_HOST'] is user input, ensure it only contains |
| 418 | | // characters allowed in hostnames. |
| 419 | 2367 | $base_url = $base_root .= '://' . preg_replace('/[^a-z0-9-:._]/i', '',
$_SERVER['HTTP_HOST']); |
| 420 | | |
| 421 | | // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'],
not |
| 422 | | // be modified by a visitor. |
| 423 | 2367 | if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) { |
| 424 | 2367 | $base_path = "/$dir"; |
| 425 | 2367 | $base_url .= $base_path; |
| 426 | 2367 | $base_path .= '/'; |
| 427 | 2367 | } |
| 428 | | else { |
| 429 | 0 | $base_path = '/'; |
| 430 | | } |
| 431 | | } |
| 432 | | |
| 433 | 2367 | if ($cookie_domain) { |
| 434 | | // If the user specifies the cookie domain, also use it for session
name. |
| 435 | 0 | $session_name = $cookie_domain; |
| 436 | 0 | } |
| 437 | | else { |
| 438 | | // Otherwise use $base_url as session name, without the protocol |
| 439 | | // to use the same session identifiers across http and https. |
| 440 | 2367 | list( , $session_name) = explode('://', $base_url, 2); |
| 441 | | // We escape the hostname because it can be modified by a visitor. |
| 442 | 2367 | if (!empty($_SERVER['HTTP_HOST'])) { |
| 443 | 2367 | $cookie_domain = check_plain($_SERVER['HTTP_HOST']); |
| 444 | 2367 | } |
| 445 | | } |
| 446 | | // To prevent session cookies from being hijacked, a user can configure
the |
| 447 | | // SSL version of their website to only transfer session cookies via SSL
by |
| 448 | | // using PHP's session.cookie_secure setting. The browser will then use
two |
| 449 | | // separate session cookies for the HTTPS and HTTP versions of the site.
So we |
| 450 | | // must use different session identifiers for HTTPS and HTTP to prevent a |
| 451 | | // cookie collision. |
| 452 | 2367 | if (ini_get('session.cookie_secure')) { |
| 453 | 0 | $session_name .= 'SSL'; |
| 454 | 0 | } |
| 455 | | // Strip leading periods, www., and port numbers from cookie domain. |
| 456 | 2367 | $cookie_domain = ltrim($cookie_domain, '.'); |
| 457 | 2367 | if (strpos($cookie_domain, 'www.') === 0) { |
| 458 | 0 | $cookie_domain = substr($cookie_domain, 4); |
| 459 | 0 | } |
| 460 | 2367 | $cookie_domain = explode(':', $cookie_domain); |
| 461 | 2367 | $cookie_domain = '.' . $cookie_domain[0]; |
| 462 | | // Per RFC 2109, cookie domains must contain at least one dot other than
the |
| 463 | | // first. For hosts such as 'localhost' or IP Addresses we don't set a
cookie domain. |
| 464 | 2367 | if (count(explode('.', $cookie_domain)) > 2 &&
!is_numeric(str_replace('.', '', $cookie_domain))) { |
| 465 | 0 | ini_set('session.cookie_domain', $cookie_domain); |
| 466 | 0 | } |
| 467 | 2367 | session_name('SESS' . md5($session_name)); |
| 468 | 2367 | } |
| 469 | | |
| 470 | | /** |
| 471 | | * Returns and optionally sets the filename for a system item (module, |
| 472 | | * theme, etc.). The filename, whether provided, cached, or retrieved |
| 473 | | * from the database, is only returned if the file exists. |
| 474 | | * |
| 475 | | * This function plays a key role in allowing Drupal's resources (modules |
| 476 | | * and themes) to be located in different places depending on a site's |
| 477 | | * configuration. For example, a module 'foo' may legally be be located |
| 478 | | * in any of these three places: |
| 479 | | * |
| 480 | | * modules/foo/foo.module |
| 481 | | * sites/all/modules/foo/foo.module |
| 482 | | * sites/example.com/modules/foo/foo.module |
| 483 | | * |
| 484 | | * Calling drupal_get_filename('module', 'foo') will give you one of |
| 485 | | * the above, depending on where the module is located. |
| 486 | | * |
| 487 | | * @param $type |
| 488 | | * The type of the item (i.e. theme, theme_engine, module). |
| 489 | | * @param $name |
| 490 | | * The name of the item for which the filename is requested. |
| 491 | | * @param $filename |
| 492 | | * The filename of the item if it is to be set explicitly rather |
| 493 | | * than by consulting the database. |
| 494 | | * |
| 495 | | * @return |
| 496 | | * The filename of the requested item. |
| 497 | | */ |
| 498 | 2367 | function drupal_get_filename($type, $name, $filename = NULL) { |
| 499 | 2496 | static $files = array(); |
| 500 | | |
| 501 | 2496 | if (!isset($files[$type])) { |
| 502 | 2366 | $files[$type] = array(); |
| 503 | 2366 | } |
| 504 | | |
| 505 | 2496 | if (!empty($filename) && file_exists($filename)) { |
| 506 | 2496 | $files[$type][$name] = $filename; |
| 507 | 2496 | } |
| 508 | 2496 | elseif (isset($files[$type][$name])) { |
| 509 | | // nothing |
| 510 | 2496 | } |
| 511 | | // Verify that we have an active database connection, before querying |
| 512 | | // the database. This is required because this function is called both |
| 513 | | // before we have a database connection (i.e. during installation) and |
| 514 | | // when a database connection fails. |
| 515 | 16 | elseif (db_is_active() && (($file = db_query("SELECT filename FROM
{system} WHERE name = :name AND type = :type", array(':name' => $name,
':type' => $type))->fetchField()) && file_exists($file))) { |
| 516 | 16 | $files[$type][$name] = $file; |
| 517 | 16 | } |
| 518 | | else { |
| 519 | | // Fallback to searching the filesystem if the database connection is |
| 520 | | // not established or the requested file is not found. |
| 521 | 0 | $config = conf_path(); |
| 522 | 0 | $dir = (($type == 'theme_engine') ? 'themes/engines' : "${type}s"); |
| 523 | 0 | $file = (($type == 'theme_engine') ? "$name.engine" : "$name.$type"); |
| 524 | | |
| 525 | 0 | foreach (array("$config/$dir/$file", "$config/$dir/$name/$file",
"$dir/$file", "$dir/$name/$file") as $file) { |
| 526 | 0 | if (file_exists($file)) { |
| 527 | 0 | $files[$type][$name] = $file; |
| 528 | 0 | break; |
| 529 | 0 | } |
| 530 | 0 | } |
| 531 | | } |
| 532 | | |
| 533 | 2496 | if (isset($files[$type][$name])) { |
| 534 | 2496 | return $files[$type][$name]; |
| 535 | 0 | } |
| 536 | 0 | } |
| 537 | | |
| 538 | | /** |
| 539 | | * Load the persistent variable table. |
| 540 | | * |
| 541 | | * The variable table is composed of values that have been saved in the
table |
| 542 | | * with variable_set() as well as those explicitly specified in the
configuration |
| 543 | | * file. |
| 544 | | */ |
| 545 | 2367 | function variable_init($conf = array()) { |
| 546 | | // NOTE: caching the variables improves performance by 20% when serving
cached pages. |
| 547 | 2498 | if ($cached = cache_get('variables', 'cache')) { |
| 548 | 2498 | $variables = $cached->data; |
| 549 | 2498 | } |
| 550 | | else { |
| 551 | 255 | $variables = array_map('unserialize', db_query('SELECT name, value FROM
{variable}')->fetchAllKeyed()); |
| 552 | 255 | cache_set('variables', $variables); |
| 553 | | } |
| 554 | | |
| 555 | 2498 | foreach ($conf as $name => $value) { |
| 556 | 0 | $variables[$name] = $value; |
| 557 | 0 | } |
| 558 | | |
| 559 | 2498 | return $variables; |
| 560 | 0 | } |
| 561 | | |
| 562 | | /** |
| 563 | | * Return a persistent variable. |
| 564 | | * |
| 565 | | * @param $name |
| 566 | | * The name of the variable to return. |
| 567 | | * @param $default |
| 568 | | * The default value to use if this variable has never been set. |
| 569 | | * @return |
| 570 | | * The value of the variable. |
| 571 | | */ |
| 572 | 2367 | function variable_get($name, $default) { |
| 573 | 2498 | global $conf; |
| 574 | | |
| 575 | 2498 | return isset($conf[$name]) ? $conf[$name] : $default; |
| 576 | 0 | } |
| 577 | | |
| 578 | | /** |
| 579 | | * Set a persistent variable. |
| 580 | | * |
| 581 | | * @param $name |
| 582 | | * The name of the variable to set. |
| 583 | | * @param $value |
| 584 | | * The value to set. This can be any PHP data type; these functions take
care |
| 585 | | * of serialization as necessary. |
| 586 | | */ |
| 587 | 2367 | function variable_set($name, $value) { |
| 588 | 301 | global $conf; |
| 589 | | |
| 590 | 301 | db_merge('variable')->key(array('name' => $name))->fields(array('value'
=> serialize($value)))->execute(); |
| 591 | | |
| 592 | 301 | cache_clear_all('variables', 'cache'); |
| 593 | | |
| 594 | 301 | $conf[$name] = $value; |
| 595 | 301 | } |
| 596 | | |
| 597 | | /** |
| 598 | | * Unset a persistent variable. |
| 599 | | * |
| 600 | | * @param $name |
| 601 | | * The name of the variable to undefine. |
| 602 | | */ |
| 603 | 2367 | function variable_del($name) { |
| 604 | 161 | global $conf; |
| 605 | | |
| 606 | 161 | db_delete('variable') |
| 607 | 161 | ->condition('name', $name) |
| 608 | 161 | ->execute(); |
| 609 | 161 | cache_clear_all('variables', 'cache'); |
| 610 | | |
| 611 | 161 | unset($conf[$name]); |
| 612 | 161 | } |
| 613 | | |
| 614 | | |
| 615 | | /** |
| 616 | | * Retrieve the current page from the cache. |
| 617 | | * |
| 618 | | * Note: we do not serve cached pages when status messages are waiting
(from |
| 619 | | * a redirected form submission which was completed). |
| 620 | | */ |
| 621 | 2367 | function page_get_cache() { |
| 622 | 2 | global $user, $base_root; |
| 623 | | |
| 624 | 2 | $cache = NULL; |
| 625 | | |
| 626 | 2 | if (!$user->uid && ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') && count(drupal_set_message()) == 0)
{ |
| 627 | 2 | $cache = cache_get($base_root . request_uri(), 'cache_page'); |
| 628 | | |
| 629 | 2 | if (empty($cache)) { |
| 630 | 1 | ob_start(); |
| 631 | 1 | } |
| 632 | 2 | } |
| 633 | | |
| 634 | 2 | return $cache; |
| 635 | 0 | } |
| 636 | | |
| 637 | | /** |
| 638 | | * Call all init or exit hooks without including all modules. |
| 639 | | * |
| 640 | | * @param $hook |
| 641 | | * The name of the bootstrap hook we wish to invoke. |
| 642 | | */ |
| 643 | 2367 | function bootstrap_invoke_all($hook) { |
| 644 | 2367 | foreach (module_list(TRUE, TRUE) as $module) { |
| 645 | 0 | module_invoke($module, $hook); |
| 646 | 0 | } |
| 647 | 2367 | } |
| 648 | | |
| 649 | | /** |
| 650 | | * Includes a file with the provided type and name. This prevents |
| 651 | | * including a theme, engine, module, etc., more than once. |
| 652 | | * |
| 653 | | * @param $type |
| 654 | | * The type of item to load (i.e. theme, theme_engine, module). |
| 655 | | * @param $name |
| 656 | | * The name of the item to load. |
| 657 | | * |
| 658 | | * @return |
| 659 | | * TRUE if the item is loaded or has already been loaded. |
| 660 | | */ |
| 661 | 2367 | function drupal_load($type, $name) { |
| 662 | 2496 | static $files = array(); |
| 663 | | |
| 664 | 2496 | if (isset($files[$type][$name])) { |
| 665 | 134 | return TRUE; |
| 666 | 0 | } |
| 667 | | |
| 668 | 2438 | $filename = drupal_get_filename($type, $name); |
| 669 | | |
| 670 | 2438 | if ($filename) { |
| 671 | 2438 | include_once DRUPAL_ROOT . '/' . $filename; |
| 672 | 2438 | $files[$type][$name] = TRUE; |
| 673 | | |
| 674 | 2438 | return TRUE; |
| 675 | 0 | } |
| 676 | | |
| 677 | 0 | return FALSE; |
| 678 | 0 | } |
| 679 | | |
| 680 | | /** |
| 681 | | * Set HTTP headers in preparation for a page response. |
| 682 | | * |
| 683 | | * Authenticated users are always given a 'no-cache' header, and will |
| 684 | | * fetch a fresh page on every request. This prevents authenticated |
| 685 | | * users seeing locally cached pages that show them as logged out. |
| 686 | | * |
| 687 | | * @see page_set_cache() |
| 688 | | */ |
| 689 | 2367 | function drupal_page_header() { |
| 690 | 2366 | header("Expires: Sun, 19 Nov 1978 05:00:00 GMT"); |
| 691 | 2366 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
| 692 | 2366 | header("Cache-Control: store, no-cache, must-revalidate"); |
| 693 | 2366 | header("Cache-Control: post-check=0, pre-check=0", FALSE); |
| 694 | 2366 | } |
| 695 | | |
| 696 | | /** |
| 697 | | * Set HTTP headers in preparation for a cached page response. |
| 698 | | * |
| 699 | | * The general approach here is that anonymous users can keep a local |
| 700 | | * cache of the page, but must revalidate it on every request. Then, |
| 701 | | * they are given a '304 Not Modified' response as long as they stay |
| 702 | | * logged out and the page has not been modified. |
| 703 | | * |
| 704 | | */ |
| 705 | 2367 | function drupal_page_cache_header($cache) { |
| 706 | | // Set default values: |
| 707 | 1 | $last_modified = gmdate('D, d M Y H:i:s', $cache->created) . ' GMT'; |
| 708 | 1 | $etag = '"' . md5($last_modified) . '"'; |
| 709 | | |
| 710 | | // See if the client has provided the required HTTP headers: |
| 711 | 1 | $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE; |
| 712 | 1 | $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE; |
| 713 | | |
| 714 | 1 | if ($if_modified_since && $if_none_match |
| 715 | 0 | && $if_none_match == $etag // etag must match |
| 716 | 1 | && $if_modified_since == $last_modified) { // if-modified-since must
match |
| 717 | 0 | header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified'); |
| 718 | | // All 304 responses must send an etag if the 200 response for the same
object contained an etag |
| 719 | 0 | header("Etag: $etag"); |
| 720 | 0 | return; |
| 721 | 0 | } |
| 722 | | |
| 723 | | // Send appropriate response: |
| 724 | 1 | header("Last-Modified: $last_modified"); |
| 725 | 1 | header("ETag: $etag"); |
| 726 | | |
| 727 | | // The following headers force validation of cache: |
| 728 | 1 | header("Expires: Sun, 19 Nov 1978 05:00:00 GMT"); |
| 729 | 1 | header("Cache-Control: must-revalidate"); |
| 730 | | |
| 731 | 1 | if (variable_get('page_compression', TRUE)) { |
| 732 | | // Determine if the browser accepts gzipped data. |
| 733 | 1 | if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === FALSE &&
function_exists('gzencode')) { |
| 734 | | // Strip the gzip header and run uncompress. |
| 735 | 1 | $cache->data = gzinflate(substr(substr($cache->data, 10), 0, -8)); |
| 736 | 1 | } |
| 737 | 0 | elseif (function_exists('gzencode')) { |
| 738 | 0 | header('Content-Encoding: gzip'); |
| 739 | 0 | } |
| 740 | 1 | } |
| 741 | | |
| 742 | | // Send the original request's headers. We send them one after |
| 743 | | // another so PHP's header() function can deal with duplicate |
| 744 | | // headers. |
| 745 | 1 | $headers = explode("\n", $cache->headers); |
| 746 | 1 | foreach ($headers as $header) { |
| 747 | 1 | header($header); |
| 748 | 1 | } |
| 749 | | |
| 750 | 1 | print $cache->data; |
| 751 | 0 | } |
| 752 | | |
| 753 | | /** |
| 754 | | * Define the critical hooks that force modules to always be loaded. |
| 755 | | */ |
| 756 | 2367 | function bootstrap_hooks() { |
| 757 | 154 | return array('boot', 'exit'); |
| 758 | 0 | } |
| 759 | | |
| 760 | | /** |
| 761 | | * Unserializes and appends elements from a serialized string. |
| 762 | | * |
| 763 | | * @param $obj |
| 764 | | * The object to which the elements are appended. |
| 765 | | * @param $field |
| 766 | | * The attribute of $obj whose value should be unserialized. |
| 767 | | */ |
| 768 | 2367 | function drupal_unpack($obj, $field = 'data') { |
| 769 | 2405 | if ($obj->$field && $data = unserialize($obj->$field)) { |
| 770 | 164 | foreach ($data as $key => $value) { |
| 771 | 164 | if (!isset($obj->$key)) { |
| 772 | 164 | $obj->$key = $value; |
| 773 | 164 | } |
| 774 | 164 | } |
| 775 | 164 | } |
| 776 | 2405 | return $obj; |
| 777 | 0 | } |
| 778 | | |
| 779 | | /** |
| 780 | | * Encode special characters in a plain-text string for display as HTML. |
| 781 | | * |
| 782 | | * Uses drupal_validate_utf8 to prevent cross site scripting attacks on |
| 783 | | * Internet Explorer 6. |
| 784 | | */ |
| 785 | 2367 | function check_plain($text) { |
| 786 | 2497 | return drupal_validate_utf8($text) ? htmlspecialchars($text, ENT_QUOTES)
: ''; |
| 787 | 0 | } |
| 788 | | |
| 789 | | /** |
| 790 | | * Checks whether a string is valid UTF-8. |
| 791 | | * |
| 792 | | * All functions designed to filter input should use drupal_validate_utf8 |
| 793 | | * to ensure they operate on valid UTF-8 strings to prevent bypass of the |
| 794 | | * filter. |
| 795 | | * |
| 796 | | * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is
presented |
| 797 | | * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent |
| 798 | | * bytes. When these subsequent bytes are HTML control characters such as |
| 799 | | * quotes or angle brackets, parts of the text that were deemed safe by
filters |
| 800 | | * end up in locations that are potentially unsafe; An onerror attribute
that |
| 801 | | * is outside of a tag, and thus deemed safe by a filter, can be
interpreted |
| 802 | | * by the browser as if it were inside the tag. |
| 803 | | * |
| 804 | | * This function exploits preg_match behaviour (since PHP 4.3.5) when used |
| 805 | | * with the u modifier, as a fast way to find invalid UTF-8. When the
matched |
| 806 | | * string contains an invalid byte sequence, it will fail silently. |
| 807 | | * |
| 808 | | * preg_match may not fail on 4 and 5 octet sequences, even though they |
| 809 | | * are not supported by the specification. |
| 810 | | * |
| 811 | | * The specific preg_match behaviour is present since PHP 4.3.5. |
| 812 | | * |
| 813 | | * @param $text |
| 814 | | * The text to check. |
| 815 | | * @return |
| 816 | | * TRUE if the text is valid UTF-8, FALSE if not. |
| 817 | | */ |
| 818 | 2367 | function drupal_validate_utf8($text) { |
| 819 | 2497 | if (strlen($text) == 0) { |
| 820 | 1762 | return TRUE; |
| 821 | 0 | } |
| 822 | 2497 | return (preg_match('/^./us', $text) == 1); |
| 823 | 0 | } |
| 824 | | |
| 825 | | /** |
| 826 | | * Since $_SERVER['REQUEST_URI'] is only available on Apache, we |
| 827 | | * generate an equivalent using other environment variables. |
| 828 | | */ |
| 829 | 2367 | function request_uri() { |
| 830 | | |
| 831 | 2152 | if (isset($_SERVER['REQUEST_URI'])) { |
| 832 | 2152 | $uri = $_SERVER['REQUEST_URI']; |
| 833 | 2152 | } |
| 834 | | else { |
| 835 | 0 | if (isset($_SERVER['argv'])) { |
| 836 | 0 | $uri = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['argv'][0]; |
| 837 | 0 | } |
| 838 | 0 | elseif (isset($_SERVER['QUERY_STRING'])) { |
| 839 | 0 | $uri = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING']; |
| 840 | 0 | } |
| 841 | | else { |
| 842 | 0 | $uri = $_SERVER['SCRIPT_NAME']; |
| 843 | | } |
| 844 | | } |
| 845 | | |
| 846 | 2152 | return $uri; |
| 847 | 0 | } |
| 848 | | |
| 849 | | /** |
| 850 | | * Log a system message. |
| 851 | | * |
| 852 | | * @param $type |
| 853 | | * The category to which this message belongs. |
| 854 | | * @param $message |
| 855 | | * The message to store in the log. See t() for documentation |
| 856 | | * on how $message and $variables interact. Keep $message |
| 857 | | * translatable by not concatenating dynamic values into it! |
| 858 | | * @param $variables |
| 859 | | * Array of variables to replace in the message on display or |
| 860 | | * NULL if message is already translated or not possible to |
| 861 | | * translate. |
| 862 | | * @param $severity |
| 863 | | * The severity of the message, as per RFC 3164 |
| 864 | | * @param $link |
| 865 | | * A link to associate with the message. |
| 866 | | * |
| 867 | | * @see watchdog_severity_levels() |
| 868 | | */ |
| 869 | 2367 | function watchdog($type, $message, $variables = array(), $severity =
WATCHDOG_NOTICE, $link = NULL) { |
| 870 | 704 | global $user, $base_root; |
| 871 | | |
| 872 | 704 | static $in_error_state = FALSE; |
| 873 | | |
| 874 | | // It is possible that the error handling will itself trigger an error.
In that case, we could |
| 875 | | // end up in an infinite loop. To avoid that, we implement a simple
static semaphore. |
| 876 | 704 | if (!$in_error_state) { |
| 877 | 704 | $in_error_state = TRUE; |
| 878 | | |
| 879 | | // Prepare the fields to be logged |
| 880 | | $log_message = array( |
| 881 | 704 | 'type' => $type, |
| 882 | 704 | 'message' => $message, |
| 883 | 704 | 'variables' => $variables, |
| 884 | 704 | 'severity' => $severity, |
| 885 | 704 | 'link' => $link, |
| 886 | 704 | 'user' => $user, |
| 887 | 704 | 'request_uri' => $base_root . request_uri(), |
| 888 | 704 | 'referer' => $_SERVER['HTTP_REFERER'], |
| 889 | 704 | 'ip' => ip_address(), |
| 890 | 704 | 'timestamp' => REQUEST_TIME, |
| 891 | 704 | ); |
| 892 | | |
| 893 | | // Call the logging hooks to log/process the message |
| 894 | 704 | foreach (module_implements('watchdog', TRUE) as $module) { |
| 895 | 704 | module_invoke($module, 'watchdog', $log_message); |
| 896 | 704 | } |
| 897 | 704 | } |
| 898 | 704 | $in_error_state = FALSE; |
| 899 | 704 | } |
| 900 | | |
| 901 | | /** |
| 902 | | * Set a message which reflects the status of the performed operation. |
| 903 | | * |
| 904 | | * If the function is called with no arguments, this function returns all
set |
| 905 | | * messages without clearing them. |
| 906 | | * |
| 907 | | * @param $message |
| 908 | | * The message should begin with a capital letter and always ends with a |
| 909 | | * period '.'. |
| 910 | | * @param $type |
| 911 | | * The type of the message. One of the following values are possible: |
| 912 | | * - 'status' |
| 913 | | * - 'warning' |
| 914 | | * - 'error' |
| 915 | | * @param $repeat |
| 916 | | * If this is FALSE and the message is already set, then the message
won't |
| 917 | | * be repeated. |
| 918 | | */ |
| 919 | 2367 | function drupal_set_message($message = NULL, $type = 'status', $repeat =
TRUE) { |
| 920 | 2181 | if ($message) { |
| 921 | 385 | if (!isset($_SESSION['messages'])) { |
| 922 | 385 | $_SESSION['messages'] = array(); |
| 923 | 385 | } |
| 924 | | |
| 925 | 385 | if (!isset($_SESSION['messages'][$type])) { |
| 926 | 385 | $_SESSION['messages'][$type] = array(); |
| 927 | 385 | } |
| 928 | | |
| 929 | 385 | if ($repeat || !in_array($message, $_SESSION['messages'][$type])) { |
| 930 | 385 | $_SESSION['messages'][$type][] = $message; |
| 931 | 385 | } |
| 932 | 385 | } |
| 933 | | |
| 934 | | // messages not set when DB connection fails |
| 935 | 2181 | return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL; |
| 936 | 0 | } |
| 937 | | |
| 938 | | /** |
| 939 | | * Return all messages that have been set. |
| 940 | | * |
| 941 | | * @param $type |
| 942 | | * (optional) Only return messages of this type. |
| 943 | | * @param $clear_queue |
| 944 | | * (optional) Set to FALSE if you do not want to clear the messages queue |
| 945 | | * @return |
| 946 | | * An associative array, the key is the message type, the value an array |
| 947 | | * of messages. If the $type parameter is passed, you get only that type, |
| 948 | | * or an empty array if there are no such messages. If $type is not
passed, |
| 949 | | * all message types are returned, or an empty array if none exist. |
| 950 | | */ |
| 951 | 2367 | function drupal_get_messages($type = NULL, $clear_queue = TRUE) { |
| 952 | 1871 | if ($messages = drupal_set_message()) { |
| 953 | 347 | if ($type) { |
| 954 | 0 | if ($clear_queue) { |
| 955 | 0 | unset($_SESSION['messages'][$type]); |
| 956 | 0 | } |
| 957 | 0 | if (isset($messages[$type])) { |
| 958 | 0 | return array($type => $messages[$type]); |
| 959 | 0 | } |
| 960 | 0 | } |
| 961 | | else { |
| 962 | 347 | if ($clear_queue) { |
| 963 | 347 | unset($_SESSION['messages']); |
| 964 | 347 | } |
| 965 | 347 | return $messages; |
| 966 | | } |
| 967 | 0 | } |
| 968 | 1525 | return array(); |
| 969 | 0 | } |
| 970 | | |
| 971 | | /** |
| 972 | | * Check to see if an IP address has been blocked. |
| 973 | | * |
| 974 | | * Blocked IP addresses are stored in the database by default. However for |
| 975 | | * performance reasons we allow an override in settings.php. This allows us |
| 976 | | * to avoid querying the database at this critical stage of the bootstrap
if |
| 977 | | * an administrative interface for IP address blocking is not required. |
| 978 | | * |
| 979 | | * @param $ip string |
| 980 | | * IP address to check. |
| 981 | | * @return bool |
| 982 | | * TRUE if access is denied, FALSE if access is allowed. |
| 983 | | */ |
| 984 | 2367 | function drupal_is_denied($ip) { |
| 985 | | // Because this function is called on every page request, we first check |
| 986 | | // for an array of IP addresses in settings.php before querying the |
| 987 | | // database. |
| 988 | 2367 | $blocked_ips = variable_get('blocked_ips', NULL); |
| 989 | 2367 | if (isset($blocked_ips) && is_array($blocked_ips)) { |
| 990 | 0 | return in_array($ip, $blocked_ips); |
| 991 | 0 | } |
| 992 | | else { |
| 993 | 2367 | return (bool)db_query("SELECT 1 FROM {blocked_ips} WHERE ip = :ip",
array(':ip' => $ip))->fetchField(); |
| 994 | | } |
| 995 | 0 | } |
| 996 | | |
| 997 | | /** |
| 998 | | * Generates a default anonymous $user object. |
| 999 | | * |
| 1000 | | * @return Object - the user object. |
| 1001 | | */ |
| 1002 | 2367 | function drupal_anonymous_user($session = '') { |
| 1003 | 870 | $user = new stdClass(); |
| 1004 | 870 | $user->uid = 0; |
| 1005 | 870 | $user->hostname = ip_address(); |
| 1006 | 870 | $user->roles = array(); |
| 1007 | 870 | $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; |
| 1008 | 870 | $user->session = $session; |
| 1009 | 870 | $user->cache = 0; |
| 1010 | 870 | return $user; |
| 1011 | 0 | } |
| 1012 | | |
| 1013 | | /** |
| 1014 | | * A string describing a phase of Drupal to load. Each phase adds to the |
| 1015 | | * previous one, so invoking a later phase automatically runs the earlier |
| 1016 | | * phases too. The most important usage is that if you want to access the |
| 1017 | | * Drupal database from a script without loading anything else, you can |
| 1018 | | * include bootstrap.inc, and call
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE). |
| 1019 | | * |
| 1020 | | * @param $phase |
| 1021 | | * A constant. Allowed values are: |
| 1022 | | * DRUPAL_BOOTSTRAP_CONFIGURATION: initialize configuration. |
| 1023 | | * DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE: try to call a non-database cache
fetch routine. |
| 1024 | | * DRUPAL_BOOTSTRAP_DATABASE: initialize database layer. |
| 1025 | | * DRUPAL_BOOTSTRAP_ACCESS: identify and reject banned hosts. |
| 1026 | | * DRUPAL_BOOTSTRAP_SESSION: initialize session handling. |
| 1027 | | * DRUPAL_BOOTSTRAP_VARIABLES: initialize variable handling. |
| 1028 | | * DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: load bootstrap.inc and module.inc,
start |
| 1029 | | * the variable system and try to serve a page from the cache. |
| 1030 | | * DRUPAL_BOOTSTRAP_LANGUAGE: identify the language used on the page. |
| 1031 | | * DRUPAL_BOOTSTRAP_PATH: set $_GET['q'] to Drupal path of request. |
| 1032 | | * DRUPAL_BOOTSTRAP_FULL: Drupal is fully loaded, validate and fix
input data. |
| 1033 | | */ |
| 1034 | 2367 | function drupal_bootstrap($phase = NULL) { |
| 1035 | 2497 | static $phases = array(DRUPAL_BOOTSTRAP_CONFIGURATION,
DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE, DRUPAL_BOOTSTRAP_DATABASE,
DRUPAL_BOOTSTRAP_ACCESS, DRUPAL_BOOTSTRAP_SESSION,
DRUPAL_BOOTSTRAP_VARIABLES, DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE,
DRUPAL_BOOTSTRAP_LANGUAGE, DRUPAL_BOOTSTRAP_PATH, DRUPAL_BOOTSTRAP_FULL),
$completed_phase = -1; |
| 1036 | | |
| 1037 | 2497 | if (isset($phase)) { |
| 1038 | 2497 | while ($phases && $phase > $completed_phase) { |
| 1039 | 2367 | $current_phase = array_shift($phases); |
| 1040 | 2367 | _drupal_bootstrap($current_phase); |
| 1041 | 2367 | $completed_phase = $current_phase; |
| 1042 | 2367 | } |
| 1043 | 2496 | } |
| 1044 | 2496 | return $completed_phase; |
| 1045 | 0 | } |
| 1046 | | |
| 1047 | | /** |
| 1048 | | * Return the current bootstrap phase for this Drupal process. The |
| 1049 | | * current phase is the one most recently completed by |
| 1050 | | * drupal_bootstrap(). |
| 1051 | | * |
| 1052 | | * @see drupal_bootstrap |
| 1053 | | */ |
| 1054 | 2367 | function drupal_get_bootstrap_phase() { |
| 1055 | 136 | return drupal_bootstrap(); |
| 1056 | 0 | } |
| 1057 | | |
| 1058 | 2367 | function _drupal_bootstrap($phase) { |
| 1059 | 2367 | global $conf; |
| 1060 | | |
| 1061 | | switch ($phase) { |
| 1062 | | |
| 1063 | 2367 | case DRUPAL_BOOTSTRAP_CONFIGURATION: |
| 1064 | 2367 | drupal_initialize_variables(); |
| 1065 | | // Start a page timer: |
| 1066 | 2367 | timer_start('page'); |
| 1067 | | // Initialize the configuration |
| 1068 | 2367 | conf_init(); |
| 1069 | 2367 | break; |
| 1070 | | |
| 1071 | 2367 | case DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE: |
| 1072 | | // Allow specifying special cache handlers in settings.php, like |
| 1073 | | // using memcached or files for storing cache information. |
| 1074 | 2367 | require_once DRUPAL_ROOT . '/' . variable_get('cache_inc',
'includes/cache.inc'); |
| 1075 | | // If the page_cache_fastpath is set to TRUE in settings.php and |
| 1076 | | // page_cache_fastpath (implemented in the special implementation of |
| 1077 | | // cache.inc) printed the page and indicated this with a returned
TRUE |
| 1078 | | // then we are done. |
| 1079 | 2367 | if (variable_get('page_cache_fastpath', FALSE) &&
page_cache_fastpath()) { |
| 1080 | 0 | exit; |
| 1081 | 0 | } |
| 1082 | 2367 | break; |
| 1083 | | |
| 1084 | 2367 | case DRUPAL_BOOTSTRAP_DATABASE: |
| 1085 | | // Initialize the database system. Note that the connection |
| 1086 | | // won't be initialized until it is actually requested. |
| 1087 | 2367 | require_once DRUPAL_ROOT . '/includes/database/database.inc'; |
| 1088 | | // Register autoload functions so that we can access classes and
interfaces. |
| 1089 | 2367 | spl_autoload_register('drupal_autoload_class'); |
| 1090 | 2367 | spl_autoload_register('drupal_autoload_interface'); |
| 1091 | 2367 | break; |
| 1092 | | |
| 1093 | 2367 | case DRUPAL_BOOTSTRAP_ACCESS: |
| 1094 | | // Deny access to blocked IP addresses - t() is not yet available. |
| 1095 | 2367 | if (drupal_is_denied(ip_address())) { |
| 1096 | 0 | header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); |
| 1097 | 0 | print 'Sorry, ' . check_plain(ip_address()) . ' has been banned.'; |
| 1098 | 0 | exit(); |
| 1099 | 0 | } |
| 1100 | 2367 | break; |
| 1101 | | |
| 1102 | 2367 | case DRUPAL_BOOTSTRAP_SESSION: |
| 1103 | 2367 | require_once DRUPAL_ROOT . '/' . variable_get('session_inc',
'includes/session.inc'); |
| 1104 | 2367 | session_set_save_handler('_sess_open', '_sess_close', '_sess_read',
'_sess_write', '_sess_destroy_sid', '_sess_gc'); |
| 1105 | 2367 | session_start(); |
| 1106 | 2367 | break; |
| 1107 | | |
| 1108 | 2367 | case DRUPAL_BOOTSTRAP_VARIABLES: |
| 1109 | | // Initialize configuration variables, using values from settings.php
if available. |
| 1110 | 2367 | $conf = variable_init(isset($conf) ? $conf : array()); |
| 1111 | 2367 | break; |
| 1112 | | |
| 1113 | 2367 | case DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: |
| 1114 | | // Load module handling. |
| 1115 | 2367 | require_once DRUPAL_ROOT . '/includes/module.inc'; |
| 1116 | 2367 | $cache_mode = variable_get('cache', CACHE_DISABLED); |
| 1117 | | // Get the page from the cache. |
| 1118 | 2367 | $cache = $cache_mode == CACHE_DISABLED ? '' : page_get_cache(); |
| 1119 | | // If the skipping of the bootstrap hooks is not enforced, call
hook_boot. |
| 1120 | 2367 | if ($cache_mode != CACHE_AGGRESSIVE) { |
| 1121 | 2367 | bootstrap_invoke_all('boot'); |
| 1122 | 2367 | } |
| 1123 | | // If there is a cached page, display it. |
| 1124 | 2367 | if ($cache) { |
| 1125 | 1 | drupal_page_cache_header($cache); |
| 1126 | | // If the skipping of the bootstrap hooks is not enforced, call
hook_exit. |
| 1127 | 0 | if ($cache_mode != CACHE_AGGRESSIVE) { |
| 1128 | 0 | bootstrap_invoke_all('exit'); |
| 1129 | 0 | } |
| 1130 | | // We are done. |
| 1131 | 0 | exit; |
| 1132 | 0 | } |
| 1133 | | // Prepare for non-cached page workflow. |
| 1134 | 2366 | drupal_page_header(); |
| 1135 | 2366 | break; |
| 1136 | | |
| 1137 | 2366 | case DRUPAL_BOOTSTRAP_LANGUAGE: |
| 1138 | 2366 | drupal_init_language(); |
| 1139 | 2366 | break; |
| 1140 | | |
| 1141 | 2366 | case DRUPAL_BOOTSTRAP_PATH: |
| 1142 | 2366 | require_once DRUPAL_ROOT . '/includes/path.inc'; |
| 1143 | | // Initialize $_GET['q'] prior to loading modules and invoking
hook_init(). |
| 1144 | 2366 | drupal_init_path(); |
| 1145 | 2366 | break; |
| 1146 | | |
| 1147 | 2366 | case DRUPAL_BOOTSTRAP_FULL: |
| 1148 | 2366 | require_once DRUPAL_ROOT . '/includes/common.inc'; |
| 1149 | 2366 | _drupal_bootstrap_full(); |
| 1150 | 2366 | break; |
| 1151 | 0 | } |
| 1152 | 2367 | } |
| 1153 | | |
| 1154 | | /** |
| 1155 | | * Enables use of the theme system without requiring database access. |
| 1156 | | * |
| 1157 | | * Loads and initializes the theme system for site installs, updates and
when |
| 1158 | | * the site is in offline mode. This also applies when the database fails. |
| 1159 | | * |
| 1160 | | * @see _drupal_maintenance_theme() |
| 1161 | | */ |
| 1162 | 2367 | function drupal_maintenance_theme() { |
| 1163 | 0 | require_once DRUPAL_ROOT . '/includes/theme.maintenance.inc'; |
| 1164 | 0 | _drupal_maintenance_theme(); |
| 1165 | 0 | } |
| 1166 | | |
| 1167 | | /** |
| 1168 | | * Return the name of the localisation function. Use in code that needs to |
| 1169 | | * run both during installation and normal operation. |
| 1170 | | */ |
| 1171 | 2367 | function get_t() { |
| 1172 | 2496 | static $t; |
| 1173 | 2496 | if (is_null($t)) { |
| 1174 | 2366 | $t = function_exists('install_main') ? 'st' : 't'; |
| 1175 | 2366 | } |
| 1176 | 2496 | return $t; |
| 1177 | 0 | } |
| 1178 | | |
| 1179 | | /** |
| 1180 | | * Choose a language for the current page, based on site and user
preferences. |
| 1181 | | */ |
| 1182 | 2367 | function drupal_init_language() { |
| 1183 | 2367 | global $language, $user; |
| 1184 | | |
| 1185 | | // Ensure the language is correctly returned, even without multilanguage
support. |
| 1186 | | // Useful for eg. XML/HTML 'lang' attributes. |
| 1187 | 2367 | if (variable_get('language_count', 1) == 1) { |
| 1188 | 2294 | $language = language_default(); |
| 1189 | 2294 | } |
| 1190 | | else { |
| 1191 | 73 | include_once DRUPAL_ROOT . '/includes/language.inc'; |
| 1192 | 73 | $language = language_initialize(); |
| 1193 | | } |
| 1194 | 2367 | } |
| 1195 | | |
| 1196 | | /** |
| 1197 | | * Get a list of languages set up indexed by the specified key |
| 1198 | | * |
| 1199 | | * @param $field The field to index the list with. |
| 1200 | | * @param $reset Boolean to request a reset of the list. |
| 1201 | | */ |
| 1202 | 2367 | function language_list($field = 'language', $reset = FALSE) { |
| 1203 | 106 | static $languages = NULL; |
| 1204 | | |
| 1205 | | // Reset language list |
| 1206 | 106 | if ($reset) { |
| 1207 | 9 | $languages = NULL; |
| 1208 | 9 | } |
| 1209 | | |
| 1210 | | // Init language list |
| 1211 | 106 | if (!isset($languages)) { |
| 1212 | 106 | if (variable_get('language_count', 1) > 1 || module_exists('locale')) { |
| 1213 | 89 | $languages['language'] = db_query('SELECT * FROM {languages} ORDER BY
weight ASC, name ASC')->fetchAllAssoc('language'); |
| 1214 | 89 | } |
| 1215 | | else { |
| 1216 | | // No locale module, so use the default language only. |
| 1217 | 17 | $default = language_default(); |
| 1218 | 17 | $languages['language'][$default->language] = $default; |
| 1219 | | } |
| 1220 | 106 | } |
| 1221 | | |
| 1222 | | // Return the array indexed by the right field |
| 1223 | 106 | if (!isset($languages[$field])) { |
| 1224 | 74 | $languages[$field] = array(); |
| 1225 | 74 | foreach ($languages['language'] as $lang) { |
| 1226 | | // Some values should be collected into an array |
| 1227 | 74 | if (in_array($field, array('enabled', 'weight'))) { |
| 1228 | 74 | $languages[$field][$lang->$field][$lang->language] = $lang; |
| 1229 | 74 | } |
| 1230 | | else { |
| 1231 | 0 | $languages[$field][$lang->$field] = $lang; |
| 1232 | | } |
| 1233 | 74 | } |
| 1234 | 74 | } |
| 1235 | 106 | return $languages[$field]; |
| 1236 | 0 | } |
| 1237 | | |
| 1238 | | /** |
| 1239 | | * Default language used on the site |
| 1240 | | * |
| 1241 | | * @param $property |
| 1242 | | * Optional property of the language object to return |
| 1243 | | */ |
| 1244 | 2367 | function language_default($property = NULL) { |
| 1245 | 2365 | $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' => '')); |
| 1246 | 2365 | return $property ? $language->$property : $language; |
| 1247 | 0 | } |
| 1248 | | |
| 1249 | | /** |
| 1250 | | * If Drupal is behind a reverse proxy, we use the X-Forwarded-For header |
| 1251 | | * instead of $_SERVER['REMOTE_ADDR'], which would be the IP address of |
| 1252 | | * the proxy server, and not the client's. If Drupal is run in a cluster |
| 1253 | | * we use the X-Cluster-Client-Ip header instead. |
| 1254 | | * |
| 1255 | | * @param $reset |
| 1256 | | * Reset the current IP address saved in static. |
| 1257 | | * @return |
| 1258 | | * IP address of client machine, adjusted for reverse proxy and/or
cluster |
| 1259 | | * environments. |
| 1260 | | */ |
| 1261 | 2367 | function ip_address($reset = FALSE) { |
| 1262 | 2497 | static $ip_address = NULL; |
| 1263 | | |
| 1264 | 2497 | if (!isset($ip_address) || $reset) { |
| 1265 | 2368 | $ip_address = $_SERVER['REMOTE_ADDR']; |
| 1266 | | |
| 1267 | 2368 | if (variable_get('reverse_proxy', 0)) { |
| 1268 | 1 | if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) { |
| 1269 | | // If an array of known reverse proxy IPs is provided, then trust |
| 1270 | | // the XFF header if request really comes from one of them. |
| 1271 | 1 | $reverse_proxy_addresses = variable_get('reverse_proxy_addresses',
array()); |
| 1272 | 1 | if (!empty($reverse_proxy_addresses) && in_array($ip_address,
$reverse_proxy_addresses, TRUE)) { |
| 1273 | | // If there are several arguments, we need to check the most |
| 1274 | | // recently added one, i.e. the last one. |
| 1275 | 1 | $ip_address_parts = explode(',',
$_SERVER['HTTP_X_FORWARDED_FOR']); |
| 1276 | 1 | $ip_address = array_pop($ip_address_parts); |
| 1277 | 1 | } |
| 1278 | 1 | } |
| 1279 | | |
| 1280 | | // When Drupal is run in a cluster environment, REMOTE_ADDR contains
the IP |
| 1281 | | // address of a server in the cluster, while the IP address of the
client is |
| 1282 | | // stored in HTTP_X_CLUSTER_CLIENT_IP. |
| 1283 | 1 | if (array_key_exists('HTTP_X_CLUSTER_CLIENT_IP', $_SERVER)) { |
| 1284 | 1 | $ip_address = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP']; |
| 1285 | 1 | } |
| 1286 | 1 | } |
| 1287 | 2368 | } |
| 1288 | | |
| 1289 | 2497 | return $ip_address; |
| 1290 | 0 | } |
| 1291 | | |
| 1292 | | /** |
| 1293 | | * @ingroup schemaapi |
| 1294 | | * @{ |
| 1295 | | */ |
| 1296 | | |
| 1297 | | /** |
| 1298 | | * Get the schema definition of a table, or the whole database schema. |
| 1299 | | * |
| 1300 | | * The returned schema will include any modifications made by any |
| 1301 | | * module that implements hook_schema_alter(). |
| 1302 | | * |
| 1303 | | * @param $table |
| 1304 | | * The name of the table. If not given, the schema of all tables is
returned. |
| 1305 | | * @param $rebuild |
| 1306 | | * If true, the schema will be rebuilt instead of retrieved from the
cache. |
| 1307 | | */ |
| 1308 | 2367 | function drupal_get_schema($table = NULL, $rebuild = FALSE) { |
| 1309 | 647 | static $schema = array(); |
| 1310 | | |
| 1311 | 647 | if (empty($schema) || $rebuild) { |
| 1312 | | // Try to load the schema from cache. |
| 1313 | 647 | if (!$rebuild && $cached = cache_get('schema')) { |
| 1314 | 513 | $schema = $cached->data; |
| 1315 | 513 | } |
| 1316 | | // Otherwise, rebuild the schema cache. |
| 1317 | | else { |
| 1318 | 134 | $schema = array(); |
| 1319 | | // Load the .install files to get hook_schema. |
| 1320 | | // On some databases this function may be called before bootstrap has |
| 1321 | | // been completed, so we force the functions we need to load just in
case. |
| 1322 | 134 | if (drupal_function_exists('module_load_all_includes')) { |
| 1323 | | |
| 1324 | | // There is currently a bug in module_list() where it caches what
it |
| 1325 | | // was last called with, which is not always what you want. |
| 1326 | | // module_load_all_includes() calls module_list(), but if this
function |
| 1327 | | // is called very early in the bootstrap process then it will be |
| 1328 | | // uninitialized and therefore return no modules. Instead, we have
to |
| 1329 | | // "prime" module_list() here to to values we want, specifically |
| 1330 | | // "yes rebuild the list and don't limit to bootstrap". |
| 1331 | | // TODO: Remove this call after http://drupal.org/node/222109 is
fixed. |
| 1332 | 134 | module_list(TRUE, FALSE); |
| 1333 | 134 | module_load_all_includes('install'); |
| 1334 | 134 | } |
| 1335 | | |
| 1336 | | // Invoke hook_schema for all modules. |
| 1337 | 134 | foreach (module_implements('schema') as $module) { |
| 1338 | 134 | $current = module_invoke($module, 'schema'); |
| 1339 | 134 | require_once DRUPAL_ROOT . '/includes/common.inc'; |
| 1340 | 134 | if (drupal_function_exists('_drupal_initialize_schema')) { |
| 1341 | 134 | _drupal_initialize_schema($module, $current); |
| 1342 | 134 | } |
| 1343 | | |
| 1344 | 134 | $schema = array_merge($schema, $current); |
| 1345 | 134 | } |
| 1346 | | |
| 1347 | 134 | if (drupal_function_exists('drupal_alter')) { |
| 1348 | 134 | drupal_alter('schema', $schema); |
| 1349 | 134 | } |
| 1350 | | |
| 1351 | 134 | if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL) { |
| 1352 | 134 | cache_set('schema', $schema); |
| 1353 | 134 | } |
| 1354 | | } |
| 1355 | 647 | } |
| 1356 | | |
| 1357 | 647 | if (!isset($table)) { |
| 1358 | 134 | return $schema; |
| 1359 | 0 | } |
| 1360 | 624 | elseif (isset($schema[$table])) { |
| 1361 | 624 | return $schema[$table]; |
| 1362 | 0 | } |
| 1363 | | else { |
| 1364 | 0 | return FALSE; |
| 1365 | | } |
| 1366 | 0 | } |
| 1367 | | |
| 1368 | | /** |
| 1369 | | * @} End of "ingroup schemaapi". |
| 1370 | | */ |
| 1371 | | |
| 1372 | | |
| 1373 | | /** |
| 1374 | | * @ingroup registry |
| 1375 | | * @{ |
| 1376 | | */ |
| 1377 | | |
| 1378 | | /** |
| 1379 | | * Confirm that a function is available. |
| 1380 | | * |
| 1381 | | * If the function is already available, this function does nothing. |
| 1382 | | * If the function is not available, it tries to load the file where the |
| 1383 | | * function lives. If the file is not available, it returns false, so that
it |
| 1384 | | * can be used as a drop-in replacement for function_exists(). |
| 1385 | | * |
| 1386 | | * @param $function |
| 1387 | | * The name of the function to check or load. |
| 1388 | | * @return |
| 1389 | | * TRUE if the function is now available, FALSE otherwise. |
| 1390 | | */ |
| 1391 | 2367 | function drupal_function_exists($function) { |
| 1392 | 2496 | static $checked = array(); |
| 1393 | | |
| 1394 | 2496 | if (defined('MAINTENANCE_MODE')) { |
| 1395 | 0 | return function_exists($function); |
| 1396 | 0 | } |
| 1397 | | |
| 1398 | 2496 | if (isset($checked[$function])) { |
| 1399 | 2378 | return $checked[$function]; |
| 1400 | 0 | } |
| 1401 | 2496 | $checked[$function] = FALSE; |
| 1402 | | |
| 1403 | 2496 | if (function_exists($function)) { |
| 1404 | 2496 | registry_mark_code('function', $function); |
| 1405 | 2496 | $checked[$function] = TRUE; |
| 1406 | 2496 | return TRUE; |
| 1407 | 0 | } |
| 1408 | | |
| 1409 | 1528 | $checked[$function] = _registry_check_code('function', $function); |
| 1410 | | |
| 1411 | 1528 | return $checked[$function]; |
| 1412 | 0 | } |
| 1413 | | |
| 1414 | | /** |
| 1415 | | * Confirm that an interface is available. |
| 1416 | | * |
| 1417 | | * This function parallels drupal_function_exists(), but is rarely |
| 1418 | | * called directly. Instead, it is registered as an spl_autoload() |
| 1419 | | * handler, and PHP calls it for us when necessary. |
| 1420 | | * |
| 1421 | | * @param $interface |
| 1422 | | * The name of the interface to check or load. |
| 1423 | | * @return |
| 1424 | | * TRUE if the interface is currently available, FALSE otherwise. |
| 1425 | | */ |
| 1426 | 2367 | function drupal_autoload_interface($interface) { |
| 1427 | 2370 | return _registry_check_code('interface', $interface); |
| 1428 | 0 | } |
| 1429 | | |
| 1430 | | /** |
| 1431 | | * Confirm that a class is available. |
| 1432 | | * |
| 1433 | | * This function parallels drupal_function_exists(), but is rarely |
| 1434 | | * called directly. Instead, it is registered as an spl_autoload() |
| 1435 | | * handler, and PHP calls it for us when necessary. |
| 1436 | | * |
| 1437 | | * @param $class |
| 1438 | | * The name of the class to check or load. |
| 1439 | | * @return |
| 1440 | | * TRUE if the class is currently available, FALSE otherwise. |
| 1441 | | */ |
| 1442 | 2367 | function drupal_autoload_class($class) { |
| 1443 | 2489 | return _registry_check_code('class', $class); |
| 1444 | 0 | } |
| 1445 | | |
| 1446 | | /** |
| 1447 | | * Helper to check for a resource in the registry. |
| 1448 | | */ |
| 1449 | 2367 | function _registry_check_code($type, $name) { |
| 1450 | 2490 | $file = db_query("SELECT filename FROM {registry} WHERE name = :name AND
type = :type", array( |
| 1451 | 2490 | ':name' => $name, |
| 1452 | 2490 | ':type' => $type, |
| 1453 | 2490 | )) |
| 1454 | 2490 | ->fetchField(); |
| 1455 | 2490 | if ($file) { |
| 1456 | 2348 | require_once DRUPAL_ROOT . '/' . $file; |
| 1457 | 2348 | registry_mark_code($type, $name); |
| 1458 | 2348 | return TRUE; |
| 1459 | 0 | } |
| 1460 | 2376 | return FALSE; |
| 1461 | 0 | } |
| 1462 | | |
| 1463 | | /** |
| 1464 | | * Collect the resources used for this request. |
| 1465 | | * |
| 1466 | | * @param $type |
| 1467 | | * The type of resource. |
| 1468 | | * @param $name |
| 1469 | | * The name of the resource. |
| 1470 | | * @param $return |
| 1471 | | * Boolean flag to indicate whether to return the resources. |
| 1472 | | */ |
| 1473 | 2367 | function registry_mark_code($type, $name, $return = FALSE) { |
| 1474 | 2496 | static $resources = array(); |
| 1475 | | |
| 1476 | 2496 | if ($type && $name) { |
| 1477 | 2496 | if (!isset($resources[$type])) { |
| 1478 | 2366 | $resources[$type] = array(); |
| 1479 | 2366 | } |
| 1480 | 2496 | if (!in_array($name, $resources[$type])) { |
| 1481 | 2496 | $resources[$type][] = $name; |
| 1482 | 2496 | } |
| 1483 | 2496 | } |
| 1484 | | |
| 1485 | 2496 | if ($return) { |
| 1486 | 1756 | return $resources; |
| 1487 | 0 | } |
| 1488 | 2496 | } |
| 1489 | | |
| 1490 | | /** |
| 1491 | | * Rescan all enabled modules and rebuild the registry. |
| 1492 | | * |
| 1493 | | * Rescans all code in modules or includes directory, storing a mapping of |
| 1494 | | * each function, file, and hook implementation in the database. |
| 1495 | | */ |
| 1496 | 2367 | function registry_rebuild() { |
| 1497 | 146 | require_once DRUPAL_ROOT . '/includes/registry.inc'; |
| 1498 | 146 | _registry_rebuild(); |
| 1499 | 146 | } |
| 1500 | | |
| 1501 | | /** |
| 1502 | | * Save the files required by the registry for this path. |
| 1503 | | */ |
| 1504 | 2367 | function registry_cache_path_files() { |
| 1505 | 1756 | if ($used_code = registry_mark_code(NULL, NULL, TRUE)) { |
| 1506 | 1756 | $files = array(); |
| 1507 | 1756 | $type_sql = array(); |
| 1508 | 1756 | $params = array(); |
| 1509 | | |
| 1510 | 1756 | $select = db_select('registry')->distinct(); |
| 1511 | 1756 | $select->addField('registry', 'filename'); |
| 1512 | | |
| 1513 | | // This creates a series of 2-clause AND conditions that are then ORed
together. |
| 1514 | 1756 | $ors = db_or(); |
| 1515 | 1756 | foreach ($used_code as $type => $names) { |
| 1516 | 1756 | $and = db_and() |
| 1517 | 1756 | ->condition('name', $names, 'IN') |
| 1518 | 1756 | ->condition('type', $type); |
| 1519 | 1756 | $ors->condition($and); |
| 1520 | 1756 | } |
| 1521 | 1756 | $select->condition($ors); |
| 1522 | 1756 | $files = $select->execute()->fetchCol(); |
| 1523 | | |
| 1524 | 1756 | if ($files) { |
| 1525 | 1756 | sort($files); |
| 1526 | | // Only write this to cache if the file list we are going to cache |
| 1527 | | // is different to what we loaded earlier in the request. |
| 1528 | 1756 | if ($files != registry_load_path_files(TRUE)) { |
| 1529 | 1400 | $menu = menu_get_item(); |
| 1530 | 1400 | cache_set('registry:' . $menu['path'], implode(';', $files),
'cache_registry'); |
| 1531 | 1400 | } |
| 1532 | 1756 | } |
| 1533 | 1756 | } |
| 1534 | 1756 | } |
| 1535 | | |
| 1536 | | /** |
| 1537 | | * registry_load_path_files |
| 1538 | | */ |
| 1539 | 2367 | function registry_load_path_files($return = FALSE) { |
| 1540 | 2346 | static $file_cache_data = array(); |
| 1541 | 2346 | if ($return) { |
| 1542 | 1756 | sort($file_cache_data); |
| 1543 | 1756 | return $file_cache_data; |
| 1544 | 0 | } |
| 1545 | 2333 | $menu = menu_get_item(); |
| 1546 | 2333 | $cache = cache_get('registry:' . $menu['path'], 'cache_registry'); |
| 1547 | 2333 | if (!empty($cache->data)) { |
| 1548 | 1647 | foreach(explode(';', $cache->data) as $file) { |
| 1549 | 1647 | require_once DRUPAL_ROOT . '/' . $file; |
| 1550 | 1647 | $file_cache_data[] = $file; |
| 1551 | 1647 | } |
| 1552 | 1647 | } |
| 1553 | 2333 | } |
| 1554 | | |
| 1555 | | /** |
| 1556 | | * @} End of "ingroup registry". |
| 1557 | | */ |
| 1558 | 2367 | |