PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tenants/apps/samlservice/deploy-samlservice/samlservice/lib/SimpleSAML/Error/Error.php

https://github.com/hpgihan/cronus
PHP | 174 lines | 89 code | 31 blank | 54 comment | 12 complexity | 80363c1f131069e34fb791c601ab4291 MD5 | raw file
  1. <?php
  2. /**
  3. * Class which wraps simpleSAMLphp errors in exceptions.
  4. *
  5. * @author Olav Morken, UNINETT AS.
  6. * @package simpleSAMLphp
  7. * @version $Id$
  8. */
  9. class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
  10. /**
  11. * The error code.
  12. *
  13. * @var string
  14. */
  15. private $errorCode;
  16. /**
  17. * The parameters for the error.
  18. *
  19. * @var array
  20. */
  21. private $parameters;
  22. /**
  23. * Constructor for this error.
  24. *
  25. * The error can either be given as a string, or as an array. If it is an array, the
  26. * first element in the array (with index 0), is the error code, while the other elements
  27. * are replacements for the error text.
  28. *
  29. * @param mixed $errorCode One of the error codes defined in the errors dictionary.
  30. * @param Exception $cause The exception which caused this fatal error (if any).
  31. */
  32. public function __construct($errorCode, Exception $cause = NULL) {
  33. assert('is_string($errorCode) || is_array($errorCode)');
  34. if (is_array($errorCode)) {
  35. $this->parameters = $errorCode;
  36. unset($this->parameters[0]);
  37. $this->errorCode = $errorCode[0];
  38. } else {
  39. $this->parameters = array();
  40. $this->errorCode = $errorCode;
  41. }
  42. if (!empty($this->parameters)) {
  43. $msg = $this->errorCode . '(';
  44. foreach ($this->parameters as $k => $v) {
  45. if ($k === 0) {
  46. continue;
  47. }
  48. $msg .= var_export($k, TRUE) . ' => ' . var_export($v, TRUE) . ', ';
  49. }
  50. $msg = substr($msg, 0, -2) . ')';
  51. } else {
  52. $msg = $this->errorCode;
  53. }
  54. parent::__construct($msg, -1, $cause);
  55. }
  56. /**
  57. * Retrieve the error code given when throwing this error.
  58. *
  59. * @return string The error code.
  60. */
  61. public function getErrorCode() {
  62. return $this->errorCode;
  63. }
  64. /**
  65. * Set the HTTP return code for this error.
  66. *
  67. * This should be overridden by subclasses who want a different return code than 500 Internal Server Error.
  68. */
  69. protected function setHTTPCode() {
  70. header('HTTP/1.0 500 Internal Server Error');
  71. }
  72. /**
  73. * Save an error report.
  74. *
  75. * @return array The array with the error report data.
  76. */
  77. protected function saveError() {
  78. $data = $this->format();
  79. $emsg = array_shift($data);
  80. $etrace = implode("\n", $data);
  81. $reportId = SimpleSAML_Utilities::stringToHex(SimpleSAML_Utilities::generateRandomBytes(4));
  82. SimpleSAML_Logger::error('Error report with id ' . $reportId . ' generated.');
  83. $config = SimpleSAML_Configuration::getInstance();
  84. $session = SimpleSAML_Session::getInstance();
  85. if (isset($_SERVER['HTTP_REFERER'])) {
  86. $referer = $_SERVER['HTTP_REFERER'];
  87. /*
  88. * Remove anything after the first '?' or ';', just
  89. * in case it contains any sensitive data.
  90. */
  91. $referer = explode('?', $referer, 2);
  92. $referer = $referer[0];
  93. $referer = explode(';', $referer, 2);
  94. $referer = $referer[0];
  95. } else {
  96. $referer = 'unknown';
  97. }
  98. $errorData = array(
  99. 'exceptionMsg' => $emsg,
  100. 'exceptionTrace' => $etrace,
  101. 'reportId' => $reportId,
  102. 'trackId' => $session->getTrackID(),
  103. 'url' => SimpleSAML_Utilities::selfURLNoQuery(),
  104. 'version' => $config->getVersion(),
  105. 'referer' => $referer,
  106. );
  107. $session->setData('core:errorreport', $reportId, $errorData);
  108. return $errorData;
  109. }
  110. /**
  111. * Display this error.
  112. *
  113. * This method displays a standard simpleSAMLphp error page and exits.
  114. */
  115. public function show() {
  116. $this->setHTTPCode();
  117. /* Log the error message. */
  118. $this->logError();
  119. $errorData = $this->saveError();
  120. $config = SimpleSAML_Configuration::getInstance();
  121. $t = new SimpleSAML_XHTML_Template($config, 'error.php', 'errors');
  122. $t->data['showerrors'] = $config->getBoolean('showerrors', true);
  123. $t->data['error'] = $errorData;
  124. $t->data['errorCode'] = $this->errorCode;
  125. $t->data['parameters'] = $this->parameters;
  126. /* Check if there is a valid technical contact email address. */
  127. if($config->getString('technicalcontact_email', 'na@example.org') !== 'na@example.org') {
  128. /* Enable error reporting. */
  129. $baseurl = SimpleSAML_Utilities::getBaseURL();
  130. $t->data['errorReportAddress'] = $baseurl . 'errorreport.php';
  131. }
  132. $session = SimpleSAML_Session::getInstance();
  133. $attributes = $session->getAttributes();
  134. if (is_array($attributes) && array_key_exists('mail', $attributes) && count($attributes['mail']) > 0) {
  135. $email = $attributes['mail'][0];
  136. } else {
  137. $email = '';
  138. }
  139. $t->data['email'] = $email;
  140. $t->show();
  141. exit;
  142. }
  143. }