1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* vim: set expandtab sw=4 ts=4 sts=4: */ |
4
|
|
|
/** |
5
|
|
|
* Incidents controller handling incident creation and rendering. |
6
|
|
|
* |
7
|
|
|
* phpMyAdmin Error reporting server |
8
|
|
|
* Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/) |
9
|
|
|
* |
10
|
|
|
* Licensed under The MIT License |
11
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
12
|
|
|
* Redistributions of files must retain the above copyright notice. |
13
|
|
|
* |
14
|
|
|
* @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/) |
15
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
16
|
|
|
* |
17
|
|
|
* @see https://www.phpmyadmin.net/ |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace App\Controller; |
21
|
|
|
|
22
|
|
|
use Cake\Core\Configure; |
23
|
|
|
use Cake\Http\Exception\NotFoundException; |
24
|
|
|
use Cake\ORM\TableRegistry; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Incidents controller handling incident creation and rendering. |
28
|
|
|
*/ |
29
|
|
|
class IncidentsController extends AppController |
30
|
|
|
{ |
31
|
|
|
public $uses = [ |
32
|
|
|
'Incident', |
33
|
|
|
'Notification', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public $components = ['Mailer']; |
37
|
|
|
|
38
|
|
|
public function create() |
39
|
|
|
{ |
40
|
|
|
// Only allow POST requests |
41
|
|
|
$this->request->allowMethod(['post']); |
42
|
|
|
|
43
|
|
|
$bugReport = $this->request->input('json_decode', true); |
44
|
|
|
$result = $this->Incidents->createIncidentFromBugReport($bugReport); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if (count($result['incidents']) > 0 |
47
|
|
|
&& ! in_array(false, $result['incidents']) |
48
|
|
|
) { |
49
|
|
|
$response = [ |
50
|
|
|
'success' => true, |
51
|
|
|
'message' => 'Thank you for your submission', |
52
|
|
|
'incident_id' => $result['incidents'], // Return a list of incident ids. |
53
|
|
|
]; |
54
|
|
|
} else { |
55
|
|
|
$response = [ |
56
|
|
|
'success' => false, |
57
|
|
|
'message' => 'There was a problem with your submission.', |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
$this->autoRender = false; |
61
|
|
|
$this->response->header([ |
|
|
|
|
62
|
|
|
'Content-Type' => 'application/json', |
63
|
|
|
'X-Content-Type-Options' => 'nosniff', |
64
|
|
|
]); |
65
|
|
|
$this->response->body( |
|
|
|
|
66
|
|
|
json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
// For all the newly added reports, |
70
|
|
|
// send notification emails |
71
|
|
|
foreach ($result['reports'] as $report_id) { |
72
|
|
|
$this->_sendNotificationMail($report_id); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function json($id) |
79
|
|
|
{ |
80
|
|
|
if (! isset($id) || ! $id) { |
81
|
|
|
throw new NotFoundException(__('Invalid Incident')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->Incidents->recursive = -1; |
|
|
|
|
85
|
|
|
$incident = $this->Incidents->findById($id)->all()->first(); |
86
|
|
|
if (! $incident) { |
87
|
|
|
throw new NotFoundException(__('Invalid Incident')); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$incident['full_report'] = |
91
|
|
|
json_decode($incident['full_report'], true); |
92
|
|
|
$incident['stacktrace'] = |
93
|
|
|
json_decode($incident['stacktrace'], true); |
94
|
|
|
|
95
|
|
|
$this->autoRender = false; |
96
|
|
|
$this->response->body(json_encode($incident, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
|
|
|
|
97
|
|
|
|
98
|
|
|
return $this->response; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function view($incidentId) |
102
|
|
|
{ |
103
|
|
|
if (! isset($incidentId) || ! $incidentId) { |
104
|
|
|
throw new NotFoundException(__('Invalid Incident')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$incident = $this->Incidents->findById($incidentId)->all()->first(); |
|
|
|
|
108
|
|
|
if (! $incident) { |
109
|
|
|
throw new NotFoundException(__('Invalid Incident')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$incident['full_report'] = |
113
|
|
|
json_decode($incident['full_report'], true); |
114
|
|
|
$incident['stacktrace'] = |
115
|
|
|
json_decode($incident['stacktrace'], true); |
116
|
|
|
|
117
|
|
|
$this->set('incident', $incident); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function _sendNotificationMail($reportId) |
121
|
|
|
{ |
122
|
|
|
$this->Reports = TableRegistry::get('Reports'); |
|
|
|
|
123
|
|
|
$report = $this->Reports->findById($reportId)->all()->first()->toArray(); |
124
|
|
|
$this->Reports->id = $reportId; |
|
|
|
|
125
|
|
|
|
126
|
|
|
$viewVars = [ |
127
|
|
|
'report' => $report, |
128
|
|
|
'project_name' => Configure::read('GithubRepoPath'), |
129
|
|
|
'incidents' => $this->Reports->getIncidents()->toArray(), |
130
|
|
|
'incidents_with_description' => $this->Reports->getIncidentsWithDescription(), |
131
|
|
|
'incidents_with_stacktrace' => $this->Reports->getIncidentsWithDifferentStacktrace(), |
132
|
|
|
'related_reports' => $this->Reports->getRelatedReports(), |
133
|
|
|
'status' => $this->Reports->status |
134
|
|
|
]; |
135
|
|
|
$viewVars = array_merge($viewVars, $this->_getSimilarFields($reportId)); |
136
|
|
|
|
137
|
|
|
$this->Mailer->sendReportMail($viewVars); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function _getSimilarFields($id) |
141
|
|
|
{ |
142
|
|
|
$this->Reports->id = $id; |
143
|
|
|
|
144
|
|
|
$viewVars = [ |
145
|
|
|
'columns' => TableRegistry::get('Incidents')->summarizableFields, |
|
|
|
|
146
|
|
|
]; |
147
|
|
|
$relatedEntries = []; |
148
|
|
|
|
149
|
|
|
foreach (TableRegistry::get('Incidents')->summarizableFields as $field) { |
|
|
|
|
150
|
|
|
list($entriesWithCount, $totalEntries) = |
151
|
|
|
$this->Reports->getRelatedByField($field, 25, true); |
152
|
|
|
$relatedEntries[$field] = $entriesWithCount; |
153
|
|
|
$viewVars["${field}_distinct_count"] = $totalEntries; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$viewVars['related_entries'] = $relatedEntries; |
157
|
|
|
|
158
|
|
|
return $viewVars; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|