forked from NicheOffice/php-web-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
153 lines (106 loc) · 4.48 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<meta name="google-site-verification=pWatsMNCbuA6w8ziNuq3c6wCJz3ugTq-BdwTKcorJEk" />
<meta name="google-adsense-account" content="ca-pub-9850650985058210"><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9850650985058210"
crossorigin="anonymous"></script>
<script async custom-element="amp-auto-ads"
src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
</script>
<meta name="google-adsense-account" content="ca-pub-9850650985058210"><meta name="google-site-verification=pWatsMNCbuA6w8ziNuq3c6wCJz3ugTq-BdwTKcorJEk" />
<?php
define('PROXY_START', microtime(true));
require("vendor/autoload.php");
use Proxy\Http\Request;
use Proxy\Http\Response;
use Proxy\Plugin\AbstractPlugin;
use Proxy\Event\FilterEvent;
use Proxy\Config;
use Proxy\Proxy;
// start the session
session_start();
// load config...
Config::load('./config.php');
// custom config file to be written to by a bash script or something
Config::load('./custom_config.php');
if (!Config::get('app_key')) {
die("app_key inside config.php cannot be empty!");
}
if (!function_exists('curl_version')) {
die("cURL extension is not loaded!");
}
// how are our URLs be generated from this point? this must be set here so the proxify_url function below can make use of it
if (Config::get('url_mode') == 2) {
Config::set('encryption_key', md5(Config::get('app_key') . $_SERVER['REMOTE_ADDR']));
} else if (Config::get('url_mode') == 3) {
Config::set('encryption_key', md5(Config::get('app_key') . session_id()));
}
// very important!!! otherwise requests are queued while waiting for session file to be unlocked
session_write_close();
// form submit in progress...
if (isset($_POST['url'])) {
$url = $_POST['url'];
$url = add_http($url);
header("HTTP/1.1 302 Found");
header('Location: ' . proxify_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 302 Found");
header("Location: " . Config::get('index_redirect'));
} else {
if (isset($_GET["tos"]) != "") {
echo render_template("./templates/tos.php", array(
'version' => Proxy::VERSION
));
} else {
echo render_template("./templates/main.php", array('version' => Proxy::VERSION));
}
}
exit;
}
// decode q parameter to get the real URL
$url = url_decrypt($_GET['q']);
$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 if (class_exists('\\Proxy\\Plugin\\' . $plugin_class)) {
// does the native plugin from php-proxy package with such name exist?
$plugin_class = '\\Proxy\\Plugin\\' . $plugin_class;
}
// otherwise plugin_class better be loaded already through composer.json and match namespace exactly \\Vendor\\Plugin\\SuperPlugin
$proxy->getEventDispatcher()->addSubscriber(new $plugin_class());
}
try {
// request sent to index.php
$request = Request::createFromGlobals();
// remove all GET parameters such as ?q=
$request->get->clear();
// 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())
));
// Cannot modify header information - headers already sent
header("HTTP/1.1 302 Found");
header("Location: {$url}");
} else {
echo render_template("./templates/main.php", array(
'url' => $url,
'error_msg' => $ex->getMessage(),
'version' => Proxy::VERSION
));
}
}
<amp-auto-ads type="adsense"
data-ad-client="ca-pub-9850650985058210">
</amp-auto-ads>
?>