PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/hideurip/proxybrowser/index.php

https://github.com/abilng/hideurip
PHP | 341 lines | 327 code | 14 blank | 0 comment | 59 complexity | 3e6aa1166b9c7faf9ce7b4136c8233b2 MD5 | raw file
  1. <?php
  2. error_reporting(0);
  3. class proxied {
  4. var $url;
  5. var $url_first;
  6. var $ch;
  7. var $info;
  8. var $data;
  9. var $to_unlink;
  10. function proxied ($url) {
  11. session_start();
  12. $this->url = $url;
  13. $this->handle_actions();
  14. $this->ch = curl_init($url); //intilize curl
  15. $this->set_options();
  16. $s = array();
  17. $r = array();
  18. if ($_SESSION['__no_javascript'] == 'yes') {
  19. $s[] = '#<\s*script[^>]*?>.*?<\s*/\s*script\s*>#si';
  20. $r[] = '';
  21. $s[] = '#(\bon[a-z]+)\s*=\s*(?:"([^"]*)"?|\'([^\']*)\'?|([^\'"\s>]*))?#si';
  22. $r[] = '';
  23. $s[] = '#<noscript>(.*?)</noscript>#si';
  24. $r[] = '\\1';
  25. }
  26. if ($_SESSION['__no_images'] == 'yes') {
  27. $s[] = '#<(img|image)[^>]*?>#si';
  28. $r[] = '';
  29. }
  30. if ($_SESSION['__no_title'] == 'yes') {
  31. $s[] = '#<\s*title[^>]*?>.*?<\s*/\s*title\s*>#si';
  32. $r[] = '';
  33. }
  34. if ($_SESSION['__no_meta'] == 'yes') {
  35. $s[] = '#<(meta)[^>]*?>#si';
  36. $r[] = '';
  37. }
  38. $this->data = preg_replace($s, $r, curl_exec($this->ch));
  39. $this->info = curl_getinfo($this->ch);
  40. $this->url = parse_url($this->info['url']);
  41. $this->url['full'] = $this->info['url'];
  42. $this->url_first = $this->info['url'];
  43. header("Content-Type: {$this->info[content_type]}");
  44. if (eregi('css', $this->info['content_type'])) {
  45. $this->data = $this->parse_css($this->data);
  46. } elseif (eregi('html|xml', $this->info['content_type'])) {
  47. $this->data = $this->parse_html($this->data);
  48. }
  49. echo $this->data;
  50. curl_close($this->ch);
  51. if ($this->to_unlink) {
  52. foreach ($this->to_unlink as $file) {
  53. @unlink($file);
  54. }
  55. }
  56. }
  57. function set_options () {
  58. if (!array_key_exists('__no_javascript', $_SESSION)) {
  59. $_SESSION['__no_javascript'] = 'yes';
  60. $_SESSION['__no_images'] = 'no';
  61. $_SESSION['__no_title'] = 'no';
  62. $_SESSION['__no_meta'] = 'no';
  63. }
  64. $options = array(
  65. CURLOPT_RETURNTRANSFER => true,
  66. CURLOPT_FOLLOWLOCATION => true,
  67. CURLOPT_AUTOREFERER => true,
  68. CURLOPT_COOKIEFILE => 'cookies/' . session_id() . '.txt',
  69. CURLOPT_COOKIEJAR => 'cookies/' . session_id() . '.txt'
  70. );
  71. if (count($_POST)) {
  72. $post = array();
  73. foreach ($_POST as $key => $value) {
  74. $post[$key] = $value;
  75. }
  76. }
  77. if (count($_FILES)) {
  78. $post = $post ? $post : array();
  79. foreach ($_FILES as $name => $file) {
  80. $this->to_unlink[] = "uploads/$file[name]";
  81. move_uploaded_file($file['tmp_name'], "uploads/$file[name]");
  82. $post[$name] = "@uploads/$file[name]";
  83. }
  84. }
  85. if ($post) {
  86. $options[CURLOPT_POST] = true;
  87. $options[CURLOPT_POSTFIELDS] = $post;
  88. }
  89. if (ereg('__proxy_url=', $_SERVER['HTTP_REFERER'])) {
  90. preg_match('#__proxy_url=([^&]+)#', $_SERVER['HTTP_REFERER'], $referer);
  91. $referer = base64_decode($referer[1]);
  92. $options[CURLOPT_REFERER] = $referer;
  93. }
  94. if (!eregi('google\.com', $this->url)) {
  95. $options[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
  96. } else {
  97. $options[CURLOPT_USERAGENT] = 'None';
  98. }
  99. foreach ($options as $option => $value) {
  100. @curl_setopt($this->ch, $option, $value);
  101. }
  102. }
  103. function parse_html ($string) {
  104. $parse = array(
  105. 'a' => array('href'),
  106. 'img' => array('src', 'longdesc'),
  107. 'image' => array('src', 'longdesc'),
  108. 'body' => array('background'),
  109. 'frame' => array('src', 'longdesc'),
  110. 'iframe' => array('src', 'longdesc'),
  111. 'head' => array('profile'),
  112. 'layer' => array('src'),
  113. 'input' => array('src', 'usemap'),
  114. 'form' => array('action'),
  115. 'area' => array('href'),
  116. 'link' => array('href', 'src', 'urn'),
  117. 'meta' => array('content'),
  118. 'param' => array('value'),
  119. 'applet' => array('codebase', 'code', 'object', 'archive'),
  120. 'object' => array('usermap', 'codebase', 'classid', 'archive', 'data'),
  121. 'script' => array('src'),
  122. 'select' => array('src'),
  123. 'hr' => array('src'),
  124. 'table' => array('background'),
  125. 'tr' => array('background'),
  126. 'th' => array('background'),
  127. 'td' => array('background'),
  128. 'bgsound' => array('src'),
  129. 'blockquote' => array('cite'),
  130. 'del' => array('cite'),
  131. 'embed' => array('src'),
  132. 'fig' => array('src', 'imagemap'),
  133. 'ilayer' => array('src'),
  134. 'ins' => array('cite'),
  135. 'note' => array('src'),
  136. 'overlay' => array('src', 'imagemap'),
  137. 'q' => array('cite'),
  138. 'ul' => array('src')
  139. );
  140. $tags = $this->get_tags($string);
  141. $to_replace = array();
  142. foreach ($tags as $tag) {
  143. $tag_name = $this->get_tag_name($tag);
  144. $attributes = $this->get_attributes($tag);
  145. if ($tag_name == 'base' && $attributes['href']) {
  146. $this->url = parse_url($attributes['href']);
  147. $this->url['full'] = $attributes['href'];
  148. $to_replace[] = array(
  149. 'string' => $tag,
  150. 'value' => ''
  151. );
  152. }
  153. if ($attributes['style']) {
  154. $attributes['style'] = $this->parse_css($attributes['style']);
  155. }
  156. if ($parse[$tag_name]) {
  157. $extra_html = '';
  158. $relink = true;
  159. $new_tag = "<$tag_name";
  160. switch ($tag_name) {
  161. case 'form':
  162. if (strtolower($attributes['method']) == 'get' || !$attributes['method']) {
  163. $url = $attributes['action'] ? $this->encode_url($attributes['action'], false, true) : $this->encode_url($this->url['full'], false, true);
  164. $extra_html = "<input type=\"hidden\" name=\"__proxy_url\" value=\"$url\" /><input type=\"hidden\" name=\"__proxy_action\" value=\"redirect_get\" />";
  165. $attributes['action'] = './';
  166. $attributes['method'] = 'post';
  167. $relink = false;
  168. }
  169. break;
  170. case 'head':
  171. if ($_GET['__proxy_form'] != '0') {
  172. $extra_html = "<script language=\"javascript\" type=\"text/javascript\">\n";
  173. $extra_html .= "var __proxy_url = '{$this->url_first}';\n";
  174. $no_javascript = $_SESSION['__no_javascript'] == '1' ? 'true' : 'false';
  175. $extra_html .= "var __no_javascript = $no_javascript;\n";
  176. $no_images = $_SESSION['__no_images'] == 'yes' ? '1' : 'false';
  177. $extra_html .= "var __no_images = $no_images;\n";
  178. $no_title = $_SESSION['__no_title'] == 'yes' ? '1' : 'false';
  179. $extra_html .= "var __no_title = $no_title;\n";
  180. $no_meta = $_SESSION['__no_meta'] == 'yes' ? '1' : 'false';
  181. $extra_html .= "var __no_meta = $no_meta;\n";
  182. $extra_html .= "</script>\n";
  183. $extra_html .= '<script language="javascript" type="text/javascript" src="./js/main.js"></script>';
  184. }
  185. break;
  186. }
  187. if ($attributes) {
  188. foreach ($attributes as $attribute_name => $attribute_value) {
  189. if (in_array($attribute_name, $parse[$tag_name])) {
  190. switch ($tag_name) {
  191. default:
  192. if ($relink) {
  193. if ($attribute_name == 'src') {
  194. $extra = '&__proxy_form=0';
  195. } else {
  196. $extra = '';
  197. }
  198. $attribute_value = $this->encode_url($attribute_value) . $extra;
  199. }
  200. break;
  201. case 'meta':
  202. if (eregi('refresh', $attributes['http-equiv']) && $tag_name == 'meta' && $attribute_name == 'content' && preg_match('#^(\s*[0-9]*\s*;\s*url=)(.*)#i', $attribute_value, $content)) {
  203. $attribute_value = $content[1] . $this->encode_url($content[2]);
  204. }
  205. break;
  206. }
  207. }
  208. $new_tag .= " $attribute_name=\"$attribute_value\"";
  209. }
  210. }
  211. $new_tag .= ">$extra_html";
  212. $to_replace[] = array(
  213. 'string' => $tag,
  214. 'value' => $new_tag
  215. );
  216. }
  217. }
  218. $string = $this->mass_replace($to_replace, $string);
  219. return $string;
  220. }
  221. function parse_css ($string) {
  222. $to_replace = array();
  223. preg_match_all('#url[\s]*\([\s]*("[^"]+"|\'[^\']+\'|[^\s>]+)[\s]*\)#si', $string, $urls);
  224. for ($i = 0; $i < count($urls[0]); $i++) {
  225. $url = $this->encode_url(preg_replace('#^("([^"]+)"|\'([^\']+)\')$#', '\\2\\3', $urls[1][$i]));
  226. $to_replace[] = array(
  227. 'string' => $urls[0][$i],
  228. 'value' => "url('$url')"
  229. );
  230. }
  231. preg_match_all('#@import[\s]*("[^"]+"|\'[^\']+\'|[^\s>]+)#si', $string, $urls);
  232. for ($i = 0; $i < count($urls[0]); $i++) {
  233. $url = $this->encode_url(preg_replace('#^("([^"]+)"|\'([^\']+)\')$#', '\\2\\3', $urls[1][$i]));
  234. $to_replace[] = array(
  235. 'string' => $urls[0][$i],
  236. 'value' => "@import '$url'"
  237. );
  238. }
  239. $string = $this->mass_replace($to_replace, $string);
  240. return $string;
  241. }
  242. function get_tags ($string) {
  243. preg_match_all('#<([a-z-]+)([^>]+)>#si', $string, $tags);
  244. return $tags[0];
  245. }
  246. function get_tag_name ($string) {
  247. preg_match('#^<([a-z0-9-]+)#i', $string, $matches);
  248. return strtolower($matches[1]);
  249. }
  250. function get_attributes ($string) {
  251. $attributes = array();
  252. $string = preg_replace('#^<[a-z-]+|>$#i', '', $string);
  253. if ($string) {
  254. preg_match_all('#([a-z-]+)=?("[^">]*"|\'[^\'>]*\'|[^\s>]*)#si', $string, $matches);
  255. for ($i = 0; $i < count($matches[0]); $i++) {
  256. $attributes[strtolower($matches[1][$i])] = $this->strip_quotes($matches[2][$i]);
  257. }
  258. return $attributes;
  259. } else {
  260. return false;
  261. }
  262. }
  263. function strip_quotes ($string) {
  264. return ereg_replace('^("([^"]*)"|^\'([^\']*)\')$', '\\2\\3', $string);
  265. }
  266. function mass_replace ($array, $string) {
  267. foreach ($array as $replacement) {
  268. $string = str_replace($replacement['string'], $replacement['value'], $string);
  269. }
  270. return $string;
  271. }
  272. function encode_url ($string, $raw = false, $plain = false) {
  273. $string = $this->strip_quotes(html_entity_decode($string));
  274. if (eregi('^[a-z]{2,}:', $string)) {
  275. } elseif (ereg('^/', $string)) {
  276. $string = "{$this->url[scheme]}://{$this->url[host]}$string";
  277. } elseif (ereg('^#', $string)) {
  278. $raw = true;
  279. } elseif (eregi('^mailto:', $string)) {
  280. $raw = true;
  281. } elseif (ereg('^\.\./', $string)) {
  282. preg_match_all('#\.\./#', $string, $matches);
  283. $path = ereg_replace('/([^/]*)$', '/', $this->url['path']);
  284. for ($i = 0; $i < count($matches[0]); $i++) {
  285. $path = ereg_replace('([^/]*)/$', '', $path);
  286. }
  287. $path = ereg_replace('/$', '', $path) . '/';
  288. $string = ereg_replace('\.\./', '', $string);
  289. $string = "{$this->url[scheme]}://{$this->url[host]}$path$string";
  290. } else {
  291. $string = ereg_replace('^\./', '', $string);
  292. $path = ereg_replace('/([^/]*)$', '/', $this->url['path']);
  293. $path = ereg_replace('/$', '', $path) . '/';
  294. $string = "{$this->url[scheme]}://{$this->url[host]}$path$string";
  295. }
  296. return $raw ? $string : (!$plain ? './?__proxy_url=' : '') . base64_encode($string);
  297. }
  298. function handle_actions () {
  299. if ($_POST['__proxy_action'] == 'redirect_get') {
  300. $url = base64_decode($_POST['__proxy_url']);
  301. unset($_POST['__proxy_action'], $_POST['__proxy_url']);
  302. $get = '';
  303. foreach ($_POST as $key => $value) {
  304. $value = urlencode($value);
  305. $get .= "&$key=$value";
  306. }
  307. $get = ereg('\?', $url) ? $get : ereg_replace('^&', '?', $get);
  308. $url = base64_encode($url . $get); //encode url
  309. header("Location: ./?__proxy_url=$url");
  310. exit;
  311. } elseif ($_POST['__proxy_action'] == 'redirect_browse') {
  312. $_SESSION['__no_javascript'] = (bool) $_POST['__no_javascript'] ? 'yes' : 'no';
  313. $_SESSION['__no_images'] = (bool) $_POST['__no_images'] ? 'yes' : 'no';
  314. $_SESSION['__no_title'] = (bool) $_POST['__no_title'] ? 'yes' : 'no';
  315. $_SESSION['__no_meta'] = (bool) $_POST['__no_meta'] ? 'yes' : 'no';
  316. header('Location: ./?__proxy_url=' . base64_encode($_POST['__proxy_url'])); //encode url
  317. exit;
  318. }
  319. }
  320. }
  321. $url = @parse_url($_GET['__proxy_url']) && strlen($_GET['__proxy_url']) ? base64_decode($_GET['__proxy_url']) : false;
  322. if (!$url && !$_POST['__proxy_action']) {
  323. include 'home.php';
  324. exit;
  325. }
  326. $proxied = new proxied($url);
  327. ?>