通过php实现对js的加密混淆

kevin.Zhu 发布于:2013-1-16 15:38 分类:Php  有 14 人浏览,获得评论 0 条  


使用php对js进行混淆加密,具体方法如下:



<?php //js路径 $jsPath = __DIR__.'/assets/js/*'; //不需要压缩的JS $exclude = array(     'jQuery.ui.position.min.js',     'easy_validator.pack.js',     'easydialog.min.js',     'jquery.ad-gallery.js',     'jquery.autocomplete.js',     'jquery.bgiframe.min.js',     'jquery.fancybox-1.3.4.pack.js',     'jquery.min.js',     'jquery.mousewheel-3.0.4.pack.js',     'jquery.uploadify.js',     'jquery.validationEngine-zh_CN.js',     'jquery.validationEngine.js',     'exportting.js',     'highcharts.js',     'jquery.bgiframe.min.js', );   //js混淆加密并更换名称    foreach(glob($jsPath) as $filePath) {     $fileName = array_pop(explode('/',$filePath));     if( !in_array($fileName,$exclude) )     {         $content = file_get_contents($filePath);         //js混淆         $params = array(             'code' => $content,             'operate' => 'uglify',         );         
        $response = remote::post($params);         if( $response['status'] == true)         {             //js加密             $params = array(                 'operate' => 'pack',                 'code' => $response['text'],             );             $response = remote::post($params);             if( $response['status'] == true)             {                 $text = $response['text'];                 file_put_contents($filePath,$text);                 //更换js文件名                 $id = uuid(false).'.js';                 $newPath = str_replace($fileName,$id,$filePath);                 rename($filePath,$newPath);                 error_log("{$filePath}======>{$newPath}\n", 3, "log.txt");             }         }            
    }     else     {         $id = uuid(false).'.js';         $newPath = str_replace($fileName,$id,$filePath);         rename($filePath,$newPath);         error_log("{$filePath}======>{$newPath}\n", 3, "log.txt");     }     sleep(1); } class remote {     public static $timeout = 30;     const HTTP_OK = 200;     const HTTP_FAIL = 401;     const HTTP_ERROR = 500;     public static function post($params=array())     {         $paramss = '';         $headers[] = 'Accept    application/json, text/javascript, */*; q=0.01';         $headers[] = 'Content-Type  application/x-www-form-urlencoded; charset=UTF-8';         $headers[] = 'Accept-Language   zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3';         $user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0';         $ch = curl_init('http://tool.lu/js/ajax.html');         curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);         curl_setopt($ch,CURLOPT_REFERER,'http://tool.lu/js/');         curl_setopt($ch,CURLOPT_USERAGENT,$user_agent);         curl_setopt($ch,CURLOPT_TIMEOUT,self::$timeout);         curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);         if(!empty($params)) {             curl_setopt($ch,CURLOPT_POST, 1);             curl_setopt($ch,CURLOPT_POSTFIELDS,$params);         }         $response = curl_exec($ch);         $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);         switch($status) {             case self::HTTP_OK:             case self::HTTP_FAIL:                 $json = json_decode($response, true);                 break;             case self::HTTP_ERROR:             default:                 $json = null;                 break;         }         curl_close($ch);         return $json;     } } function uuid( $more = true, $admin = false ) {     if ( $more )     {         return uniqid( md5( mt_rand() ), true );     }     else     {         if ( $admin ) return uniqid( "zzz", false );         return uniqid( create_guid_section( 3 ), false );     } } function create_guid_section( $characters ) {     $return = "";     for ( $i = 0; $i < $characters; $i++ )     {         $return .= dechex( mt_rand( 0, 15 ) );     }     return $return; }