-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdanmaku.php
196 lines (181 loc) · 6.81 KB
/
danmaku.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
require '/opt/cos-php-sdk-v5/vendor/autoload.php';
require '/opt/tencentcloud-sdk-php/TCloudAutoLoader.php';
require 'config.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Cms\V20190321\CmsClient;
use TencentCloud\Cms\V20190321\Models\TextModerationRequest;
//初始化
$cosClient = new Qcloud\Cos\Client(array('region' => $region,
'credentials'=> array(
'secretId' => $secret_id,
'secretKey' => $secret_key)));
//敏感词
function aword($str){
global $appid, $secret_id, $secret_key;
if(!$str){
return false;
}
if(getenv('CMS') == "true"){
try {
echo "CMS Log: \n";
var_dump($secret_id);
$cred = new Credential($secret_id, $secret_key);
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("cms.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new CmsClient($cred, "ap-guangzhou", $clientProfile);
$req = new TextModerationRequest();
$params = json_encode(["Content"=>base64_encode($str)]);
// var_dump($params);
$req->fromJsonString($params);
$resp = $client->TextModeration($req);
$result = json_decode($resp->toJsonString());
var_dump($result);
}
catch(TencentCloudSDKException $e) {
echo "CMS Error:\n";
echo $e;
}
if($result->Data->EvilFlag)
return true;
}
$list = require("aword.php");
for ($i=0; $i<count($list); $i++ ){
$content = substr_count($str, base64_decode($list[$i]));
if( $content > 0 ){
$info = $content;
break;
}
}
return $info>0 ? true : false;
}
function main_handler($event, $context) {
print "Event info:\n";
print_r($event);
global $appid, $secret_id, $secret_key;
global $cosClient;
global $bucket;
global $origin;
$event = (array)$event;
// print json_encode($event);
if($event['headers']->origin){
//生产环境
//检测域名,如不需要可注释掉
if(!strpos($event['headers']->origin, $origin)){
return ["code"=>"0", "msg"=>"Failed to match the origin"];
}
}
try {
//列出Bucket
$result = $cosClient->listBuckets();
if(strpos(json_encode((array)$result['Buckets']), "danmaku-".$appid)){
//bucket存在
}else{
//尝试创建bucket
try {
$bucket = "danmaku-".$appid; //存储桶名称 格式:BucketName-APPID
$result = $cosClient->createBucket(array('Bucket' => $bucket));
//请求成功
echo "Bucket Create:\n";
print_r($result);
} catch (\Exception $e) {
//请求失败
die($e);
}
}
} catch (\Exception $e) {
//请求失败
die($e);
}
if($event['httpMethod'] == "GET"){
//读取弹幕
$danmakuFile = "https://".$bucket.".cos.ap-chengdu.myqcloud.com/data/".$event['queryStringParameters']->id.".json";
$danmakuContent = file_get_contents($danmakuFile);
if($danmakuContent){
return(json_decode($danmakuContent));
}else{
return ["code"=>0, "data"=>[]];
}
}elseif($event['httpMethod'] == "POST"){
//创建弹幕
//先看看弹幕文件是否存在
$body = json_decode($event['body']);
//敏感词
if(aword($body->text))
return ["code"=> -1, "msg"=>"无法发送"];
if($body->id != ""){
$danmakuFile = "https://".$bucket.".cos.ap-chengdu.myqcloud.com/data/".$body->id.".json";
$danmakuContent = file_get_contents($danmakuFile);
var_dump($danmakuContent);
$key = "tmp/".$body->id.".json";
$reqid = (array)$event['headers'];
$reqid = $reqid['x-api-requestid'];
$danmaku = ["code"=>0, "data"=>[[
$body->time, $body->type, $body->color, $reqid, $body->text
]]]; //随便生成一个
if(!$danmakuContent){
//创建临时文件
fopen("/".$key, "w");
if($body->time && $body->color && $body->text){
//写下第一条弹幕~~
file_put_contents("/".$key, json_encode($danmaku));
}else{
return ["code"=>0, "msg"=> "Danmaku not completed:", $body];
}
//上传到Bucket
try {
$result = $cosClient->putObject(array(
'Bucket' => "danmaku-".$appid,
'Key' => "data/".$body->id.".json",
'Body' => fopen("/".$key, 'rb'),
));
// 请求成功
echo "File upload: \n";
print_r($result);
// $this->versionId = $result['VersionId'];
return $danmaku;
} catch (\Exception $e) {
// 请求失败
echo($e);
}
}else{
//弹幕文件已经存在了,那就先读取,再合并,再上传吧!
//创建临时文件
fopen("/".$key, "w");
if($body->time && $body->color && $body->text){
$newDanmaku = (array)json_decode($danmakuContent);
var_dump($danmaku);
array_push($newDanmaku["data"], $danmaku['data'][0]);
print json_encode($newDanmaku);
file_put_contents("/".$key, json_encode($newDanmaku));
}else{
return ["code"=>0, "msg"=> "Danmaku not completed:", $body];
}
//上传到Bucket
try {
$result = $cosClient->putObject(array(
'Bucket' => "danmaku-".$appid,
'Key' => "data/".$body->id.".json",
'Body' => fopen("/".$key, 'rb'),
));
// 请求成功
echo "File upload: \n";
print_r($result);
return $danmaku;
} catch (\Exception $e) {
echo($e);
}
}
}
}else{
//未知请求方式
return ["code"=>0, "msg"=> "Unkow Request Method"];
}
return ["code"=> -1, "msg"=> "Unknown Error"];
}
?>