dmBridge PHP API
DMHTTPRequest.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 
00017 final class DMHTTPRequest extends DMHTTPMessage {
00018 
00020    private static $current;
00022    private $method;
00024    private $password;
00026    private $redirected_uri;
00028    private $session;
00030    private $uri;
00032    private $username;
00033 
00034 
00038    public static function getCurrent() {
00039       if (!self::$current) {
00040          // build the incoming DMHTTPRequest
00041          $method = DMHTTPMethod::GET;
00042          if (array_key_exists("REQUEST_METHOD", $_SERVER)) {
00043             switch ($_SERVER['REQUEST_METHOD']) {
00044             case "POST":
00045                $method = DMHTTPMethod::POST;
00046                break;
00047             }
00048          }
00049          
00050          $uri = self::getHostURI() . $_SERVER['REQUEST_URI'];
00051          $uri = new DMInternalURI($uri);
00052          self::$current =  new DMHTTPRequest($uri, $method);
00053          self::unMagicQuotes($_POST);
00054 
00055          // set representation
00056          $rep = new DMHTTPRepresentation();
00057          self::$current->setRepresentation($rep);
00058          // set form data
00059          foreach ($_POST as $key => $value) {
00060             $rep->addFormData($key, $value);
00061          }
00062          // set session
00063          self::$current->setSession(new DMSession());
00064 
00065          // not using these anymore; let the GC eat them
00066          unset($_GET);
00067          unset($_POST);
00068       }
00069       return self::$current;
00070    }
00071 
00075    private static function getHostURI() {
00076       $http = "";
00077       if (array_key_exists("HTTPS", $_SERVER)) {
00078          $http = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "s" : "";
00079       }
00080       $tmp = explode("/", $_SERVER['SERVER_PROTOCOL']);
00081       $protocol = strtolower($tmp[0]) . $http;
00082       $port = ($_SERVER['SERVER_PORT'] == 80)
00083          ? "" : ":" . $_SERVER['SERVER_PORT'];
00084       return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port;
00085    }
00086 
00095    public static function redirectToParams(
00096          $params, array $query = array()) {
00097       $uri = self::getCurrent()->getURI();
00098       $uri->setParams($params);
00099       if (!count($query)) {
00100          $uri->unsetQuery();
00101       }
00102       foreach ($query as $key => $value) {
00103          $uri->addQueryValue($key, $value);
00104       }
00105 
00106       self::redirectToURI(new DMURI($uri->getAbsoluteURIAsString()));
00107    }
00108 
00116    public static function redirectToURI(DMURI $uri) {
00117       header("Location: " . $uri);
00118    }
00119 
00124    public static function reload() {
00125       self::redirectToURI(self::getCurrent()->getURI());
00126    }
00127 
00136    private static function unMagicQuotes(&$array_or_string) {
00137       if (get_magic_quotes_gpc()) {
00138          if (is_array($array_or_string)) {
00139             foreach ($array_or_string as &$value) {
00140                if (!is_array($value)) {
00141                   $value = stripslashes($value);
00142                } else {
00143                   self::unMagicQuotes($value);
00144                }
00145             }
00146          }
00147       }
00148    }
00149 
00154    public function __construct(DMURI $uri = null,
00155          $method = DMHTTPMethod::GET) {
00156       $this->setRepresentation(new DMHTTPRepresentation());
00157       if (!is_null($method)) {
00158          $this->setMethod($method);
00159       }
00160       if (!is_null($uri)) {
00161          $this->setURI($uri);
00162       }
00163    }
00164 
00168    public function getMethod() {
00169       return $this->method;
00170    }
00171 
00175    public function setMethod($method) {
00176       $this->method = $method;
00177    }
00178 
00182    public function getPassword() {
00183       return $this->password;
00184    }
00185 
00189    public function setPassword($password) {
00190       $this->password = $password;
00191    }
00192 
00198    public function getRedirectedURI() {
00199       return $this->redirected_uri ? clone $this->redirected_uri : null;
00200    }
00201 
00206    public function setRedirectedURI(DMURI $uri) {
00207       $this->redirected_uri = $uri;
00208    }
00209 
00214    public function getSession() {
00215       return $this->session;
00216    }
00217 
00222    public function setSession(DMSession $session) {
00223       $this->session = $session;
00224    }
00225 
00230    public function getURI() {
00231       return clone $this->uri;
00232    }
00233 
00238    public function setURI(DMURI $uri) {
00239       $this->uri = $uri;
00240    }
00241 
00245    public function getUsername() {
00246       return $this->username;
00247    }
00248 
00252    public function setUsername($username) {
00253       $this->username = $username;
00254    }
00255 
00256 }
 All Data Structures Functions Variables