1+ <?php
2+ /*
3+ * OWASP Enterprise Security API (ESAPI)
4+ *
5+ * This file is part of the Open Web Application Security Project (OWASP)
6+ * Enterprise Security API (ESAPI) project. For details, please see
7+ * <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
8+ *
9+ * Copyright (c) 2008 - The OWASP Foundation
10+ *
11+ * The ESAPI is published by OWASP under the BSD license. You should read and accept the
12+ * LICENSE before you use, modify, and/or redistribute this software.
13+ */
14+
15+ $ SOURCE_DIR = "src/main/javascript/ " ;
16+ $ COMPLETE_FILES = array ( "core.js " , "esapi.properties.js " , "org.owasp.esapi.js " , "org.owasp.esapi.codecs.js " , "org.owasp.esapi.reference.encoding.js " );
17+
18+ $ OUTPUT_DIR = "dist/ " ;
19+ $ OUTPUT_FILE = "esapi.js " ;
20+ $ OUTPUT_FILE_COMPRESSED = "esapi-compressed.js " ;
21+
22+ $ JDK_HOME = "/home/cschmidt/jdk1.6.0_14/ " ;
23+ $ YUI_COMPRESSOR_JAR = "lib/yuicompressor-2.4.2.jar " ;
24+ $ TMP_DIR = "tmp/ " ;
25+
26+ mkdir ($ TMP_DIR , 0700 );
27+ mkdir ($ OUTPUT_DIR , 0700 );
28+ unlink ($ OUTPUT_DIR .$ OUTPUT_FILE );
29+ unlink ($ OUTPUT_DIR .$ OUTPUT_FILE_COMPRESSED );
30+
31+ /**
32+ * Compress Javascript/CSS using the YUI Compressor
33+ *
34+ * You must set $jarFile and $tempDir before calling the minify functions.
35+ * Also, depending on your shell's environment, you may need to specify
36+ * the full path to java in $javaExecutable or use putenv() to setup the
37+ * Java environment.
38+ *
39+ * <code>
40+ * Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.3.5.jar';
41+ * Minify_YUICompressor::$tempDir = '/tmp';
42+ * $code = Minify_YUICompressor::minifyJs(
43+ * $code
44+ * ,array('nomunge' => true, 'line-break' => 1000)
45+ * );
46+ * </code>
47+ *
48+ * @todo unit tests, $options docs
49+ *
50+ * @package Minify
51+ * @author Stephen Clay <[email protected] > 52+ */
53+ class Minify_YUICompressor {
54+
55+ /**
56+ * Filepath of the YUI Compressor jar file. This must be set before
57+ * calling minifyJs() or minifyCss().
58+ *
59+ * @var string
60+ */
61+ public static $ jarFile = null ;
62+
63+ /**
64+ * Writable temp directory. This must be set before calling minifyJs()
65+ * or minifyCss().
66+ *
67+ * @var string
68+ */
69+ public static $ tempDir = null ;
70+
71+ /**
72+ * Filepath of "java" executable (may be needed if not in shell's PATH)
73+ *
74+ * @var string
75+ */
76+ public static $ javaExecutable = 'java ' ;
77+
78+ /**
79+ * Minify a Javascript string
80+ *
81+ * @param string $js
82+ *
83+ * @param array $options (verbose is ignored)
84+ *
85+ * @see http://www.julienlecomte.net/yuicompressor/README
86+ *
87+ * @return string
88+ */
89+ public static function minifyJs ($ js , $ options = array ())
90+ {
91+ return self ::_minify ('js ' , $ js , $ options );
92+ }
93+
94+ /**
95+ * Minify a CSS string
96+ *
97+ * @param string $css
98+ *
99+ * @param array $options (verbose is ignored)
100+ *
101+ * @see http://www.julienlecomte.net/yuicompressor/README
102+ *
103+ * @return string
104+ */
105+ public static function minifyCss ($ css , $ options = array ())
106+ {
107+ return self ::_minify ('css ' , $ css , $ options );
108+ }
109+
110+ private static function _minify ($ type , $ content , $ options )
111+ {
112+ self ::_prepare ();
113+ if (! ($ tmpFile = tempnam (self ::$ tempDir , 'yuic_ ' ))) {
114+ throw new Exception ('Minify_YUICompressor : could not create temp file. ' );
115+ }
116+ file_put_contents ($ tmpFile , $ content );
117+ exec (self ::_getCmd ($ options , $ type , $ tmpFile ), $ output );
118+ unlink ($ tmpFile );
119+ return implode ("\n" , $ output );
120+ }
121+
122+ private static function _getCmd ($ userOptions , $ type , $ tmpFile )
123+ {
124+ $ o = array_merge (
125+ array (
126+ 'charset ' => ''
127+ ,'line-break ' => 5000
128+ ,'type ' => $ type
129+ ,'nomunge ' => false
130+ ,'preserve-semi ' => false
131+ ,'disable-optimizations ' => false
132+ )
133+ ,$ userOptions
134+ );
135+ $ cmd = self ::$ javaExecutable . ' -jar ' . escapeshellarg (self ::$ jarFile )
136+ . " --type {$ type }"
137+ . (preg_match ('/^[a-zA-Z \\-]+$/ ' , $ o ['charset ' ])
138+ ? " --charset {$ o ['charset ' ]}"
139+ : '' )
140+ . (is_numeric ($ o ['line-break ' ]) && $ o ['line-break ' ] >= 0
141+ ? ' --line-break ' . (int )$ o ['line-break ' ]
142+ : '' );
143+ if ($ type === 'js ' ) {
144+ foreach (array ('nomunge ' , 'preserve-semi ' , 'disable-optimizations ' ) as $ opt ) {
145+ $ cmd .= $ o [$ opt ]
146+ ? " -- {$ opt }"
147+ : '' ;
148+ }
149+ }
150+ return $ cmd . ' ' . escapeshellarg ($ tmpFile );
151+ }
152+
153+ private static function _prepare ()
154+ {
155+ if (! is_file (self ::$ jarFile )
156+ || ! is_dir (self ::$ tempDir )
157+ || ! is_writable (self ::$ tempDir )
158+ ) {
159+ throw new Exception ('Minify_YUICompressor : $jarFile and $tempDir must be set. ' );
160+ }
161+ }
162+ }
163+
164+
165+ echo ("Building Uncompressed File: $ OUTPUT_DIR . $ OUTPUT_FILE \n\r" );
166+
167+ $ fpOut = fopen ( $ OUTPUT_DIR .$ OUTPUT_FILE , "w " );
168+
169+ foreach ($ COMPLETE_FILES as $ fileName ) {
170+ echo ("- Reading $ fileName \n" );
171+ $ fp = fopen ( $ SOURCE_DIR .$ fileName , "r " );
172+ $ contents = "" ;
173+ while (!feof ($ fp )) {
174+ $ contents .= fgets ($ fp );
175+ }
176+ fwrite ( $ fpOut , $ contents );
177+ fclose ($ fp );
178+ }
179+
180+ fclose ($ fpOut );
181+
182+ echo ("Building Compressed File: $ OUTPUT_DIR . $ OUTPUT_FILE_COMPRESSED \n\r" );
183+
184+ Minify_YUICompressor::$ jarFile = $ YUI_COMPRESSOR_JAR ;
185+ Minify_YUICompressor::$ tempDir = $ TMP_DIR ;
186+ Minify_YUICompressor::$ javaExecutable = $ JDK_HOME ."/bin/java " ;
187+
188+ $ fp = fopen ( $ OUTPUT_DIR .$ OUTPUT_FILE , "r " );
189+ $ uncompressed = "" ;
190+ while (!feof ($ fp )) {
191+ $ uncompressed .= fgets ( $ fp );
192+ }
193+ fclose ($ fp );
194+
195+ $ compressed = Minify_YUICompressor::minifyJs ( $ uncompressed , array ());
196+
197+ $ fp = fopen ( $ OUTPUT_DIR .$ OUTPUT_FILE_COMPRESSED , "w " );
198+ fwrite ( $ fp , $ compressed );
199+ fclose ($ fp );
200+
201+ ?>
0 commit comments