<?php
/*
* JAP4CPANEL (http://jap4cpanel.sourceforge.net/)
* Copyright (C) 2007-2008 Jeroen Van Steirteghem
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class Jap {
var $_path = null;
var $_url1 = null;
var $_url2 = null;
var $_url3 = null;
function Jap($url) {
$this->_path = 'TMP/'.uniqid(rand(), true);
if(preg_match('/^(http[s]?):\/\/((?:[-a-z0-9]+\.){1,}[a-z]+):([0-9]+)$/i', $url, $matches1) != 0) {
$this->_url1 = $matches1[1];
$this->_url2 = $matches1[2];
$this->_url3 = $matches1[3];
}
}
function execute() {
ob_start();
$this->_load($this->_path);
$ch = curl_init();
$this->_processOption1($ch);
$this->_processOption2($ch);
$this->_processOption3($ch);
$this->_processOption4($ch);
$this->_processOption5($ch);
$this->_processOption6($ch);
$this->_processOption7($ch);
$this->_process($ch);
curl_close($ch);
$this->_unload($this->_path);
ob_end_flush();
}
function _processOption1($ch) {
$url = $this->_url1.'://'.$this->_url2.':'.$this->_url3;
// webmail redirection fix
$url = $url.str_replace('/jap/webmail/', '/webmail/', $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_URL, $url);
}
function _processOption2($ch) {
curl_setopt($ch, CURLOPT_HEADER, true);
}
function _processOption3($ch) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
function _processOption4($ch) {
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
}
function _processOption5($ch) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
function _processOption6($ch) {
$__COOKIE = array();
foreach($_COOKIE as $key => $value) {
$this->_processArray($__COOKIE, $key, $value);
}
$__ = null;
foreach($__COOKIE as $key => $value) {
if($__ == null) {
$__ = urlencode($key).'='.urlencode($value);
} else {
$__ = $__.'; '.urlencode($key).'='.urlencode($value);
}
}
curl_setopt($ch, CURLOPT_COOKIE, $__);
}
function _processOption7($ch) {
switch($_SERVER['REQUEST_METHOD']) {
case 'GET':
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, true);
if($_SERVER['CONTENT_TYPE'] == 'application/x-www-form-urlencoded') {
$__POST = array();
foreach($_POST as $key => $value) {
$this->_processArray($__POST, $key, $value);
}
$__ = null;
foreach($__POST as $key => $value) {
if($__ == null) {
$__ = urlencode($key).'='.urlencode($value);
} else {
$__ = $__.'&'.urlencode($key).'='.urlencode($value);
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $__);
} else {
$__POST = array();
foreach($_POST as $key => $value) {
$this->_processArray($__POST, $key, $value);
}
$__FILES1 = array();
$__FILES2 = array();
foreach($_FILES as $key => $value) {
$this->_processArray($__FILES1, $key, $value['name']);
$this->_processArray($__FILES2, $key, $value['tmp_name']);
}
$__ = array();
foreach($__POST as $key => $value) {
$__[$key] = $value;
}
foreach(array_keys($__FILES1) as $key) {
if($__FILES2[$key] != null) {
$this->_load($this->_path.'/'.basename($__FILES2[$key]));
rename($__FILES2[$key], $this->_path.'/'.basename($__FILES2[$key]).'/'.$__FILES1[$key]);
$__[$key] = '@'.$this->_path.'/'.basename($__FILES2[$key]).'/'.$__FILES1[$key];
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $__);
}
break;
}
}
function _processArray(&$array, $key, $value) {
if(is_array($value) == true) {
foreach(array_keys($value) as $key2) {
$this->_processArray($array, $key.'['.$key2.']', $value[$key2]);
}
} else {
$array[$key] = $value;
}
}
function _process($ch) {
$content = curl_exec($ch);
$content1 = substr($content, 0, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$content2 = substr($content, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$contents = preg_split('/[\r]?\n/i', $content1);
$i = count($contents) - 3;
while($i >= 0) {
if($contents[$i] != '') {
$this->_processFunction1($contents[$i]);
$i = $i - 1;
} else {
$i = -1;
}
}
$this->_processFunction2($content2);
}
function _processFunction1($content) {
if(preg_match('/^Location/i', $content) != 0) {
$content = str_replace($this->_url2.':'.$this->_url3, $_SERVER['HTTP_HOST'], $content);
// webmail redirection fix
$content = str_replace('/webmail/', '/jap/webmail/', $content);
header($content);
return;
}
if(preg_match('/^Set-Cookie/i', $content) != 0) {
$content = str_replace($this->_url2.':'.$this->_url3, $_SERVER['HTTP_HOST'], $content);
$content = str_replace('; secure', '', $content);
header($content, false);
return;
}
if(preg_match('/^Transfer-Encoding/i', $content) != 0) {
return;
}
if(preg_match('/^WWW-Authenticate/i', $content) != 0) {
return;
}
header($content);
}
function _processFunction2($content) {
// webmail horde fix
$content = str_replace($this->_url2.':'.$this->_url3.'/horde/', $_SERVER['HTTP_HOST'].'/horde/', $content);
echo $content;
}
function _load($path) {
if(is_dir($path) == false) {
mkdir($path, 0777);
}
}
function _unload($path) {
if(is_dir($path) == true) {
$dh = opendir($path);
while(($name = readdir($dh)) !== false) {
if($name != '.' && $name != '..') {
if(is_file($path.'/'.$name) == true) {
unlink($path.'/'.$name);
} else {
$this->_unload($path.'/'.$name);
}
}
}
closedir($dh);
rmdir($path);
}
}
}
?>