/zklib/zkfingerprint.php
PHP | 305 lines | 206 code | 48 blank | 51 comment | 12 complexity | a160f3e5e3ec9a2e4fb11827be55923c MD5 | raw file
- <?php
- function sendUDP($self,$command,$command_string,$template)
- {
- $cmd='';
- $cmd_string='';
- $tempt = '';
- $strlen = strlen( $command );
- for( $i = 0; $i < $strlen; $i+=2 ) {
- $char = substr( $command, $i, 2 );
- $chr=chr(hexdec($char));
- //print_r(unpack("H*",$chr));
- $cmd=$cmd.$chr;
- }
- $strlen = strlen( $command_string );
- for( $i = 0; $i < $strlen; $i+=2 ) {
- $char = substr( $command_string, $i, 2 );
- echo $char;
- $chr=chr(hexdec($char));
- //print_r(unpack("H*",$chr));
- $cmd_string=$cmd_string.$chr;
- }
- $strlen = strlen( $template );
- for( $i = 0; $i < $strlen; $i+=2 ) {
- $char = substr( $template, $i, 2 );
- echo $char;
- $chr=chr(hexdec($char));
- //print_r(unpack("H*",$chr));
- $tempt=$tempt.$chr;
- }
- $cmd_string=$cmd_string.$tempt;
- echo "<br>command= ";
- print_r(unpack("H*",$cmd));
- echo "<br>command_string= ";
- print_r(unpack("H*",$cmd_string));
- echo "<br>template= ";
- print_r(unpack("H*",$tempt));
- $chksum = 0;
- $session_id = $self->session_id;
- $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $self->data_recv, 0, 8) );
- $reply_id = hexdec( $u['h8'].$u['h7'] );
- echo "<br>cmd".print_r($cmd)."<br>";
- $buf = $self->createHeader(CMD_USERTEMP_WRQ, $chksum, $session_id, $reply_id, $cmd_string);
- echo "<br>total data sent= ";
- print_r(unpack("H*",$buf));
- socket_sendto($self->zkclient, $buf, strlen($buf), 0, $self->ip, $self->port);
- try {
- @socket_recvfrom($self->zkclient, $self->data_recv, 1024, 0, $self->ip, $self->port);
-
- $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr( $self->data_recv, 0, 8 ) );
-
- $self->session_id = hexdec( $u['h6'].$u['h5'] );
- echo "<br><br> received something<br>";
- print_r(unpack("H*",$self->data_recv));
- return substr( $self->data_recv, 8 );
- } catch(ErrorException $e) {
- echo "<br><br> received exception<br>";
- return FALSE;
- } catch(exception $e) {
- echo "<br><br> received exception<br>";
- return False;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////
- function zksetfingerprint($self, $size, $pin, $finger_id, $valid, $template) {
- $command = CMD_USERTEMP_WRQ;
- //$command=2323;
- $u_size=unpack("H2h1/H2h2",intval($size));
- echo "<br>unpacked size<br>".($size%256)."<br>".(($size-$size%256)/256)."<br>".gettype($size)."<br>";
- $garbage=substr($template,0,500);
- $command_string=chr(2).chr($size%256).chr(($size-$size%256)/256).chr($pin%256).chr(($pin-$pin%256)/256).chr($finger_id).chr($valid).$garbage;
- //$command_string=chr(3).chr(5).chr(6);
- echo "<br>cmd str<br>";
- print_r(unpack("H*",$command_string));
- //$command_string = str_pad(chr( $uid ), 2, chr(0)).chr($role).str_pad($password, 8, chr(0)).str_pad($name, 28, chr(0)).str_pad(chr(1), 9, chr(0)).str_pad($userid, 8, chr(0)).str_repeat(chr(0),16);
- //$byte1 = chr((int)($uid % 256));
- //echo "<br>byte1=<br>";
- //print_r(unpack("H*",$byte1));
- // $byte2 = chr((int)($uid >> 8));
- //echo "<br>byte1=<br>";
- //print_r(unpack("H*",$byte2));
- //echo "<br>strpad pass=<br>";
- //print_r(str_pad($password, 8, chr(0)));
- //echo "<br>strpad pass unpackd=<br>";
- //print_r(unpack("H*",str_pad($password, 8, chr(0))));
- // $command_string = $byte1.$byte2.chr($role).str_pad($password, 8, chr(0)).str_pad($name, 28, chr(0)).str_pad(chr(1), 9, chr(0)).str_pad($userid, 8, chr///(0)).str_repeat(chr(0),16);
- //echo "<br>unpacked cmd str<br>";
- //print_r(unpack("H*",$command_string));
- $chksum = 0;
- $session_id = $self->session_id;
-
- $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $self->data_recv, 0, 8) );
- $reply_id = hexdec( $u['h8'].$u['h7'] );
- $buf = $self->createHeader($command, $chksum, $session_id, $reply_id, $command_string);
- echo "<br>buffer========<br>";
- print_r(unpack("H*",$buf));
- echo "<br><br>";
- socket_sendto($self->zkclient, $buf, strlen($buf), 0, $self->ip, $self->port);
-
- try {
- @socket_recvfrom($self->zkclient, $self->data_recv, 1024, 0, $self->ip, $self->port);
-
- $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr( $self->data_recv, 0, 8 ) );
-
- $self->session_id = hexdec( $u['h6'].$u['h5'] );
- echo "<br><br> received something<br>";
- print_r(unpack("H*",$self->data_recv));
- return substr( $self->data_recv, 8 );
- } catch(ErrorException $e) {
- echo "<br><br> received exception<br>";
- return FALSE;
- } catch(exception $e) {
- echo "<br><br> received exception<br>";
- return False;
- }
- }
-
- function zkgetfingerprint($self) {
- $command = CMD_USERTEMP_RRQ;
- $command_string = chr(2);
- $chksum = 0;
- $session_id = $self->session_id;
- echo "<br><br>sid".$session_id." cmd".$command."data_recv= ".($self->data_recv)."<br>";
- $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $self->data_recv, 0, 8) );
- //echo substr($self->data_recv,0,8)." ";
- print_r( unpack("H*",substr($self->data_recv,0,8)));
- echo gettype($u);
- print_r($u);
- //echo gettype($u['h8']);
- //echo "substr";
- //echo chr(5);
- //echo "substr";
- //echo substr( $self->data_recv, 0, 8);
- $reply_id = hexdec( $u['h8'].$u['h7'] );
- echo "reply_id = ".$reply_id."<br>";
- echo "before createheader<br>"."command = ".dechex($command)." chksum= ".dechex($chksum)." session id = ".dechex($session_id)." reply_id = ".dechex($reply_id) . "<br>";
- $buf = $self->createHeader($command, $chksum, $session_id, $reply_id, $command_string);
- echo "buf = ";
- print_r(unpack("H*",$buf));
- //echo decbin(52);
- //echo "<br>";
-
- socket_sendto($self->zkclient, $buf, strlen($buf), 0, $self->ip, $self->port);
-
- try {
- @socket_recvfrom($self->zkclient, $self->data_recv, 1024, 0, $self->ip, $self->port);
- echo "<br><br>first recvfrm<br>";
- print_r(unpack("H*",$self->data_recv));
- $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr( $self->data_recv, 0, 8 ) );
- echo "<br><br>\$u taken from data_recv <br>";
- print_r($u);
- if ( getSizeUser($self) ) {
- $bytes = getSizeUser($self);
- echo "<br><br>entering while loop-bytes = ".$bytes."<br>";
- while ( $bytes > 0 ) {
- @socket_recvfrom($self->zkclient, $data_recv, 1032, 0, $self->ip, $self->port);
- array_push( $self->userdata, $data_recv);
- echo "<br>in loop<br>";
- $value = unpack('H*', $data_recv);
- print_r($value);
- echo "<br><br>";
- /*
- for($i=0;$i<count($value);$i++)
- {
- $c = chr($value[$i+1]);
- $utf8 = utf8_decode(chr($value[$i+1]));
- $iso88591_1 = utf8_decode($utf8);
- $iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $utf8);
- //$iso88591_2 = mb_convert_encoding($utf8, 'ISO-8859-1', 'UTF-8');
- $iso88591 = chr($value[$i+1]);
- $utf8_1 = utf8_encode($iso88591);
- $utf8_2 = iconv('ISO-8859-1', 'UTF-8', $iso88591);
- //$utf8_2 = mb_convert_encoding($iso88591, 'UTF-8', 'ISO-8859-1');
- echo "char val-> ".$value[$i+1]."char-> ".chr($value[$i+1])." utf-8 enc-> ".chr(utf8_encode($value[$i+1]))." utf-8 dec-> ".chr(utf8_decode($value[$i+1]));
- echo "<br>";
- echo $utf8_1;
- }
- */
- //$binary = base_convert($value[1], 16, 2);
- //echo pack('H*', base_convert($binary, 2, 16));
- $bytes -= 1024;
- }
- echo "<br>while loop ends. total userdata(*showing [0] only edit later)->";
- print_r(unpack("H*",$self->userdata[0]));
- $self->session_id = hexdec( $u['h6'].$u['h5'] );
- @socket_recvfrom($self->zkclient, $data_recv, 1024, 0, $self->ip, $self->port);
- echo "<br><br>last data received<br>";
- print_r(unpack("H*",$data_recv));
- echo "<br><br>";
- }
-
-
- $users = array();
- if ( count($self->userdata) > 0 ) {
- //The first 4 bytes don't seem to be related to the user
- for ( $x=0; $x<count($self->userdata); $x++) {
- if ( $x > 0 ){
- echo "<br>";
- print_r(unpack("H*",$self->userdata[$x]));
- $self->userdata[$x] = substr( $self->userdata[$x], 8 );
- echo "<br>";
- print_r(unpack("H*",$self->userdata[$x]));
- }
- }
-
- $userdata = implode( '', $self->userdata );
- echo "<br><br>after imploding userdata<br>";
- print_r(unpack("H*",$userdata));
- echo "<br>";
- $userdata = substr($userdata,12);
- $usertemp = substr($userdata,6);
- file_put_contents("usertemp.txt",utf8_encode($usertemp));
- //$tmp=file_get_contents('usertemp.txt');
- echo "<br><br>strlen userdata<br>";
- echo strlen($userdata);
- echo "<br>userdata<br>";
- echo "<br><br>rejecting first 12 bytes<br>";
- print_r(unpack("H*",$userdata));
- echo "<br>userdata<br>";
- echo "<br>template taken<br>";
- print_r(unpack("H*",$usertemp));
- echo "<br>";
- print_r(iconv('ISO-8859-1', 'UTF-8', $userdata));
- echo "<br>userdata<br>";
- print_r(utf8_encode($userdata));
- echo "<br><br>strlen userdata after 11<br>";
- echo strlen($userdata);
- echo "<br>userdata<br>";
- while ( strlen($userdata) > 72 ) {
-
- $u = unpack( 'H144', substr( $userdata, 0, 72) );
- echo "<br>unpacked first 72 bytes h144 ".strlen($u[1])."<br>";
- print_r($u);
- echo "<br>";
- //$uid = hexdec( substr($u[1], 0, 4) ).' ';
- $u1 = hexdec( substr($u[1], 2, 2) );
- echo "<br> \$u1= ". $u1;
- echo "<br>";
- $u2 = hexdec( substr($u[1], 4, 2) );
- echo "<br> \$u2= ". $u2;
- echo "<br>";
- $uid = $u1+($u2*256);
- echo "<br> \$uid= ". $uid;
- echo "<br>";
- $cardno = hexdec( substr($u[1], 78, 2).substr($u[1], 76, 2).substr($u[1], 74, 2).substr($u[1], 72, 2) ).' ';
- $role = hexdec( substr($u[1], 4, 4) ).' ';
- $password = hex2bin( substr( $u[1], 8, 16 ) ).' ';
- $name = hex2bin( substr( $u[1], 24, 74 ) ). ' ';
- $userid = hex2bin( substr( $u[1], 98, 72) ).' ';
-
- //Clean up some messy characters from the user name
- $password = explode( chr(0), $password, 2 );
- $password = $password[0];
- $userid = explode( chr(0), $userid, 2);
- $userid = $userid[0];
- $name = explode(chr(0), $name, 3);
- $name = utf8_encode($name[0]);
- $cardno = str_pad($cardno,11,'0',STR_PAD_LEFT);
-
- if ( $name == "" )
- $name = $uid;
-
- $users[$uid] = array($userid, $name, $cardno, $uid,intval( $role ), $password);
-
- $userdata = substr( $userdata, 72 );
- }
- }
-
- return $users;
- } catch(ErrorException $e) {
- return FALSE;
- } catch(exception $e) {
- return False;
- }
- }
- ?>