PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/zklib/zkuser.php

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