Overview

Namespaces

  • Sleepy
  • Module
    • Authentication
    • CSV
    • DB
    • FormBuilder
    • FSDB
    • IP2Country
    • Mailer
    • MobiDetect
    • Navigation
    • StaticCache
  • PHP

Classes

  • Sleepy\Debug
  • Sleepy\Hook
  • Sleepy\Router
  • Sleepy\SM
  • Sleepy\Template

Exceptions

  • Sleepy\RouteNotFound
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
 1: <?php
 2: namespace Module\IP2Country;
 3: 
 4: require_once(DIRBASE . '/modules/enabled/file-system-database/class.fsdb.php');
 5: 
 6: /**
 7:  * Cross-references countries with IP addresses using a flat-file-database.
 8:  *
 9:  * ### Usage
10:  *
11:  * <code>
12:  *   $i = new \Module\IP2Country\Converter();
13:  *   $countryCode = $i->getCountryCode($_SERVER['REMOTE_ADDR']);
14:  *
15:  *   if ($countryCode != false) {
16:  *     echo $countryCode;
17:  *   } else {
18:  *     echo $_SERVER['REMOTE_ADDR'] .
19:  *       "(" . ip2long($_SERVER['REMOTE_ADDR']) .
20:  *       ") Not found in " . $i->getTable($_SERVER['REMOTE_ADDR']) . ".";
21:  *   }
22:  * </code>
23:  *
24:  * ### Changelog
25:  *
26:  * ## Version 1.2
27:  * * Added namespacing
28:  *
29:  * ## Version 1.1
30:  * * Added the date section to documentation
31:  *
32:  * ### Dependencies
33:  * * class.fsdb.php
34:  *
35:  * @date August 13, 2014
36:  * @author Jaime A. Rodriguez <hi.i.am.jaime@gmail.com>
37:  * @version 1.2
38:  * @license  http://opensource.org/licenses/MIT
39:  */
40: class Converter {
41:     private $db;
42: 
43:     private $upperRange = array(
44:         50000000,    100000000,  150000000,  200000000,  250000000,  300000000,
45:         350000000,   400000000,  450000000,  500000000,  550000000,  600000000,
46:         650000000,   700000000,  750000000,  800000000,  850000000,  900000000,
47:         950000000,  1000000000, 1050000000, 1100000000, 1150000000, 1200000000,
48:         1250000000, 1300000000, 1350000000, 1400000000, 1450000000, 1500000000,
49:         1550000000, 1600000000, 1650000000, 1700000000, 1750000000, 1800000000,
50:         1850000000, 1900000000, 1950000000, 2000000000, 2050000000, 2100000000,
51:         2150000000, 2200000000, 2250000000, 2300000000, 2350000000, 2400000000,
52:         2450000000, 2500000000, 2550000000, 2600000000, 2650000000, 2700000000,
53:         2750000000, 2800000000, 2850000000, 2900000000, 2950000000, 3000000000,
54:         3050000000, 3100000000, 3150000000, 3200000000, 3250000000, 3300000000,
55:         3350000000, 3400000000, 3450000000, 3500000000, 3550000000, 3600000000,
56:         3650000000, 3700000000, 3750000000, 3800000000, 3850000000, 3900000000,
57:         3950000000, 4000000000
58:     );
59: 
60:     public function __construct() {
61:         $this->db = new \FSDB\Connection('./ipData/');
62:     }
63: 
64:     public function getCountryCode($ip) {
65:         $dec = ip2long($ip);
66:         $data = $this->db->select($this->getTable($ip), "*");
67:         foreach ($data as $row) {
68:             if ($row->startIP <= $dec && $row->endIP >= $dec) {
69:                 return $row->countryCode;
70:             }
71:         }
72: 
73:         return false;
74:     }
75: 
76:     public function getTable($ip) {
77:         $dec = ip2long($ip);
78: 
79:         foreach ($this->upperRange as $limit) {
80:             if ($dec >= $limit - 50000000 && $dec <= $limit) {
81:                 return (string) $limit;
82:             }
83:         }
84: 
85:         return false;
86:     }
87: }
sleepyMUSTACHE v.0.8 API documentation generated by ApiGen