xref: /plugin/siteexport/inc/httpproxy.php (revision 8035b959c7d8deae0fd2bdc033da29881204b389)
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($functions){
32        global $conf;
33
34        if ( $functions->getConf('useProxy') ) {
35            unset($conf['proxy']);
36        }
37
38        // call parent constructor
39        $this->debugClass = $functions->debug;
40        $this->settings = $functions->settings;
41        parent::__construct();
42
43        $this->timeout = 60; //max. 25 sec
44        $this->headers['If-Modified-Since'] = gmdate('r', 0);
45        $this->status = -1;
46        $this->debug = true;
47
48		if ( $this->settings->cookie == null ) {
49			$this->_debug("Has to re-authenticate request.");
50			if ( !$this->authenticate() ) {
51
52                $this->_debug("Trying other Authentication (auth.php):"); // Try again.
53    		    if ( !(auth_setup() && $this->authenticate(true)) )	{
54                    $this->_debug("Trying other Authentication (config):", $functions->authenticate() && $this->authenticate(true) ? 'authenticated' : 'not authenticated'); // Try again.
55    		    } else {
56                    $this->_debug("Ok, using default auth.php"); // Try again.
57    		    }
58			}
59
60			$this->_debug("Using Authentication:", array('user' => $this->user, 'password' => '*****'));
61
62		} else {
63			$this->cookies = $this->settings->cookie;
64		}
65
66		$this->headers['X-Real-Ip'] = clientIP(true);
67		$this->headers['X-Site-Exporter'] = getSecurityToken();
68		$this->headers['Accept-Encoding'] = $_SERVER['HTTP_ACCEPT_ENCODING'];
69		$this->headers['Accept-Charset'] = $_SERVER['HTTP_ACCEPT_CHARSET'];
70		$this->agent = $_SERVER['HTTP_USER_AGENT'];
71	}
72
73	/**
74	 * Authenticate using currently logged in user
75	 */
76	private function authenticate($secondAttempt=false) {
77
78		global $auth, $INPUT;
79
80		// Ok, this is evil. We read the login information of the current user and forward it to the HTTPClient
81		list($this->user, $sticky, $this->pass) = auth_getCookie();
82
83		// Logged in in second attempt is now in Session.
84		if ( $secondAttempt && !isset($this->user) && $INPUT->str('u') && $INPUT->str('p') ) {
85
86			// We hacked directly into the login mechanism which provides the login information without encryption via $INPUT
87			$this->user = $INPUT->str('u');
88			$this->pass = $INPUT->str('p');
89			$sticky = $INPUT->str('r');
90		} else {
91			$secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session
92			$this->pass = !empty($this->pass) ? $this->auth_decrypt($this->pass, $secret) : '';
93		}
94
95		return isset($this->user);
96	}
97
98	/**
99	 * Auth Decryption has changed from Weatherwax to Binky
100	 */
101	private function auth_decrypt($pass, $secret) {
102
103		if ( function_exists('auth_decrypt') ) {
104			// Binky
105			return auth_decrypt($pass, $secret);
106		} else if ( function_exists('PMA_blowfish_decrypt') ) {
107			// Weatherwax
108			return PMA_blowfish_decrypt($pass, $secret);
109		} else {
110			$this->debugClass->runtimeException("No decryption method found");
111		}
112	}
113
114	/**
115	 * Remeber HTTPClient Cookie after successfull authentication
116	 */
117	function sendRequest($url,$data='',$method='GET') {
118
119		$returnCode = parent::sendRequest($url,$data,$method);
120		if ( $this->settings->cookie == null ) {
121			$this->settings->cookie = $this->cookies;
122		}
123
124		return $returnCode;
125	}
126
127	 /**
128	 * print debug info to file if exists
129	 */
130	public function _debug($info,$var=null){
131
132		if ( !$this->debugClass ) {
133			return;
134		}
135
136		$this->debugClass->message("[HTTPClient] " . $info, $var, 1);
137	}
138}
139
140//Setup VIM: ex: et ts=4 enc=utf-8 :
141