[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/dtools/wsod/ -> wsod_emergency.php (source)

   1  <?php
   2  /*
   3      version: $Id: wsod_emergency.php,v 1.1.2.6 2009/06/22 12:18:40 kenorb Exp $
   4      author: kenorb@gmail.com
   5  */
   6  
   7  $verbose = TRUE;
   8  $fix_on_fly  = TRUE;
   9  $output = '';
  10  
  11  require_once  './wsod.module'; // include functions
  12  
  13  $drupal_path = wsod_detect_drupal_path($output);
  14  if (!empty($drupal_path)) {
  15      chdir($drupal_path); // change dir to Drupal path
  16  } else {
  17      $verbose ? print "Fatal error: Couldn't detect your Drupal installation in:<br>\n".$output : NULL;
  18      $verbose ? print "Move this module inside your Drupal installation!": NULL;
  19      exit;
  20  }
  21  
  22  /* simulate Drupal boot */
  23  require_once  './includes/bootstrap.inc'; // load bootstrap file
  24  require_once  './includes/menu.inc'; // load menu file (needed for Menu flags)
  25  wsod_drupal_bootstrap_run(DRUPAL_BOOTSTRAP_FULL); // execute WSOD version of Drupal bootstrap
  26  drupal_page_footer();
  27  
  28  /* enable wsod module */
  29  if (!module_exists('wsod')) {
  30      module_enable(array('wsod')); // enable wsod module if it's not enabled
  31  }
  32  if (!function_exists('wsod_check_wsod')) { // if wsod testing function doesn't exist, there is some path problem
  33      module_rebuild_cache(); // so we need to rebuild path of modules
  34      drupal_load('module', 'wsod'); // load module files again
  35  } // if this will not help, please check manually in your filesystem if you don't have duplicated wsod modules
  36  
  37  wsod_check_wsod($verbose, $fix_on_fly, TRUE); // run diagnostic
  38  
  39  /**
  40   * WSOD version of Drupal drupal_bootstrap()
  41   */
  42  function wsod_drupal_bootstrap_run($phase) {
  43    drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
  44    drupal_bootstrap(DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE);
  45    drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  46    drupal_bootstrap(DRUPAL_BOOTSTRAP_ACCESS);
  47    wsod_drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); // replace handler sess_close() by WSOD
  48    drupal_bootstrap(DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE);
  49    drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
  50    drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
  51    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  52  }
  53  
  54  /**
  55   * WSOD version of Drupal _drupal_bootstrap()
  56   *
  57   * Modifications:
  58   * - replace handler sess_close() by WSOD
  59   *   Some fatal errors can't be handled by set_error_handler(), but can be handled by session_set_save_handler()
  60   *
  61   * @param $phase
  62   *   A constant. Allowed values are:
  63   *     DRUPAL_BOOTSTRAP_CONFIGURATION: initialize configuration.
  64   *     DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE: try to call a non-database cache fetch routine.
  65   *     DRUPAL_BOOTSTRAP_DATABASE: initialize database layer.
  66   *     DRUPAL_BOOTSTRAP_ACCESS: identify and reject banned hosts.
  67   *     DRUPAL_BOOTSTRAP_SESSION: initialize session handling.
  68   *     DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: load bootstrap.inc and module.inc, start
  69   *       the variable system and try to serve a page from the cache.
  70   *     DRUPAL_BOOTSTRAP_LANGUAGE: identify the language used on the page.
  71   *     DRUPAL_BOOTSTRAP_PATH: set $_GET['q'] to Drupal path of request.
  72   *     DRUPAL_BOOTSTRAP_FULL: Drupal is fully loaded, validate and fix input data.
  73   */
  74  function wsod_drupal_bootstrap($phase) {
  75    global $conf;
  76  
  77    switch ($phase) {
  78  
  79      case DRUPAL_BOOTSTRAP_CONFIGURATION:
  80        drupal_unset_globals();
  81        // Start a page timer:
  82        timer_start('page');
  83        // Initialize the configuration
  84        conf_init();
  85        break;
  86  
  87      case DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE:
  88        // Allow specifying special cache handlers in settings.php, like
  89        // using memcached or files for storing cache information.
  90        require_once variable_get('cache_inc', './includes/cache.inc');
  91        // If the page_cache_fastpath is set to TRUE in settings.php and
  92        // page_cache_fastpath (implemented in the special implementation of
  93        // cache.inc) printed the page and indicated this with a returned TRUE
  94        // then we are done.
  95        if (variable_get('page_cache_fastpath', FALSE) && page_cache_fastpath()) {
  96          exit;
  97        }
  98        break;
  99  
 100      case DRUPAL_BOOTSTRAP_DATABASE:
 101        // Initialize the default database.
 102        require_once  './includes/database.inc';
 103        db_set_active();
 104        break;
 105  
 106      case DRUPAL_BOOTSTRAP_ACCESS:
 107        // Deny access to hosts which were banned - t() is not yet available.
 108        if (drupal_is_denied('host', ip_address())) {
 109          header('HTTP/1.1 403 Forbidden');
 110          print 'Sorry, '. check_plain(ip_address()) .' has been banned.';
 111          exit();
 112        }
 113        break;
 114  
 115      case DRUPAL_BOOTSTRAP_SESSION:
 116        require_once variable_get('session_inc', './includes/session.inc');
 117        session_set_save_handler('sess_open', 'wsod_sess_close', 'sess_read', 'sess_write', 'sess_destroy_sid', 'sess_gc'); // WSOD: replace wsod handle for sess_close()
 118        if(!isset($_SESSION)){ // WSOD: additional condition if session has been already started
 119            session_start();
 120        }
 121        break;
 122  
 123      case DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE:
 124        // Initialize configuration variables, using values from settings.php if available.
 125        $conf = variable_init(isset($conf) ? $conf : array());
 126        $cache_mode = variable_get('cache', CACHE_DISABLED);
 127        // Get the page from the cache.
 128        $cache = $cache_mode == CACHE_DISABLED ? '' : page_get_cache();
 129        // If the skipping of the bootstrap hooks is not enforced, call hook_boot.
 130        if (!$cache || $cache_mode != CACHE_AGGRESSIVE) {
 131          // Load module handling.
 132          require_once  './includes/module.inc';
 133          bootstrap_invoke_all('boot');
 134        }
 135        // If there is a cached page, display it.
 136        if ($cache) {
 137          drupal_page_cache_header($cache);
 138          // If the skipping of the bootstrap hooks is not enforced, call hook_exit.
 139          if ($cache_mode != CACHE_AGGRESSIVE) {
 140            bootstrap_invoke_all('exit');
 141          }
 142          // We are done.
 143          exit;
 144        }
 145        // Prepare for non-cached page workflow.
 146        drupal_page_header();
 147        break;
 148  
 149      case DRUPAL_BOOTSTRAP_LANGUAGE:
 150        drupal_init_language();
 151        break;
 152  
 153      case DRUPAL_BOOTSTRAP_PATH:
 154        require_once  './includes/path.inc';
 155        // Initialize $_GET['q'] prior to loading modules and invoking hook_init().
 156        drupal_init_path();
 157        break;
 158  
 159      case DRUPAL_BOOTSTRAP_FULL:
 160        require_once  './includes/common.inc';
 161        _drupal_bootstrap_full();
 162        break;
 163    }
 164  }
 165  
 166  /** 
 167   * Detect Drupal path by scanning each level of current path
 168   * 
 169   */ 
 170  function wsod_detect_drupal_path(&$output, $start_dir = __FILE__) {
 171      $drupal_path = FALSE; // set init variable
 172      $path_arr = wsod_pathposall(dirname($start_dir));
 173      $bootstrap_file = '/includes/bootstrap.inc';
 174      $buff_output = '';
 175      foreach ($path_arr as $path) {
 176          if (file_exists($path.$bootstrap_file)) {
 177              $drupal_path = $path;
 178              return $drupal_path;
 179          } else {
 180              $buff_output .= "Couldn't find $path$bootstrap_file!<br>\n";
 181          }
 182          // FIXME: file_exists() [function.file-exists]: open_basedir restriction in effect. File(//includes/bootstrap.inc) is not within the allowed path(s)
 183      }
 184      $output .= $buff_output;
 185      return NULL;
 186  }
 187  
 188  /** 
 189   * pathposall 
 190   * 
 191   * Find all occurrences of a subdirs in a path
 192   * 
 193   * @param string $dir
 194   * @return array
 195   */ 
 196  function wsod_pathposall($dir){ 
 197      $slash = wsod_detect_filesystem_slash();
 198      $pos = wsod_strposall($dir,$slash);
 199      foreach ($pos as $no) {
 200          $subdir[] = substr($dir, 0, $no);
 201      }
 202      if (is_array($subdir)) {
 203          rsort($subdir); // make list in reverse
 204          return $subdir; // return array of available subdir paths
 205      } else {
 206          return array();
 207      }
 208  } 
 209  
 210  /** 
 211   * Detect slash type of filesystem
 212   * 
 213   */ 
 214  function wsod_detect_filesystem_slash() {
 215      if (strpos(__FILE__, '/') !== FALSE) { // detect filesystem
 216          return '/'; // Unix style
 217      } else {
 218          return '\\'; // Windows style
 219      }
 220  }
 221  
 222  /** 
 223   * Find all occurrences of a needle in a haystack 
 224   * 
 225   * @param string $haystack 
 226   * @param string $needle 
 227   * @return array or false 
 228   */ 
 229  function wsod_strposall($haystack,$needle){ 
 230      $s=0; $i=0; 
 231      while (is_integer($i)){ 
 232          $i = strpos($haystack,$needle,$s); 
 233          if (is_integer($i)) { 
 234              $aStrPos[] = $i; 
 235              $s = $i+strlen($needle); 
 236          } 
 237      } 
 238      if (isset($aStrPos)) { 
 239          return $aStrPos; 
 240      } else { 
 241          return false; 
 242      } 
 243  } 
 244  
 245  /**
 246   * Get content type of page header 
 247   * 
 248   * @return string
 249   * 
 250   */
 251  function wsod_sess_close() {
 252    wsod_check_wsod(TRUE);
 253    return TRUE;
 254  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7