1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use App\Blog; |
6
|
|
|
use App\Comments; |
7
|
|
|
use App\Helper\FormatHelper; |
8
|
|
|
use Closure; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
|
11
|
|
|
class SecureCommentInputMiddleware |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Handle an incoming request. |
15
|
|
|
* |
16
|
|
|
* @param Request $request |
17
|
|
|
* @param Closure $next |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
|
|
public function handle(Request $request, Closure $next) |
21
|
|
|
{ |
22
|
|
|
$blog = new Blog(); |
23
|
|
|
$comment = new Comments(); |
24
|
|
|
$method = $request->getMethod(); |
25
|
|
|
$requestPath = $request->getRequestUri(); |
26
|
|
|
$returnArray = array(); |
27
|
|
|
$returnStatus = 0; |
28
|
|
|
|
29
|
|
|
$authorName = $request->input("authorName"); |
30
|
|
|
$content = $request->input("content"); |
31
|
|
|
$captcha = $request->input("captcha"); |
32
|
|
|
$spam = $request->input("computer"); |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
if ($method == "POST" && $requestPath == "/api/comment/add") { |
36
|
|
|
if ($spam != null) { |
37
|
|
|
$returnArray["error-code"] = "bot-detected"; |
38
|
|
|
$returnStatus = 403; |
39
|
|
|
} else if ($captcha == null) { |
40
|
|
|
$returnArray["error-code"] = "captcha-missing"; |
41
|
|
|
$returnStatus = 400; |
42
|
|
|
} else if ($captcha != getenv("CAPTCHA_SECRET")) { |
43
|
|
|
$returnArray["error-code"] = "captcha-wrong"; |
44
|
|
|
$returnStatus = 400; |
45
|
|
|
} else { |
46
|
|
|
$blogHash = $request->input("blogHash"); |
47
|
|
|
$articleHash = $request->input("articleHash"); |
48
|
|
|
$blogResult = $blog->where("hash", $blogHash)->first(); |
49
|
|
|
|
50
|
|
|
$articleTitle = $request->input("articleTitle"); |
51
|
|
|
$articleAuthor = $request->input("articleAuthor"); |
52
|
|
|
$articleUrl = $request->input("articleUrl"); |
53
|
|
|
|
54
|
|
|
if ($blogHash == null || $articleHash == null || $articleTitle == null || $articleAuthor == null || $articleUrl == null || $authorName == null || $content == null) { |
55
|
|
|
$returnArray["error-code"] = "invalid-request"; |
56
|
|
|
$returnStatus = 400; |
57
|
|
|
} else if ($blogResult == null) { |
58
|
|
|
$returnArray["error-code"] = "blog-not-found"; |
59
|
|
|
$returnStatus = 404; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} else if ($method == "PUT" && $requestPath == "/api/comment") { |
63
|
|
|
$hash = $request->input("hash"); |
64
|
|
|
$commentResult = $comment->where("hash", $hash)->first(); |
65
|
|
|
if ($commentResult == null) { |
66
|
|
|
$returnArray["error-code"] = "comment-not-found"; |
67
|
|
|
$returnStatus = 404; |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$returnArray["error-code"] = "request-not-found"; |
71
|
|
|
$returnStatus = 400; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!empty($returnArray)) { |
75
|
|
|
return FormatHelper::formatData($returnArray, false, $returnStatus); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $next($request); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|