Request.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @todo This class obviously needs work.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Request
  24. {
  25. protected static $inst;
  26. protected $uri;
  27. protected $sport;
  28. protected $rport;
  29. protected $path;
  30. protected $method;
  31. protected static $secure;
  32. protected $referrer;
  33. protected $userAgent;
  34. protected $IP;
  35. protected $auth;
  36. public function __construct()
  37. {
  38. $this->uri = $_SERVER['REQUEST_URI'];
  39. $this->sport = $_SERVER['SERVER_PORT'];
  40. $this->rport = $_SERVER['REMOTE_PORT'];
  41. $this->method = $_SERVER['REQUEST_METHOD'];
  42. if(array_key_exists('PATH_INFO', $_SERVER))
  43. {
  44. $this->path = trim($_SERVER['PATH_INFO']);
  45. }
  46. $this->IP = $_SERVER['REMOTE_ADDR'];
  47. $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
  48. if(array_key_exists('HTTP_REFERER', $_SERVER))
  49. {
  50. $this->referrer = $_SERVER['HTTP_REFERER'];
  51. }
  52. $this->isSecure();
  53. $this->auth = array();
  54. if(array_key_exists('PHP_AUTH_USER', $_SERVER))
  55. $this->auth['basicuser'] = $_SERVER['PHP_AUTH_USER'];
  56. if(array_key_exists('PHP_AUTH_PW', $_SERVER))
  57. $this->auth['basicpw'] = $_SERVER['PHP_AUTH_PW'];
  58. if(array_key_exists('PHP_AUTH_DIGEST', $_SERVER))
  59. $this->auth['digest'] = $_SERVER['PHP_AUTH_DIGEST'];
  60. }
  61. public function __get($name)
  62. {
  63. $method = 'get' . $name;
  64. if (!method_exists($this, $method))
  65. {
  66. throw new Exception('Request Object Not Found');
  67. }
  68. return $this->$method();
  69. }
  70. public function Get()
  71. {
  72. if (!isset(self::$inst)) {
  73. $c = __CLASS__;
  74. self::$inst = new $c();
  75. }
  76. return self::$inst;
  77. }
  78. /**
  79. * Checks the PHP $_SERVER var for the presence of HTTPS. Returns a boolean.
  80. * @return boolean
  81. */
  82. public static function isSecure()
  83. {
  84. if(!isset(self::$secure))
  85. {
  86. if(array_key_exists('HTTPS', $_SERVER))
  87. {
  88. if($_SERVER['HTTPS'] == 'on')
  89. {
  90. self::$secure = true;
  91. }
  92. else
  93. {
  94. self::$secure = false;
  95. }
  96. }
  97. elseif(array_key_exists('SERVER_PORT', $_SERVER))
  98. {
  99. if($_SERVER['SERVER_PORT'] == '443')
  100. {
  101. self::$secure = true;
  102. }
  103. else
  104. {
  105. self::$secure = false;
  106. }
  107. }
  108. else
  109. {
  110. self::$secure = false;
  111. }
  112. }
  113. return (bool)self::$secure;
  114. }
  115. public function Path()
  116. {
  117. return $this->path;
  118. }
  119. public function URI()
  120. {
  121. return $this->URI();
  122. }
  123. public function IP()
  124. {
  125. return $this->IP;
  126. }
  127. public function Agent()
  128. {
  129. return $this->userAgent;
  130. }
  131. public static function getGETString(array $exclude = array())
  132. {
  133. $getstring = '';
  134. foreach($_GET as $key=>$value)
  135. {
  136. if(!in_array($key, $exclude))
  137. {
  138. if($getstring == '')
  139. {
  140. $getstring = urlencode($key).'='.urlencode($value);
  141. }
  142. else
  143. {
  144. $getstring .= '&'.urlencode($key).'='.urlencode($value);
  145. }
  146. }
  147. }
  148. }
  149. public static function Redirect(Staple_Route $route, array $get = array())
  150. {
  151. $to = (string)$route;
  152. if($get != array())
  153. {
  154. $to .= '?'.Staple_Link::getArraytoString($get);
  155. }
  156. header('Location: '.$to);
  157. exit(0);
  158. }
  159. }