123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?php
- namespace getusersig;
- if ( version_compare( PHP_VERSION, '5.1.2' ) < 0 ) {
- trigger_error( 'need php 5.1.2 or newer', E_USER_ERROR );
- }
- class Getusersig {
- private $key = false;
- private $sdkappid = 0;
-
- public function genUserSig( $userid, $expire = 15552000 ) {
- return $this->__genSig( $userid, $expire, '', false );
- }
-
- public function genPrivateMapKey( $userid, $expire, $roomid, $privilegeMap ) {
- $userbuf = $this->__genUserBuf( $userid, $roomid, $expire, $privilegeMap, 0, '' );
- return $this->__genSig( $userid, $expire, $userbuf, true );
- }
-
- public function genPrivateMapKeyWithStringRoomID( $userid, $expire, $roomstr, $privilegeMap ) {
- $userbuf = $this->__genUserBuf( $userid, 0, $expire, $privilegeMap, 0, $roomstr );
- return $this->__genSig( $userid, $expire, $userbuf, true );
- }
- public function __construct( $sdkappid, $key ) {
- $this->sdkappid = $sdkappid;
- $this->key = $key;
- }
-
- private function base64_url_encode( $string ) {
- static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
- $base64 = base64_encode( $string );
- if ( $base64 === false ) {
- throw new \Exception( 'base64_encode error' );
- }
- return str_replace( array_keys( $replace ), array_values( $replace ), $base64 );
- }
-
- private function base64_url_decode( $base64 ) {
- static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
- $string = str_replace( array_values( $replace ), array_keys( $replace ), $base64 );
- $result = base64_decode( $string );
- if ( $result == false ) {
- throw new \Exception( 'base64_url_decode error' );
- }
- return $result;
- }
-
- private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap, $dwAccountType,$roomStr ) {
-
- if($roomStr == '')
- $userbuf = pack( 'C1', '0' );
- else
- $userbuf = pack( 'C1', '1' );
- $userbuf .= pack( 'n', strlen( $account ) );
-
- $userbuf .= pack( 'a'.strlen( $account ), $account );
-
- $userbuf .= pack( 'N', $this->sdkappid );
-
- $userbuf .= pack( 'N', $dwAuthID );
-
- $expire = $dwExpTime + time();
- $userbuf .= pack( 'N', $expire );
-
- $userbuf .= pack( 'N', $dwPrivilegeMap );
-
- $userbuf .= pack( 'N', $dwAccountType );
-
- if($roomStr != '')
- {
- $userbuf .= pack( 'n', strlen( $roomStr ) );
-
- $userbuf .= pack( 'a'.strlen( $roomStr ), $roomStr );
-
- }
- return $userbuf;
- }
-
- private function hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled ) {
- $content_to_be_signed = 'TLS.identifier:' . $identifier . "\n"
- . 'TLS.sdkappid:' . $this->sdkappid . "\n"
- . 'TLS.time:' . $curr_time . "\n"
- . 'TLS.expire:' . $expire . "\n";
- if ( true == $userbuf_enabled ) {
- $content_to_be_signed .= 'TLS.userbuf:' . $base64_userbuf . "\n";
- }
- return base64_encode( hash_hmac( 'sha256', $content_to_be_signed, $this->key, true ) );
- }
-
- private function __genSig( $identifier, $expire, $userbuf, $userbuf_enabled ) {
- $curr_time = time();
- $sig_array = Array(
- 'TLS.ver' => '2.0',
- 'TLS.identifier' => strval( $identifier ),
- 'TLS.sdkappid' => intval( $this->sdkappid ),
- 'TLS.expire' => intval( $expire ),
- 'TLS.time' => intval( $curr_time )
- );
- $base64_userbuf = '';
- if ( true == $userbuf_enabled ) {
- $base64_userbuf = base64_encode( $userbuf );
- $sig_array['TLS.userbuf'] = strval( $base64_userbuf );
- }
- $sig_array['TLS.sig'] = $this->hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled );
- if ( $sig_array['TLS.sig'] === false ) {
- throw new \Exception( 'base64_encode error' );
- }
- $json_str_sig = json_encode( $sig_array );
- if ( $json_str_sig === false ) {
- throw new \Exception( 'json_encode error' );
- }
- $compressed = gzcompress( $json_str_sig );
- if ( $compressed === false ) {
- throw new \Exception( 'gzcompress error' );
- }
- return $this->base64_url_encode( $compressed );
- }
-
- private function __verifySig( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
- try {
- $error_msg = '';
- $compressed_sig = $this->base64_url_decode( $sig );
- $pre_level = error_reporting( E_ERROR );
- $uncompressed_sig = gzuncompress( $compressed_sig );
- error_reporting( $pre_level );
- if ( $uncompressed_sig === false ) {
- throw new \Exception( 'gzuncompress error' );
- }
- $sig_doc = json_decode( $uncompressed_sig );
- if ( $sig_doc == false ) {
- throw new \Exception( 'json_decode error' );
- }
- $sig_doc = ( array )$sig_doc;
- if ( $sig_doc['TLS.identifier'] !== $identifier ) {
- throw new \Exception( "identifier dosen't match" );
- }
- if ( $sig_doc['TLS.sdkappid'] != $this->sdkappid ) {
- throw new \Exception( "sdkappid dosen't match" );
- }
- $sig = $sig_doc['TLS.sig'];
- if ( $sig == false ) {
- throw new \Exception( 'sig field is missing' );
- }
- $init_time = $sig_doc['TLS.time'];
- $expire_time = $sig_doc['TLS.expire'];
- $curr_time = time();
- if ( $curr_time > $init_time+$expire_time ) {
- throw new \Exception( 'sig expired' );
- }
- $userbuf_enabled = false;
- $base64_userbuf = '';
- if ( isset( $sig_doc['TLS.userbuf'] ) ) {
- $base64_userbuf = $sig_doc['TLS.userbuf'];
- $userbuf = base64_decode( $base64_userbuf );
- $userbuf_enabled = true;
- }
- $sigCalculated = $this->hmacsha256( $identifier, $init_time, $expire_time, $base64_userbuf, $userbuf_enabled );
- if ( $sig != $sigCalculated ) {
- throw new \Exception( 'verify failed' );
- }
- return true;
- } catch ( \Exception $ex ) {
- $error_msg = $ex->getMessage();
- return false;
- }
- }
-
- public function verifySig( $sig, $identifier, &$init_time, &$expire_time, &$error_msg ) {
- $userbuf = '';
- return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
- }
-
- public function verifySigWithUserBuf( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
- return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
- }
- }
|