1
|
|
|
<?php |
2
|
|
|
namespace zacksleo\yii2\gitlab\behaviors; |
3
|
|
|
|
4
|
|
|
use yii; |
5
|
|
|
use yii\base\Behavior; |
6
|
|
|
use yii\base\Exception; |
7
|
|
|
use yii\base\UserException; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\web\HttpException; |
10
|
|
|
use yii\helpers\VarDumper; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ErrorBehavior |
14
|
|
|
* @package zacksleo\yii2\gitlab\behaviors\ErrorBehavior |
15
|
|
|
* @property string $apiRoot |
16
|
|
|
* @property string $privateToken |
17
|
|
|
* @property string $projectName |
18
|
|
|
* @property string $defaultMessage |
19
|
|
|
*/ |
20
|
|
|
class ErrorBehavior extends Behavior |
21
|
|
|
{ |
22
|
|
|
public $apiRoot; |
23
|
|
|
public $privateToken; |
24
|
|
|
public $projectName; |
25
|
|
|
public $defaultMessage = 'Error'; |
26
|
2 |
|
public function events() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
2 |
|
Controller::EVENT_BEFORE_ACTION => 'beforeAction' |
30
|
|
|
]; |
31
|
|
|
} |
32
|
|
|
/** |
33
|
|
|
* @param $event |
34
|
|
|
* @return boolean |
35
|
|
|
*/ |
36
|
3 |
|
public function beforeAction($event) |
|
|
|
|
37
|
|
|
{ |
38
|
3 |
|
if (($exception = Yii::$app->getErrorHandler()->exception) === null) { |
39
|
2 |
|
$exception = new HttpException(404, Yii::t('yii', 'Page not found.')); |
40
|
|
|
} |
41
|
3 |
|
if ($exception instanceof HttpException) { |
42
|
3 |
|
$code = $exception->statusCode; |
43
|
|
|
} else { |
44
|
|
|
$code = $exception->getCode(); |
45
|
|
|
} |
46
|
3 |
|
if ($exception instanceof Exception) { |
47
|
3 |
|
$name = $exception->getName(); |
48
|
|
|
} else { |
49
|
|
|
$name = $this->defaultMessage ?: Yii::t('yii', 'Error'); |
50
|
|
|
} |
51
|
3 |
|
if ($code) { |
52
|
3 |
|
$name .= " (#$code)"; |
53
|
|
|
} |
54
|
3 |
|
if ($exception instanceof UserException) { |
55
|
3 |
|
$message = $exception->getMessage(); |
56
|
|
|
} else { |
57
|
|
|
$message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.'); |
58
|
|
|
} |
59
|
3 |
|
if ($code > 499 && $code < 600) { |
60
|
1 |
|
$projectId = $this->getProjectId(); |
61
|
1 |
|
if (empty($projectId)) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
1 |
|
$url = $this->apiRoot . '/projects/' . $projectId . '/issues'; |
65
|
1 |
|
$ch = curl_init(); |
66
|
1 |
|
curl_setopt($ch, CURLOPT_URL, $url); |
67
|
1 |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
68
|
1 |
|
'PRIVATE-TOKEN: ' . $this->privateToken, |
69
|
|
|
)); |
70
|
1 |
|
$description = ''; |
71
|
1 |
|
if (!empty(Yii::$app->request->userIP)) { |
72
|
|
|
$description .= '<blockquote>IP: ' . Yii::$app->request->userIP . '</blockquote>'; |
73
|
|
|
} |
74
|
1 |
|
if (!empty(Yii::$app->request->getReferrer())) { |
75
|
|
|
$description .= '<blockquote>Refer:' . Yii::$app->request->getReferrer() . '</blockquote>'; |
76
|
|
|
} |
77
|
1 |
|
if (YII_DEBUG) { |
78
|
|
|
$description .= '<blockquote>X-Debug-Tag:' . Yii::$app->log->targets['debug']->tag . '</blockquote>'; |
79
|
|
|
} |
80
|
1 |
|
$content = htmlspecialchars( |
81
|
1 |
|
VarDumper::dumpAsString($_REQUEST), |
82
|
1 |
|
ENT_QUOTES | ENT_SUBSTITUTE, |
83
|
1 |
|
Yii::$app->charset, |
84
|
1 |
|
true |
85
|
|
|
); |
86
|
1 |
|
$description .= '<blockquote>' . $content . '</blockquote>'; |
87
|
1 |
|
$description .= '<blockquote>URL: ' . Yii::$app->request->absoluteUrl; |
88
|
1 |
|
$description .= '</blockquote><br/><pre>' . $exception . '</pre>'; |
89
|
1 |
|
curl_setopt( |
90
|
1 |
|
$ch, |
91
|
1 |
|
CURLOPT_POSTFIELDS, |
92
|
|
|
[ |
93
|
1 |
|
'title' => $message, |
94
|
1 |
|
'description' => $description, |
95
|
1 |
|
'labels' => 'bug', |
96
|
|
|
] |
97
|
|
|
); |
98
|
1 |
|
curl_setopt($ch, CURLOPT_HEADER, false); |
99
|
1 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
100
|
1 |
|
curl_setopt($ch, CURLOPT_VERBOSE, false); |
101
|
1 |
|
curl_exec($ch); |
102
|
1 |
|
curl_close($ch); |
103
|
|
|
} |
104
|
3 |
|
return true; |
105
|
|
|
} |
106
|
|
|
/** |
107
|
|
|
* @return bool|integer |
108
|
|
|
*/ |
109
|
2 |
|
private function getProjectId() |
110
|
|
|
{ |
111
|
2 |
|
$url = $this->apiRoot . '/projects/' . urlencode($this->projectName); |
112
|
2 |
|
$ch = curl_init(); |
113
|
2 |
|
curl_setopt($ch, CURLOPT_URL, $url); |
114
|
2 |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
115
|
2 |
|
'PRIVATE-TOKEN: ' . $this->privateToken, |
116
|
|
|
)); |
117
|
2 |
|
curl_setopt($ch, CURLOPT_HEADER, false); |
118
|
2 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
119
|
2 |
|
curl_setopt($ch, CURLOPT_VERBOSE, false); |
120
|
2 |
|
$data = curl_exec($ch); |
121
|
2 |
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
122
|
2 |
|
curl_close($ch); |
123
|
2 |
|
if ($httpCode >= 200 && $httpCode < 300) { |
124
|
2 |
|
$project = json_decode($data, true); |
125
|
2 |
|
return isset($project['id']) ? $project['id'] : false; |
126
|
|
|
} |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.