Passed
Push — master ( 7fd069...1d579c )
by Magnus
02:07
created

IPHandler::checkIP()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.4578

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 26
ccs 4
cts 14
cp 0.2857
crap 3.4578
rs 9.7666
1
<?php
2
3
namespace Malm18\IPChecker;
4
5
class IPHandler
6
{
7
8
9
    /**
10
    * Check active.
11
    *
12
    */
13
    // public function checkIP($theIP)
14
    // {
15
    //     $theIP2 = $theIP . " svansen";
16
    //     return $theIP2;
17
    // }
18
19
    // public function checkIP2($theIP)
20
    // {
21
    //     $hostname = "";
22
    //     $type = "";
23
    //
24
    //
25
    //     if (filter_var($theIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
26
    //         $type = "IPv6";
27
    //         $hostname = gethostbyaddr("$theIP");
28
    //     } elseif (filter_var($theIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
29
    //         $type = "IPv4";
30
    //         $hostname = gethostbyaddr("$theIP");
31
    //     } else {
32
    //         $type = "Inte riktig IP-adress";
33
    //     }
34
    //
35
    //     $ipInfo = array("ipaddress"=>$theIP, "hostname"=>$hostname, "type"=>$type);
36
    //     return $ipInfo;
37
    // }
38
39
40 2
    public function checkIP($theIP)
41
    {
42 2
        if (filter_var($theIP, FILTER_VALIDATE_IP)) {
43
            $url = 'http://api.ipstack.com/';
44
            $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
45
            // $this->ipstackKey = $keys["ipstackKey"];
46
            // $apiKey = $this->ipstackKey;
47
            $apiKey = $keys["ipstackKey"];
48
            $requestUrl = $url . $theIP . '?access_key=' . $apiKey;
49
            $curl = curl_init($requestUrl);
50
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_RETURNTRANSFER, true);
Loading history...
51
        // curl_setopt($curl, CURLOPT_HTTPHEADER, [
52
        //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
53
        //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
54
        //   'Content-Type: application/json'
55
        // ]);
56
            $response = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
57
            $response2 = json_decode($response, true);
58
            curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
59
            // echo $response . PHP_EOL;
60
            return $response2;
61
        } else {
62 2
            $response = array("type" => "not valid ip", "ip" => "", "latitude"=> "", "longitude"=> "",
63
            "city" => "", "country_name" => "", "region_name" => "", "continent_name" => "", "location['country_code']" => "");
64
            // $response2 = json_decode($response, true);
65 2
            return $response;
66
        }
67
    }
68
69
70 4
    public function minLong($longitude)
71
    {
72
73 4
        $minLong = floatval($longitude)-0.6427;
74 4
        return $minLong;
75
    }
76
77 4
    public function maxLong($longitude)
78
    {
79
80 4
        $maxLong = floatval($longitude)+0.6427;
81 4
        return $maxLong;
82
    }
83
84 4
    public function minLat($latitude)
85
    {
86
87 4
        $minLat = (floatval($latitude)) - 0.260;
88 4
        return $minLat;
89
    }
90
91 4
    public function maxLat($latitude)
92
    {
93
94 4
        $maxLat = (floatval($latitude)) + 0.260;
95 4
        return $maxLat;
96
    }
97
98
99
    // public function mapLink($latitude, $longitude, $minLat, $maxLat, $minLong, $maxLong)
100
    // {
101
    //                 https://www.openstreetmap.org/export/embed.html?bbox=-6.8860333396912%2C53.093889465332%2C-5.6006333396912%2C53.613889465332&amp;layer=mapnik&amp;marker=53.353889465332%2-6.2433333396912
102
    //     // $link = "https://www.openstreetmap.org/export/embed.html?bbox=12.669982910156252%2C55.56592203025787%2C13.955383300781252%2C56.08506381314523&amp;layer=mapnik&amp;marker=55.82635894724891%2C13.31268310546875"
103
    //
104
    //     $link = "https://www.openstreetmap.org/export/embed.html?bbox=" . $minLong . "%2C" . $minLat . "%2C" . $maxLong . "%2C" . $maxLat . "&amp;layer=mapnik&amp;marker=" . $latitude . "%2C" . $longitude;
105
    //
106
    //     return $link;
107
    // }
108
109 4
    public function mapLink($latitude, $longitude, $minLat, $maxLat, $minLong, $maxLong)
110
    {
111 4
        if ($latitude) {
112 3
            $link = "https://www.openstreetmap.org/export/embed.html?bbox=" . $minLong . "%2C" . $minLat . "%2C" . $maxLong . "%2C" . $maxLat . "&amp;layer=mapnik&amp;marker=" . $latitude . "%2C" . $longitude;
113
        } else {
114 1
            $link = "https://www.openstreetmap.org/export/embed.html?bbox=-0.64%2C85%2C0.64%2C90&amp;layer=mapnik&amp;marker=87.5%2C0";
115
        }
116
117 4
        return $link;
118
    }
119
120 1
    public function largeMapLink($latitude, $longitude)
121
    {
122
123 1
        if ($latitude) {
124 1
            $link = "https://www.openstreetmap.org/?mlat=" . $latitude . "&amp;mlon=" . $longitude . "#map=10/" . $latitude . "/" . $longitude;
125
        // <a href="https://www.openstreetmap.org/?mlat=55.8264&amp;mlon=13.3127#map=10/55.8264/13.3127">
126
        }
127 1
        return $link;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $link does not seem to be defined for all execution paths leading up to this point.
Loading history...
128
    }
129
130
131 3
    public function checkOwnIP()
132
    {
133 3
        $remoteAddr = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']:'127.0.0.1';
134 3
        return $remoteAddr;
135
    }
136
137
138
139
140
// echo "http://api.ipstack.com/130.235.136.64?access_key=d1efc4cc23a8b14dfad565ee6bde80b8";
141
142
    // /**
143
    // * Check active.
144
    // *
145
    // * @return variable , variable
146
    // */
147
    // public function active($computer, $human)
148
    // {
149
    //     if ($computer->value() > $human->value()) {
150
    //         $computer->setActive(true);
151
    //         return $computer->getName();
152
    //     } else {
153
    //         $human->setActive(true);
154
    //         return $human->getName();
155
    //     }
156
    // }
157
158
159
    // /**
160
    // * Check active in another way.
161
    // *
162
    // * @return variable , variable
163
    // */
164
    // public function getActive($computer, $human)
165
    // {
166
    //     if ($computer->getActive() == true) {
167
    //         return $computer->getName();
168
    //     }
169
    //     return $human->getName();
170
    // }
171
172
173
174
    // /**
175
    // * Check active in yet another way.
176
    // *
177
    // * @return string , string
178
    // */
179
    // public function getActive2($computer, $human)
180
    // {
181
    //     if ($human->getTotalScore() >= 100) {
182
    //         $human->setWinner();
183
    //         return "pig/gameOver";
184
    //     } elseif ($computer->getTotalScore() >= 100) {
185
    //         $computer->setWinner();
186
    //         return "pig/gameOver";
187
    //     } else {
188
    //         if ($computer->getActive() == true) {
189
    //             return "pig/playC";
190
    //         } elseif ($human->getActive() == true) {
191
    //             if ($human->getDie1() !==1 && $human->getDie2() !==1) {
192
    //                 return "pig/playH";
193
    //             } else {
194
    //                 return "pig/playC";
195
    //             }
196
    //         }
197
    //     }
198
    // }
199
200
   // /**
201
   //  * mainRoll.
202
   //  *
203
   //  * @return void , void
204
   //  */
205
   //  public function mainRoll($human, $computer)
206
   //  {
207
   //      if ($computer->getActive() == true) {
208
   //          $computer->setActive(false);
209
   //          $human->setActive(true);
210
   //      } else {
211
   //          // $human->setTotalScore($human->getTotalScore + $human->getTurnScore);
212
   //          $computer->setActive(true);
213
   //          $human->setActive(false);
214
   //      }
215
   //      $computer->setTotalScore($computer->getTotalScore() + $computer->getTurnScore());
216
   //      $human->setTotalScore($human->getTotalScore() + $human->getTurnScore());
217
   //      $computer->setTurnScore(0);
218
   //      $human->setTurnScore(0);
219
   //      $computer->setRolls(0);
220
   //      $human->setRolls(0);
221
   //      $human->setDie1(null);
222
   //      $human->setDie2(null);
223
   //      $computer->setDie1(null);
224
   //      $computer->setDie2(null);
225
   //  }
226
227
228
    // /**
229
    //  * mainRoll.
230
    //  *
231
    //  * @return void , void
232
    //  */
233
    // public function mainRoll2($human, $computer)
234
    // {
235
    //     if ($computer->getActive() !== true) {
236
    //         $human->roll2();
237
    //         $human->setRolls($human->getRolls() + 1);
238
    //         if ($human->getDie1() !==1 && $human->getDie2() !==1) {
239
    //             $human->setTurnScore($human->getTurnScore() + $human->getDiceSum());
240
    //         } else {
241
    //             $human->setTurnScore(0);
242
    //         }
243
    //     }
244
    // }
245
246
247
    // /**
248
    //  * mainRoll.
249
    //  *
250
    //  * @return void , void
251
    //  */
252
    // public function computerRoll($computer, $human)
253
    // {
254
    //     $totalHuman = $human->getTotalScore();
255
    //     if ($computer->getActive() == true) {
256
    //         do {
257
    //             $computer->roll2();
258
    //             $computer->setRolls($computer->getRolls() + 1);
259
    //             $computer->setTurnScore($computer->getTurnScore() + $computer->getDiceSum());
260
    //         } while ($computer->rollOrNot($totalHuman) > 1 && ($computer->getDie1() !==1 && $computer->getDie2() !==1));
261
    //         if ($computer->getDie1() ==1 || $computer->getDie2() ==1) {
262
    //             $computer->setTurnScore(0);
263
    //         }
264
    //     }
265
    // }
266
267
268
    // /**
269
    //  * isWinner
270
    //  * @return string , isWinner
271
    //  */
272
    //
273
    // public function isWinner2($human, $computer)
274
    // {
275
    //     if ($human->isWinner() == true) {
276
    //         return $human->getName();
277
    //     } else {
278
    //         return $computer->getName();
279
    //     }
280
    // }
281
}
282