annotate includes/bootstrap.inc @ 13:134d4b2e75f6

updated quicktabs and google analytics modules
author danieleb <danielebarchiesi@me.com>
date Tue, 29 Oct 2013 13:48:59 +0000
parents ff03f76ab3fe
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * @file
danielebarchiesi@0 5 * Functions that need to be loaded on every Drupal request.
danielebarchiesi@0 6 */
danielebarchiesi@0 7
danielebarchiesi@0 8 /**
danielebarchiesi@0 9 * The current system version.
danielebarchiesi@0 10 */
danielebarchiesi@0 11 define('VERSION', '7.23');
danielebarchiesi@0 12
danielebarchiesi@0 13 /**
danielebarchiesi@0 14 * Core API compatibility.
danielebarchiesi@0 15 */
danielebarchiesi@0 16 define('DRUPAL_CORE_COMPATIBILITY', '7.x');
danielebarchiesi@0 17
danielebarchiesi@0 18 /**
danielebarchiesi@0 19 * Minimum supported version of PHP.
danielebarchiesi@0 20 */
danielebarchiesi@0 21 define('DRUPAL_MINIMUM_PHP', '5.2.4');
danielebarchiesi@0 22
danielebarchiesi@0 23 /**
danielebarchiesi@0 24 * Minimum recommended value of PHP memory_limit.
danielebarchiesi@0 25 */
danielebarchiesi@0 26 define('DRUPAL_MINIMUM_PHP_MEMORY_LIMIT', '32M');
danielebarchiesi@0 27
danielebarchiesi@0 28 /**
danielebarchiesi@0 29 * Error reporting level: display no errors.
danielebarchiesi@0 30 */
danielebarchiesi@0 31 define('ERROR_REPORTING_HIDE', 0);
danielebarchiesi@0 32
danielebarchiesi@0 33 /**
danielebarchiesi@0 34 * Error reporting level: display errors and warnings.
danielebarchiesi@0 35 */
danielebarchiesi@0 36 define('ERROR_REPORTING_DISPLAY_SOME', 1);
danielebarchiesi@0 37
danielebarchiesi@0 38 /**
danielebarchiesi@0 39 * Error reporting level: display all messages.
danielebarchiesi@0 40 */
danielebarchiesi@0 41 define('ERROR_REPORTING_DISPLAY_ALL', 2);
danielebarchiesi@0 42
danielebarchiesi@0 43 /**
danielebarchiesi@0 44 * Indicates that the item should never be removed unless explicitly selected.
danielebarchiesi@0 45 *
danielebarchiesi@0 46 * The item may be removed using cache_clear_all() with a cache ID.
danielebarchiesi@0 47 */
danielebarchiesi@0 48 define('CACHE_PERMANENT', 0);
danielebarchiesi@0 49
danielebarchiesi@0 50 /**
danielebarchiesi@0 51 * Indicates that the item should be removed at the next general cache wipe.
danielebarchiesi@0 52 */
danielebarchiesi@0 53 define('CACHE_TEMPORARY', -1);
danielebarchiesi@0 54
danielebarchiesi@0 55 /**
danielebarchiesi@0 56 * @defgroup logging_severity_levels Logging severity levels
danielebarchiesi@0 57 * @{
danielebarchiesi@0 58 * Logging severity levels as defined in RFC 3164.
danielebarchiesi@0 59 *
danielebarchiesi@0 60 * The WATCHDOG_* constant definitions correspond to the logging severity levels
danielebarchiesi@0 61 * defined in RFC 3164, section 4.1.1. PHP supplies predefined LOG_* constants
danielebarchiesi@0 62 * for use in the syslog() function, but their values on Windows builds do not
danielebarchiesi@0 63 * correspond to RFC 3164. The associated PHP bug report was closed with the
danielebarchiesi@0 64 * comment, "And it's also not a bug, as Windows just have less log levels,"
danielebarchiesi@0 65 * and "So the behavior you're seeing is perfectly normal."
danielebarchiesi@0 66 *
danielebarchiesi@0 67 * @see http://www.faqs.org/rfcs/rfc3164.html
danielebarchiesi@0 68 * @see http://bugs.php.net/bug.php?id=18090
danielebarchiesi@0 69 * @see http://php.net/manual/function.syslog.php
danielebarchiesi@0 70 * @see http://php.net/manual/network.constants.php
danielebarchiesi@0 71 * @see watchdog()
danielebarchiesi@0 72 * @see watchdog_severity_levels()
danielebarchiesi@0 73 */
danielebarchiesi@0 74
danielebarchiesi@0 75 /**
danielebarchiesi@0 76 * Log message severity -- Emergency: system is unusable.
danielebarchiesi@0 77 */
danielebarchiesi@0 78 define('WATCHDOG_EMERGENCY', 0);
danielebarchiesi@0 79
danielebarchiesi@0 80 /**
danielebarchiesi@0 81 * Log message severity -- Alert: action must be taken immediately.
danielebarchiesi@0 82 */
danielebarchiesi@0 83 define('WATCHDOG_ALERT', 1);
danielebarchiesi@0 84
danielebarchiesi@0 85 /**
danielebarchiesi@0 86 * Log message severity -- Critical conditions.
danielebarchiesi@0 87 */
danielebarchiesi@0 88 define('WATCHDOG_CRITICAL', 2);
danielebarchiesi@0 89
danielebarchiesi@0 90 /**
danielebarchiesi@0 91 * Log message severity -- Error conditions.
danielebarchiesi@0 92 */
danielebarchiesi@0 93 define('WATCHDOG_ERROR', 3);
danielebarchiesi@0 94
danielebarchiesi@0 95 /**
danielebarchiesi@0 96 * Log message severity -- Warning conditions.
danielebarchiesi@0 97 */
danielebarchiesi@0 98 define('WATCHDOG_WARNING', 4);
danielebarchiesi@0 99
danielebarchiesi@0 100 /**
danielebarchiesi@0 101 * Log message severity -- Normal but significant conditions.
danielebarchiesi@0 102 */
danielebarchiesi@0 103 define('WATCHDOG_NOTICE', 5);
danielebarchiesi@0 104
danielebarchiesi@0 105 /**
danielebarchiesi@0 106 * Log message severity -- Informational messages.
danielebarchiesi@0 107 */
danielebarchiesi@0 108 define('WATCHDOG_INFO', 6);
danielebarchiesi@0 109
danielebarchiesi@0 110 /**
danielebarchiesi@0 111 * Log message severity -- Debug-level messages.
danielebarchiesi@0 112 */
danielebarchiesi@0 113 define('WATCHDOG_DEBUG', 7);
danielebarchiesi@0 114
danielebarchiesi@0 115 /**
danielebarchiesi@0 116 * @} End of "defgroup logging_severity_levels".
danielebarchiesi@0 117 */
danielebarchiesi@0 118
danielebarchiesi@0 119 /**
danielebarchiesi@0 120 * First bootstrap phase: initialize configuration.
danielebarchiesi@0 121 */
danielebarchiesi@0 122 define('DRUPAL_BOOTSTRAP_CONFIGURATION', 0);
danielebarchiesi@0 123
danielebarchiesi@0 124 /**
danielebarchiesi@0 125 * Second bootstrap phase: try to serve a cached page.
danielebarchiesi@0 126 */
danielebarchiesi@0 127 define('DRUPAL_BOOTSTRAP_PAGE_CACHE', 1);
danielebarchiesi@0 128
danielebarchiesi@0 129 /**
danielebarchiesi@0 130 * Third bootstrap phase: initialize database layer.
danielebarchiesi@0 131 */
danielebarchiesi@0 132 define('DRUPAL_BOOTSTRAP_DATABASE', 2);
danielebarchiesi@0 133
danielebarchiesi@0 134 /**
danielebarchiesi@0 135 * Fourth bootstrap phase: initialize the variable system.
danielebarchiesi@0 136 */
danielebarchiesi@0 137 define('DRUPAL_BOOTSTRAP_VARIABLES', 3);
danielebarchiesi@0 138
danielebarchiesi@0 139 /**
danielebarchiesi@0 140 * Fifth bootstrap phase: initialize session handling.
danielebarchiesi@0 141 */
danielebarchiesi@0 142 define('DRUPAL_BOOTSTRAP_SESSION', 4);
danielebarchiesi@0 143
danielebarchiesi@0 144 /**
danielebarchiesi@0 145 * Sixth bootstrap phase: set up the page header.
danielebarchiesi@0 146 */
danielebarchiesi@0 147 define('DRUPAL_BOOTSTRAP_PAGE_HEADER', 5);
danielebarchiesi@0 148
danielebarchiesi@0 149 /**
danielebarchiesi@0 150 * Seventh bootstrap phase: find out language of the page.
danielebarchiesi@0 151 */
danielebarchiesi@0 152 define('DRUPAL_BOOTSTRAP_LANGUAGE', 6);
danielebarchiesi@0 153
danielebarchiesi@0 154 /**
danielebarchiesi@0 155 * Final bootstrap phase: Drupal is fully loaded; validate and fix input data.
danielebarchiesi@0 156 */
danielebarchiesi@0 157 define('DRUPAL_BOOTSTRAP_FULL', 7);
danielebarchiesi@0 158
danielebarchiesi@0 159 /**
danielebarchiesi@0 160 * Role ID for anonymous users; should match what's in the "role" table.
danielebarchiesi@0 161 */
danielebarchiesi@0 162 define('DRUPAL_ANONYMOUS_RID', 1);
danielebarchiesi@0 163
danielebarchiesi@0 164 /**
danielebarchiesi@0 165 * Role ID for authenticated users; should match what's in the "role" table.
danielebarchiesi@0 166 */
danielebarchiesi@0 167 define('DRUPAL_AUTHENTICATED_RID', 2);
danielebarchiesi@0 168
danielebarchiesi@0 169 /**
danielebarchiesi@0 170 * The number of bytes in a kilobyte.
danielebarchiesi@0 171 *
danielebarchiesi@0 172 * For more information, visit http://en.wikipedia.org/wiki/Kilobyte.
danielebarchiesi@0 173 */
danielebarchiesi@0 174 define('DRUPAL_KILOBYTE', 1024);
danielebarchiesi@0 175
danielebarchiesi@0 176 /**
danielebarchiesi@0 177 * The language code used when no language is explicitly assigned.
danielebarchiesi@0 178 *
danielebarchiesi@0 179 * Defined by ISO639-2 for "Undetermined".
danielebarchiesi@0 180 */
danielebarchiesi@0 181 define('LANGUAGE_NONE', 'und');
danielebarchiesi@0 182
danielebarchiesi@0 183 /**
danielebarchiesi@0 184 * The type of language used to define the content language.
danielebarchiesi@0 185 */
danielebarchiesi@0 186 define('LANGUAGE_TYPE_CONTENT', 'language_content');
danielebarchiesi@0 187
danielebarchiesi@0 188 /**
danielebarchiesi@0 189 * The type of language used to select the user interface.
danielebarchiesi@0 190 */
danielebarchiesi@0 191 define('LANGUAGE_TYPE_INTERFACE', 'language');
danielebarchiesi@0 192
danielebarchiesi@0 193 /**
danielebarchiesi@0 194 * The type of language used for URLs.
danielebarchiesi@0 195 */
danielebarchiesi@0 196 define('LANGUAGE_TYPE_URL', 'language_url');
danielebarchiesi@0 197
danielebarchiesi@0 198 /**
danielebarchiesi@0 199 * Language written left to right. Possible value of $language->direction.
danielebarchiesi@0 200 */
danielebarchiesi@0 201 define('LANGUAGE_LTR', 0);
danielebarchiesi@0 202
danielebarchiesi@0 203 /**
danielebarchiesi@0 204 * Language written right to left. Possible value of $language->direction.
danielebarchiesi@0 205 */
danielebarchiesi@0 206 define('LANGUAGE_RTL', 1);
danielebarchiesi@0 207
danielebarchiesi@0 208 /**
danielebarchiesi@0 209 * Time of the current request in seconds elapsed since the Unix Epoch.
danielebarchiesi@0 210 *
danielebarchiesi@0 211 * This differs from $_SERVER['REQUEST_TIME'], which is stored as a float
danielebarchiesi@0 212 * since PHP 5.4.0. Float timestamps confuse most PHP functions
danielebarchiesi@0 213 * (including date_create()).
danielebarchiesi@0 214 *
danielebarchiesi@0 215 * @see http://php.net/manual/reserved.variables.server.php
danielebarchiesi@0 216 * @see http://php.net/manual/function.time.php
danielebarchiesi@0 217 */
danielebarchiesi@0 218 define('REQUEST_TIME', (int) $_SERVER['REQUEST_TIME']);
danielebarchiesi@0 219
danielebarchiesi@0 220 /**
danielebarchiesi@0 221 * Flag used to indicate that text is not sanitized, so run check_plain().
danielebarchiesi@0 222 *
danielebarchiesi@0 223 * @see drupal_set_title()
danielebarchiesi@0 224 */
danielebarchiesi@0 225 define('CHECK_PLAIN', 0);
danielebarchiesi@0 226
danielebarchiesi@0 227 /**
danielebarchiesi@0 228 * Flag used to indicate that text has already been sanitized.
danielebarchiesi@0 229 *
danielebarchiesi@0 230 * @see drupal_set_title()
danielebarchiesi@0 231 */
danielebarchiesi@0 232 define('PASS_THROUGH', -1);
danielebarchiesi@0 233
danielebarchiesi@0 234 /**
danielebarchiesi@0 235 * Signals that the registry lookup cache should be reset.
danielebarchiesi@0 236 */
danielebarchiesi@0 237 define('REGISTRY_RESET_LOOKUP_CACHE', 1);
danielebarchiesi@0 238
danielebarchiesi@0 239 /**
danielebarchiesi@0 240 * Signals that the registry lookup cache should be written to storage.
danielebarchiesi@0 241 */
danielebarchiesi@0 242 define('REGISTRY_WRITE_LOOKUP_CACHE', 2);
danielebarchiesi@0 243
danielebarchiesi@0 244 /**
danielebarchiesi@0 245 * Regular expression to match PHP function names.
danielebarchiesi@0 246 *
danielebarchiesi@0 247 * @see http://php.net/manual/en/language.functions.php
danielebarchiesi@0 248 */
danielebarchiesi@0 249 define('DRUPAL_PHP_FUNCTION_PATTERN', '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*');
danielebarchiesi@0 250
danielebarchiesi@0 251 /**
danielebarchiesi@0 252 * Provides a caching wrapper to be used in place of large array structures.
danielebarchiesi@0 253 *
danielebarchiesi@0 254 * This class should be extended by systems that need to cache large amounts
danielebarchiesi@0 255 * of data and have it represented as an array to calling functions. These
danielebarchiesi@0 256 * arrays can become very large, so ArrayAccess is used to allow different
danielebarchiesi@0 257 * strategies to be used for caching internally (lazy loading, building caches
danielebarchiesi@0 258 * over time etc.). This can dramatically reduce the amount of data that needs
danielebarchiesi@0 259 * to be loaded from cache backends on each request, and memory usage from
danielebarchiesi@0 260 * static caches of that same data.
danielebarchiesi@0 261 *
danielebarchiesi@0 262 * Note that array_* functions do not work with ArrayAccess. Systems using
danielebarchiesi@0 263 * DrupalCacheArray should use this only internally. If providing API functions
danielebarchiesi@0 264 * that return the full array, this can be cached separately or returned
danielebarchiesi@0 265 * directly. However since DrupalCacheArray holds partial content by design, it
danielebarchiesi@0 266 * should be a normal PHP array or otherwise contain the full structure.
danielebarchiesi@0 267 *
danielebarchiesi@0 268 * Note also that due to limitations in PHP prior to 5.3.4, it is impossible to
danielebarchiesi@0 269 * write directly to the contents of nested arrays contained in this object.
danielebarchiesi@0 270 * Only writes to the top-level array elements are possible. So if you
danielebarchiesi@0 271 * previously had set $object['foo'] = array(1, 2, 'bar' => 'baz'), but later
danielebarchiesi@0 272 * want to change the value of 'bar' from 'baz' to 'foobar', you cannot do so
danielebarchiesi@0 273 * a targeted write like $object['foo']['bar'] = 'foobar'. Instead, you must
danielebarchiesi@0 274 * overwrite the entire top-level 'foo' array with the entire set of new
danielebarchiesi@0 275 * values: $object['foo'] = array(1, 2, 'bar' => 'foobar'). Due to this same
danielebarchiesi@0 276 * limitation, attempts to create references to any contained data, nested or
danielebarchiesi@0 277 * otherwise, will fail silently. So $var = &$object['foo'] will not throw an
danielebarchiesi@0 278 * error, and $var will be populated with the contents of $object['foo'], but
danielebarchiesi@0 279 * that data will be passed by value, not reference. For more information on
danielebarchiesi@0 280 * the PHP limitation, see the note in the official PHP documentation at·
danielebarchiesi@0 281 * http://php.net/manual/en/arrayaccess.offsetget.php on
danielebarchiesi@0 282 * ArrayAccess::offsetGet().
danielebarchiesi@0 283 *
danielebarchiesi@0 284 * By default, the class accounts for caches where calling functions might
danielebarchiesi@0 285 * request keys in the array that won't exist even after a cache rebuild. This
danielebarchiesi@0 286 * prevents situations where a cache rebuild would be triggered over and over
danielebarchiesi@0 287 * due to a 'missing' item. These cases are stored internally as a value of
danielebarchiesi@0 288 * NULL. This means that the offsetGet() and offsetExists() methods
danielebarchiesi@0 289 * must be overridden if caching an array where the top level values can
danielebarchiesi@0 290 * legitimately be NULL, and where $object->offsetExists() needs to correctly
danielebarchiesi@0 291 * return (equivalent to array_key_exists() vs. isset()). This should not
danielebarchiesi@0 292 * be necessary in the majority of cases.
danielebarchiesi@0 293 *
danielebarchiesi@0 294 * Classes extending this class must override at least the
danielebarchiesi@0 295 * resolveCacheMiss() method to have a working implementation.
danielebarchiesi@0 296 *
danielebarchiesi@0 297 * offsetSet() is not overridden by this class by default. In practice this
danielebarchiesi@0 298 * means that assigning an offset via arrayAccess will only apply while the
danielebarchiesi@0 299 * object is in scope and will not be written back to the persistent cache.
danielebarchiesi@0 300 * This follows a similar pattern to static vs. persistent caching in
danielebarchiesi@0 301 * procedural code. Extending classes may wish to alter this behavior, for
danielebarchiesi@0 302 * example by overriding offsetSet() and adding an automatic call to persist().
danielebarchiesi@0 303 *
danielebarchiesi@0 304 * @see SchemaCache
danielebarchiesi@0 305 */
danielebarchiesi@0 306 abstract class DrupalCacheArray implements ArrayAccess {
danielebarchiesi@0 307
danielebarchiesi@0 308 /**
danielebarchiesi@0 309 * A cid to pass to cache_set() and cache_get().
danielebarchiesi@0 310 */
danielebarchiesi@0 311 protected $cid;
danielebarchiesi@0 312
danielebarchiesi@0 313 /**
danielebarchiesi@0 314 * A bin to pass to cache_set() and cache_get().
danielebarchiesi@0 315 */
danielebarchiesi@0 316 protected $bin;
danielebarchiesi@0 317
danielebarchiesi@0 318 /**
danielebarchiesi@0 319 * An array of keys to add to the cache at the end of the request.
danielebarchiesi@0 320 */
danielebarchiesi@0 321 protected $keysToPersist = array();
danielebarchiesi@0 322
danielebarchiesi@0 323 /**
danielebarchiesi@0 324 * Storage for the data itself.
danielebarchiesi@0 325 */
danielebarchiesi@0 326 protected $storage = array();
danielebarchiesi@0 327
danielebarchiesi@0 328 /**
danielebarchiesi@0 329 * Constructs a DrupalCacheArray object.
danielebarchiesi@0 330 *
danielebarchiesi@0 331 * @param $cid
danielebarchiesi@0 332 * The cid for the array being cached.
danielebarchiesi@0 333 * @param $bin
danielebarchiesi@0 334 * The bin to cache the array.
danielebarchiesi@0 335 */
danielebarchiesi@0 336 public function __construct($cid, $bin) {
danielebarchiesi@0 337 $this->cid = $cid;
danielebarchiesi@0 338 $this->bin = $bin;
danielebarchiesi@0 339
danielebarchiesi@0 340 if ($cached = cache_get($this->cid, $this->bin)) {
danielebarchiesi@0 341 $this->storage = $cached->data;
danielebarchiesi@0 342 }
danielebarchiesi@0 343 }
danielebarchiesi@0 344
danielebarchiesi@0 345 /**
danielebarchiesi@0 346 * Implements ArrayAccess::offsetExists().
danielebarchiesi@0 347 */
danielebarchiesi@0 348 public function offsetExists($offset) {
danielebarchiesi@0 349 return $this->offsetGet($offset) !== NULL;
danielebarchiesi@0 350 }
danielebarchiesi@0 351
danielebarchiesi@0 352 /**
danielebarchiesi@0 353 * Implements ArrayAccess::offsetGet().
danielebarchiesi@0 354 */
danielebarchiesi@0 355 public function offsetGet($offset) {
danielebarchiesi@0 356 if (isset($this->storage[$offset]) || array_key_exists($offset, $this->storage)) {
danielebarchiesi@0 357 return $this->storage[$offset];
danielebarchiesi@0 358 }
danielebarchiesi@0 359 else {
danielebarchiesi@0 360 return $this->resolveCacheMiss($offset);
danielebarchiesi@0 361 }
danielebarchiesi@0 362 }
danielebarchiesi@0 363
danielebarchiesi@0 364 /**
danielebarchiesi@0 365 * Implements ArrayAccess::offsetSet().
danielebarchiesi@0 366 */
danielebarchiesi@0 367 public function offsetSet($offset, $value) {
danielebarchiesi@0 368 $this->storage[$offset] = $value;
danielebarchiesi@0 369 }
danielebarchiesi@0 370
danielebarchiesi@0 371 /**
danielebarchiesi@0 372 * Implements ArrayAccess::offsetUnset().
danielebarchiesi@0 373 */
danielebarchiesi@0 374 public function offsetUnset($offset) {
danielebarchiesi@0 375 unset($this->storage[$offset]);
danielebarchiesi@0 376 }
danielebarchiesi@0 377
danielebarchiesi@0 378 /**
danielebarchiesi@0 379 * Flags an offset value to be written to the persistent cache.
danielebarchiesi@0 380 *
danielebarchiesi@0 381 * If a value is assigned to a cache object with offsetSet(), by default it
danielebarchiesi@0 382 * will not be written to the persistent cache unless it is flagged with this
danielebarchiesi@0 383 * method. This allows items to be cached for the duration of a request,
danielebarchiesi@0 384 * without necessarily writing back to the persistent cache at the end.
danielebarchiesi@0 385 *
danielebarchiesi@0 386 * @param $offset
danielebarchiesi@0 387 * The array offset that was requested.
danielebarchiesi@0 388 * @param $persist
danielebarchiesi@0 389 * Optional boolean to specify whether the offset should be persisted or
danielebarchiesi@0 390 * not, defaults to TRUE. When called with $persist = FALSE the offset will
danielebarchiesi@0 391 * be unflagged so that it will not be written at the end of the request.
danielebarchiesi@0 392 */
danielebarchiesi@0 393 protected function persist($offset, $persist = TRUE) {
danielebarchiesi@0 394 $this->keysToPersist[$offset] = $persist;
danielebarchiesi@0 395 }
danielebarchiesi@0 396
danielebarchiesi@0 397 /**
danielebarchiesi@0 398 * Resolves a cache miss.
danielebarchiesi@0 399 *
danielebarchiesi@0 400 * When an offset is not found in the object, this is treated as a cache
danielebarchiesi@0 401 * miss. This method allows classes implementing the interface to look up
danielebarchiesi@0 402 * the actual value and allow it to be cached.
danielebarchiesi@0 403 *
danielebarchiesi@0 404 * @param $offset
danielebarchiesi@0 405 * The offset that was requested.
danielebarchiesi@0 406 *
danielebarchiesi@0 407 * @return
danielebarchiesi@0 408 * The value of the offset, or NULL if no value was found.
danielebarchiesi@0 409 */
danielebarchiesi@0 410 abstract protected function resolveCacheMiss($offset);
danielebarchiesi@0 411
danielebarchiesi@0 412 /**
danielebarchiesi@0 413 * Writes a value to the persistent cache immediately.
danielebarchiesi@0 414 *
danielebarchiesi@0 415 * @param $data
danielebarchiesi@0 416 * The data to write to the persistent cache.
danielebarchiesi@0 417 * @param $lock
danielebarchiesi@0 418 * Whether to acquire a lock before writing to cache.
danielebarchiesi@0 419 */
danielebarchiesi@0 420 protected function set($data, $lock = TRUE) {
danielebarchiesi@0 421 // Lock cache writes to help avoid stampedes.
danielebarchiesi@0 422 // To implement locking for cache misses, override __construct().
danielebarchiesi@0 423 $lock_name = $this->cid . ':' . $this->bin;
danielebarchiesi@0 424 if (!$lock || lock_acquire($lock_name)) {
danielebarchiesi@0 425 if ($cached = cache_get($this->cid, $this->bin)) {
danielebarchiesi@0 426 $data = $cached->data + $data;
danielebarchiesi@0 427 }
danielebarchiesi@0 428 cache_set($this->cid, $data, $this->bin);
danielebarchiesi@0 429 if ($lock) {
danielebarchiesi@0 430 lock_release($lock_name);
danielebarchiesi@0 431 }
danielebarchiesi@0 432 }
danielebarchiesi@0 433 }
danielebarchiesi@0 434
danielebarchiesi@0 435 /**
danielebarchiesi@0 436 * Destructs the DrupalCacheArray object.
danielebarchiesi@0 437 */
danielebarchiesi@0 438 public function __destruct() {
danielebarchiesi@0 439 $data = array();
danielebarchiesi@0 440 foreach ($this->keysToPersist as $offset => $persist) {
danielebarchiesi@0 441 if ($persist) {
danielebarchiesi@0 442 $data[$offset] = $this->storage[$offset];
danielebarchiesi@0 443 }
danielebarchiesi@0 444 }
danielebarchiesi@0 445 if (!empty($data)) {
danielebarchiesi@0 446 $this->set($data);
danielebarchiesi@0 447 }
danielebarchiesi@0 448 }
danielebarchiesi@0 449 }
danielebarchiesi@0 450
danielebarchiesi@0 451 /**
danielebarchiesi@0 452 * Starts the timer with the specified name.
danielebarchiesi@0 453 *
danielebarchiesi@0 454 * If you start and stop the same timer multiple times, the measured intervals
danielebarchiesi@0 455 * will be accumulated.
danielebarchiesi@0 456 *
danielebarchiesi@0 457 * @param $name
danielebarchiesi@0 458 * The name of the timer.
danielebarchiesi@0 459 */
danielebarchiesi@0 460 function timer_start($name) {
danielebarchiesi@0 461 global $timers;
danielebarchiesi@0 462
danielebarchiesi@0 463 $timers[$name]['start'] = microtime(TRUE);
danielebarchiesi@0 464 $timers[$name]['count'] = isset($timers[$name]['count']) ? ++$timers[$name]['count'] : 1;
danielebarchiesi@0 465 }
danielebarchiesi@0 466
danielebarchiesi@0 467 /**
danielebarchiesi@0 468 * Reads the current timer value without stopping the timer.
danielebarchiesi@0 469 *
danielebarchiesi@0 470 * @param $name
danielebarchiesi@0 471 * The name of the timer.
danielebarchiesi@0 472 *
danielebarchiesi@0 473 * @return
danielebarchiesi@0 474 * The current timer value in ms.
danielebarchiesi@0 475 */
danielebarchiesi@0 476 function timer_read($name) {
danielebarchiesi@0 477 global $timers;
danielebarchiesi@0 478
danielebarchiesi@0 479 if (isset($timers[$name]['start'])) {
danielebarchiesi@0 480 $stop = microtime(TRUE);
danielebarchiesi@0 481 $diff = round(($stop - $timers[$name]['start']) * 1000, 2);
danielebarchiesi@0 482
danielebarchiesi@0 483 if (isset($timers[$name]['time'])) {
danielebarchiesi@0 484 $diff += $timers[$name]['time'];
danielebarchiesi@0 485 }
danielebarchiesi@0 486 return $diff;
danielebarchiesi@0 487 }
danielebarchiesi@0 488 return $timers[$name]['time'];
danielebarchiesi@0 489 }
danielebarchiesi@0 490
danielebarchiesi@0 491 /**
danielebarchiesi@0 492 * Stops the timer with the specified name.
danielebarchiesi@0 493 *
danielebarchiesi@0 494 * @param $name
danielebarchiesi@0 495 * The name of the timer.
danielebarchiesi@0 496 *
danielebarchiesi@0 497 * @return
danielebarchiesi@0 498 * A timer array. The array contains the number of times the timer has been
danielebarchiesi@0 499 * started and stopped (count) and the accumulated timer value in ms (time).
danielebarchiesi@0 500 */
danielebarchiesi@0 501 function timer_stop($name) {
danielebarchiesi@0 502 global $timers;
danielebarchiesi@0 503
danielebarchiesi@0 504 if (isset($timers[$name]['start'])) {
danielebarchiesi@0 505 $stop = microtime(TRUE);
danielebarchiesi@0 506 $diff = round(($stop - $timers[$name]['start']) * 1000, 2);
danielebarchiesi@0 507 if (isset($timers[$name]['time'])) {
danielebarchiesi@0 508 $timers[$name]['time'] += $diff;
danielebarchiesi@0 509 }
danielebarchiesi@0 510 else {
danielebarchiesi@0 511 $timers[$name]['time'] = $diff;
danielebarchiesi@0 512 }
danielebarchiesi@0 513 unset($timers[$name]['start']);
danielebarchiesi@0 514 }
danielebarchiesi@0 515
danielebarchiesi@0 516 return $timers[$name];
danielebarchiesi@0 517 }
danielebarchiesi@0 518
danielebarchiesi@0 519 /**
danielebarchiesi@0 520 * Returns the appropriate configuration directory.
danielebarchiesi@0 521 *
danielebarchiesi@0 522 * Returns the configuration path based on the site's hostname, port, and
danielebarchiesi@0 523 * pathname. Uses find_conf_path() to find the current configuration directory.
danielebarchiesi@0 524 * See default.settings.php for examples on how the URL is converted to a
danielebarchiesi@0 525 * directory.
danielebarchiesi@0 526 *
danielebarchiesi@0 527 * @param bool $require_settings
danielebarchiesi@0 528 * Only configuration directories with an existing settings.php file
danielebarchiesi@0 529 * will be recognized. Defaults to TRUE. During initial installation,
danielebarchiesi@0 530 * this is set to FALSE so that Drupal can detect a matching directory,
danielebarchiesi@0 531 * then create a new settings.php file in it.
danielebarchiesi@0 532 * @param bool $reset
danielebarchiesi@0 533 * Force a full search for matching directories even if one had been
danielebarchiesi@0 534 * found previously. Defaults to FALSE.
danielebarchiesi@0 535 *
danielebarchiesi@0 536 * @return
danielebarchiesi@0 537 * The path of the matching directory.
danielebarchiesi@0 538 *
danielebarchiesi@0 539 * @see default.settings.php
danielebarchiesi@0 540 */
danielebarchiesi@0 541 function conf_path($require_settings = TRUE, $reset = FALSE) {
danielebarchiesi@0 542 $conf = &drupal_static(__FUNCTION__, '');
danielebarchiesi@0 543
danielebarchiesi@0 544 if ($conf && !$reset) {
danielebarchiesi@0 545 return $conf;
danielebarchiesi@0 546 }
danielebarchiesi@0 547
danielebarchiesi@0 548 $confdir = 'sites';
danielebarchiesi@0 549
danielebarchiesi@0 550 $sites = array();
danielebarchiesi@0 551 if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/sites.php')) {
danielebarchiesi@0 552 // This will overwrite $sites with the desired mappings.
danielebarchiesi@0 553 include(DRUPAL_ROOT . '/' . $confdir . '/sites.php');
danielebarchiesi@0 554 }
danielebarchiesi@0 555
danielebarchiesi@0 556 $uri = explode('/', $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']);
danielebarchiesi@0 557 $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
danielebarchiesi@0 558 for ($i = count($uri) - 1; $i > 0; $i--) {
danielebarchiesi@0 559 for ($j = count($server); $j > 0; $j--) {
danielebarchiesi@0 560 $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
danielebarchiesi@0 561 if (isset($sites[$dir]) && file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $sites[$dir])) {
danielebarchiesi@0 562 $dir = $sites[$dir];
danielebarchiesi@0 563 }
danielebarchiesi@0 564 if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir . '/settings.php') || (!$require_settings && file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir))) {
danielebarchiesi@0 565 $conf = "$confdir/$dir";
danielebarchiesi@0 566 return $conf;
danielebarchiesi@0 567 }
danielebarchiesi@0 568 }
danielebarchiesi@0 569 }
danielebarchiesi@0 570 $conf = "$confdir/default";
danielebarchiesi@0 571 return $conf;
danielebarchiesi@0 572 }
danielebarchiesi@0 573
danielebarchiesi@0 574 /**
danielebarchiesi@0 575 * Sets appropriate server variables needed for command line scripts to work.
danielebarchiesi@0 576 *
danielebarchiesi@0 577 * This function can be called by command line scripts before bootstrapping
danielebarchiesi@0 578 * Drupal, to ensure that the page loads with the desired server parameters.
danielebarchiesi@0 579 * This is because many parts of Drupal assume that they are running in a web
danielebarchiesi@0 580 * browser and therefore use information from the global PHP $_SERVER variable
danielebarchiesi@0 581 * that does not get set when Drupal is run from the command line.
danielebarchiesi@0 582 *
danielebarchiesi@0 583 * In many cases, the default way in which this function populates the $_SERVER
danielebarchiesi@0 584 * variable is sufficient, and it can therefore be called without passing in
danielebarchiesi@0 585 * any input. However, command line scripts running on a multisite installation
danielebarchiesi@0 586 * (or on any installation that has settings.php stored somewhere other than
danielebarchiesi@0 587 * the sites/default folder) need to pass in the URL of the site to allow
danielebarchiesi@0 588 * Drupal to detect the correct location of the settings.php file. Passing in
danielebarchiesi@0 589 * the 'url' parameter is also required for functions like request_uri() to
danielebarchiesi@0 590 * return the expected values.
danielebarchiesi@0 591 *
danielebarchiesi@0 592 * Most other parameters do not need to be passed in, but may be necessary in
danielebarchiesi@0 593 * some cases; for example, if Drupal's ip_address() function needs to return
danielebarchiesi@0 594 * anything but the standard localhost value ('127.0.0.1'), the command line
danielebarchiesi@0 595 * script should pass in the desired value via the 'REMOTE_ADDR' key.
danielebarchiesi@0 596 *
danielebarchiesi@0 597 * @param $variables
danielebarchiesi@0 598 * (optional) An associative array of variables within $_SERVER that should
danielebarchiesi@0 599 * be replaced. If the special element 'url' is provided in this array, it
danielebarchiesi@0 600 * will be used to populate some of the server defaults; it should be set to
danielebarchiesi@0 601 * the URL of the current page request, excluding any $_GET request but
danielebarchiesi@0 602 * including the script name (e.g., http://www.example.com/mysite/index.php).
danielebarchiesi@0 603 *
danielebarchiesi@0 604 * @see conf_path()
danielebarchiesi@0 605 * @see request_uri()
danielebarchiesi@0 606 * @see ip_address()
danielebarchiesi@0 607 */
danielebarchiesi@0 608 function drupal_override_server_variables($variables = array()) {
danielebarchiesi@0 609 // Allow the provided URL to override any existing values in $_SERVER.
danielebarchiesi@0 610 if (isset($variables['url'])) {
danielebarchiesi@0 611 $url = parse_url($variables['url']);
danielebarchiesi@0 612 if (isset($url['host'])) {
danielebarchiesi@0 613 $_SERVER['HTTP_HOST'] = $url['host'];
danielebarchiesi@0 614 }
danielebarchiesi@0 615 if (isset($url['path'])) {
danielebarchiesi@0 616 $_SERVER['SCRIPT_NAME'] = $url['path'];
danielebarchiesi@0 617 }
danielebarchiesi@0 618 unset($variables['url']);
danielebarchiesi@0 619 }
danielebarchiesi@0 620 // Define default values for $_SERVER keys. These will be used if $_SERVER
danielebarchiesi@0 621 // does not already define them and no other values are passed in to this
danielebarchiesi@0 622 // function.
danielebarchiesi@0 623 $defaults = array(
danielebarchiesi@0 624 'HTTP_HOST' => 'localhost',
danielebarchiesi@0 625 'SCRIPT_NAME' => NULL,
danielebarchiesi@0 626 'REMOTE_ADDR' => '127.0.0.1',
danielebarchiesi@0 627 'REQUEST_METHOD' => 'GET',
danielebarchiesi@0 628 'SERVER_NAME' => NULL,
danielebarchiesi@0 629 'SERVER_SOFTWARE' => NULL,
danielebarchiesi@0 630 'HTTP_USER_AGENT' => NULL,
danielebarchiesi@0 631 );
danielebarchiesi@0 632 // Replace elements of the $_SERVER array, as appropriate.
danielebarchiesi@0 633 $_SERVER = $variables + $_SERVER + $defaults;
danielebarchiesi@0 634 }
danielebarchiesi@0 635
danielebarchiesi@0 636 /**
danielebarchiesi@0 637 * Initializes the PHP environment.
danielebarchiesi@0 638 */
danielebarchiesi@0 639 function drupal_environment_initialize() {
danielebarchiesi@0 640 if (!isset($_SERVER['HTTP_REFERER'])) {
danielebarchiesi@0 641 $_SERVER['HTTP_REFERER'] = '';
danielebarchiesi@0 642 }
danielebarchiesi@0 643 if (!isset($_SERVER['SERVER_PROTOCOL']) || ($_SERVER['SERVER_PROTOCOL'] != 'HTTP/1.0' && $_SERVER['SERVER_PROTOCOL'] != 'HTTP/1.1')) {
danielebarchiesi@0 644 $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
danielebarchiesi@0 645 }
danielebarchiesi@0 646
danielebarchiesi@0 647 if (isset($_SERVER['HTTP_HOST'])) {
danielebarchiesi@0 648 // As HTTP_HOST is user input, ensure it only contains characters allowed
danielebarchiesi@0 649 // in hostnames. See RFC 952 (and RFC 2181).
danielebarchiesi@0 650 // $_SERVER['HTTP_HOST'] is lowercased here per specifications.
danielebarchiesi@0 651 $_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']);
danielebarchiesi@0 652 if (!drupal_valid_http_host($_SERVER['HTTP_HOST'])) {
danielebarchiesi@0 653 // HTTP_HOST is invalid, e.g. if containing slashes it may be an attack.
danielebarchiesi@0 654 header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
danielebarchiesi@0 655 exit;
danielebarchiesi@0 656 }
danielebarchiesi@0 657 }
danielebarchiesi@0 658 else {
danielebarchiesi@0 659 // Some pre-HTTP/1.1 clients will not send a Host header. Ensure the key is
danielebarchiesi@0 660 // defined for E_ALL compliance.
danielebarchiesi@0 661 $_SERVER['HTTP_HOST'] = '';
danielebarchiesi@0 662 }
danielebarchiesi@0 663
danielebarchiesi@0 664 // When clean URLs are enabled, emulate ?q=foo/bar using REQUEST_URI. It is
danielebarchiesi@0 665 // not possible to append the query string using mod_rewrite without the B
danielebarchiesi@0 666 // flag (this was added in Apache 2.2.8), because mod_rewrite unescapes the
danielebarchiesi@0 667 // path before passing it on to PHP. This is a problem when the path contains
danielebarchiesi@0 668 // e.g. "&" or "%" that have special meanings in URLs and must be encoded.
danielebarchiesi@0 669 $_GET['q'] = request_path();
danielebarchiesi@0 670
danielebarchiesi@0 671 // Enforce E_ALL, but allow users to set levels not part of E_ALL.
danielebarchiesi@0 672 error_reporting(E_ALL | error_reporting());
danielebarchiesi@0 673
danielebarchiesi@0 674 // Override PHP settings required for Drupal to work properly.
danielebarchiesi@0 675 // sites/default/default.settings.php contains more runtime settings.
danielebarchiesi@0 676 // The .htaccess file contains settings that cannot be changed at runtime.
danielebarchiesi@0 677
danielebarchiesi@0 678 // Don't escape quotes when reading files from the database, disk, etc.
danielebarchiesi@0 679 ini_set('magic_quotes_runtime', '0');
danielebarchiesi@0 680 // Use session cookies, not transparent sessions that puts the session id in
danielebarchiesi@0 681 // the query string.
danielebarchiesi@0 682 ini_set('session.use_cookies', '1');
danielebarchiesi@0 683 ini_set('session.use_only_cookies', '1');
danielebarchiesi@0 684 ini_set('session.use_trans_sid', '0');
danielebarchiesi@0 685 // Don't send HTTP headers using PHP's session handler.
danielebarchiesi@0 686 ini_set('session.cache_limiter', 'none');
danielebarchiesi@0 687 // Use httponly session cookies.
danielebarchiesi@0 688 ini_set('session.cookie_httponly', '1');
danielebarchiesi@0 689
danielebarchiesi@0 690 // Set sane locale settings, to ensure consistent string, dates, times and
danielebarchiesi@0 691 // numbers handling.
danielebarchiesi@0 692 setlocale(LC_ALL, 'C');
danielebarchiesi@0 693 }
danielebarchiesi@0 694
danielebarchiesi@0 695 /**
danielebarchiesi@0 696 * Validates that a hostname (for example $_SERVER['HTTP_HOST']) is safe.
danielebarchiesi@0 697 *
danielebarchiesi@0 698 * @return
danielebarchiesi@0 699 * TRUE if only containing valid characters, or FALSE otherwise.
danielebarchiesi@0 700 */
danielebarchiesi@0 701 function drupal_valid_http_host($host) {
danielebarchiesi@0 702 return preg_match('/^\[?(?:[a-zA-Z0-9-:\]_]+\.?)+$/', $host);
danielebarchiesi@0 703 }
danielebarchiesi@0 704
danielebarchiesi@0 705 /**
danielebarchiesi@0 706 * Sets the base URL, cookie domain, and session name from configuration.
danielebarchiesi@0 707 */
danielebarchiesi@0 708 function drupal_settings_initialize() {
danielebarchiesi@0 709 global $base_url, $base_path, $base_root;
danielebarchiesi@0 710
danielebarchiesi@0 711 // Export these settings.php variables to the global namespace.
danielebarchiesi@0 712 global $databases, $cookie_domain, $conf, $installed_profile, $update_free_access, $db_url, $db_prefix, $drupal_hash_salt, $is_https, $base_secure_url, $base_insecure_url;
danielebarchiesi@0 713 $conf = array();
danielebarchiesi@0 714
danielebarchiesi@0 715 if (file_exists(DRUPAL_ROOT . '/' . conf_path() . '/settings.php')) {
danielebarchiesi@0 716 include_once DRUPAL_ROOT . '/' . conf_path() . '/settings.php';
danielebarchiesi@0 717 }
danielebarchiesi@0 718 $is_https = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on';
danielebarchiesi@0 719
danielebarchiesi@0 720 if (isset($base_url)) {
danielebarchiesi@0 721 // Parse fixed base URL from settings.php.
danielebarchiesi@0 722 $parts = parse_url($base_url);
danielebarchiesi@0 723 if (!isset($parts['path'])) {
danielebarchiesi@0 724 $parts['path'] = '';
danielebarchiesi@0 725 }
danielebarchiesi@0 726 $base_path = $parts['path'] . '/';
danielebarchiesi@0 727 // Build $base_root (everything until first slash after "scheme://").
danielebarchiesi@0 728 $base_root = substr($base_url, 0, strlen($base_url) - strlen($parts['path']));
danielebarchiesi@0 729 }
danielebarchiesi@0 730 else {
danielebarchiesi@0 731 // Create base URL.
danielebarchiesi@0 732 $http_protocol = $is_https ? 'https' : 'http';
danielebarchiesi@0 733 $base_root = $http_protocol . '://' . $_SERVER['HTTP_HOST'];
danielebarchiesi@0 734
danielebarchiesi@0 735 $base_url = $base_root;
danielebarchiesi@0 736
danielebarchiesi@0 737 // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'], not
danielebarchiesi@0 738 // be modified by a visitor.
danielebarchiesi@0 739 if ($dir = rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/')) {
danielebarchiesi@0 740 $base_path = $dir;
danielebarchiesi@0 741 $base_url .= $base_path;
danielebarchiesi@0 742 $base_path .= '/';
danielebarchiesi@0 743 }
danielebarchiesi@0 744 else {
danielebarchiesi@0 745 $base_path = '/';
danielebarchiesi@0 746 }
danielebarchiesi@0 747 }
danielebarchiesi@0 748 $base_secure_url = str_replace('http://', 'https://', $base_url);
danielebarchiesi@0 749 $base_insecure_url = str_replace('https://', 'http://', $base_url);
danielebarchiesi@0 750
danielebarchiesi@0 751 if ($cookie_domain) {
danielebarchiesi@0 752 // If the user specifies the cookie domain, also use it for session name.
danielebarchiesi@0 753 $session_name = $cookie_domain;
danielebarchiesi@0 754 }
danielebarchiesi@0 755 else {
danielebarchiesi@0 756 // Otherwise use $base_url as session name, without the protocol
danielebarchiesi@0 757 // to use the same session identifiers across HTTP and HTTPS.
danielebarchiesi@0 758 list( , $session_name) = explode('://', $base_url, 2);
danielebarchiesi@0 759 // HTTP_HOST can be modified by a visitor, but we already sanitized it
danielebarchiesi@0 760 // in drupal_settings_initialize().
danielebarchiesi@0 761 if (!empty($_SERVER['HTTP_HOST'])) {
danielebarchiesi@0 762 $cookie_domain = $_SERVER['HTTP_HOST'];
danielebarchiesi@0 763 // Strip leading periods, www., and port numbers from cookie domain.
danielebarchiesi@0 764 $cookie_domain = ltrim($cookie_domain, '.');
danielebarchiesi@0 765 if (strpos($cookie_domain, 'www.') === 0) {
danielebarchiesi@0 766 $cookie_domain = substr($cookie_domain, 4);
danielebarchiesi@0 767 }
danielebarchiesi@0 768 $cookie_domain = explode(':', $cookie_domain);
danielebarchiesi@0 769 $cookie_domain = '.' . $cookie_domain[0];
danielebarchiesi@0 770 }
danielebarchiesi@0 771 }
danielebarchiesi@0 772 // Per RFC 2109, cookie domains must contain at least one dot other than the
danielebarchiesi@0 773 // first. For hosts such as 'localhost' or IP Addresses we don't set a cookie domain.
danielebarchiesi@0 774 if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
danielebarchiesi@0 775 ini_set('session.cookie_domain', $cookie_domain);
danielebarchiesi@0 776 }
danielebarchiesi@0 777 // To prevent session cookies from being hijacked, a user can configure the
danielebarchiesi@0 778 // SSL version of their website to only transfer session cookies via SSL by
danielebarchiesi@0 779 // using PHP's session.cookie_secure setting. The browser will then use two
danielebarchiesi@0 780 // separate session cookies for the HTTPS and HTTP versions of the site. So we
danielebarchiesi@0 781 // must use different session identifiers for HTTPS and HTTP to prevent a
danielebarchiesi@0 782 // cookie collision.
danielebarchiesi@0 783 if ($is_https) {
danielebarchiesi@0 784 ini_set('session.cookie_secure', TRUE);
danielebarchiesi@0 785 }
danielebarchiesi@0 786 $prefix = ini_get('session.cookie_secure') ? 'SSESS' : 'SESS';
danielebarchiesi@0 787 session_name($prefix . substr(hash('sha256', $session_name), 0, 32));
danielebarchiesi@0 788 }
danielebarchiesi@0 789
danielebarchiesi@0 790 /**
danielebarchiesi@0 791 * Returns and optionally sets the filename for a system resource.
danielebarchiesi@0 792 *
danielebarchiesi@0 793 * The filename, whether provided, cached, or retrieved from the database, is
danielebarchiesi@0 794 * only returned if the file exists.
danielebarchiesi@0 795 *
danielebarchiesi@0 796 * This function plays a key role in allowing Drupal's resources (modules
danielebarchiesi@0 797 * and themes) to be located in different places depending on a site's
danielebarchiesi@0 798 * configuration. For example, a module 'foo' may legally be located
danielebarchiesi@0 799 * in any of these three places:
danielebarchiesi@0 800 *
danielebarchiesi@0 801 * modules/foo/foo.module
danielebarchiesi@0 802 * sites/all/modules/foo/foo.module
danielebarchiesi@0 803 * sites/example.com/modules/foo/foo.module
danielebarchiesi@0 804 *
danielebarchiesi@0 805 * Calling drupal_get_filename('module', 'foo') will give you one of
danielebarchiesi@0 806 * the above, depending on where the module is located.
danielebarchiesi@0 807 *
danielebarchiesi@0 808 * @param $type
danielebarchiesi@0 809 * The type of the item (theme, theme_engine, module, profile).
danielebarchiesi@0 810 * @param $name
danielebarchiesi@0 811 * The name of the item for which the filename is requested.
danielebarchiesi@0 812 * @param $filename
danielebarchiesi@0 813 * The filename of the item if it is to be set explicitly rather
danielebarchiesi@0 814 * than by consulting the database.
danielebarchiesi@0 815 *
danielebarchiesi@0 816 * @return
danielebarchiesi@0 817 * The filename of the requested item or NULL if the item is not found.
danielebarchiesi@0 818 */
danielebarchiesi@0 819 function drupal_get_filename($type, $name, $filename = NULL) {
danielebarchiesi@0 820 // The location of files will not change during the request, so do not use
danielebarchiesi@0 821 // drupal_static().
danielebarchiesi@0 822 static $files = array(), $dirs = array();
danielebarchiesi@0 823
danielebarchiesi@0 824 // Profiles are a special case: they have a fixed location and naming.
danielebarchiesi@0 825 if ($type == 'profile') {
danielebarchiesi@0 826 $profile_filename = "profiles/$name/$name.profile";
danielebarchiesi@0 827 $files[$type][$name] = file_exists($profile_filename) ? $profile_filename : FALSE;
danielebarchiesi@0 828 }
danielebarchiesi@0 829 if (!isset($files[$type])) {
danielebarchiesi@0 830 $files[$type] = array();
danielebarchiesi@0 831 }
danielebarchiesi@0 832
danielebarchiesi@0 833 if (!empty($filename) && file_exists($filename)) {
danielebarchiesi@0 834 $files[$type][$name] = $filename;
danielebarchiesi@0 835 }
danielebarchiesi@0 836 elseif (isset($files[$type][$name])) {
danielebarchiesi@0 837 // nothing
danielebarchiesi@0 838 }
danielebarchiesi@0 839 // Verify that we have an active database connection, before querying
danielebarchiesi@0 840 // the database. This is required because this function is called both
danielebarchiesi@0 841 // before we have a database connection (i.e. during installation) and
danielebarchiesi@0 842 // when a database connection fails.
danielebarchiesi@0 843 else {
danielebarchiesi@0 844 try {
danielebarchiesi@0 845 if (function_exists('db_query')) {
danielebarchiesi@0 846 $file = db_query("SELECT filename FROM {system} WHERE name = :name AND type = :type", array(':name' => $name, ':type' => $type))->fetchField();
danielebarchiesi@0 847 if (file_exists(DRUPAL_ROOT . '/' . $file)) {
danielebarchiesi@0 848 $files[$type][$name] = $file;
danielebarchiesi@0 849 }
danielebarchiesi@0 850 }
danielebarchiesi@0 851 }
danielebarchiesi@0 852 catch (Exception $e) {
danielebarchiesi@0 853 // The database table may not exist because Drupal is not yet installed,
danielebarchiesi@0 854 // or the database might be down. We have a fallback for this case so we
danielebarchiesi@0 855 // hide the error completely.
danielebarchiesi@0 856 }
danielebarchiesi@0 857 // Fallback to searching the filesystem if the database could not find the
danielebarchiesi@0 858 // file or the file returned by the database is not found.
danielebarchiesi@0 859 if (!isset($files[$type][$name])) {
danielebarchiesi@0 860 // We have a consistent directory naming: modules, themes...
danielebarchiesi@0 861 $dir = $type . 's';
danielebarchiesi@0 862 if ($type == 'theme_engine') {
danielebarchiesi@0 863 $dir = 'themes/engines';
danielebarchiesi@0 864 $extension = 'engine';
danielebarchiesi@0 865 }
danielebarchiesi@0 866 elseif ($type == 'theme') {
danielebarchiesi@0 867 $extension = 'info';
danielebarchiesi@0 868 }
danielebarchiesi@0 869 else {
danielebarchiesi@0 870 $extension = $type;
danielebarchiesi@0 871 }
danielebarchiesi@0 872
danielebarchiesi@0 873 if (!isset($dirs[$dir][$extension])) {
danielebarchiesi@0 874 $dirs[$dir][$extension] = TRUE;
danielebarchiesi@0 875 if (!function_exists('drupal_system_listing')) {
danielebarchiesi@0 876 require_once DRUPAL_ROOT . '/includes/common.inc';
danielebarchiesi@0 877 }
danielebarchiesi@0 878 // Scan the appropriate directories for all files with the requested
danielebarchiesi@0 879 // extension, not just the file we are currently looking for. This
danielebarchiesi@0 880 // prevents unnecessary scans from being repeated when this function is
danielebarchiesi@0 881 // called more than once in the same page request.
danielebarchiesi@0 882 $matches = drupal_system_listing("/^" . DRUPAL_PHP_FUNCTION_PATTERN . "\.$extension$/", $dir, 'name', 0);
danielebarchiesi@0 883 foreach ($matches as $matched_name => $file) {
danielebarchiesi@0 884 $files[$type][$matched_name] = $file->uri;
danielebarchiesi@0 885 }
danielebarchiesi@0 886 }
danielebarchiesi@0 887 }
danielebarchiesi@0 888 }
danielebarchiesi@0 889
danielebarchiesi@0 890 if (isset($files[$type][$name])) {
danielebarchiesi@0 891 return $files[$type][$name];
danielebarchiesi@0 892 }
danielebarchiesi@0 893 }
danielebarchiesi@0 894
danielebarchiesi@0 895 /**
danielebarchiesi@0 896 * Loads the persistent variable table.
danielebarchiesi@0 897 *
danielebarchiesi@0 898 * The variable table is composed of values that have been saved in the table
danielebarchiesi@0 899 * with variable_set() as well as those explicitly specified in the
danielebarchiesi@0 900 * configuration file.
danielebarchiesi@0 901 */
danielebarchiesi@0 902 function variable_initialize($conf = array()) {
danielebarchiesi@0 903 // NOTE: caching the variables improves performance by 20% when serving
danielebarchiesi@0 904 // cached pages.
danielebarchiesi@0 905 if ($cached = cache_get('variables', 'cache_bootstrap')) {
danielebarchiesi@0 906 $variables = $cached->data;
danielebarchiesi@0 907 }
danielebarchiesi@0 908 else {
danielebarchiesi@0 909 // Cache miss. Avoid a stampede.
danielebarchiesi@0 910 $name = 'variable_init';
danielebarchiesi@0 911 if (!lock_acquire($name, 1)) {
danielebarchiesi@0 912 // Another request is building the variable cache.
danielebarchiesi@0 913 // Wait, then re-run this function.
danielebarchiesi@0 914 lock_wait($name);
danielebarchiesi@0 915 return variable_initialize($conf);
danielebarchiesi@0 916 }
danielebarchiesi@0 917 else {
danielebarchiesi@0 918 // Proceed with variable rebuild.
danielebarchiesi@0 919 $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
danielebarchiesi@0 920 cache_set('variables', $variables, 'cache_bootstrap');
danielebarchiesi@0 921 lock_release($name);
danielebarchiesi@0 922 }
danielebarchiesi@0 923 }
danielebarchiesi@0 924
danielebarchiesi@0 925 foreach ($conf as $name => $value) {
danielebarchiesi@0 926 $variables[$name] = $value;
danielebarchiesi@0 927 }
danielebarchiesi@0 928
danielebarchiesi@0 929 return $variables;
danielebarchiesi@0 930 }
danielebarchiesi@0 931
danielebarchiesi@0 932 /**
danielebarchiesi@0 933 * Returns a persistent variable.
danielebarchiesi@0 934 *
danielebarchiesi@0 935 * Case-sensitivity of the variable_* functions depends on the database
danielebarchiesi@0 936 * collation used. To avoid problems, always use lower case for persistent
danielebarchiesi@0 937 * variable names.
danielebarchiesi@0 938 *
danielebarchiesi@0 939 * @param $name
danielebarchiesi@0 940 * The name of the variable to return.
danielebarchiesi@0 941 * @param $default
danielebarchiesi@0 942 * The default value to use if this variable has never been set.
danielebarchiesi@0 943 *
danielebarchiesi@0 944 * @return
danielebarchiesi@0 945 * The value of the variable. Unserialization is taken care of as necessary.
danielebarchiesi@0 946 *
danielebarchiesi@0 947 * @see variable_del()
danielebarchiesi@0 948 * @see variable_set()
danielebarchiesi@0 949 */
danielebarchiesi@0 950 function variable_get($name, $default = NULL) {
danielebarchiesi@0 951 global $conf;
danielebarchiesi@0 952
danielebarchiesi@0 953 return isset($conf[$name]) ? $conf[$name] : $default;
danielebarchiesi@0 954 }
danielebarchiesi@0 955
danielebarchiesi@0 956 /**
danielebarchiesi@0 957 * Sets a persistent variable.
danielebarchiesi@0 958 *
danielebarchiesi@0 959 * Case-sensitivity of the variable_* functions depends on the database
danielebarchiesi@0 960 * collation used. To avoid problems, always use lower case for persistent
danielebarchiesi@0 961 * variable names.
danielebarchiesi@0 962 *
danielebarchiesi@0 963 * @param $name
danielebarchiesi@0 964 * The name of the variable to set.
danielebarchiesi@0 965 * @param $value
danielebarchiesi@0 966 * The value to set. This can be any PHP data type; these functions take care
danielebarchiesi@0 967 * of serialization as necessary.
danielebarchiesi@0 968 *
danielebarchiesi@0 969 * @see variable_del()
danielebarchiesi@0 970 * @see variable_get()
danielebarchiesi@0 971 */
danielebarchiesi@0 972 function variable_set($name, $value) {
danielebarchiesi@0 973 global $conf;
danielebarchiesi@0 974
danielebarchiesi@0 975 db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();
danielebarchiesi@0 976
danielebarchiesi@0 977 cache_clear_all('variables', 'cache_bootstrap');
danielebarchiesi@0 978
danielebarchiesi@0 979 $conf[$name] = $value;
danielebarchiesi@0 980 }
danielebarchiesi@0 981
danielebarchiesi@0 982 /**
danielebarchiesi@0 983 * Unsets a persistent variable.
danielebarchiesi@0 984 *
danielebarchiesi@0 985 * Case-sensitivity of the variable_* functions depends on the database
danielebarchiesi@0 986 * collation used. To avoid problems, always use lower case for persistent
danielebarchiesi@0 987 * variable names.
danielebarchiesi@0 988 *
danielebarchiesi@0 989 * @param $name
danielebarchiesi@0 990 * The name of the variable to undefine.
danielebarchiesi@0 991 *
danielebarchiesi@0 992 * @see variable_get()
danielebarchiesi@0 993 * @see variable_set()
danielebarchiesi@0 994 */
danielebarchiesi@0 995 function variable_del($name) {
danielebarchiesi@0 996 global $conf;
danielebarchiesi@0 997
danielebarchiesi@0 998 db_delete('variable')
danielebarchiesi@0 999 ->condition('name', $name)
danielebarchiesi@0 1000 ->execute();
danielebarchiesi@0 1001 cache_clear_all('variables', 'cache_bootstrap');
danielebarchiesi@0 1002
danielebarchiesi@0 1003 unset($conf[$name]);
danielebarchiesi@0 1004 }
danielebarchiesi@0 1005
danielebarchiesi@0 1006 /**
danielebarchiesi@0 1007 * Retrieves the current page from the cache.
danielebarchiesi@0 1008 *
danielebarchiesi@0 1009 * Note: we do not serve cached pages to authenticated users, or to anonymous
danielebarchiesi@0 1010 * users when $_SESSION is non-empty. $_SESSION may contain status messages
danielebarchiesi@0 1011 * from a form submission, the contents of a shopping cart, or other user-
danielebarchiesi@0 1012 * specific content that should not be cached and displayed to other users.
danielebarchiesi@0 1013 *
danielebarchiesi@0 1014 * @param $check_only
danielebarchiesi@0 1015 * (optional) Set to TRUE to only return whether a previous call found a
danielebarchiesi@0 1016 * cache entry.
danielebarchiesi@0 1017 *
danielebarchiesi@0 1018 * @return
danielebarchiesi@0 1019 * The cache object, if the page was found in the cache, NULL otherwise.
danielebarchiesi@0 1020 */
danielebarchiesi@0 1021 function drupal_page_get_cache($check_only = FALSE) {
danielebarchiesi@0 1022 global $base_root;
danielebarchiesi@0 1023 static $cache_hit = FALSE;
danielebarchiesi@0 1024
danielebarchiesi@0 1025 if ($check_only) {
danielebarchiesi@0 1026 return $cache_hit;
danielebarchiesi@0 1027 }
danielebarchiesi@0 1028
danielebarchiesi@0 1029 if (drupal_page_is_cacheable()) {
danielebarchiesi@0 1030 $cache = cache_get($base_root . request_uri(), 'cache_page');
danielebarchiesi@0 1031 if ($cache !== FALSE) {
danielebarchiesi@0 1032 $cache_hit = TRUE;
danielebarchiesi@0 1033 }
danielebarchiesi@0 1034 return $cache;
danielebarchiesi@0 1035 }
danielebarchiesi@0 1036 }
danielebarchiesi@0 1037
danielebarchiesi@0 1038 /**
danielebarchiesi@0 1039 * Determines the cacheability of the current page.
danielebarchiesi@0 1040 *
danielebarchiesi@0 1041 * @param $allow_caching
danielebarchiesi@0 1042 * Set to FALSE if you want to prevent this page to get cached.
danielebarchiesi@0 1043 *
danielebarchiesi@0 1044 * @return
danielebarchiesi@0 1045 * TRUE if the current page can be cached, FALSE otherwise.
danielebarchiesi@0 1046 */
danielebarchiesi@0 1047 function drupal_page_is_cacheable($allow_caching = NULL) {
danielebarchiesi@0 1048 $allow_caching_static = &drupal_static(__FUNCTION__, TRUE);
danielebarchiesi@0 1049 if (isset($allow_caching)) {
danielebarchiesi@0 1050 $allow_caching_static = $allow_caching;
danielebarchiesi@0 1051 }
danielebarchiesi@0 1052
danielebarchiesi@0 1053 return $allow_caching_static && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD')
danielebarchiesi@0 1054 && !drupal_is_cli();
danielebarchiesi@0 1055 }
danielebarchiesi@0 1056
danielebarchiesi@0 1057 /**
danielebarchiesi@0 1058 * Invokes a bootstrap hook in all bootstrap modules that implement it.
danielebarchiesi@0 1059 *
danielebarchiesi@0 1060 * @param $hook
danielebarchiesi@0 1061 * The name of the bootstrap hook to invoke.
danielebarchiesi@0 1062 *
danielebarchiesi@0 1063 * @see bootstrap_hooks()
danielebarchiesi@0 1064 */
danielebarchiesi@0 1065 function bootstrap_invoke_all($hook) {
danielebarchiesi@0 1066 // Bootstrap modules should have been loaded when this function is called, so
danielebarchiesi@0 1067 // we don't need to tell module_list() to reset its internal list (and we
danielebarchiesi@0 1068 // therefore leave the first parameter at its default value of FALSE). We
danielebarchiesi@0 1069 // still pass in TRUE for the second parameter, though; in case this is the
danielebarchiesi@0 1070 // first time during the bootstrap that module_list() is called, we want to
danielebarchiesi@0 1071 // make sure that its internal cache is primed with the bootstrap modules
danielebarchiesi@0 1072 // only.
danielebarchiesi@0 1073 foreach (module_list(FALSE, TRUE) as $module) {
danielebarchiesi@0 1074 drupal_load('module', $module);
danielebarchiesi@0 1075 module_invoke($module, $hook);
danielebarchiesi@0 1076 }
danielebarchiesi@0 1077 }
danielebarchiesi@0 1078
danielebarchiesi@0 1079 /**
danielebarchiesi@0 1080 * Includes a file with the provided type and name.
danielebarchiesi@0 1081 *
danielebarchiesi@0 1082 * This prevents including a theme, engine, module, etc., more than once.
danielebarchiesi@0 1083 *
danielebarchiesi@0 1084 * @param $type
danielebarchiesi@0 1085 * The type of item to load (i.e. theme, theme_engine, module).
danielebarchiesi@0 1086 * @param $name
danielebarchiesi@0 1087 * The name of the item to load.
danielebarchiesi@0 1088 *
danielebarchiesi@0 1089 * @return
danielebarchiesi@0 1090 * TRUE if the item is loaded or has already been loaded.
danielebarchiesi@0 1091 */
danielebarchiesi@0 1092 function drupal_load($type, $name) {
danielebarchiesi@0 1093 // Once a file is included this can't be reversed during a request so do not
danielebarchiesi@0 1094 // use drupal_static() here.
danielebarchiesi@0 1095 static $files = array();
danielebarchiesi@0 1096
danielebarchiesi@0 1097 if (isset($files[$type][$name])) {
danielebarchiesi@0 1098 return TRUE;
danielebarchiesi@0 1099 }
danielebarchiesi@0 1100
danielebarchiesi@0 1101 $filename = drupal_get_filename($type, $name);
danielebarchiesi@0 1102
danielebarchiesi@0 1103 if ($filename) {
danielebarchiesi@0 1104 include_once DRUPAL_ROOT . '/' . $filename;
danielebarchiesi@0 1105 $files[$type][$name] = TRUE;
danielebarchiesi@0 1106
danielebarchiesi@0 1107 return TRUE;
danielebarchiesi@0 1108 }
danielebarchiesi@0 1109
danielebarchiesi@0 1110 return FALSE;
danielebarchiesi@0 1111 }
danielebarchiesi@0 1112
danielebarchiesi@0 1113 /**
danielebarchiesi@0 1114 * Sets an HTTP response header for the current page.
danielebarchiesi@0 1115 *
danielebarchiesi@0 1116 * Note: When sending a Content-Type header, always include a 'charset' type,
danielebarchiesi@0 1117 * too. This is necessary to avoid security bugs (e.g. UTF-7 XSS).
danielebarchiesi@0 1118 *
danielebarchiesi@0 1119 * @param $name
danielebarchiesi@0 1120 * The HTTP header name, or the special 'Status' header name.
danielebarchiesi@0 1121 * @param $value
danielebarchiesi@0 1122 * The HTTP header value; if equal to FALSE, the specified header is unset.
danielebarchiesi@0 1123 * If $name is 'Status', this is expected to be a status code followed by a
danielebarchiesi@0 1124 * reason phrase, e.g. "404 Not Found".
danielebarchiesi@0 1125 * @param $append
danielebarchiesi@0 1126 * Whether to append the value to an existing header or to replace it.
danielebarchiesi@0 1127 */
danielebarchiesi@0 1128 function drupal_add_http_header($name, $value, $append = FALSE) {
danielebarchiesi@0 1129 // The headers as name/value pairs.
danielebarchiesi@0 1130 $headers = &drupal_static('drupal_http_headers', array());
danielebarchiesi@0 1131
danielebarchiesi@0 1132 $name_lower = strtolower($name);
danielebarchiesi@0 1133 _drupal_set_preferred_header_name($name);
danielebarchiesi@0 1134
danielebarchiesi@0 1135 if ($value === FALSE) {
danielebarchiesi@0 1136 $headers[$name_lower] = FALSE;
danielebarchiesi@0 1137 }
danielebarchiesi@0 1138 elseif (isset($headers[$name_lower]) && $append) {
danielebarchiesi@0 1139 // Multiple headers with identical names may be combined using comma (RFC
danielebarchiesi@0 1140 // 2616, section 4.2).
danielebarchiesi@0 1141 $headers[$name_lower] .= ',' . $value;
danielebarchiesi@0 1142 }
danielebarchiesi@0 1143 else {
danielebarchiesi@0 1144 $headers[$name_lower] = $value;
danielebarchiesi@0 1145 }
danielebarchiesi@0 1146 drupal_send_headers(array($name => $headers[$name_lower]), TRUE);
danielebarchiesi@0 1147 }
danielebarchiesi@0 1148
danielebarchiesi@0 1149 /**
danielebarchiesi@0 1150 * Gets the HTTP response headers for the current page.
danielebarchiesi@0 1151 *
danielebarchiesi@0 1152 * @param $name
danielebarchiesi@0 1153 * An HTTP header name. If omitted, all headers are returned as name/value
danielebarchiesi@0 1154 * pairs. If an array value is FALSE, the header has been unset.
danielebarchiesi@0 1155 *
danielebarchiesi@0 1156 * @return
danielebarchiesi@0 1157 * A string containing the header value, or FALSE if the header has been set,
danielebarchiesi@0 1158 * or NULL if the header has not been set.
danielebarchiesi@0 1159 */
danielebarchiesi@0 1160 function drupal_get_http_header($name = NULL) {
danielebarchiesi@0 1161 $headers = &drupal_static('drupal_http_headers', array());
danielebarchiesi@0 1162 if (isset($name)) {
danielebarchiesi@0 1163 $name = strtolower($name);
danielebarchiesi@0 1164 return isset($headers[$name]) ? $headers[$name] : NULL;
danielebarchiesi@0 1165 }
danielebarchiesi@0 1166 else {
danielebarchiesi@0 1167 return $headers;
danielebarchiesi@0 1168 }
danielebarchiesi@0 1169 }
danielebarchiesi@0 1170
danielebarchiesi@0 1171 /**
danielebarchiesi@0 1172 * Sets the preferred name for the HTTP header.
danielebarchiesi@0 1173 *
danielebarchiesi@0 1174 * Header names are case-insensitive, but for maximum compatibility they should
danielebarchiesi@0 1175 * follow "common form" (see RFC 2617, section 4.2).
danielebarchiesi@0 1176 */
danielebarchiesi@0 1177 function _drupal_set_preferred_header_name($name = NULL) {
danielebarchiesi@0 1178 static $header_names = array();
danielebarchiesi@0 1179
danielebarchiesi@0 1180 if (!isset($name)) {
danielebarchiesi@0 1181 return $header_names;
danielebarchiesi@0 1182 }
danielebarchiesi@0 1183 $header_names[strtolower($name)] = $name;
danielebarchiesi@0 1184 }
danielebarchiesi@0 1185
danielebarchiesi@0 1186 /**
danielebarchiesi@0 1187 * Sends the HTTP response headers that were previously set, adding defaults.
danielebarchiesi@0 1188 *
danielebarchiesi@0 1189 * Headers are set in drupal_add_http_header(). Default headers are not set
danielebarchiesi@0 1190 * if they have been replaced or unset using drupal_add_http_header().
danielebarchiesi@0 1191 *
danielebarchiesi@0 1192 * @param array $default_headers
danielebarchiesi@0 1193 * (optional) An array of headers as name/value pairs.
danielebarchiesi@0 1194 * @param bool $only_default
danielebarchiesi@0 1195 * (optional) If TRUE and headers have already been sent, send only the
danielebarchiesi@0 1196 * specified headers.
danielebarchiesi@0 1197 */
danielebarchiesi@0 1198 function drupal_send_headers($default_headers = array(), $only_default = FALSE) {
danielebarchiesi@0 1199 $headers_sent = &drupal_static(__FUNCTION__, FALSE);
danielebarchiesi@0 1200 $headers = drupal_get_http_header();
danielebarchiesi@0 1201 if ($only_default && $headers_sent) {
danielebarchiesi@0 1202 $headers = array();
danielebarchiesi@0 1203 }
danielebarchiesi@0 1204 $headers_sent = TRUE;
danielebarchiesi@0 1205
danielebarchiesi@0 1206 $header_names = _drupal_set_preferred_header_name();
danielebarchiesi@0 1207 foreach ($default_headers as $name => $value) {
danielebarchiesi@0 1208 $name_lower = strtolower($name);
danielebarchiesi@0 1209 if (!isset($headers[$name_lower])) {
danielebarchiesi@0 1210 $headers[$name_lower] = $value;
danielebarchiesi@0 1211 $header_names[$name_lower] = $name;
danielebarchiesi@0 1212 }
danielebarchiesi@0 1213 }
danielebarchiesi@0 1214 foreach ($headers as $name_lower => $value) {
danielebarchiesi@0 1215 if ($name_lower == 'status') {
danielebarchiesi@0 1216 header($_SERVER['SERVER_PROTOCOL'] . ' ' . $value);
danielebarchiesi@0 1217 }
danielebarchiesi@0 1218 // Skip headers that have been unset.
danielebarchiesi@0 1219 elseif ($value !== FALSE) {
danielebarchiesi@0 1220 header($header_names[$name_lower] . ': ' . $value);
danielebarchiesi@0 1221 }
danielebarchiesi@0 1222 }
danielebarchiesi@0 1223 }
danielebarchiesi@0 1224
danielebarchiesi@0 1225 /**
danielebarchiesi@0 1226 * Sets HTTP headers in preparation for a page response.
danielebarchiesi@0 1227 *
danielebarchiesi@0 1228 * Authenticated users are always given a 'no-cache' header, and will fetch a
danielebarchiesi@0 1229 * fresh page on every request. This prevents authenticated users from seeing
danielebarchiesi@0 1230 * locally cached pages.
danielebarchiesi@0 1231 *
danielebarchiesi@0 1232 * Also give each page a unique ETag. This will force clients to include both
danielebarchiesi@0 1233 * an If-Modified-Since header and an If-None-Match header when doing
danielebarchiesi@0 1234 * conditional requests for the page (required by RFC 2616, section 13.3.4),
danielebarchiesi@0 1235 * making the validation more robust. This is a workaround for a bug in Mozilla
danielebarchiesi@0 1236 * Firefox that is triggered when Drupal's caching is enabled and the user
danielebarchiesi@0 1237 * accesses Drupal via an HTTP proxy (see
danielebarchiesi@0 1238 * https://bugzilla.mozilla.org/show_bug.cgi?id=269303): When an authenticated
danielebarchiesi@0 1239 * user requests a page, and then logs out and requests the same page again,
danielebarchiesi@0 1240 * Firefox may send a conditional request based on the page that was cached
danielebarchiesi@0 1241 * locally when the user was logged in. If this page did not have an ETag
danielebarchiesi@0 1242 * header, the request only contains an If-Modified-Since header. The date will
danielebarchiesi@0 1243 * be recent, because with authenticated users the Last-Modified header always
danielebarchiesi@0 1244 * refers to the time of the request. If the user accesses Drupal via a proxy
danielebarchiesi@0 1245 * server, and the proxy already has a cached copy of the anonymous page with an
danielebarchiesi@0 1246 * older Last-Modified date, the proxy may respond with 304 Not Modified, making
danielebarchiesi@0 1247 * the client think that the anonymous and authenticated pageviews are
danielebarchiesi@0 1248 * identical.
danielebarchiesi@0 1249 *
danielebarchiesi@0 1250 * @see drupal_page_set_cache()
danielebarchiesi@0 1251 */
danielebarchiesi@0 1252 function drupal_page_header() {
danielebarchiesi@0 1253 $headers_sent = &drupal_static(__FUNCTION__, FALSE);
danielebarchiesi@0 1254 if ($headers_sent) {
danielebarchiesi@0 1255 return TRUE;
danielebarchiesi@0 1256 }
danielebarchiesi@0 1257 $headers_sent = TRUE;
danielebarchiesi@0 1258
danielebarchiesi@0 1259 $default_headers = array(
danielebarchiesi@0 1260 'Expires' => 'Sun, 19 Nov 1978 05:00:00 GMT',
danielebarchiesi@0 1261 'Last-Modified' => gmdate(DATE_RFC1123, REQUEST_TIME),
danielebarchiesi@0 1262 'Cache-Control' => 'no-cache, must-revalidate, post-check=0, pre-check=0',
danielebarchiesi@0 1263 'ETag' => '"' . REQUEST_TIME . '"',
danielebarchiesi@0 1264 );
danielebarchiesi@0 1265 drupal_send_headers($default_headers);
danielebarchiesi@0 1266 }
danielebarchiesi@0 1267
danielebarchiesi@0 1268 /**
danielebarchiesi@0 1269 * Sets HTTP headers in preparation for a cached page response.
danielebarchiesi@0 1270 *
danielebarchiesi@0 1271 * The headers allow as much as possible in proxies and browsers without any
danielebarchiesi@0 1272 * particular knowledge about the pages. Modules can override these headers
danielebarchiesi@0 1273 * using drupal_add_http_header().
danielebarchiesi@0 1274 *
danielebarchiesi@0 1275 * If the request is conditional (using If-Modified-Since and If-None-Match),
danielebarchiesi@0 1276 * and the conditions match those currently in the cache, a 304 Not Modified
danielebarchiesi@0 1277 * response is sent.
danielebarchiesi@0 1278 */
danielebarchiesi@0 1279 function drupal_serve_page_from_cache(stdClass $cache) {
danielebarchiesi@0 1280 // Negotiate whether to use compression.
danielebarchiesi@0 1281 $page_compression = variable_get('page_compression', TRUE) && extension_loaded('zlib');
danielebarchiesi@0 1282 $return_compressed = $page_compression && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE;
danielebarchiesi@0 1283
danielebarchiesi@0 1284 // Get headers set in hook_boot(). Keys are lower-case.
danielebarchiesi@0 1285 $hook_boot_headers = drupal_get_http_header();
danielebarchiesi@0 1286
danielebarchiesi@0 1287 // Headers generated in this function, that may be replaced or unset using
danielebarchiesi@0 1288 // drupal_add_http_headers(). Keys are mixed-case.
danielebarchiesi@0 1289 $default_headers = array();
danielebarchiesi@0 1290
danielebarchiesi@0 1291 foreach ($cache->data['headers'] as $name => $value) {
danielebarchiesi@0 1292 // In the case of a 304 response, certain headers must be sent, and the
danielebarchiesi@0 1293 // remaining may not (see RFC 2616, section 10.3.5). Do not override
danielebarchiesi@0 1294 // headers set in hook_boot().
danielebarchiesi@0 1295 $name_lower = strtolower($name);
danielebarchiesi@0 1296 if (in_array($name_lower, array('content-location', 'expires', 'cache-control', 'vary')) && !isset($hook_boot_headers[$name_lower])) {
danielebarchiesi@0 1297 drupal_add_http_header($name, $value);
danielebarchiesi@0 1298 unset($cache->data['headers'][$name]);
danielebarchiesi@0 1299 }
danielebarchiesi@0 1300 }
danielebarchiesi@0 1301
danielebarchiesi@0 1302 // If the client sent a session cookie, a cached copy will only be served
danielebarchiesi@0 1303 // to that one particular client due to Vary: Cookie. Thus, do not set
danielebarchiesi@0 1304 // max-age > 0, allowing the page to be cached by external proxies, when a
danielebarchiesi@0 1305 // session cookie is present unless the Vary header has been replaced or
danielebarchiesi@0 1306 // unset in hook_boot().
danielebarchiesi@0 1307 $max_age = !isset($_COOKIE[session_name()]) || isset($hook_boot_headers['vary']) ? variable_get('page_cache_maximum_age', 0) : 0;
danielebarchiesi@0 1308 $default_headers['Cache-Control'] = 'public, max-age=' . $max_age;
danielebarchiesi@0 1309
danielebarchiesi@0 1310 // Entity tag should change if the output changes.
danielebarchiesi@0 1311 $etag = '"' . $cache->created . '-' . intval($return_compressed) . '"';
danielebarchiesi@0 1312 header('Etag: ' . $etag);
danielebarchiesi@0 1313
danielebarchiesi@0 1314 // See if the client has provided the required HTTP headers.
danielebarchiesi@0 1315 $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
danielebarchiesi@0 1316 $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE;
danielebarchiesi@0 1317
danielebarchiesi@0 1318 if ($if_modified_since && $if_none_match
danielebarchiesi@0 1319 && $if_none_match == $etag // etag must match
danielebarchiesi@0 1320 && $if_modified_since == $cache->created) { // if-modified-since must match
danielebarchiesi@0 1321 header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
danielebarchiesi@0 1322 drupal_send_headers($default_headers);
danielebarchiesi@0 1323 return;
danielebarchiesi@0 1324 }
danielebarchiesi@0 1325
danielebarchiesi@0 1326 // Send the remaining headers.
danielebarchiesi@0 1327 foreach ($cache->data['headers'] as $name => $value) {
danielebarchiesi@0 1328 drupal_add_http_header($name, $value);
danielebarchiesi@0 1329 }
danielebarchiesi@0 1330
danielebarchiesi@0 1331 $default_headers['Last-Modified'] = gmdate(DATE_RFC1123, $cache->created);
danielebarchiesi@0 1332
danielebarchiesi@0 1333 // HTTP/1.0 proxies does not support the Vary header, so prevent any caching
danielebarchiesi@0 1334 // by sending an Expires date in the past. HTTP/1.1 clients ignores the
danielebarchiesi@0 1335 // Expires header if a Cache-Control: max-age= directive is specified (see RFC
danielebarchiesi@0 1336 // 2616, section 14.9.3).
danielebarchiesi@0 1337 $default_headers['Expires'] = 'Sun, 19 Nov 1978 05:00:00 GMT';
danielebarchiesi@0 1338
danielebarchiesi@0 1339 drupal_send_headers($default_headers);
danielebarchiesi@0 1340
danielebarchiesi@0 1341 // Allow HTTP proxies to cache pages for anonymous users without a session
danielebarchiesi@0 1342 // cookie. The Vary header is used to indicates the set of request-header
danielebarchiesi@0 1343 // fields that fully determines whether a cache is permitted to use the
danielebarchiesi@0 1344 // response to reply to a subsequent request for a given URL without
danielebarchiesi@0 1345 // revalidation. If a Vary header has been set in hook_boot(), it is assumed
danielebarchiesi@0 1346 // that the module knows how to cache the page.
danielebarchiesi@0 1347 if (!isset($hook_boot_headers['vary']) && !variable_get('omit_vary_cookie')) {
danielebarchiesi@0 1348 header('Vary: Cookie');
danielebarchiesi@0 1349 }
danielebarchiesi@0 1350
danielebarchiesi@0 1351 if ($page_compression) {
danielebarchiesi@0 1352 header('Vary: Accept-Encoding', FALSE);
danielebarchiesi@0 1353 // If page_compression is enabled, the cache contains gzipped data.
danielebarchiesi@0 1354 if ($return_compressed) {
danielebarchiesi@0 1355 // $cache->data['body'] is already gzip'ed, so make sure
danielebarchiesi@0 1356 // zlib.output_compression does not compress it once more.
danielebarchiesi@0 1357 ini_set('zlib.output_compression', '0');
danielebarchiesi@0 1358 header('Content-Encoding: gzip');
danielebarchiesi@0 1359 }
danielebarchiesi@0 1360 else {
danielebarchiesi@0 1361 // The client does not support compression, so unzip the data in the
danielebarchiesi@0 1362 // cache. Strip the gzip header and run uncompress.
danielebarchiesi@0 1363 $cache->data['body'] = gzinflate(substr(substr($cache->data['body'], 10), 0, -8));
danielebarchiesi@0 1364 }
danielebarchiesi@0 1365 }
danielebarchiesi@0 1366
danielebarchiesi@0 1367 // Print the page.
danielebarchiesi@0 1368 print $cache->data['body'];
danielebarchiesi@0 1369 }
danielebarchiesi@0 1370
danielebarchiesi@0 1371 /**
danielebarchiesi@0 1372 * Defines the critical hooks that force modules to always be loaded.
danielebarchiesi@0 1373 */
danielebarchiesi@0 1374 function bootstrap_hooks() {
danielebarchiesi@0 1375 return array('boot', 'exit', 'watchdog', 'language_init');
danielebarchiesi@0 1376 }
danielebarchiesi@0 1377
danielebarchiesi@0 1378 /**
danielebarchiesi@0 1379 * Unserializes and appends elements from a serialized string.
danielebarchiesi@0 1380 *
danielebarchiesi@0 1381 * @param $obj
danielebarchiesi@0 1382 * The object to which the elements are appended.
danielebarchiesi@0 1383 * @param $field
danielebarchiesi@0 1384 * The attribute of $obj whose value should be unserialized.
danielebarchiesi@0 1385 */
danielebarchiesi@0 1386 function drupal_unpack($obj, $field = 'data') {
danielebarchiesi@0 1387 if ($obj->$field && $data = unserialize($obj->$field)) {
danielebarchiesi@0 1388 foreach ($data as $key => $value) {
danielebarchiesi@0 1389 if (!empty($key) && !isset($obj->$key)) {
danielebarchiesi@0 1390 $obj->$key = $value;
danielebarchiesi@0 1391 }
danielebarchiesi@0 1392 }
danielebarchiesi@0 1393 }
danielebarchiesi@0 1394 return $obj;
danielebarchiesi@0 1395 }
danielebarchiesi@0 1396
danielebarchiesi@0 1397 /**
danielebarchiesi@0 1398 * Translates a string to the current language or to a given language.
danielebarchiesi@0 1399 *
danielebarchiesi@0 1400 * The t() function serves two purposes. First, at run-time it translates
danielebarchiesi@0 1401 * user-visible text into the appropriate language. Second, various mechanisms
danielebarchiesi@0 1402 * that figure out what text needs to be translated work off t() -- the text
danielebarchiesi@0 1403 * inside t() calls is added to the database of strings to be translated.
danielebarchiesi@0 1404 * These strings are expected to be in English, so the first argument should
danielebarchiesi@0 1405 * always be in English. To enable a fully-translatable site, it is important
danielebarchiesi@0 1406 * that all human-readable text that will be displayed on the site or sent to
danielebarchiesi@0 1407 * a user is passed through the t() function, or a related function. See the
danielebarchiesi@0 1408 * @link http://drupal.org/node/322729 Localization API @endlink pages for
danielebarchiesi@0 1409 * more information, including recommendations on how to break up or not
danielebarchiesi@0 1410 * break up strings for translation.
danielebarchiesi@0 1411 *
danielebarchiesi@0 1412 * @section sec_translating_vars Translating Variables
danielebarchiesi@0 1413 * You should never use t() to translate variables, such as calling
danielebarchiesi@0 1414 * @code t($text); @endcode, unless the text that the variable holds has been
danielebarchiesi@0 1415 * passed through t() elsewhere (e.g., $text is one of several translated
danielebarchiesi@0 1416 * literal strings in an array). It is especially important never to call
danielebarchiesi@0 1417 * @code t($user_text); @endcode, where $user_text is some text that a user
danielebarchiesi@0 1418 * entered - doing that can lead to cross-site scripting and other security
danielebarchiesi@0 1419 * problems. However, you can use variable substitution in your string, to put
danielebarchiesi@0 1420 * variable text such as user names or link URLs into translated text. Variable
danielebarchiesi@0 1421 * substitution looks like this:
danielebarchiesi@0 1422 * @code
danielebarchiesi@0 1423 * $text = t("@name's blog", array('@name' => format_username($account)));
danielebarchiesi@0 1424 * @endcode
danielebarchiesi@0 1425 * Basically, you can put variables like @name into your string, and t() will
danielebarchiesi@0 1426 * substitute their sanitized values at translation time. (See the
danielebarchiesi@0 1427 * Localization API pages referenced above and the documentation of
danielebarchiesi@0 1428 * format_string() for details about how to define variables in your string.)
danielebarchiesi@0 1429 * Translators can then rearrange the string as necessary for the language
danielebarchiesi@0 1430 * (e.g., in Spanish, it might be "blog de @name").
danielebarchiesi@0 1431 *
danielebarchiesi@0 1432 * @section sec_alt_funcs_install Use During Installation Phase
danielebarchiesi@0 1433 * During the Drupal installation phase, some resources used by t() wil not be
danielebarchiesi@0 1434 * available to code that needs localization. See st() and get_t() for
danielebarchiesi@0 1435 * alternatives.
danielebarchiesi@0 1436 *
danielebarchiesi@0 1437 * @param $string
danielebarchiesi@0 1438 * A string containing the English string to translate.
danielebarchiesi@0 1439 * @param $args
danielebarchiesi@0 1440 * An associative array of replacements to make after translation. Based
danielebarchiesi@0 1441 * on the first character of the key, the value is escaped and/or themed.
danielebarchiesi@0 1442 * See format_string() for details.
danielebarchiesi@0 1443 * @param $options
danielebarchiesi@0 1444 * An associative array of additional options, with the following elements:
danielebarchiesi@0 1445 * - 'langcode' (defaults to the current language): The language code to
danielebarchiesi@0 1446 * translate to a language other than what is used to display the page.
danielebarchiesi@0 1447 * - 'context' (defaults to the empty context): The context the source string
danielebarchiesi@0 1448 * belongs to.
danielebarchiesi@0 1449 *
danielebarchiesi@0 1450 * @return
danielebarchiesi@0 1451 * The translated string.
danielebarchiesi@0 1452 *
danielebarchiesi@0 1453 * @see st()
danielebarchiesi@0 1454 * @see get_t()
danielebarchiesi@0 1455 * @see format_string()
danielebarchiesi@0 1456 * @ingroup sanitization
danielebarchiesi@0 1457 */
danielebarchiesi@0 1458 function t($string, array $args = array(), array $options = array()) {
danielebarchiesi@0 1459 global $language;
danielebarchiesi@0 1460 static $custom_strings;
danielebarchiesi@0 1461
danielebarchiesi@0 1462 // Merge in default.
danielebarchiesi@0 1463 if (empty($options['langcode'])) {
danielebarchiesi@0 1464 $options['langcode'] = isset($language->language) ? $language->language : 'en';
danielebarchiesi@0 1465 }
danielebarchiesi@0 1466 if (empty($options['context'])) {
danielebarchiesi@0 1467 $options['context'] = '';
danielebarchiesi@0 1468 }
danielebarchiesi@0 1469
danielebarchiesi@0 1470 // First, check for an array of customized strings. If present, use the array
danielebarchiesi@0 1471 // *instead of* database lookups. This is a high performance way to provide a
danielebarchiesi@0 1472 // handful of string replacements. See settings.php for examples.
danielebarchiesi@0 1473 // Cache the $custom_strings variable to improve performance.
danielebarchiesi@0 1474 if (!isset($custom_strings[$options['langcode']])) {
danielebarchiesi@0 1475 $custom_strings[$options['langcode']] = variable_get('locale_custom_strings_' . $options['langcode'], array());
danielebarchiesi@0 1476 }
danielebarchiesi@0 1477 // Custom strings work for English too, even if locale module is disabled.
danielebarchiesi@0 1478 if (isset($custom_strings[$options['langcode']][$options['context']][$string])) {
danielebarchiesi@0 1479 $string = $custom_strings[$options['langcode']][$options['context']][$string];
danielebarchiesi@0 1480 }
danielebarchiesi@0 1481 // Translate with locale module if enabled.
danielebarchiesi@0 1482 elseif ($options['langcode'] != 'en' && function_exists('locale')) {
danielebarchiesi@0 1483 $string = locale($string, $options['context'], $options['langcode']);
danielebarchiesi@0 1484 }
danielebarchiesi@0 1485 if (empty($args)) {
danielebarchiesi@0 1486 return $string;
danielebarchiesi@0 1487 }
danielebarchiesi@0 1488 else {
danielebarchiesi@0 1489 return format_string($string, $args);
danielebarchiesi@0 1490 }
danielebarchiesi@0 1491 }
danielebarchiesi@0 1492
danielebarchiesi@0 1493 /**
danielebarchiesi@0 1494 * Formats a string for HTML display by replacing variable placeholders.
danielebarchiesi@0 1495 *
danielebarchiesi@0 1496 * This function replaces variable placeholders in a string with the requested
danielebarchiesi@0 1497 * values and escapes the values so they can be safely displayed as HTML. It
danielebarchiesi@0 1498 * should be used on any unknown text that is intended to be printed to an HTML
danielebarchiesi@0 1499 * page (especially text that may have come from untrusted users, since in that
danielebarchiesi@0 1500 * case it prevents cross-site scripting and other security problems).
danielebarchiesi@0 1501 *
danielebarchiesi@0 1502 * In most cases, you should use t() rather than calling this function
danielebarchiesi@0 1503 * directly, since it will translate the text (on non-English-only sites) in
danielebarchiesi@0 1504 * addition to formatting it.
danielebarchiesi@0 1505 *
danielebarchiesi@0 1506 * @param $string
danielebarchiesi@0 1507 * A string containing placeholders.
danielebarchiesi@0 1508 * @param $args
danielebarchiesi@0 1509 * An associative array of replacements to make. Occurrences in $string of
danielebarchiesi@0 1510 * any key in $args are replaced with the corresponding value, after optional
danielebarchiesi@0 1511 * sanitization and formatting. The type of sanitization and formatting
danielebarchiesi@0 1512 * depends on the first character of the key:
danielebarchiesi@0 1513 * - @variable: Escaped to HTML using check_plain(). Use this as the default
danielebarchiesi@0 1514 * choice for anything displayed on a page on the site.
danielebarchiesi@0 1515 * - %variable: Escaped to HTML and formatted using drupal_placeholder(),
danielebarchiesi@0 1516 * which makes it display as <em>emphasized</em> text.
danielebarchiesi@0 1517 * - !variable: Inserted as is, with no sanitization or formatting. Only use
danielebarchiesi@0 1518 * this for text that has already been prepared for HTML display (for
danielebarchiesi@0 1519 * example, user-supplied text that has already been run through
danielebarchiesi@0 1520 * check_plain() previously, or is expected to contain some limited HTML
danielebarchiesi@0 1521 * tags and has already been run through filter_xss() previously).
danielebarchiesi@0 1522 *
danielebarchiesi@0 1523 * @see t()
danielebarchiesi@0 1524 * @ingroup sanitization
danielebarchiesi@0 1525 */
danielebarchiesi@0 1526 function format_string($string, array $args = array()) {
danielebarchiesi@0 1527 // Transform arguments before inserting them.
danielebarchiesi@0 1528 foreach ($args as $key => $value) {
danielebarchiesi@0 1529 switch ($key[0]) {
danielebarchiesi@0 1530 case '@':
danielebarchiesi@0 1531 // Escaped only.
danielebarchiesi@0 1532 $args[$key] = check_plain($value);
danielebarchiesi@0 1533 break;
danielebarchiesi@0 1534
danielebarchiesi@0 1535 case '%':
danielebarchiesi@0 1536 default:
danielebarchiesi@0 1537 // Escaped and placeholder.
danielebarchiesi@0 1538 $args[$key] = drupal_placeholder($value);
danielebarchiesi@0 1539 break;
danielebarchiesi@0 1540
danielebarchiesi@0 1541 case '!':
danielebarchiesi@0 1542 // Pass-through.
danielebarchiesi@0 1543 }
danielebarchiesi@0 1544 }
danielebarchiesi@0 1545 return strtr($string, $args);
danielebarchiesi@0 1546 }
danielebarchiesi@0 1547
danielebarchiesi@0 1548 /**
danielebarchiesi@0 1549 * Encodes special characters in a plain-text string for display as HTML.
danielebarchiesi@0 1550 *
danielebarchiesi@0 1551 * Also validates strings as UTF-8 to prevent cross site scripting attacks on
danielebarchiesi@0 1552 * Internet Explorer 6.
danielebarchiesi@0 1553 *
danielebarchiesi@0 1554 * @param $text
danielebarchiesi@0 1555 * The text to be checked or processed.
danielebarchiesi@0 1556 *
danielebarchiesi@0 1557 * @return
danielebarchiesi@0 1558 * An HTML safe version of $text, or an empty string if $text is not
danielebarchiesi@0 1559 * valid UTF-8.
danielebarchiesi@0 1560 *
danielebarchiesi@0 1561 * @see drupal_validate_utf8()
danielebarchiesi@0 1562 * @ingroup sanitization
danielebarchiesi@0 1563 */
danielebarchiesi@0 1564 function check_plain($text) {
danielebarchiesi@0 1565 return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
danielebarchiesi@0 1566 }
danielebarchiesi@0 1567
danielebarchiesi@0 1568 /**
danielebarchiesi@0 1569 * Checks whether a string is valid UTF-8.
danielebarchiesi@0 1570 *
danielebarchiesi@0 1571 * All functions designed to filter input should use drupal_validate_utf8
danielebarchiesi@0 1572 * to ensure they operate on valid UTF-8 strings to prevent bypass of the
danielebarchiesi@0 1573 * filter.
danielebarchiesi@0 1574 *
danielebarchiesi@0 1575 * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented
danielebarchiesi@0 1576 * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent
danielebarchiesi@0 1577 * bytes. When these subsequent bytes are HTML control characters such as
danielebarchiesi@0 1578 * quotes or angle brackets, parts of the text that were deemed safe by filters
danielebarchiesi@0 1579 * end up in locations that are potentially unsafe; An onerror attribute that
danielebarchiesi@0 1580 * is outside of a tag, and thus deemed safe by a filter, can be interpreted
danielebarchiesi@0 1581 * by the browser as if it were inside the tag.
danielebarchiesi@0 1582 *
danielebarchiesi@0 1583 * The function does not return FALSE for strings containing character codes
danielebarchiesi@0 1584 * above U+10FFFF, even though these are prohibited by RFC 3629.
danielebarchiesi@0 1585 *
danielebarchiesi@0 1586 * @param $text
danielebarchiesi@0 1587 * The text to check.
danielebarchiesi@0 1588 *
danielebarchiesi@0 1589 * @return
danielebarchiesi@0 1590 * TRUE if the text is valid UTF-8, FALSE if not.
danielebarchiesi@0 1591 */
danielebarchiesi@0 1592 function drupal_validate_utf8($text) {
danielebarchiesi@0 1593 if (strlen($text) == 0) {
danielebarchiesi@0 1594 return TRUE;
danielebarchiesi@0 1595 }
danielebarchiesi@0 1596 // With the PCRE_UTF8 modifier 'u', preg_match() fails silently on strings
danielebarchiesi@0 1597 // containing invalid UTF-8 byte sequences. It does not reject character
danielebarchiesi@0 1598 // codes above U+10FFFF (represented by 4 or more octets), though.
danielebarchiesi@0 1599 return (preg_match('/^./us', $text) == 1);
danielebarchiesi@0 1600 }
danielebarchiesi@0 1601
danielebarchiesi@0 1602 /**
danielebarchiesi@0 1603 * Returns the equivalent of Apache's $_SERVER['REQUEST_URI'] variable.
danielebarchiesi@0 1604 *
danielebarchiesi@0 1605 * Because $_SERVER['REQUEST_URI'] is only available on Apache, we generate an
danielebarchiesi@0 1606 * equivalent using other environment variables.
danielebarchiesi@0 1607 */
danielebarchiesi@0 1608 function request_uri() {
danielebarchiesi@0 1609 if (isset($_SERVER['REQUEST_URI'])) {
danielebarchiesi@0 1610 $uri = $_SERVER['REQUEST_URI'];
danielebarchiesi@0 1611 }
danielebarchiesi@0 1612 else {
danielebarchiesi@0 1613 if (isset($_SERVER['argv'])) {
danielebarchiesi@0 1614 $uri = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['argv'][0];
danielebarchiesi@0 1615 }
danielebarchiesi@0 1616 elseif (isset($_SERVER['QUERY_STRING'])) {
danielebarchiesi@0 1617 $uri = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
danielebarchiesi@0 1618 }
danielebarchiesi@0 1619 else {
danielebarchiesi@0 1620 $uri = $_SERVER['SCRIPT_NAME'];
danielebarchiesi@0 1621 }
danielebarchiesi@0 1622 }
danielebarchiesi@0 1623 // Prevent multiple slashes to avoid cross site requests via the Form API.
danielebarchiesi@0 1624 $uri = '/' . ltrim($uri, '/');
danielebarchiesi@0 1625
danielebarchiesi@0 1626 return $uri;
danielebarchiesi@0 1627 }
danielebarchiesi@0 1628
danielebarchiesi@0 1629 /**
danielebarchiesi@0 1630 * Logs an exception.
danielebarchiesi@0 1631 *
danielebarchiesi@0 1632 * This is a wrapper function for watchdog() which automatically decodes an
danielebarchiesi@0 1633 * exception.
danielebarchiesi@0 1634 *
danielebarchiesi@0 1635 * @param $type
danielebarchiesi@0 1636 * The category to which this message belongs.
danielebarchiesi@0 1637 * @param $exception
danielebarchiesi@0 1638 * The exception that is going to be logged.
danielebarchiesi@0 1639 * @param $message
danielebarchiesi@0 1640 * The message to store in the log. If empty, a text that contains all useful
danielebarchiesi@0 1641 * information about the passed-in exception is used.
danielebarchiesi@0 1642 * @param $variables
danielebarchiesi@0 1643 * Array of variables to replace in the message on display. Defaults to the
danielebarchiesi@0 1644 * return value of drupal_decode_exception().
danielebarchiesi@0 1645 * @param $severity
danielebarchiesi@0 1646 * The severity of the message, as per RFC 3164.
danielebarchiesi@0 1647 * @param $link
danielebarchiesi@0 1648 * A link to associate with the message.
danielebarchiesi@0 1649 *
danielebarchiesi@0 1650 * @see watchdog()
danielebarchiesi@0 1651 * @see drupal_decode_exception()
danielebarchiesi@0 1652 */
danielebarchiesi@0 1653 function watchdog_exception($type, Exception $exception, $message = NULL, $variables = array(), $severity = WATCHDOG_ERROR, $link = NULL) {
danielebarchiesi@0 1654
danielebarchiesi@0 1655 // Use a default value if $message is not set.
danielebarchiesi@0 1656 if (empty($message)) {
danielebarchiesi@0 1657 // The exception message is run through check_plain() by _drupal_decode_exception().
danielebarchiesi@0 1658 $message = '%type: !message in %function (line %line of %file).';
danielebarchiesi@0 1659 }
danielebarchiesi@0 1660 // $variables must be an array so that we can add the exception information.
danielebarchiesi@0 1661 if (!is_array($variables)) {
danielebarchiesi@0 1662 $variables = array();
danielebarchiesi@0 1663 }
danielebarchiesi@0 1664
danielebarchiesi@0 1665 require_once DRUPAL_ROOT . '/includes/errors.inc';
danielebarchiesi@0 1666 $variables += _drupal_decode_exception($exception);
danielebarchiesi@0 1667 watchdog($type, $message, $variables, $severity, $link);
danielebarchiesi@0 1668 }
danielebarchiesi@0 1669
danielebarchiesi@0 1670 /**
danielebarchiesi@0 1671 * Logs a system message.
danielebarchiesi@0 1672 *
danielebarchiesi@0 1673 * @param $type
danielebarchiesi@0 1674 * The category to which this message belongs. Can be any string, but the
danielebarchiesi@0 1675 * general practice is to use the name of the module calling watchdog().
danielebarchiesi@0 1676 * @param $message
danielebarchiesi@0 1677 * The message to store in the log. Keep $message translatable
danielebarchiesi@0 1678 * by not concatenating dynamic values into it! Variables in the
danielebarchiesi@0 1679 * message should be added by using placeholder strings alongside
danielebarchiesi@0 1680 * the variables argument to declare the value of the placeholders.
danielebarchiesi@0 1681 * See t() for documentation on how $message and $variables interact.
danielebarchiesi@0 1682 * @param $variables
danielebarchiesi@0 1683 * Array of variables to replace in the message on display or
danielebarchiesi@0 1684 * NULL if message is already translated or not possible to
danielebarchiesi@0 1685 * translate.
danielebarchiesi@0 1686 * @param $severity
danielebarchiesi@0 1687 * The severity of the message; one of the following values as defined in
danielebarchiesi@0 1688 * @link http://www.faqs.org/rfcs/rfc3164.html RFC 3164: @endlink
danielebarchiesi@0 1689 * - WATCHDOG_EMERGENCY: Emergency, system is unusable.
danielebarchiesi@0 1690 * - WATCHDOG_ALERT: Alert, action must be taken immediately.
danielebarchiesi@0 1691 * - WATCHDOG_CRITICAL: Critical conditions.
danielebarchiesi@0 1692 * - WATCHDOG_ERROR: Error conditions.
danielebarchiesi@0 1693 * - WATCHDOG_WARNING: Warning conditions.
danielebarchiesi@0 1694 * - WATCHDOG_NOTICE: (default) Normal but significant conditions.
danielebarchiesi@0 1695 * - WATCHDOG_INFO: Informational messages.
danielebarchiesi@0 1696 * - WATCHDOG_DEBUG: Debug-level messages.
danielebarchiesi@0 1697 * @param $link
danielebarchiesi@0 1698 * A link to associate with the message.
danielebarchiesi@0 1699 *
danielebarchiesi@0 1700 * @see watchdog_severity_levels()
danielebarchiesi@0 1701 * @see hook_watchdog()
danielebarchiesi@0 1702 */
danielebarchiesi@0 1703 function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) {
danielebarchiesi@0 1704 global $user, $base_root;
danielebarchiesi@0 1705
danielebarchiesi@0 1706 static $in_error_state = FALSE;
danielebarchiesi@0 1707
danielebarchiesi@0 1708 // It is possible that the error handling will itself trigger an error. In that case, we could
danielebarchiesi@0 1709 // end up in an infinite loop. To avoid that, we implement a simple static semaphore.
danielebarchiesi@0 1710 if (!$in_error_state && function_exists('module_implements')) {
danielebarchiesi@0 1711 $in_error_state = TRUE;
danielebarchiesi@0 1712
danielebarchiesi@0 1713 // The user object may not exist in all conditions, so 0 is substituted if needed.
danielebarchiesi@0 1714 $user_uid = isset($user->uid) ? $user->uid : 0;
danielebarchiesi@0 1715
danielebarchiesi@0 1716 // Prepare the fields to be logged
danielebarchiesi@0 1717 $log_entry = array(
danielebarchiesi@0 1718 'type' => $type,
danielebarchiesi@0 1719 'message' => $message,
danielebarchiesi@0 1720 'variables' => $variables,
danielebarchiesi@0 1721 'severity' => $severity,
danielebarchiesi@0 1722 'link' => $link,
danielebarchiesi@0 1723 'user' => $user,
danielebarchiesi@0 1724 'uid' => $user_uid,
danielebarchiesi@0 1725 'request_uri' => $base_root . request_uri(),
danielebarchiesi@0 1726 'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',
danielebarchiesi@0 1727 'ip' => ip_address(),
danielebarchiesi@0 1728 // Request time isn't accurate for long processes, use time() instead.
danielebarchiesi@0 1729 'timestamp' => time(),
danielebarchiesi@0 1730 );
danielebarchiesi@0 1731
danielebarchiesi@0 1732 // Call the logging hooks to log/process the message
danielebarchiesi@0 1733 foreach (module_implements('watchdog') as $module) {
danielebarchiesi@0 1734 module_invoke($module, 'watchdog', $log_entry);
danielebarchiesi@0 1735 }
danielebarchiesi@0 1736
danielebarchiesi@0 1737 // It is critical that the semaphore is only cleared here, in the parent
danielebarchiesi@0 1738 // watchdog() call (not outside the loop), to prevent recursive execution.
danielebarchiesi@0 1739 $in_error_state = FALSE;
danielebarchiesi@0 1740 }
danielebarchiesi@0 1741 }
danielebarchiesi@0 1742
danielebarchiesi@0 1743 /**
danielebarchiesi@0 1744 * Sets a message to display to the user.
danielebarchiesi@0 1745 *
danielebarchiesi@0 1746 * Messages are stored in a session variable and displayed in page.tpl.php via
danielebarchiesi@0 1747 * the $messages theme variable.
danielebarchiesi@0 1748 *
danielebarchiesi@0 1749 * Example usage:
danielebarchiesi@0 1750 * @code
danielebarchiesi@0 1751 * drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
danielebarchiesi@0 1752 * @endcode
danielebarchiesi@0 1753 *
danielebarchiesi@0 1754 * @param string $message
danielebarchiesi@0 1755 * (optional) The translated message to be displayed to the user. For
danielebarchiesi@0 1756 * consistency with other messages, it should begin with a capital letter and
danielebarchiesi@0 1757 * end with a period.
danielebarchiesi@0 1758 * @param string $type
danielebarchiesi@0 1759 * (optional) The message's type. Defaults to 'status'. These values are
danielebarchiesi@0 1760 * supported:
danielebarchiesi@0 1761 * - 'status'
danielebarchiesi@0 1762 * - 'warning'
danielebarchiesi@0 1763 * - 'error'
danielebarchiesi@0 1764 * @param bool $repeat
danielebarchiesi@0 1765 * (optional) If this is FALSE and the message is already set, then the
danielebarchiesi@0 1766 * message won't be repeated. Defaults to TRUE.
danielebarchiesi@0 1767 *
danielebarchiesi@0 1768 * @return array|null
danielebarchiesi@0 1769 * A multidimensional array with keys corresponding to the set message types.
danielebarchiesi@0 1770 * The indexed array values of each contain the set messages for that type.
danielebarchiesi@0 1771 * Or, if there are no messages set, the function returns NULL.
danielebarchiesi@0 1772 *
danielebarchiesi@0 1773 * @see drupal_get_messages()
danielebarchiesi@0 1774 * @see theme_status_messages()
danielebarchiesi@0 1775 */
danielebarchiesi@0 1776 function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) {
danielebarchiesi@0 1777 if ($message) {
danielebarchiesi@0 1778 if (!isset($_SESSION['messages'][$type])) {
danielebarchiesi@0 1779 $_SESSION['messages'][$type] = array();
danielebarchiesi@0 1780 }
danielebarchiesi@0 1781
danielebarchiesi@0 1782 if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
danielebarchiesi@0 1783 $_SESSION['messages'][$type][] = $message;
danielebarchiesi@0 1784 }
danielebarchiesi@0 1785
danielebarchiesi@0 1786 // Mark this page as being uncacheable.
danielebarchiesi@0 1787 drupal_page_is_cacheable(FALSE);
danielebarchiesi@0 1788 }
danielebarchiesi@0 1789
danielebarchiesi@0 1790 // Messages not set when DB connection fails.
danielebarchiesi@0 1791 return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL;
danielebarchiesi@0 1792 }
danielebarchiesi@0 1793
danielebarchiesi@0 1794 /**
danielebarchiesi@0 1795 * Returns all messages that have been set with drupal_set_message().
danielebarchiesi@0 1796 *
danielebarchiesi@0 1797 * @param string $type
danielebarchiesi@0 1798 * (optional) Limit the messages returned by type. Defaults to NULL, meaning
danielebarchiesi@0 1799 * all types. These values are supported:
danielebarchiesi@0 1800 * - NULL
danielebarchiesi@0 1801 * - 'status'
danielebarchiesi@0 1802 * - 'warning'
danielebarchiesi@0 1803 * - 'error'
danielebarchiesi@0 1804 * @param bool $clear_queue
danielebarchiesi@0 1805 * (optional) If this is TRUE, the queue will be cleared of messages of the
danielebarchiesi@0 1806 * type specified in the $type parameter. Otherwise the queue will be left
danielebarchiesi@0 1807 * intact. Defaults to TRUE.
danielebarchiesi@0 1808 *
danielebarchiesi@0 1809 * @return array
danielebarchiesi@0 1810 * A multidimensional array with keys corresponding to the set message types.
danielebarchiesi@0 1811 * The indexed array values of each contain the set messages for that type.
danielebarchiesi@0 1812 * The messages returned are limited to the type specified in the $type
danielebarchiesi@0 1813 * parameter. If there are no messages of the specified type, an empty array
danielebarchiesi@0 1814 * is returned.
danielebarchiesi@0 1815 *
danielebarchiesi@0 1816 * @see drupal_set_message()
danielebarchiesi@0 1817 * @see theme_status_messages()
danielebarchiesi@0 1818 */
danielebarchiesi@0 1819 function drupal_get_messages($type = NULL, $clear_queue = TRUE) {
danielebarchiesi@0 1820 if ($messages = drupal_set_message()) {
danielebarchiesi@0 1821 if ($type) {
danielebarchiesi@0 1822 if ($clear_queue) {
danielebarchiesi@0 1823 unset($_SESSION['messages'][$type]);
danielebarchiesi@0 1824 }
danielebarchiesi@0 1825 if (isset($messages[$type])) {
danielebarchiesi@0 1826 return array($type => $messages[$type]);
danielebarchiesi@0 1827 }
danielebarchiesi@0 1828 }
danielebarchiesi@0 1829 else {
danielebarchiesi@0 1830 if ($clear_queue) {
danielebarchiesi@0 1831 unset($_SESSION['messages']);
danielebarchiesi@0 1832 }
danielebarchiesi@0 1833 return $messages;
danielebarchiesi@0 1834 }
danielebarchiesi@0 1835 }
danielebarchiesi@0 1836 return array();
danielebarchiesi@0 1837 }
danielebarchiesi@0 1838
danielebarchiesi@0 1839 /**
danielebarchiesi@0 1840 * Gets the title of the current page.
danielebarchiesi@0 1841 *
danielebarchiesi@0 1842 * The title is displayed on the page and in the title bar.
danielebarchiesi@0 1843 *
danielebarchiesi@0 1844 * @return
danielebarchiesi@0 1845 * The current page's title.
danielebarchiesi@0 1846 */
danielebarchiesi@0 1847 function drupal_get_title() {
danielebarchiesi@0 1848 $title = drupal_set_title();
danielebarchiesi@0 1849
danielebarchiesi@0 1850 // During a bootstrap, menu.inc is not included and thus we cannot provide a title.
danielebarchiesi@0 1851 if (!isset($title) && function_exists('menu_get_active_title')) {
danielebarchiesi@0 1852 $title = check_plain(menu_get_active_title());
danielebarchiesi@0 1853 }
danielebarchiesi@0 1854
danielebarchiesi@0 1855 return $title;
danielebarchiesi@0 1856 }
danielebarchiesi@0 1857
danielebarchiesi@0 1858 /**
danielebarchiesi@0 1859 * Sets the title of the current page.
danielebarchiesi@0 1860 *
danielebarchiesi@0 1861 * The title is displayed on the page and in the title bar.
danielebarchiesi@0 1862 *
danielebarchiesi@0 1863 * @param $title
danielebarchiesi@0 1864 * Optional string value to assign to the page title; or if set to NULL
danielebarchiesi@0 1865 * (default), leaves the current title unchanged.
danielebarchiesi@0 1866 * @param $output
danielebarchiesi@0 1867 * Optional flag - normally should be left as CHECK_PLAIN. Only set to
danielebarchiesi@0 1868 * PASS_THROUGH if you have already removed any possibly dangerous code
danielebarchiesi@0 1869 * from $title using a function like check_plain() or filter_xss(). With this
danielebarchiesi@0 1870 * flag the string will be passed through unchanged.
danielebarchiesi@0 1871 *
danielebarchiesi@0 1872 * @return
danielebarchiesi@0 1873 * The updated title of the current page.
danielebarchiesi@0 1874 */
danielebarchiesi@0 1875 function drupal_set_title($title = NULL, $output = CHECK_PLAIN) {
danielebarchiesi@0 1876 $stored_title = &drupal_static(__FUNCTION__);
danielebarchiesi@0 1877
danielebarchiesi@0 1878 if (isset($title)) {
danielebarchiesi@0 1879 $stored_title = ($output == PASS_THROUGH) ? $title : check_plain($title);
danielebarchiesi@0 1880 }
danielebarchiesi@0 1881
danielebarchiesi@0 1882 return $stored_title;
danielebarchiesi@0 1883 }
danielebarchiesi@0 1884
danielebarchiesi@0 1885 /**
danielebarchiesi@0 1886 * Checks to see if an IP address has been blocked.
danielebarchiesi@0 1887 *
danielebarchiesi@0 1888 * Blocked IP addresses are stored in the database by default. However for
danielebarchiesi@0 1889 * performance reasons we allow an override in settings.php. This allows us
danielebarchiesi@0 1890 * to avoid querying the database at this critical stage of the bootstrap if
danielebarchiesi@0 1891 * an administrative interface for IP address blocking is not required.
danielebarchiesi@0 1892 *
danielebarchiesi@0 1893 * @param $ip
danielebarchiesi@0 1894 * IP address to check.
danielebarchiesi@0 1895 *
danielebarchiesi@0 1896 * @return bool
danielebarchiesi@0 1897 * TRUE if access is denied, FALSE if access is allowed.
danielebarchiesi@0 1898 */
danielebarchiesi@0 1899 function drupal_is_denied($ip) {
danielebarchiesi@0 1900 // Because this function is called on every page request, we first check
danielebarchiesi@0 1901 // for an array of IP addresses in settings.php before querying the
danielebarchiesi@0 1902 // database.
danielebarchiesi@0 1903 $blocked_ips = variable_get('blocked_ips');
danielebarchiesi@0 1904 $denied = FALSE;
danielebarchiesi@0 1905 if (isset($blocked_ips) && is_array($blocked_ips)) {
danielebarchiesi@0 1906 $denied = in_array($ip, $blocked_ips);
danielebarchiesi@0 1907 }
danielebarchiesi@0 1908 // Only check if database.inc is loaded already. If
danielebarchiesi@0 1909 // $conf['page_cache_without_database'] = TRUE; is set in settings.php,
danielebarchiesi@0 1910 // then the database won't be loaded here so the IPs in the database
danielebarchiesi@0 1911 // won't be denied. However the user asked explicitly not to use the
danielebarchiesi@0 1912 // database and also in this case it's quite likely that the user relies
danielebarchiesi@0 1913 // on higher performance solutions like a firewall.
danielebarchiesi@0 1914 elseif (class_exists('Database', FALSE)) {
danielebarchiesi@0 1915 $denied = (bool)db_query("SELECT 1 FROM {blocked_ips} WHERE ip = :ip", array(':ip' => $ip))->fetchField();
danielebarchiesi@0 1916 }
danielebarchiesi@0 1917 return $denied;
danielebarchiesi@0 1918 }
danielebarchiesi@0 1919
danielebarchiesi@0 1920 /**
danielebarchiesi@0 1921 * Handles denied users.
danielebarchiesi@0 1922 *
danielebarchiesi@0 1923 * @param $ip
danielebarchiesi@0 1924 * IP address to check. Prints a message and exits if access is denied.
danielebarchiesi@0 1925 */
danielebarchiesi@0 1926 function drupal_block_denied($ip) {
danielebarchiesi@0 1927 // Deny access to blocked IP addresses - t() is not yet available.
danielebarchiesi@0 1928 if (drupal_is_denied($ip)) {
danielebarchiesi@0 1929 header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
danielebarchiesi@0 1930 print 'Sorry, ' . check_plain(ip_address()) . ' has been banned.';
danielebarchiesi@0 1931 exit();
danielebarchiesi@0 1932 }
danielebarchiesi@0 1933 }
danielebarchiesi@0 1934
danielebarchiesi@0 1935 /**
danielebarchiesi@0 1936 * Returns a string of highly randomized bytes (over the full 8-bit range).
danielebarchiesi@0 1937 *
danielebarchiesi@0 1938 * This function is better than simply calling mt_rand() or any other built-in
danielebarchiesi@0 1939 * PHP function because it can return a long string of bytes (compared to < 4
danielebarchiesi@0 1940 * bytes normally from mt_rand()) and uses the best available pseudo-random
danielebarchiesi@0 1941 * source.
danielebarchiesi@0 1942 *
danielebarchiesi@0 1943 * @param $count
danielebarchiesi@0 1944 * The number of characters (bytes) to return in the string.
danielebarchiesi@0 1945 */
danielebarchiesi@0 1946 function drupal_random_bytes($count) {
danielebarchiesi@0 1947 // $random_state does not use drupal_static as it stores random bytes.
danielebarchiesi@0 1948 static $random_state, $bytes, $php_compatible;
danielebarchiesi@0 1949 // Initialize on the first call. The contents of $_SERVER includes a mix of
danielebarchiesi@0 1950 // user-specific and system information that varies a little with each page.
danielebarchiesi@0 1951 if (!isset($random_state)) {
danielebarchiesi@0 1952 $random_state = print_r($_SERVER, TRUE);
danielebarchiesi@0 1953 if (function_exists('getmypid')) {
danielebarchiesi@0 1954 // Further initialize with the somewhat random PHP process ID.
danielebarchiesi@0 1955 $random_state .= getmypid();
danielebarchiesi@0 1956 }
danielebarchiesi@0 1957 $bytes = '';
danielebarchiesi@0 1958 }
danielebarchiesi@0 1959 if (strlen($bytes) < $count) {
danielebarchiesi@0 1960 // PHP versions prior 5.3.4 experienced openssl_random_pseudo_bytes()
danielebarchiesi@0 1961 // locking on Windows and rendered it unusable.
danielebarchiesi@0 1962 if (!isset($php_compatible)) {
danielebarchiesi@0 1963 $php_compatible = version_compare(PHP_VERSION, '5.3.4', '>=');
danielebarchiesi@0 1964 }
danielebarchiesi@0 1965 // /dev/urandom is available on many *nix systems and is considered the
danielebarchiesi@0 1966 // best commonly available pseudo-random source.
danielebarchiesi@0 1967 if ($fh = @fopen('/dev/urandom', 'rb')) {
danielebarchiesi@0 1968 // PHP only performs buffered reads, so in reality it will always read
danielebarchiesi@0 1969 // at least 4096 bytes. Thus, it costs nothing extra to read and store
danielebarchiesi@0 1970 // that much so as to speed any additional invocations.
danielebarchiesi@0 1971 $bytes .= fread($fh, max(4096, $count));
danielebarchiesi@0 1972 fclose($fh);
danielebarchiesi@0 1973 }
danielebarchiesi@0 1974 // openssl_random_pseudo_bytes() will find entropy in a system-dependent
danielebarchiesi@0 1975 // way.
danielebarchiesi@0 1976 elseif ($php_compatible && function_exists('openssl_random_pseudo_bytes')) {
danielebarchiesi@0 1977 $bytes .= openssl_random_pseudo_bytes($count - strlen($bytes));
danielebarchiesi@0 1978 }
danielebarchiesi@0 1979 // If /dev/urandom is not available or returns no bytes, this loop will
danielebarchiesi@0 1980 // generate a good set of pseudo-random bytes on any system.
danielebarchiesi@0 1981 // Note that it may be important that our $random_state is passed
danielebarchiesi@0 1982 // through hash() prior to being rolled into $output, that the two hash()
danielebarchiesi@0 1983 // invocations are different, and that the extra input into the first one -
danielebarchiesi@0 1984 // the microtime() - is prepended rather than appended. This is to avoid
danielebarchiesi@0 1985 // directly leaking $random_state via the $output stream, which could
danielebarchiesi@0 1986 // allow for trivial prediction of further "random" numbers.
danielebarchiesi@0 1987 while (strlen($bytes) < $count) {
danielebarchiesi@0 1988 $random_state = hash('sha256', microtime() . mt_rand() . $random_state);
danielebarchiesi@0 1989 $bytes .= hash('sha256', mt_rand() . $random_state, TRUE);
danielebarchiesi@0 1990 }
danielebarchiesi@0 1991 }
danielebarchiesi@0 1992 $output = substr($bytes, 0, $count);
danielebarchiesi@0 1993 $bytes = substr($bytes, $count);
danielebarchiesi@0 1994 return $output;
danielebarchiesi@0 1995 }
danielebarchiesi@0 1996
danielebarchiesi@0 1997 /**
danielebarchiesi@0 1998 * Calculates a base-64 encoded, URL-safe sha-256 hmac.
danielebarchiesi@0 1999 *
danielebarchiesi@0 2000 * @param $data
danielebarchiesi@0 2001 * String to be validated with the hmac.
danielebarchiesi@0 2002 * @param $key
danielebarchiesi@0 2003 * A secret string key.
danielebarchiesi@0 2004 *
danielebarchiesi@0 2005 * @return
danielebarchiesi@0 2006 * A base-64 encoded sha-256 hmac, with + replaced with -, / with _ and
danielebarchiesi@0 2007 * any = padding characters removed.
danielebarchiesi@0 2008 */
danielebarchiesi@0 2009 function drupal_hmac_base64($data, $key) {
danielebarchiesi@0 2010 $hmac = base64_encode(hash_hmac('sha256', $data, $key, TRUE));
danielebarchiesi@0 2011 // Modify the hmac so it's safe to use in URLs.
danielebarchiesi@0 2012 return strtr($hmac, array('+' => '-', '/' => '_', '=' => ''));
danielebarchiesi@0 2013 }
danielebarchiesi@0 2014
danielebarchiesi@0 2015 /**
danielebarchiesi@0 2016 * Calculates a base-64 encoded, URL-safe sha-256 hash.
danielebarchiesi@0 2017 *
danielebarchiesi@0 2018 * @param $data
danielebarchiesi@0 2019 * String to be hashed.
danielebarchiesi@0 2020 *
danielebarchiesi@0 2021 * @return
danielebarchiesi@0 2022 * A base-64 encoded sha-256 hash, with + replaced with -, / with _ and
danielebarchiesi@0 2023 * any = padding characters removed.
danielebarchiesi@0 2024 */
danielebarchiesi@0 2025 function drupal_hash_base64($data) {
danielebarchiesi@0 2026 $hash = base64_encode(hash('sha256', $data, TRUE));
danielebarchiesi@0 2027 // Modify the hash so it's safe to use in URLs.
danielebarchiesi@0 2028 return strtr($hash, array('+' => '-', '/' => '_', '=' => ''));
danielebarchiesi@0 2029 }
danielebarchiesi@0 2030
danielebarchiesi@0 2031 /**
danielebarchiesi@0 2032 * Merges multiple arrays, recursively, and returns the merged array.
danielebarchiesi@0 2033 *
danielebarchiesi@0 2034 * This function is similar to PHP's array_merge_recursive() function, but it
danielebarchiesi@0 2035 * handles non-array values differently. When merging values that are not both
danielebarchiesi@0 2036 * arrays, the latter value replaces the former rather than merging with it.
danielebarchiesi@0 2037 *
danielebarchiesi@0 2038 * Example:
danielebarchiesi@0 2039 * @code
danielebarchiesi@0 2040 * $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b')));
danielebarchiesi@0 2041 * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd')));
danielebarchiesi@0 2042 *
danielebarchiesi@0 2043 * // This results in array('fragment' => array('x', 'y'), 'attributes' => array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b', 'c', 'd'))).
danielebarchiesi@0 2044 * $incorrect = array_merge_recursive($link_options_1, $link_options_2);
danielebarchiesi@0 2045 *
danielebarchiesi@0 2046 * // This results in array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd'))).
danielebarchiesi@0 2047 * $correct = drupal_array_merge_deep($link_options_1, $link_options_2);
danielebarchiesi@0 2048 * @endcode
danielebarchiesi@0 2049 *
danielebarchiesi@0 2050 * @param ...
danielebarchiesi@0 2051 * Arrays to merge.
danielebarchiesi@0 2052 *
danielebarchiesi@0 2053 * @return
danielebarchiesi@0 2054 * The merged array.
danielebarchiesi@0 2055 *
danielebarchiesi@0 2056 * @see drupal_array_merge_deep_array()
danielebarchiesi@0 2057 */
danielebarchiesi@0 2058 function drupal_array_merge_deep() {
danielebarchiesi@0 2059 $args = func_get_args();
danielebarchiesi@0 2060 return drupal_array_merge_deep_array($args);
danielebarchiesi@0 2061 }
danielebarchiesi@0 2062
danielebarchiesi@0 2063 /**
danielebarchiesi@0 2064 * Merges multiple arrays, recursively, and returns the merged array.
danielebarchiesi@0 2065 *
danielebarchiesi@0 2066 * This function is equivalent to drupal_array_merge_deep(), except the
danielebarchiesi@0 2067 * input arrays are passed as a single array parameter rather than a variable
danielebarchiesi@0 2068 * parameter list.
danielebarchiesi@0 2069 *
danielebarchiesi@0 2070 * The following are equivalent:
danielebarchiesi@0 2071 * - drupal_array_merge_deep($a, $b);
danielebarchiesi@0 2072 * - drupal_array_merge_deep_array(array($a, $b));
danielebarchiesi@0 2073 *
danielebarchiesi@0 2074 * The following are also equivalent:
danielebarchiesi@0 2075 * - call_user_func_array('drupal_array_merge_deep', $arrays_to_merge);
danielebarchiesi@0 2076 * - drupal_array_merge_deep_array($arrays_to_merge);
danielebarchiesi@0 2077 *
danielebarchiesi@0 2078 * @see drupal_array_merge_deep()
danielebarchiesi@0 2079 */
danielebarchiesi@0 2080 function drupal_array_merge_deep_array($arrays) {
danielebarchiesi@0 2081 $result = array();
danielebarchiesi@0 2082
danielebarchiesi@0 2083 foreach ($arrays as $array) {
danielebarchiesi@0 2084 foreach ($array as $key => $value) {
danielebarchiesi@0 2085 // Renumber integer keys as array_merge_recursive() does. Note that PHP
danielebarchiesi@0 2086 // automatically converts array keys that are integer strings (e.g., '1')
danielebarchiesi@0 2087 // to integers.
danielebarchiesi@0 2088 if (is_integer($key)) {
danielebarchiesi@0 2089 $result[] = $value;
danielebarchiesi@0 2090 }
danielebarchiesi@0 2091 // Recurse when both values are arrays.
danielebarchiesi@0 2092 elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
danielebarchiesi@0 2093 $result[$key] = drupal_array_merge_deep_array(array($result[$key], $value));
danielebarchiesi@0 2094 }
danielebarchiesi@0 2095 // Otherwise, use the latter value, overriding any previous value.
danielebarchiesi@0 2096 else {
danielebarchiesi@0 2097 $result[$key] = $value;
danielebarchiesi@0 2098 }
danielebarchiesi@0 2099 }
danielebarchiesi@0 2100 }
danielebarchiesi@0 2101
danielebarchiesi@0 2102 return $result;
danielebarchiesi@0 2103 }
danielebarchiesi@0 2104
danielebarchiesi@0 2105 /**
danielebarchiesi@0 2106 * Generates a default anonymous $user object.
danielebarchiesi@0 2107 *
danielebarchiesi@0 2108 * @return Object - the user object.
danielebarchiesi@0 2109 */
danielebarchiesi@0 2110 function drupal_anonymous_user() {
danielebarchiesi@0 2111 $user = new stdClass();
danielebarchiesi@0 2112 $user->uid = 0;
danielebarchiesi@0 2113 $user->hostname = ip_address();
danielebarchiesi@0 2114 $user->roles = array();
danielebarchiesi@0 2115 $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
danielebarchiesi@0 2116 $user->cache = 0;
danielebarchiesi@0 2117 return $user;
danielebarchiesi@0 2118 }
danielebarchiesi@0 2119
danielebarchiesi@0 2120 /**
danielebarchiesi@0 2121 * Ensures Drupal is bootstrapped to the specified phase.
danielebarchiesi@0 2122 *
danielebarchiesi@0 2123 * In order to bootstrap Drupal from another PHP script, you can use this code:
danielebarchiesi@0 2124 * @code
danielebarchiesi@0 2125 * define('DRUPAL_ROOT', '/path/to/drupal');
danielebarchiesi@0 2126 * require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
danielebarchiesi@0 2127 * drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
danielebarchiesi@0 2128 * @endcode
danielebarchiesi@0 2129 *
danielebarchiesi@0 2130 * @param $phase
danielebarchiesi@0 2131 * A constant telling which phase to bootstrap to. When you bootstrap to a
danielebarchiesi@0 2132 * particular phase, all earlier phases are run automatically. Possible
danielebarchiesi@0 2133 * values:
danielebarchiesi@0 2134 * - DRUPAL_BOOTSTRAP_CONFIGURATION: Initializes configuration.
danielebarchiesi@0 2135 * - DRUPAL_BOOTSTRAP_PAGE_CACHE: Tries to serve a cached page.
danielebarchiesi@0 2136 * - DRUPAL_BOOTSTRAP_DATABASE: Initializes the database layer.
danielebarchiesi@0 2137 * - DRUPAL_BOOTSTRAP_VARIABLES: Initializes the variable system.
danielebarchiesi@0 2138 * - DRUPAL_BOOTSTRAP_SESSION: Initializes session handling.
danielebarchiesi@0 2139 * - DRUPAL_BOOTSTRAP_PAGE_HEADER: Sets up the page header.
danielebarchiesi@0 2140 * - DRUPAL_BOOTSTRAP_LANGUAGE: Finds out the language of the page.
danielebarchiesi@0 2141 * - DRUPAL_BOOTSTRAP_FULL: Fully loads Drupal. Validates and fixes input
danielebarchiesi@0 2142 * data.
danielebarchiesi@0 2143 * @param $new_phase
danielebarchiesi@0 2144 * A boolean, set to FALSE if calling drupal_bootstrap from inside a
danielebarchiesi@0 2145 * function called from drupal_bootstrap (recursion).
danielebarchiesi@0 2146 *
danielebarchiesi@0 2147 * @return
danielebarchiesi@0 2148 * The most recently completed phase.
danielebarchiesi@0 2149 */
danielebarchiesi@0 2150 function drupal_bootstrap($phase = NULL, $new_phase = TRUE) {
danielebarchiesi@0 2151 // Not drupal_static(), because does not depend on any run-time information.
danielebarchiesi@0 2152 static $phases = array(
danielebarchiesi@0 2153 DRUPAL_BOOTSTRAP_CONFIGURATION,
danielebarchiesi@0 2154 DRUPAL_BOOTSTRAP_PAGE_CACHE,
danielebarchiesi@0 2155 DRUPAL_BOOTSTRAP_DATABASE,
danielebarchiesi@0 2156 DRUPAL_BOOTSTRAP_VARIABLES,
danielebarchiesi@0 2157 DRUPAL_BOOTSTRAP_SESSION,
danielebarchiesi@0 2158 DRUPAL_BOOTSTRAP_PAGE_HEADER,
danielebarchiesi@0 2159 DRUPAL_BOOTSTRAP_LANGUAGE,
danielebarchiesi@0 2160 DRUPAL_BOOTSTRAP_FULL,
danielebarchiesi@0 2161 );
danielebarchiesi@0 2162 // Not drupal_static(), because the only legitimate API to control this is to
danielebarchiesi@0 2163 // call drupal_bootstrap() with a new phase parameter.
danielebarchiesi@0 2164 static $final_phase;
danielebarchiesi@0 2165 // Not drupal_static(), because it's impossible to roll back to an earlier
danielebarchiesi@0 2166 // bootstrap state.
danielebarchiesi@0 2167 static $stored_phase = -1;
danielebarchiesi@0 2168
danielebarchiesi@0 2169 // When not recursing, store the phase name so it's not forgotten while
danielebarchiesi@0 2170 // recursing.
danielebarchiesi@0 2171 if ($new_phase) {
danielebarchiesi@0 2172 $final_phase = $phase;
danielebarchiesi@0 2173 }
danielebarchiesi@0 2174 if (isset($phase)) {
danielebarchiesi@0 2175 // Call a phase if it has not been called before and is below the requested
danielebarchiesi@0 2176 // phase.
danielebarchiesi@0 2177 while ($phases && $phase > $stored_phase && $final_phase > $stored_phase) {
danielebarchiesi@0 2178 $current_phase = array_shift($phases);
danielebarchiesi@0 2179
danielebarchiesi@0 2180 // This function is re-entrant. Only update the completed phase when the
danielebarchiesi@0 2181 // current call actually resulted in a progress in the bootstrap process.
danielebarchiesi@0 2182 if ($current_phase > $stored_phase) {
danielebarchiesi@0 2183 $stored_phase = $current_phase;
danielebarchiesi@0 2184 }
danielebarchiesi@0 2185
danielebarchiesi@0 2186 switch ($current_phase) {
danielebarchiesi@0 2187 case DRUPAL_BOOTSTRAP_CONFIGURATION:
danielebarchiesi@0 2188 _drupal_bootstrap_configuration();
danielebarchiesi@0 2189 break;
danielebarchiesi@0 2190
danielebarchiesi@0 2191 case DRUPAL_BOOTSTRAP_PAGE_CACHE:
danielebarchiesi@0 2192 _drupal_bootstrap_page_cache();
danielebarchiesi@0 2193 break;
danielebarchiesi@0 2194
danielebarchiesi@0 2195 case DRUPAL_BOOTSTRAP_DATABASE:
danielebarchiesi@0 2196 _drupal_bootstrap_database();
danielebarchiesi@0 2197 break;
danielebarchiesi@0 2198
danielebarchiesi@0 2199 case DRUPAL_BOOTSTRAP_VARIABLES:
danielebarchiesi@0 2200 _drupal_bootstrap_variables();
danielebarchiesi@0 2201 break;
danielebarchiesi@0 2202
danielebarchiesi@0 2203 case DRUPAL_BOOTSTRAP_SESSION:
danielebarchiesi@0 2204 require_once DRUPAL_ROOT . '/' . variable_get('session_inc', 'includes/session.inc');
danielebarchiesi@0 2205 drupal_session_initialize();
danielebarchiesi@0 2206 break;
danielebarchiesi@0 2207
danielebarchiesi@0 2208 case DRUPAL_BOOTSTRAP_PAGE_HEADER:
danielebarchiesi@0 2209 _drupal_bootstrap_page_header();
danielebarchiesi@0 2210 break;
danielebarchiesi@0 2211
danielebarchiesi@0 2212 case DRUPAL_BOOTSTRAP_LANGUAGE:
danielebarchiesi@0 2213 drupal_language_initialize();
danielebarchiesi@0 2214 break;
danielebarchiesi@0 2215
danielebarchiesi@0 2216 case DRUPAL_BOOTSTRAP_FULL:
danielebarchiesi@0 2217 require_once DRUPAL_ROOT . '/includes/common.inc';
danielebarchiesi@0 2218 _drupal_bootstrap_full();
danielebarchiesi@0 2219 break;
danielebarchiesi@0 2220 }
danielebarchiesi@0 2221 }
danielebarchiesi@0 2222 }
danielebarchiesi@0 2223 return $stored_phase;
danielebarchiesi@0 2224 }
danielebarchiesi@0 2225
danielebarchiesi@0 2226 /**
danielebarchiesi@0 2227 * Returns the time zone of the current user.
danielebarchiesi@0 2228 */
danielebarchiesi@0 2229 function drupal_get_user_timezone() {
danielebarchiesi@0 2230 global $user;
danielebarchiesi@0 2231 if (variable_get('configurable_timezones', 1) && $user->uid && $user->timezone) {
danielebarchiesi@0 2232 return $user->timezone;
danielebarchiesi@0 2233 }
danielebarchiesi@0 2234 else {
danielebarchiesi@0 2235 // Ignore PHP strict notice if time zone has not yet been set in the php.ini
danielebarchiesi@0 2236 // configuration.
danielebarchiesi@0 2237 return variable_get('date_default_timezone', @date_default_timezone_get());
danielebarchiesi@0 2238 }
danielebarchiesi@0 2239 }
danielebarchiesi@0 2240
danielebarchiesi@0 2241 /**
danielebarchiesi@0 2242 * Gets a salt useful for hardening against SQL injection.
danielebarchiesi@0 2243 *
danielebarchiesi@0 2244 * @return
danielebarchiesi@0 2245 * A salt based on information in settings.php, not in the database.
danielebarchiesi@0 2246 */
danielebarchiesi@0 2247 function drupal_get_hash_salt() {
danielebarchiesi@0 2248 global $drupal_hash_salt, $databases;
danielebarchiesi@0 2249 // If the $drupal_hash_salt variable is empty, a hash of the serialized
danielebarchiesi@0 2250 // database credentials is used as a fallback salt.
danielebarchiesi@0 2251 return empty($drupal_hash_salt) ? hash('sha256', serialize($databases)) : $drupal_hash_salt;
danielebarchiesi@0 2252 }
danielebarchiesi@0 2253
danielebarchiesi@0 2254 /**
danielebarchiesi@0 2255 * Provides custom PHP error handling.
danielebarchiesi@0 2256 *
danielebarchiesi@0 2257 * @param $error_level
danielebarchiesi@0 2258 * The level of the error raised.
danielebarchiesi@0 2259 * @param $message
danielebarchiesi@0 2260 * The error message.
danielebarchiesi@0 2261 * @param $filename
danielebarchiesi@0 2262 * The filename that the error was raised in.
danielebarchiesi@0 2263 * @param $line
danielebarchiesi@0 2264 * The line number the error was raised at.
danielebarchiesi@0 2265 * @param $context
danielebarchiesi@0 2266 * An array that points to the active symbol table at the point the error
danielebarchiesi@0 2267 * occurred.
danielebarchiesi@0 2268 */
danielebarchiesi@0 2269 function _drupal_error_handler($error_level, $message, $filename, $line, $context) {
danielebarchiesi@0 2270 require_once DRUPAL_ROOT . '/includes/errors.inc';
danielebarchiesi@0 2271 _drupal_error_handler_real($error_level, $message, $filename, $line, $context);
danielebarchiesi@0 2272 }
danielebarchiesi@0 2273
danielebarchiesi@0 2274 /**
danielebarchiesi@0 2275 * Provides custom PHP exception handling.
danielebarchiesi@0 2276 *
danielebarchiesi@0 2277 * Uncaught exceptions are those not enclosed in a try/catch block. They are
danielebarchiesi@0 2278 * always fatal: the execution of the script will stop as soon as the exception
danielebarchiesi@0 2279 * handler exits.
danielebarchiesi@0 2280 *
danielebarchiesi@0 2281 * @param $exception
danielebarchiesi@0 2282 * The exception object that was thrown.
danielebarchiesi@0 2283 */
danielebarchiesi@0 2284 function _drupal_exception_handler($exception) {
danielebarchiesi@0 2285 require_once DRUPAL_ROOT . '/includes/errors.inc';
danielebarchiesi@0 2286
danielebarchiesi@0 2287 try {
danielebarchiesi@0 2288 // Log the message to the watchdog and return an error page to the user.
danielebarchiesi@0 2289 _drupal_log_error(_drupal_decode_exception($exception), TRUE);
danielebarchiesi@0 2290 }
danielebarchiesi@0 2291 catch (Exception $exception2) {
danielebarchiesi@0 2292 // Another uncaught exception was thrown while handling the first one.
danielebarchiesi@0 2293 // If we are displaying errors, then do so with no possibility of a further uncaught exception being thrown.
danielebarchiesi@0 2294 if (error_displayable()) {
danielebarchiesi@0 2295 print '<h1>Additional uncaught exception thrown while handling exception.</h1>';
danielebarchiesi@0 2296 print '<h2>Original</h2><p>' . _drupal_render_exception_safe($exception) . '</p>';
danielebarchiesi@0 2297 print '<h2>Additional</h2><p>' . _drupal_render_exception_safe($exception2) . '</p><hr />';
danielebarchiesi@0 2298 }
danielebarchiesi@0 2299 }
danielebarchiesi@0 2300 }
danielebarchiesi@0 2301
danielebarchiesi@0 2302 /**
danielebarchiesi@0 2303 * Sets up the script environment and loads settings.php.
danielebarchiesi@0 2304 */
danielebarchiesi@0 2305 function _drupal_bootstrap_configuration() {
danielebarchiesi@0 2306 // Set the Drupal custom error handler.
danielebarchiesi@0 2307 set_error_handler('_drupal_error_handler');
danielebarchiesi@0 2308 set_exception_handler('_drupal_exception_handler');
danielebarchiesi@0 2309
danielebarchiesi@0 2310 drupal_environment_initialize();
danielebarchiesi@0 2311 // Start a page timer:
danielebarchiesi@0 2312 timer_start('page');
danielebarchiesi@0 2313 // Initialize the configuration, including variables from settings.php.
danielebarchiesi@0 2314 drupal_settings_initialize();
danielebarchiesi@0 2315 }
danielebarchiesi@0 2316
danielebarchiesi@0 2317 /**
danielebarchiesi@0 2318 * Attempts to serve a page from the cache.
danielebarchiesi@0 2319 */
danielebarchiesi@0 2320 function _drupal_bootstrap_page_cache() {
danielebarchiesi@0 2321 global $user;
danielebarchiesi@0 2322
danielebarchiesi@0 2323 // Allow specifying special cache handlers in settings.php, like
danielebarchiesi@0 2324 // using memcached or files for storing cache information.
danielebarchiesi@0 2325 require_once DRUPAL_ROOT . '/includes/cache.inc';
danielebarchiesi@0 2326 foreach (variable_get('cache_backends', array()) as $include) {
danielebarchiesi@0 2327 require_once DRUPAL_ROOT . '/' . $include;
danielebarchiesi@0 2328 }
danielebarchiesi@0 2329 // Check for a cache mode force from settings.php.
danielebarchiesi@0 2330 if (variable_get('page_cache_without_database')) {
danielebarchiesi@0 2331 $cache_enabled = TRUE;
danielebarchiesi@0 2332 }
danielebarchiesi@0 2333 else {
danielebarchiesi@0 2334 drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE);
danielebarchiesi@0 2335 $cache_enabled = variable_get('cache');
danielebarchiesi@0 2336 }
danielebarchiesi@0 2337 drupal_block_denied(ip_address());
danielebarchiesi@0 2338 // If there is no session cookie and cache is enabled (or forced), try
danielebarchiesi@0 2339 // to serve a cached page.
danielebarchiesi@0 2340 if (!isset($_COOKIE[session_name()]) && $cache_enabled) {
danielebarchiesi@0 2341 // Make sure there is a user object because its timestamp will be
danielebarchiesi@0 2342 // checked, hook_boot might check for anonymous user etc.
danielebarchiesi@0 2343 $user = drupal_anonymous_user();
danielebarchiesi@0 2344 // Get the page from the cache.
danielebarchiesi@0 2345 $cache = drupal_page_get_cache();
danielebarchiesi@0 2346 // If there is a cached page, display it.
danielebarchiesi@0 2347 if (is_object($cache)) {
danielebarchiesi@0 2348 header('X-Drupal-Cache: HIT');
danielebarchiesi@0 2349 // Restore the metadata cached with the page.
danielebarchiesi@0 2350 $_GET['q'] = $cache->data['path'];
danielebarchiesi@0 2351 drupal_set_title($cache->data['title'], PASS_THROUGH);
danielebarchiesi@0 2352 date_default_timezone_set(drupal_get_user_timezone());
danielebarchiesi@0 2353 // If the skipping of the bootstrap hooks is not enforced, call
danielebarchiesi@0 2354 // hook_boot.
danielebarchiesi@0 2355 if (variable_get('page_cache_invoke_hooks', TRUE)) {
danielebarchiesi@0 2356 bootstrap_invoke_all('boot');
danielebarchiesi@0 2357 }
danielebarchiesi@0 2358 drupal_serve_page_from_cache($cache);
danielebarchiesi@0 2359 // If the skipping of the bootstrap hooks is not enforced, call
danielebarchiesi@0 2360 // hook_exit.
danielebarchiesi@0 2361 if (variable_get('page_cache_invoke_hooks', TRUE)) {
danielebarchiesi@0 2362 bootstrap_invoke_all('exit');
danielebarchiesi@0 2363 }
danielebarchiesi@0 2364 // We are done.
danielebarchiesi@0 2365 exit;
danielebarchiesi@0 2366 }
danielebarchiesi@0 2367 else {
danielebarchiesi@0 2368 header('X-Drupal-Cache: MISS');
danielebarchiesi@0 2369 }
danielebarchiesi@0 2370 }
danielebarchiesi@0 2371 }
danielebarchiesi@0 2372
danielebarchiesi@0 2373 /**
danielebarchiesi@0 2374 * Initializes the database system and registers autoload functions.
danielebarchiesi@0 2375 */
danielebarchiesi@0 2376 function _drupal_bootstrap_database() {
danielebarchiesi@0 2377 // Redirect the user to the installation script if Drupal has not been
danielebarchiesi@0 2378 // installed yet (i.e., if no $databases array has been defined in the
danielebarchiesi@0 2379 // settings.php file) and we are not already installing.
danielebarchiesi@0 2380 if (empty($GLOBALS['databases']) && !drupal_installation_attempted()) {
danielebarchiesi@0 2381 include_once DRUPAL_ROOT . '/includes/install.inc';
danielebarchiesi@0 2382 install_goto('install.php');
danielebarchiesi@0 2383 }
danielebarchiesi@0 2384
danielebarchiesi@0 2385 // The user agent header is used to pass a database prefix in the request when
danielebarchiesi@0 2386 // running tests. However, for security reasons, it is imperative that we
danielebarchiesi@0 2387 // validate we ourselves made the request.
danielebarchiesi@0 2388 if ($test_prefix = drupal_valid_test_ua()) {
danielebarchiesi@0 2389 // Set the test run id for use in other parts of Drupal.
danielebarchiesi@0 2390 $test_info = &$GLOBALS['drupal_test_info'];
danielebarchiesi@0 2391 $test_info['test_run_id'] = $test_prefix;
danielebarchiesi@0 2392 $test_info['in_child_site'] = TRUE;
danielebarchiesi@0 2393
danielebarchiesi@0 2394 foreach ($GLOBALS['databases']['default'] as &$value) {
danielebarchiesi@0 2395 // Extract the current default database prefix.
danielebarchiesi@0 2396 if (!isset($value['prefix'])) {
danielebarchiesi@0 2397 $current_prefix = '';
danielebarchiesi@0 2398 }
danielebarchiesi@0 2399 elseif (is_array($value['prefix'])) {
danielebarchiesi@0 2400 $current_prefix = $value['prefix']['default'];
danielebarchiesi@0 2401 }
danielebarchiesi@0 2402 else {
danielebarchiesi@0 2403 $current_prefix = $value['prefix'];
danielebarchiesi@0 2404 }
danielebarchiesi@0 2405
danielebarchiesi@0 2406 // Remove the current database prefix and replace it by our own.
danielebarchiesi@0 2407 $value['prefix'] = array(
danielebarchiesi@0 2408 'default' => $current_prefix . $test_prefix,
danielebarchiesi@0 2409 );
danielebarchiesi@0 2410 }
danielebarchiesi@0 2411 }
danielebarchiesi@0 2412
danielebarchiesi@0 2413 // Initialize the database system. Note that the connection
danielebarchiesi@0 2414 // won't be initialized until it is actually requested.
danielebarchiesi@0 2415 require_once DRUPAL_ROOT . '/includes/database/database.inc';
danielebarchiesi@0 2416
danielebarchiesi@0 2417 // Register autoload functions so that we can access classes and interfaces.
danielebarchiesi@0 2418 // The database autoload routine comes first so that we can load the database
danielebarchiesi@0 2419 // system without hitting the database. That is especially important during
danielebarchiesi@0 2420 // the install or upgrade process.
danielebarchiesi@0 2421 spl_autoload_register('drupal_autoload_class');
danielebarchiesi@0 2422 spl_autoload_register('drupal_autoload_interface');
danielebarchiesi@0 2423 }
danielebarchiesi@0 2424
danielebarchiesi@0 2425 /**
danielebarchiesi@0 2426 * Loads system variables and all enabled bootstrap modules.
danielebarchiesi@0 2427 */
danielebarchiesi@0 2428 function _drupal_bootstrap_variables() {
danielebarchiesi@0 2429 global $conf;
danielebarchiesi@0 2430
danielebarchiesi@0 2431 // Initialize the lock system.
danielebarchiesi@0 2432 require_once DRUPAL_ROOT . '/' . variable_get('lock_inc', 'includes/lock.inc');
danielebarchiesi@0 2433 lock_initialize();
danielebarchiesi@0 2434
danielebarchiesi@0 2435 // Load variables from the database, but do not overwrite variables set in settings.php.
danielebarchiesi@0 2436 $conf = variable_initialize(isset($conf) ? $conf : array());
danielebarchiesi@0 2437 // Load bootstrap modules.
danielebarchiesi@0 2438 require_once DRUPAL_ROOT . '/includes/module.inc';
danielebarchiesi@0 2439 module_load_all(TRUE);
danielebarchiesi@0 2440 }
danielebarchiesi@0 2441
danielebarchiesi@0 2442 /**
danielebarchiesi@0 2443 * Invokes hook_boot(), initializes locking system, and sends HTTP headers.
danielebarchiesi@0 2444 */
danielebarchiesi@0 2445 function _drupal_bootstrap_page_header() {
danielebarchiesi@0 2446 bootstrap_invoke_all('boot');
danielebarchiesi@0 2447
danielebarchiesi@0 2448 if (!drupal_is_cli()) {
danielebarchiesi@0 2449 ob_start();
danielebarchiesi@0 2450 drupal_page_header();
danielebarchiesi@0 2451 }
danielebarchiesi@0 2452 }
danielebarchiesi@0 2453
danielebarchiesi@0 2454 /**
danielebarchiesi@0 2455 * Returns the current bootstrap phase for this Drupal process.
danielebarchiesi@0 2456 *
danielebarchiesi@0 2457 * The current phase is the one most recently completed by drupal_bootstrap().
danielebarchiesi@0 2458 *
danielebarchiesi@0 2459 * @see drupal_bootstrap()
danielebarchiesi@0 2460 */
danielebarchiesi@0 2461 function drupal_get_bootstrap_phase() {
danielebarchiesi@0 2462 return drupal_bootstrap();
danielebarchiesi@0 2463 }
danielebarchiesi@0 2464
danielebarchiesi@0 2465 /**
danielebarchiesi@0 2466 * Returns the test prefix if this is an internal request from SimpleTest.
danielebarchiesi@0 2467 *
danielebarchiesi@0 2468 * @return
danielebarchiesi@0 2469 * Either the simpletest prefix (the string "simpletest" followed by any
danielebarchiesi@0 2470 * number of digits) or FALSE if the user agent does not contain a valid
danielebarchiesi@0 2471 * HMAC and timestamp.
danielebarchiesi@0 2472 */
danielebarchiesi@0 2473 function drupal_valid_test_ua() {
danielebarchiesi@0 2474 // No reason to reset this.
danielebarchiesi@0 2475 static $test_prefix;
danielebarchiesi@0 2476
danielebarchiesi@0 2477 if (isset($test_prefix)) {
danielebarchiesi@0 2478 return $test_prefix;
danielebarchiesi@0 2479 }
danielebarchiesi@0 2480
danielebarchiesi@0 2481 if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/^(simpletest\d+);(.+);(.+);(.+)$/", $_SERVER['HTTP_USER_AGENT'], $matches)) {
danielebarchiesi@0 2482 list(, $prefix, $time, $salt, $hmac) = $matches;
danielebarchiesi@0 2483 $check_string = $prefix . ';' . $time . ';' . $salt;
danielebarchiesi@0 2484 // We use the salt from settings.php to make the HMAC key, since
danielebarchiesi@0 2485 // the database is not yet initialized and we can't access any Drupal variables.
danielebarchiesi@0 2486 // The file properties add more entropy not easily accessible to others.
danielebarchiesi@0 2487 $key = drupal_get_hash_salt() . filectime(__FILE__) . fileinode(__FILE__);
danielebarchiesi@0 2488 $time_diff = REQUEST_TIME - $time;
danielebarchiesi@0 2489 // Since we are making a local request a 5 second time window is allowed,
danielebarchiesi@0 2490 // and the HMAC must match.
danielebarchiesi@0 2491 if ($time_diff >= 0 && $time_diff <= 5 && $hmac == drupal_hmac_base64($check_string, $key)) {
danielebarchiesi@0 2492 $test_prefix = $prefix;
danielebarchiesi@0 2493 return $test_prefix;
danielebarchiesi@0 2494 }
danielebarchiesi@0 2495 }
danielebarchiesi@0 2496
danielebarchiesi@0 2497 $test_prefix = FALSE;
danielebarchiesi@0 2498 return $test_prefix;
danielebarchiesi@0 2499 }
danielebarchiesi@0 2500
danielebarchiesi@0 2501 /**
danielebarchiesi@0 2502 * Generates a user agent string with a HMAC and timestamp for simpletest.
danielebarchiesi@0 2503 */
danielebarchiesi@0 2504 function drupal_generate_test_ua($prefix) {
danielebarchiesi@0 2505 static $key;
danielebarchiesi@0 2506
danielebarchiesi@0 2507 if (!isset($key)) {
danielebarchiesi@0 2508 // We use the salt from settings.php to make the HMAC key, since
danielebarchiesi@0 2509 // the database is not yet initialized and we can't access any Drupal variables.
danielebarchiesi@0 2510 // The file properties add more entropy not easily accessible to others.
danielebarchiesi@0 2511 $key = drupal_get_hash_salt() . filectime(__FILE__) . fileinode(__FILE__);
danielebarchiesi@0 2512 }
danielebarchiesi@0 2513 // Generate a moderately secure HMAC based on the database credentials.
danielebarchiesi@0 2514 $salt = uniqid('', TRUE);
danielebarchiesi@0 2515 $check_string = $prefix . ';' . time() . ';' . $salt;
danielebarchiesi@0 2516 return $check_string . ';' . drupal_hmac_base64($check_string, $key);
danielebarchiesi@0 2517 }
danielebarchiesi@0 2518
danielebarchiesi@0 2519 /**
danielebarchiesi@0 2520 * Enables use of the theme system without requiring database access.
danielebarchiesi@0 2521 *
danielebarchiesi@0 2522 * Loads and initializes the theme system for site installs, updates and when
danielebarchiesi@0 2523 * the site is in maintenance mode. This also applies when the database fails.
danielebarchiesi@0 2524 *
danielebarchiesi@0 2525 * @see _drupal_maintenance_theme()
danielebarchiesi@0 2526 */
danielebarchiesi@0 2527 function drupal_maintenance_theme() {
danielebarchiesi@0 2528 require_once DRUPAL_ROOT . '/includes/theme.maintenance.inc';
danielebarchiesi@0 2529 _drupal_maintenance_theme();
danielebarchiesi@0 2530 }
danielebarchiesi@0 2531
danielebarchiesi@0 2532 /**
danielebarchiesi@0 2533 * Returns a simple 404 Not Found page.
danielebarchiesi@0 2534 *
danielebarchiesi@0 2535 * If fast 404 pages are enabled, and this is a matching page then print a
danielebarchiesi@0 2536 * simple 404 page and exit.
danielebarchiesi@0 2537 *
danielebarchiesi@0 2538 * This function is called from drupal_deliver_html_page() at the time when a
danielebarchiesi@0 2539 * a normal 404 page is generated, but it can also optionally be called directly
danielebarchiesi@0 2540 * from settings.php to prevent a Drupal bootstrap on these pages. See
danielebarchiesi@0 2541 * documentation in settings.php for the benefits and drawbacks of using this.
danielebarchiesi@0 2542 *
danielebarchiesi@0 2543 * Paths to dynamically-generated content, such as image styles, should also be
danielebarchiesi@0 2544 * accounted for in this function.
danielebarchiesi@0 2545 */
danielebarchiesi@0 2546 function drupal_fast_404() {
danielebarchiesi@0 2547 $exclude_paths = variable_get('404_fast_paths_exclude', FALSE);
danielebarchiesi@0 2548 if ($exclude_paths && !preg_match($exclude_paths, $_GET['q'])) {
danielebarchiesi@0 2549 $fast_paths = variable_get('404_fast_paths', FALSE);
danielebarchiesi@0 2550 if ($fast_paths && preg_match($fast_paths, $_GET['q'])) {
danielebarchiesi@0 2551 drupal_add_http_header('Status', '404 Not Found');
danielebarchiesi@0 2552 $fast_404_html = variable_get('404_fast_html', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server.</p></body></html>');
danielebarchiesi@0 2553 // Replace @path in the variable with the page path.
danielebarchiesi@0 2554 print strtr($fast_404_html, array('@path' => check_plain(request_uri())));
danielebarchiesi@0 2555 exit;
danielebarchiesi@0 2556 }
danielebarchiesi@0 2557 }
danielebarchiesi@0 2558 }
danielebarchiesi@0 2559
danielebarchiesi@0 2560 /**
danielebarchiesi@0 2561 * Returns TRUE if a Drupal installation is currently being attempted.
danielebarchiesi@0 2562 */
danielebarchiesi@0 2563 function drupal_installation_attempted() {
danielebarchiesi@0 2564 return defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'install';
danielebarchiesi@0 2565 }
danielebarchiesi@0 2566
danielebarchiesi@0 2567 /**
danielebarchiesi@0 2568 * Returns the name of the proper localization function.
danielebarchiesi@0 2569 *
danielebarchiesi@0 2570 * get_t() exists to support localization for code that might run during
danielebarchiesi@0 2571 * the installation phase, when some elements of the system might not have
danielebarchiesi@0 2572 * loaded.
danielebarchiesi@0 2573 *
danielebarchiesi@0 2574 * This would include implementations of hook_install(), which could run
danielebarchiesi@0 2575 * during the Drupal installation phase, and might also be run during
danielebarchiesi@0 2576 * non-installation time, such as while installing the module from the the
danielebarchiesi@0 2577 * module administration page.
danielebarchiesi@0 2578 *
danielebarchiesi@0 2579 * Example usage:
danielebarchiesi@0 2580 * @code
danielebarchiesi@0 2581 * $t = get_t();
danielebarchiesi@0 2582 * $translated = $t('translate this');
danielebarchiesi@0 2583 * @endcode
danielebarchiesi@0 2584 *
danielebarchiesi@0 2585 * Use t() if your code will never run during the Drupal installation phase.
danielebarchiesi@0 2586 * Use st() if your code will only run during installation and never any other
danielebarchiesi@0 2587 * time. Use get_t() if your code could run in either circumstance.
danielebarchiesi@0 2588 *
danielebarchiesi@0 2589 * @see t()
danielebarchiesi@0 2590 * @see st()
danielebarchiesi@0 2591 * @ingroup sanitization
danielebarchiesi@0 2592 */
danielebarchiesi@0 2593 function get_t() {
danielebarchiesi@0 2594 static $t;
danielebarchiesi@0 2595 // This is not converted to drupal_static because there is no point in
danielebarchiesi@0 2596 // resetting this as it can not change in the course of a request.
danielebarchiesi@0 2597 if (!isset($t)) {
danielebarchiesi@0 2598 $t = drupal_installation_attempted() ? 'st' : 't';
danielebarchiesi@0 2599 }
danielebarchiesi@0 2600 return $t;
danielebarchiesi@0 2601 }
danielebarchiesi@0 2602
danielebarchiesi@0 2603 /**
danielebarchiesi@0 2604 * Initializes all the defined language types.
danielebarchiesi@0 2605 */
danielebarchiesi@0 2606 function drupal_language_initialize() {
danielebarchiesi@0 2607 $types = language_types();
danielebarchiesi@0 2608
danielebarchiesi@0 2609 // Ensure the language is correctly returned, even without multilanguage
danielebarchiesi@0 2610 // support. Also make sure we have a $language fallback, in case a language
danielebarchiesi@0 2611 // negotiation callback needs to do a full bootstrap.
danielebarchiesi@0 2612 // Useful for eg. XML/HTML 'lang' attributes.
danielebarchiesi@0 2613 $default = language_default();
danielebarchiesi@0 2614 foreach ($types as $type) {
danielebarchiesi@0 2615 $GLOBALS[$type] = $default;
danielebarchiesi@0 2616 }
danielebarchiesi@0 2617 if (drupal_multilingual()) {
danielebarchiesi@0 2618 include_once DRUPAL_ROOT . '/includes/language.inc';
danielebarchiesi@0 2619 foreach ($types as $type) {
danielebarchiesi@0 2620 $GLOBALS[$type] = language_initialize($type);
danielebarchiesi@0 2621 }
danielebarchiesi@0 2622 // Allow modules to react on language system initialization in multilingual
danielebarchiesi@0 2623 // environments.
danielebarchiesi@0 2624 bootstrap_invoke_all('language_init');
danielebarchiesi@0 2625 }
danielebarchiesi@0 2626 }
danielebarchiesi@0 2627
danielebarchiesi@0 2628 /**
danielebarchiesi@0 2629 * Returns a list of the built-in language types.
danielebarchiesi@0 2630 *
danielebarchiesi@0 2631 * @return
danielebarchiesi@0 2632 * An array of key-values pairs where the key is the language type and the
danielebarchiesi@0 2633 * value is its configurability.
danielebarchiesi@0 2634 */
danielebarchiesi@0 2635 function drupal_language_types() {
danielebarchiesi@0 2636 return array(
danielebarchiesi@0 2637 LANGUAGE_TYPE_INTERFACE => TRUE,
danielebarchiesi@0 2638 LANGUAGE_TYPE_CONTENT => FALSE,
danielebarchiesi@0 2639 LANGUAGE_TYPE_URL => FALSE,
danielebarchiesi@0 2640 );
danielebarchiesi@0 2641 }
danielebarchiesi@0 2642
danielebarchiesi@0 2643 /**
danielebarchiesi@0 2644 * Returns TRUE if there is more than one language enabled.
danielebarchiesi@0 2645 *
danielebarchiesi@0 2646 * @return
danielebarchiesi@0 2647 * TRUE if more than one language is enabled.
danielebarchiesi@0 2648 */
danielebarchiesi@0 2649 function drupal_multilingual() {
danielebarchiesi@0 2650 // The "language_count" variable stores the number of enabled languages to
danielebarchiesi@0 2651 // avoid unnecessarily querying the database when building the list of
danielebarchiesi@0 2652 // enabled languages on monolingual sites.
danielebarchiesi@0 2653 return variable_get('language_count', 1) > 1;
danielebarchiesi@0 2654 }
danielebarchiesi@0 2655
danielebarchiesi@0 2656 /**
danielebarchiesi@0 2657 * Returns an array of the available language types.
danielebarchiesi@0 2658 *
danielebarchiesi@0 2659 * @return
danielebarchiesi@0 2660 * An array of all language types where the keys of each are the language type
danielebarchiesi@0 2661 * name and its value is its configurability (TRUE/FALSE).
danielebarchiesi@0 2662 */
danielebarchiesi@0 2663 function language_types() {
danielebarchiesi@0 2664 return array_keys(variable_get('language_types', drupal_language_types()));
danielebarchiesi@0 2665 }
danielebarchiesi@0 2666
danielebarchiesi@0 2667 /**
danielebarchiesi@0 2668 * Returns a list of installed languages, indexed by the specified key.
danielebarchiesi@0 2669 *
danielebarchiesi@0 2670 * @param $field
danielebarchiesi@0 2671 * (optional) The field to index the list with.
danielebarchiesi@0 2672 *
danielebarchiesi@0 2673 * @return
danielebarchiesi@0 2674 * An associative array, keyed on the values of $field.
danielebarchiesi@0 2675 * - If $field is 'weight' or 'enabled', the array is nested, with the outer
danielebarchiesi@0 2676 * array's values each being associative arrays with language codes as
danielebarchiesi@0 2677 * keys and language objects as values.
danielebarchiesi@0 2678 * - For all other values of $field, the array is only one level deep, and
danielebarchiesi@0 2679 * the array's values are language objects.
danielebarchiesi@0 2680 */
danielebarchiesi@0 2681 function language_list($field = 'language') {
danielebarchiesi@0 2682 $languages = &drupal_static(__FUNCTION__);
danielebarchiesi@0 2683 // Init language list
danielebarchiesi@0 2684 if (!isset($languages)) {
danielebarchiesi@0 2685 if (drupal_multilingual() || module_exists('locale')) {
danielebarchiesi@0 2686 $languages['language'] = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC')->fetchAllAssoc('language');
danielebarchiesi@0 2687 // Users cannot uninstall the native English language. However, we allow
danielebarchiesi@0 2688 // it to be hidden from the installed languages. Therefore, at least one
danielebarchiesi@0 2689 // other language must be enabled then.
danielebarchiesi@0 2690 if (!$languages['language']['en']->enabled && !variable_get('language_native_enabled', TRUE)) {
danielebarchiesi@0 2691 unset($languages['language']['en']);
danielebarchiesi@0 2692 }
danielebarchiesi@0 2693 }
danielebarchiesi@0 2694 else {
danielebarchiesi@0 2695 // No locale module, so use the default language only.
danielebarchiesi@0 2696 $default = language_default();
danielebarchiesi@0 2697 $languages['language'][$default->language] = $default;
danielebarchiesi@0 2698 }
danielebarchiesi@0 2699 }
danielebarchiesi@0 2700
danielebarchiesi@0 2701 // Return the array indexed by the right field
danielebarchiesi@0 2702 if (!isset($languages[$field])) {
danielebarchiesi@0 2703 $languages[$field] = array();
danielebarchiesi@0 2704 foreach ($languages['language'] as $lang) {
danielebarchiesi@0 2705 // Some values should be collected into an array
danielebarchiesi@0 2706 if (in_array($field, array('enabled', 'weight'))) {
danielebarchiesi@0 2707 $languages[$field][$lang->$field][$lang->language] = $lang;
danielebarchiesi@0 2708 }
danielebarchiesi@0 2709 else {
danielebarchiesi@0 2710 $languages[$field][$lang->$field] = $lang;
danielebarchiesi@0 2711 }
danielebarchiesi@0 2712 }
danielebarchiesi@0 2713 }
danielebarchiesi@0 2714 return $languages[$field];
danielebarchiesi@0 2715 }
danielebarchiesi@0 2716
danielebarchiesi@0 2717 /**
danielebarchiesi@0 2718 * Returns the default language used on the site
danielebarchiesi@0 2719 *
danielebarchiesi@0 2720 * @param $property
danielebarchiesi@0 2721 * Optional property of the language object to return
danielebarchiesi@0 2722 */
danielebarchiesi@0 2723 function language_default($property = NULL) {
danielebarchiesi@0 2724 $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' => ''));
danielebarchiesi@0 2725 return $property ? $language->$property : $language;
danielebarchiesi@0 2726 }
danielebarchiesi@0 2727
danielebarchiesi@0 2728 /**
danielebarchiesi@0 2729 * Returns the requested URL path of the page being viewed.
danielebarchiesi@0 2730 *
danielebarchiesi@0 2731 * Examples:
danielebarchiesi@0 2732 * - http://example.com/node/306 returns "node/306".
danielebarchiesi@0 2733 * - http://example.com/drupalfolder/node/306 returns "node/306" while
danielebarchiesi@0 2734 * base_path() returns "/drupalfolder/".
danielebarchiesi@0 2735 * - http://example.com/path/alias (which is a path alias for node/306) returns
danielebarchiesi@0 2736 * "path/alias" as opposed to the internal path.
danielebarchiesi@0 2737 * - http://example.com/index.php returns an empty string (meaning: front page).
danielebarchiesi@0 2738 * - http://example.com/index.php?page=1 returns an empty string.
danielebarchiesi@0 2739 *
danielebarchiesi@0 2740 * @return
danielebarchiesi@0 2741 * The requested Drupal URL path.
danielebarchiesi@0 2742 *
danielebarchiesi@0 2743 * @see current_path()
danielebarchiesi@0 2744 */
danielebarchiesi@0 2745 function request_path() {
danielebarchiesi@0 2746 static $path;
danielebarchiesi@0 2747
danielebarchiesi@0 2748 if (isset($path)) {
danielebarchiesi@0 2749 return $path;
danielebarchiesi@0 2750 }
danielebarchiesi@0 2751
danielebarchiesi@0 2752 if (isset($_GET['q']) && is_string($_GET['q'])) {
danielebarchiesi@0 2753 // This is a request with a ?q=foo/bar query string. $_GET['q'] is
danielebarchiesi@0 2754 // overwritten in drupal_path_initialize(), but request_path() is called
danielebarchiesi@0 2755 // very early in the bootstrap process, so the original value is saved in
danielebarchiesi@0 2756 // $path and returned in later calls.
danielebarchiesi@0 2757 $path = $_GET['q'];
danielebarchiesi@0 2758 }
danielebarchiesi@0 2759 elseif (isset($_SERVER['REQUEST_URI'])) {
danielebarchiesi@0 2760 // This request is either a clean URL, or 'index.php', or nonsense.
danielebarchiesi@0 2761 // Extract the path from REQUEST_URI.
danielebarchiesi@0 2762 $request_path = strtok($_SERVER['REQUEST_URI'], '?');
danielebarchiesi@0 2763 $base_path_len = strlen(rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/'));
danielebarchiesi@0 2764 // Unescape and strip $base_path prefix, leaving q without a leading slash.
danielebarchiesi@0 2765 $path = substr(urldecode($request_path), $base_path_len + 1);
danielebarchiesi@0 2766 // If the path equals the script filename, either because 'index.php' was
danielebarchiesi@0 2767 // explicitly provided in the URL, or because the server added it to
danielebarchiesi@0 2768 // $_SERVER['REQUEST_URI'] even when it wasn't provided in the URL (some
danielebarchiesi@0 2769 // versions of Microsoft IIS do this), the front page should be served.
danielebarchiesi@0 2770 if ($path == basename($_SERVER['PHP_SELF'])) {
danielebarchiesi@0 2771 $path = '';
danielebarchiesi@0 2772 }
danielebarchiesi@0 2773 }
danielebarchiesi@0 2774 else {
danielebarchiesi@0 2775 // This is the front page.
danielebarchiesi@0 2776 $path = '';
danielebarchiesi@0 2777 }
danielebarchiesi@0 2778
danielebarchiesi@0 2779 // Under certain conditions Apache's RewriteRule directive prepends the value
danielebarchiesi@0 2780 // assigned to $_GET['q'] with a slash. Moreover we can always have a trailing
danielebarchiesi@0 2781 // slash in place, hence we need to normalize $_GET['q'].
danielebarchiesi@0 2782 $path = trim($path, '/');
danielebarchiesi@0 2783
danielebarchiesi@0 2784 return $path;
danielebarchiesi@0 2785 }
danielebarchiesi@0 2786
danielebarchiesi@0 2787 /**
danielebarchiesi@0 2788 * Returns a component of the current Drupal path.
danielebarchiesi@0 2789 *
danielebarchiesi@0 2790 * When viewing a page at the path "admin/structure/types", for example, arg(0)
danielebarchiesi@0 2791 * returns "admin", arg(1) returns "structure", and arg(2) returns "types".
danielebarchiesi@0 2792 *
danielebarchiesi@0 2793 * Avoid use of this function where possible, as resulting code is hard to
danielebarchiesi@0 2794 * read. In menu callback functions, attempt to use named arguments. See the
danielebarchiesi@0 2795 * explanation in menu.inc for how to construct callbacks that take arguments.
danielebarchiesi@0 2796 * When attempting to use this function to load an element from the current
danielebarchiesi@0 2797 * path, e.g. loading the node on a node page, use menu_get_object() instead.
danielebarchiesi@0 2798 *
danielebarchiesi@0 2799 * @param $index
danielebarchiesi@0 2800 * The index of the component, where each component is separated by a '/'
danielebarchiesi@0 2801 * (forward-slash), and where the first component has an index of 0 (zero).
danielebarchiesi@0 2802 * @param $path
danielebarchiesi@0 2803 * A path to break into components. Defaults to the path of the current page.
danielebarchiesi@0 2804 *
danielebarchiesi@0 2805 * @return
danielebarchiesi@0 2806 * The component specified by $index, or NULL if the specified component was
danielebarchiesi@0 2807 * not found. If called without arguments, it returns an array containing all
danielebarchiesi@0 2808 * the components of the current path.
danielebarchiesi@0 2809 */
danielebarchiesi@0 2810 function arg($index = NULL, $path = NULL) {
danielebarchiesi@0 2811 // Even though $arguments doesn't need to be resettable for any functional
danielebarchiesi@0 2812 // reasons (the result of explode() does not depend on any run-time
danielebarchiesi@0 2813 // information), it should be resettable anyway in case a module needs to
danielebarchiesi@0 2814 // free up the memory used by it.
danielebarchiesi@0 2815 // Use the advanced drupal_static() pattern, since this is called very often.
danielebarchiesi@0 2816 static $drupal_static_fast;
danielebarchiesi@0 2817 if (!isset($drupal_static_fast)) {
danielebarchiesi@0 2818 $drupal_static_fast['arguments'] = &drupal_static(__FUNCTION__);
danielebarchiesi@0 2819 }
danielebarchiesi@0 2820 $arguments = &$drupal_static_fast['arguments'];
danielebarchiesi@0 2821
danielebarchiesi@0 2822 if (!isset($path)) {
danielebarchiesi@0 2823 $path = $_GET['q'];
danielebarchiesi@0 2824 }
danielebarchiesi@0 2825 if (!isset($arguments[$path])) {
danielebarchiesi@0 2826 $arguments[$path] = explode('/', $path);
danielebarchiesi@0 2827 }
danielebarchiesi@0 2828 if (!isset($index)) {
danielebarchiesi@0 2829 return $arguments[$path];
danielebarchiesi@0 2830 }
danielebarchiesi@0 2831 if (isset($arguments[$path][$index])) {
danielebarchiesi@0 2832 return $arguments[$path][$index];
danielebarchiesi@0 2833 }
danielebarchiesi@0 2834 }
danielebarchiesi@0 2835
danielebarchiesi@0 2836 /**
danielebarchiesi@0 2837 * Returns the IP address of the client machine.
danielebarchiesi@0 2838 *
danielebarchiesi@0 2839 * If Drupal is behind a reverse proxy, we use the X-Forwarded-For header
danielebarchiesi@0 2840 * instead of $_SERVER['REMOTE_ADDR'], which would be the IP address of
danielebarchiesi@0 2841 * the proxy server, and not the client's. The actual header name can be
danielebarchiesi@0 2842 * configured by the reverse_proxy_header variable.
danielebarchiesi@0 2843 *
danielebarchiesi@0 2844 * @return
danielebarchiesi@0 2845 * IP address of client machine, adjusted for reverse proxy and/or cluster
danielebarchiesi@0 2846 * environments.
danielebarchiesi@0 2847 */
danielebarchiesi@0 2848 function ip_address() {
danielebarchiesi@0 2849 $ip_address = &drupal_static(__FUNCTION__);
danielebarchiesi@0 2850
danielebarchiesi@0 2851 if (!isset($ip_address)) {
danielebarchiesi@0 2852 $ip_address = $_SERVER['REMOTE_ADDR'];
danielebarchiesi@0 2853
danielebarchiesi@0 2854 if (variable_get('reverse_proxy', 0)) {
danielebarchiesi@0 2855 $reverse_proxy_header = variable_get('reverse_proxy_header', 'HTTP_X_FORWARDED_FOR');
danielebarchiesi@0 2856 if (!empty($_SERVER[$reverse_proxy_header])) {
danielebarchiesi@0 2857 // If an array of known reverse proxy IPs is provided, then trust
danielebarchiesi@0 2858 // the XFF header if request really comes from one of them.
danielebarchiesi@0 2859 $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
danielebarchiesi@0 2860
danielebarchiesi@0 2861 // Turn XFF header into an array.
danielebarchiesi@0 2862 $forwarded = explode(',', $_SERVER[$reverse_proxy_header]);
danielebarchiesi@0 2863
danielebarchiesi@0 2864 // Trim the forwarded IPs; they may have been delimited by commas and spaces.
danielebarchiesi@0 2865 $forwarded = array_map('trim', $forwarded);
danielebarchiesi@0 2866
danielebarchiesi@0 2867 // Tack direct client IP onto end of forwarded array.
danielebarchiesi@0 2868 $forwarded[] = $ip_address;
danielebarchiesi@0 2869
danielebarchiesi@0 2870 // Eliminate all trusted IPs.
danielebarchiesi@0 2871 $untrusted = array_diff($forwarded, $reverse_proxy_addresses);
danielebarchiesi@0 2872
danielebarchiesi@0 2873 // The right-most IP is the most specific we can trust.
danielebarchiesi@0 2874 $ip_address = array_pop($untrusted);
danielebarchiesi@0 2875 }
danielebarchiesi@0 2876 }
danielebarchiesi@0 2877 }
danielebarchiesi@0 2878
danielebarchiesi@0 2879 return $ip_address;
danielebarchiesi@0 2880 }
danielebarchiesi@0 2881
danielebarchiesi@0 2882 /**
danielebarchiesi@0 2883 * @addtogroup schemaapi
danielebarchiesi@0 2884 * @{
danielebarchiesi@0 2885 */
danielebarchiesi@0 2886
danielebarchiesi@0 2887 /**
danielebarchiesi@0 2888 * Gets the schema definition of a table, or the whole database schema.
danielebarchiesi@0 2889 *
danielebarchiesi@0 2890 * The returned schema will include any modifications made by any
danielebarchiesi@0 2891 * module that implements hook_schema_alter().
danielebarchiesi@0 2892 *
danielebarchiesi@0 2893 * @param $table
danielebarchiesi@0 2894 * The name of the table. If not given, the schema of all tables is returned.
danielebarchiesi@0 2895 * @param $rebuild
danielebarchiesi@0 2896 * If true, the schema will be rebuilt instead of retrieved from the cache.
danielebarchiesi@0 2897 */
danielebarchiesi@0 2898 function drupal_get_schema($table = NULL, $rebuild = FALSE) {
danielebarchiesi@0 2899 static $schema;
danielebarchiesi@0 2900
danielebarchiesi@0 2901 if ($rebuild || !isset($table)) {
danielebarchiesi@0 2902 $schema = drupal_get_complete_schema($rebuild);
danielebarchiesi@0 2903 }
danielebarchiesi@0 2904 elseif (!isset($schema)) {
danielebarchiesi@0 2905 $schema = new SchemaCache();
danielebarchiesi@0 2906 }
danielebarchiesi@0 2907
danielebarchiesi@0 2908 if (!isset($table)) {
danielebarchiesi@0 2909 return $schema;
danielebarchiesi@0 2910 }
danielebarchiesi@0 2911 if (isset($schema[$table])) {
danielebarchiesi@0 2912 return $schema[$table];
danielebarchiesi@0 2913 }
danielebarchiesi@0 2914 else {
danielebarchiesi@0 2915 return FALSE;
danielebarchiesi@0 2916 }
danielebarchiesi@0 2917 }
danielebarchiesi@0 2918
danielebarchiesi@0 2919 /**
danielebarchiesi@0 2920 * Extends DrupalCacheArray to allow for dynamic building of the schema cache.
danielebarchiesi@0 2921 */
danielebarchiesi@0 2922 class SchemaCache extends DrupalCacheArray {
danielebarchiesi@0 2923
danielebarchiesi@0 2924 /**
danielebarchiesi@0 2925 * Constructs a SchemaCache object.
danielebarchiesi@0 2926 */
danielebarchiesi@0 2927 public function __construct() {
danielebarchiesi@0 2928 // Cache by request method.
danielebarchiesi@0 2929 parent::__construct('schema:runtime:' . ($_SERVER['REQUEST_METHOD'] == 'GET'), 'cache');
danielebarchiesi@0 2930 }
danielebarchiesi@0 2931
danielebarchiesi@0 2932 /**
danielebarchiesi@0 2933 * Overrides DrupalCacheArray::resolveCacheMiss().
danielebarchiesi@0 2934 */
danielebarchiesi@0 2935 protected function resolveCacheMiss($offset) {
danielebarchiesi@0 2936 $complete_schema = drupal_get_complete_schema();
danielebarchiesi@0 2937 $value = isset($complete_schema[$offset]) ? $complete_schema[$offset] : NULL;
danielebarchiesi@0 2938 $this->storage[$offset] = $value;
danielebarchiesi@0 2939 $this->persist($offset);
danielebarchiesi@0 2940 return $value;
danielebarchiesi@0 2941 }
danielebarchiesi@0 2942 }
danielebarchiesi@0 2943
danielebarchiesi@0 2944 /**
danielebarchiesi@0 2945 * Gets the whole database schema.
danielebarchiesi@0 2946 *
danielebarchiesi@0 2947 * The returned schema will include any modifications made by any
danielebarchiesi@0 2948 * module that implements hook_schema_alter().
danielebarchiesi@0 2949 *
danielebarchiesi@0 2950 * @param $rebuild
danielebarchiesi@0 2951 * If true, the schema will be rebuilt instead of retrieved from the cache.
danielebarchiesi@0 2952 */
danielebarchiesi@0 2953 function drupal_get_complete_schema($rebuild = FALSE) {
danielebarchiesi@0 2954 static $schema = array();
danielebarchiesi@0 2955
danielebarchiesi@0 2956 if (empty($schema) || $rebuild) {
danielebarchiesi@0 2957 // Try to load the schema from cache.
danielebarchiesi@0 2958 if (!$rebuild && $cached = cache_get('schema')) {
danielebarchiesi@0 2959 $schema = $cached->data;
danielebarchiesi@0 2960 }
danielebarchiesi@0 2961 // Otherwise, rebuild the schema cache.
danielebarchiesi@0 2962 else {
danielebarchiesi@0 2963 $schema = array();
danielebarchiesi@0 2964 // Load the .install files to get hook_schema.
danielebarchiesi@0 2965 // On some databases this function may be called before bootstrap has
danielebarchiesi@0 2966 // been completed, so we force the functions we need to load just in case.
danielebarchiesi@0 2967 if (function_exists('module_load_all_includes')) {
danielebarchiesi@0 2968 // This function can be called very early in the bootstrap process, so
danielebarchiesi@0 2969 // we force the module_list() cache to be refreshed to ensure that it
danielebarchiesi@0 2970 // contains the complete list of modules before we go on to call
danielebarchiesi@0 2971 // module_load_all_includes().
danielebarchiesi@0 2972 module_list(TRUE);
danielebarchiesi@0 2973 module_load_all_includes('install');
danielebarchiesi@0 2974 }
danielebarchiesi@0 2975
danielebarchiesi@0 2976 require_once DRUPAL_ROOT . '/includes/common.inc';
danielebarchiesi@0 2977 // Invoke hook_schema for all modules.
danielebarchiesi@0 2978 foreach (module_implements('schema') as $module) {
danielebarchiesi@0 2979 // Cast the result of hook_schema() to an array, as a NULL return value
danielebarchiesi@0 2980 // would cause array_merge() to set the $schema variable to NULL as well.
danielebarchiesi@0 2981 // That would break modules which use $schema further down the line.
danielebarchiesi@0 2982 $current = (array) module_invoke($module, 'schema');
danielebarchiesi@0 2983 // Set 'module' and 'name' keys for each table, and remove descriptions,
danielebarchiesi@0 2984 // as they needlessly slow down cache_get() for every single request.
danielebarchiesi@0 2985 _drupal_schema_initialize($current, $module);
danielebarchiesi@0 2986 $schema = array_merge($schema, $current);
danielebarchiesi@0 2987 }
danielebarchiesi@0 2988
danielebarchiesi@0 2989 drupal_alter('schema', $schema);
danielebarchiesi@0 2990 // If the schema is empty, avoid saving it: some database engines require
danielebarchiesi@0 2991 // the schema to perform queries, and this could lead to infinite loops.
danielebarchiesi@0 2992 if (!empty($schema) && (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL)) {
danielebarchiesi@0 2993 cache_set('schema', $schema);
danielebarchiesi@0 2994 }
danielebarchiesi@0 2995 if ($rebuild) {
danielebarchiesi@0 2996 cache_clear_all('schema:', 'cache', TRUE);
danielebarchiesi@0 2997 }
danielebarchiesi@0 2998 }
danielebarchiesi@0 2999 }
danielebarchiesi@0 3000
danielebarchiesi@0 3001 return $schema;
danielebarchiesi@0 3002 }
danielebarchiesi@0 3003
danielebarchiesi@0 3004 /**
danielebarchiesi@0 3005 * @} End of "addtogroup schemaapi".
danielebarchiesi@0 3006 */
danielebarchiesi@0 3007
danielebarchiesi@0 3008
danielebarchiesi@0 3009 /**
danielebarchiesi@0 3010 * @addtogroup registry
danielebarchiesi@0 3011 * @{
danielebarchiesi@0 3012 */
danielebarchiesi@0 3013
danielebarchiesi@0 3014 /**
danielebarchiesi@0 3015 * Confirms that an interface is available.
danielebarchiesi@0 3016 *
danielebarchiesi@0 3017 * This function is rarely called directly. Instead, it is registered as an
danielebarchiesi@0 3018 * spl_autoload() handler, and PHP calls it for us when necessary.
danielebarchiesi@0 3019 *
danielebarchiesi@0 3020 * @param $interface
danielebarchiesi@0 3021 * The name of the interface to check or load.
danielebarchiesi@0 3022 *
danielebarchiesi@0 3023 * @return
danielebarchiesi@0 3024 * TRUE if the interface is currently available, FALSE otherwise.
danielebarchiesi@0 3025 */
danielebarchiesi@0 3026 function drupal_autoload_interface($interface) {
danielebarchiesi@0 3027 return _registry_check_code('interface', $interface);
danielebarchiesi@0 3028 }
danielebarchiesi@0 3029
danielebarchiesi@0 3030 /**
danielebarchiesi@0 3031 * Confirms that a class is available.
danielebarchiesi@0 3032 *
danielebarchiesi@0 3033 * This function is rarely called directly. Instead, it is registered as an
danielebarchiesi@0 3034 * spl_autoload() handler, and PHP calls it for us when necessary.
danielebarchiesi@0 3035 *
danielebarchiesi@0 3036 * @param $class
danielebarchiesi@0 3037 * The name of the class to check or load.
danielebarchiesi@0 3038 *
danielebarchiesi@0 3039 * @return
danielebarchiesi@0 3040 * TRUE if the class is currently available, FALSE otherwise.
danielebarchiesi@0 3041 */
danielebarchiesi@0 3042 function drupal_autoload_class($class) {
danielebarchiesi@0 3043 return _registry_check_code('class', $class);
danielebarchiesi@0 3044 }
danielebarchiesi@0 3045
danielebarchiesi@0 3046 /**
danielebarchiesi@0 3047 * Checks for a resource in the registry.
danielebarchiesi@0 3048 *
danielebarchiesi@0 3049 * @param $type
danielebarchiesi@0 3050 * The type of resource we are looking up, or one of the constants
danielebarchiesi@0 3051 * REGISTRY_RESET_LOOKUP_CACHE or REGISTRY_WRITE_LOOKUP_CACHE, which
danielebarchiesi@0 3052 * signal that we should reset or write the cache, respectively.
danielebarchiesi@0 3053 * @param $name
danielebarchiesi@0 3054 * The name of the resource, or NULL if either of the REGISTRY_* constants
danielebarchiesi@0 3055 * is passed in.
danielebarchiesi@0 3056 *
danielebarchiesi@0 3057 * @return
danielebarchiesi@0 3058 * TRUE if the resource was found, FALSE if not.
danielebarchiesi@0 3059 * NULL if either of the REGISTRY_* constants is passed in as $type.
danielebarchiesi@0 3060 */
danielebarchiesi@0 3061 function _registry_check_code($type, $name = NULL) {
danielebarchiesi@0 3062 static $lookup_cache, $cache_update_needed;
danielebarchiesi@0 3063
danielebarchiesi@0 3064 if ($type == 'class' && class_exists($name) || $type == 'interface' && interface_exists($name)) {
danielebarchiesi@0 3065 return TRUE;
danielebarchiesi@0 3066 }
danielebarchiesi@0 3067
danielebarchiesi@0 3068 if (!isset($lookup_cache)) {
danielebarchiesi@0 3069 $lookup_cache = array();
danielebarchiesi@0 3070 if ($cache = cache_get('lookup_cache', 'cache_bootstrap')) {
danielebarchiesi@0 3071 $lookup_cache = $cache->data;
danielebarchiesi@0 3072 }
danielebarchiesi@0 3073 }
danielebarchiesi@0 3074
danielebarchiesi@0 3075 // When we rebuild the registry, we need to reset this cache so
danielebarchiesi@0 3076 // we don't keep lookups for resources that changed during the rebuild.
danielebarchiesi@0 3077 if ($type == REGISTRY_RESET_LOOKUP_CACHE) {
danielebarchiesi@0 3078 $cache_update_needed = TRUE;
danielebarchiesi@0 3079 $lookup_cache = NULL;
danielebarchiesi@0 3080 return;
danielebarchiesi@0 3081 }
danielebarchiesi@0 3082
danielebarchiesi@0 3083 // Called from drupal_page_footer, we write to permanent storage if there
danielebarchiesi@0 3084 // changes to the lookup cache for this request.
danielebarchiesi@0 3085 if ($type == REGISTRY_WRITE_LOOKUP_CACHE) {
danielebarchiesi@0 3086 if ($cache_update_needed) {
danielebarchiesi@0 3087 cache_set('lookup_cache', $lookup_cache, 'cache_bootstrap');
danielebarchiesi@0 3088 }
danielebarchiesi@0 3089 return;
danielebarchiesi@0 3090 }
danielebarchiesi@0 3091
danielebarchiesi@0 3092 // $type is either 'interface' or 'class', so we only need the first letter to
danielebarchiesi@0 3093 // keep the cache key unique.
danielebarchiesi@0 3094 $cache_key = $type[0] . $name;
danielebarchiesi@0 3095 if (isset($lookup_cache[$cache_key])) {
danielebarchiesi@0 3096 if ($lookup_cache[$cache_key]) {
danielebarchiesi@0 3097 require_once DRUPAL_ROOT . '/' . $lookup_cache[$cache_key];
danielebarchiesi@0 3098 }
danielebarchiesi@0 3099 return (bool) $lookup_cache[$cache_key];
danielebarchiesi@0 3100 }
danielebarchiesi@0 3101
danielebarchiesi@0 3102 // This function may get called when the default database is not active, but
danielebarchiesi@0 3103 // there is no reason we'd ever want to not use the default database for
danielebarchiesi@0 3104 // this query.
danielebarchiesi@0 3105 $file = Database::getConnection('default', 'default')->query("SELECT filename FROM {registry} WHERE name = :name AND type = :type", array(
danielebarchiesi@0 3106 ':name' => $name,
danielebarchiesi@0 3107 ':type' => $type,
danielebarchiesi@0 3108 ))
danielebarchiesi@0 3109 ->fetchField();
danielebarchiesi@0 3110
danielebarchiesi@0 3111 // Flag that we've run a lookup query and need to update the cache.
danielebarchiesi@0 3112 $cache_update_needed = TRUE;
danielebarchiesi@0 3113
danielebarchiesi@0 3114 // Misses are valuable information worth caching, so cache even if
danielebarchiesi@0 3115 // $file is FALSE.
danielebarchiesi@0 3116 $lookup_cache[$cache_key] = $file;
danielebarchiesi@0 3117
danielebarchiesi@0 3118 if ($file) {
danielebarchiesi@0 3119 require_once DRUPAL_ROOT . '/' . $file;
danielebarchiesi@0 3120 return TRUE;
danielebarchiesi@0 3121 }
danielebarchiesi@0 3122 else {
danielebarchiesi@0 3123 return FALSE;
danielebarchiesi@0 3124 }
danielebarchiesi@0 3125 }
danielebarchiesi@0 3126
danielebarchiesi@0 3127 /**
danielebarchiesi@0 3128 * Rescans all enabled modules and rebuilds the registry.
danielebarchiesi@0 3129 *
danielebarchiesi@0 3130 * Rescans all code in modules or includes directories, storing the location of
danielebarchiesi@0 3131 * each interface or class in the database.
danielebarchiesi@0 3132 */
danielebarchiesi@0 3133 function registry_rebuild() {
danielebarchiesi@0 3134 system_rebuild_module_data();
danielebarchiesi@0 3135 registry_update();
danielebarchiesi@0 3136 }
danielebarchiesi@0 3137
danielebarchiesi@0 3138 /**
danielebarchiesi@0 3139 * Updates the registry based on the latest files listed in the database.
danielebarchiesi@0 3140 *
danielebarchiesi@0 3141 * This function should be used when system_rebuild_module_data() does not need
danielebarchiesi@0 3142 * to be called, because it is already known that the list of files in the
danielebarchiesi@0 3143 * {system} table matches those in the file system.
danielebarchiesi@0 3144 *
danielebarchiesi@0 3145 * @return
danielebarchiesi@0 3146 * TRUE if the registry was rebuilt, FALSE if another thread was rebuilding
danielebarchiesi@0 3147 * in parallel and the current thread just waited for completion.
danielebarchiesi@0 3148 *
danielebarchiesi@0 3149 * @see registry_rebuild()
danielebarchiesi@0 3150 */
danielebarchiesi@0 3151 function registry_update() {
danielebarchiesi@0 3152 // install_system_module() calls module_enable() which calls into this
danielebarchiesi@0 3153 // function during initial system installation, so the lock system is neither
danielebarchiesi@0 3154 // loaded nor does its storage exist yet.
danielebarchiesi@0 3155 $in_installer = drupal_installation_attempted();
danielebarchiesi@0 3156 if (!$in_installer && !lock_acquire(__FUNCTION__)) {
danielebarchiesi@0 3157 // Another request got the lock, wait for it to finish.
danielebarchiesi@0 3158 lock_wait(__FUNCTION__);
danielebarchiesi@0 3159 return FALSE;
danielebarchiesi@0 3160 }
danielebarchiesi@0 3161
danielebarchiesi@0 3162 require_once DRUPAL_ROOT . '/includes/registry.inc';
danielebarchiesi@0 3163 _registry_update();
danielebarchiesi@0 3164
danielebarchiesi@0 3165 if (!$in_installer) {
danielebarchiesi@0 3166 lock_release(__FUNCTION__);
danielebarchiesi@0 3167 }
danielebarchiesi@0 3168 return TRUE;
danielebarchiesi@0 3169 }
danielebarchiesi@0 3170
danielebarchiesi@0 3171 /**
danielebarchiesi@0 3172 * @} End of "addtogroup registry".
danielebarchiesi@0 3173 */
danielebarchiesi@0 3174
danielebarchiesi@0 3175 /**
danielebarchiesi@0 3176 * Provides central static variable storage.
danielebarchiesi@0 3177 *
danielebarchiesi@0 3178 * All functions requiring a static variable to persist or cache data within
danielebarchiesi@0 3179 * a single page request are encouraged to use this function unless it is
danielebarchiesi@0 3180 * absolutely certain that the static variable will not need to be reset during
danielebarchiesi@0 3181 * the page request. By centralizing static variable storage through this
danielebarchiesi@0 3182 * function, other functions can rely on a consistent API for resetting any
danielebarchiesi@0 3183 * other function's static variables.
danielebarchiesi@0 3184 *
danielebarchiesi@0 3185 * Example:
danielebarchiesi@0 3186 * @code
danielebarchiesi@0 3187 * function language_list($field = 'language') {
danielebarchiesi@0 3188 * $languages = &drupal_static(__FUNCTION__);
danielebarchiesi@0 3189 * if (!isset($languages)) {
danielebarchiesi@0 3190 * // If this function is being called for the first time after a reset,
danielebarchiesi@0 3191 * // query the database and execute any other code needed to retrieve
danielebarchiesi@0 3192 * // information about the supported languages.
danielebarchiesi@0 3193 * ...
danielebarchiesi@0 3194 * }
danielebarchiesi@0 3195 * if (!isset($languages[$field])) {
danielebarchiesi@0 3196 * // If this function is being called for the first time for a particular
danielebarchiesi@0 3197 * // index field, then execute code needed to index the information already
danielebarchiesi@0 3198 * // available in $languages by the desired field.
danielebarchiesi@0 3199 * ...
danielebarchiesi@0 3200 * }
danielebarchiesi@0 3201 * // Subsequent invocations of this function for a particular index field
danielebarchiesi@0 3202 * // skip the above two code blocks and quickly return the already indexed
danielebarchiesi@0 3203 * // information.
danielebarchiesi@0 3204 * return $languages[$field];
danielebarchiesi@0 3205 * }
danielebarchiesi@0 3206 * function locale_translate_overview_screen() {
danielebarchiesi@0 3207 * // When building the content for the translations overview page, make
danielebarchiesi@0 3208 * // sure to get completely fresh information about the supported languages.
danielebarchiesi@0 3209 * drupal_static_reset('language_list');
danielebarchiesi@0 3210 * ...
danielebarchiesi@0 3211 * }
danielebarchiesi@0 3212 * @endcode
danielebarchiesi@0 3213 *
danielebarchiesi@0 3214 * In a few cases, a function can have certainty that there is no legitimate
danielebarchiesi@0 3215 * use-case for resetting that function's static variable. This is rare,
danielebarchiesi@0 3216 * because when writing a function, it's hard to forecast all the situations in
danielebarchiesi@0 3217 * which it will be used. A guideline is that if a function's static variable
danielebarchiesi@0 3218 * does not depend on any information outside of the function that might change
danielebarchiesi@0 3219 * during a single page request, then it's ok to use the "static" keyword
danielebarchiesi@0 3220 * instead of the drupal_static() function.
danielebarchiesi@0 3221 *
danielebarchiesi@0 3222 * Example:
danielebarchiesi@0 3223 * @code
danielebarchiesi@0 3224 * function actions_do(...) {
danielebarchiesi@0 3225 * // $stack tracks the number of recursive calls.
danielebarchiesi@0 3226 * static $stack;
danielebarchiesi@0 3227 * $stack++;
danielebarchiesi@0 3228 * if ($stack > variable_get('actions_max_stack', 35)) {
danielebarchiesi@0 3229 * ...
danielebarchiesi@0 3230 * return;
danielebarchiesi@0 3231 * }
danielebarchiesi@0 3232 * ...
danielebarchiesi@0 3233 * $stack--;
danielebarchiesi@0 3234 * }
danielebarchiesi@0 3235 * @endcode
danielebarchiesi@0 3236 *
danielebarchiesi@0 3237 * In a few cases, a function needs a resettable static variable, but the
danielebarchiesi@0 3238 * function is called many times (100+) during a single page request, so
danielebarchiesi@0 3239 * every microsecond of execution time that can be removed from the function
danielebarchiesi@0 3240 * counts. These functions can use a more cumbersome, but faster variant of
danielebarchiesi@0 3241 * calling drupal_static(). It works by storing the reference returned by
danielebarchiesi@0 3242 * drupal_static() in the calling function's own static variable, thereby
danielebarchiesi@0 3243 * removing the need to call drupal_static() for each iteration of the function.
danielebarchiesi@0 3244 * Conceptually, it replaces:
danielebarchiesi@0 3245 * @code
danielebarchiesi@0 3246 * $foo = &drupal_static(__FUNCTION__);
danielebarchiesi@0 3247 * @endcode
danielebarchiesi@0 3248 * with:
danielebarchiesi@0 3249 * @code
danielebarchiesi@0 3250 * // Unfortunately, this does not work.
danielebarchiesi@0 3251 * static $foo = &drupal_static(__FUNCTION__);
danielebarchiesi@0 3252 * @endcode
danielebarchiesi@0 3253 * However, the above line of code does not work, because PHP only allows static
danielebarchiesi@0 3254 * variables to be initializied by literal values, and does not allow static
danielebarchiesi@0 3255 * variables to be assigned to references.
danielebarchiesi@0 3256 * - http://php.net/manual/en/language.variables.scope.php#language.variables.scope.static
danielebarchiesi@0 3257 * - http://php.net/manual/en/language.variables.scope.php#language.variables.scope.references
danielebarchiesi@0 3258 * The example below shows the syntax needed to work around both limitations.
danielebarchiesi@0 3259 * For benchmarks and more information, see http://drupal.org/node/619666.
danielebarchiesi@0 3260 *
danielebarchiesi@0 3261 * Example:
danielebarchiesi@0 3262 * @code
danielebarchiesi@0 3263 * function user_access($string, $account = NULL) {
danielebarchiesi@0 3264 * // Use the advanced drupal_static() pattern, since this is called very often.
danielebarchiesi@0 3265 * static $drupal_static_fast;
danielebarchiesi@0 3266 * if (!isset($drupal_static_fast)) {
danielebarchiesi@0 3267 * $drupal_static_fast['perm'] = &drupal_static(__FUNCTION__);
danielebarchiesi@0 3268 * }
danielebarchiesi@0 3269 * $perm = &$drupal_static_fast['perm'];
danielebarchiesi@0 3270 * ...
danielebarchiesi@0 3271 * }
danielebarchiesi@0 3272 * @endcode
danielebarchiesi@0 3273 *
danielebarchiesi@0 3274 * @param $name
danielebarchiesi@0 3275 * Globally unique name for the variable. For a function with only one static,
danielebarchiesi@0 3276 * variable, the function name (e.g. via the PHP magic __FUNCTION__ constant)
danielebarchiesi@0 3277 * is recommended. For a function with multiple static variables add a
danielebarchiesi@0 3278 * distinguishing suffix to the function name for each one.
danielebarchiesi@0 3279 * @param $default_value
danielebarchiesi@0 3280 * Optional default value.
danielebarchiesi@0 3281 * @param $reset
danielebarchiesi@0 3282 * TRUE to reset a specific named variable, or all variables if $name is NULL.
danielebarchiesi@0 3283 * Resetting every variable should only be used, for example, for running
danielebarchiesi@0 3284 * unit tests with a clean environment. Should be used only though via
danielebarchiesi@0 3285 * function drupal_static_reset() and the return value should not be used in
danielebarchiesi@0 3286 * this case.
danielebarchiesi@0 3287 *
danielebarchiesi@0 3288 * @return
danielebarchiesi@0 3289 * Returns a variable by reference.
danielebarchiesi@0 3290 *
danielebarchiesi@0 3291 * @see drupal_static_reset()
danielebarchiesi@0 3292 */
danielebarchiesi@0 3293 function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
danielebarchiesi@0 3294 static $data = array(), $default = array();
danielebarchiesi@0 3295 // First check if dealing with a previously defined static variable.
danielebarchiesi@0 3296 if (isset($data[$name]) || array_key_exists($name, $data)) {
danielebarchiesi@0 3297 // Non-NULL $name and both $data[$name] and $default[$name] statics exist.
danielebarchiesi@0 3298 if ($reset) {
danielebarchiesi@0 3299 // Reset pre-existing static variable to its default value.
danielebarchiesi@0 3300 $data[$name] = $default[$name];
danielebarchiesi@0 3301 }
danielebarchiesi@0 3302 return $data[$name];
danielebarchiesi@0 3303 }
danielebarchiesi@0 3304 // Neither $data[$name] nor $default[$name] static variables exist.
danielebarchiesi@0 3305 if (isset($name)) {
danielebarchiesi@0 3306 if ($reset) {
danielebarchiesi@0 3307 // Reset was called before a default is set and yet a variable must be
danielebarchiesi@0 3308 // returned.
danielebarchiesi@0 3309 return $data;
danielebarchiesi@0 3310 }
danielebarchiesi@0 3311 // First call with new non-NULL $name. Initialize a new static variable.
danielebarchiesi@0 3312 $default[$name] = $data[$name] = $default_value;
danielebarchiesi@0 3313 return $data[$name];
danielebarchiesi@0 3314 }
danielebarchiesi@0 3315 // Reset all: ($name == NULL). This needs to be done one at a time so that
danielebarchiesi@0 3316 // references returned by earlier invocations of drupal_static() also get
danielebarchiesi@0 3317 // reset.
danielebarchiesi@0 3318 foreach ($default as $name => $value) {
danielebarchiesi@0 3319 $data[$name] = $value;
danielebarchiesi@0 3320 }
danielebarchiesi@0 3321 // As the function returns a reference, the return should always be a
danielebarchiesi@0 3322 // variable.
danielebarchiesi@0 3323 return $data;
danielebarchiesi@0 3324 }
danielebarchiesi@0 3325
danielebarchiesi@0 3326 /**
danielebarchiesi@0 3327 * Resets one or all centrally stored static variable(s).
danielebarchiesi@0 3328 *
danielebarchiesi@0 3329 * @param $name
danielebarchiesi@0 3330 * Name of the static variable to reset. Omit to reset all variables.
danielebarchiesi@0 3331 */
danielebarchiesi@0 3332 function drupal_static_reset($name = NULL) {
danielebarchiesi@0 3333 drupal_static($name, NULL, TRUE);
danielebarchiesi@0 3334 }
danielebarchiesi@0 3335
danielebarchiesi@0 3336 /**
danielebarchiesi@0 3337 * Detects whether the current script is running in a command-line environment.
danielebarchiesi@0 3338 */
danielebarchiesi@0 3339 function drupal_is_cli() {
danielebarchiesi@0 3340 return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
danielebarchiesi@0 3341 }
danielebarchiesi@0 3342
danielebarchiesi@0 3343 /**
danielebarchiesi@0 3344 * Formats text for emphasized display in a placeholder inside a sentence.
danielebarchiesi@0 3345 *
danielebarchiesi@0 3346 * Used automatically by format_string().
danielebarchiesi@0 3347 *
danielebarchiesi@0 3348 * @param $text
danielebarchiesi@0 3349 * The text to format (plain-text).
danielebarchiesi@0 3350 *
danielebarchiesi@0 3351 * @return
danielebarchiesi@0 3352 * The formatted text (html).
danielebarchiesi@0 3353 */
danielebarchiesi@0 3354 function drupal_placeholder($text) {
danielebarchiesi@0 3355 return '<em class="placeholder">' . check_plain($text) . '</em>';
danielebarchiesi@0 3356 }
danielebarchiesi@0 3357
danielebarchiesi@0 3358 /**
danielebarchiesi@0 3359 * Registers a function for execution on shutdown.
danielebarchiesi@0 3360 *
danielebarchiesi@0 3361 * Wrapper for register_shutdown_function() that catches thrown exceptions to
danielebarchiesi@0 3362 * avoid "Exception thrown without a stack frame in Unknown".
danielebarchiesi@0 3363 *
danielebarchiesi@0 3364 * @param $callback
danielebarchiesi@0 3365 * The shutdown function to register.
danielebarchiesi@0 3366 * @param ...
danielebarchiesi@0 3367 * Additional arguments to pass to the shutdown function.
danielebarchiesi@0 3368 *
danielebarchiesi@0 3369 * @return
danielebarchiesi@0 3370 * Array of shutdown functions to be executed.
danielebarchiesi@0 3371 *
danielebarchiesi@0 3372 * @see register_shutdown_function()
danielebarchiesi@0 3373 * @ingroup php_wrappers
danielebarchiesi@0 3374 */
danielebarchiesi@0 3375 function &drupal_register_shutdown_function($callback = NULL) {
danielebarchiesi@0 3376 // We cannot use drupal_static() here because the static cache is reset during
danielebarchiesi@0 3377 // batch processing, which breaks batch handling.
danielebarchiesi@0 3378 static $callbacks = array();
danielebarchiesi@0 3379
danielebarchiesi@0 3380 if (isset($callback)) {
danielebarchiesi@0 3381 // Only register the internal shutdown function once.
danielebarchiesi@0 3382 if (empty($callbacks)) {
danielebarchiesi@0 3383 register_shutdown_function('_drupal_shutdown_function');
danielebarchiesi@0 3384 }
danielebarchiesi@0 3385 $args = func_get_args();
danielebarchiesi@0 3386 array_shift($args);
danielebarchiesi@0 3387 // Save callback and arguments
danielebarchiesi@0 3388 $callbacks[] = array('callback' => $callback, 'arguments' => $args);
danielebarchiesi@0 3389 }
danielebarchiesi@0 3390 return $callbacks;
danielebarchiesi@0 3391 }
danielebarchiesi@0 3392
danielebarchiesi@0 3393 /**
danielebarchiesi@0 3394 * Executes registered shutdown functions.
danielebarchiesi@0 3395 */
danielebarchiesi@0 3396 function _drupal_shutdown_function() {
danielebarchiesi@0 3397 $callbacks = &drupal_register_shutdown_function();
danielebarchiesi@0 3398
danielebarchiesi@0 3399 // Set the CWD to DRUPAL_ROOT as it is not guaranteed to be the same as it
danielebarchiesi@0 3400 // was in the normal context of execution.
danielebarchiesi@0 3401 chdir(DRUPAL_ROOT);
danielebarchiesi@0 3402
danielebarchiesi@0 3403 try {
danielebarchiesi@0 3404 while (list($key, $callback) = each($callbacks)) {
danielebarchiesi@0 3405 call_user_func_array($callback['callback'], $callback['arguments']);
danielebarchiesi@0 3406 }
danielebarchiesi@0 3407 }
danielebarchiesi@0 3408 catch (Exception $exception) {
danielebarchiesi@0 3409 // If we are displaying errors, then do so with no possibility of a further uncaught exception being thrown.
danielebarchiesi@0 3410 require_once DRUPAL_ROOT . '/includes/errors.inc';
danielebarchiesi@0 3411 if (error_displayable()) {
danielebarchiesi@0 3412 print '<h1>Uncaught exception thrown in shutdown function.</h1>';
danielebarchiesi@0 3413 print '<p>' . _drupal_render_exception_safe($exception) . '</p><hr />';
danielebarchiesi@0 3414 }
danielebarchiesi@0 3415 }
danielebarchiesi@0 3416 }
danielebarchiesi@0 3417
danielebarchiesi@0 3418 /**
danielebarchiesi@0 3419 * Compares the memory required for an operation to the available memory.
danielebarchiesi@0 3420 *
danielebarchiesi@0 3421 * @param $required
danielebarchiesi@0 3422 * The memory required for the operation, expressed as a number of bytes with
danielebarchiesi@0 3423 * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes,
danielebarchiesi@0 3424 * 9mbytes).
danielebarchiesi@0 3425 * @param $memory_limit
danielebarchiesi@0 3426 * (optional) The memory limit for the operation, expressed as a number of
danielebarchiesi@0 3427 * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G,
danielebarchiesi@0 3428 * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP
danielebarchiesi@0 3429 * memory_limit will be used. Defaults to NULL.
danielebarchiesi@0 3430 *
danielebarchiesi@0 3431 * @return
danielebarchiesi@0 3432 * TRUE if there is sufficient memory to allow the operation, or FALSE
danielebarchiesi@0 3433 * otherwise.
danielebarchiesi@0 3434 */
danielebarchiesi@0 3435 function drupal_check_memory_limit($required, $memory_limit = NULL) {
danielebarchiesi@0 3436 if (!isset($memory_limit)) {
danielebarchiesi@0 3437 $memory_limit = ini_get('memory_limit');
danielebarchiesi@0 3438 }
danielebarchiesi@0 3439
danielebarchiesi@0 3440 // There is sufficient memory if:
danielebarchiesi@0 3441 // - No memory limit is set.
danielebarchiesi@0 3442 // - The memory limit is set to unlimited (-1).
danielebarchiesi@0 3443 // - The memory limit is greater than the memory required for the operation.
danielebarchiesi@0 3444 return ((!$memory_limit) || ($memory_limit == -1) || (parse_size($memory_limit) >= parse_size($required)));
danielebarchiesi@0 3445 }