root/g-map/files/lib/data/gmap/GmapApi.class.php @ 1494

Revision 1494, 2.7 kB (checked in by Torben Brodt, 21 months ago)

might fix iso charset

Line 
1<?php
2
3/**
4 * gets several positions and returns a clustered array
5 *
6 * @author      Torben Brodt
7 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
8 */
9class GmapApi extends DatabaseObject {
10        protected $apikey = null;
11        protected $cache_search = array();
12
13        /**
14         * creates new instance
15         */
16        public function __construct() {
17                $apikey = GMAP_API_KEY;
18                $apikey = explode("\n", StringUtil::unifyNewlines($apikey));
19                $apikey = $apikey[0];
20                $apikey = explode(":", $apikey);
21               
22                // this is the way, woltlab wants the users to enter their api key
23                if(count($apikey) == 2) {
24                        $this->apikey = $apikey[1];
25                }
26
27                // this is the way, almost all of the woltlab users really enter their api key
28                else if(!empty($apikey)) {
29                        $this->apikey = $apikey[0];
30                }
31        }
32
33        /**
34         * is active? api key existent?
35         *
36         * @return      boolean
37         */
38        public function isActive() {
39                return !empty($this->apikey);
40        }
41       
42        /**
43         * @var array<string>
44         */
45        public function getFields() {
46                if(defined('GMAP_CUSTOMINPUT') && ($const = GMAP_CUSTOMINPUT) && !empty($const)) {
47                        $tmp = explode(",", GMAP_CUSTOMINPUT);
48                        $cols = array();
49                        foreach($tmp as $field) {
50                                $col = User::getUserOptionID($field);
51                                if($col) {
52                                        $cols[] = $field;
53                                }
54                        }
55                } else {
56                        $cols = array('location');
57                }
58                return $cols;
59        }
60       
61        /**
62         * @var string
63         */
64        public function getColumn() {
65                $cols = array();
66                foreach($this->getFields() as $field) {
67                        $cols[] = User::getUserOptionID($field);
68                }
69
70                $col = array();
71                foreach($cols as $id) {
72                        $col[] = 'CONCAT(userOption'.$id.', IF(userOption'.$id.' = "", "", " "))';
73                }
74                $col = 'CONCAT('.implode(',', $col).')';
75               
76                return $col;
77        }
78       
79        /**
80         * ask google for geopositions
81         *
82         * @param       $location               string
83         * @return                              array<float>    keys are lat and lon
84         */
85        public function search($location) {
86                if(!$this->isActive()) {
87                        return;
88                }
89
90                if (CHARSET != 'UTF-8') {
91                        $location = StringUtil::convertEncoding(CHARSET, 'UTF-8', $location);
92                }
93                $lookupstring = urlencode(StringUtil::trim($location));
94               
95                if(isset($this->cache_search[$lookupstring])) {
96                        return $this->cache_search[$lookupstring];
97                }
98               
99                $url = "http://maps.google.com/maps/geo?q=".$lookupstring."&key=".$this->apikey."&output=csv";
100               
101                require_once(WCF_DIR.'lib/util/FileUtil.class.php');
102                $res = array();
103                try {
104                        $tmp = FileUtil::downloadFileFromHttp($url, 'gmap.search');
105                        foreach(file($tmp) as $row) {
106                                if (preg_match('/^200,[^,]+,([^,]+),([^,]+)$/', $row, $hits)) {
107                                        $res = array(
108                                                'lat' => trim($hits[1]),
109                                                'lon' => trim($hits[2])
110                                        );
111                                        break;
112                                }
113                        }
114                        @unlink($tmp);
115                } catch(Exception $e) {
116                        error_log($e->getMessage());
117                }
118
119                return $this->cache_search[$lookupstring] = $res;
120        }
121}
Note: See TracBrowser for help on using the browser.