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.res.Resource;
025import lucee.commons.io.res.util.ResourceUtil;
026import lucee.runtime.PageContext;
027import lucee.runtime.exp.FunctionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.util.ListUtil;
032
033/**
034 * Implements the CFML Function compress
035 */
036public final class Extract implements Function {
037        
038    public static boolean call(PageContext pc , String strFormat, String strSource, String srcTarget) throws PageException {
039        
040        strFormat=strFormat.trim().toLowerCase();
041        int format=CompressUtil.FORMAT_ZIP;
042        if(strFormat.equals("bzip")) format=CompressUtil.FORMAT_BZIP;
043        else if(strFormat.equals("bzip2")) format=CompressUtil.FORMAT_BZIP2;
044        else if(strFormat.equals("gzip")) format=CompressUtil.FORMAT_GZIP;
045        else if(strFormat.equals("tar")) format=CompressUtil.FORMAT_TAR;
046        else if(strFormat.equals("tbz")) format=CompressUtil.FORMAT_TBZ;
047        else if(strFormat.startsWith("tar.bz")) format=CompressUtil.FORMAT_TBZ;
048        else if(strFormat.equals("tbz2")) format=CompressUtil.FORMAT_TBZ2;
049        else if(strFormat.startsWith("tar.gz")) format=CompressUtil.FORMAT_TGZ;
050        else if(strFormat.equals("tgz")) format=CompressUtil.FORMAT_TGZ;
051        else if(strFormat.equals("zip")) format=CompressUtil.FORMAT_ZIP;
052        else throw new FunctionException(pc,"compress",1,"format","invalid format definition ["+strFormat+"]," +
053                " valid formats are [bzip,gzip,tar,tbz (tar bzip),tgz (tar gzip) and zip]");
054        
055        
056        String[] arrSources=ListUtil.toStringArrayEL(ListUtil.listToArrayRemoveEmpty(strSource,","));
057        
058        Resource[] sources=new Resource[arrSources.length];
059        for(int i=0;i<sources.length;i++) {
060            sources[i]=ResourceUtil.toResourceExisting(pc, arrSources[i]);
061                //FileUtil.toFileExisting(pc,arrSources[i]);
062            pc.getConfig().getSecurityManager().checkFileLocation(sources[i]);
063        }
064
065        
066        Resource target=ResourceUtil.toResourceExisting(pc,srcTarget);
067        pc.getConfig().getSecurityManager().checkFileLocation(target);
068        
069        try {
070            CompressUtil.extract(format,sources,target);
071        } catch (IOException e) {
072            throw Caster.toPageException(e);
073        }
074        return true;
075    }
076    
077}