59 lines
2.5 KiB
PHP
59 lines
2.5 KiB
PHP
<?php
|
|
|
|
$url = 'https://lkv.at/at/zertifizierung/themen/BIO/zertifizierte-BIO-Betriebe.php';
|
|
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
|
|
header('Status: 405');
|
|
header('Content-Length: 56');
|
|
header('Allow: GET, HEAD');
|
|
echo "{\"error\":\"method not allowed\",\"allow\":[\"GET\",\"HEAD\"]}\n";
|
|
exit;
|
|
}
|
|
|
|
function jenc($data): string {
|
|
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
$info = $_SERVER['PATH_INFO'];
|
|
if ($info !== '') {
|
|
header('Status: 404');
|
|
header('Content-Length: 22');
|
|
echo "{\"error\":\"not found\"}\n";
|
|
exit;
|
|
}
|
|
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
$postalCode = '';
|
|
if (isset($_GET['postalCode']) && ctype_digit($_GET['postalCode'])) {
|
|
$postalCode = $_GET['postalCode'];
|
|
}
|
|
$lfbisNr = '';
|
|
if (isset($_GET['lfbisNr'])) {
|
|
if (strlen($_GET['lfbisNr']) === 7) {
|
|
$lfbisNr = '"' . $_GET['lfbisNr'];
|
|
} else {
|
|
$lfbisNr = 'invalid';
|
|
}
|
|
}
|
|
|
|
echo "{\"sourceUrl\":\"$url\",\"data\":[\n";
|
|
passthru(<<<EOF
|
|
curl -s '$url' | grep -A3 '<tr' \
|
|
| sed 's@<tr>@ {@;s@</tr>@ },@;s@<td><a .*href="\([^"]*\)".*>\(.*\)</a></td>@ "certUrl":"\\1","certNr":"\\2",@;s@<td>\(.*\)<br/>\(.*\)<br/>\([0-9]*\) \([^<]*\)\(<br/>\)\?</td>@ "name":"\\1","address":"\\2","postalCode":"\\3","city":"\\4"@;s@<td>\(.*\)<br/>\(.*\)</td>@ "name":"\\1","type":"\\2"@' \
|
|
| sed 's@"certUrl":"\(/[^"]*\)"@"certUrl":"https://lkv.at\\1"@;s@"certNr":"[^0-9]*0\?\([0-9]\{7\}\|[0-9]\{1,4\}\|[0-9]\{7,\}\)_[^"]*"@\\0,"id":"\\1"@;s@"id":"\([0-9]\{7\}\)"@\\0,"lfbisNr":"\\1"@;s@"id":"\([^"]\{,6\}\|[^"]\{8,\}\)"@\\0,"lfbisNr":null@' \
|
|
| sed 's@"certUrl":"\([^"]*\)","certNr":"\([^"]*\)",@"certificates":[{"pdfUrl":"\\1","nr":"\\2"}],\\n @' \
|
|
| perl -ne 'BEGIN { our \$N = 8; our @buf; } push @buf, \$_; if (@buf > \$N) { print shift @buf; } my \$chunk = join "", @buf;
|
|
if (\$chunk =~ /(}],\n(?:.*\n){6}\s*"name":"[^"]*","type":)/) {
|
|
\$chunk =~ s/(}],\n((?:.*\n){2})(?:.*\n){2}\s*"certificates":\[\{"pdfUrl":"([^"]*)","nr":"([^"]*)"\}\],\n.*\n\s*"name":"[^"]*","type":"([^"]*)")/},{"pdfUrl":"\$3","nr":"\$4","type":"\$5"}],\n\$2/;
|
|
@buf = map { "\$_\n" } split /\n/, \$chunk;
|
|
} END { print for @buf; }' \
|
|
| grep -B3 -A1 --no-group-separator '"postalCode":"$postalCode' | grep -B2 -A2 --no-group-separator '"lfbisNr":$lfbisNr' \
|
|
| sed '\$s/.$//'
|
|
| sed 's@\s*",@",@g;s@:"\s*@:"@g'
|
|
EOF);
|
|
echo "]}\n";
|