Issues (1933)

a/src/Controller/ipJsonController.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Ylvan\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
 * A JSON controller to show validation status of an IP
14
 * @SuppressWarnings(PHPMD.LongVariable)
15
 * @SuppressWarnings(PHPMD.ShortVariable)
16
 */
17
class IpJsonController implements ContainerInjectableInterface
18
{
19
    use ContainerInjectableTrait;
20
21
    public function indexActionGet() : object
22
    {
23
        $page = $this->di->get("page");
24
        $session = $this->di->session;
0 ignored issues
show
Accessing session on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
25
26
        $ipv4 = $session->get("ipv4", null);
27
        $ipv6 = $session->get("ipv6", null);
28
        $domain = $session->get("domain", null);
29
        $ipToValidate = $session->get("ipToValidate", null);
30
31
32
        $json = [
33
            "ipToValidate" => $ipToValidate,
34
            "ipv4" => $ipv4,
35
            "ipv6" => $ipv6,
36
            "domainName" => $domain
37
        ];
38
39
        $data = [
40
            "title" => "Validera med JSON",
41
            "json" => $json,
42
        ];
43
44
        $page->add("ip/validateJson", $data);
45
46
47
        // $title = "Validera IP med JSON";
48
        $title = "Validera IP med JSON";
49
        return $page->render([
50
            "title" => $title,
51
        ]);
52
    } 
53
54
55
    /**
56
     * This is the index method action, it handles:
57
     * GET METHOD mountpoint
58
     * GET METHOD mountpoint/
59
     * GET METHOD mountpoint/index
60
     *
61
     * @return array
62
     */
63
    public function indexActionPost() : array
64
    {
65
        // $page = $this->di->get("page");
66
        $request = $this->di->get("request");
67
        $session = $this->di->session;
0 ignored issues
show
Accessing session on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
68
69
70
        $ipToValidate = $request->getPOST("ip", null);
71
        $doValidate = $request->getPOST("doValidate", null);
72
73
        if ($doValidate) {
74
            $ipv4 = $this->ipv4($ipToValidate);
75
            $ipv6 = $this->ipv6($ipToValidate);
76
            $domain = $this->domainName($ipToValidate);
77
            // }
78
            $session->set("ipv4", $ipv4);
79
            $session->set("ipv6", $ipv6);
80
            $session->set("domain", $domain);
81
            $session->set("ipToValidate", $ipToValidate);
82
        }
83
84
        // Deal with the action and return a response.
85
        $json = [
86
            "ipToValidate" => $ipToValidate ?? null,
87
            "ipv4" => $ipv4 ?? null,
88
            "ipv6" => $ipv6 ?? null,
89
            "domainName" => $domain ?? null
90
        ];
91
        return [$json];
92
    }
93
94
    
95
96
    /**
97
     * check if ipv4 is valid
98
     */
99
    public function ipv4($ip) : string
100
    {
101
        // code for ip check
102
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
103
            return "Validerar";
104
        } else {
105
            return "Validerar ej";
106
        }
107
    }
108
109
    /**
110
     * check if ipv6 is valid
111
     */
112
    public function ipv6($ip) : string
113
    {
114
        // code for ip check
115
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
116
            return "Validerar";
117
        } else {
118
            return "Validerar ej";
119
        }
120
    }
121
122
    
123
    /**
124
     * check if ip have a domain name
125
     */
126
    public function domainName($ip) : string
127
    {
128
        if (filter_var($ip, FILTER_VALIDATE_IP)) {
129
            return gethostbyaddr($ip);
130
        } else {
131
            return "ip ej validerad";
132
        }
133
    }
134
}
135