commit | author | age
|
8bd4d9
|
1 |
<?php |
JK |
2 |
class BotAPIGGHTTPException extends Exception { |
|
3 |
private $httpcode; |
|
4 |
private $content; |
|
5 |
|
|
6 |
function __construct($msg, $httpcode, $content) { |
|
7 |
$this->httpcode = $httpcode; |
|
8 |
$this->content = $content; |
9dae9f
|
9 |
parent::__construct($msg.' Błąd '.$httpcode); |
8bd4d9
|
10 |
} |
JK |
11 |
|
|
12 |
function __get($name) { |
|
13 |
return $this->$name; |
|
14 |
} |
|
15 |
} |
|
16 |
class BotAPIGGXMLException extends Exception { |
|
17 |
private $content; |
|
18 |
|
|
19 |
function __construct($msg, $content) { |
|
20 |
$this->content = $content; |
|
21 |
parent::__construct($msg); |
|
22 |
} |
|
23 |
|
|
24 |
function __get($name) { |
|
25 |
return $this->$name; |
|
26 |
} |
|
27 |
} |
|
28 |
|
|
29 |
class BotAPIGGReplyException extends Exception { |
|
30 |
private $xml; |
|
31 |
|
|
32 |
function __construct($msg, SimpleXMLElement $xml) { |
|
33 |
$this->xml = $xml; |
|
34 |
parent::__construct($msg); |
|
35 |
} |
|
36 |
|
|
37 |
function __get($name) { |
|
38 |
return $this->$name; |
|
39 |
} |
|
40 |
|
|
41 |
function __toString() { |
|
42 |
return $this->getMessage().' Błąd '.((string)$this->xml->status).': '.((string)$this->xml->errorMsg); |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
class BotAPIGG extends config { |
bcbe9c
|
47 |
private static $token; |
8bd4d9
|
48 |
|
JK |
49 |
const STATUS_DOSTEPNY = 2; |
|
50 |
const STATUS_DOSTEPNY_DESC = 4; |
|
51 |
const STATUS_ONLINE = 2; |
|
52 |
const STATUS_ONLINE_DESC = 4; |
|
53 |
|
|
54 |
const STATUS_ZAJETY = 3; |
|
55 |
const STATUS_ZAJETY_DESC = 5; |
|
56 |
const STATUS_AWAY = 3; |
|
57 |
const STATUS_AWAY_DESC = 5; |
|
58 |
|
|
59 |
const STATUS_NIE_PRZESZKADZAC = 33; |
|
60 |
const STATUS_NIE_PRZESZKADZAC_DESC = 34; |
|
61 |
const STATUS_DND = 33; |
|
62 |
const STATUS_DND_DESC = 34; |
|
63 |
|
|
64 |
const STATUS_POROZMAWIAJ = 23; |
|
65 |
const STATUS_POROZMAWIAJ_DESC = 24; |
|
66 |
const STATUS_CHAT = 23; |
|
67 |
const STATUS_CHAT_DESC = 24; |
|
68 |
|
|
69 |
const STATUS_NIEWIDOCZNY = 20; |
|
70 |
const STATUS_NIEWIDOCZNY_DESC = 22; |
|
71 |
const STATUS_INVISIBLE = 20; |
|
72 |
const STATUS_INVISIBLE_DESC = 22; |
|
73 |
|
fb87e0
|
74 |
private function httpQuery($address, $curlopts = array(), $useToken = TRUE, $parseXML = TRUE) { |
8bd4d9
|
75 |
if(!is_array($curlopts)) { |
JK |
76 |
$curlopts = array(); |
|
77 |
} |
|
78 |
|
|
79 |
if($useToken) { |
|
80 |
if(!isset($curlopts[CURLOPT_HTTPHEADER]) || !is_array($curlopts[CURLOPT_HTTPHEADER])) { |
|
81 |
$curlopts[CURLOPT_HTTPHEADER] = array(); |
|
82 |
} |
|
83 |
|
|
84 |
$token = $this->getToken(); |
|
85 |
|
|
86 |
$curlopts[CURLOPT_HTTPHEADER][] = 'Token: '.$token['token']; |
|
87 |
} |
|
88 |
|
|
89 |
$dane = curl_init($address); |
|
90 |
$curlopts[CURLOPT_RETURNTRANSFER] = TRUE; |
|
91 |
$curlopts[CURLOPT_USERAGENT] = 'Bot Gadu-Gadu/'.main::VERSION.' (http://jacekk.info/botgg)'; |
|
92 |
$curlopts[CURLOPT_SSL_CIPHER_LIST] = 'HIGH:-MD5:-aNULL:-DES'; |
|
93 |
$curlopts[CURLOPT_SSL_VERIFYPEER] = TRUE; |
|
94 |
$curlopts[CURLOPT_SSL_VERIFYHOST] = 2; |
|
95 |
$curlopts[CURLOPT_CAPATH] = BOT_TOPDIR.'/data/ca-certificates/'; |
|
96 |
curl_setopt_array($dane, $curlopts); |
|
97 |
$tok2 = $tok = curl_exec($dane); |
|
98 |
$info = curl_getinfo($dane); |
|
99 |
|
fb87e0
|
100 |
if($parseXML) { |
JK |
101 |
try { |
|
102 |
libxml_use_internal_errors(TRUE); |
|
103 |
$tok = new SimpleXMLElement($tok); |
|
104 |
} |
|
105 |
catch(Exception $e) { |
|
106 |
throw new BotAPIGGXMLException('Otrzymano błędny XML od botmastera.', $tok2); |
|
107 |
} |
|
108 |
|
|
109 |
if(!$tok) { |
|
110 |
if($info['http_code'] != 200) { |
|
111 |
throw new BotAPIGGHTTPException('Nie udało się wykonać zapytania HTTP.', $info['http_code'], $tok2); |
|
112 |
} |
|
113 |
else |
|
114 |
{ |
|
115 |
throw new BotAPIGGXMLException('Otrzymano błędny XML od botmastera.', $tok2); |
|
116 |
} |
|
117 |
} |
8bd4d9
|
118 |
} |
fb87e0
|
119 |
else |
JK |
120 |
{ |
8bd4d9
|
121 |
if($info['http_code'] != 200) { |
JK |
122 |
throw new BotAPIGGHTTPException('Nie udało się wykonać zapytania HTTP.', $info['http_code'], $tok2); |
|
123 |
} |
|
124 |
} |
|
125 |
|
|
126 |
return $tok; |
|
127 |
} |
|
128 |
|
|
129 |
function getToken($force = FALSE) { |
fb87e0
|
130 |
if($force || self::$token === NULL) { |
8bd4d9
|
131 |
$auth = $this->APIs['Gadu-Gadu']; |
JK |
132 |
|
fb87e0
|
133 |
$tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/getToken/'.$auth['numer'], array( |
8bd4d9
|
134 |
CURLOPT_USERPWD => $auth['login'].':'.$auth['haslo'], |
JK |
135 |
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
fb87e0
|
136 |
), FALSE); |
8bd4d9
|
137 |
|
JK |
138 |
if($tok->errorMsg) { |
|
139 |
throw new BotAPIGGReplyException('Pobieranie tokena nie powiodło się.', $tok); |
|
140 |
} |
|
141 |
|
fb87e0
|
142 |
self::$token = array('token' => (string)$tok->token, 'host' => (string)$tok->server, 'port' => (int)$tok->port); |
8bd4d9
|
143 |
} |
JK |
144 |
|
fb87e0
|
145 |
return self::$token; |
8bd4d9
|
146 |
} |
JK |
147 |
|
|
148 |
function setStatus($status, $desc = '') { |
|
149 |
$auth = $this->APIs['Gadu-Gadu']; |
|
150 |
$token = $this->getToken(); |
|
151 |
|
fb87e0
|
152 |
$tok = $this->httpQuery('https://'.$token['host'].'/setStatus/'.$auth['numer'], array( |
8bd4d9
|
153 |
CURLOPT_POST => TRUE, |
09ac53
|
154 |
CURLOPT_POSTFIELDS => http_build_query(array( |
fb87e0
|
155 |
'status' => $status, |
JK |
156 |
'desc' => $desc, |
09ac53
|
157 |
), '', '&'), |
8bd4d9
|
158 |
)); |
JK |
159 |
|
|
160 |
if( (string)$tok->status != '0') { |
|
161 |
throw new BotAPIGGReplyException('Ustawianie statusu nie powiodło się.', $tok); |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
function setUrl($url) { |
|
166 |
$auth = $this->APIs['Gadu-Gadu']; |
|
167 |
|
fb87e0
|
168 |
$tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/setUrl/'.$auth['numer'], array( |
8bd4d9
|
169 |
CURLOPT_POST => TRUE, |
JK |
170 |
CURLOPT_POSTFIELDS => $url, |
|
171 |
)); |
|
172 |
|
|
173 |
if( (string)$tok->status != '0') { |
|
174 |
throw new BotAPIGGReplyException('Ustawianie adresu URL bota nie powiodło się.', $tok); |
|
175 |
} |
|
176 |
|
|
177 |
return $tok; |
|
178 |
} |
|
179 |
|
fb87e0
|
180 |
function getImage($hash) { |
JK |
181 |
$auth = $this->APIs['Gadu-Gadu']; |
|
182 |
$token = $this->getToken(); |
|
183 |
|
|
184 |
$tok = $this->httpQuery('https://'.$token['host'].'/botmaster/setUrl/'.$auth['numer'], array( |
|
185 |
CURLOPT_POST => TRUE, |
09ac53
|
186 |
CURLOPT_POSTFIELDS => http_build_query(array('hash' => $hash), '', '&'), |
fb87e0
|
187 |
), TRUE, FALSE); |
JK |
188 |
|
|
189 |
return $tok; |
|
190 |
} |
|
191 |
|
c79ebe
|
192 |
function existsImage($hash) { |
fb87e0
|
193 |
$auth = $this->APIs['Gadu-Gadu']; |
JK |
194 |
$token = $this->getToken(); |
|
195 |
|
|
196 |
$tok = $this->httpQuery('https://'.$token['host'].'/botmaster/setUrl/'.$auth['numer'], array( |
|
197 |
CURLOPT_POST => TRUE, |
09ac53
|
198 |
CURLOPT_POSTFIELDS => http_build_query(array('hash' => $hash), '', '&'), |
aea763
|
199 |
)); |
fb87e0
|
200 |
|
JK |
201 |
if( (string)$tok->status != '0') { |
|
202 |
return FALSE; |
|
203 |
} |
|
204 |
|
|
205 |
return TRUE; |
|
206 |
} |
|
207 |
|
|
208 |
function putImage($path) { |
|
209 |
$fp = fopen($path, 'r'); |
|
210 |
if(!$fp) { |
|
211 |
return FALSE; |
|
212 |
} |
|
213 |
|
|
214 |
$auth = $this->APIs['Gadu-Gadu']; |
|
215 |
$token = $this->getToken(); |
|
216 |
|
|
217 |
$tok = $this->httpQuery('https://'.$token['host'].'/botmaster/setUrl/'.$auth['numer'], array( |
|
218 |
CURLOPT_HTTPHEADER => array('Content-Type: image/x-any'), |
|
219 |
CURLOPT_POST => TRUE, |
|
220 |
CURLOPT_INFILE => $fp, |
aea763
|
221 |
)); |
fb87e0
|
222 |
|
JK |
223 |
if( (string)$tok->status != '0') { |
|
224 |
throw new BotAPIGGReplyException('Przesyłanie obrazka do botmastera nie powiodło się.', $tok); |
|
225 |
} |
|
226 |
|
|
227 |
return (string)$tok->hash; |
|
228 |
} |
|
229 |
|
8bd4d9
|
230 |
/** |
JK |
231 |
* Wysyła wiadomość do podanych użytkowników |
|
232 |
* @param array $toURL Lista adresatów wiadomości w postaci: array('Gadu-Gadu://NUMER@gadu-gadu.pl', ...) |
|
233 |
* @param BotMsg $msg Wiadomość do wysłania |
|
234 |
* @param array $params Parametry przekazywane funkcji. Aktualnie dostępne: |
|
235 |
* array( 'SendToOffline' => (bool)TRUE/FALSE ) |
|
236 |
*/ |
|
237 |
function sendMessage($toURL, BotMsg $msg, $params = array()) { |
|
238 |
$to = array(); |
|
239 |
foreach($toURL as $url) { |
|
240 |
$url = parse_url($url); |
|
241 |
if($url['scheme'] != 'Gadu-Gadu') { |
|
242 |
continue; |
|
243 |
} |
|
244 |
|
fb87e0
|
245 |
if($url['user'] == '' || !ctype_digit($url['user'])) { |
8bd4d9
|
246 |
throw new Exception('Nieznany użytkownik sieci Gadu-Gadu, któremu należy dostarczyć wiadomość.'); |
JK |
247 |
} |
|
248 |
|
|
249 |
$to[] = $url['user']; |
|
250 |
} |
|
251 |
|
|
252 |
if(empty($to)) { |
|
253 |
return NULL; |
|
254 |
} |
|
255 |
|
|
256 |
$msg = new BotMsgGG($msg); |
|
257 |
|
|
258 |
$auth = $this->APIs['Gadu-Gadu']; |
|
259 |
$token = $this->getToken(); |
|
260 |
|
|
261 |
$headers = array('Content-Type: application/x-www-form-urlencoded'); |
|
262 |
|
|
263 |
if($params['SendToOffline'] == FALSE) { |
|
264 |
$headers[] = 'Send-to-offline: 0'; |
|
265 |
} |
|
266 |
|
|
267 |
while(!empty($to)) { |
|
268 |
$to_part = implode(',', array_splice($to, -5000)); |
|
269 |
|
fb87e0
|
270 |
$tok = $this->httpQuery('https://'.$token['host'].'/sendMessage/'.$auth['numer'], array( |
8bd4d9
|
271 |
CURLOPT_HTTPHEADER => $headers, |
JK |
272 |
CURLOPT_POST => TRUE, |
fb87e0
|
273 |
CURLOPT_POSTFIELDS => array( |
JK |
274 |
'to' => $to_part, |
|
275 |
'msg' => $msg->getGG(FALSE), |
|
276 |
), |
8bd4d9
|
277 |
)); |
JK |
278 |
|
|
279 |
if((string)$tok->status == '18') { |
|
280 |
$tok = $this->httpQuery('https://'.$token['host'].'/sendMessage/'.$auth['numer'], FALSE, array( |
|
281 |
CURLOPT_HTTPHEADER => $headers, |
|
282 |
CURLOPT_POST => TRUE, |
fb87e0
|
283 |
CURLOPT_POSTFIELDS => array( |
JK |
284 |
'to' => $to_part, |
|
285 |
'msg' => $msg->getGG(TRUE), |
|
286 |
), |
8bd4d9
|
287 |
)); |
JK |
288 |
} |
|
289 |
|
|
290 |
if((string)$tok->status != '0') { |
|
291 |
throw new BotAPIGGReplyException('Problemy przy wysyłaniu wiadomości do sieci Gadu-Gadu.', $tok); |
|
292 |
} |
|
293 |
} |
|
294 |
|
|
295 |
return TRUE; |
|
296 |
} |
|
297 |
} |
|
298 |
?> |