Completed
Pull Request — master (#7)
by Lars
03:19
created

SecureCommentInputMiddleware   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0
wmc 18

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 56 18
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
        if ($spam != null) {
35
            $returnArray["error-code"] = "bot-detected";
36
            $returnStatus = 403;
37
        } else if ($captcha == null) {
38
            $returnArray["error-code"] = "captcha-missing";
39
            $returnStatus = 400;
40
        } else if ($captcha != getenv("CAPTCHA_SECRET")) {
41
            $returnArray["error-code"] = "captcha-wrong";
42
            $returnStatus = 400;
43
        } else if ($method == "POST" && $requestPath == "/api/comment/add") {
44
            $blogHash = $request->input("blogHash");
45
            $articleHash = $request->input("articleHash");
46
            $blogResult = $blog->where("hash", $blogHash)->first();
47
48
            $articleTitle = $request->input("articleTitle");
49
            $articleAuthor = $request->input("articleAuthor");
50
            $articleUrl = $request->input("articleUrl");
51
52
            if ($blogHash == null && $articleHash == null && $articleTitle == null && $articleAuthor == null && $articleUrl == null && $authorName == null && $content == null) {
53
                $returnArray["error-code"] = "invalid-request";
54
                $returnStatus = 400;
55
            } else if ($blogResult == null) {
56
                $returnArray["error-code"] = "blog-not-found";
57
                $returnStatus = 404;
58
            }
59
        } else if ($method == "PUT" && strpos($requestPath, "/api/comment/edit/") !== false) {
60
            $hash = $request->route()[2]["hash"];
61
            $commentResult = $comment->where("hash", $hash)->first();
62
            if ($commentResult == null) {
63
                $returnArray["error-code"] = "comment-not-found";
64
                $returnStatus = 404;
65
            }
66
        } else {
67
            $returnArray["error-code"] = "request-not-found";
68
            $returnStatus = 400;
69
        }
70
71
        if (!empty($returnArray)) {
72
            return FormatHelper::formatData($returnArray, false, $returnStatus);
73
        }
74
75
        return $next($request);
76
    }
77
}
78