1: <?php
2: /**
3: * @category Horde
4: * @package Controller
5: * @author James Pepin <james@bluestatedigital.com>
6: * @license http://www.horde.org/licenses/bsd BSD
7: */
8: class Horde_Controller_Request_Http implements Horde_Controller_Request
9: {
10: /**
11: * Request path
12: * @var string
13: */
14: protected $_path;
15:
16: /**
17: * All the headers
18: * @var array
19: */
20: protected $_headers = null;
21:
22: public function setPath($path)
23: {
24: $this->_path = $path;
25: }
26:
27: public function getPath()
28: {
29: return $this->_path;
30: }
31:
32: public function getMethod()
33: {
34: $serverVars = $this->getServerVars();
35: return $serverVars['REQUEST_METHOD'];
36: }
37:
38: /**
39: * Gets the request variables GET, POST, COOKIE, SERVER, REQUEST etc.
40: *
41: * @param string $name The name of the superglobal whose vars to return
42: */
43: protected function getVars($name)
44: {
45: return $GLOBALS['_' . $name];
46: }
47:
48: public function getGetVars()
49: {
50: return $this->getVars('GET');
51: }
52:
53: public function getFileVars()
54: {
55: return $this->getVars('FILES');
56: }
57:
58: public function getServerVars()
59: {
60: return $this->getVars('SERVER');
61: }
62:
63: public function getPostVars()
64: {
65: return $this->getVars('POST');
66: }
67:
68: public function getCookieVars()
69: {
70: return $this->getVars('COOKIE');
71: }
72:
73: public function getRequestVars()
74: {
75: return $this->getVars('REQUEST');
76: }
77:
78: public function getSessionId()
79: {
80: //TODO: how do we get session ID?
81: //should probably be passing it in the constructor, or via setSession
82: //we should definitely lazy-load sessions though cause we don't always care about it
83: //perhaps a preFilter to start the session if the controller requests it, and then call setSession on the request
84: //object
85: return 0;
86: }
87:
88: /**
89: * Gets the value of header.
90: *
91: * Returns the value of the specified request header.
92: *
93: * @param string $name the name of the header
94: * @return string the value of the specified header
95: */
96: public function getHeader($name)
97: {
98: if ($this->_headers == null) {
99: $this->_headers = $this->_getAllHeaders();
100: }
101: $name = Horde_String::lower($name);
102: if (isset($this->_headers[$name])) {
103: return $this->_headers[$name];
104: }
105: return null;
106: }
107:
108: /**
109: * Gets all the header names.
110: *
111: * Returns an array of all the header names this request
112: * contains.
113: *
114: * @return array all the available headers as strings
115: */
116: public function getHeaderNames()
117: {
118: if ($this->_headers == null) {
119: $this->_headers = $this->_getAllHeaders();
120: }
121: return array_keys($this->_headers);
122: }
123:
124: /**
125: * Gets all the headers.
126: *
127: * Returns an associative array of all the header names and values of this
128: * request.
129: *
130: * @return array containing all the headers
131: */
132: public function getHeaders()
133: {
134: if ($this->_headers == null) {
135: $this->_headers = $this->_getAllHeaders();
136: }
137: return $this->_headers;
138: }
139:
140: /**
141: * Returns all HTTP_* headers.
142: *
143: * Returns all the HTTP_* headers. Works both if PHP is an apache
144: * module and if it's running as a CGI.
145: *
146: * @return array the headers' names and values
147: */
148: private function _getAllHeaders()
149: {
150: if (function_exists('getallheaders')) {
151: return array_change_key_case(getallheaders(), CASE_LOWER);
152: }
153:
154: $result = array();
155: $server = $this->getServerVars();
156: reset($server);
157: foreach ($server as $key => $value) {
158: $header_name = substr($key, 0, 5);
159: if ($header_name == 'HTTP_') {
160: $hdr = str_replace('_', '-', Horde_String::lower(substr($key, 5)));
161: $result[$hdr] = $value;
162: }
163: }
164:
165: return $result;
166: }
167: }
168: