WebService.php
Go to the documentation of this file.
00001 <?php 00004 00029 abstract class WebService 00030 { 00032 public static $data_ini = "/data/"; 00033 00035 public static $network_ini = "/usr/share/structwsf/"; 00036 00038 protected $db_username = ""; 00039 00041 protected $db_password = ""; 00042 00044 protected $db_dsn = ""; 00045 00047 protected $db_host = "localhost"; 00048 00050 protected $dtdBaseURL = ""; 00051 00053 protected $wsf_graph = ""; 00054 00056 protected $wsf_base_url = ""; 00057 00059 protected $wsf_base_path = ""; 00060 00062 protected $wsf_local_ip = ""; 00063 00065 protected $wsf_solr_core = ""; 00066 00068 protected $ontologies_files_folder = ""; 00069 00071 protected $solr_host = "localhost"; 00072 00074 protected $ontological_structure_folder = ""; 00075 00077 protected $track_create = FALSE; 00078 00080 protected $track_update = FALSE; 00081 00083 protected $track_delete = FALSE; 00084 00090 protected $tracking_endpoint = ""; 00091 00093 protected $triplestore_port = "8890"; 00094 00096 protected $solr_port = "8983"; 00097 00098 00100 protected $log_table = "SD.WSF.ws_queries_log"; 00101 00107 protected $solr_auto_commit = FALSE; 00108 00113 protected $fields_index_folder = "/tmp/"; 00114 00116 protected $uri; 00117 00119 protected $title; 00120 00122 protected $crud_usage; 00123 00125 protected $endpoint; 00126 00128 protected $owlapiNbSessions; 00129 00131 protected $owlapiBridgeURI; 00132 00133 protected $geoEnabled = FALSE; 00134 00135 function __construct() 00136 { 00137 // Load INI settings 00138 $data_ini = parse_ini_file(self::$data_ini . "data.ini", TRUE); 00139 $network_ini = parse_ini_file(self::$network_ini . "network.ini", TRUE); 00140 00141 // Check if we can read the files 00142 if($data_ini === FALSE || $network_ini === FALSE) 00143 { 00144 // Get the web service reference 00145 $webservice = substr($_SERVER["SCRIPT_NAME"], 0, strrpos($_SERVER["SCRIPT_NAME"], "/") + 1); 00146 00147 // Get the query MIME 00148 $mimes = array(); 00149 00150 $header = $_SERVER['HTTP_ACCEPT']; 00151 00152 if(strlen($header) > 0) 00153 { 00154 // break up string into pieces (languages and q factors) 00155 preg_match_all('/([^,]+)/', $header, $accepts); 00156 00157 foreach($accepts[0] as $accept) 00158 { 00159 $foo = explode(";", str_replace(" ", "", $accept)); 00160 00161 if(isset($foo[1])) 00162 { 00163 if(stripos($foo[1], "q=") !== FALSE) 00164 { 00165 $foo[1] = str_replace("q=", "", $foo[1]); 00166 } 00167 else 00168 { 00169 $foo[1] = "1"; 00170 } 00171 } 00172 else 00173 { 00174 array_push($foo, "1"); 00175 } 00176 00177 $mimes[$foo[0]] = $foo[1]; 00178 } 00179 00180 // In the case that there is a Accept: header, but that it is empty. We set it to: anything. 00181 if(count($mimes) <= 0) 00182 { 00183 $mimes["*/*"] = 1; 00184 } 00185 00186 arsort($mimes, SORT_NUMERIC); 00187 } 00188 00189 $errorMime = ""; 00190 00191 foreach($mimes as $mime => $q) 00192 { 00193 $mime = strtolower($mime); 00194 00195 switch($mime) 00196 { 00197 case "application/rdf+xml": 00198 case "text/xml": 00199 case "application/sparql-results+xml": 00200 case "application/xhtml+rdfa": 00201 case "text/html": 00202 $errorMime = "text/xml"; 00203 break; 00204 00205 case "application/sparql-results+json": 00206 case "application/iron+json": 00207 case "application/json": 00208 case "application/bib+json": 00209 $errorMime = "application/json"; 00210 break; 00211 } 00212 00213 if($errorMime != "") 00214 { 00215 break; 00216 } 00217 } 00218 00219 // Create the error object 00220 include_once("Error.php"); 00221 00222 $error = new Error("HTTP-500", $webservice, "Error", 00223 "Can't read the data.ini and/or the network.ini configuration files on the server.", 00224 "", $errorMime, "Fatal"); 00225 00226 // Return the error according to the requested mime. 00227 header("HTTP/1.1 500 Internal Server Error"); 00228 header("Content-Type: $errorMime"); 00229 00230 echo $error->getError(); 00231 00232 die; 00233 } 00234 00235 if(isset($data_ini["triplestore"]["username"])) 00236 { 00237 $this->db_username = $data_ini["triplestore"]["username"]; 00238 } 00239 00240 if(isset($data_ini["triplestore"]["password"])) 00241 { 00242 $this->db_password = $data_ini["triplestore"]["password"]; 00243 } 00244 if(isset($data_ini["triplestore"]["dsn"])) 00245 { 00246 $this->db_dsn = $data_ini["triplestore"]["dsn"]; 00247 } 00248 if(isset($data_ini["triplestore"]["host"])) 00249 { 00250 $this->db_host = $data_ini["triplestore"]["host"]; 00251 } 00252 if(isset($data_ini["triplestore"]["log_table"])) 00253 { 00254 $this->log_table = $data_ini["triplestore"]["log_table"]; 00255 } 00256 00257 if(isset($data_ini["datasets"]["dtd_base"])) 00258 { 00259 $this->dtdBaseUrl = $data_ini["datasets"]["dtd_base"]; 00260 } 00261 if(isset($data_ini["datasets"]["wsf_graph"])) 00262 { 00263 $this->wsf_graph = $data_ini["datasets"]["wsf_graph"]; 00264 } 00265 00266 if(isset($network_ini["network"]["wsf_base_url"])) 00267 { 00268 $this->wsf_base_url = $network_ini["network"]["wsf_base_url"]; 00269 } 00270 if(isset($network_ini["network"]["wsf_base_path"])) 00271 { 00272 $this->wsf_base_path = $network_ini["network"]["wsf_base_path"]; 00273 } 00274 if(isset($network_ini["network"]["wsf_local_ip"])) 00275 { 00276 $this->wsf_local_ip = $network_ini["network"]["wsf_local_ip"]; 00277 } 00278 00279 00280 if(isset($network_ini["tracking"]["track_create"])) 00281 { 00282 if(strtolower($network_ini["tracking"]["track_create"]) == "true" || $network_ini["tracking"]["track_create"] == "1") 00283 { 00284 $this->track_create = TRUE; 00285 } 00286 } 00287 if(isset($network_ini["tracking"]["track_update"])) 00288 { 00289 if(strtolower($network_ini["tracking"]["track_update"]) == "true" || $network_ini["tracking"]["track_update"] == "1") 00290 { 00291 $this->track_update = TRUE; 00292 } 00293 } 00294 if(isset($network_ini["tracking"]["track_delete"])) 00295 { 00296 if(strtolower($network_ini["tracking"]["track_delete"]) == "true" || $network_ini["tracking"]["track_delete"] == "1") 00297 { 00298 $this->track_delete = TRUE; 00299 } 00300 } 00301 if(isset($network_ini["tracking"]["tracking_endpoint"])) 00302 { 00303 $this->tracking_endpoint = $network_ini["tracking"]["tracking_endpoint"]; 00304 } 00305 00306 00307 if(isset($network_ini["owlapi"]["nb_sessions"])) 00308 { 00309 $this->owlapiNbSessions = $network_ini["owlapi"]["nb_sessions"]; 00310 } 00311 if(isset($network_ini["owlapi"]["bridge_uri"])) 00312 { 00313 $this->owlapiBridgeURI = $network_ini["owlapi"]["bridge_uri"]; 00314 } 00315 00316 if(isset($network_ini["geo"]["geoenabled"])) 00317 { 00318 if(strtolower($network_ini["geo"]["geoenabled"]) == "true" || $network_ini["geo"]["geoenabled"] == "1") 00319 { 00320 $this->geoEnabled = TRUE; 00321 } 00322 } 00323 00324 00325 if(isset($data_ini["solr"]["wsf_solr_core"])) 00326 { 00327 $this->wsf_solr_core = $data_ini["solr"]["wsf_solr_core"]; 00328 } 00329 00330 if(isset($data_ini["solr"]["host"])) 00331 { 00332 $this->solr_host = $data_ini["solr"]["host"]; 00333 } 00334 00335 if(isset($data_ini["ontologies"]["ontologies_files_folder"])) 00336 { 00337 $this->ontologies_files_folder = $data_ini["ontologies"]["ontologies_files_folder"]; 00338 } 00339 if(isset($data_ini["ontologies"]["ontological_structure_folder"])) 00340 { 00341 $this->ontological_structure_folder = $data_ini["ontologies"]["ontological_structure_folder"]; 00342 } 00343 if(isset($data_ini["triplestore"]["port"])) 00344 { 00345 $this->triplestore_port = $data_ini["triplestore"]["port"]; 00346 } 00347 if(isset($data_ini["solr"]["port"])) 00348 { 00349 $this->solr_port = $data_ini["solr"]["port"]; 00350 } 00351 if(isset($data_ini["solr"]["fields_index_folder"])) 00352 { 00353 $this->fields_index_folder = $data_ini["solr"]["fields_index_folder"]; 00354 } 00355 if(strtolower($data_ini["solr"]["solr_auto_commit"]) == "true" || $data_ini["solr"]["solr_auto_commit"] == "1") 00356 { 00357 $this->solr_auto_commit = TRUE; 00358 } 00359 00360 // This handler is defined for the fatal script errors. If a script can't be finished because a fatal error 00361 // occured, then the handleFatalPhpError function is used to return the error message to the requester. 00362 register_shutdown_function("handleFatalPhpError"); 00363 } 00364 00365 function __destruct() { } 00366 00367 00384 abstract public function ws_conneg($accept, $accept_charset, $accept_encoding, $accept_language); 00385 00396 abstract public function ws_serialize(); 00397 00410 abstract public function ws_respond($content); 00411 00420 abstract public function process(); 00421 00422 00439 abstract public function pipeline_conneg($accept, $accept_charset, $accept_encoding, $accept_language); 00440 00451 abstract public function pipeline_getResponseHeaderStatus(); 00452 00463 abstract public function pipeline_getError(); 00464 00465 00476 abstract public function pipeline_getResponseHeaderStatusMsg(); 00477 00490 abstract public function pipeline_getResponseHeaderStatusMsgExt(); 00491 00492 00503 abstract public function pipeline_getResultset(); 00504 00515 abstract public function pipeline_serialize(); 00516 00527 abstract public function pipeline_serialize_reification(); 00528 00541 abstract public function injectDoctype($xmlDoc); 00542 00555 abstract protected function validateQuery(); 00556 00569 public function xmlEncode($string) 00570 { 00571 // Replace all the possible entities by their character. That way, we won't "double encode" 00572 // these entities. Otherwise, we can endup with things such as "&amp;" which some 00573 // XML parsers doesn't seem to like (and throws errors). 00574 $string = str_replace(array ("%5C", "&", "<", ">"), array ("\\", "&", "<", ">"), $string); 00575 00576 return str_replace(array ("\\", "&", "<", ">"), array ("%5C", "&", "<", ">"), $string); 00577 } 00578 00591 public function jsonEncode($string) { return str_replace(array ('\\', '"', "\n", "\r"), array ('\\\\', '\\"', "\\n", "\\r"), $string); } 00592 } 00593 00603 class CrudUsage 00604 { 00606 public $create; 00607 00609 public $read; 00610 00612 public $update; 00613 00615 public $delete; 00616 00617 function __construct($create, $read, $update, $delete) 00618 { 00619 $this->create = $create; 00620 $this->read = $read; 00621 $this->update = $update; 00622 $this->delete = $delete; 00623 } 00624 } 00625 00626 function handleFatalPhpError() 00627 { 00628 include_once("Error.php"); 00629 00630 $last_error = error_get_last(); 00631 00632 if($last_error['type'] === E_ERROR || $last_error['type'] === E_CORE_ERROR || $last_error['type'] === E_COMPILE_ERROR 00633 || $last_error['type'] === E_RECOVERABLE_ERROR || $last_error['type'] === E_USER_ERROR) 00634 { 00635 // Check accept parameter 00636 $mimes = array(); 00637 00638 $header = $_SERVER['HTTP_ACCEPT']; 00639 00640 if(strlen($header) > 0) 00641 { 00642 // break up string into pieces (languages and q factors) 00643 preg_match_all('/([^,]+)/', $header, $accepts); 00644 00645 foreach($accepts[0] as $accept) 00646 { 00647 $foo = explode(";", str_replace(" ", "", $accept)); 00648 00649 if(isset($foo[1])) 00650 { 00651 if(stripos($foo[1], "q=") !== FALSE) 00652 { 00653 $foo[1] = str_replace("q=", "", $foo[1]); 00654 } 00655 else 00656 { 00657 $foo[1] = "1"; 00658 } 00659 } 00660 else 00661 { 00662 array_push($foo, "1"); 00663 } 00664 00665 $mimes[$foo[0]] = $foo[1]; 00666 } 00667 00668 // In the case that there is a Accept: header, but that it is empty. We set it to: anything. 00669 if(count($mimes) <= 0) 00670 { 00671 $mimes["*/*"] = 1; 00672 } 00673 00674 arsort($mimes, SORT_NUMERIC); 00675 } 00676 00677 $errorMime = ""; 00678 00679 foreach($mimes as $mime => $q) 00680 { 00681 $mime = strtolower($mime); 00682 00683 switch($mime) 00684 { 00685 case "application/rdf+xml": 00686 case "text/xml": 00687 case "application/sparql-results+xml": 00688 case "application/xhtml+rdfa": 00689 case "text/html": 00690 $errorMime = "text/xml"; 00691 break; 00692 00693 case "application/sparql-results+json": 00694 case "application/iron+json": 00695 case "application/json": 00696 case "application/bib+json": 00697 $errorMime = "application/json"; 00698 break; 00699 } 00700 00701 if($errorMime != "") 00702 { 00703 break; 00704 } 00705 } 00706 00707 // Check what web service returned the error 00708 $webservice = substr($_SERVER["SCRIPT_NAME"], 0, strrpos($_SERVER["SCRIPT_NAME"], "/") + 1); 00709 00710 $errorName = ""; 00711 00712 switch($last_error['type']) 00713 { 00714 case E_ERROR; 00715 $errorName = "Error"; 00716 break; 00717 00718 case E_CORE_ERROR; 00719 $errorName = "Core Error"; 00720 break; 00721 00722 case E_COMPILE_ERROR; 00723 $errorName = "Compile Error"; 00724 break; 00725 00726 case E_RECOVERABLE_ERROR; 00727 $errorName = "Recoverable Error"; 00728 break; 00729 00730 case E_USER_ERROR; 00731 $errorName = "User Error"; 00732 break; 00733 } 00734 00735 // Create the error object 00736 $error = new Error("HTTP-500", $webservice, $errorName, $last_error['message'], 00737 "[file]: " . $last_error['file'] . "[line]:" . $last_error['line'], $errorMime, "Fatal"); 00738 00739 // Return the error according to the requested mime. 00740 header("HTTP/1.1 500 Internal Server Error"); 00741 header("Content-Type: $errorMime"); 00742 00743 echo $error->getError(); 00744 00745 die; 00746 } 00747 } 00748 00750 00751 ?>
