Move busui to seperate repository
[bus.git] / MaxMindWebServices.class.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
 
/**
 * The Abstraction Layer that all MaxMind Web Services extend from
 *
 * @access  private
 * @author      Nathan White < contact at nathanwhite dot us >
 *
 */
 
class MaxMindWebServices {
 
 
        /**
     * The licence Key for of a Maxmind web services account
     *
     * @var     string
     * @access  private
     */
        var $licenceKey = "";
        
        
        /**
         * An array that holds all returned values from a Maxmind request
     *
     * @var             array
     * @access  private
         */
        var $data = array();
 
        /**
         * Set the Licence Key
     *
     * @var             string
     * @access  public
         */
        function setLicenceKey($key){
                $this->licenceKey = $key;
        }
 
        /**
         * Test to see if the Service produced an Error
     *
     * @return  bool
     * @access  public
         */
        function isError(){
                $error = $this->getError();
                if( isset($error) ) return true;
                else return false;
        }
 
        /**
         * Get all Results in a single array for fast processing
     *
     * @return  array
     * @access  public
         */
        function getResultArray(){
                return $this->data;
        }
        
        /**
         * Returns the City and State from a metro code
     *
     * @param   string
     * @return  string
     * @access  public
         */
        function lookupMetroCode($code){
                if( !isset($this->_metroCodes) ){
                        $this->_metroCodes = parse_ini_file(dirname( __FILE__ ).'/ini/metroCodes.ini');
                }
                return $this->_metroCodes[$code];
        }
        
        /**
         * Returns the Country Name from the code
     *
     * @param   string
     * @return  string
     * @access  public
         */
        function lookupCountryCode($code){
                if( !isset($this->_countryCodes) ){
                        $this->_countryCodes = parse_ini_file(dirname( __FILE__ ).'/ini/countryCodes.ini');
                }
                return $this->_countryCodes["'".$code."'"];
        }
        
        /**
         * Returns the SubCountry Name from the code ( States, Provinces )
     *
     * @param   string
     * @param   string
     * @return  string
     * @access  public
         */     
        function lookupSubCountryCode($code, $countryCode){
                if( !isset($this->_subCountryCodes) ){
                        $this->_subCountryCodes = parse_ini_file(dirname( __FILE__ ).'/ini/subCountryCodes.ini', true);
                }
                if( is_array($this->_subCountryCodes["'".$countryCode."'"]) ){
                        return $this->_subCountryCodes["'".$countryCode."'"]["'".$code."'"];
                }
        }
        /**
         * Generic Web Service Request for MaxMind
     *
     * @access  private
         */
        function _queryMaxMind($url){
                
                $ch = curl_init();    // initialize curl handle
                curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
        curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_TIMEOUT, 4); // times out after 4s
 
                return curl_exec($ch);
                
        }
 
 
        /**
         * Function to handle parsing the csv string returned from MaxMind
     *
     * This function was found in the comments section on:
     * http://www.php.net/manual/en/function.fgetcsv.php
     *
     * @var             string  csv line to parse
     * @var             string  delimiter to use for spliting
     * @var             bool    remove quotes around values
     * @return  array   the parts of the csv line
     * @access  public
     * @author  php@dogpoop.cjb.net
         */
 
        function csv_split($line,$delim=',',$removeQuotes=true) {
 
                $fields = array();
                $fldCount = 0;
                $inQuotes = false;
                for ($i = 0; $i < strlen($line); $i++) {
                        if (!isset($fields[$fldCount])) $fields[$fldCount] = "";
                        $tmp = substr($line,$i,strlen($delim));
                        
                        if ($tmp === $delim && !$inQuotes) {
                                $fldCount++;
                                $i += strlen($delim)-1;
                        }
                        else if ($fields[$fldCount] == "" && $line[$i] == '"' && !$inQuotes) {
                                if (!$removeQuotes) $fields[$fldCount] .= $line[$i];
                                $inQuotes = true;
                        }
                        else if ($line[$i] == '"') {
                                if ($line[$i+1] == '"') {
                                        $i++;
                                        $fields[$fldCount] .= $line[$i];
                                }
                                else {
                                        if (!$removeQuotes) $fields[$fldCount] .= $line[$i];
                                        $inQuotes = false;
                                }
                        }
                        else {
                                $fields[$fldCount] .= $line[$i];
                        }
                }
                return $fields;
        }
 
 
}
 
?>