001    package railo.runtime.functions.system;
002    
003    import java.io.IOException;
004    
005    import railo.commons.io.CompressUtil;
006    import railo.commons.io.ModeUtil;
007    import railo.commons.io.res.Resource;
008    import railo.commons.io.res.util.ResourceUtil;
009    import railo.runtime.PageContext;
010    import railo.runtime.exp.FunctionException;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.ext.function.Function;
013    import railo.runtime.op.Caster;
014    import railo.runtime.type.util.ListUtil;
015    
016    /**
017     * Implements the CFML Function compress
018     */
019    public final class Compress implements Function {
020        
021        public static boolean call(PageContext pc , String strFormat, String strSource, String srcTarget) throws PageException {
022            return call(pc,strFormat,strSource,srcTarget,true,"777");
023        }
024        public static boolean call(PageContext pc , String strFormat, String strSource, String srcTarget, boolean includeBaseFolder) throws PageException {
025            return call(pc,strFormat,strSource,srcTarget,includeBaseFolder,"777");
026        }
027                
028        public static boolean call(PageContext pc , String strFormat, String strSource, String srcTarget, boolean includeBaseFolder, String strMode) throws PageException {
029            int mode;
030                    try {
031                            mode = ModeUtil.toOctalMode(strMode);
032                    } catch (IOException e) {
033                            throw Caster.toPageException(e);
034                    }
035            strFormat=strFormat.trim().toLowerCase();
036            int format=CompressUtil.FORMAT_ZIP;
037            if(strFormat.equals("bzip")) format=CompressUtil.FORMAT_BZIP;
038            else if(strFormat.equals("bzip2")) format=CompressUtil.FORMAT_BZIP2;
039            else if(strFormat.equals("gzip")) format=CompressUtil.FORMAT_GZIP;
040            else if(strFormat.equals("tar")) format=CompressUtil.FORMAT_TAR;
041            else if(strFormat.equals("tbz")) format=CompressUtil.FORMAT_TBZ;
042            else if(strFormat.startsWith("tar.bz")) format=CompressUtil.FORMAT_TBZ;
043            else if(strFormat.equals("tbz2")) format=CompressUtil.FORMAT_TBZ2;
044            else if(strFormat.startsWith("tar.gz")) format=CompressUtil.FORMAT_TGZ;
045            else if(strFormat.equals("tgz")) format=CompressUtil.FORMAT_TGZ;
046            else if(strFormat.equals("zip")) format=CompressUtil.FORMAT_ZIP;
047            else throw new FunctionException(pc,"compress",1,"format","invalid format definition ["+strFormat+"]," +
048                    " valid formats are [bzip,gzip,tar,tbz (tar bzip),tgz (tar gzip) and zip]");
049            
050            
051            String[] arrSources=ListUtil.toStringArrayEL(ListUtil.listToArrayRemoveEmpty(strSource,","));
052            
053            Resource[] sources=new Resource[arrSources.length];
054            for(int i=0;i<sources.length;i++) {
055                sources[i]=ResourceUtil.toResourceExisting(pc,arrSources[i]);
056                (pc.getConfig()).getSecurityManager().checkFileLocation(sources[i]);
057            }
058    
059            Resource target=ResourceUtil.toResourceExistingParent(pc,srcTarget);
060            (pc.getConfig()).getSecurityManager().checkFileLocation(target);
061            
062            try {
063                if(sources.length==1)CompressUtil.compress(format,sources[0],target,includeBaseFolder,mode);
064                else CompressUtil.compress(format,sources,target,mode);
065            } catch (IOException e) {
066                throw Caster.toPageException(e);
067            }
068            return true;
069        }
070        
071    }