AuthRegistrarWs.php
Go to the documentation of this file.
00001 <?php 00002 00005 00026 class AuthRegistrarWs extends WebService 00027 { 00029 private $db; 00030 00032 private $conneg; 00033 00035 private $dtdURL; 00036 00038 public static $supportedSerializations = 00039 array ("application/json", "application/rdf+xml", "application/rdf+n3", "application/*", "text/xml", "text/*", 00040 "*/*"); 00041 00043 private $registered_title = ""; 00044 00046 private $registered_endpoint = ""; 00047 00049 private $registered_crud_usage = ""; 00050 00052 private $registered_uri = ""; 00053 00055 private $requester_ip = ""; 00056 00058 private $errorMessenger = 00059 '{ 00060 "ws": "/ws/auth/registrar/ws/", 00061 "_200": { 00062 "id": "WS-AUTH-REGISTRAR-WS-200", 00063 "level": "Warning", 00064 "name": "No endpoint URL", 00065 "description": "No endpoint URL defined for this query." 00066 }, 00067 "_201": { 00068 "id": "WS-AUTH-REGISTRAR-WS-201", 00069 "level": "Warning", 00070 "name": "No crud usage defined", 00071 "description": "No crud usage defined for this query." 00072 }, 00073 "_202": { 00074 "id": "WS-AUTH-REGISTRAR-WS-202", 00075 "level": "Warning", 00076 "name": "No web service URI defined", 00077 "description": "No web service URI defined for this query." 00078 }, 00079 "_203": { 00080 "id": "WS-AUTH-REGISTRAR-WS-203", 00081 "level": "Fatal", 00082 "name": "Can\'t check of the web service was already registered to this WSF", 00083 "description": "An error occured when we tried to check if the web service was already registered to this web service network." 00084 }, 00085 "_204": { 00086 "id": "WS-AUTH-REGISTRAR-WS-204", 00087 "level": "Warning", 00088 "name": "Web service already registered", 00089 "description": "This web service is already registered to this Web Service Framework." 00090 }, 00091 "_300": { 00092 "id": "WS-AUTH-REGISTRAR-WS-300", 00093 "level": "Fatal", 00094 "name": "Can\'t register this web service to the network", 00095 "description": "An error occured when we tried to register this new web service to the network." 00096 } 00097 }'; 00098 00099 00122 function __construct($registered_title, $registered_endpoint, $registered_crud_usage, $registered_uri, $registered_ip, $requester_ip) 00123 { 00124 parent::__construct(); 00125 00126 $this->db = new DB_Virtuoso($this->db_username, $this->db_password, $this->db_dsn, $this->db_host); 00127 00128 $this->registered_title = $registered_title; 00129 $this->registered_endpoint = $registered_endpoint; 00130 $this->registered_crud_usage = $registered_crud_usage; 00131 $this->registered_uri = $registered_uri; 00132 $this->requester_ip = $requester_ip; 00133 00134 if($registered_ip == "") 00135 { 00136 $this->registered_ip = $requester_ip; 00137 } 00138 else 00139 { 00140 $this->registered_ip = $registered_ip; 00141 } 00142 00143 if(strtolower(substr($this->registered_ip, 0, 4)) == "self") 00144 { 00145 $pos = strpos($this->registered_ip, "::"); 00146 00147 if($pos !== FALSE) 00148 { 00149 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2)); 00150 00151 $this->registered_ip = $requester_ip . "::" . $account; 00152 } 00153 else 00154 { 00155 $this->registered_ip = $requester_ip; 00156 } 00157 } 00158 00159 $this->uri = $this->wsf_base_url . "/wsf/ws/auth/registrar/ws/"; 00160 $this->title = "Authentication Web Service Registration Web Service"; 00161 $this->crud_usage = new CrudUsage(TRUE, TRUE, FALSE, FALSE); 00162 $this->endpoint = $this->wsf_base_url . "/ws/auth/registrar/ws/"; 00163 00164 $this->dtdURL = "auth/authRegistrarWs.dtd"; 00165 00166 $this->errorMessenger = json_decode($this->errorMessenger); 00167 } 00168 00169 function __destruct() 00170 { 00171 parent::__destruct(); 00172 00173 if(isset($this->db)) 00174 { 00175 @$this->db->close(); 00176 } 00177 } 00178 00189 protected function validateQuery() 00190 { 00191 $ws_av = new AuthValidator($this->requester_ip, $this->wsf_graph, $this->uri); 00192 00193 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00194 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00195 00196 $ws_av->process(); 00197 00198 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00199 { 00200 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus()); 00201 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg()); 00202 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt()); 00203 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice, 00204 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description, 00205 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level); 00206 } 00207 00208 // If the system send a query on the behalf of another user, we validate that other user as well 00209 if($this->registered_ip != $this->requester_ip) 00210 { 00211 $ws_av = new AuthValidator($this->registered_ip, $this->wsf_graph, $this->uri); 00212 00213 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00214 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00215 00216 $ws_av->process(); 00217 00218 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00219 { 00220 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus()); 00221 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg()); 00222 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt()); 00223 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice, 00224 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description, 00225 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level); 00226 } 00227 } 00228 } 00229 00240 public function pipeline_getError() { return ($this->conneg->error); } 00241 00242 00253 public function pipeline_getResultset() { return ""; } 00254 00267 public function injectDoctype($xmlDoc) 00268 { 00269 $posHeader = strpos($xmlDoc, '"?>') + 3; 00270 $xmlDoc = substr($xmlDoc, 0, $posHeader) 00271 . "\n<!DOCTYPE resultset PUBLIC \"-//Structured Dynamics LLC//Auth Registrar WS DTD 0.1//EN\" \"" 00272 . $this->dtdBaseURL . $this->dtdURL . "\">" . substr($xmlDoc, $posHeader, strlen($xmlDoc) - $posHeader); 00273 00274 return ($xmlDoc); 00275 } 00276 00295 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00296 { 00297 $this->conneg = new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, 00298 AuthRegistrarWs::$supportedSerializations); 00299 00300 // Check for errors 00301 if($this->registered_endpoint == "") 00302 { 00303 $this->conneg->setStatus(400); 00304 $this->conneg->setStatusMsg("Bad Request"); 00305 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name); 00306 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws, 00307 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, "", 00308 $this->errorMessenger->_200->level); 00309 return; 00310 } 00311 00312 if($this->registered_crud_usage == "") 00313 { 00314 $this->conneg->setStatus(400); 00315 $this->conneg->setStatusMsg("Bad Request"); 00316 $this->conneg->setStatusMsgExt($this->errorMessenger->_201->name); 00317 $this->conneg->setError($this->errorMessenger->_201->id, $this->errorMessenger->ws, 00318 $this->errorMessenger->_201->name, $this->errorMessenger->_201->description, "", 00319 $this->errorMessenger->_201->level); 00320 00321 return; 00322 } 00323 00324 if($this->registered_uri == "") 00325 { 00326 $this->conneg->setStatus(400); 00327 $this->conneg->setStatusMsg("Bad Request"); 00328 $this->conneg->setStatusMsgExt($this->errorMessenger->_202->name); 00329 $this->conneg->setError($this->errorMessenger->_202->id, $this->errorMessenger->ws, 00330 $this->errorMessenger->_202->name, $this->errorMessenger->_202->description, "", 00331 $this->errorMessenger->_202->level); 00332 00333 return; 00334 } 00335 00336 // Check if the web service is already registered 00337 $resultset = 00338 $this->db->query($this->db->build_sparql_query("select ?wsf ?crudUsage from <" . $this->wsf_graph 00339 . "> where {?wsf a <http://purl.org/ontology/wsf#WebServiceFramework>. ?wsf <http://purl.org/ontology/wsf#hasWebService> <$this->registered_uri>. <$this->registered_uri> <http://purl.org/ontology/wsf#hasCrudUsage> ?crudUsage.}", 00340 array ('wsf', 'crudUsage'), FALSE)); 00341 00342 if(odbc_error()) 00343 { 00344 $this->conneg->setStatus(500); 00345 $this->conneg->setStatusMsg("Internal Error"); 00346 $this->conneg->setStatusMsgExt($this->errorMessenger->_203->name); 00347 $this->conneg->setError($this->errorMessenger->_203->id, $this->errorMessenger->ws, 00348 $this->errorMessenger->_203->name, $this->errorMessenger->_203->description, "", 00349 $this->errorMessenger->_203->level); 00350 00351 return; 00352 } 00353 elseif(odbc_fetch_row($resultset)) 00354 { 00355 $wsf = odbc_result($resultset, 1); 00356 $crud_usage = odbc_result($resultset, 2); 00357 00358 if($wsf != "" && $crud_usage != "") 00359 { 00360 $this->conneg->setStatus(400); 00361 $this->conneg->setStatusMsg("Bad Request"); 00362 $this->conneg->setStatusMsgExt($this->errorMessenger->_204->name); 00363 $this->conneg->setError($this->errorMessenger->_204->id, $this->errorMessenger->ws, 00364 $this->errorMessenger->_204->name, $this->errorMessenger->_204->description, "", 00365 $this->errorMessenger->_204->level); 00366 00367 unset($resultset); 00368 return; 00369 } 00370 } 00371 00372 unset($resultset); 00373 } 00374 00393 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00394 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); } 00395 00406 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); } 00407 00418 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); } 00419 00432 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); } 00433 00444 public function pipeline_serialize() { return ""; } 00445 00454 public function pipeline_serialize_reification() { return ""; } 00455 00466 public function ws_serialize() { return ""; } 00467 00480 public function ws_respond($content) 00481 { 00482 // First send the header of the request 00483 $this->conneg->respond(); 00484 00485 // second, send the content of the request 00486 00487 // Make sure there is no error. 00488 if($this->conneg->getStatus() == 200) 00489 { 00490 echo $content; 00491 } 00492 00493 $this->__destruct(); 00494 } 00495 00496 00505 public function process() 00506 { 00507 // Make sure there was no conneg error prior to this process call 00508 if($this->conneg->getStatus() == 200) 00509 { 00510 $this->validateQuery(); 00511 00512 // If the query is still valid 00513 if($this->conneg->getStatus() == 200) 00514 { 00515 // Create and describe the resource being registered 00516 // Note: we make sure we remove any previously defined triples that we are about to re-enter in the graph. All information other than these new properties 00517 // will remain in the graph 00518 00519 $query = "delete from <" . $this->wsf_graph . "> 00520 { 00521 <$this->registered_uri> a <http://purl.org/ontology/wsf#WebService> . 00522 <$this->registered_uri> <http://purl.org/dc/terms/title> ?title . 00523 <$this->registered_uri> <http://purl.org/ontology/wsf#endpoint> ?endpoint . 00524 <$this->registered_uri> <http://purl.org/ontology/wsf#hasCrudUsage> ?crud_usage . 00525 ?crud_usage ?crud_property ?crud_value . 00526 } 00527 where 00528 { 00529 graph <" . $this->wsf_graph . "> 00530 { 00531 <$this->registered_uri> a <http://purl.org/ontology/wsf#WebService> . 00532 <$this->registered_uri> <http://purl.org/dc/terms/title> ?title . 00533 <$this->registered_uri> <http://purl.org/ontology/wsf#endpoint> ?endpoint . 00534 <$this->registered_uri> <http://purl.org/ontology/wsf#hasCrudUsage> ?crud_usage . 00535 ?crud_usage ?crud_property ?crud_value . 00536 } 00537 } 00538 insert into <" . $this->wsf_graph . "> 00539 { 00540 <$this->registered_uri> a <http://purl.org/ontology/wsf#WebService> . 00541 <$this->registered_uri> <http://purl.org/dc/terms/title> \"$this->registered_title\" . 00542 <$this->registered_uri> <http://purl.org/ontology/wsf#endpoint> \"$this->registered_endpoint\" . 00543 <$this->registered_uri> <http://purl.org/ontology/wsf#hasCrudUsage> <" . $this->registered_uri . "usage/> . 00544 00545 <" . $this->registered_uri . "usage/> a <http://purl.org/ontology/wsf#CrudUsage> ; 00546 <http://purl.org/ontology/wsf#create> " . ($this->crud_usage->create ? "\"True\"" : "\"False\"") . " ; 00547 <http://purl.org/ontology/wsf#read> " . ($this->crud_usage->read ? "\"True\"" : "\"False\"") . " ; 00548 <http://purl.org/ontology/wsf#update> " . ($this->crud_usage->update ? "\"True\"" : "\"False\"") . " ; 00549 <http://purl.org/ontology/wsf#delete> " . ($this->crud_usage->delete ? "\"True\"" : "\"False\"") . " . 00550 00551 <" . $this->wsf_graph . "> <http://purl.org/ontology/wsf#hasWebService> <$this->registered_uri>. 00552 }"; 00553 00554 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00555 FALSE)); 00556 00557 if(odbc_error()) 00558 { 00559 $this->conneg->setStatus(500); 00560 $this->conneg->setStatusMsg("Internal Error"); 00561 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name); 00562 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws, 00563 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, odbc_errormsg(), 00564 $this->errorMessenger->_300->level); 00565 00566 return; 00567 } 00568 } 00569 } 00570 } 00571 } 00572 00574 00575 ?>
