diff --git a/.gitea/workflows/laravel.yaml b/.gitea/workflows/laravel.yaml new file mode 100644 index 0000000..b70db22 --- /dev/null +++ b/.gitea/workflows/laravel.yaml @@ -0,0 +1,41 @@ +name: Tests + +on: + push: + branches: + - master + - '*.x' + pull_request: + schedule: + - cron: '0 0 * * *' + +permissions: + contents: read + +jobs: + tests: + runs-on: centos + + name: PHP + + steps: + - name: '检查代码' + uses: https://github.com/actions/checkout@v3 + + - name: '安装PHP' + uses: XiaoLFeng/setup-php@v2 + with: + coverage: none + tools: phpmd + + - name: '检查命名问题' + run: phpmd ./ text naming + continue-on-error: true + + - name: '检查设计问题' + run: phpmd ./ text naming design + continue-on-error: true + + - name: '检查代码规模是否过大' + run: phpmd ./ text codesize + continue-on-error: true diff --git a/app/Http/Controllers/Function/Link.php b/app/Http/Controllers/Function/Link.php index ee85a7d..f60de43 100644 --- a/app/Http/Controllers/Function/Link.php +++ b/app/Http/Controllers/Function/Link.php @@ -18,7 +18,6 @@ use Illuminate\Mail\Message; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Mail; -use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Validator; @@ -264,119 +263,149 @@ public function apiCustomBlogCheck(HttpRequest $request): JsonResponse { /** @var array $returnData Json的 return 返回值 */ // 验证数据 - $resultBlog = DB::table('blog_link') - ->select('id', 'blogOwnEmail') - ->find((int)$request->id); - if (!empty($resultBlog->id)) { - // 检查输入博客是否对应 - if (!empty($resultBlog->blogOwnEmail)) { - if (strcmp($resultBlog->blogOwnEmail, $request->email) == 0) { - // 生成验证码(筛查内容) - $resultVerifyCode = DB::table('code') - ->where([ - ['email', '=', $resultBlog->blogOwnEmail], - ['type', '=', 'CODE-CUSTOM-CHECK'], - ['time', '>', time()]]) - ->get() - ->toArray(); - // 不存在验证码,生成验证码并存入数据库中 - if (empty($resultVerifyCode[0]->id)) { - // 生成6位数验证码 - $verifyCode = null; - for ($i = 0; $i < 6; $i++) - $verifyCode .= rand(0, 9); + $dataCheck = Validator::make($request->all(), [ + 'id' => 'required|int', + 'userEmail' => 'required|email', + 'userCode' => 'string|min:6|max:64|regex:#^[0-9A-Za-z]+$#', + ]); - // 存入数据库 - DB::table('code') - ->insert([ - 'email' => $resultBlog->blogOwnEmail, - 'code' => $verifyCode, - 'type' => 'CODE-CUSTOM-CHECK', - 'sendTime' => time(), - 'time' => time()+900, - ]); - // 数据整理 - $this->sendEmail = [ - 'userEmail' => $resultBlog->blogOwnEmail, - 'verifyCode' => $verifyCode, - 'sendTime' => time(), - ]; - $this->apiCustomBlogCheckSendEmail(); - $returnData = [ - 'output' => 'Success', - 'code' => 200, - 'data' => [ - 'message' => '发送成功', - ], - ]; - } else { - // 存在验证码,检查验证码是否需要重新发送 - $data = DB::table('code') + if (!$dataCheck->fails()) { + $resultBlog = DB::table('blog_link') + ->select('id', 'blogOwnEmail') + ->find((int)$request->id); + if (!empty($resultBlog->id)) { + // 检查输入博客是否对应 + if (!empty($resultBlog->blogOwnEmail)) { + if (strcmp($resultBlog->blogOwnEmail, $request->userEmail) == 0) { + // 生成验证码(筛查内容) + $resultVerifyCode = DB::table('code') ->where([ - ['email','=',$resultBlog->blogOwnEmail], - ['type','=','CODE-CUSTOM-CHECK'], - ['time','>',time()]]) + ['email', '=', $resultBlog->blogOwnEmail], + ['type', '=', 'CODE-CUSTOM-CHECK'], + ['time', '>', time()]]) ->get() ->toArray(); - $this->sendEmail = [ - 'userEmail' => $data[0]->email, - 'verifyCode' => $data[0]->code, - 'sendTime' => time(), - ]; - if ($resultVerifyCode[0]->sendTime < time()-60) { - // 发送验证码 + // 不存在验证码,生成验证码并存入数据库中 + if (empty($resultVerifyCode[0]->id)) { + // 生成6位数验证码 + $verifyCode = null; + for ($i = 0; $i < 6; $i++) + $verifyCode .= rand(0, 9); + + // 存入数据库 DB::table('code') - ->where([ - ['email','=',$resultBlog->blogOwnEmail], - ['type','=','CODE-CUSTOM-CHECK'], - ['time','>',time()]]) - ->update(['sendTime' => time()]); + ->insert([ + 'email' => $resultBlog->blogOwnEmail, + 'code' => $verifyCode, + 'type' => 'CODE-CUSTOM-CHECK', + 'sendTime' => time(), + 'time' => time() + 900, + ]); + // 数据整理 + $this->sendEmail = [ + 'userEmail' => $resultBlog->blogOwnEmail, + 'verifyCode' => $verifyCode, + 'sendTime' => time(), + ]; $this->apiCustomBlogCheckSendEmail(); $returnData = [ 'output' => 'Success', 'code' => 200, 'data' => [ - 'message' => '重新发送成功', + 'message' => '发送成功', ], ]; } else { - // 避免重复发送 - $returnData = [ - 'output' => 'SendingTimeTooFast', - 'code' => 403, - 'data' => [ - 'message' => '邮件重新发送时间过快', - 'data' => [ - 'time' => 60 - (time() - $resultVerifyCode[0]->sendTime), - ], - ], + // 存在验证码,检查验证码是否需要重新发送 + $data = DB::table('code') + ->where([ + ['email', '=', $resultBlog->blogOwnEmail], + ['type', '=', 'CODE-CUSTOM-CHECK'], + ['time', '>', time()]]) + ->get() + ->toArray(); + $this->sendEmail = [ + 'userEmail' => $data[0]->email, + 'verifyCode' => $data[0]->code, + 'sendTime' => time(), ]; + if ($resultVerifyCode[0]->sendTime < time() - 60) { + // 发送验证码 + DB::table('code') + ->where([ + ['email', '=', $resultBlog->blogOwnEmail], + ['type', '=', 'CODE-CUSTOM-CHECK'], + ['time', '>', time()]]) + ->update(['sendTime' => time()]); + $this->apiCustomBlogCheckSendEmail(); + $returnData = [ + 'output' => 'Success', + 'code' => 200, + 'data' => [ + 'message' => '重新发送成功', + ], + ]; + } else { + // 避免重复发送 + $returnData = [ + 'output' => 'SendingTimeTooFast', + 'code' => 403, + 'data' => [ + 'message' => '邮件重新发送时间过快', + 'data' => [ + 'time' => 60 - (time() - $resultVerifyCode[0]->sendTime), + ], + ], + ]; + } } + } else { + $returnData = [ + 'output' => 'EmailMismatch', + 'code' => 403, + 'data' => [ + 'message' => '邮箱与对应ID不匹配', + ], + ]; } } else { $returnData = [ - 'output' => 'EmailMismatch', + 'output' => 'NoEmail', 'code' => 403, 'data' => [ - 'message' => '邮箱与对应ID不匹配', + 'message' => '对应ID没有绑定邮箱,请联系管理员', ], ]; } } else { $returnData = [ - 'output' => 'NoEmail', + 'output' => 'NoBlog', 'code' => 403, 'data' => [ - 'message' => '对应ID没有绑定邮箱,请联系管理员', + 'message' => '没有ID对应博客', ], ]; } } else { + $errorType = array_keys($dataCheck->failed()); + $i = 0; + foreach ($dataCheck->failed() as $valueData) { + $errorInfo[$errorType[$i]] = array_keys($valueData); + if ($i == 0) { + $errorSingle = [ + 'info' => $errorType[$i], + 'need' => $errorInfo[$errorType[$i]], + ]; + } + $i++; + } $returnData = [ - 'output' => 'NoBlog', + 'output' => 'DataFormatError', 'code' => 403, 'data' => [ - 'message' => '没有ID对应博客', + 'message' => '输入内容有错误', + 'errorSingle' => $errorSingle, + 'error' => $errorInfo, ], ]; } @@ -386,8 +415,7 @@ public function apiCustomBlogCheck(HttpRequest $request): JsonResponse /** * 站长认证邮件发送模板 * - * @param array $data - * @return void + * @return void 发送链接,不做返回内容 */ private function apiCustomBlogCheckSendEmail(): void { @@ -399,7 +427,123 @@ private function apiCustomBlogCheckSendEmail(): void }); } - public function viewEditFriend($friendId): Application|Factory|View|RedirectResponse + /** + * 验证是否为站长 + * + * @param HttpRequest $request 获取HTTP中 Request 数据 + * @return JsonResponse 返回JSON数据 + */ + public function apiCustomBlogVerify(HttpRequest $request): JsonResponse + { + /** @var array $returnData Json的 return 返回值 */ + /** @var mixed $cookie 保存Cookie数据 */ + //数据验证 + $dataCheck = Validator::make($request->all(), [ + 'id' => 'required|int', + 'userEmail' => 'required|email', + 'userCode' => 'required|string|min:6|max:64|regex:#^[0-9A-Za-z]+$#', + ]); + // 验证数据是否合法 + if (!$dataCheck->fails()) { + $cookie = cookie('friend_edit', '', -1, '/'); + // 检查内容是否存在 + $resultBlog = DB::table('blog_link') + ->select('id', 'blogOwnEmail') + ->find((int)$request->id); + if (!empty($resultBlog->id)) { + if (!empty($resultBlog->blogOwnEmail)) { + // 验证此邮箱是否与该博客一致 + if (strcmp($resultBlog->blogOwnEmail, $request->userEmail) == 0) { + // 检查验证码是否存在 + $resultCode = DB::table('code') + ->select('id') + ->where([ + ['code.code', '=', $request->userCode], + ['email', '=', $request->userEmail], + ['type', '=', 'CODE-CUSTOM-CHECK'], + ['time', '>', time()]]) + ->get() + ->toArray(); + if (!empty($resultCode[0]->id)) { + // 配置Cookie + $cookie = cookie('friend_edit', password_hash($resultBlog->id, PASSWORD_DEFAULT), 15, '/',); + // 完成验证删除验证码 + DB::table('code') + ->delete((int)$resultCode[0]->id); + // Json + $returnData = [ + 'output' => 'Success', + 'code' => 200, + 'data' => [ + 'message' => '验证成功', + 'id' => $resultBlog->id + ], + ]; + } else { + // 验证码验证失败 + $returnData = [ + 'output' => 'NoVerifyCode', + 'code' => 403, + 'data' => [ + 'message' => '没有这个验证码', + ], + ]; + } + } else { + $returnData = [ + 'output' => 'EmailMismatch', + 'code' => 403, + 'data' => [ + 'message' => '邮箱与对应ID不匹配', + ], + ]; + } + } else { + $returnData = [ + 'output' => 'NoEmail', + 'code' => 403, + 'data' => [ + 'message' => '对应ID没有绑定邮箱,请联系管理员', + ], + ]; + } + } else { + $returnData = [ + 'output' => 'NoBlog', + 'code' => 403, + 'data' => [ + 'message' => '没有ID对应博客', + ], + ]; + } + } else { + $errorType = array_keys($dataCheck->failed()); + $i = 0; + foreach ($dataCheck->failed() as $valueData) { + $errorInfo[$errorType[$i]] = array_keys($valueData); + if ($i == 0) { + $errorSingle = [ + 'info' => $errorType[$i], + 'need' => $errorInfo[$errorType[$i]], + ]; + } + $i++; + } + $returnData = [ + 'output' => 'DataFormatError', + 'code' => 403, + 'data' => [ + 'message' => '输入内容有错误', + 'errorSingle' => $errorSingle, + 'error' => $errorInfo, + ], + ]; + } + return Response::json($returnData, $returnData['code']) + ->cookie($cookie); + } + + protected function viewEditFriend(HttpRequest $request, $friendId): Application|Factory|View|RedirectResponse { // 检查内容是否为空 if (!empty($friendId)) { @@ -410,19 +554,27 @@ public function viewEditFriend($friendId): Application|Factory|View|RedirectResp ->find($friendId); if (!empty($resultBlog->id)) { // 检查是否存在Cookie作为已验证 - if (Request::hasCookie('friend_edit')) { + if ($request->hasCookie('friend_edit')) { // 检查COOKIE与所验证ID是否匹配 - if (password_verify($friendId, Request::cookie('friend_edit'))) { + if (password_verify($resultBlog->id, $request->cookie('friend_edit'))) { + $this->data['blog'] = $resultBlog; + $this->data['blogColor'] = DB::table('blog_color') + ->orderBy('id') + ->get() + ->toArray(); + $this->data['blogSort'] = DB::table('blog_sort') + ->orderBy('sort') + ->get() + ->toArray(); return view('function.edit-friend', $this->data); } else { - response()->withCookie(cookie('friend_edit', null, time() - 1)); - return Response::redirectTo(route('function.edit-search')); + $cookie = cookie('friend_edit', '', -1, '/'); + return Response::redirectTo(route('function.edit-search')) + ->cookie($cookie); } } else { // 验证页面 - // 加密用户邮箱 - $this->data['blog'] = $resultBlog; - return view('function.edit-check', $this->data); + return Response::redirectTo(route('function.edit-searchOnly', $resultBlog->id)); } } else { // 不存在这一个ID用户 @@ -469,13 +621,29 @@ protected function viewSearchFriends(): Factory|View|Application protected function viewSearchFriend($friendId): Factory|View|Application|RedirectResponse { + /** @var $dataEmail array 获取修改邮箱后的值 */ $this->data['webSubTitle'] = '查询列表'; if (!empty($friendId)) { // 检查 friendId 是否存在 $resultBlog = DB::table('blog_link') - ->select('id','blogOwnEmail') + ->select('id', 'blogOwnEmail', 'blogName') ->find($friendId); if (!empty($resultBlog->id)) { + // 处理加密邮箱 + $strlenEmail = strlen($resultBlog->blogOwnEmail); + ($strlenEmail > 4) ? $j = 1 : $j = 0; + for ($i = 0; $i < $strlenEmail; $i++) { + if ($resultBlog->blogOwnEmail[$i] != '@') { + if ($i > $j && $i < $strlenEmail - ($j + 1)) { + $dataEmail[$i] = '*'; + } else { + $dataEmail[$i] = $resultBlog->blogOwnEmail[$i]; + } + } else { + $dataEmail[$i] = $resultBlog->blogOwnEmail[$i]; + } + } + $resultBlog->blogOwnEmail = implode($dataEmail); $this->data['blog'] = $resultBlog; return view('function.edit-check', $this->data); } else { diff --git a/app/Http/Middleware/EncryptCookies.php b/app/Http/Middleware/EncryptCookies.php index 6839135..1fe6b81 100644 --- a/app/Http/Middleware/EncryptCookies.php +++ b/app/Http/Middleware/EncryptCookies.php @@ -17,6 +17,6 @@ class EncryptCookies extends Middleware * @var array */ protected $except = [ - // + 'friend_edit', ]; } diff --git a/phpunit.xml b/phpunit.xml index e4386c4..531beb9 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -24,14 +24,14 @@ - - - - - - - - - + + + + + + + + + diff --git a/resources/views/function/edit-check.blade.php b/resources/views/function/edit-check.blade.php new file mode 100644 index 0000000..a1ee5b5 --- /dev/null +++ b/resources/views/function/edit-check.blade.php @@ -0,0 +1,287 @@ + + + + + + + + + @include('modules.head') + {!! $webHeader !!} + + +
+ @include('modules.navbar') +
+ +
+
+ + + +
+ + +
+
+
+
+
+
+
+

+ 身份验证

+

+ 您好 {{ $blog->blogName }} 的站长

+ +
+ + +
+
+ +
+ +
+
+
+ +
+
+ +
+ + +
+
+
+ +
+
+
+
+
+
+ + @include('modules.footer') +
+
+ + + + + + +{!! $webFooter !!} + diff --git a/resources/views/function/edit-friend.blade.php b/resources/views/function/edit-friend.blade.php new file mode 100644 index 0000000..39dce30 --- /dev/null +++ b/resources/views/function/edit-friend.blade.php @@ -0,0 +1,390 @@ + + + + + + + + + @include('modules.head') + {!! $webHeader !!} + + +
+ @include('modules.navbar') +
+ +
+
+

+ 自助友链修改系统

+

+ 您好 {{ $blog->blogName }} 的站长

+
+
+
+ +
+
+ +
+ +
+
+
+ + +
+
+ +
+ +
+
+
+
+
+
+ +
+
+ +
+ +
+
+
+ + +
+
+ +
+ +
+
+
+
+ + +
+
+ +
+ +
+
+
+ + +
+
+ +
+ +
+
+
+
+ +
+
+ + +
+
+ +
+ +
+
+
+
+
+
+ + +
+
+ + + +
+
+
+ + +
+
+
+
+ + @include('modules.footer') +
+
+ + + + + + + + +{!! $webFooter !!} + diff --git a/resources/views/function/make-friend.blade.php b/resources/views/function/make-friend.blade.php index 1a6ecc4..05383bd 100644 --- a/resources/views/function/make-friend.blade.php +++ b/resources/views/function/make-friend.blade.php @@ -210,19 +210,28 @@ class="relative left-[calc(50%+3rem)] aspect-[1155/678] w-[36.125rem] -translate
友链已登记 -
已经存有该博客(博客名字、博客地址、博主邮箱不得重复),请确认您没有输入错误吗?(如果想修改已登记博客,请使用在友链登记邮箱进行注册/登录进行修改)
+
已经存有该博客(博客名字、博客地址、博主邮箱不得重复),请确认您没有输入错误吗?(如果想修改已登记博客,请使用在友链登记邮箱进行注册/登录进行修改) +
- diff --git a/routes/api.php b/routes/api.php index c538bc5..1ee2714 100644 --- a/routes/api.php +++ b/routes/api.php @@ -46,5 +46,6 @@ Route::post('add',[Link::class,'apiCustomAdd'])->name('api.link.custom.add'); Route::get('search',[Link::class, 'apiCustomSearch'])->name('api.link.custom.search'); Route::post('blogCheck',[Link::class,'apiCustomBlogCheck'])->name('api.link.custom.blogCheck'); + Route::post('blogVerify',[Link::class,'apiCustomBlogVerify'])->name('api.link.custom.blogVerify'); }); }); diff --git a/routes/web.php b/routes/web.php index 7d18a0c..d3e3e34 100644 --- a/routes/web.php +++ b/routes/web.php @@ -31,12 +31,9 @@ Route::prefix('function')->group(function () { Route::get('link',[UserLink::class, 'viewLink'])->name('function.link'); Route::get('make-friend',[UserLink::class, 'viewMakeFriend'])->name('function.make-friend'); - Route::get('edit-search',[UserLink::class, 'viewSearchFriends'])->name('function.edit-search'); + Route::get ('edit-search',[UserLink::class, 'viewSearchFriends'])->name('function.edit-search'); Route::get('edit-search/{friendId}',[UserLink::class,'viewSearchFriend'])->name('function.edit-searchOnly'); - Route::get('edit-friend/{friendId}',function ($friendId) { - $userLink = new UserLink(); - return $userLink->viewEditFriend($friendId); - })->name('function.edit-friend'); + Route::get('edit-friend/{friendId}',[UserLink::class,'viewEditFriend'])->name('function.edit-friend'); Route::get('sponsor',function () { return view('function.sponsor'); })->name('function.sponsor');