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