PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/zklib/zktime.php

https://gitlab.com/zubair_xenomorph/zubair_zklib
PHP | 67 lines | 50 code | 17 blank | 0 comment | 1 complexity | fbdd110b5b5d7409b043202b423b51ab MD5 | raw file
  1. <?php
  2. function reverseHex($hexstr) {
  3. $tmp = '';
  4. for ( $i=strlen($hexstr); $i>=0; $i-- ) {
  5. $tmp .= substr($hexstr, $i, 2);
  6. $i--;
  7. }
  8. return $tmp;
  9. }
  10. function zksettime($self, $t) {
  11. $command = CMD_SET_TIME;
  12. $command_string = pack('I',encode_time($t));
  13. $chksum = 0;
  14. $session_id = $self->session_id;
  15. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $self->data_recv, 0, 8) );
  16. $reply_id = hexdec( $u['h8'].$u['h7'] );
  17. $buf = $self->createHeader($command, $chksum, $session_id, $reply_id, $command_string);
  18. socket_sendto($self->zkclient, $buf, strlen($buf), 0, $self->ip, $self->port);
  19. try {
  20. @socket_recvfrom($self->zkclient, $self->data_recv, 1024, 0, $self->ip, $self->port);
  21. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6', substr( $self->data_recv, 0, 8 ) );
  22. $self->session_id = hexdec( $u['h6'].$u['h5'] );
  23. return substr( $self->data_recv, 8 );
  24. } catch(ErrorException $e) {
  25. return FALSE;
  26. } catch(exception $e) {
  27. return False;
  28. }
  29. }
  30. function zkgettime($self) {
  31. $command = CMD_GET_TIME;
  32. $command_string = '';
  33. $chksum = 0;
  34. $session_id = $self->session_id;
  35. $u = unpack('H2h1/H2h2/H2h3/H2h4/H2h5/H2h6/H2h7/H2h8', substr( $self->data_recv, 0, 8) );
  36. $reply_id = hexdec( $u['h8'].$u['h7'] );
  37. $buf = $self->createHeader($command, $chksum, $session_id, $reply_id, $command_string);
  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 decode_time( hexdec( reverseHex( bin2hex( substr( $self->data_recv, 8 ) ) ) ) );
  44. } catch(ErrorException $e) {
  45. return FALSE;
  46. } catch(exception $e) {
  47. return False;
  48. }
  49. }
  50. ?>