[ Index ]
 

Code source de bblocked 0.6.5

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/includes/ -> rewrite.php (source)

   1  <?php
   2           /*********************************************************\
   3          ******              bblocked Rewrite 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 Rwrite engine
  30  
  31  class Rewrite {
  32  
  33      var $_dir;
  34      var $_host;
  35      var $_tags = array("META",
  36                             "STYLE",
  37                             "SCRIPT",
  38                             "FORM"
  39                             );
  40  
  41      var $_events = array("ONABORT",
  42                             "ONBLUR",
  43                             "ONCHANGE",
  44                             "ONCLICK",
  45                             "ONDBLCLICK",
  46                             "ONDRAGDROP",
  47                             "ONERROR",
  48                             "ONFOCUS",
  49                             "ONKEYDOWN",
  50                             "ONKEYPRESS",
  51                             "ONKEYUP",
  52                             "ONLOAD",
  53                             "ONMOUSEDOWN",
  54                             "ONMOUSEOUT",
  55                             "ONMOUSEOVER",
  56                             "ONMOUSEUP",
  57                             "ONMOVE",
  58                             "ONRESET",
  59                             "ONRESIZE",
  60                             "ONSELECT",
  61                             "ONSUBMIT",
  62                             "ONUNLOAD"
  63                             );
  64      
  65  	function Rewrite($urls, $source, $content_type='text/plain', &$output) {
  66  
  67          list($this->_host, $this->_dir) = $urls;
  68          switch(strtolower($content_type)) {
  69              
  70              case 'text/css':
  71                  $output = $this->css_parse($source); break;
  72              
  73              case 'text/javascript':
  74              case 'application/x-javascript':
  75                  $output = $this->js_parse($source); break;
  76                  
  77              case 'text/html':
  78                  $output = $this->html_parse($source);
  79              
  80              break;
  81              
  82  /*            
  83              case 'text/xml':
  84              case 'application/xml':
  85              case 'application/xhtml+xml':
  86                  return $this->xml_parse(); break;
  87              
  88              case 'text/plain':
  89              case 'text/richtext':
  90              case 'text/x-setext':
  91              case 'text/enriched':
  92              case 'text/x-speech':
  93              case 'text/tab-separated-values':
  94                  return $source; break;
  95  */
  96              default:
  97                  $output = $source; break;
  98          }
  99          
 100          if(strtolower($content_type) === 'text/html') // HTML only
 101          {
 102              if($GLOBALS['_config']['scramble_bad_words'] == 1 && $GLOBALS['_config']['scramble_bad_words'] != 2) {
 103              
 104  				function sort_strlen($a, $b) {
 105                  
 106                      $a = strlen($a); $b = strlen($b);
 107                      return ($a>$b ? 1 : ($a<$b ? -1 : 0));
 108                  }
 109                  
 110                  $words = explode(',', $GLOBALS['_config']['bad_words']);
 111                  
 112                  usort($words, "sort_strlen");
 113                  foreach($words as $word)
 114                      $output = preg_replace("'\b\Q{$word}\E\b'i", '<script>document.write(\'' . implode(preg_split("''", $word, -1, PREG_SPLIT_NO_EMPTY), '\',\'') . '\');</script>', $output);
 115              }
 116              /*elseif($GLOBALS['_config']['scramble_bad_words'] == 2)
 117              {
 118                  require($GLOBALS['_config']['rewrite_dir'].'/'.'html_base64.php'); //It already takes care of $output.
 119              }*/
 120          }
 121      }
 122  
 123  	function redir_url($in, $htmlify = true) {
 124  
 125          return "{$GLOBALS['_config']['script_url_full']}?{$GLOBALS['_config']['arg_page']}={$GLOBALS['_config']['request_page']}".(($htmlify) ? '&amp;' : '&')."{$GLOBALS['_config']['arg_url']}=" . encode_url($in);
 126      }
 127  
 128  	function alter_url($value) {
 129      
 130          if(preg_match("'^[\\\/]'", $value{0})) { $value = $this->_host . $value; }
 131          else if(!preg_match("'^[a-z]{3,6}:\/\/[a-z0-9]+'i", $value)) { $value = $this->_dir . preg_replace("'^\.+'", "", $value); }
 132          else if(preg_match("'^[a-z]{3,6}:\/\/[a-z0-9]+'i", $value)) { $value = $value; }
 133          else { $value = $this->_dir . $value; }
 134  
 135          return $value;
 136      }
 137  
 138  	function css_parse($source) {
 139  
 140          if($GLOBALS['_config']['rewrite_css'] == false)
 141              return "\n\n// Stylesheets has been disabled.\n\n";
 142          
 143          foreach(preg_split("'(\@import\s*(?:\"[^\"]*\"|\'[^\']*\'|[^\s\"\'>]+))'is", $source, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $code) {
 144          
 145              if(preg_match("'\@import\s*(\"[^\"]*\"|\'[^\']*\'|[^\s\"\'>]+)'is", $code, $import)) {
 146                      
 147                  $import[1]{0} == '"' ? $delim = '"': $import[1]{0} == "'" ? $delim = '"': $value = $import[1];
 148                  
 149                  !isset($value) ? $value = substr($import[1], 1, -1) : $delim = '';
 150                  
 151                  $source = str_replace($import[1], $delim . $this->redir_url($this->alter_url($value),0) . $delim, $source);
 152              }
 153          }
 154          
 155          $split = preg_split("'(url\s?\((?:[^\(\)]+(?:\"[^\"]*\"|\'[^\']*\')?)+\))'i", $source, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 156  
 157          foreach($split as $css) {
 158  
 159              if(preg_match("'^url\s?\('i", $css)) {
 160  
 161  
 162                  list($open, $value, $close) = preg_split("'(\(|\))'", $css);
 163  
 164                  if(preg_match("'^(\"[^\"]*\"|\'[^\']*\')$'", $value)) { $delim = $value{0}; $value = substr($value, 1, -1); }
 165  
 166                  $out .= "{$open}({$delim}" . $this->redir_url($this->alter_url($value),0) . "{$delim}){$close}";
 167              }
 168  
 169              else { $out .= $css; }
 170          }
 171  
 172          return $out;
 173      }
 174  
 175  	function js_parse($source) {
 176      
 177          if(preg_match("'^\s*<\!\-\-[.\s]*\-\->\s*$'m", $source))
 178              list($open, $source, $close) = preg_split("'(^\s*<\!\-\-|\-\->\s*$)'is", $source, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 179  
 180  
 181          if($GLOBALS['_config']['remove_script'] == true)
 182              return "// Javascript has been disabled.";
 183  
 184          if($GLOBALS['_config']['rewrite_script'] == false)
 185              return $open . $source . $close;
 186  
 187  
 188          if(preg_match("'new\s+SWFobject\s*\((.*?)\)'i", $source, $swf) && !preg_match("'^http\:\/\/(www\.)?youtube\.com'i", $GLOBALS['_config']['request_url'])) {
 189              
 190              $url = preg_replace("'^\s*(\'[^\']*\'|\"[^\"]*\"|[^)]*).*'", "\\1", $swf[1]);
 191              
 192              $url{0} == '"' ? $delim = '"': $url{0} == "'" ? $delim = '"': $value = $url;
 193              !isset($value) ? $value = substr($url, 1, -1) : $delim = '"';
 194              
 195              $source = str_replace($url, $delim . $this->redir_url($this->alter_url($value)) . $delim, $source);
 196          }
 197          
 198          
 199          //$source = preg_replace("'if\s*\(\s*\w*\.location\s*(\!\=)\s*\w*\.location\s*\)\s*\{?\s*top\.location\s*\=\s*(\w*\.)?location\.href\s*\;?\s*\}?'i", "", $source);
 200          $source = preg_replace("'((?:top\.|window\.|document\.|self\.)?location(?:\.href)?\s*\=)'ie", "'break; // Removed redirection by bblocked'", $source);
 201  
 202          preg_match_all("'[\w.-]+\.(?:href|location|action)[\t\s]*\=[\t\s]*(\"[^\"]*\"|\'[^\']*\'|[^\s\"\'\;]*)'i", $source, $action);
 203  
 204          foreach($action[1] as $k=>$v) {
 205  
 206              $value = $old_funtions[$k] = $v;
 207  
 208              if(preg_match("'^(\"[^\"]*\"|\'[^\']*\')$'", $value)) { $delim = $value{0}; $value = substr($value, 1, -1); }
 209              else { $delim = '"'; }
 210  
 211              if(preg_match("'\.action[\t\s]*\='", $action[0][$k]))
 212                  $new_funtions[$k] = $delim . SCRIPT_URL_FULL . $delim;
 213  
 214              else { $new_funtions[$k] = $delim . $this->redir_url($this->alter_url($value)) . $delim; }
 215          }
 216  
 217          $source = str_replace($old_funtions, $new_funtions, $source);
 218  
 219  
 220          $split = preg_split("'(window\.open\s?\((?:(?:(?:\"[^\"]*\"|\'[^\']*\'|[^\(\)]*),?)?)+\))'i", $source, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 221  
 222          foreach($split as $script) {
 223  
 224              if(preg_match("'^window\.open\s?\('i", $script)) {
 225  
 226                  list($open, $atrs, $close) = preg_split("'(\(|\))'", $script);
 227                  $atr = explode(",", $atrs);
 228  
 229                  $value = $atr[0];
 230  
 231                  if(preg_match("'^(\"[^\"]*\"|\'[^\']*\')$'", $value)) { $delim = $value{0}; $value = substr($value, 1, -1); }
 232                  else { $delim = '"'; }
 233  
 234                  $atr[0] = $delim . $this->redir_url($this->alter_url($value)) . $delim;
 235                  $out .= "{$open}(" . implode(",", $atr) . "){$close}";
 236  
 237              }
 238  
 239              else { $out .= $script; }
 240          }
 241  
 242          return $open . $out . $close;
 243      }
 244  
 245  	function html_parse($source) {
 246      
 247          if($GLOBALS['_config']['remove_script'] == true)
 248              $source = preg_replace("'<(\/)?noscript>'i", "<\\1span>", $source);
 249          
 250          $html = preg_split("'(<(?![^a-z0-9\/])(?:[^>]+(?:\"[^\"]*\"|\'[^\']*\')?)+>)'i", $source, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 251          
 252          foreach($html as $code) {
 253  
 254              if(preg_match("'^<(?![^a-z0-9\/])(?:[^>]+(?:\"[^\"]*\"|\'[^\']*\')?)+>$'i", $code)) {
 255  
 256                  if($code{1} == "/") { $out .= $code; }
 257  
 258                  else {
 259                      $tag_contents = substr($code, 1, -1);
 260                      list($tag_name, $atributes) = preg_split("'\s'", $tag_contents, 2);
 261  
 262                      if(array_search(strtoupper($tag_name), $this->_tags) === false) {
 263  
 264                          $parse_tag = preg_split("'([^\s\"\'<>]+(?:\"[^\"]*\"|\'[^\']*\')?)'", $tag_contents, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 265  
 266                          $new_tag_contents = array_shift($parse_tag);
 267  
 268                          foreach($parse_tag as $value) {
 269  
 270                              if(preg_match("'^\s+$'", $value)) { $new_tag_contents .= $value; }
 271                              else { $new_tag_contents .= $this->alter_general($value); }
 272                          }
 273  
 274                          $out .= "<{$new_tag_contents}>";
 275                      }
 276                      
 277                      else if(preg_match("'^meta\s+http-equiv\s*=\s*(?:\'|\")?refresh(?:\'|\")?\s+content\s*\=\s*(?:\'|\")?[0-9\s]+;\s*url\s*\=\s*([^\s\"\']*)(?:\'|\")?'i", $tag_contents, $matches))
 278                          $out .= '<' . str_replace($matches[1], "{$GLOBALS['_config']['script_url_full']}?{$GLOBALS['_config']['arg_page']}={$GLOBALS['_config']['page_proxy']}&{$GLOBALS['_config']['arg_url']}=" . encode_url($matches[1]), $tag_contents) . '>';
 279                      
 280                      else { $out .= $code; }                    
 281                  }
 282              }
 283              
 284              else
 285                  $out .= $code;
 286          }    
 287              
 288          return $this->alter_specific($out);
 289      }
 290      
 291  	function alter_general($tag_contents) {
 292  
 293          if(strpos($tag_contents, "=") !== false) {
 294  
 295              list($name, $value) = explode("=", $tag_contents, 2);
 296  
 297              if(preg_match("'^(\"[^\"]*\"|\'[^\']*\')$'", $value)) { $delim = $value{0}; $value = substr($value, 1, -1); }
 298              else { $delim = '"'; }
 299  
 300              switch(strtoupper($name)) {
 301  
 302                  case 'ACTION':
 303                  case 'BACKGROUND':
 304                  case 'HREF':
 305                  case 'SRC':
 306                      if(!preg_match("'^(?:\#|(?:javascript|mailto)\:)'i", $value))
 307                          $tag_contents = "{$name}=" . $delim . $this->redir_url($this->alter_url($value)) . $delim;
 308  
 309                      break;
 310  
 311  
 312                  case 'STYLE':
 313                      $tag_contents = "{$name}=" . $delim . $this->css_parse($value) . $delim;
 314  
 315                      break;
 316  
 317  
 318                  case 'TARGET':
 319                      if($value == ("_top"||"_blank"))  { $value = "mainFrame"; }
 320                      $tag_contents = "{$name}={$delim}{$value}{$delim}";
 321  
 322                      break;
 323  
 324  
 325                  default:
 326                      break;
 327              }
 328          }
 329  
 330          return $tag_contents;
 331      }
 332  
 333  	function alter_specific($in) {
 334  
 335          $html = preg_split("'(<(?:form|style|script)(?:(?:[^>]+(?:\"[^\"]*\"|\'[^\']*\'|[^\s\"\'>]*)?)+)?>|<\/(?:form|style|script)>)'is", $in, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 336          
 337          foreach($html as $k=>$code) {
 338          
 339              if(isset($last) && $k<=$last)
 340                  continue;
 341                  
 342              else if(preg_match("'^<form(\s|>)'i", $code)) {
 343  
 344                  if(preg_match("'<form.*?action\s*\=\s*(\"[^\"]*\"|\'[^\']*\'|[^\s\"\'>]+).*?>'is", $code, $action)) {
 345                  
 346                      $action[1]{0} == '"' ? $delim = '"': $action[1]{0} == "'" ? $delim = '"': $value = $action[1];
 347                  
 348                      !isset($value) ? $value = substr($action[1], 1, -1) : $delim = '"';
 349                      
 350                      $out .= str_replace($action[1], $delim . $GLOBALS['_config']['script_url_full'] . '/' . rawurldecode(encode_url(str_replace('://', '/', $this->alter_url($value)))) . $delim, $code);
 351                  }
 352                  
 353                  else {
 354                  
 355                      $out .= preg_replace("'^(<form)'i", '\1 action="' . $GLOBALS['_config']['script_url'] . '/' . rawurldecode(encode_url(str_replace('://', '/', $this->alter_url($value)))) . '"', $code);
 356                  }
 357              }
 358  
 359              else if(preg_match("'^<style(\s|>)'i", $code)) {
 360              
 361                  $out .= $code . $this->css_parse($html[$k+1]) . $html[$k+2];
 362                  $last = $k+2;
 363              }
 364  
 365              else if(preg_match("'^<script(\s|>)'i", $code)) {
 366  
 367                  $open = $code;
 368                  $js = $html[$k+1];
 369                  $close = $html[$k+2];
 370                  $last = $k+2;
 371                  
 372                  if(stristr($open, 'src')) {
 373                  
 374                      preg_match("'src\s*\=\s*(\"[^\"]*\"|\'[^\']*\'|[^\s\"\'>]+)'i", $open, $src);
 375                      $src[1]{0} == '"' ? $delim = '"': $src[1]{0} == "'" ? $delim = '"': $value = $src[1];
 376                      !isset($value) ? $value = substr($src[1], 1, -1) : $delim = '"';
 377                      
 378                      $out .= str_replace($src[1], ($GLOBALS['_config']['remove_script'] == true ? '""' : $delim . $this->redir_url($this->alter_url($value)) . $delim), $open) . $js . $close;
 379                  }
 380                  
 381                  else
 382                      $out .= $open . $this->js_parse($js) . $close;
 383              }
 384              
 385              else
 386                  $out .= $code;
 387                  
 388              unset($delim, $value);
 389          }
 390          
 391          return $out;
 392      }
 393  }
 394  
 395  ?>


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