Skip to content

Commit

Permalink
Merge pull request #364 from tdt/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
coreation committed Dec 21, 2015
2 parents 77e6047 + b2348f1 commit df9ad7e
Show file tree
Hide file tree
Showing 8 changed files with 3,445 additions and 3,155 deletions.
2 changes: 1 addition & 1 deletion app/Tdt/Core/DataControllers/CSVController.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function createValues($columns, $data)

foreach ($columns as $column) {
if (!empty($data[$column['index']]) || is_numeric(@$data[$column['index']])) {
$result[$column['column_name_alias']] = @$data[$column['index']];
$result[$column['column_name_alias']] = \ForceUTF8\Encoding::fixUTF8(@$data[$column['index']]);
} else {
$index = $column['index'];

Expand Down
50 changes: 44 additions & 6 deletions app/Tdt/Core/DataControllers/JSONController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,60 @@ private function getPlainJson($uri)
}

if (!filter_var($uri, FILTER_VALIDATE_URL) === false) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$parts = parse_url($uri);
if ($parts['scheme'] != 'file') {
$data = $this->getRemoteData($uri);
} else {
$data =@ file_get_contents($uri);
}
} else {
$data =@ file_get_contents($uri);
}

if ($data) {
Cache::put($uri, $data, $this->cache);
} else {
$uri = $source_definition['uri'];
\App::abort(500, "Cannot retrieve data from the JSON file located on $uri.");
}

return $data;
}

private function getRemoteData($url)
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_MAXREDIRS, 10);
$follow_allowed= ( ini_get('open_basedir') || ini_get('safe_mode')) ? false:true;

if ($follow_allowed) {
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
}

curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
curl_setopt($c, CURLOPT_REFERER, $url);
curl_setopt($c, CURLOPT_TIMEOUT, 60);
curl_setopt($c, CURLOPT_AUTOREFERER, true);
curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');
$data = curl_exec($c);
$status = curl_getinfo($c);
curl_close($c);

preg_match('/(http(|s)):\/\/(.*?)\/(.*\/|)/si', $status['url'], $link);
$data = preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/|\/)).*?)(\'|\")/si', '$1=$2' . $link[0] . '$3$4$5', $data);

$data = preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/)).*?)(\'|\")/si', '$1=$2' . $link[1] . '://' . $link[3] . '$3$4$5', $data);

if ($status['http_code'] == 200) {
return $data;
} elseif ($status['http_code'] == 301 || $status['http_code'] == 302) {
\App::abort(400, "The JSON URL redirected us to a different URI.");
}

return $data;
}
}
1 change: 1 addition & 0 deletions app/Tdt/Core/DataControllers/XMLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function readData($source_definition, $rest_parameters = array())

if (!empty($source_definition['geo_formatted']) && $source_definition['geo_formatted']) {
$data_result->geo_formatted = true;
$data_result->preferred_formats = array('geojson', 'map');
}

return $data_result;
Expand Down
3 changes: 3 additions & 0 deletions app/Tdt/Core/Formatters/FormatHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ public function getAvailableFormats($data)
$formats['WKT'] = 'wkt';
} elseif (!empty($data->geo_formatted) && $data->geo_formatted) {
if (strtolower($source_type) == 'xml') {
$formats = array_merge(array('Fullscreen map' => 'map'), $formats);
$formats['KML'] = 'kml';
$formats['GEOJSON'] = 'geojson';
unset($formats['XML']);
} elseif (strtolower($source_type) == 'json' && $data->geo_formatted) {
$formats = array_merge(array('Fullscreen map' => 'map'), $formats);
$formats['GeoJSON'] = 'geojson';
unset($formats['JSON']);
}
Expand Down
9 changes: 7 additions & 2 deletions app/Tdt/Core/Formatters/GEOJSONFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ public static function createResponse($dataObj)
public static function getBody($dataObj)
{
// Check if the original data is not GeoJSON
if ($dataObj->source_definition['type'] == 'JSON' && !empty($dataObj->geo_formatted) && $dataObj->geo_formatted) {
return json_encode($dataObj->data);
if (!empty($dataObj->geo_formatted) && $dataObj->geo_formatted) {
if ($dataObj->source_definition['type'] == 'JSON') {
return json_encode($dataObj->data);
} elseif ($dataObj->source_definition['type'] == 'XML') {
$geom = \geoPHP::load($dataObj->data, 'kml');
return $geom->out('json');
}
}

// Build the body
Expand Down
3 changes: 0 additions & 3 deletions app/Tdt/Core/Formatters/JSONFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ public static function createResponse($dataObj)

public static function getBody($dataObj)
{

// If the data is semantic, return json ld
if ($dataObj->is_semantic) {

$jsonld_formatter = new JSONLDFormatter();

return $jsonld_formatter->getBody($dataObj);
Expand All @@ -42,7 +40,6 @@ public static function getBody($dataObj)
}

if ($dataObj->is_semantic) {

// Check if a configuration is given
$conf = array();
if (!empty($dataObj->semantic->conf)) {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"muka/shape-reader" : "dev-master",
"elasticsearch/elasticsearch": "1.4.1",
"nesbot/carbon": "~1.14",
"tdt/input" : "2.0.*"
"tdt/input" : "2.0.*",
"phayes/geophp" : "dev-master"
},
"require-dev": {
"mockery/mockery": "dev-master@dev"
Expand Down
Loading

0 comments on commit df9ad7e

Please sign in to comment.