| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\CommonExtensions; |
||
| 4 | |||
| 5 | use SilverStripe\ORM\DataExtension; |
||
| 6 | use SilverStripe\Control\Controller; |
||
| 7 | use SilverStripe\Core\Injector\Injector; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Store IP as binary |
||
| 11 | * |
||
| 12 | * @link https://stackoverflow.com/questions/22636912/store-both-ipv4-and-ipv6-address-in-a-single-column |
||
| 13 | * @property \BinaryIPExtension $owner |
||
| 14 | * @property string $IP |
||
| 15 | */ |
||
| 16 | class BinaryIPExtension extends DataExtension |
||
| 17 | { |
||
| 18 | private static $db = [ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 19 | "IP" => "BinaryIP" |
||
| 20 | ]; |
||
| 21 | |||
| 22 | public function onBeforeWrite() |
||
| 23 | { |
||
| 24 | $controller = Controller::curr(); |
||
| 25 | // This is best used when IP is set on creation |
||
| 26 | if (!$this->owner->IP) { |
||
| 27 | $ip = $controller->getRequest()->getIP(); |
||
| 28 | $this->owner->IP = $ip; |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * This require geotools module |
||
| 34 | * |
||
| 35 | * @return \LeKoala\GeoTools\Models\Address|null |
||
| 36 | */ |
||
| 37 | public function getIpLocationDetails() |
||
| 38 | { |
||
| 39 | if (!$this->owner->IP) { |
||
| 40 | return null; |
||
| 41 | } |
||
| 42 | $geolocator = Injector::inst()->get(\LeKoala\Geo\Services\Geolocator::class); |
||
| 43 | return $geolocator->geolocate($this->owner->IP); |
||
| 44 | } |
||
| 45 | } |
||
| 46 |