AuthRegistrarAccess.php
Go to the documentation of this file.
00001 <?php 00002 00005 00027 class AuthRegistrarAccess extends WebService 00028 { 00030 private $db; 00031 00033 private $conneg; 00034 00036 private $dtdURL; 00037 00039 public static $supportedSerializations = 00040 array ("application/json", "application/rdf+xml", "application/rdf+n3", "application/*", "text/xml", "text/*", 00041 "*/*"); 00042 00044 private $registered_ip = ""; 00045 00047 private $crud; 00048 00050 private $ws_uris = array(); 00051 00053 private $dataset = ""; 00054 00056 private $requester_ip = ""; 00057 00059 private $target_access_uri = ""; 00060 00062 private $action = ""; 00063 00065 private $errorMessenger = 00066 '{ 00067 "ws": "/ws/auth/registrar/access/", 00068 "_200": { 00069 "id": "WS-AUTH-REGISTRAR-ACCESS-200", 00070 "level": "Warning", 00071 "name": "Action type undefined", 00072 "description": "No type of \'action\' has been defined for this query." 00073 }, 00074 "_201": { 00075 "id": "WS-AUTH-REGISTRAR-ACCESS-201", 00076 "level": "Warning", 00077 "name": "No IP to register", 00078 "description": "No IP address has been defined for this query." 00079 }, 00080 "_202": { 00081 "id": "WS-AUTH-REGISTRAR-ACCESS-202", 00082 "level": "Warning", 00083 "name": "No crud access defined", 00084 "description": "No crud access have been defined for this query." 00085 }, 00086 "_203": { 00087 "id": "WS-AUTH-REGISTRAR-ACCESS-203", 00088 "level": "Warning", 00089 "name": "No web service URI(s) defined", 00090 "description": "No web service URI(s) have been defined for this query." 00091 }, 00092 "_204": { 00093 "id": "WS-AUTH-REGISTRAR-ACCESS-204", 00094 "level": "Warning", 00095 "name": "No dataset defined", 00096 "description": "No dataset has been defined for this query." 00097 }, 00098 "_205": { 00099 "id": "WS-AUTH-REGISTRAR-ACCESS-205", 00100 "level": "Warning", 00101 "name": "No target Access URI defined for update", 00102 "description": "No target Access URI has been defined to be updated for this query." 00103 }, 00104 "_300": { 00105 "id": "WS-AUTH-REGISTRAR-ACCESS-300", 00106 "level": "Fatal", 00107 "name": "Can\'t create the access to this dataset", 00108 "description": "An error occured when we tried to create the new access to this dataset" 00109 }, 00110 "_301": { 00111 "id": "WS-AUTH-REGISTRAR-ACCESS-301", 00112 "level": "Fatal", 00113 "name": "Can\'t update the access to this dataset", 00114 "description": "An error occured when we tried to update the new access to this dataset" 00115 }, 00116 "_302": { 00117 "id": "WS-AUTH-REGISTRAR-ACCESS-302", 00118 "level": "Fatal", 00119 "name": "Can\'t delete the access to this dataset", 00120 "description": "An error occured when we tried to delete the new access to this dataset" 00121 }, 00122 "_303": { 00123 "id": "WS-AUTH-REGISTRAR-ACCESS-303", 00124 "level": "Fatal", 00125 "name": "Can\'t delete all accesses to this dataset", 00126 "description": "An error occured when we tried to delete all accesses to this dataset" 00127 } 00128 }'; 00129 00155 function __construct($crud, $ws_uris, $dataset, $action, $target_access_uri, $registered_ip, $requester_ip) 00156 { 00157 parent::__construct(); 00158 00159 $this->db = new DB_Virtuoso($this->db_username, $this->db_password, $this->db_dsn, $this->db_host); 00160 00161 $this->registered_ip = $registered_ip; 00162 $this->target_access_uri = $target_access_uri; 00163 00164 $crud = explode(";", $crud); 00165 00166 $this->crud = new CrudUsage((strtolower($crud[0]) == "true" ? TRUE : FALSE), (strtolower($crud[1]) 00167 == "true" ? TRUE : FALSE), (strtolower($crud[2]) == "true" ? TRUE : FALSE), (strtolower($crud[3]) 00168 == "true" ? TRUE : FALSE)); 00169 00170 $this->ws_uris = explode(";", $ws_uris); 00171 $this->dataset = $dataset; 00172 $this->requester_ip = $requester_ip; 00173 $this->action = $action; 00174 00175 if(strtolower(substr($this->registered_ip, 0, 4)) == "self") 00176 { 00177 $pos = strpos($this->registered_ip, "::"); 00178 00179 if($pos !== FALSE) 00180 { 00181 $account = substr($this->registered_ip, $pos + 2, strlen($this->registered_ip) - ($pos + 2)); 00182 00183 $this->registered_ip = $requester_ip . "::" . $account; 00184 } 00185 else 00186 { 00187 $this->registered_ip = $requester_ip; 00188 } 00189 } 00190 00191 $this->uri = $this->wsf_base_url . "/wsf/ws/auth/registrar/access/"; 00192 $this->title = "Authentication Access Registration Web Service"; 00193 $this->crud_usage = new CrudUsage(TRUE, TRUE, FALSE, FALSE); 00194 $this->endpoint = $this->wsf_base_url . "/ws/auth/registrar/access/"; 00195 00196 $this->dtdURL = "auth/authRegistrarAccess.dtd"; 00197 00198 $this->errorMessenger = json_decode($this->errorMessenger); 00199 } 00200 00201 function __destruct() 00202 { 00203 parent::__destruct(); 00204 00205 if(isset($this->db)) 00206 { 00207 @$this->db->close(); 00208 } 00209 } 00210 00221 protected function validateQuery() 00222 { 00223 $ws_av = new AuthValidator($this->requester_ip, $this->wsf_graph, $this->uri); 00224 00225 $ws_av->pipeline_conneg($this->conneg->getAccept(), $this->conneg->getAcceptCharset(), 00226 $this->conneg->getAcceptEncoding(), $this->conneg->getAcceptLanguage()); 00227 00228 $ws_av->process(); 00229 00230 if($ws_av->pipeline_getResponseHeaderStatus() != 200) 00231 { 00232 $this->conneg->setStatus($ws_av->pipeline_getResponseHeaderStatus()); 00233 $this->conneg->setStatusMsg($ws_av->pipeline_getResponseHeaderStatusMsg()); 00234 $this->conneg->setStatusMsgExt($ws_av->pipeline_getResponseHeaderStatusMsgExt()); 00235 $this->conneg->setError($ws_av->pipeline_getError()->id, $ws_av->pipeline_getError()->webservice, 00236 $ws_av->pipeline_getError()->name, $ws_av->pipeline_getError()->description, 00237 $ws_av->pipeline_getError()->debugInfo, $ws_av->pipeline_getError()->level); 00238 } 00239 } 00240 00253 private function uriEncode($uri) 00254 { 00255 $uri = preg_replace("|[^a-zA-z0-9]|", " ", $uri); 00256 $uri = preg_replace("/\s+/", " ", $uri); 00257 $uri = str_replace(" ", "_", $uri); 00258 00259 return ($uri); 00260 } 00261 00272 public function pipeline_getError() { return ($this->conneg->error); } 00273 00284 public function pipeline_getResultset() { return ""; } 00285 00298 public function injectDoctype($xmlDoc) 00299 { 00300 $posHeader = strpos($xmlDoc, '"?>') + 3; 00301 $xmlDoc = substr($xmlDoc, 0, $posHeader) 00302 . "\n<!DOCTYPE resultset PUBLIC \"-//Structured Dynamics LLC//Auth Registrar Access DTD 0.1//EN\" \"" 00303 . $this->dtdBaseURL . $this->dtdURL . "\">" . substr($xmlDoc, $posHeader, strlen($xmlDoc) - $posHeader); 00304 00305 return ($xmlDoc); 00306 } 00307 00326 public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00327 { 00328 $this->conneg = new Conneg($accept, $accept_charset, $accept_encoding, $accept_language, 00329 AuthRegistrarAccess::$supportedSerializations); 00330 00331 if(strtolower($this->action) != "create" && strtolower($this->action) != "delete_target" 00332 && strtolower($this->action) != "delete_all" && strtolower($this->action) != "update") 00333 { 00334 $this->conneg->setStatus(400); 00335 $this->conneg->setStatusMsg("Bad Request"); 00336 $this->conneg->setStatusMsgExt($this->errorMessenger->_200->name); 00337 $this->conneg->setError($this->errorMessenger->_200->id, $this->errorMessenger->ws, 00338 $this->errorMessenger->_200->name, $this->errorMessenger->_200->description, "", 00339 $this->errorMessenger->_200->level); 00340 return; 00341 } 00342 00343 00344 // Check for errors 00345 if($this->registered_ip == "" && strtolower($this->action) != "delete_all") 00346 { 00347 $this->conneg->setStatus(400); 00348 $this->conneg->setStatusMsg("Bad Request"); 00349 $this->conneg->setStatusMsgExt($this->errorMessenger->_201->name); 00350 $this->conneg->setError($this->errorMessenger->_201->id, $this->errorMessenger->ws, 00351 $this->errorMessenger->_201->name, $this->errorMessenger->_201->description, "", 00352 $this->errorMessenger->_201->level); 00353 return; 00354 } 00355 00356 if(strtolower($this->action) != "delete_target" && strtolower($this->action) != "delete_all") 00357 { 00358 // Only need this information for create/update 00359 if($this->crud == "") 00360 { 00361 $this->conneg->setStatus(400); 00362 $this->conneg->setStatusMsg("Bad Request"); 00363 $this->conneg->setStatusMsgExt($this->errorMessenger->_202->name); 00364 $this->conneg->setError($this->errorMessenger->_202->id, $this->errorMessenger->ws, 00365 $this->errorMessenger->_202->name, $this->errorMessenger->_202->description, "", 00366 $this->errorMessenger->_202->level); 00367 00368 return; 00369 } 00370 } 00371 00372 if(strtolower($this->action) != "delete_target" && strtolower($this->action) != "delete_all") 00373 { 00374 // Only need this information for create/update 00375 if(count($this->ws_uris) <= 0 || $this->ws_uris[0] == "") 00376 { 00377 $this->conneg->setStatus(400); 00378 $this->conneg->setStatusMsg("Bad Request"); 00379 $this->conneg->setStatusMsgExt($this->errorMessenger->_203->name); 00380 $this->conneg->setError($this->errorMessenger->_203->id, $this->errorMessenger->ws, 00381 $this->errorMessenger->_203->name, $this->errorMessenger->_203->description, "", 00382 $this->errorMessenger->_203->level); 00383 return; 00384 } 00385 } 00386 00387 if($this->dataset == "") 00388 { 00389 $this->conneg->setStatus(400); 00390 $this->conneg->setStatusMsg("Bad Request"); 00391 $this->conneg->setStatusMsgExt($this->errorMessenger->_204->name); 00392 $this->conneg->setError($this->errorMessenger->_204->id, $this->errorMessenger->ws, 00393 $this->errorMessenger->_204->name, $this->errorMessenger->_204->description, "", 00394 $this->errorMessenger->_204->level); 00395 return; 00396 } 00397 00398 if(strtolower($this->action) == "update" && $this->target_access_uri == "") 00399 { 00400 $this->conneg->setStatus(400); 00401 $this->conneg->setStatusMsg("Bad Request"); 00402 $this->conneg->setStatusMsgExt($this->errorMessenger->_205->name); 00403 $this->conneg->setError($this->errorMessenger->_205->id, $this->errorMessenger->ws, 00404 $this->errorMessenger->_205->name, $this->errorMessenger->_205->description, "", 00405 $this->errorMessenger->_205->level); 00406 return; 00407 } 00408 } 00409 00428 public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language) 00429 { $this->ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); } 00430 00441 public function pipeline_getResponseHeaderStatus() { return $this->conneg->getStatus(); } 00442 00453 public function pipeline_getResponseHeaderStatusMsg() { return $this->conneg->getStatusMsg(); } 00454 00467 public function pipeline_getResponseHeaderStatusMsgExt() { return $this->conneg->getStatusMsgExt(); } 00468 00479 public function pipeline_serialize() { return ""; } 00480 00489 public function pipeline_serialize_reification() { return ""; } 00490 00501 public function ws_serialize() { return ""; } 00502 00515 public function ws_respond($content) 00516 { 00517 // First send the header of the request 00518 $this->conneg->respond(); 00519 00520 // second, send the content of the request 00521 00522 // Make sure there is no error. 00523 if($this->conneg->getStatus() == 200) 00524 { 00525 echo $content; 00526 } 00527 00528 $this->__destruct(); 00529 } 00530 00531 00540 public function process() 00541 { 00542 // Make sure there was no conneg error prior to this process call 00543 if($this->conneg->getStatus() == 200) 00544 { 00545 $this->validateQuery(); 00546 00547 // If the query is still valid 00548 if($this->conneg->getStatus() == 200) 00549 { 00550 if(strtolower($this->action) == "create") 00551 { 00552 // Create and describe the resource being registered 00553 // Note: we make sure we remove any previously defined triples that we are about to re-enter in the graph. 00554 // All information other than these new properties will remain in the graph 00555 00556 $query = "delete from graph <" . $this->wsf_graph . "> 00557 { 00558 ?access a <http://purl.org/ontology/wsf#Access> ; 00559 <http://purl.org/ontology/wsf#registeredIP> \"$this->registered_ip\" ; 00560 <http://purl.org/ontology/wsf#datasetAccess> <$this->dataset> ; 00561 ?p ?o. 00562 } 00563 where 00564 { 00565 ?access a <http://purl.org/ontology/wsf#Access> ; 00566 <http://purl.org/ontology/wsf#registeredIP> \"$this->registered_ip\" ; 00567 <http://purl.org/ontology/wsf#datasetAccess> <$this->dataset> ; 00568 ?p ?o. 00569 } 00570 insert into <" 00571 . $this->wsf_graph . "> 00572 { 00573 <" . $this->wsf_graph . "access/" . md5($this->registered_ip . $this->dataset) 00574 . "> a <http://purl.org/ontology/wsf#Access> ; 00575 <http://purl.org/ontology/wsf#registeredIP> \"$this->registered_ip\" ; 00576 <http://purl.org/ontology/wsf#datasetAccess> <$this->dataset> ;"; 00577 00578 foreach($this->ws_uris as $uri) 00579 { 00580 $query .= "<http://purl.org/ontology/wsf#webServiceAccess> <$uri> ;"; 00581 } 00582 00583 $query .= " <http://purl.org/ontology/wsf#create> " . ($this->crud->create ? "\"True\"" : "\"False\"") . " ; 00584 <http://purl.org/ontology/wsf#read> " . ($this->crud->read ? "\"True\"" : "\"False\"") . " ; 00585 <http://purl.org/ontology/wsf#update> " . ($this->crud->update ? "\"True\"" : "\"False\"") . " ; 00586 <http://purl.org/ontology/wsf#delete> " . ($this->crud->delete ? "\"True\"" : "\"False\"") . " . 00587 }"; 00588 00589 $this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00590 FALSE)); 00591 00592 if(odbc_error()) 00593 { 00594 $this->conneg->setStatus(500); 00595 $this->conneg->setStatusMsg("Internal Error"); 00596 $this->conneg->setStatusMsgExt($this->errorMessenger->_300->name); 00597 $this->conneg->setError($this->errorMessenger->_300->id, $this->errorMessenger->ws, 00598 $this->errorMessenger->_300->name, $this->errorMessenger->_300->description, odbc_errormsg(), 00599 $this->errorMessenger->_300->level); 00600 return; 00601 } 00602 } 00603 elseif(strtolower($this->action) == "update") 00604 { 00605 // Update and describe the resource being registered 00606 00607 $query = "modify graph <" . $this->wsf_graph . "> 00608 delete 00609 { 00610 <$this->target_access_uri> a <http://purl.org/ontology/wsf#Access> ; 00611 ?p ?o. 00612 } 00613 insert 00614 { 00615 <" 00616 . $this->wsf_graph . "access/" . md5($this->registered_ip . $this->dataset) 00617 . "> a <http://purl.org/ontology/wsf#Access> ; 00618 <http://purl.org/ontology/wsf#registeredIP> \"$this->registered_ip\" ; 00619 <http://purl.org/ontology/wsf#datasetAccess> <$this->dataset> ;"; 00620 00621 foreach($this->ws_uris as $uri) 00622 { 00623 $query .= "<http://purl.org/ontology/wsf#webServiceAccess> <$uri> ;"; 00624 } 00625 00626 $query .= " <http://purl.org/ontology/wsf#create> " . ($this->crud->create ? "\"True\"" : "\"False\"") . " ; 00627 <http://purl.org/ontology/wsf#read> " . ($this->crud->read ? "\"True\"" : "\"False\"") . " ; 00628 <http://purl.org/ontology/wsf#update> " . ($this->crud->update ? "\"True\"" : "\"False\"") . " ; 00629 <http://purl.org/ontology/wsf#delete> " . ($this->crud->delete ? "\"True\"" : "\"False\"") 00630 . " . 00631 } 00632 where 00633 { 00634 <$this->target_access_uri> a <http://purl.org/ontology/wsf#Access> ; 00635 ?p ?o. 00636 }"; 00637 00638 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00639 FALSE)); 00640 00641 if(odbc_error()) 00642 { 00643 $this->conneg->setStatus(500); 00644 $this->conneg->setStatusMsg("Internal Error"); 00645 $this->conneg->setStatusMsgExt($this->errorMessenger->_301->name); 00646 $this->conneg->setError($this->errorMessenger->_301->id, $this->errorMessenger->ws, 00647 $this->errorMessenger->_301->name, $this->errorMessenger->_301->description, odbc_errormsg() . $query, 00648 $this->errorMessenger->_301->level); 00649 return; 00650 } 00651 } 00652 elseif(strtolower($this->action) == "delete_target") 00653 { 00654 // Just delete target access 00655 $query = 00656 "delete from graph <" . $this->wsf_graph 00657 . "> 00658 { 00659 ?access a <http://purl.org/ontology/wsf#Access> ; 00660 <http://purl.org/ontology/wsf#registeredIP> \"$this->registered_ip\" ; 00661 <http://purl.org/ontology/wsf#datasetAccess> <$this->dataset> ; 00662 ?p ?o. 00663 } 00664 where 00665 { 00666 ?access a <http://purl.org/ontology/wsf#Access> ; 00667 <http://purl.org/ontology/wsf#registeredIP> \"$this->registered_ip\" ; 00668 <http://purl.org/ontology/wsf#datasetAccess> <$this->dataset> ; 00669 ?p ?o. 00670 }"; 00671 00672 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00673 FALSE)); 00674 00675 if(odbc_error()) 00676 { 00677 $this->conneg->setStatus(500); 00678 $this->conneg->setStatusMsg("Internal Error"); 00679 $this->conneg->setStatusMsgExt($this->errorMessenger->_302->name); 00680 $this->conneg->setError($this->errorMessenger->_302->id, $this->errorMessenger->ws, 00681 $this->errorMessenger->_302->name, $this->errorMessenger->_302->description, odbc_errormsg(), 00682 $this->errorMessenger->_302->level); 00683 return; 00684 } 00685 } 00686 else 00687 { 00688 // Delete all access to a specific dataset 00689 $query = 00690 "delete from graph <" . $this->wsf_graph 00691 . "> 00692 { 00693 ?access ?p ?o. 00694 } 00695 where 00696 { 00697 ?access a <http://purl.org/ontology/wsf#Access> ; 00698 <http://purl.org/ontology/wsf#datasetAccess> <$this->dataset> ; 00699 ?p ?o. 00700 }"; 00701 00702 @$this->db->query($this->db->build_sparql_query(str_replace(array ("\n", "\r", "\t"), " ", $query), array(), 00703 FALSE)); 00704 00705 if(odbc_error()) 00706 { 00707 $this->conneg->setStatus(500); 00708 $this->conneg->setStatusMsg("Internal Error"); 00709 $this->conneg->setError($this->errorMessenger->_303->id, $this->errorMessenger->ws, 00710 $this->errorMessenger->_303->name, $this->errorMessenger->_303->description, odbc_errormsg(), 00711 $this->errorMessenger->_303->level); 00712 return; 00713 } 00714 } 00715 } 00716 } 00717 } 00718 } 00719 00721 00722 ?>
