001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.system;
020
021import java.io.IOException;
022
023import lucee.commons.io.CompressUtil;
024import lucee.commons.io.ModeUtil;
025import lucee.commons.io.res.Resource;
026import lucee.commons.io.res.util.ResourceUtil;
027import lucee.runtime.PageContext;
028import lucee.runtime.exp.FunctionException;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.ext.function.Function;
031import lucee.runtime.op.Caster;
032import lucee.runtime.type.util.ListUtil;
033
034/**
035 * Implements the CFML Function compress
036 */
037public final class Compress implements Function {
038    
039    public static boolean call(PageContext pc , String strFormat, String strSource, String srcTarget) throws PageException {
040        return call(pc,strFormat,strSource,srcTarget,true,"777");
041    }
042    public static boolean call(PageContext pc , String strFormat, String strSource, String srcTarget, boolean includeBaseFolder) throws PageException {
043        return call(pc,strFormat,strSource,srcTarget,includeBaseFolder,"777");
044    }
045            
046    public static boolean call(PageContext pc , String strFormat, String strSource, String srcTarget, boolean includeBaseFolder, String strMode) throws PageException {
047        int mode;
048                try {
049                        mode = ModeUtil.toOctalMode(strMode);
050                } catch (IOException e) {
051                        throw Caster.toPageException(e);
052                }
053        strFormat=strFormat.trim().toLowerCase();
054        int format=CompressUtil.FORMAT_ZIP;
055        if(strFormat.equals("bzip")) format=CompressUtil.FORMAT_BZIP;
056        else if(strFormat.equals("bzip2")) format=CompressUtil.FORMAT_BZIP2;
057        else if(strFormat.equals("gzip")) format=CompressUtil.FORMAT_GZIP;
058        else if(strFormat.equals("tar")) format=CompressUtil.FORMAT_TAR;
059        else if(strFormat.equals("tbz")) format=CompressUtil.FORMAT_TBZ;
060        else if(strFormat.startsWith("tar.bz")) format=CompressUtil.FORMAT_TBZ;
061        else if(strFormat.equals("tbz2")) format=CompressUtil.FORMAT_TBZ2;
062        else if(strFormat.startsWith("tar.gz")) format=CompressUtil.FORMAT_TGZ;
063        else if(strFormat.equals("tgz")) format=CompressUtil.FORMAT_TGZ;
064        else if(strFormat.equals("zip")) format=CompressUtil.FORMAT_ZIP;
065        else throw new FunctionException(pc,"compress",1,"format","invalid format definition ["+strFormat+"]," +
066                " valid formats are [bzip,gzip,tar,tbz (tar bzip),tgz (tar gzip) and zip]");
067        
068        
069        String[] arrSources=ListUtil.toStringArrayEL(ListUtil.listToArrayRemoveEmpty(strSource,","));
070        
071        Resource[] sources=new Resource[arrSources.length];
072        for(int i=0;i<sources.length;i++) {
073            sources[i]=ResourceUtil.toResourceExisting(pc,arrSources[i]);
074            (pc.getConfig()).getSecurityManager().checkFileLocation(sources[i]);
075        }
076
077        Resource target=ResourceUtil.toResourceExistingParent(pc,srcTarget);
078        (pc.getConfig()).getSecurityManager().checkFileLocation(target);
079        
080        try {
081            if(sources.length==1)CompressUtil.compress(format,sources[0],target,includeBaseFolder,mode);
082            else CompressUtil.compress(format,sources,target,mode);
083        } catch (IOException e) {
084            throw Caster.toPageException(e);
085        }
086        return true;
087    }
088    
089}