00001 <?php
00002
00005
00025 class Conneg
00026 {
00028 private $mime = "text/plain";
00029
00031 private $charset = "utf-8";
00032
00034 private $encoding = "identity";
00035
00037 private $lang = "en";
00038
00040 private $status = 200;
00041
00043 private $statusMsg = "OK";
00044
00046 private $statusMsgExt = "";
00047
00049 private $supported_serializations = "";
00050
00052 public $error;
00053
00070 function __construct($accept = "", $accept_charset = "", $accept_encoding = "", $accept_language = "",
00071 $supported_serializations)
00072 {
00073 $this->supported_serializations = $supported_serializations;
00074
00075 $this->accept($accept);
00076
00077 if($this->status == 200)
00078 {
00079 $this->accept_charset($accept_charset);
00080 }
00081
00082 if($this->status == 200)
00083 {
00084 $this->accept_encoding($accept_encoding);
00085 }
00086
00087 if($this->status == 200)
00088 {
00089 $this->accept_language($accept_language);
00090 }
00091 }
00092
00093 function __destruct() { }
00094
00103 public function respond()
00104 {
00105 header("HTTP/1.1 " . $this->status . " " . $this->statusMsg);
00106 header("Content-Type: " . $this->mime . "; charset=" . $this->charset);
00107 header("Content-Language: " . $this->lang);
00108 header("Content-Encoding: " . $this->encoding);
00109
00110 if(!isset($this->error))
00111 {
00112 if($this->statusMsgExt != "")
00113 {
00114 echo $this->statusMsgExt . "\n";
00115 }
00116 }
00117 else
00118 {
00119 echo $this->error->getError() . "\n";
00120 }
00121
00122 $this->__destruct();
00123 }
00124
00135 public function getAccept() { return $this->mime; }
00136
00147 public function getAcceptCharset() { return $this->charset; }
00148
00159 public function getAcceptEncoding() { return $this->encoding; }
00160
00171 public function getAcceptLanguage() { return $this->lang; }
00172
00183 public function setStatus($status) { return $this->status = $status; }
00184
00195 public function setStatusMsg($statusMsg) { return $this->statusMsg = $statusMsg; }
00196
00207 public function setStatusMsgExt($statusMsgExt) { return $this->statusMsgExt = $statusMsgExt; }
00208
00226 public function setError($id, $webservice, $name, $description, $debugInfo, $level)
00227 {
00228 include_once("Error.php");
00229 $mime = "text/plain";
00230
00231 switch($this->mime)
00232 {
00233 case "application/rdf+xml":
00234 case "application/xhtml+rdfa":
00235 case "text/xml":
00236 case "text/html":
00237 case "application/sparql-results+xml":
00238 $mime = "text/xml";
00239 break;
00240
00241 case "application/sparql-results+json":
00242 case "application/json":
00243 case "application/iron+json":
00244 case "application/bib+json":
00245 $mime = "application/json";
00246 break;
00247
00248 case "application/rdf+n3":
00249 case "text/tsv":
00250 case "text/csv":
00251 case "application/x-bibtex":
00252 $mime = "text/plain";
00253 break;
00254 }
00255
00256 $this->error = new Error($id, $webservice, $name, $description, $debugInfo, $mime, $level);
00257 }
00258
00269 public function getStatus() { return $this->status; }
00270
00281 public function getStatusMsg() { return $this->statusMsg; }
00282
00293 public function getStatusMsgExt() { return $this->statusMsgExt; }
00294
00304 public function getMime() { return $this->mime; }
00305
00316 public function accept($header)
00317 {
00318 $accepts = array();
00319 $mimes = array();
00320
00321 if(strlen($header) > 0)
00322 {
00323
00324 preg_match_all('/([^,]+)/', $header, $accepts);
00325
00326 foreach($accepts[0] as $accept)
00327 {
00328 $foo = explode(";", str_replace(" ", "", $accept));
00329
00330 if(isset($foo[1]))
00331 {
00332 if(stripos($foo[1], "q=") !== FALSE)
00333 {
00334 $foo[1] = str_replace("q=", "", $foo[1]);
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 if($foo[1] == "1")
00347 {
00348 $foo[1] = "1.1";
00349 }
00350 }
00351 else
00352 {
00353 $foo[1] = "1";
00354 }
00355 }
00356 else
00357 {
00358 array_push($foo, "1");
00359 }
00360
00361 $mimes[$foo[0]] = $foo[1];
00362 }
00363
00364
00365 if(count($mimes) <= 0)
00366 {
00367 $mimes["*/*"] = 1;
00368 }
00369
00370 arsort($mimes, SORT_NUMERIC);
00371
00372 $notAcceptable406 = TRUE;
00373
00374 foreach($mimes as $mime => $q)
00375 {
00376 $mime = strtolower($mime);
00377
00378 if($mime == "application/rdf+xml"
00379 && array_search("application/rdf+xml", $this->supported_serializations) !== FALSE)
00380 {
00381 $this->status = 200;
00382 $this->statusMsg = "OK";
00383 $this->mime = "application/rdf+xml";
00384
00385 $notAcceptable406 = FALSE;
00386
00387 break;
00388 }
00389
00390 if(($mime == "application/rdf+n3"
00391 && array_search("application/rdf+n3", $this->supported_serializations) !== FALSE)
00392 || ($mime == "application/*" && array_search("application/*", $this->supported_serializations) !== FALSE))
00393 {
00394 $this->status = 200;
00395 $this->statusMsg = "OK";
00396 $this->mime = "application/rdf+n3";
00397
00398 $notAcceptable406 = FALSE;
00399
00400 break;
00401 }
00402
00403 if(($mime == "text/plain" && array_search("text/plain", $this->supported_serializations) !== FALSE)
00404 || ($mime == "text/*" && array_search("text/*", $this->supported_serializations) !== FALSE)
00405 || ($mime == "application/*" && array_search("application/*", $this->supported_serializations) !== FALSE)
00406 || ($mime == "*/*" && array_search("*/*", $this->supported_serializations) !== FALSE))
00407 {
00408 $this->status = 200;
00409 $this->statusMsg = "OK";
00410 $this->mime = "text/xml";
00411
00412 $notAcceptable406 = FALSE;
00413
00414 break;
00415 }
00416
00417 if($mime == "application/xhtml+xml"
00418 && array_search("application/xhtml+xml", $this->supported_serializations) !== FALSE)
00419 {
00420 $this->status = 200;
00421 $this->statusMsg = "OK";
00422 $this->mime = "application/xhtml+rdfa";
00423
00424 $notAcceptable406 = FALSE;
00425
00426 break;
00427 }
00428
00429 if($mime == "application/xhtml+rdfa"
00430 && array_search("application/xhtml+rdfa", $this->supported_serializations) !== FALSE)
00431 {
00432 $this->status = 200;
00433 $this->statusMsg = "OK";
00434 $this->mime = "application/xhtml+rdfa";
00435
00436 $notAcceptable406 = FALSE;
00437
00438 break;
00439 }
00440
00441 if($mime == "text/xml" && array_search("text/xml", $this->supported_serializations) !== FALSE)
00442 {
00443 $this->status = 200;
00444 $this->statusMsg = "OK";
00445 $this->mime = "text/xml";
00446
00447 $notAcceptable406 = FALSE;
00448
00449 break;
00450 }
00451
00452 if($mime == "application/xml" && array_search("application/xml", $this->supported_serializations) !== FALSE)
00453 {
00454 $this->status = 200;
00455 $this->statusMsg = "OK";
00456 $this->mime = "application/xml";
00457
00458 $notAcceptable406 = FALSE;
00459
00460 break;
00461 }
00462
00463 if($mime == "text/html" && array_search("text/html", $this->supported_serializations) !== FALSE)
00464 {
00465 $this->status = 200;
00466 $this->statusMsg = "OK";
00467 $this->mime = "text/html";
00468
00469 $notAcceptable406 = FALSE;
00470
00471 break;
00472 }
00473
00474 if($mime == "application/sparql-results+xml"
00475 && array_search("application/sparql-results+xml", $this->supported_serializations) !== FALSE)
00476 {
00477 $this->status = 200;
00478 $this->statusMsg = "OK";
00479 $this->mime = "application/sparql-results+xml";
00480
00481 $notAcceptable406 = FALSE;
00482
00483 break;
00484 }
00485
00486 if($mime == "application/sparql-results+json"
00487 && array_search("application/sparql-results+json", $this->supported_serializations) !== FALSE)
00488 {
00489 $this->status = 200;
00490 $this->statusMsg = "OK";
00491 $this->mime = "application/sparql-results+json";
00492
00493 $notAcceptable406 = FALSE;
00494
00495 break;
00496 }
00497
00498 if($mime == "text/tsv" && array_search("text/tsv", $this->supported_serializations) !== FALSE)
00499 {
00500 $this->status = 200;
00501 $this->statusMsg = "OK";
00502 $this->mime = "text/tsv";
00503
00504 $notAcceptable406 = FALSE;
00505
00506 break;
00507 }
00508
00509 if($mime == "text/csv" && array_search("text/csv", $this->supported_serializations) !== FALSE)
00510 {
00511 $this->status = 200;
00512 $this->statusMsg = "OK";
00513 $this->mime = "text/csv";
00514
00515 $notAcceptable406 = FALSE;
00516
00517 break;
00518 }
00519
00520 if($mime == "application/sparql-results+json"
00521 && array_search("application/sparql-results+json", $this->supported_serializations) !== FALSE)
00522 {
00523 $this->status = 200;
00524 $this->statusMsg = "OK";
00525 $this->mime = "application/sparql-results+json";
00526
00527 $notAcceptable406 = FALSE;
00528
00529 break;
00530 }
00531
00532 if($mime == "application/x-bibtex"
00533 && array_search("application/x-bibtex", $this->supported_serializations) !== FALSE)
00534 {
00535 $this->status = 200;
00536 $this->statusMsg = "OK";
00537 $this->mime = "application/x-bibtex";
00538
00539 $notAcceptable406 = FALSE;
00540
00541 break;
00542 }
00543
00544 if($mime == "application/json" && array_search("application/json", $this->supported_serializations) !== FALSE)
00545 {
00546 $this->status = 200;
00547 $this->statusMsg = "OK";
00548 $this->mime = "application/json";
00549
00550 $notAcceptable406 = FALSE;
00551
00552 break;
00553 }
00554
00555 if($mime == "application/iron+json"
00556 && array_search("application/iron+json", $this->supported_serializations) !== FALSE)
00557 {
00558 $this->status = 200;
00559 $this->statusMsg = "OK";
00560 $this->mime = "application/iron+json";
00561
00562 $notAcceptable406 = FALSE;
00563
00564 break;
00565 }
00566
00567 if($mime == "application/bib+json"
00568 && array_search("application/bib+json", $this->supported_serializations) !== FALSE)
00569 {
00570 $this->status = 200;
00571 $this->statusMsg = "OK";
00572 $this->mime = "application/bib+json";
00573
00574 $notAcceptable406 = FALSE;
00575
00576 break;
00577 }
00578 }
00579
00580 if($notAcceptable406)
00581 {
00582 $this->status = 406;
00583 $this->statusMsg = "Not Acceptable";
00584 $this->statusMsgExt = "Unacceptable mime type requested";
00585 }
00586 }
00587 else
00588 {
00589
00590
00591 $this->status = 200;
00592 $this->statusMsg = "OK";
00593 $this->mime = "application/rdf+xml";
00594 }
00595 }
00596
00607 public function accept_charset($header)
00608 {
00609 $accepts = array();
00610 $charsets = array();
00611
00612 if(strlen($header) > 0)
00613 {
00614
00615 preg_match_all('/([^,]+)/', $header, $accepts);
00616
00617 foreach($accepts[0] as $accept)
00618 {
00619 $foo = explode(";", str_replace(" ", "", $accept));
00620
00621 if(isset($foo[1]))
00622 {
00623 if(stripos($foo[1], "q=") !== FALSE)
00624 {
00625 $foo[1] = str_replace("q=", "", $foo[1]);
00626 }
00627 else
00628 {
00629 $foo[1] = "1";
00630 }
00631 }
00632 else
00633 {
00634 array_push($foo, "1");
00635 }
00636
00637 $charsets[$foo[0]] = $foo[1];
00638 }
00639
00640
00641 if(count($charsets) <= 0)
00642 {
00643 $charsets["*"] = 1;
00644 }
00645
00646 arsort($charsets, SORT_NUMERIC);
00647
00648 $notAcceptable406 = TRUE;
00649
00650 foreach($charsets as $charset => $q)
00651 {
00652 $charset = strtolower($charset);
00653
00654 if($charset == "utf-8" || $charset == "*")
00655 {
00656 $this->status = 200;
00657 $this->statusMsg = "OK";
00658 $this->charset = "utf-8";
00659
00660 $notAcceptable406 = FALSE;
00661
00662 break;
00663 }
00664 }
00665
00666 if($notAcceptable406)
00667 {
00668 $this->status = 406;
00669 $this->statusMsg = "Not Acceptable";
00670 $this->statusMsgExt = "Unacceptable charset requested";
00671 }
00672 }
00673 else
00674 {
00675
00676
00677 $this->status = 200;
00678 $this->statusMsg = "OK";
00679 $this->charset = "utf-8";
00680 }
00681 }
00682
00695 public function accept_encoding($header)
00696 {
00697 $accepts = array();
00698 $encodings = array();
00699
00700 if(strlen($header) > 0)
00701 {
00702
00703 preg_match_all('/([^,]+)/', $header, $accepts);
00704
00705 foreach($accepts[0] as $accept)
00706 {
00707 $foo = explode(";", str_replace(" ", "", $accept));
00708
00709 if(isset($foo[1]))
00710 {
00711 if(stripos($foo[1], "q=") !== FALSE)
00712 {
00713 $foo[1] = str_replace("q=", "", $foo[1]);
00714 }
00715 else
00716 {
00717 $foo[1] = "1";
00718 }
00719 }
00720 else
00721 {
00722 array_push($foo, "1");
00723 }
00724
00725 $encodings[$foo[0]] = $foo[1];
00726 }
00727
00728
00729 if(count($encodings) <= 0)
00730 {
00731 $encodings["*"] = 1;
00732 }
00733
00734 arsort($encodings, SORT_NUMERIC);
00735
00736 $notAcceptable406 = TRUE;
00737
00738 foreach($encodings as $encoding => $q)
00739 {
00740 $encoding = strtolower($encoding);
00741
00742 if($encoding == "identity" || $encoding == "*")
00743 {
00744 $this->status = 200;
00745 $this->statusMsg = "OK";
00746 $this->encoding = "identity";
00747
00748 $notAcceptable406 = FALSE;
00749
00750 break;
00751 }
00752 }
00753
00754 if($notAcceptable406)
00755 {
00756
00757
00758
00759 }
00760 }
00761 else
00762 {
00763
00764
00765 $this->status = 200;
00766 $this->statusMsg = "OK";
00767 $this->encoding = "identity";
00768 }
00769 }
00770
00784 public function accept_language($header)
00785 {
00786 $accepts = array();
00787 $languages = array();
00788
00789 if(strlen($header) > 0)
00790 {
00791
00792 preg_match_all('/([^,]+)/', $header, $accepts);
00793
00794 foreach($accepts[0] as $accept)
00795 {
00796 $foo = explode(";", str_replace(" ", "", $accept));
00797
00798 if(isset($foo[1]))
00799 {
00800 if(stripos($foo[1], "q=") !== FALSE)
00801 {
00802 $foo[1] = str_replace("q=", "", $foo[1]);
00803 }
00804 else
00805 {
00806 $foo[1] = "1";
00807 }
00808 }
00809 else
00810 {
00811 array_push($foo, "1");
00812 }
00813
00814 $languages[$foo[0]] = $foo[1];
00815 }
00816
00817
00818 if(count($languages) <= 0)
00819 {
00820 $languages["*"] = 1;
00821 }
00822
00823 arsort($languages, SORT_NUMERIC);
00824
00825 $notAcceptable406 = TRUE;
00826
00827 foreach($languages as $language => $q)
00828 {
00829 $language = strtolower($language);
00830
00831 if($language == "en" || $language == "*")
00832 {
00833 $this->status = 200;
00834 $this->statusMsg = "OK";
00835 $this->language = "en";
00836
00837 $notAcceptable406 = FALSE;
00838
00839 break;
00840 }
00841 }
00842
00843 if($notAcceptable406)
00844 {
00845
00846
00847
00848 }
00849 }
00850 else
00851 {
00852
00853
00854 $this->status = 200;
00855 $this->statusMsg = "OK";
00856 $this->language = "en";
00857 }
00858 }
00859 }
00860
00862
00863 ?>