|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bashar\GeoLocationModel; |
|
4
|
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
|
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A sample controller to show how a controller class can be implemented. |
|
10
|
|
|
* The controller will be injected with $di if implementing the interface |
|
11
|
|
|
* ContainerInjectableInterface, like this sample class does. |
|
12
|
|
|
* The controller is mounted on a particular route and can then handle all |
|
13
|
|
|
* requests for that mount point. |
|
14
|
|
|
* |
|
15
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
16
|
|
|
*/ |
|
17
|
|
|
class GeoLocationByIpModel implements ContainerInjectableInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use ContainerInjectableTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $message = "127.0.0.1"; |
|
25
|
|
|
private $apiResult; |
|
26
|
|
|
private $geoApi; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* returns the api from the config file "ipstackcfg" |
|
31
|
|
|
* |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function getDetails() : string |
|
34
|
|
|
{ |
|
35
|
4 |
|
return $this->message; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Sets the api from the config file |
|
41
|
|
|
* into the controller. |
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
13 |
|
public function setMessage($message) : void |
|
45
|
|
|
{ |
|
46
|
13 |
|
$this->message = $message; |
|
47
|
13 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Sets the api from the config file |
|
52
|
|
|
* into the controller. |
|
53
|
|
|
* |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function setGeoApi($enteredIp) : void |
|
56
|
|
|
{ |
|
57
|
2 |
|
$this->geoApi = "http://api.ipstack.com/". $enteredIp . "?access_key=" . $this->message . |
|
58
|
2 |
|
'&hostname=1&security=1'; |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* returns the api from the config file "ipstackcfg" |
|
64
|
|
|
* |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getGeoApi() |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->geoApi; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Gets the Geo Location info in |
|
74
|
|
|
* Gets the Geo Location info in |
|
75
|
|
|
* an array |
|
76
|
|
|
* |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function doCurl($geoApi) : string |
|
79
|
|
|
{ |
|
80
|
|
|
// create & initialize a curl session |
|
81
|
2 |
|
$curl = curl_init(); |
|
82
|
|
|
|
|
83
|
|
|
// set our url with curl_setopt() |
|
84
|
2 |
|
curl_setopt($curl, CURLOPT_URL, $geoApi); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
// return the transfer as a string, also with setopt() |
|
87
|
2 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
|
88
|
|
|
|
|
89
|
|
|
// curl_exec() executes the started curl session |
|
90
|
|
|
// $output contains the output string |
|
91
|
2 |
|
$output = curl_exec($curl); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// close curl resource to free up system resources |
|
94
|
|
|
// (deletes the variable made by curl_init) |
|
95
|
2 |
|
curl_close($curl); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
2 |
|
return $output; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* a json format |
|
103
|
|
|
* |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getGeoLocationJson() |
|
106
|
|
|
{ |
|
107
|
1 |
|
$encoded = json_encode($this->apiResult, true); |
|
|
|
|
|
|
108
|
1 |
|
$pattern1 = "/\"{/i"; |
|
109
|
1 |
|
$first = preg_replace($pattern1, "{", $encoded); |
|
110
|
1 |
|
$pattern2 = "/[\]]$/i"; |
|
111
|
1 |
|
$second = preg_replace($pattern2, "<br>]", $first); |
|
112
|
1 |
|
$pattern3 = "/,/i"; |
|
113
|
1 |
|
$third = preg_replace($pattern3, ",<br>********", $second); |
|
114
|
1 |
|
$pattern4 = "/{/i"; |
|
115
|
1 |
|
$fourth = preg_replace($pattern4, "{<br>********", $third); |
|
116
|
1 |
|
$pattern5 = "/}/i"; |
|
117
|
1 |
|
$fifth = preg_replace($pattern5, "<br>****}", $fourth); |
|
118
|
1 |
|
$pattern6 = "/[\\\\]/i"; |
|
119
|
1 |
|
$sixth = preg_replace($pattern6, "", $fifth); |
|
120
|
1 |
|
$string = str_replace("*", " ", $sixth); |
|
121
|
|
|
|
|
122
|
1 |
|
return $string; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Gets the Geo Location info in |
|
128
|
|
|
* an array |
|
129
|
|
|
* |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function getGeoLocationNormal($enteredIp) |
|
132
|
|
|
{ |
|
133
|
1 |
|
$this->setGeoApi($enteredIp); |
|
134
|
1 |
|
$output = $this->doCurl($this->geoApi); |
|
135
|
|
|
|
|
136
|
1 |
|
$apiResult = json_decode($output, true); |
|
137
|
1 |
|
$this->apiResult = $apiResult; |
|
138
|
1 |
|
return $this->apiResult; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|