1 <?php
2 /**
3 * XLRstats : Real Time Player Stats (http://www.xlrstats.com)
4 * (CC) BY-NC-SA 2005-2013, Mark Weirath, Özgür Uysal
5 *
6 * Licensed under the Creative Commons BY-NC-SA 3.0 License
7 * Redistributions of files must retain the above copyright notice.
8 *
9 * @link http://www.xlrstats.com
10 * @license Creative Commons BY-NC-SA 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/)
11 * @package app.Controller.Component
12 * @since XLRstats v3.0
13 * @version 0.1
14 */
15
16 App::uses('Component', 'Controller');
17 /**
18 * Class GeoIPComponent
19 */
20 class GeoIPComponent extends Component {
21
22 /**
23 * object reference
24 *
25 * @var null
26 */
27 public $gi = null;
28
29 //-------------------------------------------------------------------
30
31 /**
32 * @param Controller $controller
33 */
34 public function initialize(Controller $controller) {
35 $settings = array(
36 'res' => APP . WEBROOT_DIR . DS . 'GeoIP.dat', // absolute path
37 'src' => 'geoip.inc', // just the file name
38 );
39 App::import('Vendor', 'GeoIP', array('file' => $settings['src']));
40 $this->gi = geoip_open($settings['res'], GEOIP_STANDARD);
41 }
42
43 //-------------------------------------------------------------------
44
45 /**
46 * @param Controller $controller
47 */
48 public function shutdown(Controller $controller) {
49 geoip_close($this->gi); // cleanup
50 }
51
52 //-------------------------------------------------------------------
53
54 /**
55 * @param null $address
56 * @return bool|string
57 */
58 public function country_code($address = null) {
59 $countryCode = geoip_country_code_by_addr($this->gi, $address);
60 if ($countryCode == null) {
61 $countryCode = '-';
62 }
63 return $countryCode;
64 }
65
66 //-------------------------------------------------------------------
67
68 /**
69 * @param null $address
70 * @return bool|string
71 */
72 public function country_name($address = null) {
73 $countryName = geoip_country_name_by_addr($this->gi, $address);
74 if ($countryName == null) {
75 $countryName = 'Unknown';
76 }
77 return $countryName;
78 }
79 }
80