1<?php
2/**
3 * Copyright 2007-2016 Horde LLC (http://www.horde.org/)
4 *
5 * @author   Chuck Hagenbuch <chuck@horde.org>
6 * @license  http://www.horde.org/licenses/bsd BSD
7 * @category Horde
8 * @package  Http
9 */
10
11/**
12 * @author   Chuck Hagenbuch <chuck@horde.org>
13 * @license  http://www.horde.org/licenses/bsd BSD
14 * @category Horde
15 * @package  Http
16 *
17 * @property string|Horde_Url $uri   Default URI if not specified for
18 *                                   individual requests.
19 * @property array   $headers        Hash with additional request headers.
20 * @property string  $method         Default request method if not specified
21 *                                   for individual requests.
22 * @property array|string $data      POST data fields or POST/PUT data body.
23 * @property string  $username       Authentication user name.
24 * @property string  $password       Authentication password.
25 * @property string  $authenticationScheme  Authentication method, one of the
26 *                                   Horde_Http::AUTH_* constants.
27 * @property string  $proxyServer    Host name of a proxy server.
28 * @property integer $proxyPort      Port number of a proxy server.
29 * @property integer $proxyType      Proxy server type, one of the
30 *                                   Horde_Http::PROXY_* constants.
31 * @property string  $proxyUsername  Proxy authentication user name.
32 * @property string  $proxyPassword  Proxy authentication password.
33 * @property string  $proxyAuthenticationScheme  Proxy authentication method,
34 *                                   one of the Horde_Http::AUTH_* constants.
35 * @property integer $redirects      Maximum number of redirects to follow.
36 * @property integer $timeout        Timeout in seconds.
37 * @property string $userAgent       User-Agent: request header contents.
38 * @property boolean $verifyPeer     Verify SSL peer certificates?
39 */
40abstract class Horde_Http_Request_Base
41{
42    /**
43     * Request headers
44     * @var array
45     */
46    protected $_headers = array();
47
48    /**
49     * @var array
50     */
51    protected $_options = array();
52
53    /**
54     * Constructor
55     */
56    public function __construct($options = array())
57    {
58        $this->setOptions($options);
59    }
60
61    public function setOptions($options = array())
62    {
63        $this->_options = array_merge($this->getDefaultOptions(), $options);
64    }
65
66    public function getDefaultOptions()
67    {
68        return array(
69            'uri' => null,
70            'method' => 'GET',
71            'data' => null,
72            'username' => '',
73            'password' => '',
74            'authenticationScheme' => Horde_Http::AUTH_ANY,
75            'proxyServer' => null,
76            'proxyPort' => null,
77            'proxyType' => Horde_Http::PROXY_HTTP,
78            'proxyUsername' => null,
79            'proxyPassword' => null,
80            'proxyAuthenticationScheme' => Horde_Http::AUTH_BASIC,
81            'redirects' => 5,
82            'timeout' => 5,
83            'userAgent' => str_replace(' @' . 'version@', '', 'Horde_Http 2.1.7'),
84            'verifyPeer' => true,
85        );
86    }
87
88    /**
89     * Send this HTTP request
90     *
91     * @return Horde_Http_Response_Base
92     */
93    abstract public function send();
94
95    /**
96     * Get an adapter parameter
97     *
98     * @param string $name  The parameter to get.
99     * @return mixed        Parameter value.
100     */
101    public function __get($name)
102    {
103        switch ($name) {
104        case 'headers':
105            return $this->_headers;
106        }
107
108        return isset($this->_options[$name]) ? $this->_options[$name] : null;
109    }
110
111    /**
112     * Set a request parameter
113     *
114     * @param string $name   The parameter to set.
115     * @param mixed  $value  Parameter value.
116     */
117    public function __set($name, $value)
118    {
119        switch ($name) {
120        case 'headers':
121            $this->setHeaders($value);
122            break;
123        }
124
125        $this->_options[$name] = $value;
126    }
127
128    /**
129     * Set one or more headers
130     *
131     * @param mixed $headers A hash of header + value pairs, or a single header name
132     * @param string $value  A header value
133     */
134    public function setHeaders($headers, $value = null)
135    {
136        if (!is_array($headers)) {
137            $headers = array($headers => $value);
138        }
139
140        foreach ($headers as $header => $value) {
141            $this->_headers[$header] = $value;
142        }
143    }
144
145    /**
146     * Get the current value of $header
147     *
148     * @param string $header Header name to get
149     * @return string $header's current value
150     */
151    public function getHeader($header)
152    {
153        return isset($this->_headers[$header]) ? $this->_headers[$header] : null;
154    }
155}
156