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+ /**
16+ * Delete a file, or a folder and its contents (recursive algorithm)
17+ *
18+ * @author Aidan Lister <[email protected] > 19+ * @version 1.0.3
20+ * @link http://aidanlister.com/repos/v/function.rmdirr.php
21+ * @param string $dirname Directory to delete
22+ * @return bool Returns TRUE on success, FALSE on failure
23+ */
24+ function rmdirr ($ dirname )
25+ {
26+ // Sanity check
27+ if (!file_exists ($ dirname )) {
28+ return false ;
29+ }
30+
31+ // Simple delete for a file
32+ if (is_file ($ dirname ) || is_link ($ dirname )) {
33+ return unlink ($ dirname );
34+ }
35+
36+ // Loop through the folder
37+ $ dir = dir ($ dirname );
38+ while (false !== $ entry = $ dir ->read ()) {
39+ // Skip pointers
40+ if ($ entry == '. ' || $ entry == '.. ' || $ entry == '.svn ' ) {
41+ continue ;
42+ }
43+
44+ // Recurse
45+ rmdirr ($ dirname . DIRECTORY_SEPARATOR . $ entry );
46+ }
47+
48+ // Clean up
49+ $ dir ->close ();
50+ return ;// rmdir($dirname);
51+ }
52+
53+ /**
54+ * Compress Javascript/CSS using the YUI Compressor
55+ *
56+ * You must set $jarFile and $tempDir before calling the minify functions.
57+ * Also, depending on your shell's environment, you may need to specify
58+ * the full path to java in $javaExecutable or use putenv() to setup the
59+ * Java environment.
60+ *
61+ * <code>
62+ * Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.3.5.jar';
63+ * Minify_YUICompressor::$tempDir = '/tmp';
64+ * $code = Minify_YUICompressor::minifyJs(
65+ * $code
66+ * ,array('nomunge' => true, 'line-break' => 1000)
67+ * );
68+ * </code>
69+ *
70+ * @todo unit tests, $options docs
71+ *
72+ * @package Minify
73+ * @author Stephen Clay <[email protected] > 74+ */
75+ class Minify_YUICompressor {
76+
77+ /**
78+ * Filepath of the YUI Compressor jar file. This must be set before
79+ * calling minifyJs() or minifyCss().
80+ *
81+ * @var string
82+ */
83+ public static $ jarFile = null ;
84+
85+ /**
86+ * Writable temp directory. This must be set before calling minifyJs()
87+ * or minifyCss().
88+ *
89+ * @var string
90+ */
91+ public static $ tempDir = null ;
92+
93+ /**
94+ * Filepath of "java" executable (may be needed if not in shell's PATH)
95+ *
96+ * @var string
97+ */
98+ public static $ javaExecutable = 'java ' ;
99+
100+ /**
101+ * Minify a Javascript string
102+ *
103+ * @param string $js
104+ *
105+ * @param array $options (verbose is ignored)
106+ *
107+ * @see http://www.julienlecomte.net/yuicompressor/README
108+ *
109+ * @return string
110+ */
111+ public static function minifyJs ($ js , $ options = array ())
112+ {
113+ return self ::_minify ('js ' , $ js , $ options );
114+ }
115+
116+ /**
117+ * Minify a CSS string
118+ *
119+ * @param string $css
120+ *
121+ * @param array $options (verbose is ignored)
122+ *
123+ * @see http://www.julienlecomte.net/yuicompressor/README
124+ *
125+ * @return string
126+ */
127+ public static function minifyCss ($ css , $ options = array ())
128+ {
129+ return self ::_minify ('css ' , $ css , $ options );
130+ }
131+
132+ private static function _minify ($ type , $ content , $ options )
133+ {
134+ self ::_prepare ();
135+ if (! ($ tmpFile = tempnam (self ::$ tempDir , 'yuic_ ' ))) {
136+ throw new Exception ('Minify_YUICompressor : could not create temp file. ' );
137+ }
138+ file_put_contents ($ tmpFile , $ content );
139+ exec (self ::_getCmd ($ options , $ type , $ tmpFile ), $ output );
140+ unlink ($ tmpFile );
141+ return implode ("\n" , $ output );
142+ }
143+
144+ private static function _getCmd ($ userOptions , $ type , $ tmpFile )
145+ {
146+ $ o = array_merge (
147+ array (
148+ 'charset ' => ''
149+ ,'line-break ' => 5000
150+ ,'type ' => $ type
151+ ,'nomunge ' => false
152+ ,'preserve-semi ' => false
153+ ,'disable-optimizations ' => false
154+ )
155+ ,$ userOptions
156+ );
157+ $ cmd = self ::$ javaExecutable . ' -jar ' . escapeshellarg (self ::$ jarFile )
158+ . " --type {$ type }"
159+ . (preg_match ('/^[a-zA-Z \\-]+$/ ' , $ o ['charset ' ])
160+ ? " --charset {$ o ['charset ' ]}"
161+ : '' )
162+ . (is_numeric ($ o ['line-break ' ]) && $ o ['line-break ' ] >= 0
163+ ? ' --line-break ' . (int )$ o ['line-break ' ]
164+ : '' );
165+ if ($ type === 'js ' ) {
166+ foreach (array ('nomunge ' , 'preserve-semi ' , 'disable-optimizations ' ) as $ opt ) {
167+ $ cmd .= $ o [$ opt ]
168+ ? " -- {$ opt }"
169+ : '' ;
170+ }
171+ }
172+ return $ cmd . ' ' . escapeshellarg ($ tmpFile );
173+ }
174+
175+ private static function _prepare ()
176+ {
177+ if (! is_file (self ::$ jarFile )
178+ || ! is_dir (self ::$ tempDir )
179+ || ! is_writable (self ::$ tempDir )
180+ ) {
181+ throw new Exception ('Minify_YUICompressor : $jarFile and $tempDir must be set. ' );
182+ }
183+ }
184+ }
185+
186+ function get_files ($ root_dir , $ allow_extensions = array ( 'js ' ), $ ignore_files = array ( ), $ ignore_regex = '/^_/ ' , $ ignore_dirs = array (". " ,".. " ,".svn " ), $ all_data =array () )
187+ {
188+ // run through content of root directory
189+ $ dir_content = scandir ($ root_dir );
190+ foreach ($ dir_content as $ key => $ content )
191+ {
192+ $ path = $ root_dir .'/ ' .$ content ;
193+ if (is_file ($ path ) && is_readable ($ path ))
194+ {
195+ // skip ignored files
196+ if (!in_array ($ content , $ ignore_files ))
197+ {
198+ if (preg_match ($ ignore_regex ,$ content ) == 0 )
199+ {
200+ $ content_chunks = explode (". " ,$ content );
201+ $ ext = $ content_chunks [count ($ content_chunks ) - 1 ];
202+ // only include files with desired extensions
203+ if ( ( is_array ( $ allow_extensions ) && in_array ($ ext , $ allow_extensions ) ) || ( is_string ( $ allow_extensions ) && $ allow_extensions == '* ' ) )
204+ {
205+ // save file name with path
206+ $ all_data [] = $ path ;
207+ }
208+ }
209+ }
210+ }
211+ // if content is a directory and readable, add path and name
212+ elseif (is_dir ($ path ) && is_readable ($ path ))
213+ {
214+ // skip any ignored dirs
215+ if (!in_array ($ content , $ ignore_dirs ))
216+ {
217+ // recursive callback to open new directory
218+ $ all_data = get_files ($ path , $ allow_extensions , $ ignore_files , $ ignore_regex , $ ignore_dirs , $ all_data );
219+ }
220+ }
221+ } // end foreach
222+ return $ all_data ;
223+ } // end get_files()
224+
225+ function microtime_float () {
226+ list ($ usec ,$ sec ) = explode (" " ,microtime ());
227+ return ((float )$ usec + (float )$ sec );
228+ }
229+
230+ function copydir ( $ src , $ dst , $ allow_extensions = array ( 'js ' ), $ ignore_files = array ( ), $ ignore_regex = '/^_/ ' , $ ignore_dirs = array (". " ,".. " ,".svn " ) ) {
231+ $ filelist = get_files ($ src , $ allow_extensions , $ ignore_files , $ ignore_regex , $ ignore_dirs );
232+ foreach ($ filelist as $ i =>$ file ) {
233+ $ dir_name = substr ( $ file , 0 , strrpos ( $ file , '/ ' ) );
234+ $ dir_name = substr ( $ dir_name , strlen ($ src )+1 );
235+ if ( !is_dir ($ dst .$ dir_name ) ) {
236+ echo ("- Creating Directory: $ dst$ dir_name \n" );
237+ mkdir ($ dst .$ dir_name );
238+ }
239+
240+ $ dst_file = substr ( $ file , strlen ( $ src )+1 );
241+ echo ("- $ file ( " .filesize ($ file )." bytes) => $ dst$ dst_file " );
242+ copy ( $ file , $ dst .$ dst_file );
243+ usleep (5000 );
244+ if ( filesize ($ file ) == filesize ($ dst .$ dst_file ) )
245+ echo ("\t[OK] \n" );
246+ else
247+ echo ("\t[ERROR] \n" );
248+ }
249+ }
250+ ?>
0 commit comments