1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* T3Bot. |
4
|
|
|
* |
5
|
|
|
* @author Frank Nägler <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @link http://www.t3bot.de |
8
|
|
|
* @link http://wiki.typo3.org/T3Bot |
9
|
|
|
*/ |
10
|
|
|
namespace T3Bot\Controller; |
11
|
|
|
|
12
|
|
|
use Doctrine\DBAL\Configuration; |
13
|
|
|
use Doctrine\DBAL\DriverManager; |
14
|
|
|
use Slack\Message\Attachment; |
15
|
|
|
use T3Bot\Slack\Message; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AbstractHookController. |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractHookController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $configuration; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* GerritHookController constructor. |
29
|
|
|
* |
30
|
|
|
* @param array $configuration |
31
|
|
|
*/ |
32
|
22 |
|
public function __construct(array $configuration = []) |
33
|
|
|
{ |
34
|
22 |
|
$this->configuration = $configuration; |
35
|
22 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* public method to start processing the request. |
39
|
|
|
* |
40
|
|
|
* @param string $hook |
41
|
|
|
* @param string $input |
42
|
|
|
* |
43
|
|
|
* @throws \Doctrine\DBAL\DBALException |
44
|
|
|
*/ |
45
|
|
|
abstract public function process($hook, $input = 'php://input'); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Message $payload |
49
|
|
|
* @param string $channel |
50
|
|
|
* |
51
|
|
|
* @throws \Doctrine\DBAL\DBALException |
52
|
|
|
*/ |
53
|
3 |
|
protected function postToSlack(Message $payload, $channel) |
54
|
|
|
{ |
55
|
3 |
|
$data = []; |
56
|
3 |
|
$data['unfurl_links'] = false; |
57
|
3 |
|
$data['unfurl_media'] = false; |
58
|
3 |
|
$data['parse'] = 'none'; |
59
|
3 |
|
$data['text'] = $payload->getText(); |
60
|
3 |
|
$data['channel'] = $channel; |
61
|
3 |
|
$attachments = $payload->getAttachments(); |
62
|
3 |
|
if (count($attachments)) { |
63
|
3 |
|
$data['attachments'] = []; |
64
|
|
|
} |
65
|
|
|
/** @var \T3Bot\Slack\Message\Attachment $attachment */ |
66
|
3 |
|
foreach ($attachments as $attachment) { |
67
|
3 |
|
$data['attachments'][] = Attachment::fromData([ |
68
|
3 |
|
'title' => $attachment->getTitle(), |
69
|
3 |
|
'title_link' => $attachment->getTitleLink(), |
70
|
3 |
|
'text' => $attachment->getText(), |
71
|
3 |
|
'fallback' => $attachment->getFallback(), |
72
|
3 |
|
'color' => $attachment->getColor(), |
73
|
3 |
|
'pretext' => $attachment->getPretext(), |
74
|
3 |
|
'author_name' => $attachment->getAuthorName(), |
75
|
3 |
|
'author_icon' => $attachment->getAuthorIcon(), |
76
|
3 |
|
'author_link' => $attachment->getAuthorLink(), |
77
|
3 |
|
'image_url' => $attachment->getImageUrl(), |
78
|
3 |
|
'thumb_url' => $attachment->getThumbUrl(), |
79
|
|
|
]); |
80
|
|
|
} |
81
|
3 |
|
if (!empty($GLOBALS['config']['slack']['botAvatar'])) { |
82
|
3 |
|
$data['icon_emoji'] = $GLOBALS['config']['slack']['botAvatar']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// since the bot is a real bot, we can't push directly to slack |
86
|
|
|
// lets put the message into our messages pool |
87
|
3 |
|
$this->addMessageToQueue($data); |
88
|
3 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $data |
92
|
|
|
* |
93
|
|
|
* @throws \Doctrine\DBAL\DBALException |
94
|
|
|
*/ |
95
|
2 |
|
protected function addMessageToQueue(array $data) |
96
|
|
|
{ |
97
|
2 |
|
$config = new Configuration(); |
98
|
2 |
|
$db = DriverManager::getConnection($GLOBALS['config']['db'], $config); |
99
|
2 |
|
$db->insert('messages', ['message' => json_encode($data)]); |
100
|
2 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $haystack |
104
|
|
|
* @param string $needle |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
protected function endsWith($haystack, $needle) : bool |
109
|
|
|
{ |
110
|
|
|
// search forward starting from end minus needle length characters |
111
|
|
|
return $needle === '' || ( |
112
|
|
|
($temp = strlen($haystack) - strlen($needle)) >= 0 |
113
|
|
|
&& strpos($haystack, $needle, $temp) !== false |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|