| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * WiND - Wireless Nodes Database |
|---|
| 4 | * |
|---|
| 5 | * Copyright (C) 2011 K. Paliouras <squarious _at gmail [dot] com> |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation; version 2 dated June, 1991. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 19 | * |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | ini_set('max_execution_time', 300); |
|---|
| 23 | header('Content-Type: application/json'); |
|---|
| 24 | |
|---|
| 25 | require_once dirname(__FILE__) . '/../globals/classes/srtm.php'; |
|---|
| 26 | |
|---|
| 27 | if (!isset($_GET['lat']) || !isset($_GET['lng'])) |
|---|
| 28 | return; |
|---|
| 29 | |
|---|
| 30 | $base_url = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/'; |
|---|
| 31 | $srtm_directory = dirname(__FILE__) . '/../files/srtm/'; |
|---|
| 32 | $fname = srtm::get_filename((int)$_GET['lat'], (int)$_GET['lng']); |
|---|
| 33 | $zip_fname = $fname . '.zip'; |
|---|
| 34 | $regions = array( |
|---|
| 35 | 'Africa', |
|---|
| 36 | 'Australia', |
|---|
| 37 | 'Eurasia', |
|---|
| 38 | 'Islands', |
|---|
| 39 | 'North_America', |
|---|
| 40 | 'South_America' |
|---|
| 41 | ); |
|---|
| 42 | |
|---|
| 43 | /* |
|---|
| 44 | * Search file in all regions |
|---|
| 45 | */ |
|---|
| 46 | foreach($regions as $region) { |
|---|
| 47 | |
|---|
| 48 | if (!($srtm_data = @file_get_contents($base_url . '/' . $region .'/' . $zip_fname))) |
|---|
| 49 | continue; |
|---|
| 50 | |
|---|
| 51 | file_put_contents($srtm_directory . $zip_fname, $srtm_data); |
|---|
| 52 | if (!($zip = zip_open($srtm_directory . $zip_fname))){ |
|---|
| 53 | echo json_encode(array('result' => '500', 'error' => 'cannot unzip')); |
|---|
| 54 | return; |
|---|
| 55 | } |
|---|
| 56 | while($e = zip_read($zip)) { |
|---|
| 57 | if (zip_entry_name($e) !== $fname) |
|---|
| 58 | continue; |
|---|
| 59 | |
|---|
| 60 | zip_entry_open($zip, $e, "r"); |
|---|
| 61 | |
|---|
| 62 | $uncompressed = zip_entry_read($e, zip_entry_filesize($e)); |
|---|
| 63 | file_put_contents($srtm_directory . $fname, $uncompressed); |
|---|
| 64 | zip_close($zip); |
|---|
| 65 | unlink($srtm_directory . $zip_fname); |
|---|
| 66 | echo json_encode(array('result' => 'ok')); |
|---|
| 67 | return; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | echo json_encode(array('result' => '404')); |
|---|
| 72 | return; |
|---|