CRM-14356 - CRM_Core_Session - Guard against unset value
[civicrm-core.git] / CRM / Core / Error.php
index 409a2469bc6ba3ee5d156d30f54fac659cc930a5..251633afd02e6a81897ec0f5865014a72efb8c1b 100644 (file)
@@ -2,7 +2,7 @@
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -38,6 +38,7 @@
 
 require_once 'PEAR/ErrorStack.php';
 require_once 'PEAR/Exception.php';
+require_once 'CRM/Core/Exception.php';
 
 require_once 'Log.php';
 class CRM_Exception extends PEAR_Exception {
@@ -105,7 +106,7 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     $this->setDefaultCallback(array($this, 'handlePES'));
   }
 
-  function getMessages(&$error, $separator = '<br />') {
+  static public function getMessages(&$error, $separator = '<br />') {
     if (is_a($error, 'CRM_Core_Error')) {
       $errors = $error->getErrors();
       $message = array();
@@ -203,7 +204,11 @@ class CRM_Core_Error extends PEAR_ErrorStack {
       echo "Sorry. A non-recoverable error has occurred. The error trace below might help to resolve the issue<p>";
       CRM_Core_Error::debug(NULL, $error);
     }
-
+    static $runOnce = FALSE;
+    if ($runOnce) {
+      exit;
+    }
+    $runOnce = TRUE;
     self::abend(1);
   }
 
@@ -223,7 +228,9 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     $error['user_info']  = $pearError->getUserInfo();
     $error['to_string']  = $pearError->toString();
 
-    CRM_Core_Error::debug('Initialization Error', $error);
+    // ensure that debug does not check permissions since we are in bootstrap
+    // mode and need to print a decent message to help the user
+    CRM_Core_Error::debug('Initialization Error', $error, TRUE, TRUE, FALSE);
 
     // always log the backtrace to a file
     self::backtrace('backTrace', TRUE);
@@ -314,7 +321,13 @@ class CRM_Core_Error extends PEAR_ErrorStack {
     CRM_Core_Error::debug_var('Fatal Error Details', $vars);
     CRM_Core_Error::backtrace('backTrace', TRUE);
     $content = $template->fetch($config->fatalErrorTemplate);
-    if ($config->userFramework == 'Joomla' && class_exists('JError')) {
+    // JErrorPage exists only in 3.x and not 2.x
+    // CRM-13714
+    if ($config->userFramework == 'Joomla' && class_exists('JErrorPage')) {
+      $error = new Exception($content);
+      JErrorPage::render($error);
+    }
+    else if ($config->userFramework == 'Joomla' && class_exists('JError')) {
       JError::raiseError('CiviCRM-001', $content);
     }
     else {
@@ -405,12 +418,15 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @param  mixed  reference to variables that we need a trace of
    * @param  bool   should we log or return the output
    * @param  bool   whether to generate a HTML-escaped output
+   * @param  bool   should we check permissions before displaying output
+   *                useful when we die during initialization and permissioning
+   *                subsystem is not initialized - CRM-13765
    *
    * @return string the generated output
    * @access public
    * @static
    */
-  static function debug($name, $variable = NULL, $log = TRUE, $html = TRUE) {
+  static function debug($name, $variable = NULL, $log = TRUE, $html = TRUE, $checkPermission = TRUE) {
     $error = self::singleton();
 
     if ($variable === NULL) {
@@ -433,7 +449,10 @@ class CRM_Core_Error extends PEAR_ErrorStack {
       }
       $out = "{$prefix}$out\n";
     }
-    if ($log && CRM_Core_Permission::check('view debug output')) {
+    if (
+      $log &&
+      (!$checkPermission || CRM_Core_Permission::check('view debug output'))
+    ) {
       echo $out;
     }
 
@@ -717,12 +736,12 @@ class CRM_Core_Error extends PEAR_ErrorStack {
    * @access public
    * @static
    */
-  public static function statusBounce($status, $redirect = NULL) {
+  public static function statusBounce($status, $redirect = NULL, $title = '') {
     $session = CRM_Core_Session::singleton();
     if (!$redirect) {
       $redirect = $session->readUserContext();
     }
-    $session->setStatus($status);
+    $session->setStatus($status, $title);
     CRM_Utils_System::redirect($redirect);
   }