dmBridge PHP API
DMHTTPResponse.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00015 final class DMHTTPResponse extends DMHTTPMessage {
00016 
00018    private $cookies = array();
00020    private $status_code;
00022    private $status_message;
00023 
00028    private static function getMessageForStatusCode($code) {
00029       $msgs = array(
00030          100 => 'Continue',
00031          101 => 'Switching Protocols',
00032          200 => 'OK',
00033          201 => 'Created',
00034          202 => 'Accepted',
00035          203 => 'Non-Authoritative Information',
00036          204 => 'No Content',
00037          205 => 'Reset Content',
00038          206 => 'Partial Content',
00039          300 => 'Multiple Choices',
00040          301 => 'Moved Permanently',
00041          302 => 'Found',
00042          303 => 'See Other',
00043          304 => 'Not Modified',
00044          305 => 'Use Proxy',
00045          307 => 'Temporary Redirect',
00046          400 => 'Bad Request',
00047          401 => 'Unauthorized',
00048          402 => 'Payment Required',
00049          403 => 'Forbidden',
00050          404 => 'Not Found',
00051          405 => 'Method Not Allowed',
00052          406 => 'Not Acceptable',
00053          407 => 'Proxy Authentication Required',
00054          408 => 'Request Timeout',
00055          409 => 'Conflict',
00056          410 => 'Gone',
00057          411 => 'Length Required',
00058          412 => 'Precondition Failed',
00059          413 => 'Request Entity Too Large',
00060          414 => 'Request-URI Too Long',
00061          415 => 'Unsupported Media Type',
00062          416 => 'Requested Range Not Satisfiable',
00063          417 => 'Expectation Failed',
00064          500 => 'Internal Server Error',
00065          501 => 'Not Implemented',
00066          502 => 'Bad Gateway',
00067          503 => 'Service Unavailable',
00068          504 => 'Gateway Timeout',
00069          505 => 'HTTP Version Not Supported'
00070       );
00071       return array_key_exists($code, $msgs) ? $msgs[$code] : null;
00072    }
00073 
00074    public function __construct() {
00075       $this->setHTTPVersion("1.1");
00076       $this->setStatusCode(200);
00077    }
00078 
00083    public function __toString() {
00084       return (string) $this->getRepresentation()->getBody();
00085    }
00086 
00091    public function addCookie($key, $value) {
00092       $this->cookies[$key] = $value;
00093    }
00094 
00099    public function getCookie($key) {
00100       return array_key_exists($key, $this->cookies)
00101             ? $this->cookies[$key] : null;
00102    }
00103 
00107    public function getCookies() {
00108       return $this->cookies;
00109    }
00110 
00118    public function send() {
00119       $code = $this->getStatusCode();
00120       $message = $this->getStatusMessage()
00121             ? $this->getStatusMessage()
00122             : self::getMessageForStatusCode($code);
00123       header(sprintf("HTTP/%s %d %s",
00124             $this->getHTTPVersion(), $code, $message));
00125 
00126       foreach ($this->getHeaders() as $header) {
00127          header($header['key'] . ": " . $header['value']);
00128       }
00129       header(sprintf("Content-type: %s; charset=%s",
00130             $this->getRepresentation()->getMediaType(),
00131             $this->getRepresentation()->getCharacterSet()));
00132 
00133       $body = $this->getRepresentation()->getBody();
00134       if ($body instanceof DMFile && file_exists($body->getPathname())) {
00135          readfile($body->getPathname());
00136       } else if ($body instanceof DMShellCommand) {
00137          $ph = popen($body->getCommand(), "r");
00138          while (!feof($ph)) {
00139             echo fread($ph, 2096);
00140          }
00141          pclose($ph);
00142       } else {
00143          echo $this->getRepresentation()->getBody();
00144       }
00145    }
00146 
00150    public function getStatusCode() {
00151       return $this->status_code;
00152    }
00153 
00157    public function setStatusCode($code) {
00158       $this->status_code = (int) $code;
00159    }
00160 
00164    public function getStatusMessage() {
00165       return $this->status_message;
00166    }
00167 
00171    public function setStatusMessage($message) {
00172       $this->status_message = $message;
00173    }
00174 
00175 }
 All Data Structures Functions Variables