bootstrap.inc

  1. 7.x drupal-7.x/includes/bootstrap.inc
  2. 6.x drupal-6.x/includes/bootstrap.inc

Functions that need to be loaded on every Drupal request.

File

drupal-6.x/includes/bootstrap.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Functions that need to be loaded on every Drupal request.
  5. */
  6. /**
  7. * Indicates that the item should never be removed unless explicitly told to
  8. * using cache_clear_all() with a cache ID.
  9. */
  10. define('CACHE_PERMANENT', 0);
  11. /**
  12. * Indicates that the item should be removed at the next general cache wipe.
  13. */
  14. define('CACHE_TEMPORARY', -1);
  15. /**
  16. * Indicates that page caching is disabled.
  17. */
  18. define('CACHE_DISABLED', 0);
  19. /**
  20. * Indicates that page caching is enabled, using "normal" mode.
  21. */
  22. define('CACHE_NORMAL', 1);
  23. /**
  24. * Indicates that page caching is using "aggressive" mode. This bypasses
  25. * loading any modules for additional speed, which may break functionality in
  26. * modules that expect to be run on each page load.
  27. */
  28. define('CACHE_AGGRESSIVE', 2);
  29. /**
  30. * Log message severity -- Emergency: system is unusable.
  31. *
  32. * The WATCHDOG_* constant definitions correspond to the logging severity levels
  33. * defined in RFC 3164, section 4.1.1: http://www.faqs.org/rfcs/rfc3164.html
  34. *
  35. * @see watchdog()
  36. * @see watchdog_severity_levels()
  37. */
  38. define('WATCHDOG_EMERG', 0);
  39. /**
  40. * Log message severity -- Alert: action must be taken immediately.
  41. *
  42. * The WATCHDOG_* constant definitions correspond to the logging severity levels
  43. * defined in RFC 3164, section 4.1.1: http://www.faqs.org/rfcs/rfc3164.html
  44. *
  45. * @see watchdog()
  46. * @see watchdog_severity_levels()
  47. */
  48. define('WATCHDOG_ALERT', 1);
  49. /**
  50. * Log message severity -- Critical: critical conditions.
  51. *
  52. * The WATCHDOG_* constant definitions correspond to the logging severity levels
  53. * defined in RFC 3164, section 4.1.1: http://www.faqs.org/rfcs/rfc3164.html
  54. *
  55. * @see watchdog()
  56. * @see watchdog_severity_levels()
  57. */
  58. define('WATCHDOG_CRITICAL', 2);
  59. /**
  60. * Log message severity -- Error: error conditions.
  61. *
  62. * The WATCHDOG_* constant definitions correspond to the logging severity levels
  63. * defined in RFC 3164, section 4.1.1: http://www.faqs.org/rfcs/rfc3164.html
  64. *
  65. * @see watchdog()
  66. * @see watchdog_severity_levels()
  67. */
  68. define('WATCHDOG_ERROR', 3);
  69. /**
  70. * Log message severity -- Warning: warning conditions.
  71. *
  72. * The WATCHDOG_* constant definitions correspond to the logging severity levels
  73. * defined in RFC 3164, section 4.1.1: http://www.faqs.org/rfcs/rfc3164.html
  74. *
  75. * @see watchdog()
  76. * @see watchdog_severity_levels()
  77. */
  78. define('WATCHDOG_WARNING', 4);
  79. /**
  80. * Log message severity -- Notice: normal but significant condition.
  81. *
  82. * The WATCHDOG_* constant definitions correspond to the logging severity levels
  83. * defined in RFC 3164, section 4.1.1: http://www.faqs.org/rfcs/rfc3164.html
  84. *
  85. * @see watchdog()
  86. * @see watchdog_severity_levels()
  87. */
  88. define('WATCHDOG_NOTICE', 5);
  89. /**
  90. * Log message severity -- Informational: informational messages.
  91. *
  92. * The WATCHDOG_* constant definitions correspond to the logging severity levels
  93. * defined in RFC 3164, section 4.1.1: http://www.faqs.org/rfcs/rfc3164.html
  94. *
  95. * @see watchdog()
  96. * @see watchdog_severity_levels()
  97. */
  98. define('WATCHDOG_INFO', 6);
  99. /**
  100. * Log message severity -- Debug: debug-level messages.
  101. *
  102. * The WATCHDOG_* constant definitions correspond to the logging severity levels
  103. * defined in RFC 3164, section 4.1.1: http://www.faqs.org/rfcs/rfc3164.html
  104. *
  105. * @see watchdog()
  106. * @see watchdog_severity_levels()
  107. */
  108. define('WATCHDOG_DEBUG', 7);
  109. /**
  110. * First bootstrap phase: initialize configuration.
  111. */
  112. define('DRUPAL_BOOTSTRAP_CONFIGURATION', 0);
  113. /**
  114. * Second bootstrap phase: try to call a non-database cache
  115. * fetch routine.
  116. */
  117. define('DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE', 1);
  118. /**
  119. * Third bootstrap phase: initialize database layer.
  120. */
  121. define('DRUPAL_BOOTSTRAP_DATABASE', 2);
  122. /**
  123. * Fourth bootstrap phase: identify and reject banned hosts.
  124. */
  125. define('DRUPAL_BOOTSTRAP_ACCESS', 3);
  126. /**
  127. * Fifth bootstrap phase: initialize session handling.
  128. */
  129. define('DRUPAL_BOOTSTRAP_SESSION', 4);
  130. /**
  131. * Sixth bootstrap phase: load bootstrap.inc and module.inc, start
  132. * the variable system and try to serve a page from the cache.
  133. */
  134. define('DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE', 5);
  135. /**
  136. * Seventh bootstrap phase: find out language of the page.
  137. */
  138. define('DRUPAL_BOOTSTRAP_LANGUAGE', 6);
  139. /**
  140. * Eighth bootstrap phase: set $_GET['q'] to Drupal path of request.
  141. */
  142. define('DRUPAL_BOOTSTRAP_PATH', 7);
  143. /**
  144. * Final bootstrap phase: Drupal is fully loaded; validate and fix
  145. * input data.
  146. */
  147. define('DRUPAL_BOOTSTRAP_FULL', 8);
  148. /**
  149. * Role ID for anonymous users; should match what's in the "role" table.
  150. */
  151. define('DRUPAL_ANONYMOUS_RID', 1);
  152. /**
  153. * Role ID for authenticated users; should match what's in the "role" table.
  154. */
  155. define('DRUPAL_AUTHENTICATED_RID', 2);
  156. /**
  157. * No language negotiation. The default language is used.
  158. */
  159. define('LANGUAGE_NEGOTIATION_NONE', 0);
  160. /**
  161. * Path based negotiation with fallback to default language
  162. * if no defined path prefix identified.
  163. */
  164. define('LANGUAGE_NEGOTIATION_PATH_DEFAULT', 1);
  165. /**
  166. * Path based negotiation with fallback to user preferences
  167. * and browser language detection if no defined path prefix
  168. * identified.
  169. */
  170. define('LANGUAGE_NEGOTIATION_PATH', 2);
  171. /**
  172. * Domain based negotiation with fallback to default language
  173. * if no language identified by domain.
  174. */
  175. define('LANGUAGE_NEGOTIATION_DOMAIN', 3);
  176. /**
  177. * Language written left to right. Possible value of $language->direction.
  178. */
  179. define('LANGUAGE_LTR', 0);
  180. /**
  181. * Language written right to left. Possible value of $language->direction.
  182. */
  183. define('LANGUAGE_RTL', 1);
  184. // Hide E_DEPRECATED messages.
  185. if (defined('E_DEPRECATED')) {
  186. error_reporting(error_reporting() & ~E_DEPRECATED);
  187. }
  188. /**
  189. * Start the timer with the specified name. If you start and stop
  190. * the same timer multiple times, the measured intervals will be
  191. * accumulated.
  192. *
  193. * @param name
  194. * The name of the timer.
  195. */
  196. function timer_start($name) {
  197. global $timers;
  198. list($usec, $sec) = explode(' ', microtime());
  199. $timers[$name]['start'] = (float)$usec + (float)$sec;
  200. $timers[$name]['count'] = isset($timers[$name]['count']) ? ++$timers[$name]['count'] : 1;
  201. }
  202. /**
  203. * Read the current timer value without stopping the timer.
  204. *
  205. * @param name
  206. * The name of the timer.
  207. * @return
  208. * The current timer value in ms.
  209. */
  210. function timer_read($name) {
  211. global $timers;
  212. if (isset($timers[$name]['start'])) {
  213. list($usec, $sec) = explode(' ', microtime());
  214. $stop = (float)$usec + (float)$sec;
  215. $diff = round(($stop - $timers[$name]['start']) * 1000, 2);
  216. if (isset($timers[$name]['time'])) {
  217. $diff += $timers[$name]['time'];
  218. }
  219. return $diff;
  220. }
  221. }
  222. /**
  223. * Stop the timer with the specified name.
  224. *
  225. * @param name
  226. * The name of the timer.
  227. * @return
  228. * A timer array. The array contains the number of times the
  229. * timer has been started and stopped (count) and the accumulated
  230. * timer value in ms (time).
  231. */
  232. function timer_stop($name) {
  233. global $timers;
  234. $timers[$name]['time'] = timer_read($name);
  235. unset($timers[$name]['start']);
  236. return $timers[$name];
  237. }
  238. /**
  239. * Find the appropriate configuration directory.
  240. *
  241. * Try finding a matching configuration directory by stripping the website's
  242. * hostname from left to right and pathname from right to left. The first
  243. * configuration file found will be used; the remaining will ignored. If no
  244. * configuration file is found, return a default value '$confdir/default'.
  245. *
  246. * Example for a fictitious site installed at
  247. * http://www.drupal.org:8080/mysite/test/ the 'settings.php' is searched in
  248. * the following directories:
  249. *
  250. * 1. $confdir/8080.www.drupal.org.mysite.test
  251. * 2. $confdir/www.drupal.org.mysite.test
  252. * 3. $confdir/drupal.org.mysite.test
  253. * 4. $confdir/org.mysite.test
  254. *
  255. * 5. $confdir/8080.www.drupal.org.mysite
  256. * 6. $confdir/www.drupal.org.mysite
  257. * 7. $confdir/drupal.org.mysite
  258. * 8. $confdir/org.mysite
  259. *
  260. * 9. $confdir/8080.www.drupal.org
  261. * 10. $confdir/www.drupal.org
  262. * 11. $confdir/drupal.org
  263. * 12. $confdir/org
  264. *
  265. * 13. $confdir/default
  266. *
  267. * @param $require_settings
  268. * Only configuration directories with an existing settings.php file
  269. * will be recognized. Defaults to TRUE. During initial installation,
  270. * this is set to FALSE so that Drupal can detect a matching directory,
  271. * then create a new settings.php file in it.
  272. * @param reset
  273. * Force a full search for matching directories even if one had been
  274. * found previously.
  275. * @return
  276. * The path of the matching directory.
  277. */
  278. function conf_path($require_settings = TRUE, $reset = FALSE) {
  279. static $conf = '';
  280. if ($conf && !$reset) {
  281. return $conf;
  282. }
  283. $confdir = 'sites';
  284. $uri = explode('/', $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']);
  285. $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
  286. for ($i = count($uri) - 1; $i > 0; $i--) {
  287. for ($j = count($server); $j > 0; $j--) {
  288. $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
  289. if (file_exists("$confdir/$dir/settings.php") || (!$require_settings && file_exists("$confdir/$dir"))) {
  290. $conf = "$confdir/$dir";
  291. return $conf;
  292. }
  293. }
  294. }
  295. $conf = "$confdir/default";
  296. return $conf;
  297. }
  298. /**
  299. * Unsets all disallowed global variables. See $allowed for what's allowed.
  300. */
  301. function drupal_unset_globals() {
  302. if (ini_get('register_globals')) {
  303. $allowed = array('_ENV' => 1, '_GET' => 1, '_POST' => 1, '_COOKIE' => 1, '_FILES' => 1, '_SERVER' => 1, '_REQUEST' => 1, 'GLOBALS' => 1);
  304. foreach ($GLOBALS as $key => $value) {
  305. if (!isset($allowed[$key])) {
  306. unset($GLOBALS[$key]);
  307. }
  308. }
  309. }
  310. }
  311. /**
  312. * Validate that a hostname (for example $_SERVER['HTTP_HOST']) is safe.
  313. *
  314. * As $_SERVER['HTTP_HOST'] is user input, ensure it only contains characters
  315. * allowed in hostnames. See RFC 952 (and RFC 2181). $_SERVER['HTTP_HOST'] is
  316. * lowercased.
  317. *
  318. * @return
  319. * TRUE if only containing valid characters, or FALSE otherwise.
  320. */
  321. function drupal_valid_http_host($host) {
  322. return preg_match('/^\[?(?:[a-z0-9-:\]_]+\.?)+$/', $host);
  323. }
  324. /**
  325. * Loads the configuration and sets the base URL, cookie domain, and
  326. * session name correctly.
  327. */
  328. function conf_init() {
  329. global $base_url, $base_path, $base_root;
  330. // Export the following settings.php variables to the global namespace
  331. global $db_url, $db_prefix, $db_collation, $cookie_domain, $conf, $installed_profile, $update_free_access;
  332. $conf = array();
  333. if (!isset($_SERVER['SERVER_PROTOCOL']) || ($_SERVER['SERVER_PROTOCOL'] != 'HTTP/1.0' && $_SERVER['SERVER_PROTOCOL'] != 'HTTP/1.1')) {
  334. $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
  335. }
  336. if (isset($_SERVER['HTTP_HOST'])) {
  337. // As HTTP_HOST is user input, ensure it only contains characters allowed
  338. // in hostnames. See RFC 952 (and RFC 2181).
  339. // $_SERVER['HTTP_HOST'] is lowercased here per specifications.
  340. $_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']);
  341. if (!drupal_valid_http_host($_SERVER['HTTP_HOST'])) {
  342. // HTTP_HOST is invalid, e.g. if containing slashes it may be an attack.
  343. header($_SERVER['SERVER_PROTOCOL'] .' 400 Bad Request');
  344. exit;
  345. }
  346. }
  347. else {
  348. // Some pre-HTTP/1.1 clients will not send a Host header. Ensure the key is
  349. // defined for E_ALL compliance.
  350. $_SERVER['HTTP_HOST'] = '';
  351. }
  352. if (file_exists('./'. conf_path() .'/settings.php')) {
  353. include_once './'. conf_path() .'/settings.php';
  354. }
  355. // Ignore the placeholder URL from default.settings.php.
  356. if (isset($db_url) && $db_url == 'mysql://username:password@localhost/databasename') {
  357. $db_url = '';
  358. }
  359. if (isset($base_url)) {
  360. // Parse fixed base URL from settings.php.
  361. $parts = parse_url($base_url);
  362. if (!isset($parts['path'])) {
  363. $parts['path'] = '';
  364. }
  365. $base_path = $parts['path'] .'/';
  366. // Build $base_root (everything until first slash after "scheme://").
  367. $base_root = substr($base_url, 0, strlen($base_url) - strlen($parts['path']));
  368. }
  369. else {
  370. // Create base URL
  371. $base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
  372. $base_url = $base_root .= '://'. $_SERVER['HTTP_HOST'];
  373. // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'], not
  374. // be modified by a visitor.
  375. if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
  376. $base_path = "/$dir";
  377. $base_url .= $base_path;
  378. $base_path .= '/';
  379. }
  380. else {
  381. $base_path = '/';
  382. }
  383. }
  384. if ($cookie_domain) {
  385. // If the user specifies the cookie domain, also use it for session name.
  386. $session_name = $cookie_domain;
  387. }
  388. else {
  389. // Otherwise use $base_url as session name, without the protocol
  390. // to use the same session identifiers across HTTP and HTTPS.
  391. list( , $session_name) = explode('://', $base_url, 2);
  392. // We escape the hostname because it can be modified by a visitor.
  393. if (!empty($_SERVER['HTTP_HOST'])) {
  394. $cookie_domain = check_plain($_SERVER['HTTP_HOST']);
  395. // Strip leading periods, www., and port numbers from cookie domain.
  396. $cookie_domain = ltrim($cookie_domain, '.');
  397. if (strpos($cookie_domain, 'www.') === 0) {
  398. $cookie_domain = substr($cookie_domain, 4);
  399. }
  400. $cookie_domain = explode(':', $cookie_domain);
  401. $cookie_domain = '.'. $cookie_domain[0];
  402. }
  403. }
  404. // To prevent session cookies from being hijacked, a user can configure the
  405. // SSL version of their website to only transfer session cookies via SSL by
  406. // using PHP's session.cookie_secure setting. The browser will then use two
  407. // separate session cookies for the HTTPS and HTTP versions of the site. So we
  408. // must use different session identifiers for HTTPS and HTTP to prevent a
  409. // cookie collision.
  410. if (ini_get('session.cookie_secure')) {
  411. $session_name .= 'SSL';
  412. }
  413. // Per RFC 2109, cookie domains must contain at least one dot other than the
  414. // first. For hosts such as 'localhost' or IP Addresses we don't set a cookie domain.
  415. if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
  416. ini_set('session.cookie_domain', $cookie_domain);
  417. }
  418. session_name('SESS'. md5($session_name));
  419. }
  420. /**
  421. * Returns and optionally sets the filename for a system item (module,
  422. * theme, etc.). The filename, whether provided, cached, or retrieved
  423. * from the database, is only returned if the file exists.
  424. *
  425. * This function plays a key role in allowing Drupal's resources (modules
  426. * and themes) to be located in different places depending on a site's
  427. * configuration. For example, a module 'foo' may legally be be located
  428. * in any of these three places:
  429. *
  430. * modules/foo/foo.module
  431. * sites/all/modules/foo/foo.module
  432. * sites/example.com/modules/foo/foo.module
  433. *
  434. * Calling drupal_get_filename('module', 'foo') will give you one of
  435. * the above, depending on where the module is located.
  436. *
  437. * @param $type
  438. * The type of the item (i.e. theme, theme_engine, module, profile).
  439. * @param $name
  440. * The name of the item for which the filename is requested.
  441. * @param $filename
  442. * The filename of the item if it is to be set explicitly rather
  443. * than by consulting the database.
  444. *
  445. * @return
  446. * The filename of the requested item.
  447. */
  448. function drupal_get_filename($type, $name, $filename = NULL) {
  449. static $files = array();
  450. if (!isset($files[$type])) {
  451. $files[$type] = array();
  452. }
  453. if (!empty($filename) && file_exists($filename)) {
  454. $files[$type][$name] = $filename;
  455. }
  456. elseif (isset($files[$type][$name])) {
  457. // nothing
  458. }
  459. // Verify that we have an active database connection, before querying
  460. // the database. This is required because this function is called both
  461. // before we have a database connection (i.e. during installation) and
  462. // when a database connection fails.
  463. elseif (db_is_active() && (($file = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file))) {
  464. $files[$type][$name] = $file;
  465. }
  466. else {
  467. // Fallback to searching the filesystem if the database connection is
  468. // not established or the requested file is not found.
  469. $config = conf_path();
  470. $dir = (($type == 'theme_engine') ? 'themes/engines' : "${type}s");
  471. $file = (($type == 'theme_engine') ? "$name.engine" : "$name.$type");
  472. foreach (array("$config/$dir/$file", "$config/$dir/$name/$file", "$dir/$file", "$dir/$name/$file") as $file) {
  473. if (file_exists($file)) {
  474. $files[$type][$name] = $file;
  475. break;
  476. }
  477. }
  478. }
  479. if (isset($files[$type][$name])) {
  480. return $files[$type][$name];
  481. }
  482. }
  483. /**
  484. * Load the persistent variable table.
  485. *
  486. * The variable table is composed of values that have been saved in the table
  487. * with variable_set() as well as those explicitly specified in the configuration
  488. * file.
  489. */
  490. function variable_init($conf = array()) {
  491. // NOTE: caching the variables improves performance by 20% when serving cached pages.
  492. if ($cached = cache_get('variables', 'cache')) {
  493. $variables = $cached->data;
  494. }
  495. else {
  496. $result = db_query('SELECT * FROM {variable}');
  497. while ($variable = db_fetch_object($result)) {
  498. $variables[$variable->name] = unserialize($variable->value);
  499. }
  500. cache_set('variables', $variables);
  501. }
  502. foreach ($conf as $name => $value) {
  503. $variables[$name] = $value;
  504. }
  505. return $variables;
  506. }
  507. /**
  508. * Returns a persistent variable.
  509. *
  510. * Case-sensitivity of the variable_* functions depends on the database
  511. * collation used. To avoid problems, always use lower case for persistent
  512. * variable names.
  513. *
  514. * @param $name
  515. * The name of the variable to return.
  516. * @param $default
  517. * The default value to use if this variable has never been set.
  518. * @return
  519. * The value of the variable.
  520. *
  521. * @see variable_del(), variable_set()
  522. */
  523. function variable_get($name, $default) {
  524. global $conf;
  525. return isset($conf[$name]) ? $conf[$name] : $default;
  526. }
  527. /**
  528. * Sets a persistent variable.
  529. *
  530. * Case-sensitivity of the variable_* functions depends on the database
  531. * collation used. To avoid problems, always use lower case for persistent
  532. * variable names.
  533. *
  534. * @param $name
  535. * The name of the variable to set.
  536. * @param $value
  537. * The value to set. This can be any PHP data type; these functions take care
  538. * of serialization as necessary.
  539. *
  540. * @see variable_del(), variable_get()
  541. */
  542. function variable_set($name, $value) {
  543. global $conf;
  544. $serialized_value = serialize($value);
  545. db_query("UPDATE {variable} SET value = '%s' WHERE name = '%s'", $serialized_value, $name);
  546. if (!db_affected_rows()) {
  547. @db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $serialized_value);
  548. }
  549. cache_clear_all('variables', 'cache');
  550. $conf[$name] = $value;
  551. }
  552. /**
  553. * Unsets a persistent variable.
  554. *
  555. * Case-sensitivity of the variable_* functions depends on the database
  556. * collation used. To avoid problems, always use lower case for persistent
  557. * variable names.
  558. *
  559. * @param $name
  560. * The name of the variable to undefine.
  561. *
  562. * @see variable_get(), variable_set()
  563. */
  564. function variable_del($name) {
  565. global $conf;
  566. db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
  567. cache_clear_all('variables', 'cache');
  568. unset($conf[$name]);
  569. }
  570. /**
  571. * Retrieve the current page from the cache.
  572. *
  573. * Note: we do not serve cached pages when status messages are waiting (from
  574. * a redirected form submission which was completed).
  575. *
  576. * @param $status_only
  577. * When set to TRUE, retrieve the status of the page cache only
  578. * (whether it was started in this request or not).
  579. */
  580. function page_get_cache($status_only = FALSE) {
  581. static $status = FALSE;
  582. global $user, $base_root;
  583. if ($status_only) {
  584. return $status;
  585. }
  586. $cache = NULL;
  587. if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && count(drupal_set_message()) == 0 && $_SERVER['SERVER_SOFTWARE'] !== 'PHP CLI') {
  588. $cache = cache_get($base_root . request_uri(), 'cache_page');
  589. if (empty($cache)) {
  590. ob_start();
  591. $status = TRUE;
  592. }
  593. }
  594. return $cache;
  595. }
  596. /**
  597. * Call all init or exit hooks without including all modules.
  598. *
  599. * @param $hook
  600. * The name of the bootstrap hook we wish to invoke.
  601. */
  602. function bootstrap_invoke_all($hook) {
  603. foreach (module_list(TRUE, TRUE) as $module) {
  604. drupal_load('module', $module);
  605. module_invoke($module, $hook);
  606. }
  607. }
  608. /**
  609. * Includes a file with the provided type and name. This prevents
  610. * including a theme, engine, module, etc., more than once.
  611. *
  612. * @param $type
  613. * The type of item to load (i.e. theme, theme_engine, module, profile).
  614. * @param $name
  615. * The name of the item to load.
  616. *
  617. * @return
  618. * TRUE if the item is loaded or has already been loaded.
  619. */
  620. function drupal_load($type, $name) {
  621. static $files = array();
  622. if (isset($files[$type][$name])) {
  623. return TRUE;
  624. }
  625. $filename = drupal_get_filename($type, $name);
  626. if ($filename) {
  627. include_once "./$filename";
  628. $files[$type][$name] = TRUE;
  629. return TRUE;
  630. }
  631. return FALSE;
  632. }
  633. /**
  634. * Set HTTP headers in preparation for a page response.
  635. *
  636. * Authenticated users are always given a 'no-cache' header, and will
  637. * fetch a fresh page on every request. This prevents authenticated
  638. * users seeing locally cached pages that show them as logged out.
  639. *
  640. * @see page_set_cache()
  641. */
  642. function drupal_page_header() {
  643. header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
  644. header("Last-Modified: ". gmdate("D, d M Y H:i:s") ." GMT");
  645. header("Cache-Control: store, no-cache, must-revalidate");
  646. header("Cache-Control: post-check=0, pre-check=0", FALSE);
  647. }
  648. /**
  649. * Set HTTP headers in preparation for a cached page response.
  650. *
  651. * The general approach here is that anonymous users can keep a local
  652. * cache of the page, but must revalidate it on every request. Then,
  653. * they are given a '304 Not Modified' response as long as they stay
  654. * logged out and the page has not been modified.
  655. *
  656. */
  657. function drupal_page_cache_header($cache) {
  658. // Set default values:
  659. $last_modified = gmdate('D, d M Y H:i:s', $cache->created) .' GMT';
  660. $etag = '"'. md5($last_modified) .'"';
  661. // See if the client has provided the required HTTP headers:
  662. $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : FALSE;
  663. $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : FALSE;
  664. if ($if_modified_since && $if_none_match
  665. && $if_none_match == $etag // etag must match
  666. && $if_modified_since == $last_modified) { // if-modified-since must match
  667. header($_SERVER['SERVER_PROTOCOL'] .' 304 Not Modified');
  668. // All 304 responses must send an etag if the 200 response for the same object contained an etag
  669. header("Etag: $etag");
  670. return;
  671. }
  672. // Send appropriate response:
  673. header("Last-Modified: $last_modified");
  674. header("ETag: $etag");
  675. // The following headers force validation of cache:
  676. header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
  677. header("Cache-Control: must-revalidate");
  678. if (variable_get('page_compression', TRUE) && extension_loaded('zlib')) {
  679. // Determine if the browser accepts gzipped data.
  680. if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
  681. // $cache->data is already gzip'ed, so make sure zlib.output_compression
  682. // does not compress it once more.
  683. ini_set('zlib.output_compression', '0');
  684. header('Content-Encoding: gzip');
  685. }
  686. else {
  687. // The client does not support compression, so unzip the data in the
  688. // cache. Strip the gzip header and run uncompress.
  689. $cache->data = gzinflate(substr(substr($cache->data, 10), 0, -8));
  690. }
  691. }
  692. // Send the original request's headers. We send them one after
  693. // another so PHP's header() function can deal with duplicate
  694. // headers.
  695. $headers = explode("\n", $cache->headers);
  696. foreach ($headers as $header) {
  697. header($header);
  698. }
  699. print $cache->data;
  700. }
  701. /**
  702. * Define the critical hooks that force modules to always be loaded.
  703. */
  704. function bootstrap_hooks() {
  705. return array('boot', 'exit');
  706. }
  707. /**
  708. * Unserializes and appends elements from a serialized string.
  709. *
  710. * @param $obj
  711. * The object to which the elements are appended.
  712. * @param $field
  713. * The attribute of $obj whose value should be unserialized.
  714. */
  715. function drupal_unpack($obj, $field = 'data') {
  716. if ($obj->$field && $data = unserialize($obj->$field)) {
  717. foreach ($data as $key => $value) {
  718. if (!empty($key) && !isset($obj->$key)) {
  719. $obj->$key = $value;
  720. }
  721. }
  722. }
  723. return $obj;
  724. }
  725. /**
  726. * Return the URI of the referring page.
  727. */
  728. function referer_uri() {
  729. if (isset($_SERVER['HTTP_REFERER'])) {
  730. return $_SERVER['HTTP_REFERER'];
  731. }
  732. }
  733. /**
  734. * Encode special characters in a plain-text string for display as HTML.
  735. *
  736. * Also validates strings as UTF-8 to prevent cross site scripting attacks on
  737. * Internet Explorer 6.
  738. *
  739. * @param $text
  740. * The text to be checked or processed.
  741. * @return
  742. * An HTML safe version of $text, or an empty string if $text is not
  743. * valid UTF-8.
  744. *
  745. * @see drupal_validate_utf8().
  746. */
  747. function check_plain($text) {
  748. static $php525;
  749. if (!isset($php525)) {
  750. $php525 = version_compare(PHP_VERSION, '5.2.5', '>=');
  751. }
  752. // We duplicate the preg_match() to validate strings as UTF-8 from
  753. // drupal_validate_utf8() here. This avoids the overhead of an additional
  754. // function call, since check_plain() may be called hundreds of times during
  755. // a request. For PHP 5.2.5+, this check for valid UTF-8 should be handled
  756. // internally by PHP in htmlspecialchars().
  757. // @see http://www.php.net/releases/5_2_5.php
  758. // @todo remove this when support for either IE6 or PHP < 5.2.5 is dropped.
  759. if ($php525) {
  760. return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
  761. }
  762. return (preg_match('/^./us', $text) == 1) ? htmlspecialchars($text, ENT_QUOTES, 'UTF-8') : '';
  763. }
  764. /**
  765. * Checks whether a string is valid UTF-8.
  766. *
  767. * All functions designed to filter input should use drupal_validate_utf8
  768. * to ensure they operate on valid UTF-8 strings to prevent bypass of the
  769. * filter.
  770. *
  771. * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented
  772. * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent
  773. * bytes. When these subsequent bytes are HTML control characters such as
  774. * quotes or angle brackets, parts of the text that were deemed safe by filters
  775. * end up in locations that are potentially unsafe; An onerror attribute that
  776. * is outside of a tag, and thus deemed safe by a filter, can be interpreted
  777. * by the browser as if it were inside the tag.
  778. *
  779. * This function exploits preg_match behaviour (since PHP 4.3.5) when used
  780. * with the u modifier, as a fast way to find invalid UTF-8. When the matched
  781. * string contains an invalid byte sequence, it will fail silently.
  782. *
  783. * preg_match may not fail on 4 and 5 octet sequences, even though they
  784. * are not supported by the specification.
  785. *
  786. * The specific preg_match behaviour is present since PHP 4.3.5.
  787. *
  788. * @param $text
  789. * The text to check.
  790. * @return
  791. * TRUE if the text is valid UTF-8, FALSE if not.
  792. */
  793. function drupal_validate_utf8($text) {
  794. if (strlen($text) == 0) {
  795. return TRUE;
  796. }
  797. // For performance reasons this logic is duplicated in check_plain().
  798. return (preg_match('/^./us', $text) == 1);
  799. }
  800. /**
  801. * Since $_SERVER['REQUEST_URI'] is only available on Apache, we
  802. * generate an equivalent using other environment variables.
  803. */
  804. function request_uri() {
  805. if (isset($_SERVER['REQUEST_URI'])) {
  806. $uri = $_SERVER['REQUEST_URI'];
  807. }
  808. else {
  809. if (isset($_SERVER['argv'])) {
  810. $uri = $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['argv'][0];
  811. }
  812. elseif (isset($_SERVER['QUERY_STRING'])) {
  813. $uri = $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING'];
  814. }
  815. else {
  816. $uri = $_SERVER['SCRIPT_NAME'];
  817. }
  818. }
  819. // Prevent multiple slashes to avoid cross site requests via the FAPI.
  820. $uri = '/'. ltrim($uri, '/');
  821. return $uri;
  822. }
  823. /**
  824. * Log a system message.
  825. *
  826. * @param $type
  827. * The category to which this message belongs. Can be any string, but the
  828. * general practice is to use the name of the module calling watchdog().
  829. * @param $message
  830. * The message to store in the log. See t() for documentation
  831. * on how $message and $variables interact. Keep $message
  832. * translatable by not concatenating dynamic values into it!
  833. * @param $variables
  834. * Array of variables to replace in the message on display or
  835. * NULL if message is already translated or not possible to
  836. * translate.
  837. * @param $severity
  838. * The severity of the message, as per RFC 3164. Possible values are
  839. * WATCHDOG_ERROR, WATCHDOG_WARNING, etc.
  840. * @param $link
  841. * A link to associate with the message.
  842. *
  843. * @see watchdog_severity_levels()
  844. */
  845. function watchdog($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) {
  846. global $user, $base_root;
  847. // Prepare the fields to be logged
  848. $log_message = array(
  849. 'type' => $type,
  850. 'message' => $message,
  851. 'variables' => $variables,
  852. 'severity' => $severity,
  853. 'link' => $link,
  854. 'user' => $user,
  855. 'request_uri' => $base_root . request_uri(),
  856. 'referer' => referer_uri(),
  857. 'ip' => ip_address(),
  858. 'timestamp' => time(),
  859. );
  860. // Call the logging hooks to log/process the message
  861. foreach (module_implements('watchdog') as $module) {
  862. module_invoke($module, 'watchdog', $log_message);
  863. }
  864. }
  865. /**
  866. * Set a message which reflects the status of the performed operation.
  867. *
  868. * If the function is called with no arguments, this function returns all set
  869. * messages without clearing them.
  870. *
  871. * @param $message
  872. * The message should begin with a capital letter and always ends with a
  873. * period '.'.
  874. * @param $type
  875. * The type of the message. One of the following values are possible:
  876. * - 'status'
  877. * - 'warning'
  878. * - 'error'
  879. * @param $repeat
  880. * If this is FALSE and the message is already set, then the message won't
  881. * be repeated.
  882. */
  883. function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) {
  884. if ($message) {
  885. if (!isset($_SESSION['messages'])) {
  886. $_SESSION['messages'] = array();
  887. }
  888. if (!isset($_SESSION['messages'][$type])) {
  889. $_SESSION['messages'][$type] = array();
  890. }
  891. if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
  892. $_SESSION['messages'][$type][] = $message;
  893. }
  894. }
  895. // messages not set when DB connection fails
  896. return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL;
  897. }
  898. /**
  899. * Return all messages that have been set.
  900. *
  901. * @param $type
  902. * (optional) Only return messages of this type.
  903. * @param $clear_queue
  904. * (optional) Set to FALSE if you do not want to clear the messages queue
  905. * @return
  906. * An associative array, the key is the message type, the value an array
  907. * of messages. If the $type parameter is passed, you get only that type,
  908. * or an empty array if there are no such messages. If $type is not passed,
  909. * all message types are returned, or an empty array if none exist.
  910. */
  911. function drupal_get_messages($type = NULL, $clear_queue = TRUE) {
  912. if ($messages = drupal_set_message()) {
  913. if ($type) {
  914. if ($clear_queue) {
  915. unset($_SESSION['messages'][$type]);
  916. }
  917. if (isset($messages[$type])) {
  918. return array($type => $messages[$type]);
  919. }
  920. }
  921. else {
  922. if ($clear_queue) {
  923. unset($_SESSION['messages']);
  924. }
  925. return $messages;
  926. }
  927. }
  928. return array();
  929. }
  930. /**
  931. * Perform an access check for a given mask and rule type. Rules are usually
  932. * created via admin/user/rules page.
  933. *
  934. * If any allow rule matches, access is allowed. Otherwise, if any deny rule
  935. * matches, access is denied. If no rule matches, access is allowed.
  936. *
  937. * @param $type string
  938. * Type of access to check: Allowed values are:
  939. * - 'host': host name or IP address
  940. * - 'mail': e-mail address
  941. * - 'user': username
  942. * @param $mask string
  943. * String or mask to test: '_' matches any character, '%' matches any
  944. * number of characters.
  945. * @return bool
  946. * TRUE if access is denied, FALSE if access is allowed.
  947. */
  948. function drupal_is_denied($type, $mask) {
  949. // Because this function is called for every page request, both cached
  950. // and non-cached pages, we tried to optimize it as much as possible.
  951. // We deny access if the only matching records in the {access} table have
  952. // status 0 (deny). If any have status 1 (allow), or if there are no
  953. // matching records, we allow access.
  954. $sql = "SELECT 1 FROM {access} WHERE type = '%s' AND LOWER('%s') LIKE LOWER(mask) AND status = %d";
  955. return db_result(db_query_range($sql, $type, $mask, 0, 0, 1)) && !db_result(db_query_range($sql, $type, $mask, 1, 0, 1));
  956. }
  957. /**
  958. * Generates a default anonymous $user object.
  959. *
  960. * @return Object - the user object.
  961. */
  962. function drupal_anonymous_user($session = '') {
  963. $user = new stdClass();
  964. $user->uid = 0;
  965. $user->hostname = ip_address();
  966. $user->roles = array();
  967. $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
  968. $user->session = $session;
  969. $user->cache = 0;
  970. return $user;
  971. }
  972. /**
  973. * A string describing a phase of Drupal to load. Each phase adds to the
  974. * previous one, so invoking a later phase automatically runs the earlier
  975. * phases too. The most important usage is that if you want to access the
  976. * Drupal database from a script without loading anything else, you can
  977. * include bootstrap.inc, and call drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE).
  978. *
  979. * @param $phase
  980. * A constant. Allowed values are:
  981. * DRUPAL_BOOTSTRAP_CONFIGURATION: initialize configuration.
  982. * DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE: try to call a non-database cache fetch routine.
  983. * DRUPAL_BOOTSTRAP_DATABASE: initialize database layer.
  984. * DRUPAL_BOOTSTRAP_ACCESS: identify and reject banned hosts.
  985. * DRUPAL_BOOTSTRAP_SESSION: initialize session handling.
  986. * DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: load bootstrap.inc and module.inc, start
  987. * the variable system and try to serve a page from the cache.
  988. * DRUPAL_BOOTSTRAP_LANGUAGE: identify the language used on the page.
  989. * DRUPAL_BOOTSTRAP_PATH: set $_GET['q'] to Drupal path of request.
  990. * DRUPAL_BOOTSTRAP_FULL: Drupal is fully loaded, validate and fix input data.
  991. */
  992. function drupal_bootstrap($phase) {
  993. static $phases = array(DRUPAL_BOOTSTRAP_CONFIGURATION, DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE, DRUPAL_BOOTSTRAP_DATABASE, DRUPAL_BOOTSTRAP_ACCESS, DRUPAL_BOOTSTRAP_SESSION, DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE, DRUPAL_BOOTSTRAP_LANGUAGE, DRUPAL_BOOTSTRAP_PATH, DRUPAL_BOOTSTRAP_FULL), $phase_index = 0;
  994. while ($phase >= $phase_index && isset($phases[$phase_index])) {
  995. $current_phase = $phases[$phase_index];
  996. unset($phases[$phase_index++]);
  997. _drupal_bootstrap($current_phase);
  998. }
  999. }
  1000. function _drupal_bootstrap($phase) {
  1001. global $conf;
  1002. switch ($phase) {
  1003. case DRUPAL_BOOTSTRAP_CONFIGURATION:
  1004. drupal_unset_globals();
  1005. // Start a page timer:
  1006. timer_start('page');
  1007. // Initialize the configuration
  1008. conf_init();
  1009. break;
  1010. case DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE:
  1011. // Allow specifying special cache handlers in settings.php, like
  1012. // using memcached or files for storing cache information.
  1013. require_once variable_get('cache_inc', './includes/cache.inc');
  1014. // If the page_cache_fastpath is set to TRUE in settings.php and
  1015. // page_cache_fastpath (implemented in the special implementation of
  1016. // cache.inc) printed the page and indicated this with a returned TRUE
  1017. // then we are done.
  1018. if (variable_get('page_cache_fastpath', FALSE) && page_cache_fastpath()) {
  1019. exit;
  1020. }
  1021. break;
  1022. case DRUPAL_BOOTSTRAP_DATABASE:
  1023. // Initialize the default database.
  1024. require_once './includes/database.inc';
  1025. db_set_active();
  1026. // Allow specifying alternate lock implementations in settings.php, like
  1027. // those using APC or memcached.
  1028. require_once variable_get('lock_inc', './includes/lock.inc');
  1029. lock_init();
  1030. break;
  1031. case DRUPAL_BOOTSTRAP_ACCESS:
  1032. // Deny access to hosts which were banned - t() is not yet available.
  1033. if (drupal_is_denied('host', ip_address())) {
  1034. header($_SERVER['SERVER_PROTOCOL'] .' 403 Forbidden');
  1035. print 'Sorry, '. check_plain(ip_address()) .' has been banned.';
  1036. exit();
  1037. }
  1038. break;
  1039. case DRUPAL_BOOTSTRAP_SESSION:
  1040. require_once variable_get('session_inc', './includes/session.inc');
  1041. session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy_sid', 'sess_gc');
  1042. session_start();
  1043. break;
  1044. case DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE:
  1045. // Initialize configuration variables, using values from settings.php if available.
  1046. $conf = variable_init(isset($conf) ? $conf : array());
  1047. $cache_mode = variable_get('cache', CACHE_DISABLED);
  1048. // Get the page from the cache.
  1049. $cache = $cache_mode == CACHE_DISABLED ? '' : page_get_cache();
  1050. // If the skipping of the bootstrap hooks is not enforced, call hook_boot.
  1051. if (!$cache || $cache_mode != CACHE_AGGRESSIVE) {
  1052. // Load module handling.
  1053. require_once './includes/module.inc';
  1054. bootstrap_invoke_all('boot');
  1055. }
  1056. // If there is a cached page, display it.
  1057. if ($cache) {
  1058. drupal_page_cache_header($cache);
  1059. // If the skipping of the bootstrap hooks is not enforced, call hook_exit.
  1060. if ($cache_mode != CACHE_AGGRESSIVE) {
  1061. bootstrap_invoke_all('exit');
  1062. }
  1063. // We are done.
  1064. exit;
  1065. }
  1066. // Prepare for non-cached page workflow.
  1067. drupal_page_header();
  1068. break;
  1069. case DRUPAL_BOOTSTRAP_LANGUAGE:
  1070. drupal_init_language();
  1071. break;
  1072. case DRUPAL_BOOTSTRAP_PATH:
  1073. require_once './includes/path.inc';
  1074. // Initialize $_GET['q'] prior to loading modules and invoking hook_init().
  1075. drupal_init_path();
  1076. break;
  1077. case DRUPAL_BOOTSTRAP_FULL:
  1078. require_once './includes/common.inc';
  1079. _drupal_bootstrap_full();
  1080. break;
  1081. }
  1082. }
  1083. /**
  1084. * Enables use of the theme system without requiring database access.
  1085. *
  1086. * Loads and initializes the theme system for site installs, updates and when
  1087. * the site is in off-line mode. This also applies when the database fails.
  1088. *
  1089. * @see _drupal_maintenance_theme()
  1090. */
  1091. function drupal_maintenance_theme() {
  1092. require_once './includes/theme.maintenance.inc';
  1093. _drupal_maintenance_theme();
  1094. }
  1095. /**
  1096. * Return the name of the localisation function. Use in code that needs to
  1097. * run both during installation and normal operation.
  1098. */
  1099. function get_t() {
  1100. static $t;
  1101. if (is_null($t)) {
  1102. $t = function_exists('install_main') ? 'st' : 't';
  1103. }
  1104. return $t;
  1105. }
  1106. /**
  1107. * Choose a language for the current page, based on site and user preferences.
  1108. */
  1109. function drupal_init_language() {
  1110. global $language, $user;
  1111. // Ensure the language is correctly returned, even without multilanguage support.
  1112. // Useful for eg. XML/HTML 'lang' attributes.
  1113. if (variable_get('language_count', 1) == 1) {
  1114. $language = language_default();
  1115. }
  1116. else {
  1117. include_once './includes/language.inc';
  1118. $language = language_initialize();
  1119. }
  1120. }
  1121. /**
  1122. * Get a list of languages set up indexed by the specified key
  1123. *
  1124. * @param $field The field to index the list with.
  1125. * @param $reset Boolean to request a reset of the list.
  1126. */
  1127. function language_list($field = 'language', $reset = FALSE) {
  1128. static $languages = NULL;
  1129. // Reset language list
  1130. if ($reset) {
  1131. $languages = NULL;
  1132. }
  1133. // Init language list
  1134. if (!isset($languages)) {
  1135. if (variable_get('language_count', 1) > 1 || module_exists('locale')) {
  1136. $result = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC');
  1137. while ($row = db_fetch_object($result)) {
  1138. $languages['language'][$row->language] = $row;
  1139. }
  1140. }
  1141. else {
  1142. // No locale module, so use the default language only.
  1143. $default = language_default();
  1144. $languages['language'][$default->language] = $default;
  1145. }
  1146. }
  1147. // Return the array indexed by the right field
  1148. if (!isset($languages[$field])) {
  1149. $languages[$field] = array();
  1150. foreach ($languages['language'] as $lang) {
  1151. // Some values should be collected into an array
  1152. if (in_array($field, array('enabled', 'weight'))) {
  1153. $languages[$field][$lang->$field][$lang->language] = $lang;
  1154. }
  1155. else {
  1156. $languages[$field][$lang->$field] = $lang;
  1157. }
  1158. }
  1159. }
  1160. return $languages[$field];
  1161. }
  1162. /**
  1163. * Default language used on the site
  1164. *
  1165. * @param $property
  1166. * Optional property of the language object to return
  1167. */
  1168. function language_default($property = NULL) {
  1169. $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' => ''));
  1170. return $property ? $language->$property : $language;
  1171. }
  1172. /**
  1173. * If Drupal is behind a reverse proxy, we use the X-Forwarded-For header
  1174. * instead of $_SERVER['REMOTE_ADDR'], which would be the IP address
  1175. * of the proxy server, and not the client's.
  1176. *
  1177. * @return
  1178. * IP address of client machine, adjusted for reverse proxy.
  1179. */
  1180. function ip_address() {
  1181. static $ip_address = NULL;
  1182. if (!isset($ip_address)) {
  1183. $ip_address = $_SERVER['REMOTE_ADDR'];
  1184. if (variable_get('reverse_proxy', 0) && array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
  1185. // If an array of known reverse proxy IPs is provided, then trust
  1186. // the XFF header if request really comes from one of them.
  1187. $reverse_proxy_addresses = variable_get('reverse_proxy_addresses', array());
  1188. if (!empty($reverse_proxy_addresses) && in_array($ip_address, $reverse_proxy_addresses, TRUE)) {
  1189. // If there are several arguments, we need to check the most
  1190. // recently added one, i.e. the last one.
  1191. $ip_address_parts = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  1192. $ip_address = array_pop($ip_address_parts);
  1193. }
  1194. }
  1195. }
  1196. return $ip_address;
  1197. }
  1198. /**
  1199. * Returns a URL-safe, base64 encoded string of highly randomized bytes (over the full 8-bit range).
  1200. *
  1201. * @param $byte_count
  1202. * The number of random bytes to fetch and base64 encode.
  1203. *
  1204. * @return string
  1205. * The base64 encoded result will have a length of up to 4 * $byte_count.
  1206. */
  1207. function drupal_random_key($byte_count = 32) {
  1208. return drupal_base64_encode(drupal_random_bytes($byte_count));
  1209. }
  1210. /**
  1211. * Returns a URL-safe, base64 encoded version of the supplied string.
  1212. *
  1213. * @param $string
  1214. * The string to convert to base64.
  1215. *
  1216. * @return string
  1217. */
  1218. function drupal_base64_encode($string) {
  1219. $data = base64_encode($string);
  1220. // Modify the output so it's safe to use in URLs.
  1221. return strtr($data, array('+' => '-', '/' => '_', '=' => ''));
  1222. }
  1223. /**
  1224. * Returns a string of highly randomized bytes (over the full 8-bit range).
  1225. *
  1226. * This function is better than simply calling mt_rand() or any other built-in
  1227. * PHP function because it can return a long string of bytes (compared to < 4
  1228. * bytes normally from mt_rand()) and uses the best available pseudo-random
  1229. * source.
  1230. *
  1231. * @param $count
  1232. * The number of characters (bytes) to return in the string.
  1233. */
  1234. function drupal_random_bytes($count) {
  1235. // $random_state does not use drupal_static as it stores random bytes.
  1236. static $random_state, $bytes, $has_openssl, $has_hash;
  1237. $missing_bytes = $count - strlen($bytes);
  1238. if ($missing_bytes > 0) {
  1239. // PHP versions prior 5.3.4 experienced openssl_random_pseudo_bytes()
  1240. // locking on Windows and rendered it unusable.
  1241. if (!isset($has_openssl)) {
  1242. $has_openssl = version_compare(PHP_VERSION, '5.3.4', '>=') && function_exists('openssl_random_pseudo_bytes');
  1243. }
  1244. // openssl_random_pseudo_bytes() will find entropy in a system-dependent
  1245. // way.
  1246. if ($has_openssl) {
  1247. $bytes .= openssl_random_pseudo_bytes($missing_bytes);
  1248. }
  1249. // Else, read directly from /dev/urandom, which is available on many *nix
  1250. // systems and is considered cryptographically secure.
  1251. elseif ($fh = @fopen('/dev/urandom', 'rb')) {
  1252. // PHP only performs buffered reads, so in reality it will always read
  1253. // at least 4096 bytes. Thus, it costs nothing extra to read and store
  1254. // that much so as to speed any additional invocations.
  1255. $bytes .= fread($fh, max(4096, $missing_bytes));
  1256. fclose($fh);
  1257. }
  1258. // If we couldn't get enough entropy, this simple hash-based PRNG will
  1259. // generate a good set of pseudo-random bytes on any system.
  1260. // Note that it may be important that our $random_state is passed
  1261. // through hash() prior to being rolled into $output, that the two hash()
  1262. // invocations are different, and that the extra input into the first one -
  1263. // the microtime() - is prepended rather than appended. This is to avoid
  1264. // directly leaking $random_state via the $output stream, which could
  1265. // allow for trivial prediction of further "random" numbers.
  1266. if (strlen($bytes) < $count) {
  1267. // Initialize on the first call. The contents of $_SERVER includes a mix of
  1268. // user-specific and system information that varies a little with each page.
  1269. if (!isset($random_state)) {
  1270. $random_state = print_r($_SERVER, TRUE);
  1271. if (function_exists('getmypid')) {
  1272. // Further initialize with the somewhat random PHP process ID.
  1273. $random_state .= getmypid();
  1274. }
  1275. // hash() is only available in PHP 5.1.2+ or via PECL.
  1276. $has_hash = function_exists('hash') && in_array('sha256', hash_algos());
  1277. $bytes = '';
  1278. }
  1279. if ($has_hash) {
  1280. do {
  1281. $random_state = hash('sha256', microtime() . mt_rand() . $random_state);
  1282. $bytes .= hash('sha256', mt_rand() . $random_state, TRUE);
  1283. } while (strlen($bytes) < $count);
  1284. }
  1285. else {
  1286. do {
  1287. $random_state = md5(microtime() . mt_rand() . $random_state);
  1288. $bytes .= pack("H*", md5(mt_rand() . $random_state));
  1289. } while (strlen($bytes) < $count);
  1290. }
  1291. }
  1292. }
  1293. $output = substr($bytes, 0, $count);
  1294. $bytes = substr($bytes, $count);
  1295. return $output;
  1296. }