|
| 1 | +<?php |
| 2 | +/*********************************************** |
| 3 | +* File : admin_sync.php |
| 4 | +* Project : piwigo-openstreetmap |
| 5 | +* Descr : Create tags from reverse address |
| 6 | +* |
| 7 | +* Created : 03.07.2015 |
| 8 | +* |
| 9 | +* Copyright 2015 <xbgmsharp@gmail.com> |
| 10 | +* |
| 11 | +* This program is free software: you can redistribute it and/or modify |
| 12 | +* it under the terms of the GNU General Public License as published by |
| 13 | +* the Free Software Foundation, either version 3 of the License, or |
| 14 | +* (at your option) any later version. |
| 15 | +* |
| 16 | +* This program is distributed in the hope that it will be useful, |
| 17 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +* GNU General Public License for more details. |
| 20 | +* |
| 21 | +* You should have received a copy of the GNU General Public License |
| 22 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | +* |
| 24 | +************************************************/ |
| 25 | + |
| 26 | +// Check whether we are indeed included by Piwigo. |
| 27 | +if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
| 28 | + |
| 29 | +// Check access and exit when user status is not ok |
| 30 | +check_status(ACCESS_ADMINISTRATOR); |
| 31 | + |
| 32 | +// Setup plugin Language |
| 33 | +load_language('plugin.lang', OSM_PATH); |
| 34 | + |
| 35 | +// Fetch the template. |
| 36 | +global $template, $conf, $lang; |
| 37 | + |
| 38 | +// Generate default value |
| 39 | +$sync_options = array( |
| 40 | + 'overwrite' => true, |
| 41 | + 'simulate' => true, |
| 42 | + 'cat_id' => 0, |
| 43 | + 'subcats_included' => true, |
| 44 | + 'osm_tag_address_city_district' => false, |
| 45 | + 'osm_tag_address_city' => true, |
| 46 | + 'osm_tag_address_county' => false, |
| 47 | + 'osm_tag_address_state' => false, |
| 48 | + 'osm_tag_address_country' => false, |
| 49 | + 'osm_tag_address_postcode' => false, |
| 50 | + 'osm_tag_address_country_code' => false, |
| 51 | +); |
| 52 | + |
| 53 | +if ( isset($_POST['osm_submit']) ) |
| 54 | +{ |
| 55 | + // Override default value from the form |
| 56 | + $sync_options = array( |
| 57 | + 'overwrite' => isset($_POST['overwrite']), |
| 58 | + 'simulate' => isset($_POST['simulate']), |
| 59 | + 'cat_id' => isset($_POST['cat_id']) ? (int)$_POST['cat_id'] : 0, |
| 60 | + 'subcats_included' => isset($_POST['subcats_included']), |
| 61 | + 'osm_tag_address_city_district' => isset($_POST['osm_tag_address_city_district']), |
| 62 | + 'osm_tag_address_city' => isset($_POST['osm_tag_address_city']), |
| 63 | + 'osm_tag_address_county' => isset($_POST['osm_tag_address_county']), |
| 64 | + 'osm_tag_address_state' => isset($_POST['osm_tag_address_state']), |
| 65 | + 'osm_tag_address_country' => isset($_POST['osm_tag_address_country']), |
| 66 | + 'osm_tag_address_postcode' => isset($_POST['osm_tag_address_postcode']), |
| 67 | + 'osm_tag_address_country_code' => isset($_POST['osm_tag_address_country_code']) |
| 68 | + ); |
| 69 | + |
| 70 | + include_once( dirname(__FILE__).'/../../../include/functions_tag.inc.php' ); |
| 71 | +// TODO allow to filter on overwrite |
| 72 | + // Define files which lat and long avaiable |
| 73 | + define('SQL_EXIF', "`latitude` IS NOT NULL AND `longitude` is NOT NULL"); |
| 74 | + if ( $sync_options['cat_id']!=0 ) |
| 75 | + { |
| 76 | + $query=' SELECT id FROM '.CATEGORIES_TABLE.' WHERE '; |
| 77 | + |
| 78 | + if ( $sync_options['subcats_included']) |
| 79 | + $query .= 'uppercats REGEXP \'(^|,)'.$sync_options['cat_id'].'(,|$)\''; |
| 80 | + else |
| 81 | + $query .= 'id='.$sync_options['cat_id']; |
| 82 | + $cat_ids = array_from_query($query, 'id'); |
| 83 | + |
| 84 | + $query='SELECT `id`, `latitude`, `longitude` FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
| 85 | + WHERE '. SQL_EXIF .' AND category_id IN ('.implode(',', $cat_ids).') |
| 86 | + GROUP BY id'; |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + $query='SELECT `id`, `latitude`, `longitude` FROM '.IMAGES_TABLE.' WHERE '. SQL_EXIF; |
| 91 | + } |
| 92 | + |
| 93 | + $images = hash_from_query( $query, 'id'); |
| 94 | + $datas = array(); |
| 95 | + $errors = array(); |
| 96 | + $warnings = array(); |
| 97 | + $infos = array(); |
| 98 | + foreach ($images as $image) |
| 99 | + { |
| 100 | + // Fech reverse location from API |
| 101 | + // https://nominatim.openstreetmap.org/reverse?format=xml&lat=51.082333&lon=10.366229&zoom=12 |
| 102 | + // https://open.mapquestapi.com/nominatim/v1/reverse.php?format=xml&lat=48.858366666667&lon=2.2942166666667&zoom=12 |
| 103 | + // http://wiki.openstreetmap.org/wiki/Nominatim |
| 104 | + //$osm_url = "https://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&zoom=12&lat=". $image['latitude'] ."&lon=". $image['longitude']; |
| 105 | + $osm_url = "https://open.mapquestapi.com/nominatim/v1/reverse.php?format=json&addressdetails=1&zoom=12&lat=". $image['latitude'] ."&lon=". $image['longitude']; |
| 106 | + //print $osm_url ."<br/>"; |
| 107 | + |
| 108 | + // Ensure we do have PHP curl install |
| 109 | + // Or should fallback to fopen |
| 110 | + if (function_exists('curl_init')) |
| 111 | + { |
| 112 | + // Get cURL resource |
| 113 | + $curl = curl_init(); |
| 114 | + // Set some options http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy |
| 115 | + curl_setopt_array($curl, array( |
| 116 | + CURLOPT_RETURNTRANSFER => 1, |
| 117 | + CURLOPT_URL => $osm_url, |
| 118 | + CURLOPT_USERAGENT => 'Piwigo OSM xbgmsharp@gmail.com', |
| 119 | + CURLOPT_REFERER => 'http://piwigo.org/' |
| 120 | + )); |
| 121 | + // Send the request & save response to $json |
| 122 | + $json = curl_exec($curl); |
| 123 | + // Close request to clear up some resources |
| 124 | + curl_close($curl); |
| 125 | + |
| 126 | + } else { |
| 127 | + // Curl module un available, use fopen |
| 128 | + $opts = array( |
| 129 | + 'http'=>array( |
| 130 | + 'method'=>"GET", |
| 131 | + 'header'=>"User-Agent: Piwigo OSM xbgmsharp@gmail.com\r\n" |
| 132 | + ) |
| 133 | + ); |
| 134 | + $context = stream_context_create($opts); |
| 135 | + if (false !== ($json = @file_get_contents($osm_url, flase, $context))) { |
| 136 | + // all good |
| 137 | + //return $json; |
| 138 | + } else { |
| 139 | + // error happened |
| 140 | + //return false; |
| 141 | + $errors[] = "Error fetching reverse data"; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (!isset($json) or empty($json)) |
| 146 | + $errors[] = "Error fetching geo reverse address data for ". $image['id']; |
| 147 | + else |
| 148 | + //var_dump(json_decode($json, true)); |
| 149 | + $response = json_decode($json, true); |
| 150 | + //print_r($response); |
| 151 | + |
| 152 | + // If reponse include [address] |
| 153 | + if (isset($response) and isset($response['address']) and is_array($response['address'])) |
| 154 | + { |
| 155 | + //print_r($response['address']); |
| 156 | + //print_r($sync_options); |
| 157 | + $tag_ids = array(); |
| 158 | + if (isset($response['address']['city_district']) and $sync_options['osm_tag_address_city_district']) { |
| 159 | + array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['city_district']) ); |
| 160 | + } |
| 161 | + if (isset($response['address']['city']) and $sync_options['osm_tag_address_city']) { |
| 162 | + array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['city']) ); |
| 163 | + } |
| 164 | + if (isset($response['address']['county']) and $sync_options['osm_tag_address_county']) { |
| 165 | + array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['county']) ); |
| 166 | + } |
| 167 | + if (isset($response['address']['state']) and $sync_options['osm_tag_address_state']) { |
| 168 | + array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['state']) ); |
| 169 | + } |
| 170 | + if (isset($response['address']['country']) and $sync_options['osm_tag_address_country']) { |
| 171 | + array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['country']) ); |
| 172 | + } |
| 173 | + if (isset($response['address']['postcode']) and $sync_options['osm_tag_address_postcode']) { |
| 174 | + array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['postcode']) ); |
| 175 | + } |
| 176 | + if (isset($response['address']['country_code']) and $sync_options['osm_tag_address_country_code']) { |
| 177 | + array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['country_code']) ); |
| 178 | + } |
| 179 | + add_tags($tag_ids, [$image['id']]); |
| 180 | + } |
| 181 | + |
| 182 | + //die("Done one image"); |
| 183 | + } // Images loop |
| 184 | + |
| 185 | + // Send sync result to template |
| 186 | + $template->assign('sync_errors', $errors ); |
| 187 | + $template->assign('sync_warnings', $warnings ); |
| 188 | + $template->assign('sync_infos', $infos ); |
| 189 | + |
| 190 | + // Send result to templates |
| 191 | + $template->assign( |
| 192 | + 'metadata_result', |
| 193 | + array( |
| 194 | + 'NB_ELEMENTS_DONE' => count($datas), |
| 195 | + 'NB_ELEMENTS_CANDIDATES' => count($images), |
| 196 | + 'NB_ERRORS' => count($errors), |
| 197 | + 'NB_WARNINGS' => count($warnings), |
| 198 | + ) |
| 199 | + ); |
| 200 | +} |
| 201 | + |
| 202 | +$query = 'SELECT COUNT(*) FROM '.IMAGES_TABLE.' WHERE `latitude` IS NOT NULL and `longitude` IS NOT NULL '; |
| 203 | +list($nb_geotagged) = pwg_db_fetch_array( pwg_query($query) ); |
| 204 | + |
| 205 | +$query = 'SELECT id, CONCAT(name, IF(dir IS NULL, " (V)", "") ) AS name, uppercats, global_rank FROM '.CATEGORIES_TABLE; |
| 206 | +display_select_cat_wrapper($query, |
| 207 | + array( $sync_options['cat_id'] ), |
| 208 | + 'categories', |
| 209 | + false); |
| 210 | + |
| 211 | +// Send value to templates |
| 212 | +$template->assign( |
| 213 | + array( |
| 214 | + 'SUBCATS_INCLUDED_CHECKED' => $sync_options['subcats_included'] ? 'checked="checked"' : '', |
| 215 | + 'NB_GEOTAGGED' => $nb_geotagged, |
| 216 | + 'OSM_PATH' => OSM_PATH, |
| 217 | + ) |
| 218 | +); |
| 219 | + |
| 220 | +?> |
0 commit comments