-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessageBuilder.php
More file actions
208 lines (177 loc) · 5.17 KB
/
MessageBuilder.php
File metadata and controls
208 lines (177 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* Biblioteka implementująca BotAPI GG <https://boty.gg.pl>
* Wymagane: PHP 5.6+, php-cURL.
*
* Copyright (C) 2013-2016 Xevin Consulting Limited Marcin Bagiński <[email protected]>
* Modified by KsaR 2016-2017 <https://github.com/KsaR99/>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
spl_autoload_register(function($class) {
require_once __DIR__."/{$class}.php";
});
/**
* @brief Reprezentacja wiadomości
*/
class MessageBuilder
{
/**
* Lista odbiorców ( numery GG )
*/
public $recipientNumbers = [];
private $html = '';
private $text = '';
private $format = null;
const BOTAPI_VERSION = 'GGBotAPI-3.1;PHP-'.PHP_VERSION;
const IMG_FILE = true;
const IMG_RAW = false;
/**
* Konstruktor MessageBuilder
*/
public function __construct()
{
}
/**
* Czyści całą wiadomość
*/
public function clear()
{
$this->recipientNumbers = [];
$this->html = '';
$this->text = '';
$this->format = null;
}
/**
* Dodaje tekst do wiadomości
*
* @param string $text Tekst do wysłania
*
* @return MessageBuilder this
*/
public function addText($text)
{
$text = str_replace(["\n", "\r\r"], ["\r\n", "\r"], $text);
$html = str_replace("\r\n", '<br>', htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8'));
if ($this->format !== null) {
$this->text .= $text;
}
$this->html .= $html;
return $this;
}
/**
* Dodaje tekst do wiadomości
*
* @param string $html Tekst do wysłania w HTMLu
*
* @return MessageBuilder this
*/
public function addRawHtml($html)
{
$this->html .= $html;
return $this;
}
/**
* Ustawia tekst do wiadomości
*
* @param string $html Tekst do wysłania w HTMLu
*
* @return MessageBuilder this
*/
public function setRawHtml($html)
{
$this->html = $html;
return $this;
}
/**
* Ustawia tekst wiadomości alternatywnej
*
* @param string $message Tekst do wysłania dla GG 7.7 i starszych
*
* @return MessageBuilder this
*/
public function setAlternativeText($message)
{
$this->format = null;
$this->text = $message;
return $this;
}
/**
* Dodaje obraz do wiadomości
*
* @param string $pathOrContent Ścieżka do obrazka lub dane obrazka
* @param bool $isPath Wskazuje co zawiera zmienna pathOrContent
*
* @return MessageBuilder this
*/
public function addImage($pathOrContent, $isPath = self::IMG_FILE)
{
$p = new PushConnection;
if (!$p->authorize()) {
throw new MessageBuilderException("'PushConnection' nie jest zautoryzowany");
}
if ($isPath) {
$content = file_get_contents($pathOrContent);
} else {
$content =& $pathOrContent;
}
$crc = crc32($content);
$length = strlen($content);
$hash = sprintf('%08x%08x', $crc, $length);
if (!$p->existsImage($hash) && !$p->putImage($content)) {
throw new UnableToSendImageException('Nie udało się wysłać obrazka');
}
$this
->addRawHtml('<img name="'.$hash.'">')
->format .= pack('vCCCVV', strlen($this->text), 0x80, 0x09, 0x01, $length, $crc);
return $this;
}
/**
* Ustawia odbiorców wiadomości
*
* @param int|string|array $recipientNumbers GG odbiorców
*
* @return MessageBuilder this
*/
public function setRecipients($recipientNumbers)
{
$this->recipientNumbers = (array)$recipientNumbers;
return $this;
}
/**
* Tworzy sformatowaną wiadomość do wysłania do BotMastera
*/
public function getProtocolMessage()
{
$formatLen = strlen($this->format ?? '');
return pack('VVVV',
strlen($this->html)+1,
strlen($this->text)+1,
0,
($formatLen ? $formatLen+3 : 0))
. $this->html."\0$this->text\0"
. ($formatLen ? pack('Cv', 0x02, $formatLen).$this->format : '');
}
/**
* Zwraca sformatowaną wiadomość do wysłania do BotMastera
*/
public function reply()
{
if (!empty($this->recipientNumbers)) {
header('To: '.implode(',', $this->recipientNumbers));
}
header('BotApi-Version: '.self::BOTAPI_VERSION);
echo $this->getProtocolMessage();
}
}