Issues (9)

src/Vader/VaderHandler.php (6 issues)

Labels
Severity
1
<?php
2
3
namespace Malm18\Vader;
4
5
class VaderHandler
6
{
7
8
9
    // public function parenting()
10
    // {
11
    //     return "I am your father";
12
    // }
13
14
15
16
17 6
    public function checkCoordinates($theIP)
18
    {
19 6
        if (filter_var($theIP, FILTER_VALIDATE_IP)) {
20 2
            $coordinates = [];
21 2
            $url = 'http://api.ipstack.com/';
22 2
            $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
23 2
            $apiKey = $keys["ipstackKey"];
24 2
            $requestUrl = $url . $theIP . '?access_key=' . $apiKey;
25 2
            $curl = curl_init($requestUrl);
26 2
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
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

26
            curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_RETURNTRANSFER, true);
Loading history...
27
        // curl_setopt($curl, CURLOPT_HTTPHEADER, [
28
        //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
29
        //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
30
        //   'Content-Type: application/json'
31
        // ]);
32 2
            $response = curl_exec($curl);
0 ignored issues
show
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

32
            $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
33 2
            $response2 = json_decode($response, true);
34 2
            curl_close($curl);
0 ignored issues
show
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

34
            curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
35
36 2
            $coordinates['latitude'] = strval($response2['latitude']);
37 2
            $coordinates['longitude'] = strval($response2['longitude']);
38
            // echo $response . PHP_EOL;
39 2
            return $coordinates;
40
        } else {
41 4
            $coordinates = [];
42
            // $response = array("type" => "not valid ip", "ip" => "", "latitude"=> "", "longitude"=> "",
43
            // "city" => "", "country_name" => "", "region_name" => "", "continent_name" => "", "location['country_code']" => "");
44
            // // $response2 = json_decode($response, true);
45
            // return $response;
46 4
            $url1 = 'https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=';
47
48
            // $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
49
            // $this->ipstackKey = $keys["ipstackKey"];
50
            // $apiKey = $this->ipstackKey;
51 4
            $requestUrl = $url1 . $theIP . '&format=json&limit=1&[email protected]';
52
            // $requestUrl = 'https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1&[email protected]';
53 4
            $curl = curl_init($requestUrl);
54
            // if ($curl) {
55 4
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
56
        // curl_setopt($curl, CURLOPT_HTTPHEADER, [
57
        //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
58
        //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
59
        //   'Content-Type: application/json'
60
        // ]);
61 4
            $response = curl_exec($curl);
62
63 4
            if (strlen($response) > 10) {
64 2
                $response2 = json_decode($response, true);
65 2
                curl_close($curl);
66 2
                $coordinates['latitude'] = $response2[0]['lat'];
67 2
                $coordinates['longitude'] = $response2[0]['lon'];
68
            }
69
            // echo $response . PHP_EOL;
70 4
            return $coordinates;
71
        }
72
        // }
73
    }
74
75
76
77
78
    // public function checkWeather($latitude, $longitude)
79
    // {
80
    //
81
    //     $url1 = 'https://api.darksky.net/forecast/';
82
    //
83
    //     $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
84
    //     $apiKey = $keys["darkskyKey"];
85
    //     $endStuff = '?exclude=minutely,hourly,currently,alerts,flags&extend=daily&lang=sv&units=si';
86
    //     $requestUrl = $url1 . $apiKey . "/" . $latitude . "," . $longitude . $endStuff;
87
    //     // $requestUrl = 'https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1&[email protected]';
88
    //     $curl = curl_init($requestUrl);
89
    //     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
90
    // // curl_setopt($curl, CURLOPT_HTTPHEADER, [
91
    // //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
92
    // //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
93
    // //   'Content-Type: application/json'
94
    // // ]);
95
    //     $response = curl_exec($curl);
96
    //     $response2 = json_decode($response, true);
97
    //     curl_close($curl);
98
    //     // $coordinates['latitude'] = $response2[0]['lat'];
99
    //     // $coordinates['longitude'] = $response2[0]['lon'];
100
    //     // echo $response . PHP_EOL;
101
    //     return $response2;
102
    //
103
    //     // $coordinates = "Latitud: " . $latitude . ", longitud: " . $longitude;
104
    //     // return $coordinates;
105
    // }
106
107
108
109 4
    public function checkWeather($latitude, $longitude, $pastOrFuture)
110
    {
111
112 4
        $url1 = 'https://api.darksky.net/forecast/';
113
114 4
        $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
115 4
        $apiKey = $keys["darkskyKey"];
116 4
        $endStuff = '?exclude=minutely,hourly,currently,alerts,flags&extend=daily&lang=sv&units=si';
117
118 4
        if ($pastOrFuture == "future") {
119 1
            $requestUrl = $url1 . $apiKey . "/" . $latitude . "," . $longitude . $endStuff;
120
            // $requestUrl = 'https://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=bakery+in+berlin+wedding&format=json&limit=1&[email protected]';
121 1
            $curl = curl_init($requestUrl);
122
            // if ($curl) {
123 1
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
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

123
            curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_RETURNTRANSFER, true);
Loading history...
124
        // curl_setopt($curl, CURLOPT_HTTPHEADER, [
125
        //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
126
        //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
127
        //   'Content-Type: application/json'
128
        // ]);
129 1
            $response = curl_exec($curl);
0 ignored issues
show
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

129
            $response = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
130 1
            $response2 = json_decode($response, true);
131 1
            curl_close($curl);
0 ignored issues
show
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

131
            curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
132
            // $coordinates['latitude'] = $response2[0]['lat'];
133
            // $coordinates['longitude'] = $response2[0]['lon'];
134
            // echo $response . PHP_EOL;
135 1
            return $response2['daily']['data'];
136
        // }
137
        } else {
138
        // array of curl handles
139 3
            $multiCurl = array();
140
            // data to be returned
141 3
            $response = array();
142
            // multi handle
143 3
            $mhbutlonger = curl_multi_init();
144
145
146
147
148
        // for ($i=0; $i < 30; $i++) {
149
        //     $unixTime = time() - ($i * 24 * 60 * 60);
150
        //     $multiRequests[] = $url1 . $apiKey . "/" . $latitude . "," . $longitude . ','.$unixTime . $endStuff;
151
        // }
152
153
154
155
156
157 3
            for ($i = 0; $i < 30; $i++) {
158 3
                $unixTime = time() - ($i * 24 * 60 * 60);
159 3
                $requestUrl = $url1 . $apiKey . "/" . $latitude . "," . $longitude . ',' . $unixTime . $endStuff;
160 3
                $multiCurl[$i] = curl_init();
161 3
                curl_setopt($multiCurl[$i], CURLOPT_URL, $requestUrl);
162 3
                curl_setopt($multiCurl[$i], CURLOPT_HEADER, 0);
163 3
                curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER, 1);
164 3
                curl_multi_add_handle($mhbutlonger, $multiCurl[$i]);
165
            }
166 3
            $index = null;
167
            do {
168 3
                curl_multi_exec($mhbutlonger, $index);
169 3
            } while ($index > 0);
170
        // get content and remove handles
171 3
            foreach ($multiCurl as $k => $ch) {
172 3
                $response[$k] = curl_multi_getcontent($ch);
173 3
                curl_multi_remove_handle($mhbutlonger, $ch);
174
            }
175
            // close
176 3
            curl_multi_close($mhbutlonger);
177
178
            // $response2 = json_decode($response, true);
179
180
            // $daily = [];
181
            // $anArray = [];
182
            // $data = [];
183 3
            $data2 = [];
184 3
            $i = 0;
185 3
            foreach ($response as $item) {
186 3
                $data = json_decode($item, true);
187 3
                $data2[$i] = $data['daily']['data'][0];
188
                // $data[$i] = $item;
189 3
                $i = $i + 1;
190
            }
191
            // $daily['data'] = $data;
192
            // $anArray['daily'] = $daily;
193
194
            // return $data[0]['daily']['data'];
195
            // $response2 = json_decode($data, true);
196 3
            return $data2;
197
        }
198
    }
199
200
201
202
203
204
//     public function checkPastWeather($latitude, $longitude)
205
// {
206
//     $url1 = 'https://api.darksky.net/forecast/';
207
//
208
//     $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
209
//     $apiKey = $keys["darkskyKey"];
210
//     $endStuff = '?exclude=minutely,hourly,currently,alerts,flags&extend=daily&lang=sv&units=si';
211
//
212
//     $multiRequests = [];
213
//     #future weather
214
//     if ($this->time === "future") {
215
//         for ($i=0; $i < 7; $i++) {
216
//             $unixTime = time() + ($i * 24 * 60 * 60);
217
//             $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags';
218
//         }
219
//     }
220
//     #previous weather
221
//     if ($this->time === "past") {
222
//         for ($i=0; $i < 30; $i++) {
223
//             $unixTime = time() - ($i * 24 * 60 * 60);
224
//             $multiRequests[] = 'https://api.darksky.net/forecast/'.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.'?exclude=minutely,hourly,daily,flags';
225
//         }
226
//     }
227
//     $weather = $this->requester->multiRequest($multiRequests);
228
//     foreach ($weather as $key => $value) {
229
//         $weather[$key] = json_decode(stripslashes($value), true);
230
//     }
231
//     return $weather;
232
// }
233
234
235
236
237
238 4
    public function checkWeather2($weather)
239
    {
240 4
        $weather2 = [];
241 4
        $locale = 'sv_SE.utf8';
242 4
        setlocale(LC_TIME, $locale);
243 4
        $i = 0;
244 4
        foreach ($weather as $day) {
245
            // array_push($weather2, $day['time']);
246 4
            $unixTimestamp = $day['time'];
247 4
            $datetime = date('Y-m-d l', $unixTimestamp);
248 4
            $datetime2 = strftime('%A %d %B', strtotime($datetime));
249
250
            // $unixTimestamp = $day['time'];
251
            // $datetime = date('Y-m-d l', $unixTimestamp);
252
            // $datetime2 = strftime('%A %d %B', strtotime($datetime));
253
254
            // $datetime2 = $datetime->format('d/m');
255
            // $weather2[$i] = (['day'] => [$day]);
256 4
            $weather2[$i]['time'] = $datetime2;
257 4
            $weather2[$i]['summary'] = $day['summary'];
258 4
            $weather2[$i]['temperatureMin'] = round($day['temperatureMin']);
259 4
            $weather2[$i]['temperatureMax'] = round($day['temperatureMax']);
260 4
            $weather2[$i]['precipProbability'] = 100 * ($day['precipProbability']);
261
            // $weather2[$i]['precipType'] = $day['precipType'];
262 4
            $weather2[$i]['windSpeed'] = round($day['windSpeed']);
263 4
            $weather2[$i]['windBearing'] = $day['windBearing'];
264
            // array_push($weather2, $datetime2);
265 4
            $i = $i + 1;
266
        }
267 4
        return $weather2;
268
    }
269
270
//     foreach($inputs['test']['order'] as $test){
271
//         echo $test;
272
//
273
// }
274
275
    // echo $yummy->toppings[2]->id
276
277
//     foreach($arr as $key => &$val){
278
//     $val['color'] = 'red';
279
// }
280
281
282
    // $unixTimestamp = $_POST['timestamp'];
283
    // $datetime = new DateTime("@$unixTimestamp");
284
    // // Display GMT datetime
285
    // echo $datetime->format('d-m-Y H:i:s');
286
287
288
    // public function minLong($longitude)
289
    // {
290
    //
291
    //     $minLong = floatval($longitude)-0.6427;
292
    //     return $minLong;
293
    // }
294
    //
295
    // public function maxLong($longitude)
296
    // {
297
    //
298
    //     $maxLong = floatval($longitude)+0.6427;
299
    //     return $maxLong;
300
    // }
301
    //
302
    // public function minLat($latitude)
303
    // {
304
    //
305
    //     $minLat = (floatval($latitude)) - 0.260;
306
    //     return $minLat;
307
    // }
308
    //
309
    // public function maxLat($latitude)
310
    // {
311
    //
312
    //     $maxLat = (floatval($latitude)) + 0.260;
313
    //     return $maxLat;
314
    // }
315
316
317
    // public function mapLink($latitude, $longitude, $minLat, $maxLat, $minLong, $maxLong)
318
    // {
319
    //                 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
320
    //     // $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"
321
    //
322
    //     $link = "https://www.openstreetmap.org/export/embed.html?bbox=" . $minLong . "%2C" . $minLat . "%2C" . $maxLong . "%2C" . $maxLat . "&amp;layer=mapnik&amp;marker=" . $latitude . "%2C" . $longitude;
323
    //
324
    //     return $link;
325
    // }
326
327
    // public function mapLink($latitude, $longitude, $minLat, $maxLat, $minLong, $maxLong)
328
    // {
329
    //     if ($latitude) {
330
    //         $link = "https://www.openstreetmap.org/export/embed.html?bbox=" . $minLong . "%2C" . $minLat . "%2C" . $maxLong . "%2C" . $maxLat . "&amp;layer=mapnik&amp;marker=" . $latitude . "%2C" . $longitude;
331
    //     } else {
332
    //         $link = "https://www.openstreetmap.org/export/embed.html?bbox=-0.64%2C85%2C0.64%2C90&amp;layer=mapnik&amp;marker=87.5%2C0";
333
    //     }
334
    //
335
    //     return $link;
336
    // }
337
    //
338
    // public function largeMapLink($latitude, $longitude)
339
    // {
340
    //     if ($latitude) {
341
    //         $link = "https://www.openstreetmap.org/?mlat=" . $latitude . "&amp;mlon=" . $longitude . "#map=10/" . $latitude . "/" . $longitude;
342
    //     // <a href="https://www.openstreetmap.org/?mlat=55.8264&amp;mlon=13.3127#map=10/55.8264/13.3127">
343
    //     } else {
344
    //         $link = "https://www.openstreetmap.org";
345
    //     }
346
    //     return $link;
347
    // }
348
349
350
    // public function checkOwnIP()
351
    // {
352
    //     $remoteAddr = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']:'127.0.0.1';
353
    //     return $remoteAddr;
354
    // }
355
}
356