| 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 | */ |
|---|
| 9 | class GmapApi extends DatabaseObject { |
|---|
| 10 | protected $cache_search = array(); |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * is active? api key existent? |
|---|
| 14 | * @deprecated no api key needed any longer |
|---|
| 15 | * @return boolean |
|---|
| 16 | */ |
|---|
| 17 | public function isActive() { |
|---|
| 18 | return true; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * @var array<string> |
|---|
| 23 | */ |
|---|
| 24 | public function getFields() { |
|---|
| 25 | if(defined('GMAP_CUSTOMINPUT') && ($const = GMAP_CUSTOMINPUT) && !empty($const)) { |
|---|
| 26 | $tmp = explode(",", GMAP_CUSTOMINPUT); |
|---|
| 27 | $cols = array(); |
|---|
| 28 | foreach($tmp as $field) { |
|---|
| 29 | $col = User::getUserOptionID($field); |
|---|
| 30 | if($col) { |
|---|
| 31 | $cols[] = $field; |
|---|
| 32 | } |
|---|
| 33 | } |
|---|
| 34 | } else { |
|---|
| 35 | $cols = array('location'); |
|---|
| 36 | } |
|---|
| 37 | return $cols; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * @var string |
|---|
| 42 | */ |
|---|
| 43 | public function getColumn() { |
|---|
| 44 | $cols = array(); |
|---|
| 45 | foreach($this->getFields() as $field) { |
|---|
| 46 | $cols[] = User::getUserOptionID($field); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | $col = array(); |
|---|
| 50 | foreach($cols as $id) { |
|---|
| 51 | $col[] = 'CONCAT(userOption'.$id.', IF(userOption'.$id.' = "", "", " "))'; |
|---|
| 52 | } |
|---|
| 53 | $col = 'CONCAT('.implode(',', $col).')'; |
|---|
| 54 | |
|---|
| 55 | return $col; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * ask google for geopositions |
|---|
| 60 | * |
|---|
| 61 | * @param $location string |
|---|
| 62 | * @return array<float> keys are lat and lon |
|---|
| 63 | */ |
|---|
| 64 | public function search($location) { |
|---|
| 65 | if (CHARSET != 'UTF-8') { |
|---|
| 66 | $location = StringUtil::convertEncoding(CHARSET, 'UTF-8', $location); |
|---|
| 67 | } |
|---|
| 68 | $lookupstring = urlencode(StringUtil::trim($location)); |
|---|
| 69 | |
|---|
| 70 | if(isset($this->cache_search[$lookupstring])) { |
|---|
| 71 | return $this->cache_search[$lookupstring]; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | $url = "http://maps.google.com/maps/geo?q=".$lookupstring."&output=csv"; |
|---|
| 75 | |
|---|
| 76 | require_once(WCF_DIR.'lib/util/FileUtil.class.php'); |
|---|
| 77 | $res = array(); |
|---|
| 78 | try { |
|---|
| 79 | $tmp = FileUtil::downloadFileFromHttp($url, 'gmap.search'); |
|---|
| 80 | foreach(file($tmp) as $row) { |
|---|
| 81 | if (preg_match('/^200,[^,]+,([^,]+),([^,]+)$/', $row, $hits)) { |
|---|
| 82 | $res = array( |
|---|
| 83 | 'lat' => trim($hits[1]), |
|---|
| 84 | 'lon' => trim($hits[2]) |
|---|
| 85 | ); |
|---|
| 86 | break; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | @unlink($tmp); |
|---|
| 90 | } catch(Exception $e) { |
|---|
| 91 | error_log($e->getMessage()); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | return $this->cache_search[$lookupstring] = $res; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|