root/trunk/wifidog-auth/wifidog/classes/Geocoders/GeocoderGoogleGlobal.php @ 1410

Revision 1410, 7.2 KB (checked in by networkfusion, 4 years ago)

* Forgot to add the new geocoder classes to SVN!

Line 
1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5// +-------------------------------------------------------------------+
6// | WiFiDog Authentication Server                                     |
7// | =============================                                     |
8// |                                                                   |
9// | The WiFiDog Authentication Server is part of the WiFiDog captive  |
10// | portal suite.                                                     |
11// +-------------------------------------------------------------------+
12// | PHP version 5 required.                                           |
13// +-------------------------------------------------------------------+
14// | Homepage:     http://www.wifidog.org/                             |
15// | Source Forge: http://sourceforge.net/projects/wifidog/            |
16// +-------------------------------------------------------------------+
17// | This program is free software; you can redistribute it and/or     |
18// | modify it under the terms of the GNU General Public License as    |
19// | published by the Free Software Foundation; either version 2 of    |
20// | the License, or (at your option) any later version.               |
21// |                                                                   |
22// | This program is distributed in the hope that it will be useful,   |
23// | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
24// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
25// | GNU General Public License for more details.                      |
26// |                                                                   |
27// | You should have received a copy of the GNU General Public License |
28// | along with this program; if not, contact:                         |
29// |                                                                   |
30// | Free Software Foundation           Voice:  +1-617-542-5942        |
31// | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
32// | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
33// |                                                                   |
34// +-------------------------------------------------------------------+
35
36/**
37 * @package    WiFiDogAuthServer
38 * @subpackage Geocoders
39 * @author     Robin Jones
40 * @copyright  2009 Robin Jones, NetworkFusion
41 * @version    Subversion $Id: $
42 * @link       http://www.wifidog.org/
43 */
44
45/**
46 * Load required class
47 */
48require_once('classes/GenericObject.php');
49require_once('classes/Server.php');
50
51/**
52 * @package    WiFiDogAuthServer
53 * @subpackage Geocoders
54 * @author     Robin Jones
55 * @copyright  2009 Robin Jones, NetworkFusion
56 */
57class GeocoderGoogleGlobal extends AbstractGeocoder
58{
59
60    private $cached_latitude;
61    private $cached_longitude;
62
63    public function __construct()
64    {
65        //$this->setCountry("Earth");
66        $this->setEndpointUrl("http://maps.google.com/maps/geo?");
67        //API Key will be collected from the database in future
68        if(!$vhost=VirtualHost::getCurrentVirtualHost()) {
69            $this->setAPIKey("0");
70        }
71            else
72            $this->setAPIKey($vhost->getGoogleAPIKey());
73    }
74
75
76    /** Validate address, making sure we don't send an HTTP for nothing
77     * @return boolean
78     */
79    public function validateAddress()
80    {
81        // Make sure a city or a postal code has been entered
82        //if(($this->getCivicNumber() == "" || $this->getStreetName() == "" || $this->getCity() == "" || $this->getProvince() == "") && !$this->getPostalCode() == "")
83            //return false;
84        return true;
85    }
86
87    /** Constructs the address string that will be sent to the endpoint URL
88     * @return string address string
89     */
90    private function buildAddress()
91    {
92
93        // Build correctly formated string depending on given parameters
94        $address = Null;
95
96
97        if ($this->getStreetName() != "")
98            $address .= $this->getStreetName() . ', ';
99
100        if ($this->getCity() != "")
101            $address .= $this->getCity() . ', ';
102
103        if ($this->getProvince() != "")
104            $address .= $this->getProvince() . ', ';
105
106            $address .= $this->getPostalCode();
107        return $address;
108       
109    }
110
111    /** Constructs the HTTP query string that will be sent to the endpoint URL
112     * @return string HTTP GET query string
113     */
114    private function buildQuery()
115    {
116
117        // Build HTTP GET query string containing all parameters
118        $http_params = array ("key" => $this->getAPIKey(), "q" => $this->buildAddress(), "output" => "xml", "oe"=> "utf8", "sensor" => "false");
119        return $this->getEndpointUrl() . http_build_query($http_params);
120       
121    }
122
123
124    /** Runs the HTTP GET query
125     * @return boolean
126     */
127    private function executeQuery()
128    {
129        // Don't send multiple queries when the input has not changed
130        if ($this->shouldExecuteQuery() == true)
131        {
132
133            // Load the XML document
134            if (($xml = simplexml_load_file($this->buildQuery())) !== false)
135            {
136
137
138
139                //print $this->buildQuery() . '<br>';
140                //print_r($xml->xpath["Points"]);
141   
142                $status = $xml->Response->Status->code;
143                if (strcmp($status, "200") == 0) 
144                {
145                    // Successful geocode
146                    $coordinates = $xml->Response->Placemark->Point->coordinates;
147                   
148                    $coordinatesSplit = split(",", $coordinates);
149                    // Format: Longitude, Latitude, Altitude
150                    $this->cached_latitude = $coordinatesSplit[1];
151                    $this->cached_longitude = $coordinatesSplit[0];
152   
153                    // Prevent from sending multiple queries.
154                    $this->keepResponse();
155                }
156
157            }
158            else
159                return false;
160        }
161        return true;
162    }
163
164    /** Get the latitude for enterred infos
165     * @return string latitude ( decimal format 6-digits precision )
166     */
167    public function getLatitude()
168    {
169        if ($this->validateAddress())
170            if ($this->executeQuery() == true)
171                return $this->cached_latitude;
172            else
173                return null;
174        else
175            return null;
176    }
177
178    /** Get the longitude for enterred infos
179     * @return string longitude ( decimal format 6-digits precision )
180     */
181    public function getLongitude()
182    {
183        if ($this->validateAddress())
184            if ($this->executeQuery() == true)
185                return $this->cached_longitude;
186            else
187                return null;
188        else
189            return null;
190    }
191
192    /** Get the altitude for enterred infos
193     * @return string algitude
194     */
195    public function getAltitude()
196    {
197        // Not supported by google
198        return null;
199    }
200
201    /** Get a GIS Point instance
202     * @return GisPoint
203     */
204    public function getGisLocation()
205    {
206        $lat = $this->getLatitude();
207        $long = $this->getLongitude();
208
209        if($lat !== null && $long !== null)
210            return new GisPoint($lat, $long, 0);
211        return null;
212    }
213
214}
215
216/*
217 * Local variables:
218 * tab-width: 4
219 * c-basic-offset: 4
220 * c-hanging-comment-ender-p: nil
221 * End:
222 */
223
224
Note: See TracBrowser for help on using the browser.