Skip to content

Commit

Permalink
first commit from php-proxy-script
Browse files Browse the repository at this point in the history
  • Loading branch information
Athlon1600 committed May 10, 2015
1 parent 6ac6348 commit dfe9fad
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/*
composer.lock
.htaccess
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "athlon1600/php-proxy-app",
"type": "project",
"version": "1.0.0",
"keywords": ["php proxy", "php proxy application", "php proxy web", "proxy script", "php web proxy", "web proxy"],
"homepage": "https://www.php-proxy.com/",
"require": {
"athlon1600/php-proxy": "@dev"
}
}
37 changes: 37 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

// all possible options will be stored
$config = array();

// make it as long as possible for extra security... secret key is being used when encrypting urls
$config['secret_key'] = '';

// plugins to load - plugins will be loaded in this exact order as in array
$config['plugins'] = array(
'Test',
'Stream',
'HeaderRewrite',
'Cookie',
'Proxify',
// site specific plugins
'Youtube',
'DailyMotion',
'RedTube',
'XHamster',
'XVideos',
'Twitter'
);

// additional curl options to go with each request
$config['curl'] = array(
//CURLOPT_INTERFACE => '123.123.123.13',
//CURLOPT_USERAGENT => 'Firefox 5000'
);

//$config['error_redirect'] = "https://unblockvideos.com/#error={error_msg}";
//$config['index_redirect'] = 'https://unblockvideos.com/';

// this better be here other Config::load fails
return $config;

?>
142 changes: 142 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

require("vendor/autoload.php");

define('PROXY_START', microtime(true));
define('SCRIPT_BASE', (!empty($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
define('SCRIPT_DIR', pathinfo(SCRIPT_BASE, PATHINFO_DIRNAME).'/');

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;

use Proxy\Plugin\AbstractPlugin;
use Proxy\Event\FilterEvent;
use Proxy\Config;
use Proxy\Proxy;

// load config...
Config::load('./config.php');

// form submit in progress...
if(isset($_POST['url'])){

$url = $_POST['url'];
$url = add_http($url);

header("HTTP/1.1 302 Found");
header('Location: '.SCRIPT_BASE.'?q='.encrypt_url($url));
exit;

} else if(!isset($_GET['q'])){

// must be at homepage - should we redirect somewhere else?
if(Config::get('index_redirect')){

// redirect to...
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".Config::get('index_redirect'));

} else {
echo render_template("./templates/main.php", array('script_base' => SCRIPT_BASE, 'version' => Proxy::VERSION));
}

exit;
}


// get real URL
$url = decrypt_url($_GET['q']);
define('URL', $url);


$proxy = new Proxy();


// load plugins
foreach(Config::get('plugins', array()) as $plugin){

$plugin_class = $plugin.'Plugin';

if(file_exists('./plugins/'.$plugin_class.'.php')){

// use user plugin from /plugins/
require_once('./plugins/'.$plugin_class.'.php');

} else {

// use native plugin from php-proxy - it was already loaded into namespace automatically through composer
$plugin_class = '\\Proxy\\Plugin\\'.$plugin_class;
}

$proxy->getEventDispatcher()->addSubscriber(new $plugin_class());
}

// provide URL form
$proxy->getEventDispatcher()->addListener('request.complete', function($event){

$request = $event['request'];
$response = $event['response'];

$url = $request->getUri();

// we attach url_form only if this is a html response
if(!is_html($response->headers->get('content-type'))){
return;
}

$url_form = render_template("./templates/url_form.php", array(
'url' => $url,
'script_base' => SCRIPT_BASE
));

$output = $response->getContent();

// does the html page contain <body> tag, if so insert our form right after <body> tag starts
$output = preg_replace('@<body.*?>@is', '$0'.PHP_EOL.$url_form, $output, 1, $count);

// <body> tag was not found, just put the form at the top of the page
if($count == 0){
$output = $url_form.$output;
}

$response->setContent($output);
});


try {

// request sent to index.php
$request = Request::createFromGlobals();

// forward it to some other URL
$response = $proxy->forward($request, $url);

// if that was a streaming response, then everything was already sent and script will be killed before it even reaches this line
$response->send();

} catch (Exception $ex){

// if the site is on server2.proxy.com then you may wish to redirect it back to proxy.com
if(Config::get("error_redirect")){

$url = render_string(Config::get("error_redirect"), array(
'error_msg' => rawurlencode($ex->getMessage())
));

header("HTTP/1.1 302 Found");
header("Location: {$url}");

} else {

echo render_template("./templates/main.php", array(
'url' => $url,
'script_base' => SCRIPT_BASE,
'error_msg' => $ex->getMessage(),
'version' => Proxy::VERSION
));

}
}

?>
25 changes: 25 additions & 0 deletions plugins/TestPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Proxy\Plugin\AbstractPlugin;
use Proxy\Event\ProxyEvent;

class TestPlugin extends AbstractPlugin {

public function onBeforeRequest(ProxyEvent $event){
// fired right before a request is being sent to a proxy
}

public function onHeadersReceived(ProxyEvent $event){
// fired right after response headers have been fully received - last chance to modify before sending it back to the user
}

public function onCurlWrite(ProxyEvent $event){
// fired as the data is being written piece by piece
}

public function onCompleted(ProxyEvent $event){
// fired after the full response=headers+body has been read - will only be called on "non-streaming" responses
}
}

?>
90 changes: 90 additions & 0 deletions templates/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html>
<head>

<title>PHP-Proxy</title>

<meta name="generator" content="php-proxy.com">
<meta name="version" content="<?=$version;?>">

<style type="text/css">
html body {
font-family: Arial,Helvetica,sans-serif;
font-size: 12px;
}

#container {
width:500px;
margin:0 auto;
margin-top:150px;
}

#error {
color:red;
font-weight:bold;
}

#frm {
padding:10px 15px;
background-color:#FFC8C8;

border:1px solid #818181;

-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
}

#footer {
text-align:center;
font-size:10px;
margin-top:35px;
clear:both;
}
</style>

</head>

<body>


<div id="container">

<div style="text-align:center;">
<h1 style="color:blue;">PHP-Proxy</h1>
</div>

<?php if(isset($error_msg)){ ?>

<div id="error">
<p><?php echo $error_msg; ?></p>
</div>

<?php } ?>

<div id="frm">

<!-- I wouldn't touch this part -->

<form action="index.php" method="post" style="margin-bottom:0;">
<input name="url" type="text" style="width:400px;" autocomplete="off" placeholder="http://" />
<input type="submit" value="Go" />
</form>

<script type="text/javascript">
document.getElementsByName("url")[0].focus();
</script>

<!-- [END] -->

</div>

</div>

<div id="footer">
Powered by <a href="//www.php-proxy.com/" target="_blank">PHP-Proxy</a> <?php echo $version; ?>
</div>


</body>
</html>
62 changes: 62 additions & 0 deletions templates/url_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

<style type="text/css">

html body {
margin-top: 50px !important;
}

#top_form {
position: fixed;
top:0;
left:0;
width: 100%;

margin:0;

z-index: 2100000000;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;

border-bottom:1px solid #151515;

background:#FFC8C8;

height:45px;
line-height:45px;
}

#top_form input[name=url] {
width: 550px;
height: 20px;
padding: 5px;
font: 13px "Helvetica Neue",Helvetica,Arial,sans-serif;
border: 0px none;
background: none repeat scroll 0% 0% #FFF;
}



</style>

<script src="//www.php-proxy.com/assets/url_form.js"></script>

<div id="top_form">

<div style="width:800px; margin:0 auto;">

<form method="post" action="index.php" target="_top" style="margin:0; padding:0;">
<input type="button" value="Home" onclick="window.location.href='index.php'">
<input type="text" name="url" value="<?php echo $url; ?>" autocomplete="off">
<input type="hidden" name="form" value="1">
<input type="submit" value="Go">
</form>

</div>

</div>

<script type="text/javascript">
smart_select(document.getElementsByName("url")[0]);
</script>

0 comments on commit dfe9fad

Please sign in to comment.