proxy.php

Simple redirection proxy
git clone git://git.finwo.net/misc/proxy.php
Log | Files | Refs | README

Request.php (4085B)


      1 <?php
      2 
      3 namespace Finwo;
      4 
      5 /**
      6  * Class Request
      7  *
      8  * Handles some minor common functions
      9  */
     10 class Request
     11 {
     12     static function uri()
     13     {
     14         return $_SERVER['REQUEST_URI'];
     15     }
     16 
     17     static function fulluri()
     18     {
     19         return http_build_url(array(
     20             'scheme' => Request::scheme(),
     21             'host'   => Request::domain(),
     22             'path'   => Request::uri(),
     23         ));
     24     }
     25 
     26     static function safeuri()
     27     {
     28         return urlencode(Request::fulluri());
     29     }
     30 
     31     static function scheme()
     32     {
     33         static $cache = null;
     34         if (is_null($cache)) {
     35             if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
     36                 $cache = $_SERVER['HTTP_X_FORWARDED_PROTO'];
     37             } elseif (isset($_SERVER['REQUEST_SCHEME'])) {
     38                 $cache = $_SERVER['REQUEST_SCHEME'];
     39             } elseif (intval($_SERVER['SERVER_PORT']) == 443) {
     40                 $cache = 'https';
     41             } else {
     42                 $cache = 'http';
     43             }
     44         }
     45 
     46         return $cache;
     47     }
     48 
     49     static function domain()
     50     {
     51         static $cache = null;
     52         if (is_null($cache)) {
     53             if (isset($_SERVER['HTTP_HOST'])) {
     54                 $cache = $_SERVER['HTTP_HOST'];
     55             } elseif (isset($_SERVER['SERVER_NAME'])) {
     56                 $cache = $_SERVER['SERVER_NAME'];
     57             } else {
     58                 $cache = 'islive.nl';
     59             }
     60         }
     61 
     62         return $cache;
     63     }
     64 
     65     /**
     66      * Transmits a request to another host
     67      *
     68      * If returning response, beware that it's raw
     69      * (includes headers, transfer encoding, etc)
     70      *
     71      * @param       $url
     72      * @param array $options
     73      *
     74      * @return bool|string
     75      */
     76     static function transmit($url, $options = array())
     77     {
     78         // Default options
     79         $options = array_merge(array(
     80             'return'  => true,
     81             'method'  => 'GET',
     82             'headers' => array(),
     83             'body   ' => null,
     84         ), $options);
     85 
     86         // Disect the url
     87         $parts = parse_url($url);
     88 
     89         // Convert the body if needed
     90         if (in_array(gettype($options['body']), array( 'array', 'object' ))) {
     91             $options['body'] = http_build_query($options['body']);
     92             array_push($options['headers'], "Content-Type: application/x-www-form-urlencoded");
     93         }
     94 
     95         // Open a connection to the target host
     96         $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);
     97         if (!$fp) {
     98             return false;
     99         }
    100 
    101         // Construct message to the host
    102         $out = strtoupper($options['method']) . " " . $parts['path'] . (isset($parts['query']) ? '?' . $parts['query'] : '') . " HTTP/1.0\r\n";
    103         $out .= "Host: " . $parts['host'] . "\r\n";
    104         if (count($options['headers'])) {
    105             $out .= \implode("\r\n", $options['headers']) . "\r\n";
    106         }
    107         $out .= is_string($options['body']) ? sprintf("Content-Length: %s\r\n", strlen($options['body'])) : '';
    108         $out .= "Connection: Close\r\n\r\n";
    109         $out .= is_string($options['body']) ? $options['body'] : '';
    110 
    111         // Send request
    112         fwrite($fp, $out);
    113 
    114         // What we'll return
    115         $result = true;
    116 
    117         // Return data if asked to
    118         if ($options['return']) {
    119             $result = stream_get_contents($fp);
    120         }
    121 
    122         // Close the socket again
    123         fclose($fp);
    124 
    125         // Return answer
    126         return $result;
    127     }
    128 
    129     /**
    130      * Returns the request headers
    131      *
    132      * @return array
    133      */
    134     static function headers()
    135     {
    136         // Fetch given headers
    137         $headers = [];
    138         foreach ($_SERVER as $name => $value) {
    139             if (startsWith($name, 'HTTP_')) {
    140                 $name = explode('_', $name);
    141                 array_shift($name);
    142                 $name           = implode('-', array_map(function ($element) {
    143                     return ucfirst(strtolower($element));
    144                 }, $name));
    145                 $headers[$name] = $value;
    146             }
    147         }
    148 
    149         return $headers;
    150     }
    151 }