[ Index ]
 

Code source de bblocked 0.6.5

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/includes/proxy/ -> base.php (source)

   1  <?php
   2           /*********************************************************\
   3          ******               bblocked Proxy class              ******
   4         *****                                                     *****
   5        ****               Copyleft (C) 2007  bblocked               ****
   6       ***                                                             ***
   7      **  This program is free software; you can redistribute it and/or  **
   8     **   modify it under the terms of the GNU General Public License     **
   9    **    as published by the Free Software Foundation; either version 2   **
  10   **     of the License, or (at your option) any later version.            **
  11   **                                                                       **
  12   **     This program is distributed in the hope that it will be useful,   **
  13    **    but WITHOUT ANY WARRANTY; without even the implied warranty of   **
  14     **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   **
  15      **  GNU General Public License for more details.                   **
  16       ***                                                             ***
  17        ****                                                         ****
  18         ****               http://www.bblocked.org/               *****
  19          ******                                                 ******
  20           \*********************************************************/
  21  
  22  
  23  /* Do not remove, prevents direct file access */
  24  if(!defined('BB'))
  25      die();
  26  
  27  
  28  
  29  // Class containing bblocked Proxy engine
  30  
  31  class Proxy {
  32  
  33      var $_socket;
  34      var $_url;
  35      var $_method;
  36      var $_headers;
  37      var $_page;
  38      var $_content_type;
  39      var $_no_cache;
  40      var $_realm;
  41      var $_cookies = array();
  42      var $_auth = array();
  43      
  44  	function url_parse($url, &$container) {
  45      
  46          $container = array();
  47          $temp = (is_array(@parse_url($url)) ? @parse_url($url) : @parse_url(decode_url($url)));
  48      
  49          if(!empty($temp)) {
  50          
  51              $temp['port_ext'] = '';
  52              $temp['base']            = $temp['scheme'] . '://' . $temp['host'];
  53      
  54              if(isset($temp['port']) && (($temp['scheme'] == 'http' && $temp['port'] != 80) || ($temp['scheme'] == 'ftp' && $temp['port'] != 21)))
  55                  $temp['base'] .= $temp['port_ext'] = ':' . $temp['port'];
  56  
  57              else
  58                  $temp['port'] = $temp['scheme'] === 'https' ? 443 : ($temp['scheme'] === 'ftp' ? 21 : 80);
  59  
  60              
  61              if($temp['scheme'] == 'https' && (!$GLOBALS['_config']['unsecure_ssl'] && !$_SERVER['HTTPS'])) {
  62  
  63                  $temp['port_ext'] ? '' : $temp['port'] = 80;
  64                  $temp['scheme'] = 'http';
  65              }
  66              
  67              $temp['path'] = isset($temp['path']) ? $temp['path'] : '/';
  68              $path         = array();
  69              $temp['path'] = explode('/', $temp['path']);
  70          
  71              foreach($temp['path'] as $dir) {
  72              
  73                  if($dir === '..')
  74                      array_pop($path);
  75  
  76                  else if($dir !== '.') {
  77                  
  78                      for ($dir = rawurldecode($dir), $new_dir = '', $i = 0, $count_i = strlen($dir); $i < $count_i; $new_dir .= strspn($dir{$i}, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$-_.+!*\'(),?:@&;=') ? $dir{$i} : rawurlencode($dir{$i}), ++$i);
  79                      $path[] = $new_dir;
  80                  }
  81              }
  82      
  83              $temp['path']    = str_replace('/%7E', '/~', '/' . ltrim(implode('/', $path), '/'));
  84              $temp['file']    = substr($temp['path'], strrpos($temp['path'], '/')+1);
  85              $temp['dir']    = substr($temp['path'], 0, strrpos($temp['path'], '/'));
  86              $temp['prev_dir'] = substr_count($temp['path'], '/') > 1 ? substr($temp['dir'], 0, strrpos($temp['dir'], '/')+1) : '/';
  87              $temp['full']    = $temp['base'] . $temp['path'] . ($temp['path']{-1} == '/' ? $temp['file'] : '') . ($temp['query'] ? '?' . $temp['query']: '');
  88              $container = $temp;
  89      
  90              return true;
  91          }
  92          
  93          return false;
  94      }
  95      
  96  	function write_cache() {
  97      
  98          $time = time();
  99          
 100          foreach($this->_headers as $k=>$v)
 101              $headers .= "{$k}: {$v}\r\n";
 102          
 103          $content = "{$time}\r\n{$headers}\r\n{$this->_page}";
 104          $fp = @fopen($GLOBALS['_config']['cache_dir'] . md5($this->_url['full']) . '.cache', 'w');
 105          @fwrite($fp, $content . "\n\n<!-- servered from cache; cached on " . date("D, d F Y", $time) . ' -->');
 106          @fclose($fp);
 107      }
 108      
 109  	function read_cache(&$data) {
 110      
 111          $cache_file = $GLOBALS['_config']['cache_dir'] . md5($this->_url['full']) . '.cache';
 112          if(file_exists($cache_file)) {
 113          
 114              list($date, $data) = explode("\r\n", file_get_contents($cache_file), 2);
 115              
 116              if(time() <= $date+$GLOBALS['_config']['cache_time'])
 117                  return true;
 118          }
 119          return false;
 120      }
 121      
 122  	function parse_response($data, $cache=false) {
 123          
 124          $headers = array();
 125          
 126          list($headers, $this->_page) = preg_split("'(HTTP\/1\.[01] \d{3} [a-zA-Z\x20]+\r\n(?:[a-zA-Z\-]{3,}\s*\:.*?\r\n)+\r\n)'s", $data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 127          
 128          $headers = explode("\r\n", $headers);
 129          
 130          sscanf(array_shift($headers), '%s %s', $http_ver, $response_code);
 131          
 132          
 133          foreach($headers as $h) {
 134          
 135              if($h != '') {
 136                  list($k, $v) = explode(':', $h, 2);
 137                  if(strtolower(trim($k)) == 'set-cookie')
 138                      $header[strtolower(trim($k))][] = trim($v);
 139                  
 140                  else
 141                      $header[strtolower(trim($k))] = trim($v);
 142              }
 143          }
 144          
 145          if($response_code == 304 && isset($header['last-modifed'])) {
 146          
 147              header('HTTP/1.1 304 Not Modified');
 148              header('Date: ' . gmdate("D, d M Y H:i:s", (isset($header['date']) ? intval(@strtotime($header['date'])) : time())));
 149              exit(0);
 150          }
 151          
 152          if(isset($header['set-cookie'])) {
 153          
 154              if($GLOBALS['_config']['accept_cookies']) {
 155              
 156                  foreach($header['set-cookie'] as $cookie) {
 157                  
 158                      $name = $value = $expires = $path = $domain = $secure = $expires_time = '';
 159              
 160                      preg_match("'^\s*([^=;,\s]*)\s*=?\s*([^;]*)'",    $cookie, $match) && list(, $name, $value)    = $match;
 161                      preg_match("';\s*expires\s*=\s*([^;]*)'i",    $cookie, $match) && list(, $expires)    = $match;
 162                      preg_match("';\s*path\s*=\s*([^;,\s]*)'i",    $cookie, $match) && list(, $path)        = $match;
 163                      preg_match("';\s*domain\s*=\s*([^;,\s]*)'i",    $cookie, $match) && list(, $domain)        = $match;
 164                      preg_match("';\s*(secure\b)'i",            $cookie, $match) && list(, $secure)        = $match;
 165              
 166                      $expires_time = empty($expires) ? 0 : intval(@strtotime($expires));
 167                      $path = empty($path) ? '/' : $path;
 168                          
 169                      if(empty($domain))
 170                          $domain = $this->_url['host'];
 171      
 172                      else {
 173                      
 174                          $domain = '.' . strtolower(str_replace('..', '.', trim($domain, '.')));
 175              
 176                          if((!preg_match("'\Q" . $domain . "\E$'i", $this->_url['host']) && $domain != '.' . $this->_url['host']) || (substr_count($domain, '.') < 2 && $domain{0} == '.'))
 177                              continue;
 178                      }
 179                      
 180                      if(count($_COOKIE) >= 15 && time()-$expires_time <= 0)
 181                          $this->_cookies[] = add_cookie(current($_COOKIE), '', 1);
 182                      
 183                      if($GLOBALS['_config']['session_cookies'])
 184                          $_SESSION['data']['cookies'][serialize("$name;$path;$domain")] = serialize("$value;$secure");
 185                          
 186                      else
 187                          $this->_cookies[] = add_cookie("COOKIE;$name;$path;$domain", "$value;$secure", $expires_time);
 188                  }
 189              }                
 190              unset($header['set-cookie']);
 191          }
 192          
 193          if(!empty($this->_cookies))
 194              $header['set-cookie'] = $this->_cookies;
 195              
 196          if(isset($header['location'])) {
 197          
 198              if(preg_match("'^[\\\/]+'", $header['location']{0}))
 199                  $this->HTTP($this->_url['base'] . $header['location']);
 200                  
 201              else
 202                  $this->HTTP($header['location']);
 203                  
 204              return false;
 205          }
 206          
 207          else {
 208          
 209              if(isset($header['content-type'])) {
 210              
 211                  strpos($header['content-type'], ';') !== false ? (list($this->_content_type, $encoding) = explode(';', str_replace(' ', '', $header['content-type']), 2)) : $this->_content_type = $header['content-type'];
 212                  
 213                  if(preg_match("'^(text\/(plain|html))'i", $header['content-type']) && !preg_match("'\.js$'i", $this->_url['full']))
 214                      $_SESSION['current_url'] = $this->_url['full'];
 215              }
 216                  
 217              if(isset($header['content-length'])) {
 218              
 219                  $content_length = $headers['content-length'];
 220                  unset($header['content-length']);
 221              }
 222              
 223              if(isset($header['content-disposition']))
 224                  $header['content-disposition'] = empty($header['content-disposition']) ? ($header['content-disposition'] == 'application/octet_stream' ? 'attachment' : 'inline') . '; filename="' . $this->_url['file'] . '"' : $header['content-disposition'];
 225              
 226              if(isset($header['p3p']) && preg_match("'policyref\s*=\s*[\'\"]?([^\'\"\s]*)[\'\"]?'i", $headers['p3p'][0], $matches))
 227                  unset($header['p3p']);
 228                  
 229              if(isset($header['refresh']) && preg_match("'([0-9]+)\s*;\s*url\=\s*(\S*)'i", $header['refresh'], $matches))
 230                  $header['refresh'] = $matches[1] . "; url={$GLOBALS['_config']['script_url_full']}?{$GLOBALS['_config']['arg_page']}=proxy&{$GLOBALS['_config']['arg_url']}=" . encode_url($matches[2]);
 231              
 232              if(isset($header['uri']))
 233                  unset($header['uri']);
 234                  
 235              if(isset($header['content-location']))
 236                  unset($header['content-location']);
 237              
 238              unset($header['connection'], $header['keep-alive']);
 239              
 240              if($response_code == 401 && isset($header['www-authenticate']) && preg_match("'basic\s+(?:realm=\"(.*?)\")?'i", $header['www-authenticate'], $matches)) {
 241              
 242                  unset($header['www-authenticate'], $this->_page);
 243                  
 244                  if(isset($this->_auth[$matches[1]])) {
 245                  
 246                      $this->_realm = $matches[1];
 247                      $this->HTTP();
 248                  }
 249                  
 250                  else {
 251                      $GLOBALS['_config']['realm_name']  = $matches[1];
 252                      print_template(TEMPLATE_HTTP_AUTH);
 253                  }
 254              }
 255              
 256              if($GLOBALS['_config']['request_page'] == 'raw')
 257                  $this->_page = $data;
 258                  
 259              else
 260                  $this->_headers = $header;
 261              
 262              if($cache == true) {
 263              
 264                  switch(true) {
 265                  
 266                      case ($response_code != 200):
 267                      case (isset($header['set-cookie'])):
 268                      case ($this->_no_cache === true):
 269                          break;
 270                          
 271                      default:
 272                          $this->write_cache();
 273                          break;
 274                  }
 275              }
 276              
 277              return true;
 278          }
 279      }
 280      
 281  	function output_page() {
 282      
 283          global $_config;
 284          
 285          if(is_array($this->_headers)) {
 286          
 287              foreach($this->_headers as $k=>$v) {
 288              
 289                  if($k == 'content-type' && preg_match("'^text\/plain(?:\s*\;\s*(.*))?'i", $v, $encoding)) {
 290                  
 291                      header('content-type: text/html'. (isset($encoding[1]) ? "; {$encoding}" : ''));
 292                      $this->_page = '<pre>' . htmlentities($this->_page) . '</pre>';
 293                      continue;
 294                  }
 295              
 296                  if(is_array($v))
 297                      foreach($v as $v2) { header("{$k}: {$v2}"); }
 298                  
 299                  header("{$k}: {$v}");
 300              }
 301          }
 302          
 303          $url = $this->_url;
 304          new Rewrite(array($url['base'], $url['base'] . $url['dir'] . '/'), $this->_page, $this->_content_type, $output);
 305          print($output);
 306          
 307          preg_match("'<title>(.*)?<\/title>'is", $this->_page, $title);
 308          
 309          if($_config['force_title'])
 310              $title = addcslashes(($_config['powered_by'] ? str_replace('{:title:}', ($title[1] ? html_entity_decode(preg_replace("'([\r\n]+\s*)'", " ", $_config['title'])) : $url['full']), $_config['powered_by_text']) : $_config['title']), "'");
 311          
 312          else
 313              $title = addcslashes(($_config['powered_by'] ? str_replace('{:title:}', ($title[1] ? html_entity_decode(preg_replace("'([\r\n]+\s*)'", " ", $title[1])) : $url['full']), $_config['powered_by_text']) : $title[1]), "'");
 314          
 315          $url = addcslashes($url['full'], "'");
 316          
 317          print <<<OUTPUT
 318          
 319  <script defer>
 320      <!-- script added by bblocked <http://www.bblocked.org/>
 321  
 322          // change title of document            
 323          top.document.title='{$title}';
 324          
 325          // change URL in bblocked form
 326          top.headerFrame.document.f.{$GLOBALS['_config']['arg_url']}.value='{$url}';
 327      -->
 328  </script>
 329  
 330  OUTPUT;
 331      }
 332  }
 333  
 334  ?>


Généré le : Tue Nov 20 20:31:26 2007 par Balluche grâce à PHPXref 0.7 Clicky Web Analytics