forked from albandes/henry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhenry.php
370 lines (322 loc) · 12.4 KB
/
henry.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* henry
*
* PHP service wrapper for Henry Turnstiles : https://www.henry.com.br/
*
* @author Rogério Albandes <[email protected]>
* @version 0.1
* @package henry
* @example example.php
* @link https://github.com/albandes/henry
* @license GNU License
*
*/
class henry
{
/*
* Maximum message size
*/
const MESSAGE_MAX_LEN = '2048';
/**
* Turnstile ipv4 address
*
* @var string
*/
private $_ip;
/**
* Turnstile connection port
*
* @var string
*/
private $_port;
/**
* Turnstile´s socket
*
* @var null
*/
private $_socket = null;
public function __construct ($ip = '192.168.1.1', $port='3000')
{
$this->_ip = $ip;
$this->_port = $port;
}
function display()
{
echo $this->_port . "<br />";
echo $this->_ip . "<br />";
}
/**
* Set Turnstile ipv4 address
*
* @param string $ip Turnstile ipv4 address
*
* @return void
*/
public function setIp ($ip)
{
$this->_ip = $ip;
}
/**
* Set Turnstile port
*
* @param string $port Turnstile connection port
*
* @return void
*/
public function setPort ($port)
{
$this->_port = $port;
}
/**
* Connect to equipment
* Creates the socket
*
* @return string|true Error message or true if connect
*
*/
public function connect()
{
if( !empty($this->_socket) )
return " Socket exists ."; // << impede a recriação da conexão caso ela já exista
$this->_socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if( empty($this->_socket) ){
return "socket_create() falhou: ".socket_strerror(socket_last_error());
}
@socket_set_option($this->_socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>30,'usec'=>500));
$result = @socket_connect($this->_socket, $this->_ip, $this->_port);
if( empty($result) ){
@socket_close($this->_socket);
$this->_socket = false;
return "socket_connect() fail, message: ".socket_strerror(socket_last_error());
}
return true;
}
/**
* Writes to socket from the given buffer
*
* @param string $command Command to be sent to turnstile
*
* @return string|true Error message or true if socket was writed
*
*/
public function writeSocket($command)
{
$response = @socket_write($this->_socket, $command, strlen($command));
if( $response === false ){
$this->_socket = null;
return "socket_write() fail, message: ".socket_strerror(socket_last_error());
}
return true;
}
/**
* Listen Turnstile
*
* @author Júlio Filho
* @author Rogério Albandes <[email protected]>
*
* @return array [
* 'success' => true|false,
* 'message' => Error or success message
* 'original_response' => Unchanged string that the turnstile sent
* 'original_response_bytes' => Unchanged string that the ratchet sent , converted in bytes array
* 'size' => Size of received data
* 'index' => message index / thread number
* 'command' => Command identifier ex: ED, REON, EMSG, etc
* 'err_or_version' => Error code (response) or message version (sending)
* 'data' => Message, if reply, usually separated by "]". Read the documentation
* ]
*/
public function listen()
{
$this->connect();
//if( $erro ) return $erro;
$arrayReturn = array();
$response = @socket_read($this->_socket, self::MESSAGE_MAX_LEN);
if( empty($response) ){
$error = socket_last_error();
@socket_close($this->_socket);
$this->_socket = null; // em caso de falha, força a reconexão
if( $error == 11 ){
$arrayReturn['success'] = false;
$arrayReturn['message'] = 'Timeout';
return $arrayReturn;
}
else{
$arrayReturn['success'] = false;
$arrayReturn['message'] = "socket_read() falhou: ".socket_strerror($error);
return $arrayReturn;
}
}
$bytes = unpack('C*', $response);
$length = count($bytes);
if( empty($bytes[1]) || $bytes[1] !== 2 ){
$arrayReturn['success'] = false;
$arrayReturn['message'] = 'a - interferência na comunicação com o equipamento';
return $arrayReturn;
}
if( empty($bytes[$length]) || $bytes[$length] !== 3 ){
$arrayReturn['success'] = false;
$arrayReturn['message'] = 'b - interferência na comunicação com o equipamento';
return $arrayReturn;
}
$arrayReturn['success'] = true;
$arrayReturn['message'] = 'Success';
$arrayReturn['original_response'] = $response;
$arrayReturn['original_response_bytes'] = $bytes;
$arrayReturn['size'] = $bytes[2];
$arrayReturn['index'] = chr($bytes[4]).chr($bytes[5]);
$arrayReturn['command'] = "";
for($i=7; $bytes[$i]!=43 && $i<$length-1; $i++){
$arrayReturn['command'] .= chr($bytes[$i]);
}
$arrayReturn['err_or_version'] = "";
for($i=$i+1; $bytes[$i]!=43 && $i<$length-1; $i++){
$arrayReturn['err_or_version'] .= chr($bytes[$i]);
}
$arrayReturn['data'] = '';
for($i=$i+1; $i<$length-1; $i++){
$arrayReturn['data'] .= chr($bytes[$i]);
}
return $arrayReturn;
}
function generate($sString) {
if(!empty($sString)) {
$sByteInicial = "02 ";
$sTamanhoMensagem = $this->getStringSize($sString);
$sMensagem = $this->string2Hex($sString);
$sCheckSun = $this->getCheckSum($sString);
$sByteFinal = " 03";
return $sByteInicial . $sTamanhoMensagem . $sMensagem . $sCheckSun . $sByteFinal;
} else
return false;
}
function getStringSize($sString) {
$nTamanhoString = strlen($sString);
$nHex1 = $nTamanhoString % 256;
$nHex16 = (int) ($nTamanhoString / 256);
$nHex1 = dechex($nHex1);
if(strlen($nHex1) === 1)
$nHex1 = "0".$nHex1;
$nHex16 = dechex($nHex16);
if(strlen($nHex16) === 1)
$nHex16 = "0".$nHex16;
$sResultado = $nHex1." ".$nHex16;
return strtoupper($sResultado);
}
function string2Hex($sString) {
$sHex = "";
$vString = str_split($sString);
foreach($vString as $sCharactere)
$sHex .= " ".dechex(ord($sCharactere)); // Transforms that character to ASCII and then converts it to hexadecimal
return strtoupper($sHex);
}
function getCheckSum($sString) {
$nTamanhoString = strlen($sString);
$sXor = "";
$vString = str_split($sString);
foreach($vString as $sCharactere)
$sXor ^= ord($sCharactere);
$sXor ^= $nTamanhoString % 256;
$sXor ^= $nTamanhoString / 256;
$nHex1 = $sXor % 16;
$nHex16 = (int) ($sXor / 16);
$sResultado = " ".dechex($nHex16) . dechex($nHex1);
return strtoupper($sResultado);
}
function hex2str($hex){
$str='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$str .= chr(hexdec(substr($hex,$i,2)));
}
return $str;
}
public function flushBuffer()
{
$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$retval = "";
if( !empty($socket) )
{
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>1,'usec'=>0));
$result = @socket_connect($socket, $this->_ip, $this->_port);
if( !empty($result) )
{
do
{
$response = @socket_read($socket, self::MESSAGE_MAX_LEN);
$retval .= $response;
}
while(!empty($response));
}
}
@socket_close($socket);
return $retval;
}
function __destruct()
{
$this->socket = null;
@socket_close($this->socket);
}
public function parseError($errorNumber)
{
$arrayError = array(
0 => "Não há erro",
1 => "Não há dados",
10 => "Comando desconhecido",
11 => "Tamanho do pacote é inválido",
12 => "Parâmetros informados são inválidos",
13 => "Erro de checksum",
14 => "Tamanho dos parâmetros são inválidos",
15 => "Número da mensagem é inválido",
16 => "Start Byte é inválido",
17 => "Erro para receber pacote",
20 => "Não há empregador cadastrado",
21 => "Não há usuários cadastrados",
22 => "Usuário não cadastrado",
23 => "Usuário já cadastrado",
24 => "Limite de cadastro de usuários atingido",
25 => "Equipamento não possui biometria",
26 => "Index biométrico não encontrado",
27 => "Limite de cadastro de digitais atingido",
28 => "Equipamento não possui eventos",
29 => "Erro na manipulação de biometrias",
30 => "Documento do empregador é inválido",
31 => "Tipo do documento do empregador é inválido",
32 => "Ip é inválido",
33 => "Tipo de operação do usuário é inválida",
34 => "Identificador do empregado é inválido",
35 => "Documento do empregador é inválido",
36 => "Referencia do empregado é inválida",
37 => "Referencia de cartão de usuario é inválida",
43 => "Erro ao gravar dados",
44 => "Erro ao ler dados",
50 => "Erro desconhecido",
61 => "Matrícula já existe",
62 => "Identificador já existe",
63 => "Opção inválida",
64 => "Matrícula não existe",
65 => "Identificador não existe",
66 => "Cartão necessário mas não informado",
180 => "Horário contido no usuário não existe",
181 => "Período contido no horário não existe",
182 => "Escala contida no usuário não existe",
183 => "Faixa de dias da semana não informada ou inválida (acionamento e períodos)",
184 => "Hora não informada ou inválida (acionamento e períodos)",
185 => "Período não informado ou inválido (horários)",
186 => "Horário não informado ou inválido (cartões)",
187 => "Indice não informado ou inválido (horários, periodos e acionamentos)",
188 => "Data não informada ou inválida (feriados)",
189 => "Mensagem não informada (funções)",
190 => "Erro na memoria (acionamento)",
191 => "Mensagem não informada (funções)",
192 => "Informação de tipo de acesso invalida",
193 => "Informação de tipo de cartão invalida",
240 => "Registro não foi encontrado (Grupos de acesso, período, horários, acionamentos)",
241 => "Registro já existe (Grupos de acesso, período, horários, acionamentos)",
242 => "Registro não existe (Grupos de acesso, período, horários, acionamentos)",
243 => "Limite atingido (Grupos de acesso, período, horários, acionamentos)",
244 => "Erro no tipo de operação (Grupos de acesso, período, horários, acionamentos)"
);
return $arrayError[$errorNumber];
}
}