forked from data-ac-uk/OPDLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrgProfileDocument.php
298 lines (252 loc) · 7 KB
/
OrgProfileDocument.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
class OrgProfileDocument
{
# allowed $from:
# url
# result
# string
function __construct( $param, $from = "url" )
{
if( $from == "local"){
$this->result = OrgProfileDocument::get_local( $param );
if( $this->result["STATUS"] != "ok" )
{
throw new OPD_Load_Exception( "Failed to load document: Error ".$this->result["STATUS"] );
}
}
if( $from == "url" )
{
$this->result = OrgProfileDocument::get_url( $param );
if( $this->result["HTTP_CODE"] != "200" )
{
throw new OPD_Load_Exception( "Failed to load document: Error ".$this->result["HTTP_CODE"] );
}
}
if( $from == "result" )
{
$this->result = $param;
}
$parse_as = "Turtle";
if( in_array($from, array("result","url","local") ) )
{
$effective_url = $this->result["EFFECTIVE_URL"];
if( $this->result["CONTENT_TYPE"]=="application/rdf+xml" ) { $parse_as = "RDFXML"; }
$document = $this->result["CONTENT"];
}
elseif( $from == "string" )
{
$document = $param;
$effective_url = "http://example.org/opd";
}
else
{
throw new OPD_Exception( "Unknown value for 'from': '$from'" );
}
$this->graph = new Graphite();
$this->graph->ns( "aiiso", "http://purl.org/vocab/aiiso/schema#" );
$this->graph->ns( "org", "http://www.w3.org/ns/org#" );
$this->n = 0;
if( $parse_as == "RDFXML" )
{
$this->n = $this->graph->addRDFXML( $effective_url, $document );
}
elseif( $parse_as == "Turtle" )
{
$this->n = $this->graph->addTurtle( $effective_url, $document );
}
else
{
throw new OPD_Exception( "Unknown parse_as value: '$parse_as'." );
}
# assume turtle.
if( $this->n == 0 )
{
throw new OPD_Parse_Exception( "Failed to parse OPD as a $parse_as document.", $parse_as, $document );
}
$profileDocument = $this->graph->allOfType( "oo:OrganizationProfileDocument" );
if( count($profileDocument) == 0 )
{
throw new OPD_Parse_Exception( "Document contains no oo:OrganizationProfileDocument.", $parse_as, $document );
}
if( count($profileDocument) > 1 )
{
throw new OPD_Parse_Exception( "Document contains more than one oo:OrganizationProfileDocument.", $parse_as, $document );
}
$this->doc = $profileDocument[0];
if( !$this->doc->has( "foaf:primaryTopic" ) )
{
throw new OPD_Parse_Exception( "oo:OrganizationProfileDocument does not have a foaf:primaryTotpic.", $parse_as, $document );
}
$this->org = $this->doc->get( "foaf:primaryTopic" );
}
static function from_string( $string )
{
return new OrgProfileDocument( $string, "string" );
}
static function autodiscover ( $homepages ) {
$opds = array();
foreach($homepages as $url)
{
try{
$opd = OrgProfileDocument::discover( $url );
}
catch( OPD_Discover_Exception $e )
{
continue;
}
catch( OPD_Load_Exception $e )
{
continue;
}
catch( OPD_Parse_Exception $e )
{
continue;
}
catch( Exception $e )
{
continue;
}
$opds[] = $opd->opd_url;
}
return $opds;
}
static function discover( $url )
{
$ok = preg_match( "/^(https?:\/\/[-a-z0-9\.]+)/", $url, $bits );
if( !$ok )
{
throw new OPD_Discover_Exception( "Discovery URL does not appear valid." );
}
$homepage = $bits[1]."/";
# Ok. step 1, try .well-known/openorg
$wt = $homepage.".well-known/openorg";
$result = OrgProfileDocument::get_url( $wt );
if( $result["HTTP_CODE"] == "200" )
{
$opd = new OrgProfileDocument( $result, "result" );
$opd->discovery = "WELL-KNOWN";
$opd->opd_url = $result['EFFECTIVE_URL'];
return $opd;
}
# well, didn't find it that way, lets try through the homepage
$result = OrgProfileDocument::get_url( $homepage );
if( $result["HTTP_CODE"] != "200" )
{
# could not load the homepage, weird but can happen
throw new OPD_Discover_Exception( "Failed to discover via well-known. Failed to load homepage with error ".$result["HTTP_CODE"]."." );
}
$content = preg_replace( "/\n/", ' ', $result["CONTENT"] );
$links = array();
preg_replace( "/<link([^>]+)>/e", '$links []= "$1";', $content );
foreach( $links as $link )
{
$link = preg_replace( '/\\\\\'/', '"', $link );
$l = array();
preg_replace( "/([a-z]+)\s*=\s*(\"([^\"]+)\"|([^\s]+))/ei", '$l["$1"] = "$3$4"', $link );
if( @ $l["rel"] == "" ) { continue; }
# use url_to_absolute if available otherwise busk it
if( @ $l["href"] )
{
if( function_exists( "url_to_absolute" ) )
{
# if the url to absolute library is available, obviously
# we'll use that.
$l["href"] = url_to_absolute( $homepage, $l["href"] );
}
else
{
# otherwise busk it, unless it starts with https? in which
# case no action is required.
if( ! preg_match( "/^https?:/", $l["href"] ) )
{
$l["href"] = $homepage . preg_replace( "/^\//", "", $l["href"] );
}
}
}
$linkdata[$l["rel"]][]= $l;
}
if( !@$linkdata["openorg"][0]["href"] )
{
throw new OPD_Discover_Exception( "Failed to discover via well-known. Homepage loaded OK but had no rel='openorg' link." );
}
$opd_url = $linkdata["openorg"][0]["href"];
$opd = new OrgProfileDocument( $opd_url, "url" );
$opd->discovery = "LINK";
$opd->opd_url = $linkdata["openorg"][0]["href"];
return $opd;
}
private static function get_local($path)
{
$ret = array();
if(!file_exists($path)){
$ret['STATUS'] = 'Error: File Not Found';
return $ret;
}
$ret['STATUS'] = 'ok';
$ret['EFFECTIVE_URL'] = $path;
$ret['CONTENT_TYPE'] = trim(shell_exec("file -bi " . escapeshellarg( $path )));
$ret['CONTENT_LENGTH_DOWNLOAD'] = filesize($path);
$ret['CONTENT'] = file_get_contents($path);
return $ret;
}
private static function get_url($url)
{
global $last_httpCode;
$process = curl_init($url);
$headers = array();
$headers[] = 'Accept: text/turtle, application/rdf+xml, */*;q=0.1';
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$r = array();
$r["CONTENT"] = curl_exec($process);
$r["HTTP_CODE"] = curl_getinfo($process, CURLINFO_HTTP_CODE);
$r["EFFECTIVE_URL"] = curl_getinfo($process, CURLINFO_EFFECTIVE_URL);
$r["CONTENT_TYPE"] = curl_getinfo($process, CURLINFO_CONTENT_TYPE);
$r["CONTENT_LENGTH_DOWNLOAD"] = curl_getinfo($process, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($process);
return $r;
}
function datasets( $subjects )
{
if( !is_array( $subjects ) ) { $subjects = array( $subjects ); }
$datasets = array();
foreach( $this->org->all( "-oo:organization" ) as $org_thing )
{
foreach( $org_thing->all( "dcterms:subject" ) as $thing_subject )
{
foreach( $subjects as $wanted_subject )
{
if( $thing_subject == $wanted_subject )
{
$datasets []= $org_thing;
continue(2);
}
}
}
}
return $datasets;
}
}
class OPD_Exception extends Exception
{
}
class OPD_Discover_Exception extends OPD_Exception
{
}
class OPD_Load_Exception extends OPD_Exception
{
}
class OPD_Parse_Exception extends OPD_Exception
{
var $format;
var $document;
function __construct( $msg, $format, $document )
{
$this->format = $format;
$this->document = $document;
parent::__construct( $msg );
}
}