Skip to content

Commit af362f9

Browse files
authored
Merge pull request #1 from Willywes/analysis-ajrd70
Apply fixes from StyleCI
2 parents e00d426 + da03882 commit af362f9

7 files changed

+185
-173
lines changed

src/AccessToken.php

+73-62
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,40 @@ public function __construct()
1212
{
1313
$this->salt = rand(0, 100000);
1414

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

18-
$this->privileges = array();
18+
$this->privileges = [];
1919
}
2020

2121
public function packContent()
2222
{
23-
$buffer = unpack("C*", pack("V", $this->salt));
24-
$buffer = array_merge($buffer, unpack("C*", pack("V", $this->ts)));
25-
$buffer = array_merge($buffer, unpack("C*", pack("v", sizeof($this->privileges))));
23+
$buffer = unpack('C*', pack('V', $this->salt));
24+
$buffer = array_merge($buffer, unpack('C*', pack('V', $this->ts)));
25+
$buffer = array_merge($buffer, unpack('C*', pack('v', sizeof($this->privileges))));
2626
foreach ($this->privileges as $key => $value) {
27-
$buffer = array_merge($buffer, unpack("C*", pack("v", $key)));
28-
$buffer = array_merge($buffer, unpack("C*", pack("V", $value)));
27+
$buffer = array_merge($buffer, unpack('C*', pack('v', $key)));
28+
$buffer = array_merge($buffer, unpack('C*', pack('V', $value)));
2929
}
30+
3031
return $buffer;
3132
}
3233

3334
public function unpackContent($msg)
3435
{
3536
$pos = 0;
36-
$salt = unpack("V", substr($msg, $pos, 4))[1];
37+
$salt = unpack('V', substr($msg, $pos, 4))[1];
3738
$pos += 4;
38-
$ts = unpack("V", substr($msg, $pos, 4))[1];
39+
$ts = unpack('V', substr($msg, $pos, 4))[1];
3940
$pos += 4;
40-
$size = unpack("v", substr($msg, $pos, 2))[1];
41+
$size = unpack('v', substr($msg, $pos, 2))[1];
4142
$pos += 2;
4243

43-
$privileges = array();
44+
$privileges = [];
4445
for ($i = 0; $i < $size; $i++) {
45-
$key = unpack("v", substr($msg, $pos, 2));
46+
$key = unpack('v', substr($msg, $pos, 2));
4647
$pos += 2;
47-
$value = unpack("V", substr($msg, $pos, 4));
48+
$value = unpack('V', substr($msg, $pos, 4));
4849
$pos += 4;
4950
$privileges[$key[1]] = $value[1];
5051
}
@@ -56,56 +57,60 @@ public function unpackContent($msg)
5657

5758
class AccessToken
5859
{
59-
const Privileges = array(
60-
"kJoinChannel" => 1,
61-
"kPublishAudioStream" => 2,
62-
"kPublishVideoStream" => 3,
63-
"kPublishDataStream" => 4,
64-
"kPublishAudioCdn" => 5,
65-
"kPublishVideoCdn" => 6,
66-
"kRequestPublishAudioStream" => 7,
67-
"kRequestPublishVideoStream" => 8,
68-
"kRequestPublishDataStream" => 9,
69-
"kInvitePublishAudioStream" => 10,
70-
"kInvitePublishVideoStream" => 11,
71-
"kInvitePublishDataStream" => 12,
72-
"kAdministrateChannel" => 101,
73-
"kRtmLogin" => 1000,
74-
);
75-
76-
public $appID, $appCertificate, $channelName, $uid;
60+
const Privileges = [
61+
'kJoinChannel' => 1,
62+
'kPublishAudioStream' => 2,
63+
'kPublishVideoStream' => 3,
64+
'kPublishDataStream' => 4,
65+
'kPublishAudioCdn' => 5,
66+
'kPublishVideoCdn' => 6,
67+
'kRequestPublishAudioStream' => 7,
68+
'kRequestPublishVideoStream' => 8,
69+
'kRequestPublishDataStream' => 9,
70+
'kInvitePublishAudioStream' => 10,
71+
'kInvitePublishVideoStream' => 11,
72+
'kInvitePublishDataStream' => 12,
73+
'kAdministrateChannel' => 101,
74+
'kRtmLogin' => 1000,
75+
];
76+
77+
public $appID;
78+
public $appCertificate;
79+
public $channelName;
80+
public $uid;
7781
public $message;
7882

79-
function __construct()
83+
public function __construct()
8084
{
8185
$this->message = new Message();
8286
}
8387

84-
function setUid($uid)
88+
public function setUid($uid)
8589
{
8690
if ($uid === 0) {
87-
$this->uid = "";
91+
$this->uid = '';
8892
} else {
89-
$this->uid = $uid . '';
93+
$this->uid = $uid.'';
9094
}
9195
}
9296

93-
function is_nonempty_string($name, $str)
97+
public function is_nonempty_string($name, $str)
9498
{
95-
if (is_string($str) && $str !== "") {
99+
if (is_string($str) && $str !== '') {
96100
return true;
97101
}
98-
echo $name . " check failed, should be a non-empty string";
102+
echo $name.' check failed, should be a non-empty string';
103+
99104
return false;
100105
}
101106

102-
static function init($appID, $appCertificate, $channelName, $uid)
107+
public static function init($appID, $appCertificate, $channelName, $uid)
103108
{
104109
$accessToken = new AccessToken();
105110

106-
if (!$accessToken->is_nonempty_string("appID", $appID) ||
107-
!$accessToken->is_nonempty_string("appCertificate", $appCertificate) ||
108-
!$accessToken->is_nonempty_string("channelName", $channelName)) {
111+
if (! $accessToken->is_nonempty_string('appID', $appID) ||
112+
! $accessToken->is_nonempty_string('appCertificate', $appCertificate) ||
113+
! $accessToken->is_nonempty_string('channelName', $channelName)) {
109114
return null;
110115
}
111116

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

116121
$accessToken->setUid($uid);
117122
$accessToken->message = new Message();
123+
118124
return $accessToken;
119125
}
120126

121-
static function initWithToken($token, $appCertificate, $channel, $uid)
127+
public static function initWithToken($token, $appCertificate, $channel, $uid)
122128
{
123129
$accessToken = new AccessToken();
124-
if (!$accessToken->extract($token, $appCertificate, $channel, $uid)) {
130+
if (! $accessToken->extract($token, $appCertificate, $channel, $uid)) {
125131
return null;
126132
}
133+
127134
return $accessToken;
128135
}
129136

130-
function addPrivilege($key, $expireTimestamp)
137+
public function addPrivilege($key, $expireTimestamp)
131138
{
132139
$this->message->privileges[$key] = $expireTimestamp;
140+
133141
return $this;
134142
}
135143

136-
function extract($token, $appCertificate, $channelName, $uid)
144+
public function extract($token, $appCertificate, $channelName, $uid)
137145
{
138146
$ver_len = 3;
139147
$appid_len = 32;
140148
$version = substr($token, 0, $ver_len);
141-
if ($version !== "006") {
142-
echo 'invalid version ' . $version;
149+
if ($version !== '006') {
150+
echo 'invalid version '.$version;
151+
143152
return false;
144153
}
145154

146-
if (!$this->is_nonempty_string("token", $token) ||
147-
!$this->is_nonempty_string("appCertificate", $appCertificate) ||
148-
!$this->is_nonempty_string("channelName", $channelName)) {
155+
if (! $this->is_nonempty_string('token', $token) ||
156+
! $this->is_nonempty_string('appCertificate', $appCertificate) ||
157+
! $this->is_nonempty_string('channelName', $channelName)) {
149158
return false;
150159
}
151160

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

155164
$pos = 0;
156-
$len = unpack("v", $content . substr($pos, 2))[1];
165+
$len = unpack('v', $content.substr($pos, 2))[1];
157166
$pos += 2;
158167
$sig = substr($content, $pos, $len);
159168
$pos += $len;
160-
$crc_channel = unpack("V", substr($content, $pos, 4))[1];
169+
$crc_channel = unpack('V', substr($content, $pos, 4))[1];
161170
$pos += 4;
162-
$crc_uid = unpack("V", substr($content, $pos, 4))[1];
171+
$crc_uid = unpack('V', substr($content, $pos, 4))[1];
163172
$pos += 4;
164-
$msgLen = unpack("v", substr($content, $pos, 2))[1];
173+
$msgLen = unpack('v', substr($content, $pos, 2))[1];
165174
$pos += 2;
166175
$msg = substr($content, $pos, $msgLen);
167176

@@ -174,27 +183,29 @@ function extract($token, $appCertificate, $channelName, $uid)
174183
$this->appCertificate = $appCertificate;
175184
$this->channelName = $channelName;
176185
$this->setUid($uid);
186+
177187
return true;
178188
}
179189

180-
function build()
190+
public function build()
181191
{
182192
$msg = $this->message->packContent();
183-
$val = array_merge(unpack("C*", $this->appID), unpack("C*", $this->channelName), unpack("C*", $this->uid), $msg);
193+
$val = array_merge(unpack('C*', $this->appID), unpack('C*', $this->channelName), unpack('C*', $this->uid), $msg);
184194

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

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

190-
$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);
191-
$version = "006";
192-
$ret = $version . $this->appID . base64_encode(implode(array_map("chr", $content)));
200+
$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);
201+
$version = '006';
202+
$ret = $version.$this->appID.base64_encode(implode(array_map('chr', $content)));
203+
193204
return $ret;
194205
}
195206
}
196207

197208
function packString($value)
198209
{
199-
return pack("v", strlen($value)) . $value;
210+
return pack('v', strlen($value)).$value;
200211
}

src/AgoraSDK.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class AgoraSDK
66
{
77
// Build wonderful things
88

9-
public static function Version(){
9+
public static function Version()
10+
{
1011
return 'test';
1112
}
1213
}

src/DynamicKey4.php

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
<?php
22

3-
function generateRecordingKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType='ARS')
3+
function generateRecordingKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType = 'ARS')
44
{
5-
return generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType);
5+
return generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType);
66
}
77

8-
function generateMediaChannelKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType='ACS')
8+
function generateMediaChannelKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType = 'ACS')
99
{
10-
return generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType);
10+
return generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType);
1111
}
1212

13-
function generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs ,$serviceType)
13+
function generateDynamicKey($appID, $appCertificate, $channelName, $ts, $randomInt, $uid, $expiredTs, $serviceType)
1414
{
15-
$version = "004";
15+
$version = '004';
1616

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

20-
$uidStr = "0000000000" . $uid;
21-
$uidStr = substr($uidStr,-10);
22-
23-
$expiredStr = "0000000000" . $expiredTs;
24-
$expiredStr = substr($expiredStr,-10);
20+
$uidStr = '0000000000'.$uid;
21+
$uidStr = substr($uidStr, -10);
2522

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

28-
return $version . $signature . $appID . $ts . $randomStr . $expiredStr;
26+
$signature = generateSignature($appID, $appCertificate, $channelName, $ts, $randomStr, $uidStr, $expiredStr, $serviceType);
27+
28+
return $version.$signature.$appID.$ts.$randomStr.$expiredStr;
2929
}
3030

31-
function generateSignature($appID, $appCertificate, $channelName, $ts, $randomStr, $uidStr, $expiredStr ,$serviceType)
31+
function generateSignature($appID, $appCertificate, $channelName, $ts, $randomStr, $uidStr, $expiredStr, $serviceType)
3232
{
33-
$concat = $serviceType . $appID . $ts . $randomStr . $channelName . $uidStr . $expiredStr;
33+
$concat = $serviceType.$appID.$ts.$randomStr.$channelName.$uidStr.$expiredStr;
34+
3435
return hash_hmac('sha1', $concat, $appCertificate);
3536
}
36-
?>

0 commit comments

Comments
 (0)