xref: /plugin/siteexport/inc/httpproxy.php (revision 0cd3f1b43aa8213eaf4461040ca62aafbb931533)
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     */
31cb1f35bbSGerry Weißbach    function __construct($debug, $settings){
327d101cc1SGerry Weißbach        global $conf;
337d101cc1SGerry Weißbach
347d101cc1SGerry Weißbach        // call parent constructor
357d101cc1SGerry Weißbach        $this->debugClass = $debug;
36cb1f35bbSGerry Weißbach        $this->settings = $settings;
377d101cc1SGerry Weißbach        parent::__construct();
387d101cc1SGerry Weißbach
397d101cc1SGerry Weißbach        $this->timeout = 60; //max. 25 sec
402ab96209SGerry Weißbach        $this->headers['If-Modified-Since'] = gmdate('r', 0);
417d101cc1SGerry Weißbach        $this->status = -1;
427d101cc1SGerry Weißbach        $this->debug = true;
43cb1f35bbSGerry Weißbach
44cb1f35bbSGerry Weißbach		if ( $this->settings->cookie == null ) {
45cb1f35bbSGerry Weißbach			$this->_debug("Has to re-authenticate request.");
46cb1f35bbSGerry Weißbach			if ( !$this->authenticate() ) {
47cb1f35bbSGerry Weißbach				$this->_debug("Trying other Authentication (auth.php):", auth_setup() && $this->authenticate(true) ? 'authenticated' : 'not authenticated'); // Try again.
487d101cc1SGerry Weißbach			}
497d101cc1SGerry Weißbach
504c005702SGerry Weißbach			$this->_debug("Using Authentication:", array('user' => $this->user, 'password' => '*****'));
51cb1f35bbSGerry Weißbach
52cb1f35bbSGerry Weißbach		} else {
53cb1f35bbSGerry Weißbach			$this->cookies = $this->settings->cookie;
54cb1f35bbSGerry Weißbach		}
55cb1f35bbSGerry Weißbach
56cb1f35bbSGerry Weißbach		$this->headers['X-Real-Ip'] = clientIP(true);
57*0cd3f1b4SGerry Weißbach		$this->headers['X-Site-Exporter'] = getSecurityToken();
58cb1f35bbSGerry Weißbach		$this->headers['Accept-Encoding'] = $_SERVER['HTTP_ACCEPT_ENCODING'];
59cb1f35bbSGerry Weißbach		$this->headers['Accept-Charset'] = $_SERVER['HTTP_ACCEPT_CHARSET'];
60cb1f35bbSGerry Weißbach		$this->agent = $_SERVER['HTTP_USER_AGENT'];
61cb1f35bbSGerry Weißbach	}
62cb1f35bbSGerry Weißbach
63cb1f35bbSGerry Weißbach	/**
64cb1f35bbSGerry Weißbach	 * Authenticate using currently logged in user
65cb1f35bbSGerry Weißbach	 */
66cb1f35bbSGerry Weißbach	private function authenticate($secondAttempt=false) {
67cb1f35bbSGerry Weißbach
68cb1f35bbSGerry Weißbach		global $auth, $INPUT;
69cb1f35bbSGerry Weißbach
70cb1f35bbSGerry Weißbach		// Ok, this is evil. We read the login information of the current user and forward it to the HTTPClient
71cb1f35bbSGerry Weißbach		list($this->user, $sticky, $this->pass) = auth_getCookie();
72cb1f35bbSGerry Weißbach
73cb1f35bbSGerry Weißbach		// Logged in in second attempt is now in Session.
74cb1f35bbSGerry Weißbach		if ( $secondAttempt && !isset($this->user) && $INPUT->str('u') && $INPUT->str('p') ) {
75cb1f35bbSGerry Weißbach
76cb1f35bbSGerry Weißbach			// We hacked directly into the login mechanism which provides the login information without encryption via $INPUT
77cb1f35bbSGerry Weißbach			$this->user = $INPUT->str('u');
78cb1f35bbSGerry Weißbach			$this->pass = $INPUT->str('p');
79cb1f35bbSGerry Weißbach			$sticky = $INPUT->str('r');
80cb1f35bbSGerry Weißbach		} else {
81cb1f35bbSGerry Weißbach			$secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session
82ec70cd20SGerry Weißbach			$this->pass = $this->auth_decrypt($this->pass, $secret);
83cb1f35bbSGerry Weißbach		}
84cb1f35bbSGerry Weißbach
85cb1f35bbSGerry Weißbach		return isset($this->user);
86cb1f35bbSGerry Weißbach	}
87cb1f35bbSGerry Weißbach
88cb1f35bbSGerry Weißbach	/**
89ec70cd20SGerry Weißbach	 * Auth Decryption has changed from Weatherwax to Binky
90ec70cd20SGerry Weißbach	 */
91ec70cd20SGerry Weißbach	private function auth_decrypt($pass, $secret) {
92ec70cd20SGerry Weißbach
93ec70cd20SGerry Weißbach		if ( function_exists('auth_decrypt') ) {
94ec70cd20SGerry Weißbach			// Binky
95ec70cd20SGerry Weißbach			return auth_decrypt($pass, $secret);
96ec70cd20SGerry Weißbach		} else if ( function_exists('PMA_blowfish_decrypt') ) {
97ec70cd20SGerry Weißbach			// Weatherwax
98ec70cd20SGerry Weißbach			return PMA_blowfish_decrypt($pass, $secret);
99ec70cd20SGerry Weißbach		} else {
100ec70cd20SGerry Weißbach			$this->debugClass->runtimeException("No decryption method found");
101ec70cd20SGerry Weißbach		}
102ec70cd20SGerry Weißbach	}
103ec70cd20SGerry Weißbach
104ec70cd20SGerry Weißbach	/**
105cb1f35bbSGerry Weißbach	 * Remeber HTTPClient Cookie after successfull authentication
106cb1f35bbSGerry Weißbach	 */
107cb1f35bbSGerry Weißbach	function sendRequest($url,$data='',$method='GET') {
108cb1f35bbSGerry Weißbach
109cb1f35bbSGerry Weißbach		$returnCode = parent::sendRequest($url,$data,$method);
110cb1f35bbSGerry Weißbach		if ( $this->settings->cookie == null ) {
111cb1f35bbSGerry Weißbach			$this->settings->cookie = $this->cookies;
112cb1f35bbSGerry Weißbach		}
113cb1f35bbSGerry Weißbach
114cb1f35bbSGerry Weißbach		return $returnCode;
115cb1f35bbSGerry Weißbach	}
1167d101cc1SGerry Weißbach
1177d101cc1SGerry Weißbach	 /**
1187d101cc1SGerry Weißbach	 * print debug info to file if exists
1197d101cc1SGerry Weißbach	 */
1207d101cc1SGerry Weißbach	public function _debug($info,$var=null){
1217d101cc1SGerry Weißbach
1227d101cc1SGerry Weißbach		if ( !$this->debugClass ) {
1237d101cc1SGerry Weißbach			return;
1247d101cc1SGerry Weißbach		}
1257d101cc1SGerry Weißbach
126cb1f35bbSGerry Weißbach		$this->debugClass->message("[HTTPClient] " . $info, $var, 1);
1277d101cc1SGerry Weißbach	}
1287d101cc1SGerry Weißbach}
1297d101cc1SGerry Weißbach
1307d101cc1SGerry Weißbach//Setup VIM: ex: et ts=4 enc=utf-8 :