<commit_before><?php
/**
* Php proxy to access OpenStreetMap services
* @package   lizmap
* @subpackage lizmap
* @author    3liz
* @copyright 2011 3liz
* @link      http://3liz.com
* @license Mozilla Public License : http://www.mozilla.org/MPL/
*/

class ignCtrl extends jController {

  /**
  * Query the IGN Geoportal API
  * @param text $query A query on IGN BD adresse object
  * @param text $bbox A bounding box in EPSG:4326
  * @return XML.
  */
  function address() {
    $rep = $this->getResponse('json');
    $rep->data = array();

    $query = $this->param('query');
    if ( !$query )
      return $rep;

    // Get the project
    $project = filter_var($this->param('project'), FILTER_SANITIZE_STRING);
    if(!$project)
      return $rep;

    // Get repository data
    $repository = $this->param('repository');
    if(!$repository)
      return $rep;

    // Get the project object
    $lproj = null;
    try {
        $lproj = lizmap::getProject($repository.'~'.$project);
        if(!$lproj)
            return $rep;
    }
    catch(UnknownLizmapProjectException $e) {
        jLog::logEx($e, 'error');
        return $rep;
    }

    $configOptions = $lproj->getOptions();
    if( !property_exists($configOptions, 'ignKey')
      || $configOptions->ignKey == '')
      return $rep;

    $url = 'https://wxs.ign.fr/'.$configOptions->ignKey.'/geoportail/ols?';
    $xls = '<XLS xmlns:xls="http://www.opengis.net/xls" xmlns:gml="http://www.opengis.net/gml" xmlns="http://www.opengis.net/xls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="http://www.opengis.net/xls http://schemas.opengis.net/ols/1.2/olsAll.xsd">';
    $xls .= '<RequestHeader/><Request requestID="1" version="1.2" methodName="LocationUtilityService"><GeocodeRequest returnFreeForm="false"><Address countryCode="StreetAddress">';
    $xls .= '<freeFormAddress>'.$query.'</freeFormAddress>';
    $xls .= '</Address></GeocodeRequest></Request></XLS>';
    $params = array(
      "xls"=>$xls,
      "output"=>'xml'
    );

    $url .= http_build_query($params);
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($curl_handle, CURLOPT_REFERER, jUrl::getFull('lizmap~ign:address'));
    $content = curl_exec($curl_handle);
    curl_close($curl_handle);

    $rep->content = $content;

    $content = str_replace('xmlns=', 'ns=', $content);
    $content = str_replace('gml:', '', $content);
    $xml = simplexml_load_string( $content );
    $results = array();
    $GeocodedAddresses = $xml->xpath('//GeocodedAddress');
    foreach( $GeocodedAddresses as $GeocodedAddress ) {
      $result = array();
      $address = array();

      /*
       * bug with gml:*
       */<commit_msg>[Bugfix] Search IGN: check http code response<commit_after><?php
/**
* Php proxy to access OpenStreetMap services
* @package   lizmap
* @subpackage lizmap
* @author    3liz
* @copyright 2011 3liz
* @link      http://3liz.com
* @license Mozilla Public License : http://www.mozilla.org/MPL/
*/

class ignCtrl extends jController {

  /**
  * Query the IGN Geoportal API
  * @param text $query A query on IGN BD adresse object
  * @param text $bbox A bounding box in EPSG:4326
  * @return XML.
  */
  function address() {
    $rep = $this->getResponse('json');
    $rep->data = array();

    $query = $this->param('query');
    if ( !$query )
      return $rep;

    // Get the project
    $project = filter_var($this->param('project'), FILTER_SANITIZE_STRING);
    if(!$project)
      return $rep;

    // Get repository data
    $repository = $this->param('repository');
    if(!$repository)
      return $rep;

    // Get the project object
    $lproj = null;
    try {
        $lproj = lizmap::getProject($repository.'~'.$project);
        if(!$lproj)
            return $rep;
    }
    catch(UnknownLizmapProjectException $e) {
        jLog::logEx($e, 'error');
        return $rep;
    }

    $configOptions = $lproj->getOptions();
    if( !property_exists($configOptions, 'ignKey')
      || $configOptions->ignKey == '')
      return $rep;

    $url = 'https://wxs.ign.fr/'.$configOptions->ignKey.'/geoportail/ols?';
    $xls = '<XLS xmlns:xls="http://www.opengis.net/xls" xmlns:gml="http://www.opengis.net/gml" xmlns="http://www.opengis.net/xls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="http://www.opengis.net/xls http://schemas.opengis.net/ols/1.2/olsAll.xsd">';
    $xls .= '<RequestHeader/><Request requestID="1" version="1.2" methodName="LocationUtilityService"><GeocodeRequest returnFreeForm="false"><Address countryCode="StreetAddress">';
    $xls .= '<freeFormAddress>'.$query.'</freeFormAddress>';
    $xls .= '</Address></GeocodeRequest></Request></XLS>';
    $params = array(
      "xls"=>$xls,
      "output"=>'xml'
    );

    $url .= http_build_query($params);
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($curl_handle, CURLOPT_REFERER, jUrl::getFull('lizmap~ign:address'));
    $content = curl_exec($curl_handle);
    $info = curl_getinfo($curl_handle);
    $mime = $info['content_type'];
    $http_code = (int) $info['http_code'];
    curl_close($curl_handle);

    if ( $http_code > 400 ) {
        jLog::log(json_encode($info));
        return $rep;
    }

    $rep->content = $content;

    $content = str_replace('xmlns=', 'ns=', $content);
    $content = str_replace('gml:', '', $content);
    $xml = simplexml_load_string( $content );
    $results = array();
    $GeocodedAddresses = $xml->xpath('//GeocodedAddress');
    foreach( $GeocodedAddresses as $GeocodedAddress ) {
      $result = array();
      $address = array();

      // bug with gml:*
{'fraction_non_alphanumeric': 0.17753259779338015, 'fraction_numerical': 0.008191240387830158, 'mean_word_length': 3.435137138621201, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 50, 'https://': 2, 'lorem ipsum': 0, 'www.': 12, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6882028', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2033, 'n_words': 377}