Passed
Push — main ( e61083...191161 )
by Aron
02:28
created

IPAPIController::generateJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 16
ccs 13
cts 13
cp 1
crap 1
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\Controller;
4
5
namespace artes\IP;
6
7
// use Anax\Commons\AppInjectableInterface;
8
// use Anax\Commons\AppInjectableTrait;
9
use Anax\Commons\ContainerInjectableInterface;
10
use Anax\Commons\ContainerInjectableTrait;
11
use Anax\Route\Exception\NotFoundException;
12
13
/**
14
 * A sample controller to show how a controller class can be implemented.
15
 * The controller will be injected with $app if implementing the interface
16
 * AppInjectableInterface, like this sample class does.
17
 * The controller is mounted on a particular route and can then handle all
18
 * requests for that mount point.
19
 *
20
 * @SuppressWarnings(PHPMD)
21
 */
22
class IPAPIController implements ContainerInjectableInterface
23
{
24
    use ContainerInjectableTrait;
25
26
    /**
27
     * This is the index method action, it handles:
28
     * ANY METHOD mountpoint
29
     * ANY METHOD mountpoint/
30
     * ANY METHOD mountpoint/index
31
     *
32
     * @return object
33
     */
34 1
    public function indexActionGet() : object
35
    {
36 1
        $page = $this->di->get("page");
37 1
        $realip = new RealIP();
38 1
        $ipaddress = $realip->getRealIpAddr();
39
        $data = [
40 1
            "ip" => $ipaddress
41
        ];
42 1
        $page->add(
43 1
            "ip/ipapi",
44
            $data
45
        );
46
47 1
        return $page->render([
48 1
            "title" => "My IP",
49
        ]);
50
    }
51
52
    /**
53
     * This is the index method action, it handles:
54
     * ANY METHOD mountpoint
55
     * ANY METHOD mountpoint/
56
     * ANY METHOD mountpoint/index
57
     *
58
     * @return array
59
     */
60 1
    public function checkActionGet() : array
61
    {
62 1
        $request = $this->di->get("request");
63 1
        $ip  = $request->getGet("ip", "");
64 1
        $userip = new IPCheck($ip);
65 1
        $ip4 = $userip->ipv4();
66 1
        $ip6 = $userip->ipv6();
67 1
        $userinput = $userip->getUserInput();
68 1
        $corrected = $userip->getCorrectedInput();
69 1
        $domain = $userip->getDomainName();
70 1
        $ipmsg = $userip->printIPMessage();
71 1
        $domainmsg = $userip->printDomainMessage();
72
        $myjson = [
73 1
            "ip4" => $ip4,
74 1
            "ip6" => $ip6,
75 1
            "userinput" => $userinput,
76 1
            "corrected" => $corrected,
77 1
            "domain" => $domain,
78 1
            "ipmsg" => $ipmsg,
79 1
            "domainmsg" => $domainmsg,
80
        ];
81
82 1
        $myjson = $this->generateJSON($myjson, $ip);
83 1
        return [json_encode($myjson, JSON_UNESCAPED_UNICODE)];
84
    }
85
86 2
    public function generateJSON($myjson, $ip)
87
    {
88 2
        $ipkey = "";
89
        // this loads $ipkey and $weatherkey
90 2
        include(ANAX_INSTALL_PATH . '/config/api/apikeys.php');
91 2
        $geoip = new IPGeotag($ipkey);
92 2
        $myjson["continent"] = $geoip->parseJson($ip, "continent_name");
93 2
        $myjson["country"] = $geoip->parseJson($ip, "country_name");
94 2
        $myjson["city"] = $geoip->parseJson($ip, "city");
95 2
        $myjson["zip"] = $geoip->parseJson($ip, "zip");
96 2
        $myjson["language"] = $geoip->parseJson($ip, "location", "languages", "name");
97 2
        $myjson["latitude"] = $geoip->parseJson($ip, "latitude");
98 2
        $myjson["longitude"] = $geoip->parseJson($ip, "longitude");
99 2
        $myjson["map"] = $geoip->getmap($ip);
100
101 2
        return $myjson;
102
    }
103
104
    /**
105
     * This is the index method action, it handles:
106
     * ANY METHOD mountpoint
107
     * ANY METHOD mountpoint/
108
     * ANY METHOD mountpoint/index
109
     *
110
     * @return array
111
     */
112 1
    public function checkActionPost() : array
113
    {
114 1
        $request = $this->di->get("request");
115 1
        $ip  = $request->getPost("ip", "");
116 1
        $userip = new IPCheck($ip);
117 1
        $ip4 = $userip->ipv4();
118 1
        $ip6 = $userip->ipv6();
119 1
        $userinput = $userip->getUserInput();
120 1
        $corrected = $userip->getCorrectedInput();
121 1
        $domain = $userip->getDomainName();
122 1
        $ipmsg = $userip->printIPMessage();
123 1
        $domainmsg = $userip->printDomainMessage();
124
        $myjson = [
125 1
            "ip4" => $ip4,
126 1
            "ip6" => $ip6,
127 1
            "userinput" => $userinput,
128 1
            "corrected" => $corrected,
129 1
            "domain" => $domain,
130 1
            "ipmsg" => $ipmsg,
131 1
            "domainmsg" => $domainmsg,
132
        ];
133
134 1
        $myjson = $this->generateJSON($myjson, $ip);
135 1
        return [json_encode($myjson, JSON_UNESCAPED_UNICODE)];
136
    }
137
}
138