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