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-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
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
48 function get_exclude_files($prefix = ''){
49 //add slash to prefix if it is not empty
51 $prefix = $prefix . '/';
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.'include/javascript/yui-old' => true,
59 $prefix.'include/javascript/ext-1.1.1' => true,
60 $prefix.'include/javascript/ext-2.0' => true,
61 $prefix.'include/javascript/tiny_mce' => true,
62 $prefix.'modules/Emails' => true,
63 $prefix.'jssource' => true,
64 $prefix.'modules/ModuleBuilder' => true,
67 return $compress_exempt_files;
73 /**ConcatenateFiles($from_path)
75 * This method takes in a string value of the root directory to begin processing
76 * it uses the predefined array of groupings to create a concatenated file for each grouping
77 * and places the concatenated file in root directory
78 * @from_path root directory where processing should take place
80 function ConcatenateFiles($from_path){
82 $js_groupings = array();
83 if(isset($_REQUEST['root_directory'])){
84 require('jssource/JSGroupings.php');
86 require('JSGroupings.php');
88 //get array with file sources to concatenate
89 $file_groups = $js_groupings;//from JSGroupings.php;
90 $files_opened = array();
93 //for each item in array, concatenate the source files
94 foreach($file_groups as $fg){
96 //process each group array
97 foreach($fg as $loc=>$trgt){
99 $loc = $from_path.'/'.$loc;
100 $trgt = sugar_cached($trgt);
102 //check to see that source file exists, that it is a file, and is readable
103 if(file_exists($loc) && is_file($loc) && is_readable($loc)){
104 $currPerm = fileperms($loc);
105 //check to see if target exists, if it does then open file
106 if(file_exists($trgt)){
107 if(in_array($trgt, $files_opened)){
109 if(function_exists('sugar_fopen')){
110 $trgt_handle = sugar_fopen($trgt, 'a');
112 $trgt_handle = fopen($trgt, 'a');
116 if(function_exists('sugar_fopen')){
117 $trgt_handle = sugar_fopen($trgt, 'w');
119 $trgt_handle = fopen($trgt, 'w');
124 mkdir_recursive(dirname($trgt));
125 //create and open target file
126 if(function_exists('sugar_fopen')){
127 $trgt_handle = @sugar_fopen($trgt, 'w');
129 $trgt_handle = @fopen($trgt, 'w');
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 = dirname($target_directory);
138 while(!is_dir($base) && !empty($base) && $base != dirname($base)) {
139 $base = dirname($base);
141 sugar_die("Creating $target_directory failed: please make sure {$base} is writable\n");
145 $files_opened[] = $trgt;
147 //make sure we have handles to both source and target file
149 $buffer = file_get_contents($loc);
150 $buffer .= "// End of File $relpath
153 $num = fwrite($trgt_handle, $buffer);
156 //log error, file did not get appended
157 echo "Error while concatenating file $loc to target file $trgt \n";
160 fclose($trgt_handle);
166 //set permissions on this file
167 if(!empty($currPerm) && $currPerm !== false){
168 //if we can retrieve permissions from target files, use same
169 //permission on concatenated file
170 if(function_exists('sugar_chmod')){
171 @sugar_chmod($trgt, $currPerm);
173 @chmod($trgt, $currPerm);
176 //no permissions could be retrieved, so set to 777
177 if(function_exists('sugar_chmod')){
178 @sugar_chmod($trgt, 0777);
187 function create_backup_folder($bu_path){
188 $bu_path = str_replace('\\', '/', $bu_path);
189 //get path after root
190 $jpos = strpos($bu_path,'jssource');
192 $process_path = $bu_path;
194 $process_path = substr($bu_path, $jpos);
195 $prefix_process_path = substr($bu_path, 0, $jpos-1);
197 //get rest of directories into array
198 $bu_dir_arr = explode('/', $process_path);
200 //iterate through each directory and create if needed
202 foreach($bu_dir_arr as $bu_dir){
203 if(!file_exists($prefix_process_path.'/'.$bu_dir)){
204 if(function_exists('sugar_mkdir')){
205 sugar_mkdir($prefix_process_path.'/'.$bu_dir);
207 mkdir($prefix_process_path.'/'.$bu_dir);
210 $prefix_process_path = $prefix_process_path.'/'.$bu_dir;
219 * This method will call jsmin libraries to minify passed in files
220 * This method takes in 2 string values of the files to process
221 * Processing will back up javascript files and then minify the original javascript.
222 * Back up javascript files will have an added .src extension
223 * @from_path file name and path to be processed
224 * @to_path file name and path to be used to place newly compressed contents
226 function CompressFiles($from_path,$to_path){
227 if(!defined('JSMIN_AS_LIB')){
228 define('JSMIN_AS_LIB', true);
230 //assumes jsmin.php is in same directory
231 if(isset($_REQUEST['root_directory']) || defined('INSTANCE_PATH')){
232 require_once('jssource/jsmin.php');
234 require_once('jsmin.php');
239 //check to make sure from path and to path are not empty
240 if(isset($from_path) && !empty($from_path)&&isset($to_path) && !empty($to_path)){
242 $ReadNextLine = true;
243 // Output a minified version of example.js.
244 if(file_exists($from_path) && is_file($from_path)){
245 //read in license script
246 if(function_exists('sugar_fopen')){
247 $file_handle = sugar_fopen($from_path, 'r');
249 $file_handle = fopen($from_path, 'r');
254 //Read the file until you hit a line with code. This is meant to retrieve
255 //the initial license string found in the beginning comments of js code.
256 while (!feof($file_handle) && $ReadNextLine) {
257 $newLine = fgets($file_handle, 4096);
258 $newLine = trim($newLine);
259 //See if line contains open or closing comments
261 //if opening comments are found, set $beg to true
262 if(strpos($newLine, '/*')!== false){
266 //if closing comments are found, set $beg to false
267 if(strpos($newLine, '*/')!== false){
271 //if line is not empty (has code) set the boolean to false
272 if(! empty($newLine)){$ReadNextLine = false;}
273 //If we are in a comment block, then set boolean back to true
275 $ReadNextLine = true;
276 //add new line to license string
277 $lic_str .=trim($newLine).$nl;
279 //if we are here it means that uncommented and non blank line has been reached
280 //Check to see that ReadNextLine is true, if so then add the last line collected
281 //make sure the last line is either the end to a comment block, or starts with '//'
282 //else do not add as it is live code.
283 if(!empty($newLine) && ((strpos($newLine, '*/')!== false) || ($newLine{0}.$newLine{1}== '//'))){
284 //add new line to license string
287 //set to false because $beg is false, which means the comment block has ended
288 $ReadNextLine = false;
295 fclose($file_handle);
298 //place license string into array for use with jsmin file.
299 //this will preserve the license in the file
300 $lic_arr = array($lic_str);
303 //$jMin = new JSMin($from_path,$to_path,$lic_arr);
304 $out = $lic_str . JSMin::minify(file_get_contents($from_path));
306 if(function_exists('sugar_fopen') && $fh = @sugar_fopen( $to_path, 'w' ) )
311 file_put_contents($to_path, $out);
316 echo"<B> COULD NOT COMPRESS $from_path, it is not a file \n";
321 echo"<B> COULD NOT COMPRESS $from_path, missing variables \n";
325 function reverseScripts($from_path,$to_path=''){
326 $from_path = str_replace('\\', '/', $from_path);
328 $to_path = $from_path;
330 $to_path = str_replace('\\', '/', $to_path);
332 //check to see if provided paths are legit
334 if (!file_exists($from_path))
337 echo "JS Source directory at $from_path Does Not Exist<p>\n";
341 //get correct path for backup
343 $bu_path .= substr($from_path, strlen($to_path.'/jssource/src_files'));
345 //if this is a directory, then read it and process files
346 if(is_dir($from_path)){
347 //grab file / directory and read it.
348 $handle = opendir($from_path);
349 //loop over the directory and go into each child directory
350 while (false !== ($dir = readdir($handle))) {
352 //make sure you go into directory tree and not out of tree
353 if($dir!= '.' && $dir!= '..'){
354 //make recursive call to process this directory
355 reverseScripts($from_path.'/'.$dir, $to_path );
360 //if this is not a directory, then
361 //check if this is a javascript file, then process
362 $path_parts = pathinfo($from_path);
363 if(is_file("$from_path") && isset($path_parts['extension']) && $path_parts['extension'] =='js'){
365 //create backup directory if needed
366 $bu_dir = dirname($bu_path);
368 if(!file_exists($bu_dir)){
369 //directory does not exist, log it and return
370 echo" directory $bu_dir does not exist, could not restore $bu_path";
374 //delete backup src file if it exists already
375 if(file_exists($bu_path)){
378 copy($from_path, $bu_path);
384 /**BackUpAndCompressScriptFiles
386 * This method takes in a string value of the root directory to begin processing
387 * it will process and iterate through all files and subdirectories
388 * under the passed in directory, ignoring directories and files from the predefined exclude array.
389 * Processing includes calling a method that will minify the javascript children files
390 * @from_path root directory where processing should take place
391 * @to_path root directory where processing should take place, this gets filled in dynamically
393 function BackUpAndCompressScriptFiles($from_path,$to_path = '', $backup = true){
394 //check to see if provided paths are legit
395 if (!file_exists($from_path))
398 echo "The from directory, $from_path Does Not Exist<p>\n";
401 $from_path = str_replace('\\', '/', $from_path);
405 $to_path = $from_path;
406 }elseif (!file_exists($to_path))
409 echo "The to directory, $to_path Does Not Exist<p>\n";
413 //now grab list of files to exclude from minifying
414 $exclude_files = get_exclude_files($to_path);
416 //process only if file/directory is not in exclude list
417 if(!isset($exclude_files[$from_path])){
419 //get correct path for backup
420 $bu_path = $to_path.'/jssource/src_files';
421 $bu_path .= substr($from_path, strlen($to_path));
423 //if this is a directory, then read it and process files
424 if(is_dir("$from_path")){
425 //grab file / directory and read it.
426 $handle = opendir("$from_path");
427 //loop over the directory and go into each child directory
428 while (false !== ($dir = readdir($handle))) {
430 //make sure you go into directory tree and not out of tree
431 if($dir!= '.' && $dir!= '..'){
432 //make recursive call to process this directory
433 BackUpAndCompressScriptFiles($from_path.'/'.$dir, $to_path,$backup);
439 //if this is not a directory, then
440 //check if this is a javascript file, then process
441 $path_parts = pathinfo($from_path);
442 if(is_file("$from_path") && isset($path_parts['extension']) && $path_parts['extension'] =='js'){
445 $bu_dir = dirname($bu_path);
446 if(!file_exists($bu_dir)){
447 create_backup_folder($bu_dir);
450 //delete backup src file if it exists already
451 if(file_exists($bu_path)){
454 //copy original file into a source file
455 rename($from_path, $bu_path);
457 //no need to backup, but remove file that is about to be copied
458 //if it exists in both backed up scripts and working directory
459 if(file_exists($from_path) && file_exists($bu_path)){unlink($from_path);}
462 //now make call to minify and overwrite the original file.
463 CompressFiles($bu_path, $from_path);