1<?php
2
3namespace dokuwiki\HTTP;
4
5use dokuwiki\Extension\Event;
6
7/**
8 * Adds DokuWiki specific configs to the HTTP client
9 *
10 * @author Andreas Goetz <cpuidle@gmx.de>
11 * @link https://www.dokuwiki.org/devel:httpclient
12 */
13class DokuHTTPClient extends HTTPClient
14{
15    /**
16     * Constructor.
17     *
18     * @author Andreas Gohr <andi@splitbrain.org>
19     */
20    public function __construct()
21    {
22        global $conf;
23
24        // call parent constructor
25        parent::__construct();
26
27        // set some values from the config
28        $this->proxy_host = $conf['proxy']['host'];
29        $this->proxy_port = $conf['proxy']['port'];
30        $this->proxy_user = $conf['proxy']['user'];
31        $this->proxy_pass = conf_decodeString($conf['proxy']['pass']);
32        $this->proxy_ssl = $conf['proxy']['ssl'];
33        $this->proxy_except = $conf['proxy']['except'];
34
35        // allow enabling debugging via URL parameter (if debugging allowed)
36        if ($conf['allowdebug']) {
37            if (
38                isset($_REQUEST['httpdebug']) ||
39                (
40                    isset($_SERVER['HTTP_REFERER']) &&
41                    strpos($_SERVER['HTTP_REFERER'], 'httpdebug') !== false
42                )
43            ) {
44                $this->debug = true;
45            }
46        }
47    }
48
49
50    /**
51     * Wraps an event around the parent function
52     *
53     * @triggers HTTPCLIENT_REQUEST_SEND
54     * @author   Andreas Gohr <andi@splitbrain.org>
55     */
56    /**
57     * @param string $url
58     * @param string|array $data the post data either as array or raw data
59     * @param string $method
60     * @return bool
61     */
62    public function sendRequest($url, $data = '', $method = 'GET')
63    {
64        $httpdata = [
65            'url' => $url,
66            'data' => $data,
67            'method' => $method
68        ];
69        $evt = new Event('HTTPCLIENT_REQUEST_SEND', $httpdata);
70        if ($evt->advise_before()) {
71            $url = $httpdata['url'];
72            $data = $httpdata['data'];
73            $method = $httpdata['method'];
74        }
75        $evt->advise_after();
76        unset($evt);
77        return parent::sendRequest($url, $data, $method);
78    }
79}
80