/components/com_easyblog/classes/exceptional/data.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 125 lines · 84 code · 23 blank · 18 comment · 15 complexity · 37f6f33906e6efa5ff752c9ee320d580 MD5 · raw file

  1. <?php
  2. /**
  3. * @package EasyBlog
  4. * @copyright Copyright (C) 2010 Stack Ideas Private Limited. All rights reserved.
  5. * @license GNU/GPL, see LICENSE.php
  6. *
  7. * EasyBlog is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. defined('_JEXEC') or die('Restricted access');
  14. class ExceptionalData {
  15. protected $exception;
  16. protected $backtrace = array();
  17. function __construct(Exception $exception) {
  18. $this->exception = $exception;
  19. $trace = $this->exception->getTrace();
  20. foreach ($trace as $t) {
  21. if (!isset($t["file"])) continue;
  22. $this->backtrace[] = "$t[file]:$t[line]:in `$t[function]\'";
  23. }
  24. // environment data
  25. $data = ExceptionalEnvironment::to_array();
  26. // exception data
  27. $message = $this->exception->getMessage();
  28. $now = date("c");
  29. // spoof 404 error
  30. $error_class = get_class($this->exception);
  31. if ($error_class == "Http404Error") {
  32. $error_class = "ActionController::UnknownAction";
  33. }
  34. $data["exception"] = array(
  35. "exception_class" => $error_class,
  36. "message" => $message,
  37. "backtrace" => $this->backtrace,
  38. "occurred_at" => $now
  39. );
  40. // context
  41. $context = Exceptional::$context;
  42. if (!empty($context)) {
  43. $data["context"] = $context;
  44. }
  45. if (isset($_SERVER["HTTP_HOST"])) {
  46. // request data
  47. $session = isset($_SESSION) ? $_SESSION : array();
  48. // sanitize headers
  49. $headers = getallheaders();
  50. if (isset($headers["Cookie"])) {
  51. $sessionKey = preg_quote(ini_get("session.name"), "/");
  52. $headers["Cookie"] = preg_replace("/$sessionKey=\S+/", "$sessionKey=[FILTERED]", $headers["Cookie"]);
  53. }
  54. $server = $_SERVER;
  55. $keys = array("HTTPS", "HTTP_HOST", "REQUEST_URI", "REQUEST_METHOD", "REMOTE_ADDR");
  56. $this->fill_keys($server, $keys);
  57. $protocol = $server["HTTPS"] && $server["HTTPS"] != "off" ? "https://" : "http://";
  58. $url = $server["HTTP_HOST"] ? "$protocol$server[HTTP_HOST]$server[REQUEST_URI]" : "";
  59. $data["request"] = array(
  60. "url" => $url,
  61. "request_method" => strtolower($server["REQUEST_METHOD"]),
  62. "remote_ip" => $server["REMOTE_ADDR"],
  63. "headers" => $headers,
  64. "session" => $session
  65. );
  66. if (!empty(Exceptional::$controller) && !empty(Exceptional::$action)) {
  67. $data["request"]["controller"] = Exceptional::$controller;
  68. $data["request"]["action"] = Exceptional::$action;
  69. }
  70. $params = array_merge($_GET, $_POST);
  71. if (!empty($params)) {
  72. $data["request"]["parameters"] = $params;
  73. }
  74. }
  75. $this->data = $data;
  76. }
  77. function uniqueness_hash() {
  78. return md5(implode("", $this->backtrace));
  79. }
  80. function to_json() {
  81. return json_encode($this->data);
  82. }
  83. function fill_keys(&$arr, $keys) {
  84. foreach ($keys as $key) {
  85. if (!isset($arr[$key])) {
  86. $arr[$key] = false;
  87. }
  88. }
  89. }
  90. }
  91. // http://php.net/manual/en/function.getallheaders.php
  92. if (!function_exists("getallheaders")) {
  93. function getallheaders() {
  94. $headers = array();
  95. foreach ($_SERVER as $name => $value) {
  96. if (substr($name, 0, 5) == "HTTP_") {
  97. $headers[str_replace(" ", "-", ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  98. }
  99. }
  100. return $headers;
  101. }
  102. }