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; |
|
9 |
parent::__construct($msg); |
|
10 |
} |
|
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 { |
|
47 |
private $token; |
|
48 |
|
|
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 |
|
|
74 |
private function httpQuery($address, $useToken = TRUE, $curlopts = array()) { |
|
75 |
if(!is_array($curlopts)) { |
|
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 |
|
|
100 |
try { |
|
101 |
libxml_use_internal_errors(TRUE); |
|
102 |
$tok = new SimpleXMLElement($tok); |
|
103 |
} |
|
104 |
catch(Exception $e) { |
|
105 |
throw new BotAPIGGXMLException('Otrzymano błędny XML od botmastera.', $tok2); |
|
106 |
} |
|
107 |
|
|
108 |
if(!$tok) { |
|
109 |
if($info['http_code'] != 200) { |
|
110 |
throw new BotAPIGGHTTPException('Nie udało się wykonać zapytania HTTP.', $info['http_code'], $tok2); |
|
111 |
} |
|
112 |
else |
|
113 |
{ |
|
114 |
throw new BotAPIGGXMLException('Otrzymano błędny XML od botmastera.', $tok2); |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
return $tok; |
|
119 |
} |
|
120 |
|
|
121 |
function getToken($force = FALSE) { |
|
122 |
if($force || !$this->token) { |
|
123 |
$auth = $this->APIs['Gadu-Gadu']; |
|
124 |
|
|
125 |
$tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/getToken/'.$auth['numer'], FALSE, array( |
|
126 |
CURLOPT_USERPWD => $auth['login'].':'.$auth['haslo'], |
|
127 |
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
128 |
)); |
|
129 |
|
|
130 |
if($tok->errorMsg) { |
|
131 |
throw new BotAPIGGReplyException('Pobieranie tokena nie powiodło się.', $tok); |
|
132 |
} |
|
133 |
|
|
134 |
$this->token = array('token' => (string)$tok->token, 'host' => (string)$tok->server, 'port' => (int)$tok->port); |
|
135 |
} |
|
136 |
|
|
137 |
return $this->token; |
|
138 |
} |
|
139 |
|
|
140 |
function setStatus($status, $desc = '') { |
|
141 |
$auth = $this->APIs['Gadu-Gadu']; |
|
142 |
$token = $this->getToken(); |
|
143 |
|
|
144 |
$tok = $this->httpQuery('https://'.$token['host'].'/setStatus/'.$auth['numer'], FALSE, array( |
|
145 |
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'), |
|
146 |
CURLOPT_POST => TRUE, |
|
147 |
CURLOPT_POSTFIELDS => 'token='.urlencode($token['token']).'&status='.urlencode($status).'&desc='.urlencode($desc), |
|
148 |
)); |
|
149 |
|
|
150 |
if( (string)$tok->status != '0') { |
|
151 |
throw new BotAPIGGReplyException('Ustawianie statusu nie powiodło się.', $tok); |
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
function setUrl($url) { |
|
156 |
$auth = $this->APIs['Gadu-Gadu']; |
|
157 |
|
|
158 |
$tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/setUrl/'.$auth['numer'], TRUE, array( |
|
159 |
CURLOPT_POST => TRUE, |
|
160 |
CURLOPT_POSTFIELDS => $url, |
|
161 |
)); |
|
162 |
|
|
163 |
if( (string)$tok->status != '0') { |
|
164 |
throw new BotAPIGGReplyException('Ustawianie adresu URL bota nie powiodło się.', $tok); |
|
165 |
} |
|
166 |
|
|
167 |
return $tok; |
|
168 |
} |
|
169 |
|
|
170 |
/** |
|
171 |
* Wysyła wiadomość do podanych użytkowników |
|
172 |
* @param array $toURL Lista adresatów wiadomości w postaci: array('Gadu-Gadu://NUMER@gadu-gadu.pl', ...) |
|
173 |
* @param BotMsg $msg Wiadomość do wysłania |
|
174 |
* @param array $params Parametry przekazywane funkcji. Aktualnie dostępne: |
|
175 |
* array( 'SendToOffline' => (bool)TRUE/FALSE ) |
|
176 |
*/ |
|
177 |
|
|
178 |
function sendMessage($toURL, BotMsg $msg, $params = array()) { |
|
179 |
$to = array(); |
|
180 |
foreach($toURL as $url) { |
|
181 |
$url = parse_url($url); |
|
182 |
if($url['scheme'] != 'Gadu-Gadu') { |
|
183 |
continue; |
|
184 |
} |
|
185 |
|
|
186 |
if($url['user']=='' || !ctype_digit($url['user'])) { |
|
187 |
throw new Exception('Nieznany użytkownik sieci Gadu-Gadu, któremu należy dostarczyć wiadomość.'); |
|
188 |
} |
|
189 |
|
|
190 |
$to[] = $url['user']; |
|
191 |
} |
|
192 |
|
|
193 |
if(empty($to)) { |
|
194 |
return NULL; |
|
195 |
} |
|
196 |
|
|
197 |
$msg = new BotMsgGG($msg); |
|
198 |
|
|
199 |
$auth = $this->APIs['Gadu-Gadu']; |
|
200 |
$token = $this->getToken(); |
|
201 |
|
|
202 |
$headers = array('Content-Type: application/x-www-form-urlencoded'); |
|
203 |
|
|
204 |
if($params['SendToOffline'] == FALSE) { |
|
205 |
$headers[] = 'Send-to-offline: 0'; |
|
206 |
} |
|
207 |
|
|
208 |
while(!empty($to)) { |
|
209 |
$to_part = implode(',', array_splice($to, -5000)); |
|
210 |
|
|
211 |
$tok = $this->httpQuery('https://'.$token['host'].'/sendMessage/'.$auth['numer'], FALSE, array( |
|
212 |
CURLOPT_HTTPHEADER => $headers, |
|
213 |
CURLOPT_POST => TRUE, |
|
214 |
CURLOPT_POSTFIELDS => 'token='.urlencode($token['token']).'&to='.urlencode($to_part).'&msg='.urlencode($msg->getGG(FALSE)), |
|
215 |
)); |
|
216 |
|
|
217 |
if((string)$tok->status == '18') { |
|
218 |
$tok = $this->httpQuery('https://'.$token['host'].'/sendMessage/'.$auth['numer'], FALSE, array( |
|
219 |
CURLOPT_HTTPHEADER => $headers, |
|
220 |
CURLOPT_POST => TRUE, |
|
221 |
CURLOPT_POSTFIELDS => 'token='.urlencode($token['token']).'&to='.urlencode($to_part).'&msg='.urlencode($msg->getGG(FALSE)), |
|
222 |
)); |
|
223 |
} |
|
224 |
|
|
225 |
if((string)$tok->status != '0') { |
|
226 |
throw new BotAPIGGReplyException('Problemy przy wysyłaniu wiadomości do sieci Gadu-Gadu.', $tok); |
|
227 |
} |
|
228 |
} |
|
229 |
|
|
230 |
return TRUE; |
|
231 |
} |
|
232 |
} |
|
233 |
?> |