From 3bc98ad2acbb6ccd46361cdf1a744ae613f7bcf9 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 15:57:59 -0300 Subject: [PATCH 01/44] Adjustemnts to readme --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 462c79c..76cb5fa 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # PHP ZKLib -Attendance Machine Library for PHP with a connection to the network using the UDP protocol and port 4370 +PHP library for communication with ZKTeco Attendance Machine -## Donate bitcoin +Adjsutemnts for model ZEM500 -![Donate](https://blockr.io/api/v1/address/Qr/1BTsPJFMwSiiQnTusDXUF9YA8Hrm1NLQTD) - -1BTsPJFMwSiiQnTusDXUF9YA8Hrm1NLQTD +## Donate +` +Bitcoin : 187w4iNVHX44y2PC96AuhP286aUKNjcrXV +Litecoin: LVutsPn9jaoC6SScdxsGMM2uAMvPbjNZXq +PIVX : D81ZZt8jAvWQFaLhtx3f4ntstUCCYBcdne +` From ddf1ce01ef778e6656f4f31025c0b243d7e32fbf Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 16:02:23 -0300 Subject: [PATCH 02/44] Tweaking readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 76cb5fa..f608c0d 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ PHP library for communication with ZKTeco Attendance Machine Adjsutemnts for model ZEM500 -## Donate -` +### Donations +``` Bitcoin : 187w4iNVHX44y2PC96AuhP286aUKNjcrXV Litecoin: LVutsPn9jaoC6SScdxsGMM2uAMvPbjNZXq PIVX : D81ZZt8jAvWQFaLhtx3f4ntstUCCYBcdne -` +``` From c32c900022b66f0ac8fa3073560d97617ad1f6ae Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 16:03:45 -0300 Subject: [PATCH 03/44] Adjustemnts for model ZEM500 --- README.md | 2 +- ZKLib.php | 109 +++++++++++++++++++++++++++++++++---------- ZKLib/Attendance.php | 32 ++++++------- ZKLib/User.php | 40 ++++++++-------- zktest.php | 12 +++-- 5 files changed, 128 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index f608c0d..394cab8 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ PHP library for communication with ZKTeco Attendance Machine -Adjsutemnts for model ZEM500 +Adjustemnts for model ZEM500 ### Donations ``` diff --git a/ZKLib.php b/ZKLib.php index 42828f5..8055e72 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -28,9 +28,9 @@ class ZKLib { const LEVEL_USER = 0; const LEVEL_ADMIN = 14; const DEVICE_GENERAL_INFO_STRING_LENGTH = 184; - + const TIME_OFFSSET = 936572400; - + /** * @var $socket */ @@ -170,7 +170,7 @@ protected function createCheckSum($buffer){ } return ($checksum & self::USHRT_MAX); } - + function checkValid($reply, $extraResponses = null) { /*Checks a returned packet to see if it returned CMD_ACK_OK, indicating success*/ if ($extraResponses){ @@ -329,7 +329,7 @@ public function clearAttendance(){ error_log($ex->getMessage()); } } - + public function clearUsers(){ try { return $this->execute(self::CMD_CLEAR_DATA); @@ -345,9 +345,9 @@ public function clearAdmins(){ error_log($ex->getMessage()); } } - + /** - * + * * @return \ZKLib\Attendance[] */ public function getAttendance() @@ -360,7 +360,7 @@ public function getAttendance() } try { $this->execute(self::CMD_ATTLOG_RRQ); - + $attData = ''; if($size = $this->getPrepareDataSize()) { @socket_recvfrom($this->socket, $attData, $size, MSG_WAITALL, $this->ip, $this->port); @@ -368,14 +368,23 @@ public function getAttendance() @socket_recvfrom($this->socket, $data, 1024, 0, $this->ip, $this->port); $result = array(); if ($attData){ - foreach (str_split(substr($attData, 10), 40) as $attInfo){ - $data = unpack('x2/vrecordId/Z16userId/@29/Vtime/ctype', $attInfo); - $data['time'] += self::TIME_OFFSSET; - $result[$data['recordId']] = \ZKLib\Attendance::construct($data); + foreach (str_split(substr($attData, 12), 16) as $attInfo){ + if (strlen($attInfo) < 16) { + continue; + } + $data = unpack('vuserId/vtype/Vtime/cstatus/cjnkb/vjnkc/Vworkcode', $attInfo); + $dateTime = $this->decodeTime($data['time']); + $result[] = \ZKLib\Attendance::construct( + $data['workcode'], + $data['userId'], + $dateTime, + $data['type'], + $data['status'] + ); } } return $result; - + } catch (\Exception $ex) { error_log($ex->getMessage()); } @@ -384,6 +393,7 @@ public function getAttendance() private function getPrepareDataSize() { $response = unpack('vcommand/vchecksum/vsession_id/vreply_id/vsize', $this->data); + return ( $response['command'] == self::CMD_PREPARE_DATA ) ? $response['size'] : false; } @@ -401,13 +411,13 @@ protected function func_removeAccents($s){ $s = str_replace(array('`', "'", '"', '^', '~'), '', $s); return strtr($s, "\x01\x02\x03\x04\x05", '`\'"^~'); } - - + + /** * @param \ZKLib\User $user */ public function setUser($user){ - return $this->execute(self::CMD_SET_USER, pack('vCa8a24VCx8a9x15', + return $this->execute(self::CMD_SET_USER, pack('vCa8a24VCx8a9x15', $user->getRecordId(), $user->getRole(), $user->getPassword(), @@ -417,7 +427,7 @@ public function setUser($user){ $user->getUserId() )); } - + /** * @return \ZKLib\User[] */ @@ -425,13 +435,13 @@ public function getUser(){ if (($free = $this->getFreeSize()) && !$free->getUsersStored()){ return array(); } - + if (!defined('__ZKLib_User')){ require_once __DIR__.'/ZKLib/User.php'; } try { $this->execute(self::CMD_USERTEMP_RRQ); - + $usersData = ''; if($size = $this->getPrepareDataSize()) { do { @@ -440,21 +450,32 @@ public function getUser(){ } while($size > 0); } @socket_recvfrom($this->socket, $data, 1024, 0, $this->ip, $this->port); + $result = array(); if ($usersData){ - foreach (str_split(substr($usersData, 4), 72) as $userInfo){ - $user = unpack('vrecordId/Crole/Z8password/Z24name/VcardNo/x9/Z9userId', str_pad($userInfo, 72, '\0')); - $user['name'] = $user['name']; - $result[$user['recordId']] = \ZKLib\User::construct($user); + foreach (str_split(substr($usersData, 4), 28) as $userInfo){ + if (strlen($userInfo) < 28) { + continue; + } + $user = unpack('vrecordId/Crole/a5password/a8name/VcardNo/VgroupId/VuserId', $userInfo); + $result[$user['recordId']] = \ZKLib\User::construct( + $user['recordId'], + $user['role'], + $user['password'], + $user['name'], + $user['cardNo'], + $user['groupId'], + $user['userId'] + ); } } return $result; } catch (\Exception $ex) { error_log($ex->getMessage()); } - + } - + /** * @return \ZKLib\Capacity|boolean */ @@ -468,4 +489,42 @@ public function getFreeSize() } return false; } -} \ No newline at end of file + + /** + * @return DateTime + */ + public function decodeTime($encodedTime) + { + $sec = $encodedTime % 60; + $encodedTime /= 60; + + $min = $encodedTime % 60; + $encodedTime /= 60; + + $hour = $encodedTime % 24; + $encodedTime /= 24; + + $day = ($encodedTime % 31) + 1; + $encodedTime /= 31; + + $month = ($encodedTime % 12) + 1; + $encodedTime /= 12; + + $year = $encodedTime + 2000; + + $decoded = new DateTime(); + $decoded->setDate($year, $month, $day) + ->setTime($hour, $min, $sec); + + return $decoded; + } + + /** + * @return integer + */ + public function encodeTime(\DateTime $t) + { + return ( ($t->format('Y') % 100) * 12 * 31 + (($t->format('n') - 1) * 31) + $t->format('j') - 1) * + (24 * 60 * 60) + ($t->format('G') * 60 + $t->format('i')) * 60 + $t->format('s'); + } +} diff --git a/ZKLib/Attendance.php b/ZKLib/Attendance.php index da42737..814fba9 100644 --- a/ZKLib/Attendance.php +++ b/ZKLib/Attendance.php @@ -3,46 +3,46 @@ if (!defined('__ZKLib_Attendance')){ - define('__ZKLib_Attendance', true); - + define('__ZKLib_Attendance', true); + class Attendance { private $recordId; private $userId; private $type; + private $status; private $time; - + public function getType(){ return $this->type; } - + public function getRecordId(){ return $this->recordId; } - + public function getUserId(){ return $this->userId; } - + + public function getStatus(){ + return $this->status; + } + /** * @return \DateTime */ public function getDateTime(){ return $this->time; } - - public static function construct(/*$recordId, $userId, $timestamp, $type*/){ - if ((func_num_args() == 1) && is_array(func_get_arg(0))){ - $args = func_get_arg(0); - } else { - $args = func_get_args(); - } - list($recordId, $userId, $timestamp, $type) = array_values($args); + + public static function construct($recordId, $userId, \DateTime $dateTime, $type, $status){ $instance = new self(); $instance->recordId = $recordId; $instance->userId = $userId; $instance->type = $type; - $instance->time = \DateTime::createFromFormat('U', $timestamp); + $instance->time = $dateTime; + $instance->status = $status; return $instance; } } -} \ No newline at end of file +} diff --git a/ZKLib/User.php b/ZKLib/User.php index 4341132..a0ba892 100644 --- a/ZKLib/User.php +++ b/ZKLib/User.php @@ -1,60 +1,60 @@ recordId; } - + public function getRole(){ return $this->role; } - + public function getPassword(){ return $this->password; } - + public function getName(){ return $this->name; } - + public function getCardNo(){ return $this->cardNo; } - + public function getUserId(){ return $this->userId; } - - public static function construct(/*$recordId, $role, $password, $name, $cardNo, $userId*/){ - if ((func_num_args() == 1) && is_array(func_get_arg(0))){ - $args = func_get_arg(0); - } else { - $args = func_get_args(); - } - list($recordId, $role, $password, $name, $cardNo, $userId) = array_values($args); - + + public function getGroupId(){ + return $this->groupId; + } + + public static function construct($recordId, $role, $password, $name, $cardNo, $groupId, $userId) { + $instance = new self(); $instance->recordId = $recordId; $instance->role = $role; $instance->password = $password; $instance->name = $name; $instance->cardNo = $cardNo; + $instance->groupId = $groupId; $instance->userId = $userId; - + return $instance; } - - } \ No newline at end of file + + } diff --git a/zktest.php b/zktest.php index 54edf1e..8404522 100644 --- a/zktest.php +++ b/zktest.php @@ -2,19 +2,19 @@ ZK Test - + connect(); if ( $ret ): $zk->disable(); ?> - + @@ -85,7 +85,7 @@ endforeach; } catch (Exception $e) { header("HTTP/1.0 404 Not Found"); - header('HTTP', true, 500); // 500 internal server error + header('HTTP', true, 500); // 500 internal server error } //$zk->clearAdmins(); ?> @@ -99,6 +99,7 @@ + getRecordId(); ?> + Date: Wed, 28 Jun 2017 16:38:27 -0300 Subject: [PATCH 04/44] Removing TIME_OFFSSET. Using documented time format. --- ZKLib.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index 8055e72..6f04491 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -29,8 +29,6 @@ class ZKLib { const LEVEL_ADMIN = 14; const DEVICE_GENERAL_INFO_STRING_LENGTH = 184; - const TIME_OFFSSET = 936572400; - /** * @var $socket */ @@ -303,7 +301,8 @@ public function getVersion() public function getTime() { $data = $this->execute(self::CMD_GET_TIME); - return \DateTime::createFromFormat('U', current(unpack('V', $data)) + self::TIME_OFFSSET); + $encodedTime = current(unpack('V', $data)); + return $this->decodeTime($encodedTime); } private function reverseHex($hexstr) { @@ -319,7 +318,7 @@ private function reverseHex($hexstr) { public function setTime(\DateTime $dateTime) { - return $this->execute(self::CMD_SET_TIME, pack('I', $dateTime->getTimestamp() - self::TIME_OFFSSET)); + return $this->execute(self::CMD_SET_TIME, pack('V', $this->encodeTime($dateTime))); } public function clearAttendance(){ @@ -524,7 +523,8 @@ public function decodeTime($encodedTime) */ public function encodeTime(\DateTime $t) { - return ( ($t->format('Y') % 100) * 12 * 31 + (($t->format('n') - 1) * 31) + $t->format('j') - 1) * - (24 * 60 * 60) + ($t->format('G') * 60 + $t->format('i')) * 60 + $t->format('s'); + return + (($t->format('Y') % 100) * 12 * 31 + (($t->format('n') - 1) * 31) + $t->format('j') - 1) * (24 * 60 * 60) + + ($t->format('G') * 60 + $t->format('i')) * 60 + $t->format('s'); } } From d230320c254c301b1cbfd7d21b8e53d8dc55ca9f Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 16:40:09 -0300 Subject: [PATCH 05/44] Removing not needed method --- ZKLib.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index 6f04491..f6cf7da 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -305,17 +305,6 @@ public function getTime() return $this->decodeTime($encodedTime); } - private function reverseHex($hexstr) { - $tmp = ''; - - for ( $i=strlen($hexstr); $i>=0; $i-- ) { - $tmp .= substr($hexstr, $i, 2); - $i--; - } - - return $tmp; - } - public function setTime(\DateTime $dateTime) { return $this->execute(self::CMD_SET_TIME, pack('V', $this->encodeTime($dateTime))); From c8ac474597d614958e477ded3b4a11385fc7d42b Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 16:42:53 -0300 Subject: [PATCH 06/44] Removing unuseful try/catch blocks --- ZKLib.php | 165 ++++++++++++++++++++++-------------------------------- 1 file changed, 67 insertions(+), 98 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index f6cf7da..7e0a40a 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -179,16 +179,11 @@ function checkValid($reply, $extraResponses = null) { public function connect() { - try { - $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); - socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, $this->timeout); + $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); + socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, $this->timeout); - $this->reply_id = (-1 + self::USHRT_MAX); - return $this->execute(self::CMD_CONNECT, null, [self::CMD_ACK_UNAUTH]); - - } catch (\Exception $ex) { - return false; - } + $this->reply_id = (-1 + self::USHRT_MAX); + return $this->execute(self::CMD_CONNECT, null, [self::CMD_ACK_UNAUTH]); } public function disconnect() @@ -209,30 +204,24 @@ private function unpackResponse(){ private function execute($command, $command_string = null, $extraResponses = array()) { - try { - $buf = $this->createHeader($command, $command_string); + $buf = $this->createHeader($command, $command_string); - socket_sendto($this->socket, $buf, strlen($buf), 0, $this->ip, $this->port); - @socket_recvfrom($this->socket, $this->data, 1024, 0, $this->ip, $this->port); + socket_sendto($this->socket, $buf, strlen($buf), 0, $this->ip, $this->port); + @socket_recvfrom($this->socket, $this->data, 1024, 0, $this->ip, $this->port); - if ( strlen( $this->data ) > 0 ) { - $this->unpackResponse(); + if ( strlen( $this->data ) > 0 ) { + $this->unpackResponse(); - if ($this->checkValid($this->data, $extraResponses) ) { - if (strlen($this->data) > 8){ - if ($command_string){ - return preg_replace('/^'.preg_quote($command_string, '/').'=/', '', substr( $this->data, 8 )); - } - return substr( $this->data, 8 ); + if ($this->checkValid($this->data, $extraResponses) ) { + if (strlen($this->data) > 8){ + if ($command_string){ + return preg_replace('/^'.preg_quote($command_string, '/').'=/', '', substr( $this->data, 8 )); } - return true; + return substr( $this->data, 8 ); } + return true; } - } catch (\Exception $ex) { - var_dump($ex->getMessage()); } - - return false; } public function getDeviceName() @@ -311,27 +300,15 @@ public function setTime(\DateTime $dateTime) } public function clearAttendance(){ - try { - return $this->execute(self::CMD_CLEAR_ATTLOG); - } catch (\Exception $ex) { - error_log($ex->getMessage()); - } + return $this->execute(self::CMD_CLEAR_ATTLOG); } public function clearUsers(){ - try { - return $this->execute(self::CMD_CLEAR_DATA); - } catch (\Exception $ex) { - error_log($ex->getMessage()); - } + return $this->execute(self::CMD_CLEAR_DATA); } public function clearAdmins(){ - try { - return $this->execute(self::CMD_CLEAR_ADMIN); - } catch (\Exception $ex) { - error_log($ex->getMessage()); - } + return $this->execute(self::CMD_CLEAR_ADMIN); } /** @@ -346,36 +323,32 @@ public function getAttendance() if (!defined('__ZKLib_Attendance')){ require_once __DIR__.'/ZKLib/Attendance.php'; } - try { - $this->execute(self::CMD_ATTLOG_RRQ); - $attData = ''; - if($size = $this->getPrepareDataSize()) { - @socket_recvfrom($this->socket, $attData, $size, MSG_WAITALL, $this->ip, $this->port); - } - @socket_recvfrom($this->socket, $data, 1024, 0, $this->ip, $this->port); - $result = array(); - if ($attData){ - foreach (str_split(substr($attData, 12), 16) as $attInfo){ - if (strlen($attInfo) < 16) { - continue; - } - $data = unpack('vuserId/vtype/Vtime/cstatus/cjnkb/vjnkc/Vworkcode', $attInfo); - $dateTime = $this->decodeTime($data['time']); - $result[] = \ZKLib\Attendance::construct( - $data['workcode'], - $data['userId'], - $dateTime, - $data['type'], - $data['status'] - ); + $this->execute(self::CMD_ATTLOG_RRQ); + + $attData = ''; + if($size = $this->getPrepareDataSize()) { + @socket_recvfrom($this->socket, $attData, $size, MSG_WAITALL, $this->ip, $this->port); + } + @socket_recvfrom($this->socket, $data, 1024, 0, $this->ip, $this->port); + $result = array(); + if ($attData){ + foreach (str_split(substr($attData, 12), 16) as $attInfo){ + if (strlen($attInfo) < 16) { + continue; } + $data = unpack('vuserId/vtype/Vtime/cstatus/cjnkb/vjnkc/Vworkcode', $attInfo); + $dateTime = $this->decodeTime($data['time']); + $result[] = \ZKLib\Attendance::construct( + $data['workcode'], + $data['userId'], + $dateTime, + $data['type'], + $data['status'] + ); } - return $result; - - } catch (\Exception $ex) { - error_log($ex->getMessage()); } + return $result; } private function getPrepareDataSize() @@ -427,41 +400,37 @@ public function getUser(){ if (!defined('__ZKLib_User')){ require_once __DIR__.'/ZKLib/User.php'; } - try { - $this->execute(self::CMD_USERTEMP_RRQ); - - $usersData = ''; - if($size = $this->getPrepareDataSize()) { - do { - $size -= @socket_recvfrom($this->socket, $data, $size, 0, $this->ip, $this->port); - $usersData .= substr($data, 8); - } while($size > 0); - } - @socket_recvfrom($this->socket, $data, 1024, 0, $this->ip, $this->port); - $result = array(); - if ($usersData){ - foreach (str_split(substr($usersData, 4), 28) as $userInfo){ - if (strlen($userInfo) < 28) { - continue; - } - $user = unpack('vrecordId/Crole/a5password/a8name/VcardNo/VgroupId/VuserId', $userInfo); - $result[$user['recordId']] = \ZKLib\User::construct( - $user['recordId'], - $user['role'], - $user['password'], - $user['name'], - $user['cardNo'], - $user['groupId'], - $user['userId'] - ); + $this->execute(self::CMD_USERTEMP_RRQ); + + $usersData = ''; + if($size = $this->getPrepareDataSize()) { + do { + $size -= @socket_recvfrom($this->socket, $data, $size, 0, $this->ip, $this->port); + $usersData .= substr($data, 8); + } while($size > 0); + } + @socket_recvfrom($this->socket, $data, 1024, 0, $this->ip, $this->port); + + $result = array(); + if ($usersData){ + foreach (str_split(substr($usersData, 4), 28) as $userInfo){ + if (strlen($userInfo) < 28) { + continue; } + $user = unpack('vrecordId/Crole/a5password/a8name/VcardNo/VgroupId/VuserId', $userInfo); + $result[$user['recordId']] = \ZKLib\User::construct( + $user['recordId'], + $user['role'], + $user['password'], + $user['name'], + $user['cardNo'], + $user['groupId'], + $user['userId'] + ); } - return $result; - } catch (\Exception $ex) { - error_log($ex->getMessage()); } - + return $result; } /** From 317f0308d10fab82ad8d471408fc993f69027520 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 16:53:48 -0300 Subject: [PATCH 07/44] Adding twitter bootstrap to zktest page --- zktest.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/zktest.php b/zktest.php index 8404522..0756d55 100644 --- a/zktest.php +++ b/zktest.php @@ -1,9 +1,11 @@ ZK Test + +
connect(); - if ( $ret ): + if ( $ret ): $zk->disable(); ?> +

PHP ZK Library

-
StatusIndex UID TypeStatus DateTime
getUserId(); ?> getType(); ?>getStatus(); ?> getDateTime()->format('r'); ?>
+
@@ -48,7 +51,9 @@
Status Connected

- +
+
+
@@ -90,8 +95,10 @@ //$zk->clearAdmins(); ?>
Data User
- - + + +
+
@@ -116,11 +123,13 @@ endforeach; ?>
Data Attendance
- + + enable(); $zk->disconnect(); endif ?> + From dee29637802365a8f86738a69172ad036ac3a45a Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 16:54:56 -0300 Subject: [PATCH 08/44] Spaces to tabs --- zktest.php | 234 ++++++++++++++++++++++++++--------------------------- 1 file changed, 117 insertions(+), 117 deletions(-) diff --git a/zktest.php b/zktest.php index 0756d55..4c9d7b4 100644 --- a/zktest.php +++ b/zktest.php @@ -1,135 +1,135 @@ - - ZK Test - - + + ZK Test + + - -
+ +
connect(); - if ( $ret ): - $zk->disable(); - ?> -

PHP ZK Library

+ $ret = $zk->connect(); + if ( $ret ): + $zk->disable(); + ?> +

PHP ZK Library

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
StatusConnectedVersiongetVersion() ?>OS VersiongetOs() ?>PlatformgetPlatform() ?>
Firmware VersiongetPlatformVersion() ?>WorkCodegetWorkCode() ?>SSRgetSsr() ?>Pin WidthgetPinWidth() ?>
Face Function OngetFaceOn() ?>Serial NumbergetSerialNumber() ?>Device NamegetDeviceName(); ?>Get TimegetTime()->format('r') ?>
-
-
-
- - - - - - - - - - +
Data User
UIDIDNameRolePassword
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
StatusConnectedVersiongetVersion() ?>OS VersiongetOs() ?>PlatformgetPlatform() ?>
Firmware VersiongetPlatformVersion() ?>WorkCodegetWorkCode() ?>SSRgetSsr() ?>Pin WidthgetPinWidth() ?>
Face Function OngetFaceOn() ?>Serial NumbergetSerialNumber() ?>Device NamegetDeviceName(); ?>Get TimegetTime()->format('r') ?>
+
+
+
+ + + + + + + + + + - - clearUsers(); + + clearUsers(); //$zk->setUser(\ZKLib\User::construct(1, \ZKLib\User::PRIVILEGE_SUPERADMIN, '1', 'Admin', '', '')); foreach($zk->getUser() as $user): - $role = 'Unknown'; - switch ($user->getRole()){ + $role = 'Unknown'; + switch ($user->getRole()){ case \ZKLib\User::PRIVILEGE_COMMON_USER : $role = 'USER'; break; case \ZKLib\User::PRIVILEGE_ENROLLER : $role = 'ENROLLER'; break; case \ZKLib\User::PRIVILEGE_MANAGER : $role = 'MANAGER'; break; case \ZKLib\User::PRIVILEGE_SUPERADMIN : $role = 'ADMIN'; break; - } - ?> - - - - - - - - - clearAdmins(); - ?> -
Data User
UIDIDNameRolePassword Card number
getRecordId(); ?>getUserId(); ?>getName(); ?>getPassword(); ?>getCardNo(); ?>
-
+ } + ?> + + getRecordId(); ?> + getUserId(); ?> + getName(); ?> + + getPassword(); ?> + getCardNo(); ?> + + clearAdmins(); + ?> + +
-
- - - - - - - - - - - - +
Data Attendance
IndexUIDTypeStatusDateTime
+ + + + + + + + + + + getAttendance() as $attendance): - ?> - - - - - - - - -
Data Attendance
IndexUIDTypeStatusDateTime
getRecordId(); ?>getUserId(); ?>getType(); ?>getStatus(); ?>getDateTime()->format('r'); ?>
-
+ ?> + + getRecordId(); ?> + getUserId(); ?> + getType(); ?> + getStatus(); ?> + getDateTime()->format('r'); ?> + + + +
- enable(); - $zk->disconnect(); - endif + enable(); + $zk->disconnect(); + endif ?> -
- +
+ From e18d6e49d74434c6a30bc1d68564490b7a8d3c9c Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 18:39:49 -0300 Subject: [PATCH 09/44] Adding user timezone to data structure --- ZKLib.php | 31 +++++-------------------------- ZKLib/User.php | 8 +++++++- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index 7e0a40a..3af405b 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -58,11 +58,6 @@ class ZKLib { /** @var integer */ private $reply_id; - /** - * @var null - */ - private $result = null; - public function __construct($ip = '', $port = 4370) { $this->port = $port; @@ -123,24 +118,6 @@ public function setTimeout($timeout) return $this; } - /** - * @return null - */ - public function getResult() - { - return $this->result; - } - - /** - * @param null $result - * @return ZkSocket - */ - public function setResult($result) - { - $this->result = $result; - return $this; - } - private function createHeader($command, $command_string, $chksum=0) { $buf = pack('SSSS', $command, $chksum, $this->session_id, $this->reply_id).$command_string; $this->reply_id += 1; @@ -378,13 +355,14 @@ protected function func_removeAccents($s){ * @param \ZKLib\User $user */ public function setUser($user){ - return $this->execute(self::CMD_SET_USER, pack('vCa8a24VCx8a9x15', + return $this->execute(self::CMD_SET_USER, pack('vCa5a8a5CsV', $user->getRecordId(), $user->getRole(), $user->getPassword(), $this->func_removeAccents($user->getName()), $user->getCardNo(), - 1, + $user->getGroupId(), + $user->getTimeZone(), $user->getUserId() )); } @@ -418,7 +396,7 @@ public function getUser(){ if (strlen($userInfo) < 28) { continue; } - $user = unpack('vrecordId/Crole/a5password/a8name/VcardNo/VgroupId/VuserId', $userInfo); + $user = unpack('vrecordId/Crole/a5password/a8name/a5cardNo/CgroupId/stimeZone/VuserId', $userInfo); $result[$user['recordId']] = \ZKLib\User::construct( $user['recordId'], $user['role'], @@ -426,6 +404,7 @@ public function getUser(){ $user['name'], $user['cardNo'], $user['groupId'], + $user['timeZone'], $user['userId'] ); } diff --git a/ZKLib/User.php b/ZKLib/User.php index a0ba892..93db04e 100644 --- a/ZKLib/User.php +++ b/ZKLib/User.php @@ -14,6 +14,7 @@ class User { private $password; private $role; private $cardNo; + private $timeZone; public function getRecordId(){ return $this->recordId; @@ -43,7 +44,11 @@ public function getGroupId(){ return $this->groupId; } - public static function construct($recordId, $role, $password, $name, $cardNo, $groupId, $userId) { + public function getTimeZone(){ + return $this->timeZone; + } + + public static function construct($recordId, $role, $password, $name, $cardNo, $groupId, $timeZone, $userId) { $instance = new self(); $instance->recordId = $recordId; @@ -52,6 +57,7 @@ public static function construct($recordId, $role, $password, $name, $cardNo, $g $instance->name = $name; $instance->cardNo = $cardNo; $instance->groupId = $groupId; + $instance->timeZone = $timeZone; $instance->userId = $userId; return $instance; From 1c4a7639b9f2aff1d1c5c7d00ed0db5f661676ec Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 18:45:25 -0300 Subject: [PATCH 10/44] Displaying user timezone in test page --- zktest.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/zktest.php b/zktest.php index 4c9d7b4..b501a6e 100644 --- a/zktest.php +++ b/zktest.php @@ -7,6 +7,8 @@
Name Role Password - Card number + Group + TimeZone + Card number clearUsers(); - //$zk->setUser(\ZKLib\User::construct(1, \ZKLib\User::PRIVILEGE_SUPERADMIN, '1', 'Admin', '', '')); + // $zk->clearUsers(); + // $zk->setUser(User::construct(1, User::PRIVILEGE_SUPERADMIN, '1', 'Admin', '', '', -3, 1)); foreach($zk->getUser() as $user): $role = 'Unknown'; switch ($user->getRole()){ - case \ZKLib\User::PRIVILEGE_COMMON_USER : $role = 'USER'; break; - case \ZKLib\User::PRIVILEGE_ENROLLER : $role = 'ENROLLER'; break; - case \ZKLib\User::PRIVILEGE_MANAGER : $role = 'MANAGER'; break; - case \ZKLib\User::PRIVILEGE_SUPERADMIN : $role = 'ADMIN'; break; + case User::PRIVILEGE_COMMON_USER : $role = 'USER'; break; + case User::PRIVILEGE_ENROLLER : $role = 'ENROLLER'; break; + case User::PRIVILEGE_MANAGER : $role = 'MANAGER'; break; + case User::PRIVILEGE_SUPERADMIN : $role = 'ADMIN'; break; } ?> @@ -84,6 +88,8 @@ getName(); ?> getPassword(); ?> + getGroupId(); ?> + getTimeZone(); ?> getCardNo(); ?> clearAdmins(); + // $zk->clearAdmins(); ?>
From 2f0d0e8f1bc05e409f940005d56e9bdda1d34268 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 18:47:31 -0300 Subject: [PATCH 11/44] Using proper namespace on top --- ZKLib.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index 3af405b..8299fd8 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -1,4 +1,6 @@ decodeTime($data['time']); - $result[] = \ZKLib\Attendance::construct( + $result[] = Attendance::construct( $data['workcode'], $data['userId'], $dateTime, @@ -397,7 +399,7 @@ public function getUser(){ continue; } $user = unpack('vrecordId/Crole/a5password/a8name/a5cardNo/CgroupId/stimeZone/VuserId', $userInfo); - $result[$user['recordId']] = \ZKLib\User::construct( + $result[$user['recordId']] = User::construct( $user['recordId'], $user['role'], $user['password'], From 81d40f2f713ca83ecb793489a24bad6b20107859 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 18:55:09 -0300 Subject: [PATCH 12/44] Adding command to test voice --- ZKLib.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ZKLib.php b/ZKLib.php index 8299fd8..d3e6de3 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -8,6 +8,7 @@ class ZKLib { const CMD_EXIT = 1001; const CMD_ENABLEDEVICE = 1002; const CMD_DISABLEDEVICE = 1003; + const CMD_TESTVOICE = 1017; const CMD_ACK_OK = 2000; const CMD_ACK_ERROR = 2001; const CMD_ACK_DATA = 2002; @@ -213,6 +214,11 @@ public function enable() return $this->execute(self::CMD_ENABLEDEVICE); } + public function testVoice() + { + return $this->execute(self::CMD_TESTVOICE); + } + public function disable() { return $this->execute(self::CMD_DISABLEDEVICE, chr(0).chr(0)); From 499e6f4150958085807dc6cbb8a2d19b480df53f Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 28 Jun 2017 19:26:09 -0300 Subject: [PATCH 13/44] Arranging code --- ZKLib.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index d3e6de3..723f776 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -1,6 +1,7 @@ port = $port; @@ -174,8 +178,6 @@ public function disconnect() } } - private $response_code; - private $checksum; private function unpackResponse(){ foreach ($r = unpack('vresponse_code/vchecksum/vsession_id/vreply_id', $this->data) as $key => $value){ $this->{$key} = $value; @@ -429,7 +431,7 @@ public function getFreeSize() if (!defined('__ZKLib_Capacity')){ require_once __DIR__.'/ZKLib/Capacity.php'; } - return \ZKLib\Capacity::construct(unpack('x16/Vusers_stored/x4/Vtemplates_stored/x4/Vatt_logs_stored/x12/Vadmins_stored/Vpasswords_stored/Vtemplates_capacity/Vusers_capacity/Vatt_logs_capacity/Vtemplates_available/Vusers_available/Vatt_logs_available', $free_sizes_info)); + return Capacity::construct(unpack('x16/Vusers_stored/x4/Vtemplates_stored/x4/Vatt_logs_stored/x12/Vadmins_stored/Vpasswords_stored/Vtemplates_capacity/Vusers_capacity/Vatt_logs_capacity/Vtemplates_available/Vusers_available/Vatt_logs_available', $free_sizes_info)); } return false; } From 48a4ddc4c48a05ef59a07ce1fa92bca758fc8302 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Sat, 1 Jul 2017 16:09:21 -0300 Subject: [PATCH 14/44] Reading blocks of 1032 bytes --- ZKLib.php | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index 723f776..e92b7ff 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -51,7 +51,7 @@ class ZKLib { /** * @var array */ - private $timeout = array('sec'=>60,'usec'=>500000); + private $timeout = array('sec'=>30,'usec'=>500000); /** @var string */ private $data; @@ -314,10 +314,11 @@ public function getAttendance() $this->execute(self::CMD_ATTLOG_RRQ); $attData = ''; - if($size = $this->getPrepareDataSize()) { - @socket_recvfrom($this->socket, $attData, $size, MSG_WAITALL, $this->ip, $this->port); - } - @socket_recvfrom($this->socket, $data, 1024, 0, $this->ip, $this->port); + do { + $size = @socket_recvfrom($this->socket, $data, 1032, MSG_WAITALL, $this->ip, $this->port); + $attData .= $data; + } while ($size > 0 && $size != 8); + $result = array(); if ($attData){ foreach (str_split(substr($attData, 12), 16) as $attInfo){ @@ -335,6 +336,7 @@ public function getAttendance() ); } } + return $result; } @@ -392,17 +394,14 @@ public function getUser(){ $this->execute(self::CMD_USERTEMP_RRQ); $usersData = ''; - if($size = $this->getPrepareDataSize()) { - do { - $size -= @socket_recvfrom($this->socket, $data, $size, 0, $this->ip, $this->port); - $usersData .= substr($data, 8); - } while($size > 0); - } - @socket_recvfrom($this->socket, $data, 1024, 0, $this->ip, $this->port); + do { + $size = @socket_recvfrom($this->socket, $data, 1032, 0, $this->ip, $this->port); + $usersData .= $data; + } while($size > 0 && $size != 8); $result = array(); if ($usersData){ - foreach (str_split(substr($usersData, 4), 28) as $userInfo){ + foreach (str_split(substr($usersData, 12), 28) as $userInfo){ if (strlen($userInfo) < 28) { continue; } From 2a39f45f7f3f57c9cb60f5594dfa6bb10e265be1 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Sun, 2 Jul 2017 11:44:56 -0300 Subject: [PATCH 15/44] Fixing unpack of attendance data stream --- ZKLib.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index e92b7ff..85c3059 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -24,11 +24,12 @@ class ZKLib { const CMD_CLEAR_ATTLOG = 15; const CMD_DEL_USER = 18; const CMD_CLEAR_ADMIN = 20; - const CMD_WRITE_LCD = 66; const CMD_GET_TIME = 201; const CMD_SET_TIME = 202; const CMD_VERSION = 1100; const CMD_GET_FREE_SIZES = 50; + const CMD_WRITE_LCD = 66; + const CMD_CLEAR_LCD = 67; const LEVEL_USER = 0; const LEVEL_ADMIN = 14; const DEVICE_GENERAL_INFO_STRING_LENGTH = 184; @@ -318,17 +319,18 @@ public function getAttendance() $size = @socket_recvfrom($this->socket, $data, 1032, MSG_WAITALL, $this->ip, $this->port); $attData .= $data; } while ($size > 0 && $size != 8); + $attData = substr($attData, 12); + $attData = substr($attData, 0, -8); $result = array(); if ($attData){ - foreach (str_split(substr($attData, 12), 16) as $attInfo){ - if (strlen($attInfo) < 16) { + foreach (str_split($attData, 8) as $attInfo){ + if (strlen($attInfo) < 8) { continue; } - $data = unpack('vuserId/vtype/Vtime/cstatus/cjnkb/vjnkc/Vworkcode', $attInfo); + $data = unpack('vuserId/Ctype/Cstatus/Vtime', $attInfo); $dateTime = $this->decodeTime($data['time']); $result[] = Attendance::construct( - $data['workcode'], $data['userId'], $dateTime, $data['type'], @@ -398,10 +400,12 @@ public function getUser(){ $size = @socket_recvfrom($this->socket, $data, 1032, 0, $this->ip, $this->port); $usersData .= $data; } while($size > 0 && $size != 8); + $usersData = substr($usersData, 12); + $usersData = substr($usersData, 0, -8); $result = array(); if ($usersData){ - foreach (str_split(substr($usersData, 12), 28) as $userInfo){ + foreach (str_split($usersData, 28) as $userInfo){ if (strlen($userInfo) < 28) { continue; } From 9ac1882bdae5c3bf899f3e97224df5e7eb5674e8 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Sun, 2 Jul 2017 11:47:39 -0300 Subject: [PATCH 16/44] Adding methods to extract validation of attendance and wether it is a in or out --- ZKLib/Attendance.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/ZKLib/Attendance.php b/ZKLib/Attendance.php index 814fba9..709b8e5 100644 --- a/ZKLib/Attendance.php +++ b/ZKLib/Attendance.php @@ -6,7 +6,9 @@ define('__ZKLib_Attendance', true); class Attendance { - private $recordId; + const ATTENDANCE_BY_PASSWORD = 'by_password'; + const ATTENDANCE_BY_FINGERPRINT = 'by_fingerprint'; + private $userId; private $type; private $status; @@ -16,10 +18,6 @@ public function getType(){ return $this->type; } - public function getRecordId(){ - return $this->recordId; - } - public function getUserId(){ return $this->userId; } @@ -28,6 +26,14 @@ public function getStatus(){ return $this->status; } + public function isOut(){ + return ($this->type & 0x20) > 0; + } + + public function validatedBy(){ + return ($this->type & 0x08) ? static::ATTENDANCE_BY_FINGERPRINT : static::ATTENDANCE_BY_PASSWORD; + } + /** * @return \DateTime */ @@ -35,9 +41,8 @@ public function getDateTime(){ return $this->time; } - public static function construct($recordId, $userId, \DateTime $dateTime, $type, $status){ + public static function construct($userId, \DateTime $dateTime, $type, $status){ $instance = new self(); - $instance->recordId = $recordId; $instance->userId = $userId; $instance->type = $type; $instance->time = $dateTime; From 588444fa2fac2f1f5b90c6620553fc8a37e44bd8 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Sun, 2 Jul 2017 11:49:51 -0300 Subject: [PATCH 17/44] Displaying validation and attendance in/out --- zktest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/zktest.php b/zktest.php index b501a6e..a5ea95d 100644 --- a/zktest.php +++ b/zktest.php @@ -19,7 +19,7 @@ $zk->disable(); ?>

PHP ZK Library

- +
@@ -52,7 +52,7 @@
StatusgetTime()->format('r') ?>
-
+
@@ -109,9 +109,9 @@ - - + + @@ -119,9 +119,9 @@ foreach($zk->getAttendance() as $attendance): ?> - - + + From 897cb3d205b141e69dc080e5dc8eeb6e348ff3bd Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Sun, 2 Jul 2017 15:40:34 -0300 Subject: [PATCH 18/44] Throw an exception when socket communication has an error --- ZKLib.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index 85c3059..bd4339c 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -190,7 +190,10 @@ private function execute($command, $command_string = null, $extraResponses = arr $buf = $this->createHeader($command, $command_string); socket_sendto($this->socket, $buf, strlen($buf), 0, $this->ip, $this->port); - @socket_recvfrom($this->socket, $this->data, 1024, 0, $this->ip, $this->port); + $bytes = socket_recvfrom($this->socket, $this->data, 1024, 0, $this->ip, $this->port); + if ($bytes === false) { + throw new RuntimeException(socket_strerror(socket_last_error())); + } if ( strlen( $this->data ) > 0 ) { $this->unpackResponse(); @@ -316,7 +319,10 @@ public function getAttendance() $attData = ''; do { - $size = @socket_recvfrom($this->socket, $data, 1032, MSG_WAITALL, $this->ip, $this->port); + $size = socket_recvfrom($this->socket, $data, 1032, MSG_WAITALL, $this->ip, $this->port); + if ($size === false) { + throw new RuntimeException(socket_strerror(socket_last_error())); + } $attData .= $data; } while ($size > 0 && $size != 8); $attData = substr($attData, 12); @@ -397,7 +403,10 @@ public function getUser(){ $usersData = ''; do { - $size = @socket_recvfrom($this->socket, $data, 1032, 0, $this->ip, $this->port); + $size = socket_recvfrom($this->socket, $data, 1032, 0, $this->ip, $this->port); + if ($size === false) { + throw new RuntimeException(socket_strerror(socket_last_error())); + } $usersData .= $data; } while($size > 0 && $size != 8); $usersData = substr($usersData, 12); From cd6537668cbf8a2e5aa3313a03ee317251930d39 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Sun, 2 Jul 2017 15:50:59 -0300 Subject: [PATCH 19/44] Updating supported model --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 394cab8..05ca3b0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ PHP library for communication with ZKTeco Attendance Machine -Adjustemnts for model ZEM500 +Adjustemnts for model ZEM500 (SBTS H0201) ### Donations ``` From acbf2b8346386d88bcf4a9b4e9b9ed4247987083 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 5 Jul 2017 13:49:04 -0300 Subject: [PATCH 20/44] Adding composer.json --- composer.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..021f296 --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "wnasich/php_zklib", + "description": "PHP library for communication with ZKTeco Attendance Machine", + "keywords": ["time-attendance", "zkteco", "SBTS H0201"], + "homepage": "http://github.com/wnasich/php_zklib", + "type": "library", + "license": "MIT", + "minimum-stability": "dev", + "require": { + "php": "^5.3.3", + } +} \ No newline at end of file From 4c0a260c02177bdb928823be99519218794d33a1 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 5 Jul 2017 13:56:38 -0300 Subject: [PATCH 21/44] Fixing composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 021f296..c2da114 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,6 @@ "license": "MIT", "minimum-stability": "dev", "require": { - "php": "^5.3.3", + "php": "^5.3.3" } } \ No newline at end of file From 7851e14b5bcce6ac087a1c942d2af2e379473073 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 5 Jul 2017 14:14:29 -0300 Subject: [PATCH 22/44] Updating composer.json --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c2da114..10a8298 100644 --- a/composer.json +++ b/composer.json @@ -5,8 +5,7 @@ "homepage": "http://github.com/wnasich/php_zklib", "type": "library", "license": "MIT", - "minimum-stability": "dev", "require": { - "php": "^5.3.3" + "php": ">=5.3.3" } } \ No newline at end of file From 345188af44086e921079a62a5aed14a952a9aae5 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 5 Jul 2017 16:33:17 -0300 Subject: [PATCH 23/44] Adding method for delete a user --- ZKLib.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ZKLib.php b/ZKLib.php index bd4339c..9f38d6d 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -22,7 +22,7 @@ class ZKLib { const CMD_ATTLOG_RRQ = 13; const CMD_CLEAR_DATA = 14; const CMD_CLEAR_ATTLOG = 15; - const CMD_DEL_USER = 18; + const CMD_DELETE_USER = 18; const CMD_CLEAR_ADMIN = 20; const CMD_GET_TIME = 201; const CMD_SET_TIME = 202; @@ -370,6 +370,12 @@ protected function func_removeAccents($s){ return strtr($s, "\x01\x02\x03\x04\x05", '`\'"^~'); } + /** + * @param integer $userRecordId + */ + public function deleteUser($userRecordId){ + return $this->execute(self::CMD_DELETE_USER, pack('v', $userRecordId)); + } /** * @param \ZKLib\User $user From 9724754e44cc296981e89f457d20ff6fa808b6e3 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 5 Jul 2017 20:02:05 -0300 Subject: [PATCH 24/44] Adding methods for show message on display and clear display. --- ZKLib.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ZKLib.php b/ZKLib.php index 9f38d6d..10bbd3d 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -287,7 +287,7 @@ public function getTime() public function setTime(\DateTime $dateTime) { - return $this->execute(self::CMD_SET_TIME, pack('V', $this->encodeTime($dateTime))); + return $this->execute(self::CMD_SET_TIME, pack('V', $this->encodeTime($dateTime))); } public function clearAttendance(){ @@ -302,6 +302,20 @@ public function clearAdmins(){ return $this->execute(self::CMD_CLEAR_ADMIN); } + /** + * writeLcd + * @param integer $line Display line to write 0-3 + * @param string $message Message to display. Max len 16 + */ + public function writeLcd($line, $message){ + $message = utf8_decode(substr($message, 0, 16)); + return $this->execute(self::CMD_WRITE_LCD, pack('vCa' . strlen($message), $line, 0x0, $message)); + } + + public function clearLcd(){ + return $this->execute(self::CMD_CLEAR_LCD); + } + /** * * @return \ZKLib\Attendance[] From 7cd2be335247a2de01780cbf8d1142cf88ea79e5 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 5 Jul 2017 20:08:45 -0300 Subject: [PATCH 25/44] Adding method to enable clock on LCD --- ZKLib.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ZKLib.php b/ZKLib.php index 10bbd3d..dd0ad08 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -28,6 +28,7 @@ class ZKLib { const CMD_SET_TIME = 202; const CMD_VERSION = 1100; const CMD_GET_FREE_SIZES = 50; + const CMD_ENABLE_CLOCK = 57; const CMD_WRITE_LCD = 66; const CMD_CLEAR_LCD = 67; const LEVEL_USER = 0; @@ -316,6 +317,10 @@ public function clearLcd(){ return $this->execute(self::CMD_CLEAR_LCD); } + public function enableClock($flashSeconds){ + return $this->execute(self::CMD_ENABLE_CLOCK, pack('C', $flashSeconds ? 0x01 : 0x00)); + } + /** * * @return \ZKLib\Attendance[] From 0098cc24789dd3ba5c8bb3e05d274acd14cd9c75 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 5 Jul 2017 20:49:16 -0300 Subject: [PATCH 26/44] Renaming constant to honor documentation --- ZKLib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ZKLib.php b/ZKLib.php index dd0ad08..05d9370 100644 --- a/ZKLib.php +++ b/ZKLib.php @@ -16,7 +16,7 @@ class ZKLib { const CMD_ACK_UNAUTH = 2005; const CMD_PREPARE_DATA = 1500; const CMD_DATA = 1501; - const CMD_SET_USER = 8; + const CMD_USER_WRQ = 8; const CMD_USERTEMP_RRQ = 9; const CMD_DEVICE = 11; const CMD_ATTLOG_RRQ = 13; @@ -400,7 +400,7 @@ public function deleteUser($userRecordId){ * @param \ZKLib\User $user */ public function setUser($user){ - return $this->execute(self::CMD_SET_USER, pack('vCa5a8a5CsV', + return $this->execute(self::CMD_USER_WRQ, pack('vCa5a8a5CsV', $user->getRecordId(), $user->getRole(), $user->getPassword(), From c861abc087dddaa5053eb7655cead8c0fd1b661e Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 6 Jul 2017 22:28:33 -0300 Subject: [PATCH 27/44] Adding .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..88e99d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +composer.lock \ No newline at end of file From 3a7bddc50d65620881d04817f54b991f1740d9d9 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 6 Jul 2017 22:31:35 -0300 Subject: [PATCH 28/44] Spec autoload entry on composer.json --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 10a8298..becea3e 100644 --- a/composer.json +++ b/composer.json @@ -7,5 +7,8 @@ "license": "MIT", "require": { "php": ">=5.3.3" + }, + "autoload": { + "psr-4": {"ZKLib\\": "src/"} } } \ No newline at end of file From 0482faafc30986a1fb9ef413466f1dd2b80bfdcc Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 6 Jul 2017 22:34:05 -0300 Subject: [PATCH 29/44] Moving files to new folder src/ --- {ZKLib => src}/Attendance.php | 0 {ZKLib => src}/Capacity.php | 0 {ZKLib => src}/User.php | 0 ZKLib.php => src/ZKLib.php | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename {ZKLib => src}/Attendance.php (100%) rename {ZKLib => src}/Capacity.php (100%) rename {ZKLib => src}/User.php (100%) rename ZKLib.php => src/ZKLib.php (100%) diff --git a/ZKLib/Attendance.php b/src/Attendance.php similarity index 100% rename from ZKLib/Attendance.php rename to src/Attendance.php diff --git a/ZKLib/Capacity.php b/src/Capacity.php similarity index 100% rename from ZKLib/Capacity.php rename to src/Capacity.php diff --git a/ZKLib/User.php b/src/User.php similarity index 100% rename from ZKLib/User.php rename to src/User.php diff --git a/ZKLib.php b/src/ZKLib.php similarity index 100% rename from ZKLib.php rename to src/ZKLib.php From 271df2162b11943c81ce8da7b2713d6b0d409f07 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 6 Jul 2017 22:36:26 -0300 Subject: [PATCH 30/44] Moving under namespace ZKLib. Tweaking code. --- src/Attendance.php | 91 ++++++++++++++++---------------- src/Capacity.php | 126 +++++++++++++++++++++------------------------ src/User.php | 107 ++++++++++++++++++-------------------- 3 files changed, 153 insertions(+), 171 deletions(-) diff --git a/src/Attendance.php b/src/Attendance.php index 709b8e5..bbf3b27 100644 --- a/src/Attendance.php +++ b/src/Attendance.php @@ -1,53 +1,48 @@ type; - } - - public function getUserId(){ - return $this->userId; - } - - public function getStatus(){ - return $this->status; - } - - public function isOut(){ - return ($this->type & 0x20) > 0; - } - - public function validatedBy(){ - return ($this->type & 0x08) ? static::ATTENDANCE_BY_FINGERPRINT : static::ATTENDANCE_BY_PASSWORD; - } - - /** - * @return \DateTime - */ - public function getDateTime(){ - return $this->time; - } - - public static function construct($userId, \DateTime $dateTime, $type, $status){ - $instance = new self(); - $instance->userId = $userId; - $instance->type = $type; - $instance->time = $dateTime; - $instance->status = $status; - return $instance; - } +use \DateTime; + +class Attendance { + const ATTENDANCE_BY_PASSWORD = 'by_password'; + const ATTENDANCE_BY_FINGERPRINT = 'by_fingerprint'; + + private $userId; + private $type; + private $status; + private $time; + + public function getType(){ + return $this->type; + } + + public function getUserId(){ + return $this->userId; + } + + public function getStatus(){ + return $this->status; + } + + public function isOut(){ + return ($this->type & 0x20) > 0; + } + + public function validatedBy(){ + return ($this->type & 0x08) ? static::ATTENDANCE_BY_FINGERPRINT : static::ATTENDANCE_BY_PASSWORD; + } + + /** + * @return \DateTime + */ + public function getDateTime(){ + return $this->time; + } + + public function __construct($userId, DateTime $dateTime, $type, $status){ + $this->userId = $userId; + $this->time = $dateTime; + $this->type = $type; + $this->status = $status; } } diff --git a/src/Capacity.php b/src/Capacity.php index 63492c6..363281f 100644 --- a/src/Capacity.php +++ b/src/Capacity.php @@ -1,78 +1,70 @@ att_logs_available; + } - class Capacity { - private $att_logs_available; - private $att_logs_capacity; - private $att_logs_stored; - private $templates_available; - private $templates_capacity; - private $templates_stored; - private $users_stored; - private $users_available; - private $users_capacity; - private $admins_stored; - private $passwords_stored; - - public function getAttLogsAvailable(){ - return $this->att_logs_available; - } - - public function getAttLogsCapacity(){ - return $this->att_logs_capacity; - } + public function getAttLogsCapacity(){ + return $this->att_logs_capacity; + } - public function getAttLogsStored(){ - return $this->att_logs_stored; - } + public function getAttLogsStored(){ + return $this->att_logs_stored; + } - public function getTemplatesAvailable(){ - return $this->templates_available; - } - - public function getTemplatesCapacity(){ - return $this->templates_capacity; - } - - public function getTemplatesStored(){ - return $this->templates_stored; - } + public function getTemplatesAvailable(){ + return $this->templates_available; + } - public function getUsersAvailable(){ - return $this->users_available; - } - - public function getUsersCapacity(){ - return $this->users_capacity; - } - - public function getUsersStored(){ - return $this->users_stored; - } - - public function getAdminsStoredy(){ - return $this->admins_stored; - } - - public function getPasswordsStored(){ - return $this->passwords_stored; - } - - public static function construct($data){ - $instance = new self(); - - foreach ($data as $key => $value){ - if (property_exists($instance, $key)){ - $instance->{$key} = $value; - } else { - var_dump($key); - } + public function getTemplatesCapacity(){ + return $this->templates_capacity; + } + + public function getTemplatesStored(){ + return $this->templates_stored; + } + + public function getUsersAvailable(){ + return $this->users_available; + } + + public function getUsersCapacity(){ + return $this->users_capacity; + } + + public function getUsersStored(){ + return $this->users_stored; + } + + public function getAdminsStoredy(){ + return $this->admins_stored; + } + + public function getPasswordsStored(){ + return $this->passwords_stored; + } + + public function __construct($data){ + foreach ($data as $key => $value){ + if (property_exists($this, $key)){ + $this->{$key} = $value; + } else { + var_dump($key); } - return $instance; } } -} \ No newline at end of file +} diff --git a/src/User.php b/src/User.php index 93db04e..3ef87b0 100644 --- a/src/User.php +++ b/src/User.php @@ -1,66 +1,61 @@ recordId; - } - - public function getRole(){ - return $this->role; - } - - public function getPassword(){ - return $this->password; - } - - public function getName(){ - return $this->name; - } +namespace ZKLib; + +class User { + const PRIVILEGE_COMMON_USER = 0b0000; + const PRIVILEGE_ENROLLER = 0b0010; + const PRIVILEGE_MANAGER = 0b1100; + const PRIVILEGE_SUPERADMIN = 0b1110; + + private $recordId; + private $userId; + private $groupId; + private $name; + private $password; + private $role; + private $cardNo; + private $timeZone; + + public function getRecordId(){ + return $this->recordId; + } - public function getCardNo(){ - return $this->cardNo; - } + public function getRole(){ + return $this->role; + } - public function getUserId(){ - return $this->userId; - } + public function getPassword(){ + return $this->password; + } - public function getGroupId(){ - return $this->groupId; - } + public function getName(){ + return $this->name; + } - public function getTimeZone(){ - return $this->timeZone; - } + public function getCardNo(){ + return $this->cardNo; + } - public static function construct($recordId, $role, $password, $name, $cardNo, $groupId, $timeZone, $userId) { + public function getUserId(){ + return $this->userId; + } - $instance = new self(); - $instance->recordId = $recordId; - $instance->role = $role; - $instance->password = $password; - $instance->name = $name; - $instance->cardNo = $cardNo; - $instance->groupId = $groupId; - $instance->timeZone = $timeZone; - $instance->userId = $userId; + public function getGroupId(){ + return $this->groupId; + } - return $instance; - } + public function getTimeZone(){ + return $this->timeZone; + } + public function __construct($recordId, $role, $password, $name, $cardNo, $groupId, $timeZone, $userId) { + $this->recordId = $recordId; + $this->role = $role; + $this->password = $password; + $this->name = $name; + $this->cardNo = $cardNo; + $this->groupId = $groupId; + $this->timeZone = $timeZone; + $this->userId = $userId; } +} From f2fc45eba79a7f6666b27de7cc56b895de2060c3 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 6 Jul 2017 22:36:38 -0300 Subject: [PATCH 31/44] Updating zktest.php --- zktest.php | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/zktest.php b/zktest.php index a5ea95d..26ae0ea 100644 --- a/zktest.php +++ b/zktest.php @@ -1,3 +1,9 @@ + ZK Test @@ -7,16 +13,25 @@
connect(); if ( $ret ): $zk->disable(); + + /* + $zk->testVoice(); + $zk->setUser(new User( + 99, + User::PRIVILEGE_COMMON_USER, + '1234', + 'User99', + 'Card99', + '1', + -3, + 99 + )); + */ ?>

PHP ZK Library

@@ -72,7 +87,7 @@ clearUsers(); - // $zk->setUser(User::construct(1, User::PRIVILEGE_SUPERADMIN, '1', 'Admin', '', '', -3, 1)); + // $zk->setUser(new User(1, User::PRIVILEGE_SUPERADMIN, '1', 'Admin', '', '', -3, 1)); foreach($zk->getUser() as $user): $role = 'Unknown'; switch ($user->getRole()){ From 2cadb4467d71777f273f3485adc00cd251b410b7 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 6 Jul 2017 22:37:03 -0300 Subject: [PATCH 32/44] Adding Install and Test section to README --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 05ca3b0..40bedbe 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,16 @@ PHP library for communication with ZKTeco Attendance Machine Adjustemnts for model ZEM500 (SBTS H0201) +## Install + +``` +composer require wnasich/php_zklib +``` + +## Test connection to your device +Edit file zktest.php and update the line `$zk = new ZKLib('192.168.1.201');` with your device ip address. + + ### Donations ``` Bitcoin : 187w4iNVHX44y2PC96AuhP286aUKNjcrXV From 91151a43f7f74b5a3de66a0e60adf1809565d6dd Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 6 Jul 2017 22:39:51 -0300 Subject: [PATCH 33/44] Moving under namespace ZKLib. Tweaking code. --- src/ZKLib.php | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/ZKLib.php b/src/ZKLib.php index 05d9370..d71bf2d 100644 --- a/src/ZKLib.php +++ b/src/ZKLib.php @@ -1,7 +1,7 @@ port = $port; $this->ip = $ip; + $this->port = $port; } /** @@ -286,7 +286,7 @@ public function getTime() return $this->decodeTime($encodedTime); } - public function setTime(\DateTime $dateTime) + public function setTime(DateTime $dateTime) { return $this->execute(self::CMD_SET_TIME, pack('V', $this->encodeTime($dateTime))); } @@ -309,6 +309,7 @@ public function clearAdmins(){ * @param string $message Message to display. Max len 16 */ public function writeLcd($line, $message){ + $message = str_pad($message, 16); $message = utf8_decode(substr($message, 0, 16)); return $this->execute(self::CMD_WRITE_LCD, pack('vCa' . strlen($message), $line, 0x0, $message)); } @@ -330,9 +331,6 @@ public function getAttendance() if (($free = $this->getFreeSize()) && !$free->getAttLogsStored()){ return array(); } - if (!defined('__ZKLib_Attendance')){ - require_once __DIR__.'/ZKLib/Attendance.php'; - } $this->execute(self::CMD_ATTLOG_RRQ); @@ -355,7 +353,7 @@ public function getAttendance() } $data = unpack('vuserId/Ctype/Cstatus/Vtime', $attInfo); $dateTime = $this->decodeTime($data['time']); - $result[] = Attendance::construct( + $result[] = new Attendance( $data['userId'], $dateTime, $data['type'], @@ -399,7 +397,7 @@ public function deleteUser($userRecordId){ /** * @param \ZKLib\User $user */ - public function setUser($user){ + public function setUser(User $user){ return $this->execute(self::CMD_USER_WRQ, pack('vCa5a8a5CsV', $user->getRecordId(), $user->getRole(), @@ -420,10 +418,6 @@ public function getUser(){ return array(); } - if (!defined('__ZKLib_User')){ - require_once __DIR__.'/ZKLib/User.php'; - } - $this->execute(self::CMD_USERTEMP_RRQ); $usersData = ''; @@ -444,7 +438,7 @@ public function getUser(){ continue; } $user = unpack('vrecordId/Crole/a5password/a8name/a5cardNo/CgroupId/stimeZone/VuserId', $userInfo); - $result[$user['recordId']] = User::construct( + $result[$user['recordId']] = new User( $user['recordId'], $user['role'], $user['password'], @@ -465,16 +459,13 @@ public function getUser(){ public function getFreeSize() { if (($free_sizes_info = $this->execute(self::CMD_GET_FREE_SIZES)) && is_string($free_sizes_info)) { - if (!defined('__ZKLib_Capacity')){ - require_once __DIR__.'/ZKLib/Capacity.php'; - } - return Capacity::construct(unpack('x16/Vusers_stored/x4/Vtemplates_stored/x4/Vatt_logs_stored/x12/Vadmins_stored/Vpasswords_stored/Vtemplates_capacity/Vusers_capacity/Vatt_logs_capacity/Vtemplates_available/Vusers_available/Vatt_logs_available', $free_sizes_info)); + return new Capacity(unpack('x16/Vusers_stored/x4/Vtemplates_stored/x4/Vatt_logs_stored/x12/Vadmins_stored/Vpasswords_stored/Vtemplates_capacity/Vusers_capacity/Vatt_logs_capacity/Vtemplates_available/Vusers_available/Vatt_logs_available', $free_sizes_info)); } return false; } /** - * @return DateTime + * @return \DateTime */ public function decodeTime($encodedTime) { @@ -505,7 +496,7 @@ public function decodeTime($encodedTime) /** * @return integer */ - public function encodeTime(\DateTime $t) + public function encodeTime(DateTime $t) { return (($t->format('Y') % 100) * 12 * 31 + (($t->format('n') - 1) * 31) + $t->format('j') - 1) * (24 * 60 * 60) + From 1fb60151903699fccca88f39c2ec19ae9b166efe Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 6 Jul 2017 22:56:32 -0300 Subject: [PATCH 34/44] Fix proper access of RuntimeException class --- src/ZKLib.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ZKLib.php b/src/ZKLib.php index d71bf2d..4958cbe 100644 --- a/src/ZKLib.php +++ b/src/ZKLib.php @@ -2,6 +2,7 @@ namespace ZKLib; use \DateTime; +use \RuntimeException; class ZKLib { const USHRT_MAX = 65535; From ca00155eb76e8e161ccf48ebeed380901cf11317 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Fri, 7 Jul 2017 14:44:25 -0300 Subject: [PATCH 35/44] Renaming method to getUsers() --- src/ZKLib.php | 2 +- zktest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ZKLib.php b/src/ZKLib.php index 4958cbe..bcda440 100644 --- a/src/ZKLib.php +++ b/src/ZKLib.php @@ -414,7 +414,7 @@ public function setUser(User $user){ /** * @return \ZKLib\User[] */ - public function getUser(){ + public function getUsers(){ if (($free = $this->getFreeSize()) && !$free->getUsersStored()){ return array(); } diff --git a/zktest.php b/zktest.php index 26ae0ea..d93044f 100644 --- a/zktest.php +++ b/zktest.php @@ -88,7 +88,7 @@ try { // $zk->clearUsers(); // $zk->setUser(new User(1, User::PRIVILEGE_SUPERADMIN, '1', 'Admin', '', '', -3, 1)); - foreach($zk->getUser() as $user): + foreach($zk->getUsers() as $user): $role = 'Unknown'; switch ($user->getRole()){ case User::PRIVILEGE_COMMON_USER : $role = 'USER'; break; From 1f89591d9558eb5dcdf8044444d2f5443c71fdbd Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Fri, 7 Jul 2017 15:17:23 -0300 Subject: [PATCH 36/44] Decoding role on ZKLib class --- src/User.php | 21 +++++++++++++++++++++ zktest.php | 12 ++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/User.php b/src/User.php index 3ef87b0..654ccf3 100644 --- a/src/User.php +++ b/src/User.php @@ -24,6 +24,27 @@ public function getRole(){ return $this->role; } + public function decodeRole(){ + switch ($this->role) { + case PRIVILEGE_COMMON_USER: + $decoded = 'common_user'; + break; + case PRIVILEGE_ENROLLER: + $decoded = 'enroller'; + break; + case PRIVILEGE_MANAGER: + $decoded = 'manager'; + break; + case PRIVILEGE_SUPERADMIN: + $decoded = 'superadmin'; + break; + default: + $decoded = 'Unknown'; + break; + } + return $decoded; + } + public function getPassword(){ return $this->password; } diff --git a/zktest.php b/zktest.php index d93044f..35f75b5 100644 --- a/zktest.php +++ b/zktest.php @@ -88,20 +88,12 @@ try { // $zk->clearUsers(); // $zk->setUser(new User(1, User::PRIVILEGE_SUPERADMIN, '1', 'Admin', '', '', -3, 1)); - foreach($zk->getUsers() as $user): - $role = 'Unknown'; - switch ($user->getRole()){ - case User::PRIVILEGE_COMMON_USER : $role = 'USER'; break; - case User::PRIVILEGE_ENROLLER : $role = 'ENROLLER'; break; - case User::PRIVILEGE_MANAGER : $role = 'MANAGER'; break; - case User::PRIVILEGE_SUPERADMIN : $role = 'ADMIN'; break; - } - ?> + foreach($zk->getUsers() as $user): ?>
- + From fd1caa1da3c4239315e8c8722ffd957521634eb7 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Fri, 7 Jul 2017 15:42:42 -0300 Subject: [PATCH 37/44] Fixing access to class constants --- src/User.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/User.php b/src/User.php index 654ccf3..a1c5aa4 100644 --- a/src/User.php +++ b/src/User.php @@ -26,16 +26,16 @@ public function getRole(){ public function decodeRole(){ switch ($this->role) { - case PRIVILEGE_COMMON_USER: + case static::PRIVILEGE_COMMON_USER: $decoded = 'common_user'; break; - case PRIVILEGE_ENROLLER: + case static::PRIVILEGE_ENROLLER: $decoded = 'enroller'; break; - case PRIVILEGE_MANAGER: + case static::PRIVILEGE_MANAGER: $decoded = 'manager'; break; - case PRIVILEGE_SUPERADMIN: + case static::PRIVILEGE_SUPERADMIN: $decoded = 'superadmin'; break; default: From 75a073abb59e4623afcc9b5aaf0b8340d78b829e Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Tue, 11 Jul 2017 12:34:15 -0300 Subject: [PATCH 38/44] Renaming methods --- src/ZKLib.php | 4 ++-- zktest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ZKLib.php b/src/ZKLib.php index bcda440..8854272 100644 --- a/src/ZKLib.php +++ b/src/ZKLib.php @@ -292,7 +292,7 @@ public function setTime(DateTime $dateTime) return $this->execute(self::CMD_SET_TIME, pack('V', $this->encodeTime($dateTime))); } - public function clearAttendance(){ + public function clearAttendances(){ return $this->execute(self::CMD_CLEAR_ATTLOG); } @@ -327,7 +327,7 @@ public function enableClock($flashSeconds){ * * @return \ZKLib\Attendance[] */ - public function getAttendance() + public function getAttendances() { if (($free = $this->getFreeSize()) && !$free->getAttLogsStored()){ return array(); diff --git a/zktest.php b/zktest.php index 35f75b5..90e6619 100644 --- a/zktest.php +++ b/zktest.php @@ -123,7 +123,7 @@ getAttendance() as $attendance): + foreach($zk->getAttendances() as $attendance): ?> From 4564252afefbb88e0cf13ae0adf579470e85ab2a Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Tue, 11 Jul 2017 13:09:59 -0300 Subject: [PATCH 39/44] Renaming constants of Attendance --- src/Attendance.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Attendance.php b/src/Attendance.php index bbf3b27..dfe9466 100644 --- a/src/Attendance.php +++ b/src/Attendance.php @@ -4,8 +4,8 @@ use \DateTime; class Attendance { - const ATTENDANCE_BY_PASSWORD = 'by_password'; - const ATTENDANCE_BY_FINGERPRINT = 'by_fingerprint'; + const VALIDATED_BY_PASSWORD = 'password'; + const VALIDATED_BY_FINGERPRINT = 'fingerprint'; private $userId; private $type; @@ -29,7 +29,7 @@ public function isOut(){ } public function validatedBy(){ - return ($this->type & 0x08) ? static::ATTENDANCE_BY_FINGERPRINT : static::ATTENDANCE_BY_PASSWORD; + return ($this->type & 0x08) ? static::VALIDATED_BY_FINGERPRINT : static::VALIDATED_BY_PASSWORD; } /** From 4ac9841ac32aea695b24d5d0aa66fb65b1b057d0 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Wed, 12 Jul 2017 15:53:30 -0300 Subject: [PATCH 40/44] Tweaking layout of test page --- zktest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/zktest.php b/zktest.php index 90e6619..a45f43a 100644 --- a/zktest.php +++ b/zktest.php @@ -35,6 +35,7 @@ ?>

PHP ZK Library

+
Data Attendance
Index UIDTypeIn / OutValidated By Status DateTime
getRecordId(); ?> getUserId(); ?>getType(); ?>isOut() ? 'Out' : 'In'; ?>validatedBy(); ?> getStatus(); ?> getDateTime()->format('r'); ?>
getRecordId(); ?> getUserId(); ?> getName(); ?>decodeRole(); ?> getPassword(); ?> getGroupId(); ?> getTimeZone(); ?>DateTime
getUserId(); ?>
@@ -68,8 +69,9 @@
Status
+
-
+
@@ -109,8 +111,10 @@ ?>
Data User
+
-
+
+
From 607a3dd90dc25a9d1552094d71eff88a7c880017 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 13 Jul 2017 17:53:00 -0300 Subject: [PATCH 41/44] Fixing extract of data from udp stream --- src/ZKLib.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/ZKLib.php b/src/ZKLib.php index 8854272..62c68c2 100644 --- a/src/ZKLib.php +++ b/src/ZKLib.php @@ -341,10 +341,9 @@ public function getAttendances() if ($size === false) { throw new RuntimeException(socket_strerror(socket_last_error())); } - $attData .= $data; + $attData .= substr($data, 8); } while ($size > 0 && $size != 8); - $attData = substr($attData, 12); - $attData = substr($attData, 0, -8); + $attData = substr($attData, 4); $result = array(); if ($attData){ @@ -427,10 +426,9 @@ public function getUsers(){ if ($size === false) { throw new RuntimeException(socket_strerror(socket_last_error())); } - $usersData .= $data; + $usersData .= substr($data, 8); } while($size > 0 && $size != 8); - $usersData = substr($usersData, 12); - $usersData = substr($usersData, 0, -8); + $usersData = substr($usersData, 4); $result = array(); if ($usersData){ From 8872391b12ea31b06514816a9915b32705c0650c Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 13 Jul 2017 18:03:50 -0300 Subject: [PATCH 42/44] Renaming method to getAdminsStored() --- src/Capacity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Capacity.php b/src/Capacity.php index 363281f..49e1623 100644 --- a/src/Capacity.php +++ b/src/Capacity.php @@ -50,7 +50,7 @@ public function getUsersStored(){ return $this->users_stored; } - public function getAdminsStoredy(){ + public function getAdminsStored(){ return $this->admins_stored; } From 573cc2970e90c0e4ec23c51dcfabe0d3e1303e06 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Thu, 13 Jul 2017 18:04:09 -0300 Subject: [PATCH 43/44] Displaying clock capacities on test page --- zktest.php | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/zktest.php b/zktest.php index a45f43a..92a9d04 100644 --- a/zktest.php +++ b/zktest.php @@ -19,6 +19,8 @@ if ( $ret ): $zk->disable(); + $capacity = $zk->getFreeSize(); + /* $zk->testVoice(); $zk->setUser(new User( @@ -34,6 +36,7 @@ */ ?>

PHP ZK Library

+

Clock info

Data Attendance
@@ -70,12 +73,49 @@
+ +

Capacity

- + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Data UserAttendance logs availablegetAttLogsAvailable(); ?>Attendance log capacitygetAttLogsCapacity(); ?>Attendance logs storedgetAttLogsStored(); ?>
Templates availablegetTemplatesAvailable(); ?>Templates capacitygetTemplatesCapacity(); ?>Templates storedgetTemplatesStored(); ?>
Users availablegetUsersAvailable(); ?>Users capacitygetUsersCapacity(); ?>Users storedgetUsersStored(); ?>
Admins storedgetAdminsStored(); ?>Passwords storedgetPasswordsStored(); ?>
+
+
+ +

Users

+
+
+ @@ -113,12 +153,10 @@ +

Attendance data

UID ID
- - - From f53b25d809d2f64d5c6ab7d1d0f2059c0b0ce8b9 Mon Sep 17 00:00:00 2001 From: Walter Nasich Date: Tue, 29 Aug 2017 23:27:05 -0300 Subject: [PATCH 44/44] Updating README with tested models --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 40bedbe..eb57c99 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ PHP library for communication with ZKTeco Attendance Machine -Adjustemnts for model ZEM500 (SBTS H0201) +Adjustemnts for platform ZEM500. + +Tested on models SBTS H0201, ZK F707. ## Install
Data Attendance
UID In / Out