xref: /plugin/siteexport/inc/httpproxy.php (revision 6792d0cf58db367e1e6f5b779f1b1efd0e27751f)
1<?php
2
3/**
4 * i-net software provides programming examples for illustration only,
5 * without warranty either expressed or implied, including, but not
6 * limited to, the implied warranties of merchantability and/or fitness
7 * for a particular purpose. This programming example assumes that you
8 * are familiar with the programming language being demonstrated and the
9 * tools used to create and debug procedures. i-net software support
10 * professionals can help explain the functionality of a particular
11 * procedure, but they will not modify these examples to provide added
12 * functionality or construct procedures to meet your specific needs.
13 * Copyright © i-net software 1998-2010
14 */
15
16/** ********************************************************************
17 * THIS FILE SHOULD NOT BE MODIFIED
18 ******************************************************************** */
19
20if(!defined('DOKU_INC')) die('meh');
21require_once( DOKU_INC . 'inc/HTTPClient.php');
22
23class HTTPProxy extends DokuHTTPClient {
24
25    var $debugClass = null;
26    var $setttings = null;
27
28    /**
29     * Constructor.
30     */
31    function __construct($debug, $settings){
32        global $conf;
33
34        // call parent constructor
35        $this->debugClass = $debug;
36        $this->settings = $settings;
37        parent::__construct();
38
39        $this->timeout = 60; //max. 25 sec
40        $this->headers['If-Modified-Since'] = substr(gmdate('r', 0), 0, -5).'GMT';
41        $this->status = -1;
42        $this->debug = true;
43
44		if ( $this->settings->cookie == null ) {
45			$this->_debug("Has to re-authenticate request.");
46			if ( !$this->authenticate() ) {
47				$this->_debug("Trying other Authentication (auth.php):", auth_setup() && $this->authenticate(true) ? 'authenticated' : 'not authenticated'); // Try again.
48			}
49
50			$this->_debug("Using Authentication:", array('user' => $this->user, 'password' => '*****'));
51
52		} else {
53			$this->cookies = $this->settings->cookie;
54		}
55
56		$this->headers['X-Real-Ip'] = clientIP(true);
57		$this->headers['Accept-Encoding'] = $_SERVER['HTTP_ACCEPT_ENCODING'];
58		$this->headers['Accept-Charset'] = $_SERVER['HTTP_ACCEPT_CHARSET'];
59		$this->agent = $_SERVER['HTTP_USER_AGENT'];
60	}
61
62	/**
63	 * Authenticate using currently logged in user
64	 */
65	private function authenticate($secondAttempt=false) {
66
67		global $auth, $INPUT;
68
69		// Ok, this is evil. We read the login information of the current user and forward it to the HTTPClient
70		list($this->user, $sticky, $this->pass) = auth_getCookie();
71
72		// Logged in in second attempt is now in Session.
73		if ( $secondAttempt && !isset($this->user) && $INPUT->str('u') && $INPUT->str('p') ) {
74
75			// We hacked directly into the login mechanism which provides the login information without encryption via $INPUT
76			$this->user = $INPUT->str('u');
77			$this->pass = $INPUT->str('p');
78			$sticky = $INPUT->str('r');
79		} else {
80			$secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session
81			$this->pass = $this->auth_decrypt($this->pass, $secret);
82		}
83
84		return isset($this->user);
85	}
86
87	/**
88	 * Auth Decryption has changed from Weatherwax to Binky
89	 */
90	private function auth_decrypt($pass, $secret) {
91
92		if ( function_exists('auth_decrypt') ) {
93			// Binky
94			return auth_decrypt($pass, $secret);
95		} else if ( function_exists('PMA_blowfish_decrypt') ) {
96			// Weatherwax
97			return PMA_blowfish_decrypt($pass, $secret);
98		} else {
99			$this->debugClass->runtimeException("No decryption method found");
100		}
101	}
102
103	/**
104	 * Remeber HTTPClient Cookie after successfull authentication
105	 */
106	function sendRequest($url,$data='',$method='GET') {
107
108		$returnCode = parent::sendRequest($url,$data,$method);
109		if ( $this->settings->cookie == null ) {
110			$this->settings->cookie = $this->cookies;
111		}
112
113		return $returnCode;
114	}
115
116	 /**
117	 * print debug info to file if exists
118	 */
119	public function _debug($info,$var=null){
120
121		if ( !$this->debugClass ) {
122			return;
123		}
124
125		$this->debugClass->message("[HTTPClient] " . $info, $var, 1);
126	}
127}
128
129//Setup VIM: ex: et ts=4 enc=utf-8 :