@@ -566,13 +566,21 @@ function drupal_get_filename($type, $name, $filename = NULL) {
566566 * file.
567567 */
568568function variable_init ($ conf = array (), $ regenerate = FALSE , $ recursion_depth = 0 ) {
569- // NOTE: caching the variables improves performance by 20% when serving cached pages.
569+ // NOTE: caching the variables improves performance by 20% when serving
570+ // cached pages.
570571 if (!$ regenerate && $ cached = cache_get ('variables ' , 'cache ' )) {
571572 $ variables = $ cached ->data ;
572573 }
573574 else {
574575 if (defined ('MAINTENANCE_MODE ' ) || lock_acquire ('variable_cache_regenerate ' )) {
575576 $ result = db_query ('SELECT * FROM {variable} ' );
577+ // Exit here if the database went away. Do not want to pollute the cache
578+ // with bad data. This request isn't going to end well anyway; end it
579+ // eairly.
580+ if ($ result === FALSE ) {
581+ // This function calls exit.
582+ _db_error_page ();
583+ }
576584 while ($ variable = db_fetch_object ($ result )) {
577585 $ variables [$ variable ->name ] = unserialize ($ variable ->value );
578586 }
@@ -585,15 +593,27 @@ function variable_init($conf = array(), $regenerate = FALSE, $recursion_depth =
585593 // Wait for another request that is already doing this work.
586594 lock_wait ('variable_cache_regenerate ' );
587595
588- // Run the function again. Try a limited number of times to avoid
589- // infinite recursion if the database connection is invalid for
596+ // Run the function again. Try a limited number of times to avoid
597+ // infinite recursion if the database connection is invalid for
590598 // some reason, e.g., mysqld restart, loss of network, etc.
591599 $ recursion_depth ++;
592600 if ($ recursion_depth < 50 ) {
593601 return variable_init ($ conf , $ regenerate , $ recursion_depth );
594602 }
595603
596- $ variables = array ();
604+ // If the recursion_depth hit the limit, assume we aren't going to get it
605+ // from the cache or the lock will be released any time soon. Give up and
606+ // get variables from the database.
607+ $ result = db_query ('SELECT * FROM {variable} ' );
608+ // Exit here if the database went away. Do not want to pollute the cache
609+ // with bad data. This request isn't going to end well.
610+ if ($ result === FALSE ) {
611+ // This function calls exit.
612+ _db_error_page ();
613+ }
614+ while ($ variable = db_fetch_object ($ result )) {
615+ $ variables [$ variable ->name ] = unserialize ($ variable ->value );
616+ }
597617 }
598618 }
599619
@@ -658,7 +678,7 @@ function variable_set($name, $value) {
658678 if (is_string ($ db_prefix ) && strpos ($ db_prefix , 'simpletest ' ) === 0 ) {
659679 cache_clear_all ('variables ' , 'cache ' );
660680 }
661-
681+
662682 variable_cache_rebuild ();
663683}
664684
@@ -687,7 +707,7 @@ function variable_del($name) {
687707 if (is_string ($ db_prefix ) && strpos ($ db_prefix , 'simpletest ' ) === 0 ) {
688708 cache_clear_all ('variables ' , 'cache ' );
689709 }
690-
710+
691711 variable_cache_rebuild ();
692712}
693713
@@ -814,7 +834,7 @@ function drupal_set_header($name = NULL, $value = NULL, $append = FALSE) {
814834 if (!isset ($ name )) {
815835 return $ headers ;
816836 }
817-
837+
818838 // Support the Drupal 6 header API
819839 if (!isset ($ value )) {
820840 if (strpos ($ name , ': ' ) !== FALSE ) {
@@ -1416,7 +1436,7 @@ function drupal_bootstrap($phase = NULL) {
14161436 _drupal_bootstrap ($ current_phase );
14171437 }
14181438 }
1419-
1439+
14201440 return $ phase_index ;
14211441}
14221442
@@ -1476,7 +1496,7 @@ function _drupal_bootstrap($phase) {
14761496 // those using APC or memcached.
14771497 require_once DRUPAL_ROOT . '/ ' . variable_get ('lock_inc ' , 'includes/lock.inc ' );
14781498 lock_init ();
1479-
1499+
14801500 // Detect if an installation is present.
14811501 detect_installation_or_run_installer ();
14821502
@@ -1527,11 +1547,11 @@ function _drupal_bootstrap($phase) {
15271547 // We are done.
15281548 exit ;
15291549 }
1530-
1550+
15311551 if (!$ cache && drupal_page_is_cacheable () && $ cache_mode != CACHE_EXTERNAL ) {
15321552 header ('X-Drupal-Cache: MISS ' );
15331553 }
1534-
1554+
15351555 // If using an external cache and the page is cacheable, set headers.
15361556 if ($ cache_mode == CACHE_EXTERNAL && drupal_page_is_cacheable ()) {
15371557 drupal_page_cache_header_external ();
@@ -1672,17 +1692,17 @@ function ip_address() {
16721692
16731693 if (!isset ($ ip_address )) {
16741694 $ ip_address = $ _SERVER ['REMOTE_ADDR ' ];
1675-
1695+
16761696 // Only use parts of the X-Forwarded-For (XFF) header that have followed a trusted route.
16771697 // Specifically, identify the leftmost IP address in the XFF header that is not one of ours.
16781698 // An XFF header is: X-Forwarded-For: client1, proxy1, proxy2
16791699 if (isset ($ _SERVER ['HTTP_ ' . variable_get ('x_forwarded_for_header ' , 'X_FORWARDED_FOR ' )]) && variable_get ('reverse_proxy ' , 0 )) {
16801700 // Load trusted reverse proxy server IPs.
16811701 $ reverse_proxy_addresses = variable_get ('reverse_proxy_addresses ' , array ());
1682-
1702+
16831703 // Turn XFF header into an array.
16841704 $ forwarded = explode (', ' , $ _SERVER ['HTTP_ ' . variable_get ('x_forwarded_for_header ' , 'X_FORWARDED_FOR ' )]);
1685-
1705+
16861706 // Trim the forwarded IPs; they may have been delimited by commas and spaces.
16871707 $ forwarded = array_map ('trim ' , $ forwarded );
16881708
@@ -1691,7 +1711,7 @@ function ip_address() {
16911711
16921712 // Eliminate all trusted IPs.
16931713 $ untrusted = array_diff ($ forwarded , $ reverse_proxy_addresses );
1694-
1714+
16951715 // The right-most IP is the most specific we can trust.
16961716 $ ip_address = array_pop ($ untrusted );
16971717 }
@@ -1705,7 +1725,7 @@ function ip_address() {
17051725 */
17061726function drupal_session_initialize () {
17071727 global $ user ;
1708-
1728+
17091729 session_set_save_handler ('sess_open ' , 'sess_close ' , 'sess_read ' , 'sess_write ' , 'sess_destroy_sid ' , 'sess_gc ' );
17101730
17111731 if (isset ($ _COOKIE [session_name ()])) {
@@ -1769,7 +1789,7 @@ function drupal_session_commit() {
17691789
17701790/**
17711791 * Return whether a session has been started.
1772- */
1792+ */
17731793function drupal_session_started ($ set = NULL ) {
17741794 static $ session_started = FALSE ;
17751795 if (isset ($ set )) {
@@ -1830,7 +1850,7 @@ function drupal_save_session($status = NULL) {
18301850 }
18311851 return $ save_session ;
18321852}
1833-
1853+
18341854/**
18351855 * Returns the current bootstrap phase for this Drupal process.
18361856 *
@@ -1875,7 +1895,7 @@ function drupal_generate_test_ua($prefix) {
18751895 // check the HMAC before the database is initialized. filectime()
18761896 // and fileinode() are not easily determined from remote.
18771897// $filepath = DRUPAL_ROOT . '/includes/bootstrap.inc';
1878- $ filepath = './includes/bootstrap.inc ' ;
1898+ $ filepath = './includes/bootstrap.inc ' ;
18791899// $key = sha1(serialize($databases) . filectime($filepath) . fileinode($filepath), TRUE);
18801900 $ key = sha1 (serialize ($ db_url ) . filectime ($ filepath ) . fileinode ($ filepath ), TRUE );
18811901 }
@@ -1903,7 +1923,7 @@ function drupal_is_cli() {
19031923 */
19041924function drupal_session_destroy () {
19051925 session_destroy ();
1906-
1926+
19071927 // Workaround PHP 5.2 fatal error "Failed to initialize storage module".
19081928 // @see http://bugs.php.net/bug.php?id=32330
19091929 session_set_save_handler ('sess_open ' , 'sess_close ' , 'sess_read ' , 'sess_write ' , 'sess_destroy_sid ' , 'sess_gc ' );
0 commit comments