retesere20

js-css-minify

Dec 10th, 2019
135
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // ================ js obfuscation ============= //
  3. // https://www.javascriptdeobfuscator.com/
  4. // http://deobfuscatejavascript.com/
  5. public function get_obfuscated_wiseloop($content, $ar=array()){
  6. $array= array(
  7. 'js' => $content,
  8. 'doDecoy' => (!empty($ar["doDecoy"])? $ar["doDecoy"] : false),
  9. 'doMinify' => (!empty($ar["doMinify"])? $ar["doMinify"] : true),
  10. 'doLockDomain' =>(!empty($ar["doLockDomain"])? $ar["doLockDomain"] : false),
  11. 'doScrambleVars'=>(!empty($ar["doScrambleVars"])? $ar["doScrambleVars"] : false),
  12. 'encryptionLevel'=>(!empty($ar["encryptionLevel"])? $ar["encryptionLevel"] : false),
  13. );
  14. return get_remote_data('http://wiseloop.com/wl.cms.storage.ext/wiseloop/php-javascript-obfuscator/bin/jso.php', $array);
  15. }
  16.  
  17. public function get_obfuscated_heroku_app($content, $ar=array()){
  18. $array= array(
  19. "code" => $content,
  20. "options" => array (
  21. "compact" => (!empty($ar["compact"])? $ar["compact"] : true),
  22. "selfDefending" => (!empty($ar["selfDefending"])? $ar["selfDefending"] : true),
  23. "disableConsoleOutput" => (!empty($ar["disableConsoleOutput"])? $ar["disableConsoleOutput"] : false),
  24. "debugProtection" => (!empty($ar["debugProtection"])? $ar["debugProtection"] : false),
  25. "debugProtectionInterval" => (!empty($ar["debugProtectionInterval"])? $ar["debugProtectionInterval"] : false),
  26. "stringArray" => (!empty($ar["stringArray"])? $ar["stringArray"] : true),
  27. "rotateStringArray" => (!empty($ar["rotateStringArray"])? $ar["rotateStringArray"] : true),
  28. "rotateStringArrayEnabled" => (!empty($ar["rotateStringArrayEnabled"])? $ar["rotateStringArrayEnabled"] : true),
  29. "stringArrayThreshold" => (!empty($ar["stringArrayThreshold"])? $ar["stringArrayThreshold"] : 0.8),
  30. "stringArrayThresholdEnabled"=>(!empty($ar["stringArrayThresholdEnabled"])? $ar["stringArrayThresholdEnabled"] : false),
  31. "stringArrayEncoding" => (!empty($ar["stringArrayEncoding"])? $ar["stringArrayEncoding"] : "false"),
  32. "stringArrayEncodingEnabled"=> (!empty($ar["stringArrayEncodingEnabled"])? $ar["stringArrayEncodingEnabled"] : true),
  33. "sourceMap" => (!empty($ar["sourceMap"])? $ar["sourceMap"] : false),
  34. "sourceMapMode" => (!empty($ar["sourceMapMode"])? $ar["sourceMapMode"] : "off"),
  35. "sourceMapBaseUrl" => (!empty($ar["sourceMapBaseUrl"])? $ar["sourceMapBaseUrl"] : ""),
  36. "sourceMapFileName" => (!empty($ar["sourceMapFileName"])? $ar["sourceMapFileName"] : ""),
  37. "sourceMapSeparate" => (!empty($ar["sourceMapSeparate"])? $ar["sourceMapSeparate"] : false),
  38. "domainLock" => (!empty($ar["domainLock"])? $ar["domainLock"] : array()),
  39. "reservedNames" => (!empty($ar["reservedNames"])? $ar["reservedNames"] : array()),
  40. "seed" => (!empty($ar["seed"])? $ar["seed"] : 0),
  41. "controlFlowFlatteningThreshold"=> (!empty($ar["controlFlowFlatteningThreshold"])? $ar["controlFlowFlatteningThreshold"] : 0.75),
  42. "controlFlowFlattening" => (!empty($ar["controlFlowFlattening"])? $ar["controlFlowFlattening"] : false),
  43. "deadCodeInjectionThreshold"=> (!empty($ar["deadCodeInjectionThreshold"])? $ar["deadCodeInjectionThreshold"] : 0.4),
  44. "deadCodeInjection" => (!empty($ar["deadCodeInjection"])? $ar["deadCodeInjection"] : false),
  45. "mangle" => (!empty($ar["mangle"])? $ar["mangle"] : false),
  46. "unicodeEscapeSequence" => (!empty($ar["unicodeEscapeSequence"])? $ar["unicodeEscapeSequence"] : false),
  47. "renameGlobals" => (!empty($ar["renameGlobals"])? $ar["renameGlobals"] : false),
  48. "target" => (!empty($ar["target"])? $ar["target"] : "browser"),
  49. )
  50. );
  51. $answer= get_remote_data('https://javascriptobfuscator.herokuapp.com/obfuscate', json_encode($array) );
  52. return json_decode($answer,true)["code"];
  53. }
  54.  
  55. public function get_minified($content, $ar=array()){
  56. $array= array(
  57. 'output_format' => 'json',
  58. //'output_info' => 'warnings',
  59. //'output_info' => 'errors',
  60. //'output_info' => 'statistics',
  61. 'output_info' => 'compiled_code',
  62. 'compilation_level' => (!empty($ar["compilation_level"])? $ar["compilation_level"] : "SIMPLE_OPTIMIZATIONS"), // 'SIMPLE_OPTIMIZATIONS', //WHITESPACE_ONLY, ADVANCED_OPTIMIZATIONS
  63. 'warning_level' => 'default',
  64. 'output_file_name' => 'default.js',
  65. 'js_code' => $content
  66. );
  67. $answer= get_remote_data('https://closure-compiler.appspot.com/compile', $array );
  68. return json_decode($answer,true)["compiledCode"];
  69. }
  70.  
  71. public function get_obfuscated_js($content, $level){
  72. if (empty($level))
  73. $content=$content;
  74. if (!empty($level['heroku']) || in_array('heroku',$level) )
  75. $content=get_obfuscated_heroku_app($content, array() );
  76. if (!empty($level['wiseloop']) || in_array('wiseloop',$level) )
  77. $content=get_obfuscated_wiseloop($content, array() );
  78. if (!empty($level['closure']) || in_array('closure',$level) )
  79. $content=get_minified($content, array() );
  80. return $content;
  81. //return get_remote_data('https://itask.software/tools/js-obfuscator', array('js_content'=>$content, 'obfuscate_wl'=>0, 'obfuscate_closure'=>1, 'obfuscate_heroku'=>0) );
  82. }
  83.  
  84.  
  85. public function output_obufscated_js($func_name=false, $level=array(), $dir=__DIR__, $enable_localhost=false)
  86. {
  87. //vx(debug_backtrace()[1]['function']);
  88. $javascriptCode = get_current_buffer_clean($func_name);
  89. header("Content-type: application/javascript");
  90. if(!defined('$this->test_environment')) { define('$this->test_environment', ((in_array($_SERVER['HTTP_HOST'], array('localhost','127.0.0.1' ) )) ? true : false ) ); }
  91.  
  92. if($enable_localhost && $this->test_environment) {
  93. return $javascriptCode;
  94. }
  95. else{
  96. $file= ($dir ?: __DIR__) .'/___tmp_my_scripts_encoded_'.md5($javascriptCode).'.js';
  97. if(file_exists($file) || $this->file_put_contents($file, get_obfuscated_js($javascriptCode, $level))){
  98. return $this->file_get_contents($file);
  99. }
  100. }
  101. }
  102.  
  103. // ============ obfuscated js ===========//
  104.  
  105.  
  106.  
  107. // css minify //
  108. public function minify_css2($css_content)
  109. {
  110. // some of the following functions to minimize the css-output are directly taken
  111. // from the awesome CSS JS Booster: https://github.com/Schepp/CSS-JS-Booster
  112. // all credits to Christian Schaefer: http://twitter.com/derSchepp
  113. // remove comments
  114. $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
  115. // backup values within single or double quotes
  116. preg_match_all('/(\'[^\']*?\'|"[^"]*?")/ims', $css, $hit, PREG_PATTERN_ORDER);
  117. for ($i=0; $i < count($hit[1]); $i++) {
  118. $css = str_replace($hit[1][$i], '##########' . $i . '##########', $css);
  119. }
  120. // remove traling semicolon of selector's last property
  121. $css = preg_replace('/;[\s\r\n\t]*?}[\s\r\n\t]*/ims', "}\r\n", $css);
  122. // remove any whitespace between semicolon and property-name
  123. $css = preg_replace('/;[\s\r\n\t]*?([\r\n]?[^\s\r\n\t])/ims', ';$1', $css);
  124. // remove any whitespace surrounding property-colon
  125. $css = preg_replace('/[\s\r\n\t]*:[\s\r\n\t]*?([^\s\r\n\t])/ims', ':$1', $css);
  126. // remove any whitespace surrounding selector-comma
  127. $css = preg_replace('/[\s\r\n\t]*,[\s\r\n\t]*?([^\s\r\n\t])/ims', ',$1', $css);
  128. // remove any whitespace surrounding opening parenthesis
  129. $css = preg_replace('/[\s\r\n\t]*{[\s\r\n\t]*?([^\s\r\n\t])/ims', '{$1', $css);
  130. // remove any whitespace between numbers and units
  131. $css = preg_replace('/([\d\.]+)[\s\r\n\t]+(px|em|pt|%)/ims', '$1$2', $css);
  132. // shorten zero-values
  133. $css = preg_replace('/([^\d\.]0)(px|em|pt|%)/ims', '$1', $css);
  134. // constrain multiple whitespaces
  135. $css = preg_replace('/\p{Zs}+/ims',' ', $css);
  136. // remove newlines
  137. $css = str_replace(array("\r\n", "\r", "\n"), '', $css);
  138. // Restore backupped values within single or double quotes
  139. for ($i=0; $i < count($hit[1]); $i++) {
  140. $css = str_replace('##########' . $i . '##########', $hit[1][$i], $css);
  141. }
  142. return $css;
  143. }
  144.  
  145. public function minify_css1($css_content)
  146. {
  147. $url = 'https://cssminifier.com/raw';
  148. $ch = curl_init();
  149. curl_setopt_array($ch, [
  150. CURLOPT_URL => $url,
  151. CURLOPT_RETURNTRANSFER => true,
  152. CURLOPT_POST => true,
  153. CURLOPT_HTTPHEADER => ["Content-Type: application/x-www-form-urlencoded"],
  154. CURLOPT_POSTFIELDS => http_build_query([ "input" => $css_content ])
  155. ]);
  156. $minified = curl_exec($ch);
  157. curl_close($ch);
  158. return $minified;
  159. }
RAW Paste Data