]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/minify_utils.php
Release 6.5.0
[Github/sugarcrm.git] / jssource / minify_utils.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 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.sugar_cached('')                => true,
56             $prefix.'include/javascript/tiny_mce'   => true,
57             $prefix.'include/javascript/yui'        => true,
58             $prefix.'modules/Emails'                => true,
59             $prefix.'jssource'                      => true,
60             $prefix.'modules/ModuleBuilder'         => true,
61             $prefix.'include/javascript/jquery'     => true,
62             $prefix.'include/javascript/jquery/bootstrap'     => true,
63             $prefix.'tests/PHPUnit/PHP/CodeCoverage/Report/HTML/Template' => true,
64             $prefix.'tests/jssource/minify/expect'  => true,
65             $prefix.'tests/jssource/minify/test'    => true,
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         // Minifying the group files takes a long time sometimes.
84         @ini_set('max_execution_time', 300);
85         $js_groupings = array();
86         if(isset($_REQUEST['root_directory'])){
87             require('jssource/JSGroupings.php');
88         }else{
89             require('JSGroupings.php');
90         }
91         //get array with file sources to concatenate
92         $file_groups = $js_groupings;//from JSGroupings.php;
93         $files_opened = array();
94         $currPerm = '';
95
96         $excludedFiles = get_exclude_files($from_path);
97         //for each item in array, concatenate the source files
98         foreach($file_groups as $fg){
99
100             //process each group array
101             foreach($fg as $loc=>$trgt){
102                 $already_minified = FALSE;
103                 $minified_loc = str_replace('.js', '-min.js', $loc);
104                 if(is_file($minified_loc)) {
105                     $loc = $minified_loc;
106                     $already_minified = TRUE;
107                 }
108                 $relpath = $loc;
109                 $loc = $from_path.'/'.$loc;
110
111                 $trgt = sugar_cached($trgt);
112                 //check to see that source file is a file, and is readable.
113                 if(is_file($loc) && is_readable($loc)){
114                     $currPerm = fileperms($loc);
115                     //check to see if target exists, if it does then open file
116                     if(file_exists($trgt)){
117                         if(in_array($trgt, $files_opened)){
118                             //open target file
119                             if(function_exists('sugar_fopen')){
120                                 $trgt_handle = sugar_fopen($trgt, 'a');
121                             }else{
122                                 $trgt_handle = fopen($trgt, 'a');
123                             }
124                         }else{
125                             //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
133                     }else{
134
135                         if(!function_exists('mkdir_recursive')) {
136                             require_once('include/dir_inc.php');
137                         }
138
139                         mkdir_recursive(dirname($trgt));
140                         //create and open target file
141                         if(function_exists('sugar_fopen')){
142                                 $trgt_handle = @sugar_fopen($trgt, 'w');
143                         }else{
144                             $trgt_handle = @fopen($trgt, 'w');
145                         }
146
147                         // todo: make this failure more friendly.  Ideally, it will display a
148                         //       warning to admin users and revert back to displaying all of the
149                         //       Javascript files insted of displaying the minified versions.
150                         if ($trgt_handle === false) {
151                             $target_directory = dirname($trgt);
152                             $base = dirname($target_directory);
153                             while(!is_dir($base) && !empty($base) && $base != dirname($base)) {
154                                 $base = dirname($base);
155                             }
156                             sugar_die("Creating $target_directory failed: please make sure {$base} is writable\n");
157                         }
158
159                 }
160                 $files_opened[] = $trgt;
161
162                 //make sure we have handles to both source and target file
163                 if ($trgt_handle) {
164                         if($already_minified || isset($excludedFiles[dirname($loc)])) {
165                             $buffer = file_get_contents($loc);
166                         } else {
167                             $buffer = SugarMin::minify(file_get_contents($loc));
168                         }
169
170                         $buffer .= "/* End of File $relpath */\n\n";
171                         $num = fwrite($trgt_handle, $buffer);
172
173                         if( $num=== false){
174                          //log error, file did not get appended
175                          echo "Error while concatenating file $loc to target file $trgt \n";
176                         }
177                     //close file opened.
178                     fclose($trgt_handle);
179                     }
180
181                 }
182             }
183
184             //set permissions on this file
185             if(!empty($currPerm) && $currPerm !== false){
186                 //if we can retrieve permissions from target files, use same
187                 //permission on concatenated file
188                 if(function_exists('sugar_chmod')){
189                     @sugar_chmod($trgt, $currPerm);
190                 }else{
191                     @chmod($trgt, $currPerm);
192                 }
193             }else{
194                 //no permissions could be retrieved, so set to 777
195                 if(function_exists('sugar_chmod')){
196                     @sugar_chmod($trgt, 0777);
197                 }else{
198                     @chmod($trgt, 0777);
199                 }
200             }
201         }
202
203     }
204
205     function create_backup_folder($bu_path){
206         $bu_path = str_replace('\\', '/', $bu_path);
207         //get path after root
208         $jpos = strpos($bu_path,'jssource');
209         if($jpos===false){
210             $process_path = $bu_path;
211         }else{
212             $process_path = substr($bu_path, $jpos);
213             $prefix_process_path = substr($bu_path, 0, $jpos-1);
214         }
215         //get rest of directories into array
216         $bu_dir_arr = explode('/', $process_path);
217
218         //iterate through each directory and create if needed
219
220         foreach($bu_dir_arr as $bu_dir){
221             if(!file_exists($prefix_process_path.'/'.$bu_dir)){
222                 if(function_exists('sugar_mkdir')){
223                     sugar_mkdir($prefix_process_path.'/'.$bu_dir);
224                 }else{
225                     mkdir($prefix_process_path.'/'.$bu_dir);
226                 }
227             }
228             $prefix_process_path = $prefix_process_path.'/'.$bu_dir;
229         }
230
231     }
232
233
234
235
236     /**CompressFiles
237      * This method will call jsmin libraries to minify passed in files
238      * This method takes in 2 string values of the files to process
239      * Processing will back up javascript files and then minify the original javascript.
240      * Back up javascript files will have an added .src extension
241      * @from_path file name and path to be processed
242      * @to_path file name and path to be  used to place newly compressed contents
243      */
244     function CompressFiles($from_path,$to_path){
245     if(!defined('JSMIN_AS_LIB')){
246         define('JSMIN_AS_LIB', true);
247     }
248     //assumes jsmin.php is in same directory
249     if(isset($_REQUEST['root_directory']) || defined('INSTANCE_PATH')){
250         require_once('jssource/jsmin.php');
251     }else{
252         require_once('jsmin.php');
253     }
254     $nl='
255  ';
256
257         //check to make sure from path and to path are not empty
258         if(isset($from_path) && !empty($from_path)&&isset($to_path) && !empty($to_path)){
259             $lic_str = '';
260             $ReadNextLine = true;
261             // Output a minified version of example.js.
262             if(file_exists($from_path) && is_file($from_path)){
263                 //read in license script
264                 if(function_exists('sugar_fopen')){
265                     $file_handle = sugar_fopen($from_path, 'r');
266                 }else{
267                     $file_handle = fopen($from_path, 'r');
268                 }
269                 if($file_handle){
270                     $beg = false;
271
272                     //Read the file until you hit a line with code.  This is meant to retrieve
273                     //the initial license string found in the beginning comments of js code.
274                     while (!feof($file_handle) && $ReadNextLine) {
275                         $newLine = fgets($file_handle, 4096);
276                         $newLine = trim($newLine);
277                         //See if line contains open or closing comments
278
279                         //if opening comments are found, set $beg to true
280                         if(strpos($newLine, '/*')!== false){
281                             $beg = true;
282                         }
283
284                         //if closing comments are found, set $beg to false
285                         if(strpos($newLine, '*/')!== false){
286                             $beg = false;
287                         }
288
289                         //if line is not empty (has code) set the boolean to false
290                         if(! empty($newLine)){$ReadNextLine = false;}
291                         //If we are in a comment block, then set boolean back to true
292                         if($beg){
293                             $ReadNextLine = true;
294                             //add new line to license string
295                             $lic_str .=trim($newLine).$nl;
296                         }else{
297                             //if we are here it means that uncommented and non blank line has been reached
298                             //Check to see that ReadNextLine is true, if so then add the last line collected
299                             //make sure the last line is either the end to a comment block, or starts with '//'
300                             //else do not add as it is live code.
301                             if(!empty($newLine) && ((strpos($newLine, '*/')!== false) || ($newLine{0}.$newLine{1}== '//'))){
302                                 //add new line to license string
303                                 $lic_str .=$newLine;
304                             }
305                             //set to false because $beg is false, which means the comment block has ended
306                             $ReadNextLine = false;
307
308                         }
309                     }
310
311                 }
312                 if($file_handle){
313                     fclose($file_handle);
314                 }
315
316                 //place license string into array for use with jsmin file.
317                 //this will preserve the license in the file
318                 $lic_arr = array($lic_str);
319
320                 //minify javascript
321                 //$jMin = new JSMin($from_path,$to_path,$lic_arr);
322                 $min_file = str_replace('.js', '-min.js', $from_path);
323                 if(strpos($from_path, '-min.js') !== FALSE) {
324                     $min_file = $from_path;
325                 }
326
327                 if(is_file($min_file)) {
328                     $out = file_get_contents($min_file);
329                 } else {
330                     $out = $lic_str . SugarMin::minify(file_get_contents($from_path));
331                 }
332
333                 if(function_exists('sugar_fopen') && $fh = @sugar_fopen( $to_path, 'w' ) )
334                             {
335                                 fputs( $fh, $out);
336                                 fclose( $fh );
337                                 } else {
338                                     file_put_contents($to_path, $out);
339                                 }
340
341             }else{
342                  //log failure
343                  echo"<B> COULD NOT COMPRESS $from_path, it is not a file \n";
344             }
345
346         }else{
347          //log failure
348          echo"<B> COULD NOT COMPRESS $from_path, missing variables \n";
349         }
350     }
351
352     function reverseScripts($from_path,$to_path=''){
353             $from_path = str_replace('\\', '/', $from_path);
354             if(empty($to_path)){
355                 $to_path = $from_path;
356             }
357             $to_path = str_replace('\\', '/', $to_path);
358
359             //check to see if provided paths are legit
360
361             if (!file_exists($from_path))
362             {
363                 //log error
364                 echo "JS Source directory at $from_path Does Not Exist<p>\n";
365                 return;
366             }
367
368             //get correct path for backup
369             $bu_path = $to_path;
370             $bu_path .= substr($from_path, strlen($to_path.'/jssource/src_files'));
371
372             //if this is a directory, then read it and process files
373             if(is_dir($from_path)){
374                 //grab file / directory and read it.
375                 $handle = opendir($from_path);
376                 //loop over the directory and go into each child directory
377                 while (false !== ($dir = readdir($handle))) {
378
379                   //make sure you go into directory tree and not out of tree
380                   if($dir!= '.' && $dir!= '..'){
381                     //make recursive call to process this directory
382                     reverseScripts($from_path.'/'.$dir, $to_path );
383                   }
384                 }
385             }
386
387             //if this is not a directory, then
388             //check if this is a javascript file, then process
389             $path_parts = pathinfo($from_path);
390             if(is_file("$from_path") && isset($path_parts['extension']) && $path_parts['extension'] =='js'){
391
392                     //create backup directory if needed
393                     $bu_dir = dirname($bu_path);
394
395                     if(!file_exists($bu_dir)){
396                         //directory does not exist, log it and return
397                         echo" directory $bu_dir does not exist, could not restore $bu_path";
398                         return;
399                     }
400
401                     //delete backup src file if it exists already
402                     if(file_exists($bu_path)){
403                         unlink($bu_path);
404                     }
405                       copy($from_path, $bu_path);
406                 }
407
408
409     }
410
411     /**BackUpAndCompressScriptFiles
412      *
413      * This method takes in a string value of the root directory to begin processing
414      * it will process and iterate through all files and subdirectories
415      * under the passed in directory, ignoring directories and files from the predefined exclude array.
416      * Processing includes calling a method that will minify the javascript children files
417      * @from_path root directory where processing should take place
418      * @to_path root directory where processing should take place, this gets filled in dynamically
419      */
420     function BackUpAndCompressScriptFiles($from_path,$to_path = '', $backup = true){
421             //check to see if provided paths are legit
422             if (!file_exists($from_path))
423             {
424                 //log error
425                 echo "The from directory, $from_path Does Not Exist<p>\n";
426                 return;
427             }else{
428                 $from_path = str_replace('\\', '/', $from_path);
429             }
430
431             if(empty($to_path)){
432                 $to_path = $from_path;
433             }elseif (!file_exists($to_path))
434             {
435                 //log error
436                 echo "The to directory, $to_path Does Not Exist<p>\n";
437                 return;
438             }
439
440             //now grab list of files to exclude from minifying
441             $exclude_files = get_exclude_files($to_path);
442
443             //process only if file/directory is not in exclude list
444             if(!isset($exclude_files[$from_path])){
445
446                 //get correct path for backup
447                 $bu_path = $to_path.'/jssource/src_files';
448                 $bu_path .= substr($from_path, strlen($to_path));
449
450                 //if this is a directory, then read it and process files
451                 if(is_dir("$from_path")){
452                     //grab file / directory and read it.
453                     $handle = opendir("$from_path");
454                     //loop over the directory and go into each child directory
455                     while (false !== ($dir = readdir($handle))) {
456
457                       //make sure you go into directory tree and not out of tree
458                       if($dir!= '.' && $dir!= '..'){
459                         //make recursive call to process this directory
460                         BackUpAndCompressScriptFiles($from_path.'/'.$dir, $to_path,$backup);
461                       }
462                     }
463                 }
464
465
466                 //if this is not a directory, then
467                 //check if this is a javascript file, then process
468                 // Also, check if there's a min counterpart, in which case, don't use this file.
469                 $path_parts = pathinfo($from_path);
470                 if(is_file("$from_path") && isset($path_parts['extension']) && $path_parts['extension'] =='js'){
471                     /*$min_file_path = $path_parts['dirname'].'/'.$path_parts['filename'].'-min.'.$path_parts['extension'];
472                     if(is_file($min_file_path)) {
473                         $from_path = $min_file_path;
474                     }*/
475                     if($backup){
476                         $bu_dir = dirname($bu_path);
477                         if(!file_exists($bu_dir)){
478                             create_backup_folder($bu_dir);
479                         }
480
481                         //delete backup src file if it exists already
482                         if(file_exists($bu_path)){
483                             unlink($bu_path);
484                         }
485                         //copy original file into a source file
486                           rename($from_path, $bu_path);
487                     }else{
488                         //no need to backup, but remove file that is about to be copied
489                         //if it exists in both backed up scripts and working directory
490                         if(file_exists($from_path) && file_exists($bu_path)){unlink($from_path);}
491                     }
492
493                     //now make call to minify and overwrite the original file.
494                     CompressFiles($bu_path, $from_path);
495
496                 }
497             }
498
499         }