]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/minify_utils.php
Release 6.1.4
[Github/sugarcrm.git] / jssource / minify_utils.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39
40
41     /**get_exclude_files
42      * 
43      * This method returns a predefined array.  
44      * The array holds the location of files/folders to be excluded  
45      * if a prefix is passed in, then it is prepended to the key value in the array
46      * @prefix string to be prepended to key value in array
47      */
48     function get_exclude_files($prefix = ''){
49         //add slash to prefix if it is not empty
50         if(!empty($prefix)){
51             $prefix = $prefix . '/';
52         }
53       //add prefix to key if it was passed in  
54       $compress_exempt_files = array(
55             $prefix.'cache'                         => 'cache',
56             $prefix.'include/javascript/tiny_mce'   => 'include/javascript/tiny_mce',
57             $prefix.'include/javascript/yui'        => 'include/javascript/yui',
58             $prefix.'include/javascript/yui-old'    => 'include/javascript/yui-old',
59             $prefix.'include/javascript/ext-1.1.1'  => 'include/javascript/ext-1.1.1',
60             $prefix.'include/javascript/ext-2.0'    => 'include/javascript/ext-2.0',
61             $prefix.'include/javascript/tiny_mce'   => 'include/javascript/tiny_mce',
62             $prefix.'jscalendar/lang'               => 'jscalendar/lang',
63             $prefix.'modules/Emails'                => 'modules/Emails',
64             $prefix.'jssource'                      => 'jssource',
65             $prefix.'modules/ModuleBuilder'                     => 'modules/ModuleBuilder',
66         );
67         
68         return $compress_exempt_files;
69         
70 }    
71
72
73
74     /**ConcatenateFiles($from_path)
75      * 
76      * This method takes in a string value of the root directory to begin processing  
77      * it uses the predefined array of groupings to create a concatenated file for each grouping
78      * and places the concatenated file in root directory
79      * @from_path root directory where processing should take place
80      */
81     function ConcatenateFiles($from_path){
82
83         $js_groupings = array();
84         if(isset($_REQUEST['root_directory'])){
85             require('jssource/JSGroupings.php');
86         }else{
87             require('JSGroupings.php');
88         }
89         //get array with file sources to concatenate
90         $file_groups = $js_groupings;//from JSGroupings.php;
91         $files_opened = array();
92         $currPerm = '';
93
94         //for each item in array, concatenate the source files
95         foreach($file_groups as $fg){
96
97             //process each group array
98             foreach($fg as $loc=>$trgt){
99                 $relpath = $loc;
100                 $loc = $from_path.'/'.$loc;
101                 $trgt = $from_path.'/'.$trgt;
102                                 
103                 //check to see that source file exists, that it is a file, and is readable
104                 if(file_exists($loc) && is_file($loc)  && is_readable($loc)){
105                     $currPerm = fileperms($loc);
106                     //check to see if target exists, if it does then open file
107                     if(file_exists($trgt)){
108                         if(in_array($trgt, $files_opened)){
109                             //open target file
110                             if(function_exists('sugar_fopen')){
111                                 $trgt_handle = sugar_fopen($trgt, 'a');
112                             }else{
113                                 $trgt_handle = fopen($trgt, 'a');
114                             }
115                         }else{
116                             //open target file
117                             if(function_exists('sugar_fopen')){
118                                 $trgt_handle = sugar_fopen($trgt, 'w');
119                             }else{
120                                 $trgt_handle = fopen($trgt, 'w');
121                             }    
122                         }                    
123                         
124                     }else{
125                         //create and open target file
126                         if(function_exists('sugar_fopen')){
127                                 $trgt_handle = @sugar_fopen($trgt, 'w');
128                         }else{
129                             $trgt_handle = @fopen($trgt, 'w');
130                         }
131                         
132                         // todo: make this failure more friendly.  Ideally, it will display a
133                         //       warning to admin users and revert back to displaying all of the
134                         //       Javascript files insted of displaying the minified versions.
135                         if ($trgt_handle === false) {
136                             $target_directory = dirname($trgt);
137                             $base_name = realpath(dirname(__FILE__) . '/..') . '/';
138                             $target_directory = substr($target_directory, strlen($base_name));
139                             sugar_die("please make sure {$target_directory} is writable\n");
140                         }
141                    
142                 }
143                 $files_opened[] = $trgt;
144                 
145                 //make sure we have handles to both source and target file
146                 if ($trgt_handle) {
147                         $buffer = file_get_contents($loc);
148                         $buffer .= "// End of File $relpath
149                                 
150 ";
151                         $num = fwrite($trgt_handle, $buffer);
152                         
153                         if( $num=== false){
154                          //log error, file did not get appended   
155                          echo "Error while concatenating file $loc to target file $trgt \n";
156                         }
157                     //close file opened.
158                     fclose($trgt_handle);
159                     }
160         
161                 }
162             }
163
164             //set permissions on this file
165             if(!empty($currPerm) && $currPerm !== false){
166                 //if we can retrieve permissions from target files, use same 
167                 //permission on concatenated file
168                 if(function_exists('sugar_chmod')){ 
169                     @sugar_chmod($trgt, $currPerm);
170                 }else{
171                     @chmod($trgt, $currPerm);   
172                 }
173             }else{
174                 //no permissions could be retrieved, so set to 777
175                 if(function_exists('sugar_chmod')){
176                     @sugar_chmod($trgt, 0777);
177                 }else{
178                     @chmod($trgt, 0777);   
179                 }
180             }   
181         }
182     
183     }
184
185     function create_backup_folder($bu_path){
186         $bu_path = str_replace('\\', '/', $bu_path);
187         //get path after root
188         $jpos = strpos($bu_path,'jssource');
189         if($jpos===false){
190             $process_path = $bu_path;
191         }else{
192             $process_path = substr($bu_path, $jpos);
193             $prefix_process_path = substr($bu_path, 0, $jpos-1);         
194         }
195         //get rest of directories into array
196         $bu_dir_arr = explode('/', $process_path);
197         
198         //iterate through each directory and create if needed    
199         
200         foreach($bu_dir_arr as $bu_dir){        
201             if(!file_exists($prefix_process_path.'/'.$bu_dir)){
202                 if(function_exists('sugar_mkdir')){
203                     sugar_mkdir($prefix_process_path.'/'.$bu_dir);
204                 }else{
205                     mkdir($prefix_process_path.'/'.$bu_dir);
206                 }
207             }
208             $prefix_process_path = $prefix_process_path.'/'.$bu_dir;
209         }
210         
211     }
212     
213     
214     
215     
216     /**CompressFiles
217      * This method will call jsmin libraries to minify passed in files
218      * This method takes in 2 string values of the files to process  
219      * Processing will back up javascript files and then minify the original javascript.
220      * Back up javascript files will have an added .src extension 
221      * @from_path file name and path to be processed
222      * @to_path file name and path to be  used to place newly compressed contents
223      */
224     function CompressFiles($from_path,$to_path){
225     if(!defined('JSMIN_AS_LIB')){
226         define('JSMIN_AS_LIB', true);
227     }
228     //assumes jsmin.php is in same directory
229     if(isset($_REQUEST['root_directory']) || defined('INSTANCE_PATH')){
230         require_once('jssource/jsmin.php');
231     }else{
232         require_once('jsmin.php');
233     }    
234     $nl='
235  ';     
236
237         //check to make sure from path and to path are not empty
238         if(isset($from_path) && !empty($from_path)&&isset($to_path) && !empty($to_path)){
239             $lic_str = '';
240             $ReadNextLine = true;
241             // Output a minified version of example.js.
242             if(file_exists($from_path) && is_file($from_path)){
243                 //read in license script
244                 if(function_exists('sugar_fopen')){
245                     $file_handle = sugar_fopen($from_path, 'r');
246                 }else{
247                     $file_handle = fopen($from_path, 'r');
248                 }
249                 if($file_handle){
250                     $beg = false;
251                     
252                     //Read the file until you hit a line with code.  This is meant to retrieve
253                     //the initial license string found in the beginning comments of js code.
254                     while (!feof($file_handle) && $ReadNextLine) {
255                         $newLine = fgets($file_handle, 4096);
256                         $newLine = trim($newLine);
257                         //See if line contains open or closing comments
258                         
259                         //if opening comments are found, set $beg to true 
260                         if(strpos($newLine, '/*')!== false){
261                             $beg = true;
262                         }
263                         
264                         //if closing comments are found, set $beg to false
265                         if(strpos($newLine, '*/')!== false){
266                             $beg = false;
267                         }
268
269                         //if line is not empty (has code) set the boolean to false
270                         if(! empty($newLine)){$ReadNextLine = false;}
271                         //If we are in a comment block, then set boolean back to true 
272                         if($beg){
273                             $ReadNextLine = true;
274                             //add new line to license string
275                             $lic_str .=trim($newLine).$nl;
276                         }else{
277                             //if we are here it means that uncommented and non blank line has been reached
278                             //Check to see that ReadNextLine is true, if so then add the last line collected
279                             //make sure the last line is either the end to a comment block, or starts with '//'
280                             //else do not add as it is live code.
281                             if(!empty($newLine) && ((strpos($newLine, '*/')!== false) || ($newLine{0}.$newLine{1}== '//'))){
282                                 //add new line to license string
283                                 $lic_str .=$newLine;
284                             }                            
285                             //set to false because $beg is false, which means the comment block has ended
286                             $ReadNextLine = false;
287                             
288                         }
289                     }
290                     
291                 }                    
292                 if($file_handle){
293                     fclose($file_handle);   
294                 }            
295                 
296                 //place license string into array for use with jsmin file.
297                 //this will preserve the license in the file
298                 $lic_arr = array($lic_str);
299
300                 //minify javascript
301                 //$jMin = new JSMin($from_path,$to_path,$lic_arr);
302                 $out = $lic_str . JSMin::minify(file_get_contents($from_path));
303                                 
304                 if(function_exists('sugar_fopen') && $fh = @sugar_fopen( $to_path, 'w' ) )
305                             {
306                                 fputs( $fh, $out);
307                                 fclose( $fh );
308                                 } else {
309                                     file_put_contents($to_path, $out);
310                                 }
311
312             }else{
313                  //log failure
314                  echo"<B> COULD NOT COMPRESS $from_path, it is not a file \n";   
315             }
316             
317         }else{
318          //log failure
319          echo"<B> COULD NOT COMPRESS $from_path, missing variables \n";   
320         }
321     }
322
323     function reverseScripts($from_path,$to_path=''){
324             $from_path = str_replace('\\', '/', $from_path);
325             if(empty($to_path)){
326                 $to_path = $from_path;
327             }
328             $to_path = str_replace('\\', '/', $to_path);
329
330             //check to see if provided paths are legit
331
332             if (!file_exists("$from_path"))
333             {
334                 //log error
335                 echo "JS Source directory at $from_path Does Not Exist<p>\n";
336                 return;
337             }
338
339             //get correct path for backup
340             $bu_path = $to_path;
341             $bu_path .= substr($from_path, strlen($to_path.'/jssource/src_files'));
342
343             //if this is a directory, then read it and process files
344             if(is_dir("$from_path")){     
345                 //grab file / directory and read it.
346                 $handle = opendir("$from_path");
347                 //loop over the directory and go into each child directory
348                 while (false !== ($dir = readdir($handle))) {
349
350                   //make sure you go into directory tree and not out of tree
351                   if($dir!= '.' && $dir!= '..'){
352                     //make recursive call to process this directory
353                     reverseScripts($from_path.'/'.$dir, $to_path );
354                   }  
355                 }
356             }
357
358             //if this is not a directory, then 
359             //check if this is a javascript file, then process
360             $path_parts = pathinfo($from_path);
361             if(is_file("$from_path") && isset($path_parts['extension']) && $path_parts['extension'] =='js'){
362
363                     //create backup directory if needed
364                     $bu_dir = dirname($bu_path);
365
366                     if(!file_exists($bu_dir)){
367                         //directory does not exist, log it and return
368                         echo" directory $bu_dir does not exist, could not restore $bu_path";
369                         return;
370                     }
371
372                     //delete backup src file if it exists already
373                     if(file_exists($bu_path)){
374                         unlink($bu_path);
375                     }
376                       copy($from_path, $bu_path);
377                 }                
378         
379         
380     }
381     
382     /**BackUpAndCompressScriptFiles
383      * 
384      * This method takes in a string value of the root directory to begin processing  
385      * it will process and iterate through all files and subdirectories 
386      * under the passed in directory, ignoring directories and files from the predefined exclude array.  
387      * Processing includes calling a method that will minify the javascript children files
388      * @from_path root directory where processing should take place
389      * @to_path root directory where processing should take place, this gets filled in dynamically
390      */
391     function BackUpAndCompressScriptFiles($from_path,$to_path = '', $backup = true){
392
393             //check to see if provided paths are legit
394             if (!file_exists("$from_path"))
395             {
396                 //log error
397                 echo "The from directory, $from_path Does Not Exist<p>\n";
398                 return;
399             }else{
400                 $from_path = str_replace('\\', '/', $from_path);   
401             }        
402             
403             if(empty($to_path)){
404                 $to_path = $from_path;
405             }elseif (!file_exists("$to_path"))
406             {
407                 //log error
408                 echo "The to directory, $to_path Does Not Exist<p>\n";
409                 return;
410             }        
411             
412             //now grab list of files to exclude from minifying            
413             $exclude_files = get_exclude_files($to_path);
414             
415             //process only if file/directory is not in exclude list 
416             if(!isset($exclude_files[$from_path])){
417
418             //get correct path for backup
419             $bu_path = $to_path.'/jssource/src_files';
420             $bu_path .= substr($from_path, strlen($to_path));
421             
422                     //if this is a directory, then read it and process files
423                     if(is_dir("$from_path")){     
424                         //grab file / directory and read it.
425                         $handle = opendir("$from_path");
426                         //loop over the directory and go into each child directory
427                         while (false !== ($dir = readdir($handle))) {
428
429                           //make sure you go into directory tree and not out of tree
430                           if($dir!= '.' && $dir!= '..'){
431                             //make recursive call to process this directory
432                             BackUpAndCompressScriptFiles($from_path.'/'.$dir, $to_path,$backup);
433                           }  
434                         }
435                     }
436     
437         
438                     //if this is not a directory, then 
439                     //check if this is a javascript file, then process
440                     $path_parts = pathinfo($from_path);
441                     if(is_file("$from_path") && isset($path_parts['extension']) && $path_parts['extension'] =='js'){
442
443                         if($backup){
444                             $bu_dir = dirname($bu_path);
445                             if(!file_exists($bu_dir)){
446                                 create_backup_folder($bu_dir);
447                             }
448                     
449                             //delete backup src file if it exists already
450                             if(file_exists($bu_path)){
451                                 unlink($bu_path);
452                             }
453                             //copy original file into a source file
454                               rename($from_path, $bu_path);
455                         }else{
456                             //no need to backup, but remove file that is about to be copied
457                             //if it exists in both backed up scripts and working directory
458                             if(file_exists($from_path) && file_exists($bu_path)){unlink($from_path);}   
459                         }
460                                                     
461                         //now make call to minify and overwrite the original file.
462                         CompressFiles($bu_path, $from_path);
463                                        
464                     }                
465                 }        
466             
467         }