-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCGeoIP.php
96 lines (85 loc) · 3.3 KB
/
CGeoIP.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* +----------------------------------------------------------------------+
* | PHP version 5 |
* +----------------------------------------------------------------------+
* | Copyright (C) 2010 Dinis Lage |
* +----------------------------------------------------------------------+
* | This library is free software; you can redistribute it and/or |
* | modify it under the terms of the GNU Lesser General Public |
* | License as published by the Free Software Foundation; either |
* | version 2.1 of the License, or (at your option) any later version. |
* | |
* | This library is distributed in the hope that it will be useful, |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* | Lesser General Public License for more details. |
* | |
* | You should have received a copy of the GNU Lesser General Public |
* | License along with this library; if not, write to the Free Software |
* | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
* | USA, or view it online at http://www.gnu.org/licenses/lgpl.txt. |
* +----------------------------------------------------------------------+
* | Authors: Dinis Lage <[email protected]> |
* +----------------------------------------------------------------------+
*
* @category Net
* @package GeoIP
* @author Dinis Lage <[email protected]>
* @license LGPL http://www.gnu.org/licenses/lgpl.txt
* $Id: GeoIP.php 296763 2010-03-25 00:53:44Z clockwerx $
*/
/**
* CGeoip class file.
*
* @author Dinis Lage <[email protected]>
* @link http://www.yiiframework.com/
* @version 0.1
*/
Yii::import('application.extensions.geoip.GeoIP');
class CGeoIP extends CApplicationComponent {
public $filename = '/usr/local/share/GeoIP/GeoLiteCity.dat';
public $mode;
protected static $flags = GeoIP::STANDARD;
protected static $geoip;
public function init() {
switch($this->mode) {
case 'MEMORY_CACHE':
self::$flags = GeoIP::MEMORY_CACHE;
break;
default:
self::$flags = GeoIP::STANDARD;
break;
}
self::$geoip = GeoIP::getInstance($this->filename, self::$flags);
// Run parent
parent::init();
}
public function lookupLocation($ip=null) {
$ip = $this->_getIP($ip);
return self::$geoip->lookupLocation($ip);
}
public function lookupCountryCode($ip=null) {
$ip = $this->_getIP($ip);
return self::$geoip->lookupCountryCode($ip);
}
public function lookupCountryName($ip=null) {
$ip = $this->_getIP($ip);
return self::$geoip->lookupCountryName($ip);
}
public function lookupOrg($ip=null) {
$ip = $this->_getIP($ip);
return self::$geoip->lookupOrg($ip);
}
public function lookupRegion($ip=null) {
$ip = $this->_getIP($ip);
return self::$geoip->lookupRegion($ip);
}
protected function _getIP($ip=null) {
if ($ip === null) {
$ip = Yii::app()->getRequest()->getUserHostAddress();
}
return $ip;
}
}
?>