git.ntnlv.ca
Repositories
Help
Report an Issue
qdmtl-ts.git
Code
Commits
Branches
Tags
Search
Tree:
76240b1
Branches
Tags
prod
qdmtl-ts.git
data.php
CORS: allow any origin for data.php
davvalent
commited
76240b1
at 2024-03-09 20:03:13
data.php
Blame
History
Raw
<?php /** * Négociation de contenu pour data.qdmtl.ca : .htaccess + PHP * * 1. URL rewriting pour générer le paramètre d'URL "path" passé à ce script * 2. Traitement avec gestion des URI bilingues * * Local URL rewriting * http://127.0.0.1/~david/dev-web/qdmtl-triple-store/Batiment/1373-1377-de-la-Gauchetiere-Est * http://127\.0\.0\.1/~david/dev\-web/qdmtl\-triple\-store/([A-Z][a-z]+/.+) * * Prod URL rewriting : voir .htaccess */ /** Redirection si paramètre path absent */ if (!isset($_GET['path'])) { header('Location: ./'); header('HTTP/1.1 301 Moved Permanently'); exit(); } require 'parse-ini.php'; require 'vendor/autoload.php'; /** * fonction use_path_to_query * Trois arguments : * requête, store, true pour var_dump() si debug nécessaire */ function use_path_to_query($q, $s, $vd = false) { $rs = $s->query($q); if (!$s->getErrors()) { $index_returned_query_type = [ 'construct', 'describe' ]; $result = (in_array($rs['query_type'], $index_returned_query_type)) ? $rs : $rs['result']['rows']; if ($vd) { var_dump($result); exit(); } return $result; } else { var_dump($s->getErrors()); exit(); } } $request_headers = getallheaders(); /** * Handling CORS * Deprecated since we allow any origin as of 2024-03-09 */ /* $local_host_referer = (str_contains($request_headers['Referer'], "localhost")) ? "http://localhost" : "https://qdmtl.ca"; header('Access-Control-Allow-Origin: ' . $local_host_referer); */ // allow any origin header('Access-Control-Allow-Origin: *'); /** Access to Accept header value */ $accept_header = $request_headers['Accept']; $config = array( 'db_host' => 'localhost', 'db_name' => $infos['db_name'], 'db_user' => $infos['db_user'], 'db_pwd' => $infos['db_pwd'], 'store_name' => $infos['store_name'], 'max_errors' => 1, 'store_write_buffer' => 40000 ); /** namespace prefixes */ $ns = array( 'dcterms' => 'http://purl.org/dc/terms/', 'ecrm' => 'http://erlangen-crm.org/current/', 'geo' => 'http://www.opengis.net/ont/geosparql#', 'owl' => 'http://www.w3.org/2002/07/owl#', 'qdmtl' => 'http://onto.qdmtl.ca/', 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', 'rico' => 'https://www.ica.org/standards/RiC/ontology#', 'schema' => 'https://schema.org/' ); $ns_config = array('ns' => $ns); /** instantiation */ $store = ARC2::getStore($config); $rdf_xml_serializer = ARC2::getRDFXMLSerializer($ns_config); // $store->createDBCon(); if (!$store->isSetUp()) { $store->setUp(); } $path = $_GET["path"]; // vérifier si objet de la prop owl:sameAs pour traduction $q = ' PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX qdmtld: <http://data.qdmtl.ca/> SELECT ?s WHERE { ?s owl:sameAs qdmtld:' . $path . ' . } '; $has_sameAs = use_path_to_query($q, $store); preg_match('/[A-Z][a-z]+\/(.+)/', $path, $chunked_path); foreach ($has_sameAs as $sameAs) { preg_match('/http:\/\/data\.qdmtl\.ca\/([A-Z][a-z]+\/)(.+)/', $sameAs['s'], $chunked_uri_path); if ($chunked_path[1] == $chunked_uri_path[2]) $path = $chunked_uri_path[1] . $chunked_uri_path[2]; } $q = ' PREFIX qdmtld: <http://data.qdmtl.ca/> DESCRIBE qdmtld:' . $path; $triples = use_path_to_query($q, $store); if (!$triples['result']) { echo "SPARQL query returned 0 result"; exit(); } /** json-ld */ if ($accept_header == 'application/ld+json') { $graph = new \EasyRdf\Graph(/*'http://data.qdmtl.ca/' . $path*/); $graph->parse($triples['result']); // echo $graph->dump('html');exit(); // var_dump($graph);exit(); $jsonld = $graph->serialise('application/ld+json', array( 'expand_native_types' => null, 'compact' => true, 'context' => './qdmtl-context.jsonld', 'expand' => null, 'frame' => null )); if (!is_scalar($jsonld)) { $jsonld = var_export($jsonld, true); } header('Content-type: application/ld+json'); echo $jsonld; exit(); } /** RDF/XML */ $rdf_xml_serialization = $rdf_xml_serializer->getSerializedIndex($triples['result']); header('Content-type: text/xml'); echo $rdf_xml_serialization; ?>