xref: /plugin/siteexport/inc/httpproxy.php (revision 0571ece201b9e3bc14846f6c88d943a4f1512014)
17d101cc1SGerry Weißbach<?php
27d101cc1SGerry Weißbach
37d101cc1SGerry Weißbach/**
47d101cc1SGerry Weißbach * i-net software provides programming examples for illustration only,
57d101cc1SGerry Weißbach * without warranty either expressed or implied, including, but not
67d101cc1SGerry Weißbach * limited to, the implied warranties of merchantability and/or fitness
77d101cc1SGerry Weißbach * for a particular purpose. This programming example assumes that you
87d101cc1SGerry Weißbach * are familiar with the programming language being demonstrated and the
97d101cc1SGerry Weißbach * tools used to create and debug procedures. i-net software support
107d101cc1SGerry Weißbach * professionals can help explain the functionality of a particular
117d101cc1SGerry Weißbach * procedure, but they will not modify these examples to provide added
127d101cc1SGerry Weißbach * functionality or construct procedures to meet your specific needs.
136792d0cfSGerry Weißbach * Copyright © i-net software 1998-2010
147d101cc1SGerry Weißbach */
157d101cc1SGerry Weißbach
167d101cc1SGerry Weißbach/** ********************************************************************
177d101cc1SGerry Weißbach * THIS FILE SHOULD NOT BE MODIFIED
187d101cc1SGerry Weißbach ******************************************************************** */
197d101cc1SGerry Weißbach
207d101cc1SGerry Weißbachif (!defined('DOKU_INC')) die('meh');
217d101cc1SGerry Weißbachrequire_once(DOKU_INC . 'inc/HTTPClient.php');
227d101cc1SGerry Weißbach
237d101cc1SGerry Weißbachclass HTTPProxy extends DokuHTTPClient {
247d101cc1SGerry Weißbach
257d101cc1SGerry Weißbach    var $debugClass = null;
26cb1f35bbSGerry Weißbach    var $setttings = null;
277d101cc1SGerry Weißbach
287d101cc1SGerry Weißbach    /**
297d101cc1SGerry Weißbach     * Constructor.
30*0571ece2SScrutinizer Auto-Fixer     * @param siteexport_functions $functions
317d101cc1SGerry Weißbach     */
321e0bea35SGerry Weissbach    function __construct($functions) {
337d101cc1SGerry Weißbach        global $conf;
348035b959SGerry Weißbach
358f1c46d4SGerry Weißbach        // The proxy should only be used if configured.
368f1c46d4SGerry Weißbach        // Usually the proxy will allow connections away from the current server.
378f1c46d4SGerry Weißbach        // This is what we do not want in most cases.
388035b959SGerry Weißbach        if ($functions->getConf('useProxy')) {
398035b959SGerry Weißbach            unset($conf['proxy']);
408035b959SGerry Weißbach        }
417d101cc1SGerry Weißbach
427d101cc1SGerry Weißbach        // call parent constructor
431e0bea35SGerry Weissbach        $this->debugClass = $functions->debug;
441e0bea35SGerry Weissbach        $this->settings = $functions->settings;
457d101cc1SGerry Weißbach        parent::__construct();
467d101cc1SGerry Weißbach
477d101cc1SGerry Weißbach        $this->timeout = 60; //max. 25 sec
482ab96209SGerry Weißbach        $this->headers['If-Modified-Since'] = gmdate('r', 0);
497d101cc1SGerry Weißbach        $this->status = -1;
507d101cc1SGerry Weißbach        $this->debug = true;
51cb1f35bbSGerry Weißbach
52cb1f35bbSGerry Weißbach		if ($this->settings->cookie == null) {
53cb1f35bbSGerry Weißbach			$this->_debug("Has to re-authenticate request.");
54cb1f35bbSGerry Weißbach			if (!$this->authenticate()) {
551e0bea35SGerry Weissbach
561e0bea35SGerry Weissbach                $this->_debug("Trying other Authentication (auth.php):"); // Try again.
571e0bea35SGerry Weissbach    		    if (!(auth_setup() && $this->authenticate(true))) {
581e0bea35SGerry Weissbach                    $this->_debug("Trying other Authentication (config):", $functions->authenticate() && $this->authenticate(true) ? 'authenticated' : 'not authenticated'); // Try again.
591e0bea35SGerry Weissbach    		    } else {
601e0bea35SGerry Weissbach                    $this->_debug("Ok, using default auth.php"); // Try again.
611e0bea35SGerry Weissbach    		    }
627d101cc1SGerry Weißbach			}
637d101cc1SGerry Weißbach
644c005702SGerry Weißbach			$this->_debug("Using Authentication:", array('user' => $this->user, 'password' => '*****'));
65cb1f35bbSGerry Weißbach
66cb1f35bbSGerry Weißbach		} else {
67cb1f35bbSGerry Weißbach			$this->cookies = $this->settings->cookie;
68cb1f35bbSGerry Weißbach		}
69cb1f35bbSGerry Weißbach
70cb1f35bbSGerry Weißbach		$this->headers['X-Real-Ip'] = clientIP(true);
710cd3f1b4SGerry Weißbach		$this->headers['X-Site-Exporter'] = getSecurityToken();
72cb1f35bbSGerry Weißbach		$this->headers['Accept-Encoding'] = $_SERVER['HTTP_ACCEPT_ENCODING'];
73cb1f35bbSGerry Weißbach		$this->headers['Accept-Charset'] = $_SERVER['HTTP_ACCEPT_CHARSET'];
74cb1f35bbSGerry Weißbach		$this->agent = $_SERVER['HTTP_USER_AGENT'];
75cb1f35bbSGerry Weißbach	}
76cb1f35bbSGerry Weißbach
77cb1f35bbSGerry Weißbach	/**
78cb1f35bbSGerry Weißbach	 * Authenticate using currently logged in user
79cb1f35bbSGerry Weißbach	 */
80cb1f35bbSGerry Weißbach	private function authenticate($secondAttempt = false) {
81cb1f35bbSGerry Weißbach
82cb1f35bbSGerry Weißbach		global $auth, $INPUT;
83cb1f35bbSGerry Weißbach
84cb1f35bbSGerry Weißbach		// Ok, this is evil. We read the login information of the current user and forward it to the HTTPClient
85cb1f35bbSGerry Weißbach		list($this->user, $sticky, $this->pass) = auth_getCookie();
86cb1f35bbSGerry Weißbach
87cb1f35bbSGerry Weißbach		// Logged in in second attempt is now in Session.
88cb1f35bbSGerry Weißbach		if ($secondAttempt && !isset($this->user) && $INPUT->str('u') && $INPUT->str('p')) {
89cb1f35bbSGerry Weißbach
90cb1f35bbSGerry Weißbach			// We hacked directly into the login mechanism which provides the login information without encryption via $INPUT
91cb1f35bbSGerry Weißbach			$this->user = $INPUT->str('u');
92cb1f35bbSGerry Weißbach			$this->pass = $INPUT->str('p');
93cb1f35bbSGerry Weißbach			$sticky = $INPUT->str('r');
94cb1f35bbSGerry Weißbach		} else {
95cb1f35bbSGerry Weißbach			$secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session
963de90976SGerry Weißbach			$this->pass = !empty($this->pass) ? $this->auth_decrypt($this->pass, $secret) : '';
97cb1f35bbSGerry Weißbach		}
98cb1f35bbSGerry Weißbach
99cb1f35bbSGerry Weißbach		return isset($this->user);
100cb1f35bbSGerry Weißbach	}
101cb1f35bbSGerry Weißbach
102cb1f35bbSGerry Weißbach	/**
103ec70cd20SGerry Weißbach	 * Auth Decryption has changed from Weatherwax to Binky
104ec70cd20SGerry Weißbach	 */
105ec70cd20SGerry Weißbach	private function auth_decrypt($pass, $secret) {
106ec70cd20SGerry Weißbach
107ec70cd20SGerry Weißbach		if (function_exists('auth_decrypt')) {
108ec70cd20SGerry Weißbach			// Binky
109ec70cd20SGerry Weißbach			return auth_decrypt($pass, $secret);
110ec70cd20SGerry Weißbach		} else if (function_exists('PMA_blowfish_decrypt')) {
111ec70cd20SGerry Weißbach			// Weatherwax
112ec70cd20SGerry Weißbach			return PMA_blowfish_decrypt($pass, $secret);
113ec70cd20SGerry Weißbach		} else {
114ec70cd20SGerry Weißbach			$this->debugClass->runtimeException("No decryption method found");
115ec70cd20SGerry Weißbach		}
116ec70cd20SGerry Weißbach	}
117ec70cd20SGerry Weißbach
118ec70cd20SGerry Weißbach	/**
119cb1f35bbSGerry Weißbach	 * Remeber HTTPClient Cookie after successfull authentication
120cb1f35bbSGerry Weißbach	 */
121cb1f35bbSGerry Weißbach	function sendRequest($url, $data = '', $method = 'GET') {
122cb1f35bbSGerry Weißbach
123cb1f35bbSGerry Weißbach		$returnCode = parent::sendRequest($url, $data, $method);
124cb1f35bbSGerry Weißbach		if ($this->settings->cookie == null) {
125cb1f35bbSGerry Weißbach			$this->settings->cookie = $this->cookies;
126cb1f35bbSGerry Weißbach		}
127cb1f35bbSGerry Weißbach
128cb1f35bbSGerry Weißbach		return $returnCode;
129cb1f35bbSGerry Weißbach	}
1307d101cc1SGerry Weißbach
1317d101cc1SGerry Weißbach	 /**
1327d101cc1SGerry Weißbach	 * print debug info to file if exists
133*0571ece2SScrutinizer Auto-Fixer	 * @param string $info
1347d101cc1SGerry Weißbach	 */
1357d101cc1SGerry Weißbach	public function _debug($info, $var = null) {
1367d101cc1SGerry Weißbach
1377d101cc1SGerry Weißbach		if (!$this->debugClass) {
1387d101cc1SGerry Weißbach			return;
1397d101cc1SGerry Weißbach		}
1407d101cc1SGerry Weißbach
141cb1f35bbSGerry Weißbach		$this->debugClass->message("[HTTPClient] " . $info, $var, 1);
1427d101cc1SGerry Weißbach	}
1437d101cc1SGerry Weißbach}
1447d101cc1SGerry Weißbach
1457d101cc1SGerry Weißbach//Setup VIM: ex: et ts=4 enc=utf-8 :
146