Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // http://code.google.com/p/closure-compiler/wiki/Warnings
- class Obf {
- private $src;
- private $dst;
- private $skip;
- private $copy;
- private $js = array();
- private $jsout;
- private $jscmd;
- public function __construct( $conf ) {
- echo "php obf 1.1\n";
- foreach ( $conf as $k => $v ) $this->$k = $v;
- $this->dst || $this->dst = __DIR__ . "/out/" . basename($this->src);
- $this->assert("src==dst", $this->src!=$this->dst);
- echo "{$this->src} -> {$this->dst}\n\n";
- $this->normalize_cond("skip");
- $this->normalize_cond("copy");
- $this->assert("del dst", self::del_path($this->dst));
- $this->assert("make dst", self::make_path($this->dst));
- $this->go($this->src, $this->dst);
- $this->compile_js();
- $this->src = null;
- }
- private function compile_js() {
- if ( !$this->jscmd ) return;
- $this->assert("jsout missing", $this->jsout);
- $lb = 0;
- foreach ( $this->js as $path ) $lb += filesize($path);
- echo "\ncompiling js:\n";
- echo "\e[33m{$this->jscmd}\e[0m\n";
- $res = shell_exec($this->jscmd);
- echo $res;
- $la = filesize($this->jsout);
- $ld = $la-$lb;
- $perc = ($lb==0)?0:round($la/$lb*100-100);
- if ( $ld>=0 ) $ld="\e[1;31m+$ld\e[0m";
- echo "$lb$ld ($perc%)\n";
- }
- private function go( $src, $dst, $rl=0 ) {
- static $_dir = "\e[37m[dir]\e[0m";
- static $_skip = "\e[36m[skip]\e[0m";
- static $_copy = "\e[37m[copy]\e[0m";
- static $_syntax = "\e[35m[syntax]\e[0m";
- static $_strip = "\e[33m[strip]\e[0m";
- static $_obf = "\e[31m[obf]\e[0m";
- $s = str_repeat(" ", $rl);
- $ssrc = str_replace($this->src."/", "", $src);
- if ( isset($this->skip[$src]) ) {
- echo "$s$_skip $ssrc\n";
- } elseif ( isset($this->copy[$src]) ) {
- $this->do_copy($s,$_copy,$ssrc,$src,$dst);
- } elseif ( !is_dir($src) ) {
- $ext = pathinfo($src, PATHINFO_EXTENSION);
- switch ( $ext ) {
- case "html": case "css":
- echo "$s$_strip $ssrc ";
- $this->strip($ext, $src, $dst);
- break;
- case "js":
- if ( $this->jscmd ) {
- echo "$s$_obf $ssrc\n";
- $this->js[] = $src;
- } else {
- $this->do_copy($s,$_copy,$ssrc,$src,$dst);
- }
- break;
- default:
- $this->do_copy($s,$_copy,$ssrc,$src,$dst);
- }
- } elseif ( basename($src)==".svn" ) {
- echo "$s$_skip $ssrc\n";
- } else {
- echo "$s$_dir $src\n";
- $this->assert("make $dst", self::make_path($dst));
- foreach ( scandir($src) as $fd ) {
- if ( $fd!="." && $fd!=".." ) $this->go("$src/$fd", "$dst/$fd", $rl+1);
- }
- }
- }
- private function do_copy( $s,$c,$ssrc, $src, $dst ) {
- echo "$s$c $ssrc\n";
- $this->assert("copy $ssrc -> $dst", self::copy_path($src, $dst));
- }
- private function normalize_cond( $arr ) {
- $this->$arr || $this->$arr = array();
- $ret = array();
- foreach ( $this->$arr as $f ) {
- $ret[$this->src."/$f"] = true;
- }
- $this->$arr = $ret;
- }
- private function assert( $text, $expr ) {
- if ( !$expr ) die("\e[1;31mError:\e[0m $text\n");
- return $expr;
- }
- private function strip( $type, $src, $dst ) {
- $fu = "strip_$type";
- $text = $this->readf($src);
- $lb = strlen($text);
- $text = $this->$fu($text);
- $la = strlen($text);
- $ld = $la-$lb;
- $perc = ($lb==0)?0:round($la/$lb*100-100);
- if ( $ld>=0 ) $ld="\e[1;31m+$ld\e[0m";
- echo "$lb$ld ($perc%)\n";
- $this->writef($dst, $text);
- }
- private function readf( $path ) {
- $data = file_get_contents($path);
- $this->assert("read $path", $data!==false);
- return $data;
- }
- private function writef( $path, $data ) {
- $this->assert("write $path", file_put_contents($path, $data)!==false);
- }
- private function strip_html( $text ) {
- $text = preg_replace(array(
- "#<!--.*?-->#s",
- "#\s+<#s",
- "#>\s+#s",
- ), array(
- "",
- "<",
- ">",
- ), $text);
- $text = trim($text);
- return $text;
- }
- private function strip_css( $text ) {
- $text = preg_replace(array(
- "#/\*.*?\*/#s",
- "#\s*([\}\{\:\;\,\(\)])\s*#s",
- "#;}#s",
- ), array(
- "",
- "$1",
- "}",
- ), $text);
- $text = trim($text);
- return $text;
- }
- public function add_css( $file, $list ) {
- $sig = "\x01css\x02";
- $path = $this->dst . "/" . $file;
- $text = $this->readf($path);
- $add = "";
- foreach ( $list as $p ) {
- $add .= "<style>".$this->readf($this->dst."/".$p)."</style>";
- }
- $text = str_replace($sig, $add, $text);
- $this->writef($path, $text);
- }
- public function rem_css( $file, $list ) {
- $sig = "\x01css\x02";
- $path = $this->dst . "/" . $file;
- $text = $this->readf($path);
- foreach ( $list as $p ) {
- $text = preg_replace("#<link.*?href.*?{$p}.*?stylesheet.*?>#si", $sig, $text);
- $text = preg_replace("#<link.*?stylesheet.*?href.*?{$p}.*?>#si", $sig, $text);
- }
- $text = preg_replace("#\x01.*\x02#", $sig, $text);
- $this->writef($path, $text);
- }
- public function add_js( $file, $list ) {
- $sig = "\x01js\x02";
- $path = $this->dst . "/" . $file;
- $text = $this->readf($path);
- $add = "";
- foreach ( $list as $p ) {
- $add .= "<script>".$this->readf($this->dst."/".$p)."</script>";
- }
- $text = str_replace($sig, $add, $text);
- $this->writef($path, $text);
- }
- public function rem_js( $file, $list ) {
- $sig = "\x01js\x02";
- $path = $this->dst . "/" . $file;
- $text = $this->readf($path);
- foreach ( $list as $p ) {
- $text = preg_replace("#<script.*?src.*?{$p}.*?</script>#si", $sig, $text);
- }
- $text = preg_replace("#\x01.*\x02#", $sig, $text);
- $this->writef($path, $text);
- }
- public function optimize_includes( $file ) {
- $path = $this->dst . "/" . $file;
- $text = $this->readf($path);
- $text = preg_replace(array(
- "#<style>\s*</style>#si",
- "#<script>\s*</script>#si",
- "#</style>\s*<style>#si",
- "#([^>])</script>\s*<script>([^<])#si",
- ), array(
- "",
- "",
- "",
- "$1$2",
- ), $text);
- $this->writef($path, $text);
- }
- public static function normalize_path( $path ) {
- $root = $path[0]=="/"?"/":"";
- $parts = explode("/", trim($path, "/"));
- $ret = array();
- foreach ( $parts as $part ) {
- if ( !$part || $part=="." ) continue;
- elseif ( $part==".." ) array_pop($ret);
- else array_push($ret, $part);
- }
- return $root . implode("/", $ret);
- }
- public static function del_path( $path ) {
- $path = self::normalize_path($path);
- if ( !file_exists($path) ) return true;
- elseif ( !is_dir($path) ) return unlink($path);
- else {
- foreach ( scandir($path) as $fd ) {
- if ( $fd!="." && $fd!=".." && !self::del_path("$path/$fd") ) return false;
- }
- return rmdir($path);
- }
- }
- public static function make_path( $path, $mode=0755 ) {
- $path = self::normalize_path($path);
- is_dir(dirname($path)) || self::make_path(dirname($path), $mode);
- return is_dir($path) || mkdir($path, $mode);
- }
- public static function copy_path( $src , $dst, $mode=0755 ) {
- $src = self::normalize_path($src);
- $dst = self::normalize_path($dst);
- if ( $src==$dst ) {
- return true;
- } elseif ( !file_exists($src) ) {
- return false;
- } elseif ( !is_dir($src) ) {
- return !file_exists($dst) ?
- self::make_path(dirname($dst), $mode) && copy($src, $dst) :
- self::del_path($dst) && copy($src, $dst);
- } else {
- if ( !self::del_path($dst) || !self::make_path($dst, $mode) ) return false;
- foreach ( scandir($src) as $fd ) {
- if ( $fd!="." && $fd!=".." && !self::copy_path("$src/$fd", "$dst/$fd") ) return false;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement