Skip to content

Commit

Permalink
Merge pull request #1 from Willywes/analysis-ajrd70
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
Willywes authored Jan 13, 2021
2 parents e00d426 + da03882 commit af362f9
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 173 deletions.
135 changes: 73 additions & 62 deletions src/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,40 @@ public function __construct()
{
$this->salt = rand(0, 100000);

$date = new \DateTime("now", new \DateTimeZone('UTC'));
$date = new \DateTime('now', new \DateTimeZone('UTC'));
$this->ts = $date->getTimestamp() + 24 * 3600;

$this->privileges = array();
$this->privileges = [];
}

public function packContent()
{
$buffer = unpack("C*", pack("V", $this->salt));
$buffer = array_merge($buffer, unpack("C*", pack("V", $this->ts)));
$buffer = array_merge($buffer, unpack("C*", pack("v", sizeof($this->privileges))));
$buffer = unpack('C*', pack('V', $this->salt));
$buffer = array_merge($buffer, unpack('C*', pack('V', $this->ts)));
$buffer = array_merge($buffer, unpack('C*', pack('v', sizeof($this->privileges))));
foreach ($this->privileges as $key => $value) {
$buffer = array_merge($buffer, unpack("C*", pack("v", $key)));
$buffer = array_merge($buffer, unpack("C*", pack("V", $value)));
$buffer = array_merge($buffer, unpack('C*', pack('v', $key)));
$buffer = array_merge($buffer, unpack('C*', pack('V', $value)));
}

return $buffer;
}

public function unpackContent($msg)
{
$pos = 0;
$salt = unpack("V", substr($msg, $pos, 4))[1];
$salt = unpack('V', substr($msg, $pos, 4))[1];
$pos += 4;
$ts = unpack("V", substr($msg, $pos, 4))[1];
$ts = unpack('V', substr($msg, $pos, 4))[1];
$pos += 4;
$size = unpack("v", substr($msg, $pos, 2))[1];
$size = unpack('v', substr($msg, $pos, 2))[1];
$pos += 2;

$privileges = array();
$privileges = [];
for ($i = 0; $i < $size; $i++) {
$key = unpack("v", substr($msg, $pos, 2));
$key = unpack('v', substr($msg, $pos, 2));
$pos += 2;
$value = unpack("V", substr($msg, $pos, 4));
$value = unpack('V', substr($msg, $pos, 4));
$pos += 4;
$privileges[$key[1]] = $value[1];
}
Expand All @@ -56,56 +57,60 @@ public function unpackContent($msg)

class AccessToken
{
const Privileges = array(
"kJoinChannel" => 1,
"kPublishAudioStream" => 2,
"kPublishVideoStream" => 3,
"kPublishDataStream" => 4,
"kPublishAudioCdn" => 5,
"kPublishVideoCdn" => 6,
"kRequestPublishAudioStream" => 7,
"kRequestPublishVideoStream" => 8,
"kRequestPublishDataStream" => 9,
"kInvitePublishAudioStream" => 10,
"kInvitePublishVideoStream" => 11,
"kInvitePublishDataStream" => 12,
"kAdministrateChannel" => 101,
"kRtmLogin" => 1000,
);

public $appID, $appCertificate, $channelName, $uid;
const Privileges = [
'kJoinChannel' => 1,
'kPublishAudioStream' => 2,
'kPublishVideoStream' => 3,
'kPublishDataStream' => 4,
'kPublishAudioCdn' => 5,
'kPublishVideoCdn' => 6,
'kRequestPublishAudioStream' => 7,
'kRequestPublishVideoStream' => 8,
'kRequestPublishDataStream' => 9,
'kInvitePublishAudioStream' => 10,
'kInvitePublishVideoStream' => 11,
'kInvitePublishDataStream' => 12,
'kAdministrateChannel' => 101,
'kRtmLogin' => 1000,
];

public $appID;
public $appCertificate;
public $channelName;
public $uid;
public $message;

function __construct()
public function __construct()
{
$this->message = new Message();
}

function setUid($uid)
public function setUid($uid)
{
if ($uid === 0) {
$this->uid = "";
$this->uid = '';
} else {
$this->uid = $uid . '';
$this->uid = $uid.'';
}
}

function is_nonempty_string($name, $str)
public function is_nonempty_string($name, $str)
{
if (is_string($str) && $str !== "") {
if (is_string($str) && $str !== '') {
return true;
}
echo $name . " check failed, should be a non-empty string";
echo $name.' check failed, should be a non-empty string';

return false;
}

static function init($appID, $appCertificate, $channelName, $uid)
public static function init($appID, $appCertificate, $channelName, $uid)
{
$accessToken = new AccessToken();

if (!$accessToken->is_nonempty_string("appID", $appID) ||
!$accessToken->is_nonempty_string("appCertificate", $appCertificate) ||
!$accessToken->is_nonempty_string("channelName", $channelName)) {
if (! $accessToken->is_nonempty_string('appID', $appID) ||
! $accessToken->is_nonempty_string('appCertificate', $appCertificate) ||
! $accessToken->is_nonempty_string('channelName', $channelName)) {
return null;
}

Expand All @@ -115,53 +120,57 @@ static function init($appID, $appCertificate, $channelName, $uid)

$accessToken->setUid($uid);
$accessToken->message = new Message();

return $accessToken;
}

static function initWithToken($token, $appCertificate, $channel, $uid)
public static function initWithToken($token, $appCertificate, $channel, $uid)
{
$accessToken = new AccessToken();
if (!$accessToken->extract($token, $appCertificate, $channel, $uid)) {
if (! $accessToken->extract($token, $appCertificate, $channel, $uid)) {
return null;
}

return $accessToken;
}

function addPrivilege($key, $expireTimestamp)
public function addPrivilege($key, $expireTimestamp)
{
$this->message->privileges[$key] = $expireTimestamp;

return $this;
}

function extract($token, $appCertificate, $channelName, $uid)
public function extract($token, $appCertificate, $channelName, $uid)
{
$ver_len = 3;
$appid_len = 32;
$version = substr($token, 0, $ver_len);
if ($version !== "006") {
echo 'invalid version ' . $version;
if ($version !== '006') {
echo 'invalid version '.$version;

return false;
}

if (!$this->is_nonempty_string("token", $token) ||
!$this->is_nonempty_string("appCertificate", $appCertificate) ||
!$this->is_nonempty_string("channelName", $channelName)) {
if (! $this->is_nonempty_string('token', $token) ||
! $this->is_nonempty_string('appCertificate', $appCertificate) ||
! $this->is_nonempty_string('channelName', $channelName)) {
return false;
}

$appid = substr($token, $ver_len, $appid_len);
$content = (base64_decode(substr($token, $ver_len + $appid_len, strlen($token) - ($ver_len + $appid_len))));

$pos = 0;
$len = unpack("v", $content . substr($pos, 2))[1];
$len = unpack('v', $content.substr($pos, 2))[1];
$pos += 2;
$sig = substr($content, $pos, $len);
$pos += $len;
$crc_channel = unpack("V", substr($content, $pos, 4))[1];
$crc_channel = unpack('V', substr($content, $pos, 4))[1];
$pos += 4;
$crc_uid = unpack("V", substr($content, $pos, 4))[1];
$crc_uid = unpack('V', substr($content, $pos, 4))[1];
$pos += 4;
$msgLen = unpack("v", substr($content, $pos, 2))[1];
$msgLen = unpack('v', substr($content, $pos, 2))[1];
$pos += 2;
$msg = substr($content, $pos, $msgLen);

Expand All @@ -174,27 +183,29 @@ function extract($token, $appCertificate, $channelName, $uid)
$this->appCertificate = $appCertificate;
$this->channelName = $channelName;
$this->setUid($uid);

return true;
}

function build()
public function build()
{
$msg = $this->message->packContent();
$val = array_merge(unpack("C*", $this->appID), unpack("C*", $this->channelName), unpack("C*", $this->uid), $msg);
$val = array_merge(unpack('C*', $this->appID), unpack('C*', $this->channelName), unpack('C*', $this->uid), $msg);

$sig = hash_hmac('sha256', implode(array_map("chr", $val)), $this->appCertificate, true);
$sig = hash_hmac('sha256', implode(array_map('chr', $val)), $this->appCertificate, true);

$crc_channel_name = crc32($this->channelName) & 0xffffffff;
$crc_uid = crc32($this->uid) & 0xffffffff;

$content = array_merge(unpack("C*", packString($sig)), unpack("C*", pack("V", $crc_channel_name)), unpack("C*", pack("V", $crc_uid)), unpack("C*", pack("v", count($msg))), $msg);
$version = "006";
$ret = $version . $this->appID . base64_encode(implode(array_map("chr", $content)));
$content = array_merge(unpack('C*', packString($sig)), unpack('C*', pack('V', $crc_channel_name)), unpack('C*', pack('V', $crc_uid)), unpack('C*', pack('v', count($msg))), $msg);
$version = '006';
$ret = $version.$this->appID.base64_encode(implode(array_map('chr', $content)));

return $ret;
}
}

function packString($value)
{
return pack("v", strlen($value)) . $value;
return pack('v', strlen($value)).$value;
}
3 changes: 2 additions & 1 deletion src/AgoraSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class AgoraSDK
{
// Build wonderful things

public static function Version(){
public static function Version()
{
return 'test';
}
}
36 changes: 18 additions & 18 deletions src/DynamicKey4.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<?php

function generateRecordingKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType='ARS')
function generateRecordingKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType = 'ARS')
{
return generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType);
return generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType);
}

function generateMediaChannelKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType='ACS')
function generateMediaChannelKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType = 'ACS')
{
return generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType);
return generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType);
}

function generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType)
function generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType)
{
$version = "004";
$version = '004';

$randomStr = "00000000" . dechex($randomInt);
$randomStr = substr($randomStr,-8);
$randomStr = '00000000'.dechex($randomInt);
$randomStr = substr($randomStr, -8);

$uidStr = "0000000000" . $uid;
$uidStr = substr($uidStr,-10);

$expiredStr = "0000000000" . $expiredTs;
$expiredStr = substr($expiredStr,-10);
$uidStr = '0000000000'.$uid;
$uidStr = substr($uidStr, -10);

$signature = generateSignature($appID, $appCertificate, $channelName, $ts, $randomStr, $uidStr, $expiredStr ,$serviceType);
$expiredStr = '0000000000'.$expiredTs;
$expiredStr = substr($expiredStr, -10);

return $version . $signature . $appID . $ts . $randomStr . $expiredStr;
$signature = generateSignature($appID, $appCertificate, $channelName, $ts, $randomStr, $uidStr, $expiredStr, $serviceType);

return $version.$signature.$appID.$ts.$randomStr.$expiredStr;
}

function generateSignature($appID, $appCertificate, $channelName, $ts, $randomStr, $uidStr, $expiredStr ,$serviceType)
function generateSignature($appID, $appCertificate, $channelName, $ts, $randomStr, $uidStr, $expiredStr, $serviceType)
{
$concat = $serviceType . $appID . $ts . $randomStr . $channelName . $uidStr . $expiredStr;
$concat = $serviceType.$appID.$ts.$randomStr.$channelName.$uidStr.$expiredStr;

return hash_hmac('sha1', $concat, $appCertificate);
}
?>
Loading

0 comments on commit af362f9

Please sign in to comment.