Merge pull request #13985 from mauriciofauth/error-report

Refactor PhpMyAdmin\ErrorReport class
This commit is contained in:
Maurício Meneghini Fauth 2018-02-10 00:18:17 -02:00 committed by GitHub
commit 37c2a0ec96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 94 additions and 77 deletions

View File

@ -6,8 +6,10 @@
* @package PhpMyAdmin
*/
use PhpMyAdmin\ErrorReport;
use PhpMyAdmin\Message;
use PhpMyAdmin\Response;
use PhpMyAdmin\UserPreferences;
use PhpMyAdmin\Utils\HttpRequest;
require_once 'libraries/common.inc.php';
@ -19,6 +21,8 @@ if (!isset($_REQUEST['exception_type'])
$response = Response::getInstance();
$errorReport = new ErrorReport(new HttpRequest());
if (isset($_REQUEST['send_error_report'])
&& ($_REQUEST['send_error_report'] == true
|| $_REQUEST['send_error_report'] == '1')
@ -47,10 +51,10 @@ if (isset($_REQUEST['send_error_report'])
);
}
}
$reportData = ErrorReport::getReportData($_REQUEST['exception_type']);
$reportData = $errorReport->getData($_REQUEST['exception_type']);
// report if and only if there were 'actual' errors.
if (count($reportData) > 0) {
$server_response = ErrorReport::send($reportData);
$server_response = $errorReport->send($reportData);
if ($server_response === false) {
$success = false;
} else {
@ -87,9 +91,9 @@ if (isset($_REQUEST['send_error_report'])
/* Create message object */
if ($success) {
$msg = PhpMyAdmin\Message::notice($msg);
$msg = Message::notice($msg);
} else {
$msg = PhpMyAdmin\Message::error($msg);
$msg = Message::error($msg);
}
/* Add message to response */
@ -122,7 +126,7 @@ if (isset($_REQUEST['send_error_report'])
$response->addJSON('report_setting', $GLOBALS['cfg']['SendErrorReports']);
} else {
if ($_REQUEST['exception_type'] == 'js') {
$response->addHTML(ErrorReport::getForm());
$response->addHTML($errorReport->getForm());
} else {
// clear previous errors & save new ones.
$GLOBALS['error_handler']->savePreviousErrors();

View File

@ -1,7 +1,7 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Error reporting functions used to generate and submit error reports
* Holds the PhpMyAdmin\ErrorReport class
*
* @package PhpMyAdmin
*/
@ -10,87 +10,103 @@ namespace PhpMyAdmin;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\Util;
use PhpMyAdmin\Utils\HttpRequest;
/**
* PhpMyAdmin\ErrorReport class
* Error reporting functions used to generate and submit error reports
*
* @package PhpMyAdmin
*/
class ErrorReport
{
/**
* the url where to submit reports to
* The URL where to submit reports to
*
* @var string
*/
const SUBMISSION_URL = "https://reports.phpmyadmin.net/incidents/create";
private $submissionUrl;
/**
* returns the pretty printed error report data collected from the
* @var HttpRequest
*/
private $httpRequest;
/**
* Constructor
*
* @param HttpRequest $httpRequest HttpRequest instance
*/
public function __construct(HttpRequest $httpRequest)
{
$this->httpRequest = $httpRequest;
$this->submissionUrl = 'https://reports.phpmyadmin.net/incidents/create';
}
/**
* Returns the pretty printed error report data collected from the
* current configuration or from the request parameters sent by the
* error reporting js code.
*
* @return String the report
* @return string the report
*/
public static function getPrettyReportData()
private function getPrettyData()
{
$report = self::getReportData();
$report = $this->getData();
return json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
/**
* returns the error report data collected from the current configuration or
* Returns the error report data collected from the current configuration or
* from the request parameters sent by the error reporting js code.
*
* @param string $exception_type whether exception is 'js' or 'php'
* @param string $exceptionType whether exception is 'js' or 'php'
*
* @return array error report if success, Empty Array otherwise
*/
public static function getReportData($exception_type = 'js')
public function getData($exceptionType = 'js')
{
$relParams = Relation::getRelationsParam();
// common params for both, php & js exceptions
$report = array(
"pma_version" => PMA_VERSION,
"browser_name" => PMA_USR_BROWSER_AGENT,
"browser_version" => PMA_USR_BROWSER_VER,
"user_os" => PMA_USR_OS,
"server_software" => $_SERVER['SERVER_SOFTWARE'],
"user_agent_string" => $_SERVER['HTTP_USER_AGENT'],
"locale" => $_COOKIE['pma_lang'],
"configuration_storage" =>
is_null($relParams['db']) ? "disabled" :
"enabled",
"php_version" => phpversion()
);
$report = [
"pma_version" => PMA_VERSION,
"browser_name" => PMA_USR_BROWSER_AGENT,
"browser_version" => PMA_USR_BROWSER_VER,
"user_os" => PMA_USR_OS,
"server_software" => $_SERVER['SERVER_SOFTWARE'],
"user_agent_string" => $_SERVER['HTTP_USER_AGENT'],
"locale" => $_COOKIE['pma_lang'],
"configuration_storage" =>
is_null($relParams['db']) ? "disabled" : "enabled",
"php_version" => phpversion()
];
if ($exception_type == 'js') {
if ($exceptionType == 'js') {
if (empty($_REQUEST['exception'])) {
return array();
return [];
}
$exception = $_REQUEST['exception'];
$exception["stack"] = self::translateStacktrace($exception["stack"]);
List($uri, $script_name) = self::sanitizeUrl($exception["url"]);
$exception["stack"] = $this->translateStacktrace($exception["stack"]);
list($uri, $scriptName) = $this->sanitizeUrl($exception["url"]);
$exception["uri"] = $uri;
unset($exception["url"]);
$report ["exception_type"] = 'js';
$report ["exception"] = $exception;
$report ["script_name"] = $script_name;
$report ["microhistory"] = $_REQUEST['microhistory'];
$report["exception_type"] = 'js';
$report["exception"] = $exception;
$report["script_name"] = $scriptName;
$report["microhistory"] = $_REQUEST['microhistory'];
if (! empty($_REQUEST['description'])) {
$report['steps'] = $_REQUEST['description'];
}
} elseif ($exception_type == 'php') {
$errors = array();
} elseif ($exceptionType == 'php') {
$errors = [];
// create php error report
$i = 0;
if (!isset($_SESSION['prev_errors'])
|| $_SESSION['prev_errors'] == ''
) {
return array();
return [];
}
foreach ($_SESSION['prev_errors'] as $errorObj) {
/* @var $errorObj PhpMyAdmin\Error */
@ -98,26 +114,25 @@ class ErrorReport
&& $errorObj->getType()
&& $errorObj->getNumber() != E_USER_WARNING
) {
$errors[$i++] = array(
$errors[$i++] = [
"lineNum" => $errorObj->getLine(),
"file" => $errorObj->getFile(),
"type" => $errorObj->getType(),
"msg" => $errorObj->getOnlyMessage(),
"stackTrace" => $errorObj->getBacktrace(5),
"stackhash" => $errorObj->getHash()
);
];
}
}
// if there were no 'actual' errors to be submitted.
if ($i==0) {
return array(); // then return empty array
return []; // then return empty array
}
$report ["exception_type"] = 'php';
$report["exception_type"] = 'php';
$report["errors"] = $errors;
} else {
return array();
return [];
}
return $report;
@ -131,11 +146,11 @@ class ErrorReport
* hostname and identifying query params. The second is the name of the
* php script in the url
*
* @param String $url the url to sanitize
* @param string $url the url to sanitize
*
* @return array the uri and script name
*/
public static function sanitizeUrl($url)
private function sanitizeUrl($url)
{
$components = parse_url($url);
if (isset($components["fragment"])
@ -149,25 +164,25 @@ class ErrorReport
// get script name
preg_match("<([a-zA-Z\-_\d]*\.php)$>", $components["path"], $matches);
if (count($matches) < 2) {
$script_name = 'index.php';
$scriptName = 'index.php';
} else {
$script_name = $matches[1];
$scriptName = $matches[1];
}
// remove deployment specific details to make uri more generic
if (isset($components["query"])) {
parse_str($components["query"], $query_array);
unset($query_array["db"]);
unset($query_array["table"]);
unset($query_array["token"]);
unset($query_array["server"]);
$query = http_build_query($query_array);
parse_str($components["query"], $queryArray);
unset($queryArray["db"]);
unset($queryArray["table"]);
unset($queryArray["token"]);
unset($queryArray["server"]);
$query = http_build_query($queryArray);
} else {
$query = '';
}
$uri = $script_name . "?" . $query;
return array($uri, $script_name);
$uri = $scriptName . "?" . $query;
return [$uri, $scriptName];
}
/**
@ -175,13 +190,12 @@ class ErrorReport
*
* @param array $report the report info to be sent
*
* @return String the reply of the server
* @return string the reply of the server
*/
public static function send(array $report)
public function send(array $report)
{
$httpRequest = new HttpRequest();
$response = $httpRequest->create(
self::SUBMISSION_URL,
$response = $this->httpRequest->create(
$this->submissionUrl,
"POST",
false,
json_encode($report),
@ -191,14 +205,14 @@ class ErrorReport
}
/**
* translates the cumulative line numbers in the stack trace as well as sanitize
* Translates the cumulative line numbers in the stack trace as well as sanitize
* urls and trim long lines in the context
*
* @param array $stack the stack trace
*
* @return array $stack the modified stack trace
*/
public static function translateStacktrace(array $stack)
private function translateStacktrace(array $stack)
{
foreach ($stack as &$level) {
foreach ($level["context"] as &$line) {
@ -207,9 +221,9 @@ class ErrorReport
}
}
unset($level["context"]);
List($uri, $script_name) = self::sanitizeUrl($level["url"]);
list($uri, $scriptName) = $this->sanitizeUrl($level["url"]);
$level["uri"] = $uri;
$level["scriptname"] = $script_name;
$level["scriptname"] = $scriptName;
unset($level["url"]);
}
unset($level);
@ -217,25 +231,24 @@ class ErrorReport
}
/**
* generates the error report form to collect user description and preview the
* Generates the error report form to collect user description and preview the
* report before being sent
*
* @return String the form
* @return string the form
*/
public static function getForm()
public function getForm()
{
$datas = array(
'report_data' => self::getPrettyReportData(),
$datas = [
'report_data' => $this->getPrettyData(),
'hidden_inputs' => Url::getHiddenInputs(),
'hidden_fields' => null,
);
];
$reportData = self::getReportData();
$reportData = $this->getData();
if (!empty($reportData)) {
$datas['hidden_fields'] = Url::getHiddenFields($reportData);
}
return Template::get('error/report_form')
->render($datas);
return Template::get('error/report_form')->render($datas);
}
}