PageRenderTime 29ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/zklib/zkfingerprint.php

https://gitlab.com/zubair_xenomorph/zubair_zklib
PHP | 305 lines | 206 code | 48 blank | 51 comment | 12 complexity | a160f3e5e3ec9a2e4fb11827be55923c MD5 | raw file
  1. <?php
  2. function sendUDP($self,$command,$command_string,$template)
  3. {
  4. $cmd='';
  5. $cmd_string='';
  6. $tempt = '';
  7. $strlen = strlen( $command );
  8. for( $i = 0; $i < $strlen; $i+=2 ) {
  9. $char = substr( $command, $i, 2 );
  10. $chr=chr(hexdec($char));
  11. //print_r(unpack("H*",$chr));
  12. $cmd=$cmd.$chr;
  13. }
  14. $strlen = strlen( $command_string );
  15. for( $i = 0; $i < $strlen; $i+=2 ) {
  16. $char = substr( $command_string, $i, 2 );
  17. echo $char;
  18. $chr=chr(hexdec($char));
  19. //print_r(unpack("H*",$chr));
  20. $cmd_string=$cmd_string.$chr;
  21. }
  22. $strlen = strlen( $template );
  23. for( $i = 0; $i < $strlen; $i+=2 ) {
  24. $char = substr( $template, $i, 2 );
  25. echo $char;
  26. $chr=chr(hexdec($char));
  27. //print_r(unpack("H*",$chr));
  28. $tempt=$tempt.$chr;
  29. }
  30. $cmd_string=$cmd_string.$tempt;
  31. echo "<br>command= ";
  32. print_r(unpack("H*",$cmd));
  33. echo "<br>command_string= ";
  34. print_r(unpack("H*",$cmd_string));
  35. echo "<br>template= ";
  36. print_r(unpack("H*",$tempt));
  37. $chksum = 0;
  38. $session_id = $self->session_id;
  39. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $self->data_recv, 0, 8) );
  40. $reply_id = hexdec( $u['h8'].$u['h7'] );
  41. echo "<br>cmd".print_r($cmd)."<br>";
  42. $buf = $self->createHeader(CMD_USERTEMP_WRQ, $chksum, $session_id, $reply_id, $cmd_string);
  43. echo "<br>total data sent= ";
  44. print_r(unpack("H*",$buf));
  45. socket_sendto($self->zkclient, $buf, strlen($buf), 0, $self->ip, $self->port);
  46. try {
  47. @socket_recvfrom($self->zkclient, $self->data_recv, 1024, 0, $self->ip, $self->port);
  48. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr( $self->data_recv, 0, 8 ) );
  49. $self->session_id = hexdec( $u['h6'].$u['h5'] );
  50. echo "<br><br> received something<br>";
  51. print_r(unpack("H*",$self->data_recv));
  52. return substr( $self->data_recv, 8 );
  53. } catch(ErrorException $e) {
  54. echo "<br><br> received exception<br>";
  55. return FALSE;
  56. } catch(exception $e) {
  57. echo "<br><br> received exception<br>";
  58. return False;
  59. }
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  62. function zksetfingerprint($self, $size, $pin, $finger_id, $valid, $template) {
  63. $command = CMD_USERTEMP_WRQ;
  64. //$command=2323;
  65. $u_size=unpack("H2h1/H2h2",intval($size));
  66. echo "<br>unpacked size<br>".($size%256)."<br>".(($size-$size%256)/256)."<br>".gettype($size)."<br>";
  67. $garbage=substr($template,0,500);
  68. $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;
  69. //$command_string=chr(3).chr(5).chr(6);
  70. echo "<br>cmd str<br>";
  71. print_r(unpack("H*",$command_string));
  72. //$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);
  73. //$byte1 = chr((int)($uid % 256));
  74. //echo "<br>byte1=<br>";
  75. //print_r(unpack("H*",$byte1));
  76. // $byte2 = chr((int)($uid >> 8));
  77. //echo "<br>byte1=<br>";
  78. //print_r(unpack("H*",$byte2));
  79. //echo "<br>strpad pass=<br>";
  80. //print_r(str_pad($password, 8, chr(0)));
  81. //echo "<br>strpad pass unpackd=<br>";
  82. //print_r(unpack("H*",str_pad($password, 8, chr(0))));
  83. // $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);
  84. //echo "<br>unpacked cmd str<br>";
  85. //print_r(unpack("H*",$command_string));
  86. $chksum = 0;
  87. $session_id = $self->session_id;
  88. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $self->data_recv, 0, 8) );
  89. $reply_id = hexdec( $u['h8'].$u['h7'] );
  90. $buf = $self->createHeader($command, $chksum, $session_id, $reply_id, $command_string);
  91. echo "<br>buffer========<br>";
  92. print_r(unpack("H*",$buf));
  93. echo "<br><br>";
  94. socket_sendto($self->zkclient, $buf, strlen($buf), 0, $self->ip, $self->port);
  95. try {
  96. @socket_recvfrom($self->zkclient, $self->data_recv, 1024, 0, $self->ip, $self->port);
  97. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr( $self->data_recv, 0, 8 ) );
  98. $self->session_id = hexdec( $u['h6'].$u['h5'] );
  99. echo "<br><br> received something<br>";
  100. print_r(unpack("H*",$self->data_recv));
  101. return substr( $self->data_recv, 8 );
  102. } catch(ErrorException $e) {
  103. echo "<br><br> received exception<br>";
  104. return FALSE;
  105. } catch(exception $e) {
  106. echo "<br><br> received exception<br>";
  107. return False;
  108. }
  109. }
  110. function zkgetfingerprint($self) {
  111. $command = CMD_USERTEMP_RRQ;
  112. $command_string = chr(2);
  113. $chksum = 0;
  114. $session_id = $self->session_id;
  115. echo "<br><br>sid".$session_id." cmd".$command."data_recv= ".($self->data_recv)."<br>";
  116. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $self->data_recv, 0, 8) );
  117. //echo substr($self->data_recv,0,8)." ";
  118. print_r( unpack("H*",substr($self->data_recv,0,8)));
  119. echo gettype($u);
  120. print_r($u);
  121. //echo gettype($u['h8']);
  122. //echo "substr";
  123. //echo chr(5);
  124. //echo "substr";
  125. //echo substr( $self->data_recv, 0, 8);
  126. $reply_id = hexdec( $u['h8'].$u['h7'] );
  127. echo "reply_id = ".$reply_id."<br>";
  128. echo "before createheader<br>"."command = ".dechex($command)." chksum= ".dechex($chksum)." session id = ".dechex($session_id)." reply_id = ".dechex($reply_id) . "<br>";
  129. $buf = $self->createHeader($command, $chksum, $session_id, $reply_id, $command_string);
  130. echo "buf = ";
  131. print_r(unpack("H*",$buf));
  132. //echo decbin(52);
  133. //echo "<br>";
  134. socket_sendto($self->zkclient, $buf, strlen($buf), 0, $self->ip, $self->port);
  135. try {
  136. @socket_recvfrom($self->zkclient, $self->data_recv, 1024, 0, $self->ip, $self->port);
  137. echo "<br><br>first recvfrm<br>";
  138. print_r(unpack("H*",$self->data_recv));
  139. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr( $self->data_recv, 0, 8 ) );
  140. echo "<br><br>\$u taken from data_recv <br>";
  141. print_r($u);
  142. if ( getSizeUser($self) ) {
  143. $bytes = getSizeUser($self);
  144. echo "<br><br>entering while loop-bytes = ".$bytes."<br>";
  145. while ( $bytes > 0 ) {
  146. @socket_recvfrom($self->zkclient, $data_recv, 1032, 0, $self->ip, $self->port);
  147. array_push( $self->userdata, $data_recv);
  148. echo "<br>in loop<br>";
  149. $value = unpack('H*', $data_recv);
  150. print_r($value);
  151. echo "<br><br>";
  152. /*
  153. for($i=0;$i<count($value);$i++)
  154. {
  155. $c = chr($value[$i+1]);
  156. $utf8 = utf8_decode(chr($value[$i+1]));
  157. $iso88591_1 = utf8_decode($utf8);
  158. $iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $utf8);
  159. //$iso88591_2 = mb_convert_encoding($utf8, 'ISO-8859-1', 'UTF-8');
  160. $iso88591 = chr($value[$i+1]);
  161. $utf8_1 = utf8_encode($iso88591);
  162. $utf8_2 = iconv('ISO-8859-1', 'UTF-8', $iso88591);
  163. //$utf8_2 = mb_convert_encoding($iso88591, 'UTF-8', 'ISO-8859-1');
  164. 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]));
  165. echo "<br>";
  166. echo $utf8_1;
  167. }
  168. */
  169. //$binary = base_convert($value[1], 16, 2);
  170. //echo pack('H*', base_convert($binary, 2, 16));
  171. $bytes -= 1024;
  172. }
  173. echo "<br>while loop ends. total userdata(*showing [0] only edit later)->";
  174. print_r(unpack("H*",$self->userdata[0]));
  175. $self->session_id = hexdec( $u['h6'].$u['h5'] );
  176. @socket_recvfrom($self->zkclient, $data_recv, 1024, 0, $self->ip, $self->port);
  177. echo "<br><br>last data received<br>";
  178. print_r(unpack("H*",$data_recv));
  179. echo "<br><br>";
  180. }
  181. $users = array();
  182. if ( count($self->userdata) > 0 ) {
  183. //The first 4 bytes don't seem to be related to the user
  184. for ( $x=0; $x<count($self->userdata); $x++) {
  185. if ( $x > 0 ){
  186. echo "<br>";
  187. print_r(unpack("H*",$self->userdata[$x]));
  188. $self->userdata[$x] = substr( $self->userdata[$x], 8 );
  189. echo "<br>";
  190. print_r(unpack("H*",$self->userdata[$x]));
  191. }
  192. }
  193. $userdata = implode( '', $self->userdata );
  194. echo "<br><br>after imploding userdata<br>";
  195. print_r(unpack("H*",$userdata));
  196. echo "<br>";
  197. $userdata = substr($userdata,12);
  198. $usertemp = substr($userdata,6);
  199. file_put_contents("usertemp.txt",utf8_encode($usertemp));
  200. //$tmp=file_get_contents('usertemp.txt');
  201. echo "<br><br>strlen userdata<br>";
  202. echo strlen($userdata);
  203. echo "<br>userdata<br>";
  204. echo "<br><br>rejecting first 12 bytes<br>";
  205. print_r(unpack("H*",$userdata));
  206. echo "<br>userdata<br>";
  207. echo "<br>template taken<br>";
  208. print_r(unpack("H*",$usertemp));
  209. echo "<br>";
  210. print_r(iconv('ISO-8859-1', 'UTF-8', $userdata));
  211. echo "<br>userdata<br>";
  212. print_r(utf8_encode($userdata));
  213. echo "<br><br>strlen userdata after 11<br>";
  214. echo strlen($userdata);
  215. echo "<br>userdata<br>";
  216. while ( strlen($userdata) > 72 ) {
  217. $u = unpack( 'H144', substr( $userdata, 0, 72) );
  218. echo "<br>unpacked first 72 bytes h144 ".strlen($u[1])."<br>";
  219. print_r($u);
  220. echo "<br>";
  221. //$uid = hexdec( substr($u[1], 0, 4) ).' ';
  222. $u1 = hexdec( substr($u[1], 2, 2) );
  223. echo "<br> \$u1= ". $u1;
  224. echo "<br>";
  225. $u2 = hexdec( substr($u[1], 4, 2) );
  226. echo "<br> \$u2= ". $u2;
  227. echo "<br>";
  228. $uid = $u1+($u2*256);
  229. echo "<br> \$uid= ". $uid;
  230. echo "<br>";
  231. $cardno = hexdec( substr($u[1], 78, 2).substr($u[1], 76, 2).substr($u[1], 74, 2).substr($u[1], 72, 2) ).' ';
  232. $role = hexdec( substr($u[1], 4, 4) ).' ';
  233. $password = hex2bin( substr( $u[1], 8, 16 ) ).' ';
  234. $name = hex2bin( substr( $u[1], 24, 74 ) ). ' ';
  235. $userid = hex2bin( substr( $u[1], 98, 72) ).' ';
  236. //Clean up some messy characters from the user name
  237. $password = explode( chr(0), $password, 2 );
  238. $password = $password[0];
  239. $userid = explode( chr(0), $userid, 2);
  240. $userid = $userid[0];
  241. $name = explode(chr(0), $name, 3);
  242. $name = utf8_encode($name[0]);
  243. $cardno = str_pad($cardno,11,'0',STR_PAD_LEFT);
  244. if ( $name == "" )
  245. $name = $uid;
  246. $users[$uid] = array($userid, $name, $cardno, $uid,intval( $role ), $password);
  247. $userdata = substr( $userdata, 72 );
  248. }
  249. }
  250. return $users;
  251. } catch(ErrorException $e) {
  252. return FALSE;
  253. } catch(exception $e) {
  254. return False;
  255. }
  256. }
  257. ?>