1+ <?php
2+
3+ require ("vendor/autoload.php " );
4+
5+ define ('PROXY_START ' , microtime (true ));
6+ define ('SCRIPT_BASE ' , (!empty ($ _SERVER ['HTTPS ' ]) ? 'https:// ' : 'http:// ' ).$ _SERVER ['HTTP_HOST ' ].$ _SERVER ['PHP_SELF ' ]);
7+ define ('SCRIPT_DIR ' , pathinfo (SCRIPT_BASE , PATHINFO_DIRNAME ).'/ ' );
8+
9+ use Symfony \Component \HttpFoundation \Request ;
10+ use Symfony \Component \HttpFoundation \Response ;
11+ use Symfony \Component \HttpFoundation \ParameterBag ;
12+
13+ use Proxy \Plugin \AbstractPlugin ;
14+ use Proxy \Event \FilterEvent ;
15+ use Proxy \Config ;
16+ use Proxy \Proxy ;
17+
18+ // load config...
19+ Config::load ('./config.php ' );
20+
21+ // form submit in progress...
22+ if (isset ($ _POST ['url ' ])){
23+
24+ $ url = $ _POST ['url ' ];
25+ $ url = add_http ($ url );
26+
27+ header ("HTTP/1.1 302 Found " );
28+ header ('Location: ' .SCRIPT_BASE .'?q= ' .encrypt_url ($ url ));
29+ exit ;
30+
31+ } else if (!isset ($ _GET ['q ' ])){
32+
33+ // must be at homepage - should we redirect somewhere else?
34+ if (Config::get ('index_redirect ' )){
35+
36+ // redirect to...
37+ header ("HTTP/1.1 301 Moved Permanently " );
38+ header ("Location: " .Config::get ('index_redirect ' ));
39+
40+ } else {
41+ echo render_template ("./templates/main.php " , array ('script_base ' => SCRIPT_BASE , 'version ' => Proxy::VERSION ));
42+ }
43+
44+ exit ;
45+ }
46+
47+
48+ // get real URL
49+ $ url = decrypt_url ($ _GET ['q ' ]);
50+ define ('URL ' , $ url );
51+
52+
53+ $ proxy = new Proxy ();
54+
55+
56+ // load plugins
57+ foreach (Config::get ('plugins ' , array ()) as $ plugin ){
58+
59+ $ plugin_class = $ plugin .'Plugin ' ;
60+
61+ if (file_exists ('./plugins/ ' .$ plugin_class .'.php ' )){
62+
63+ // use user plugin from /plugins/
64+ require_once ('./plugins/ ' .$ plugin_class .'.php ' );
65+
66+ } else {
67+
68+ // use native plugin from php-proxy - it was already loaded into namespace automatically through composer
69+ $ plugin_class = '\\Proxy \\Plugin \\' .$ plugin_class ;
70+ }
71+
72+ $ proxy ->getEventDispatcher ()->addSubscriber (new $ plugin_class ());
73+ }
74+
75+ // provide URL form
76+ $ proxy ->getEventDispatcher ()->addListener ('request.complete ' , function ($ event ){
77+
78+ $ request = $ event ['request ' ];
79+ $ response = $ event ['response ' ];
80+
81+ $ url = $ request ->getUri ();
82+
83+ // we attach url_form only if this is a html response
84+ if (!is_html ($ response ->headers ->get ('content-type ' ))){
85+ return ;
86+ }
87+
88+ $ url_form = render_template ("./templates/url_form.php " , array (
89+ 'url ' => $ url ,
90+ 'script_base ' => SCRIPT_BASE
91+ ));
92+
93+ $ output = $ response ->getContent ();
94+
95+ // does the html page contain <body> tag, if so insert our form right after <body> tag starts
96+ $ output = preg_replace ('@<body.*?>@is ' , '$0 ' .PHP_EOL .$ url_form , $ output , 1 , $ count );
97+
98+ // <body> tag was not found, just put the form at the top of the page
99+ if ($ count == 0 ){
100+ $ output = $ url_form .$ output ;
101+ }
102+
103+ $ response ->setContent ($ output );
104+ });
105+
106+
107+ try {
108+
109+ // request sent to index.php
110+ $ request = Request::createFromGlobals ();
111+
112+ // forward it to some other URL
113+ $ response = $ proxy ->forward ($ request , $ url );
114+
115+ // if that was a streaming response, then everything was already sent and script will be killed before it even reaches this line
116+ $ response ->send ();
117+
118+ } catch (Exception $ ex ){
119+
120+ // if the site is on server2.proxy.com then you may wish to redirect it back to proxy.com
121+ if (Config::get ("error_redirect " )){
122+
123+ $ url = render_string (Config::get ("error_redirect " ), array (
124+ 'error_msg ' => rawurlencode ($ ex ->getMessage ())
125+ ));
126+
127+ header ("HTTP/1.1 302 Found " );
128+ header ("Location: {$ url }" );
129+
130+ } else {
131+
132+ echo render_template ("./templates/main.php " , array (
133+ 'url ' => $ url ,
134+ 'script_base ' => SCRIPT_BASE ,
135+ 'error_msg ' => $ ex ->getMessage (),
136+ 'version ' => Proxy::VERSION
137+ ));
138+
139+ }
140+ }
141+
142+ ?>
0 commit comments