xref: /dokuwiki/inc/HTTP/DokuHTTPClient.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
15a8d6e48SMichael Große<?php
25a8d6e48SMichael Große
35a8d6e48SMichael Großenamespace dokuwiki\HTTP;
45a8d6e48SMichael Große
58553d24dSAndreas Gohruse dokuwiki\Extension\Event;
6*d4f83172SAndreas Gohr
75a8d6e48SMichael Große/**
85a8d6e48SMichael Große * Adds DokuWiki specific configs to the HTTP client
95a8d6e48SMichael Große *
105a8d6e48SMichael Große * @author Andreas Goetz <cpuidle@gmx.de>
118a10b6f0SElan Ruusamäe * @link https://www.dokuwiki.org/devel:httpclient
125a8d6e48SMichael Große */
13a3b08db5SAndreas Gohrclass DokuHTTPClient extends HTTPClient
14a3b08db5SAndreas Gohr{
155a8d6e48SMichael Große    /**
165a8d6e48SMichael Große     * Constructor.
175a8d6e48SMichael Große     *
185a8d6e48SMichael Große     * @author Andreas Gohr <andi@splitbrain.org>
195a8d6e48SMichael Große     */
20a3b08db5SAndreas Gohr    public function __construct()
21a3b08db5SAndreas Gohr    {
225a8d6e48SMichael Große        global $conf;
235a8d6e48SMichael Große
245a8d6e48SMichael Große        // call parent constructor
255a8d6e48SMichael Große        parent::__construct();
265a8d6e48SMichael Große
275a8d6e48SMichael Große        // set some values from the config
285a8d6e48SMichael Große        $this->proxy_host = $conf['proxy']['host'];
295a8d6e48SMichael Große        $this->proxy_port = $conf['proxy']['port'];
305a8d6e48SMichael Große        $this->proxy_user = $conf['proxy']['user'];
315a8d6e48SMichael Große        $this->proxy_pass = conf_decodeString($conf['proxy']['pass']);
325a8d6e48SMichael Große        $this->proxy_ssl = $conf['proxy']['ssl'];
335a8d6e48SMichael Große        $this->proxy_except = $conf['proxy']['except'];
345a8d6e48SMichael Große
355a8d6e48SMichael Große        // allow enabling debugging via URL parameter (if debugging allowed)
365a8d6e48SMichael Große        if ($conf['allowdebug']) {
375a8d6e48SMichael Große            if (
385a8d6e48SMichael Große                isset($_REQUEST['httpdebug']) ||
395a8d6e48SMichael Große                (
405a8d6e48SMichael Große                    isset($_SERVER['HTTP_REFERER']) &&
415a8d6e48SMichael Große                    strpos($_SERVER['HTTP_REFERER'], 'httpdebug') !== false
425a8d6e48SMichael Große                )
435a8d6e48SMichael Große            ) {
445a8d6e48SMichael Große                $this->debug = true;
455a8d6e48SMichael Große            }
465a8d6e48SMichael Große        }
475a8d6e48SMichael Große    }
485a8d6e48SMichael Große
495a8d6e48SMichael Große
505a8d6e48SMichael Große    /**
515a8d6e48SMichael Große     * Wraps an event around the parent function
525a8d6e48SMichael Große     *
535a8d6e48SMichael Große     * @triggers HTTPCLIENT_REQUEST_SEND
545a8d6e48SMichael Große     * @author   Andreas Gohr <andi@splitbrain.org>
555a8d6e48SMichael Große     */
565a8d6e48SMichael Große    /**
575a8d6e48SMichael Große     * @param string $url
585a8d6e48SMichael Große     * @param string|array $data the post data either as array or raw data
595a8d6e48SMichael Große     * @param string $method
605a8d6e48SMichael Große     * @return bool
615a8d6e48SMichael Große     */
62a3b08db5SAndreas Gohr    public function sendRequest($url, $data = '', $method = 'GET')
63a3b08db5SAndreas Gohr    {
64a3b08db5SAndreas Gohr        $httpdata = [
65a3b08db5SAndreas Gohr            'url' => $url,
665a8d6e48SMichael Große            'data' => $data,
67a3b08db5SAndreas Gohr            'method' => $method
68a3b08db5SAndreas Gohr        ];
698553d24dSAndreas Gohr        $evt = new Event('HTTPCLIENT_REQUEST_SEND', $httpdata);
705a8d6e48SMichael Große        if ($evt->advise_before()) {
715a8d6e48SMichael Große            $url = $httpdata['url'];
725a8d6e48SMichael Große            $data = $httpdata['data'];
735a8d6e48SMichael Große            $method = $httpdata['method'];
745a8d6e48SMichael Große        }
755a8d6e48SMichael Große        $evt->advise_after();
765a8d6e48SMichael Große        unset($evt);
775a8d6e48SMichael Große        return parent::sendRequest($url, $data, $method);
785a8d6e48SMichael Große    }
795a8d6e48SMichael Große}
80