RumbleFrog

Image Proxy

Jan 28th, 2017
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. <?php
  2.  
  3. class Proxy {
  4.     protected $allowed = [];
  5.  
  6.     public function __construct()
  7.     {
  8.  
  9.         if(!isset($_GET['url'])) {
  10.             header("HTTP/1.0 404 Not Found");
  11.             die('404 - 1');
  12.         }
  13.        
  14.  
  15.         if (filter_var($_GET['url'], FILTER_VALIDATE_URL) === false) {
  16.             header("HTTP/1.0 404 Not Found");
  17.             die('404 -2');
  18.         }
  19.  
  20.         if(!$this->isAllowed($_GET['url'])) {
  21.             header("HTTP/1.0 404 Not Found");
  22.             die('404 - 3');
  23.         }
  24.     }
  25.  
  26.     public function output()
  27.     {
  28.         $ext = pathinfo($this->url, PATHINFO_EXTENSION);
  29.         switch ($ext) {
  30.         case "gif":
  31.             header('Content-Type: image/gif');
  32.             readfile($this->url);
  33.             break;
  34.         case "png":
  35.             header('Content-Type: image/png');
  36.             readfile($this->url);
  37.             break;
  38.         case "jpg":
  39.         default:
  40.             header('Content-Type: image/jpeg');
  41.             readfile($this->url);
  42.             break;
  43.         }
  44.     }
  45.  
  46.     protected function getHost()
  47.     {
  48.         $result = parse_url($this->url);
  49.         return $result['host'];
  50.     }
  51.  
  52.     protected function isAllowed($url)
  53.     {
  54.         return in_array($this->getHost($url), $this->allowed);
  55.     }
  56. }
  57.  
  58. (new Proxy)->output();
Advertisement
Add Comment
Please, Sign In to add comment