-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.php
179 lines (164 loc) · 6.4 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
ini_set('memory_limit', '3072M');
require 'deployment.settings.php';
require_once 'lda.inc.php';
require 'setup.php';
require_once 'lda-cache.class.php';
require_once 'lda-request.class.php';
require_once 'lda-response.class.php';
require_once 'graphs/configgraph.class.php';
require_once 'responses/Response304.class.php';
Logger::configure("puelia.logging.properties");
LinkedDataApiRequest::eliminateDebugParams();
$HttpRequestFactory = new HttpRequestFactory();
//if(function_exists('memcache_connect')){
// $MemCacheObject = new LinkedDataApiCache();
// $HttpRequestFactory->set_cache($MemCacheObject);
//}
$Request = new LinkedDataApiRequest();
header("Access-Control-Allow-Origin: *");
define("CONFIG_PATH", '/api-config');
define("CONFIG_URL", $Request->getBaseAndSubDir().CONFIG_PATH);
logDebug("Request URI: ".$Request->getUri());
if(rtrim($Request->getPath(), '/')==$Request->getInstallSubDir()){
header("Location: ".CONFIG_URL, true, 303);
exit;
}
if ("/swagger" == $Request->getPathWithoutVersionAndExtension()) {
$swagger = file_get_contents("api-config-files/swagger.json", true);
$json = json_decode($swagger, true);
$base = preg_replace(",(.*)/[^/]*$,", '$1/', $Request->getUri());
if (isset($_SERVER["HTTP_X_3SCALE_PROXY_SECRET_TOKEN"])) {
$base = str_replace("http://", "https://", $base);
}
$json["basePath"] = $base;
//phpinfo();
header("Content-Type: application/json");
echo json_encode($json); // JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit;
}
if (
defined("PUELIA_SERVE_FROM_CACHE")
AND
PUELIA_SERVE_FROM_CACHE
AND
!$Request->hasNoCacheHeader()
AND
$cachedResponse = LinkedDataApiCache::hasCachedResponse($Request)
)
{
logDebug("Found cached response");
if (isset($Request->ifNoneMatch) && $cachedResponse->eTag == $Request->ifNoneMatch)
{
logDebug("ETag matched, returning 304");
$Response = new Response304($cachedResponse);
}
else if (isset($Request->ifModifiedSince) && $cachedResponse->generatedTime <= $Request->ifModifiedSince)
{
logDebug("Last modified date matched, returning 304");
$Response = new Response304($cachedResponse);
}
else
{
logDebug("Re-Serving cached response");
$Response = $cachedResponse;
}
}
else
{
if (!(defined("PUELIA_SERVE_FROM_CACHE") AND PUELIA_SERVE_FROM_CACHE
AND !$Request->hasNoCacheHeader())){
$HttpRequestFactory->read_from_cache(FALSE);
}
logDebug("Generating fresh response");
$files = glob('api-config-files/*.ttl');
/* if there is a config file named after the current domain, use only that */
$domainBasedConfigFilename= 'api-config-files/'.$Request->getServerName().'.ttl';
if(in_array($domainBasedConfigFilename, $files)){
$files = array($domainBasedConfigFilename);
}
/*
* Loop thru all of the RDF *.ttl files in the 'api-config-files' directory.
* For each, load the ttl file into an RDF Graph, in the variable $ConfigGraph.
* Note: the $ConfigGraph object contains the $Request object.
* If the config of $ConfigGraph matches the $Request, construct a $Response object from the
* $Request and $ConfigGraph, then call $Response->process() and exit the loop.
* Finally, call $Response->serve() and cache the $Request --> $Response pair.
* Note: the $CompleteConfigGraph object is only used if no config matches the $Request.
*/
$CompleteConfigGraph = new ConfigGraph(null, $Request, $HttpRequestFactory);
foreach($files as $file){
//logDebug("Iterating over files in /api-config: $file");
if (
defined("PUELIA_SERVE_FROM_CACHE")
AND
PUELIA_SERVE_FROM_CACHE
AND
$ConfigGraph = LinkedDataApiCache::hasCachedConfig($file)){
// logDebug("Found Cached Config {$file}");
$CompleteConfigGraph->add_graph($ConfigGraph);
$ConfigGraph->setRequest($Request);
} else {
// logDebug("Checking Config file: $file");
$rdf = file_get_contents($file);
$CompleteConfigGraph->add_rdf($rdf);
$ConfigGraph = new ConfigGraph(null, $Request, $HttpRequestFactory);
$ConfigGraph->add_rdf($rdf);
$errors = $ConfigGraph->get_parser_errors();
if(!empty($errors)){
foreach($ConfigGraph->get_parser_errors() as $errorList){
foreach($errorList as $errorMsg){
logDebug('Error parsing '.$file.' '.$errorMsg);
}
}
}
if (
defined("PUELIA_SERVE_FROM_CACHE")
AND
PUELIA_SERVE_FROM_CACHE
) {
logDebug("Caching $file");
LinkedDataApiCache::cacheConfig($file, $ConfigGraph);
}
}
/*
* $ConfigGraph contains the Graph from the ttl file for this loop iteration.
* Next, see if the ConfigGraph matches the current $Request. If it does, create a new
* $Response instance, which will contain the $Request and $ConfigGraph.
* Then, call $Response->process() and exit the loop. Below, followup with
* $Response->serve();
*/
$ConfigGraph->init();
$ConfigGraph->sparqlEndpointUri = OPS_SPARQL_ENDPOINT;
if($selectedEndpointUri = $ConfigGraph->getEndpointUri()){
logDebug("Endpoint Uri Selected: $selectedEndpointUri");
unset($CompleteConfigGraph);
$Response = new LinkedDataApiResponse($Request, $ConfigGraph, $HttpRequestFactory);
$Response->process();
break;
} else if($docPath = $ConfigGraph->dataUriToEndpointItem($Request->getUri())){
logDebug("Redirecting ".$Request->getUri()." to {$docPath}");
header("Location: $docPath", 303);
exit;
}
}
if(!isset($selectedEndpointUri)){
logDebug("No Endpoint Selected");
$Response = new LinkedDataApiResponse($Request, $CompleteConfigGraph);
if($Request->getPathWithoutExtension()==$Request->getInstallSubDir().CONFIG_PATH){
logDebug("Serving ConfigGraph");
$Response->serveConfigGraph();
} else {
logDebug("URI Requested:" . $Request->getPathWithoutExtension());
$Response->process();
}
}
}
$Response->serve();
if (defined("PUELIA_SERVE_FROM_CACHE") AND PUELIA_SERVE_FROM_CACHE
AND !$Request->hasNoCacheHeader()
AND $Response->cacheable)
{
LinkedDataApiCache::cacheResponse($Request, $Response);
}
?>