Passed
Branch 0.2 (92feb3)
by Lars
03:29
created

SecureCommentInputMiddleware   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0
wmc 17

1 Method

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