Advertisement
airevent

php скрипт для обфускации и экспорта веб приложений

Mar 11th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.40 KB | None | 0 0
  1. <?php // http://code.google.com/p/closure-compiler/wiki/Warnings
  2.  
  3. class Obf {
  4.     private $src;
  5.     private $dst;
  6.  
  7.     private $skip;
  8.     private $copy;
  9.  
  10.     private $js = array();
  11.     private $jsout;
  12.     private $jscmd;
  13.  
  14.     public function __construct( $conf ) {
  15.         echo "php obf 1.1\n";
  16.  
  17.         foreach ( $conf as $k => $v ) $this->$k = $v;
  18.  
  19.         $this->dst || $this->dst = __DIR__ . "/out/" . basename($this->src);
  20.         $this->assert("src==dst", $this->src!=$this->dst);
  21.  
  22.         echo "{$this->src} -> {$this->dst}\n\n";
  23.  
  24.         $this->normalize_cond("skip");
  25.         $this->normalize_cond("copy");
  26.  
  27.         $this->assert("del dst", self::del_path($this->dst));
  28.         $this->assert("make dst", self::make_path($this->dst));
  29.  
  30.         $this->go($this->src, $this->dst);
  31.         $this->compile_js();
  32.  
  33.         $this->src = null;
  34.     }
  35.  
  36.     private function compile_js() {
  37.         if ( !$this->jscmd ) return;
  38.         $this->assert("jsout missing", $this->jsout);
  39.  
  40.         $lb = 0;
  41.         foreach ( $this->js as $path ) $lb += filesize($path);
  42.  
  43.         echo "\ncompiling js:\n";
  44.         echo "\e[33m{$this->jscmd}\e[0m\n";
  45.  
  46.         $res = shell_exec($this->jscmd);
  47.         echo $res;
  48.  
  49.         $la = filesize($this->jsout);
  50.         $ld = $la-$lb;
  51.         $perc = ($lb==0)?0:round($la/$lb*100-100);
  52.         if ( $ld>=0 ) $ld="\e[1;31m+$ld\e[0m";
  53.         echo "$lb$ld ($perc%)\n";
  54.     }
  55.  
  56.     private function go( $src, $dst, $rl=0 ) {
  57.         static $_dir = "\e[37m[dir]\e[0m";
  58.         static $_skip = "\e[36m[skip]\e[0m";
  59.         static $_copy = "\e[37m[copy]\e[0m";
  60.         static $_syntax = "\e[35m[syntax]\e[0m";
  61.         static $_strip = "\e[33m[strip]\e[0m";
  62.         static $_obf = "\e[31m[obf]\e[0m";
  63.  
  64.         $s = str_repeat("  ", $rl);
  65.         $ssrc = str_replace($this->src."/", "", $src);
  66.  
  67.         if ( isset($this->skip[$src]) ) {
  68.             echo "$s$_skip $ssrc\n";
  69.         } elseif ( isset($this->copy[$src]) ) {
  70.             $this->do_copy($s,$_copy,$ssrc,$src,$dst);
  71.         } elseif ( !is_dir($src) ) {
  72.             $ext = pathinfo($src, PATHINFO_EXTENSION);
  73.             switch ( $ext ) {
  74.                 case "html": case "css":
  75.                     echo "$s$_strip $ssrc ";
  76.                     $this->strip($ext, $src, $dst);
  77.                     break;
  78.                 case "js":
  79.                     if ( $this->jscmd ) {
  80.                         echo "$s$_obf $ssrc\n";
  81.                         $this->js[] = $src;
  82.                     } else {
  83.                         $this->do_copy($s,$_copy,$ssrc,$src,$dst);
  84.                     }
  85.                     break;
  86.                 default:
  87.                     $this->do_copy($s,$_copy,$ssrc,$src,$dst);
  88.             }
  89.         } elseif ( basename($src)==".svn" ) {
  90.             echo "$s$_skip $ssrc\n";
  91.         } else {
  92.             echo "$s$_dir $src\n";
  93.             $this->assert("make $dst", self::make_path($dst));
  94.             foreach ( scandir($src) as $fd ) {
  95.                 if ( $fd!="." && $fd!=".." ) $this->go("$src/$fd", "$dst/$fd", $rl+1);
  96.             }
  97.         }
  98.     }
  99.  
  100.     private function do_copy( $s,$c,$ssrc, $src, $dst ) {
  101.         echo "$s$c $ssrc\n";
  102.         $this->assert("copy $ssrc -> $dst", self::copy_path($src, $dst));
  103.     }
  104.  
  105.     private function normalize_cond( $arr ) {
  106.         $this->$arr || $this->$arr = array();
  107.         $ret = array();
  108.         foreach ( $this->$arr as $f ) {
  109.             $ret[$this->src."/$f"] = true;
  110.         }
  111.         $this->$arr = $ret;
  112.     }
  113.  
  114.     private function assert( $text, $expr ) {
  115.         if ( !$expr ) die("\e[1;31mError:\e[0m $text\n");
  116.         return $expr;
  117.     }
  118.  
  119.     private function strip( $type, $src, $dst ) {
  120.         $fu = "strip_$type";
  121.  
  122.         $text = $this->readf($src);
  123.         $lb = strlen($text);
  124.  
  125.         $text = $this->$fu($text);
  126.         $la = strlen($text);
  127.  
  128.         $ld = $la-$lb;
  129.         $perc = ($lb==0)?0:round($la/$lb*100-100);
  130.         if ( $ld>=0 ) $ld="\e[1;31m+$ld\e[0m";
  131.         echo "$lb$ld ($perc%)\n";
  132.  
  133.         $this->writef($dst, $text);
  134.     }
  135.  
  136.     private function readf( $path ) {
  137.         $data = file_get_contents($path);
  138.         $this->assert("read $path", $data!==false);
  139.         return $data;
  140.     }
  141.  
  142.     private function writef( $path, $data ) {
  143.         $this->assert("write $path", file_put_contents($path, $data)!==false);
  144.     }
  145.  
  146.     private function strip_html( $text ) {
  147.         $text = preg_replace(array(
  148.             "#<!--.*?-->#s",
  149.             "#\s+<#s",
  150.             "#>\s+#s",
  151.         ), array(
  152.             "",
  153.             "<",
  154.             ">",
  155.         ), $text);
  156.         $text = trim($text);
  157.         return $text;
  158.     }
  159.  
  160.     private function strip_css( $text ) {
  161.         $text = preg_replace(array(
  162.             "#/\*.*?\*/#s",
  163.             "#\s*([\}\{\:\;\,\(\)])\s*#s",
  164.             "#;}#s",
  165.         ), array(
  166.             "",
  167.             "$1",
  168.             "}",
  169.         ), $text);
  170.         $text = trim($text);
  171.         return $text;
  172.     }
  173.  
  174.     public function add_css( $file, $list ) {
  175.         $sig = "\x01css\x02";
  176.         $path = $this->dst . "/" . $file;
  177.         $text = $this->readf($path);
  178.         $add = "";
  179.  
  180.         foreach ( $list as $p ) {
  181.             $add .= "<style>".$this->readf($this->dst."/".$p)."</style>";
  182.         }
  183.  
  184.         $text = str_replace($sig, $add, $text);
  185.         $this->writef($path, $text);
  186.     }
  187.  
  188.     public function rem_css( $file, $list ) {
  189.         $sig = "\x01css\x02";
  190.         $path = $this->dst . "/" . $file;
  191.         $text = $this->readf($path);
  192.  
  193.         foreach ( $list as $p ) {
  194.             $text = preg_replace("#<link.*?href.*?{$p}.*?stylesheet.*?>#si", $sig, $text);
  195.             $text = preg_replace("#<link.*?stylesheet.*?href.*?{$p}.*?>#si", $sig, $text);
  196.         }
  197.  
  198.         $text = preg_replace("#\x01.*\x02#", $sig, $text);
  199.         $this->writef($path, $text);
  200.     }
  201.  
  202.     public function add_js( $file, $list ) {
  203.         $sig = "\x01js\x02";
  204.         $path = $this->dst . "/" . $file;
  205.         $text = $this->readf($path);
  206.         $add = "";
  207.  
  208.         foreach ( $list as $p ) {
  209.             $add .= "<script>".$this->readf($this->dst."/".$p)."</script>";
  210.         }
  211.  
  212.         $text = str_replace($sig, $add, $text);
  213.         $this->writef($path, $text);
  214.     }
  215.  
  216.     public function rem_js( $file, $list ) {
  217.         $sig = "\x01js\x02";
  218.         $path = $this->dst . "/" . $file;
  219.         $text = $this->readf($path);
  220.  
  221.         foreach ( $list as $p ) {
  222.             $text = preg_replace("#<script.*?src.*?{$p}.*?</script>#si", $sig, $text);
  223.         }
  224.  
  225.         $text = preg_replace("#\x01.*\x02#", $sig, $text);
  226.         $this->writef($path, $text);
  227.     }
  228.  
  229.     public function optimize_includes( $file ) {
  230.         $path = $this->dst . "/" . $file;
  231.         $text = $this->readf($path);
  232.  
  233.         $text = preg_replace(array(
  234.             "#<style>\s*</style>#si",
  235.             "#<script>\s*</script>#si",
  236.             "#</style>\s*<style>#si",
  237.             "#([^>])</script>\s*<script>([^<])#si",
  238.         ), array(
  239.             "",
  240.             "",
  241.             "",
  242.             "$1$2",
  243.         ), $text);
  244.  
  245.         $this->writef($path, $text);
  246.     }
  247.  
  248.     public static function normalize_path( $path ) {
  249.         $root = $path[0]=="/"?"/":"";
  250.         $parts = explode("/", trim($path, "/"));
  251.         $ret = array();
  252.         foreach ( $parts as $part ) {
  253.             if ( !$part || $part=="." ) continue;
  254.             elseif ( $part==".." ) array_pop($ret);
  255.             else array_push($ret, $part);
  256.         }
  257.         return $root . implode("/", $ret);
  258.     }
  259.  
  260.     public static function del_path( $path ) {
  261.         $path = self::normalize_path($path);
  262.  
  263.         if ( !file_exists($path) ) return true;
  264.         elseif ( !is_dir($path) )  return unlink($path);
  265.         else {
  266.             foreach ( scandir($path) as $fd ) {
  267.                 if ( $fd!="." && $fd!=".." && !self::del_path("$path/$fd") ) return false;
  268.             }
  269.             return rmdir($path);
  270.         }
  271.     }
  272.  
  273.     public static function make_path( $path, $mode=0755 ) {
  274.         $path = self::normalize_path($path);
  275.  
  276.         is_dir(dirname($path)) || self::make_path(dirname($path), $mode);
  277.         return   is_dir($path) || mkdir($path, $mode);
  278.     }
  279.  
  280.     public static function copy_path( $src , $dst, $mode=0755 ) {
  281.         $src = self::normalize_path($src);
  282.         $dst = self::normalize_path($dst);
  283.  
  284.         if ( $src==$dst ) {
  285.             return true;
  286.         } elseif ( !file_exists($src) ) {
  287.             return false;
  288.         } elseif ( !is_dir($src) ) {
  289.             return !file_exists($dst) ?
  290.                 self::make_path(dirname($dst), $mode) && copy($src, $dst) :
  291.                 self::del_path($dst) && copy($src, $dst);
  292.         } else {
  293.             if ( !self::del_path($dst) || !self::make_path($dst, $mode) ) return false;
  294.             foreach ( scandir($src) as $fd ) {
  295.                 if ( $fd!="." && $fd!=".." && !self::copy_path("$src/$fd", "$dst/$fd") ) return false;
  296.             }
  297.             return true;
  298.         }
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement