xref: /plugin/siteexport/inc/httpproxy.php (revision 8f1c46d475f7f73952d3984c8d28eae6196d69ce)
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.
307d101cc1SGerry Weißbach     */
311e0bea35SGerry Weissbach    function __construct($functions){
327d101cc1SGerry Weißbach        global $conf;
338035b959SGerry Weißbach
34*8f1c46d4SGerry Weißbach        // The proxy should only be used if configured.
35*8f1c46d4SGerry Weißbach        // Usually the proxy will allow connections away from the current server.
36*8f1c46d4SGerry Weißbach        // This is what we do not want in most cases.
378035b959SGerry Weißbach        if ( $functions->getConf('useProxy') ) {
388035b959SGerry Weißbach            unset($conf['proxy']);
398035b959SGerry Weißbach        }
407d101cc1SGerry Weißbach
417d101cc1SGerry Weißbach        // call parent constructor
421e0bea35SGerry Weissbach        $this->debugClass = $functions->debug;
431e0bea35SGerry Weissbach        $this->settings = $functions->settings;
447d101cc1SGerry Weißbach        parent::__construct();
457d101cc1SGerry Weißbach
467d101cc1SGerry Weißbach        $this->timeout = 60; //max. 25 sec
472ab96209SGerry Weißbach        $this->headers['If-Modified-Since'] = gmdate('r', 0);
487d101cc1SGerry Weißbach        $this->status = -1;
497d101cc1SGerry Weißbach        $this->debug = true;
50cb1f35bbSGerry Weißbach
51cb1f35bbSGerry Weißbach		if ( $this->settings->cookie == null ) {
52cb1f35bbSGerry Weißbach			$this->_debug("Has to re-authenticate request.");
53cb1f35bbSGerry Weißbach			if ( !$this->authenticate() ) {
541e0bea35SGerry Weissbach
551e0bea35SGerry Weissbach                $this->_debug("Trying other Authentication (auth.php):"); // Try again.
561e0bea35SGerry Weissbach    		    if ( !(auth_setup() && $this->authenticate(true)) )	{
571e0bea35SGerry Weissbach                    $this->_debug("Trying other Authentication (config):", $functions->authenticate() && $this->authenticate(true) ? 'authenticated' : 'not authenticated'); // Try again.
581e0bea35SGerry Weissbach    		    } else {
591e0bea35SGerry Weissbach                    $this->_debug("Ok, using default auth.php"); // Try again.
601e0bea35SGerry Weissbach    		    }
617d101cc1SGerry Weißbach			}
627d101cc1SGerry Weißbach
634c005702SGerry Weißbach			$this->_debug("Using Authentication:", array('user' => $this->user, 'password' => '*****'));
64cb1f35bbSGerry Weißbach
65cb1f35bbSGerry Weißbach		} else {
66cb1f35bbSGerry Weißbach			$this->cookies = $this->settings->cookie;
67cb1f35bbSGerry Weißbach		}
68cb1f35bbSGerry Weißbach
69cb1f35bbSGerry Weißbach		$this->headers['X-Real-Ip'] = clientIP(true);
700cd3f1b4SGerry Weißbach		$this->headers['X-Site-Exporter'] = getSecurityToken();
71cb1f35bbSGerry Weißbach		$this->headers['Accept-Encoding'] = $_SERVER['HTTP_ACCEPT_ENCODING'];
72cb1f35bbSGerry Weißbach		$this->headers['Accept-Charset'] = $_SERVER['HTTP_ACCEPT_CHARSET'];
73cb1f35bbSGerry Weißbach		$this->agent = $_SERVER['HTTP_USER_AGENT'];
74cb1f35bbSGerry Weißbach	}
75cb1f35bbSGerry Weißbach
76cb1f35bbSGerry Weißbach	/**
77cb1f35bbSGerry Weißbach	 * Authenticate using currently logged in user
78cb1f35bbSGerry Weißbach	 */
79cb1f35bbSGerry Weißbach	private function authenticate($secondAttempt=false) {
80cb1f35bbSGerry Weißbach
81cb1f35bbSGerry Weißbach		global $auth, $INPUT;
82cb1f35bbSGerry Weißbach
83cb1f35bbSGerry Weißbach		// Ok, this is evil. We read the login information of the current user and forward it to the HTTPClient
84cb1f35bbSGerry Weißbach		list($this->user, $sticky, $this->pass) = auth_getCookie();
85cb1f35bbSGerry Weißbach
86cb1f35bbSGerry Weißbach		// Logged in in second attempt is now in Session.
87cb1f35bbSGerry Weißbach		if ( $secondAttempt && !isset($this->user) && $INPUT->str('u') && $INPUT->str('p') ) {
88cb1f35bbSGerry Weißbach
89cb1f35bbSGerry Weißbach			// We hacked directly into the login mechanism which provides the login information without encryption via $INPUT
90cb1f35bbSGerry Weißbach			$this->user = $INPUT->str('u');
91cb1f35bbSGerry Weißbach			$this->pass = $INPUT->str('p');
92cb1f35bbSGerry Weißbach			$sticky = $INPUT->str('r');
93cb1f35bbSGerry Weißbach		} else {
94cb1f35bbSGerry Weißbach			$secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session
953de90976SGerry Weißbach			$this->pass = !empty($this->pass) ? $this->auth_decrypt($this->pass, $secret) : '';
96cb1f35bbSGerry Weißbach		}
97cb1f35bbSGerry Weißbach
98cb1f35bbSGerry Weißbach		return isset($this->user);
99cb1f35bbSGerry Weißbach	}
100cb1f35bbSGerry Weißbach
101cb1f35bbSGerry Weißbach	/**
102ec70cd20SGerry Weißbach	 * Auth Decryption has changed from Weatherwax to Binky
103ec70cd20SGerry Weißbach	 */
104ec70cd20SGerry Weißbach	private function auth_decrypt($pass, $secret) {
105ec70cd20SGerry Weißbach
106ec70cd20SGerry Weißbach		if ( function_exists('auth_decrypt') ) {
107ec70cd20SGerry Weißbach			// Binky
108ec70cd20SGerry Weißbach			return auth_decrypt($pass, $secret);
109ec70cd20SGerry Weißbach		} else if ( function_exists('PMA_blowfish_decrypt') ) {
110ec70cd20SGerry Weißbach			// Weatherwax
111ec70cd20SGerry Weißbach			return PMA_blowfish_decrypt($pass, $secret);
112ec70cd20SGerry Weißbach		} else {
113ec70cd20SGerry Weißbach			$this->debugClass->runtimeException("No decryption method found");
114ec70cd20SGerry Weißbach		}
115ec70cd20SGerry Weißbach	}
116ec70cd20SGerry Weißbach
117ec70cd20SGerry Weißbach	/**
118cb1f35bbSGerry Weißbach	 * Remeber HTTPClient Cookie after successfull authentication
119cb1f35bbSGerry Weißbach	 */
120cb1f35bbSGerry Weißbach	function sendRequest($url,$data='',$method='GET') {
121cb1f35bbSGerry Weißbach
122cb1f35bbSGerry Weißbach		$returnCode = parent::sendRequest($url,$data,$method);
123cb1f35bbSGerry Weißbach		if ( $this->settings->cookie == null ) {
124cb1f35bbSGerry Weißbach			$this->settings->cookie = $this->cookies;
125cb1f35bbSGerry Weißbach		}
126cb1f35bbSGerry Weißbach
127cb1f35bbSGerry Weißbach		return $returnCode;
128cb1f35bbSGerry Weißbach	}
1297d101cc1SGerry Weißbach
1307d101cc1SGerry Weißbach	 /**
1317d101cc1SGerry Weißbach	 * print debug info to file if exists
1327d101cc1SGerry Weißbach	 */
1337d101cc1SGerry Weißbach	public function _debug($info,$var=null){
1347d101cc1SGerry Weißbach
1357d101cc1SGerry Weißbach		if ( !$this->debugClass ) {
1367d101cc1SGerry Weißbach			return;
1377d101cc1SGerry Weißbach		}
1387d101cc1SGerry Weißbach
139cb1f35bbSGerry Weißbach		$this->debugClass->message("[HTTPClient] " . $info, $var, 1);
1407d101cc1SGerry Weißbach	}
1417d101cc1SGerry Weißbach}
1427d101cc1SGerry Weißbach
1437d101cc1SGerry Weißbach//Setup VIM: ex: et ts=4 enc=utf-8 :
144