1<?php
2/**
3 * Copyright 2007-2016 Horde LLC (http://www.horde.org/)
4 *
5 * @author   Michael Cramer <michael@bigmichi1.de>
6 * @license  http://www.horde.org/licenses/bsd BSD
7 * @category Horde
8 * @package  Http
9 */
10
11/**
12 * @author   Michael Cramer <michael@bigmichi1.de>
13 * @license  http://www.horde.org/licenses/bsd BSD
14 * @category Horde
15 * @package  Http
16 */
17abstract class Horde_Http_Request_PeclhttpBase extends Horde_Http_Request_Base {
18    /**
19     * Map of HTTP authentication schemes from Horde_Http constants to
20     * implementation specific constants.
21     *
22     * @var array
23     */
24    protected $_httpAuthSchemes = array();
25
26    /**
27     * Map of proxy types from Horde_Http to implementation specific constants.
28     *
29     * @var array
30     */
31    protected $_proxyTypes = array();
32
33    /**
34     * Constructor.
35     *
36     * @throws Horde_Http_Exception
37     */
38    public function __construct($args = array())
39    {
40        parent::__construct($args);
41    }
42
43    /**
44     * Translates a Horde_Http::AUTH_* constant to implementation specific
45     * constants.
46     *
47     * @param string $httpAuthScheme  A Horde_Http::AUTH_* constant.
48     *
49     * @return const An implementation specific authentication scheme constant.
50     * @throws Horde_Http_Exception
51     */
52    protected function _httpAuthScheme($httpAuthScheme)
53    {
54        if (!isset($this->_httpAuthSchemes[$httpAuthScheme])) {
55            throw new Horde_Http_Exception('Unsupported authentication scheme (' . $httpAuthScheme . ')');
56        }
57        return $this->_httpAuthSchemes[$httpAuthScheme];
58    }
59
60    /**
61     * Translates a Horde_Http::PROXY_* constant to implementation specific
62     * constants.
63     *
64     * @return const
65     * @throws Horde_Http_Exception
66     */
67    protected function _proxyType()
68    {
69        $proxyType = $this->proxyType;
70        if (!isset($this->_proxyTypes[$proxyType])) {
71            throw new Horde_Http_Exception('Unsupported proxy type (' . $proxyType . ')');
72        }
73        return $this->_proxyTypes[$proxyType];
74    }
75
76    /**
77     * Generates the HTTP options for the request.
78     *
79     * @return array array with options
80     * @throws Horde_Http_Exception
81     */
82    protected function _httpOptions()
83    {
84        // Set options
85        $httpOptions = array(
86            'headers' => $this->headers,
87            'redirect' => (int)$this->redirects,
88            'ssl' => array(
89                'verifypeer' => $this->verifyPeer,
90                'verifyhost' => $this->verifyPeer
91            ),
92            'timeout' => $this->timeout,
93            'useragent' => $this->userAgent
94        );
95
96        // Proxy settings
97        if ($this->proxyServer) {
98            $httpOptions['proxyhost'] = $this->proxyServer;
99            if ($this->proxyPort) {
100                $httpOptions['proxyport'] = $this->proxyPort;
101            }
102            if ($this->proxyUsername && $this->proxyPassword) {
103                $httpOptions['proxyauth'] = $this->proxyUsername . ':' . $this->proxyPassword;
104                $httpOptions['proxyauthtype'] = $this->_httpAuthScheme($this->proxyAuthenticationScheme);
105            }
106            if ($this->proxyType == Horde_Http::PROXY_SOCKS4 || $this->proxyType == Horde_Http::PROXY_SOCKS5) {
107                $httpOptions['proxytype'] = $this->_proxyType();
108            } else if ($this->proxyType != Horde_Http::PROXY_HTTP) {
109                throw new Horde_Http_Exception(sprintf('Proxy type %s not supported by this request type!', $this->proxyType));
110            }
111        }
112
113        // Authentication settings
114        if ($this->username) {
115            $httpOptions['httpauth'] = $this->username . ':' . $this->password;
116            $httpOptions['httpauthtype'] = $this->_httpAuthScheme($this->authenticationScheme);
117        }
118
119        return $httpOptions;
120    }
121}