Skip to content

Commit df9ad7e

Browse files
committed
Merge pull request #364 from tdt/development
Development
2 parents 77e6047 + b2348f1 commit df9ad7e

File tree

8 files changed

+3445
-3155
lines changed

8 files changed

+3445
-3155
lines changed

app/Tdt/Core/DataControllers/CSVController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private function createValues($columns, $data)
181181

182182
foreach ($columns as $column) {
183183
if (!empty($data[$column['index']]) || is_numeric(@$data[$column['index']])) {
184-
$result[$column['column_name_alias']] = @$data[$column['index']];
184+
$result[$column['column_name_alias']] = \ForceUTF8\Encoding::fixUTF8(@$data[$column['index']]);
185185
} else {
186186
$index = $column['index'];
187187

app/Tdt/Core/DataControllers/JSONController.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,60 @@ private function getPlainJson($uri)
100100
}
101101

102102
if (!filter_var($uri, FILTER_VALIDATE_URL) === false) {
103-
$ch = curl_init();
104-
curl_setopt($ch, CURLOPT_URL, $uri);
105-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
106-
$data = curl_exec($ch);
107-
curl_close($ch);
103+
$parts = parse_url($uri);
104+
if ($parts['scheme'] != 'file') {
105+
$data = $this->getRemoteData($uri);
106+
} else {
107+
$data =@ file_get_contents($uri);
108+
}
108109
} else {
109110
$data =@ file_get_contents($uri);
110111
}
111112

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

119119
return $data;
120120
}
121+
122+
private function getRemoteData($url)
123+
{
124+
$c = curl_init();
125+
curl_setopt($c, CURLOPT_URL, $url);
126+
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
127+
128+
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
129+
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
130+
curl_setopt($c, CURLOPT_MAXREDIRS, 10);
131+
$follow_allowed= ( ini_get('open_basedir') || ini_get('safe_mode')) ? false:true;
132+
133+
if ($follow_allowed) {
134+
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
135+
}
136+
137+
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 9);
138+
curl_setopt($c, CURLOPT_REFERER, $url);
139+
curl_setopt($c, CURLOPT_TIMEOUT, 60);
140+
curl_setopt($c, CURLOPT_AUTOREFERER, true);
141+
curl_setopt($c, CURLOPT_ENCODING, 'gzip,deflate');
142+
$data = curl_exec($c);
143+
$status = curl_getinfo($c);
144+
curl_close($c);
145+
146+
preg_match('/(http(|s)):\/\/(.*?)\/(.*\/|)/si', $status['url'], $link);
147+
$data = preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/|\/)).*?)(\'|\")/si', '$1=$2' . $link[0] . '$3$4$5', $data);
148+
149+
$data = preg_replace('/(src|href|action)=(\'|\")((?!(http|https|javascript:|\/\/)).*?)(\'|\")/si', '$1=$2' . $link[1] . '://' . $link[3] . '$3$4$5', $data);
150+
151+
if ($status['http_code'] == 200) {
152+
return $data;
153+
} elseif ($status['http_code'] == 301 || $status['http_code'] == 302) {
154+
\App::abort(400, "The JSON URL redirected us to a different URI.");
155+
}
156+
157+
return $data;
158+
}
121159
}

app/Tdt/Core/DataControllers/XMLController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function readData($source_definition, $rest_parameters = array())
5151

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

5657
return $data_result;

app/Tdt/Core/Formatters/FormatHelper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ public function getAvailableFormats($data)
4545
$formats['WKT'] = 'wkt';
4646
} elseif (!empty($data->geo_formatted) && $data->geo_formatted) {
4747
if (strtolower($source_type) == 'xml') {
48+
$formats = array_merge(array('Fullscreen map' => 'map'), $formats);
4849
$formats['KML'] = 'kml';
50+
$formats['GEOJSON'] = 'geojson';
4951
unset($formats['XML']);
5052
} elseif (strtolower($source_type) == 'json' && $data->geo_formatted) {
53+
$formats = array_merge(array('Fullscreen map' => 'map'), $formats);
5154
$formats['GeoJSON'] = 'geojson';
5255
unset($formats['JSON']);
5356
}

app/Tdt/Core/Formatters/GEOJSONFormatter.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ public static function createResponse($dataObj)
2929
public static function getBody($dataObj)
3030
{
3131
// Check if the original data is not GeoJSON
32-
if ($dataObj->source_definition['type'] == 'JSON' && !empty($dataObj->geo_formatted) && $dataObj->geo_formatted) {
33-
return json_encode($dataObj->data);
32+
if (!empty($dataObj->geo_formatted) && $dataObj->geo_formatted) {
33+
if ($dataObj->source_definition['type'] == 'JSON') {
34+
return json_encode($dataObj->data);
35+
} elseif ($dataObj->source_definition['type'] == 'XML') {
36+
$geom = \geoPHP::load($dataObj->data, 'kml');
37+
return $geom->out('json');
38+
}
3439
}
3540

3641
// Build the body

app/Tdt/Core/Formatters/JSONFormatter.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public static function createResponse($dataObj)
2626

2727
public static function getBody($dataObj)
2828
{
29-
3029
// If the data is semantic, return json ld
3130
if ($dataObj->is_semantic) {
32-
3331
$jsonld_formatter = new JSONLDFormatter();
3432

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

4442
if ($dataObj->is_semantic) {
45-
4643
// Check if a configuration is given
4744
$conf = array();
4845
if (!empty($dataObj->semantic->conf)) {

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"muka/shape-reader" : "dev-master",
3030
"elasticsearch/elasticsearch": "1.4.1",
3131
"nesbot/carbon": "~1.14",
32-
"tdt/input" : "2.0.*"
32+
"tdt/input" : "2.0.*",
33+
"phayes/geophp" : "dev-master"
3334
},
3435
"require-dev": {
3536
"mockery/mockery": "dev-master@dev"

0 commit comments

Comments
 (0)