package com.jspsmart.upload;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
public class SmartUpload
{
protected byte[] m_binArray;
protected HttpServletRequest m_request;
protected HttpServletResponse m_response;
protected ServletContext m_application;
private int m_totalBytes = 0;
private int m_currentIndex = 0;
private int m_startData = 0;
private int m_endData = 0;
private String m_boundary = new String();
private long m_totalMaxFileSize = 0L;
private long m_maxFileSize = 0L;
private Vector m_deniedFilesList = new Vector();
private Vector m_allowedFilesList = new Vector();
private boolean m_denyPhysicalPath = false;
private boolean m_forcePhysicalPath = false;
private String m_contentDisposition = new String();
public static final int SAVE_AUTO = 0;
public static final int SAVE_VIRTUAL = 1;
public static final int SAVE_PHYSICAL = 2;
private Files m_files = new Files();
private Request m_formRequest = new Request();
public final void init(ServletConfig servletconfig)
throws ServletException
{
this.m_application = servletconfig.getServletContext();
}
public void service(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
this.m_request = httpservletrequest;
this.m_response = httpservletresponse;
}
public final void initialize(ServletConfig servletconfig, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException
{
this.m_application = servletconfig.getServletContext();
this.m_request = httpservletrequest;
this.m_response = httpservletresponse;
}
public final void initialize(PageContext pagecontext)
throws ServletException
{
this.m_application = pagecontext.getServletContext();
this.m_request = ((HttpServletRequest)pagecontext.getRequest());
this.m_response = ((HttpServletResponse)pagecontext.getResponse());
}
public final void initialize(ServletContext servletcontext, HttpSession httpsession, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, JspWriter jspwriter)
throws ServletException
{
this.m_application = servletcontext;
this.m_request = httpservletrequest;
this.m_response = httpservletresponse;
}
public void upload()
throws ServletException, IOException, SmartUploadException
{
int i = 0;
boolean flag = false;
long l = 0L;
boolean flag1 = false;
String s = new String();
String s2 = new String();
String s4 = new String();
String s5 = new String();
String s6 = new String();
String s7 = new String();
String s8 = new String();
String s9 = new String();
String s10 = new String();
boolean flag2 = false;
this.m_totalBytes = this.m_request.getContentLength();
this.m_binArray = new byte[this.m_totalBytes];
int j;
for (; i < this.m_totalBytes; i += j) {
try
{
this.m_request.getInputStream();
j = this.m_request.getInputStream().read(this.m_binArray, i, this.m_totalBytes - i);
}
catch (Exception exception)
{
throw new SmartUploadException("Unable to upload.");
}
}
for (; (!flag1) && (this.m_currentIndex < this.m_totalBytes); this.m_currentIndex += 1) {
if (this.m_binArray[this.m_currentIndex] == 13)
flag1 = true;
else
this.m_boundary += (char)this.m_binArray[this.m_currentIndex];
}
if (this.m_currentIndex == 1)
return;
for (this.m_currentIndex += 1; this.m_currentIndex < this.m_totalBytes; this.m_currentIndex += 2)
{
String s1 = getDataHeader();
this.m_currentIndex += 2;
boolean flag3 = s1.indexOf("filename") > 0;
String s3 = getDataFieldValue(s1, "name");
if (flag3)
{
s6 = getDataFieldValue(s1, "filename");
s4 = getFileName(s6);
s5 = getFileExt(s4);
s7 = getContentType(s1);
s8 = getContentDisp(s1);
s9 = getTypeMIME(s7);
s10 = getSubTypeMIME(s7);
}
getDataSection();
if ((flag3) && (s4.length() > 0))
{
if (this.m_deniedFilesList.contains(s5))
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
if ((!this.m_allowedFilesList.isEmpty()) && (!this.m_allowedFilesList.contains(s5)))
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
if ((this.m_maxFileSize > 0L) && (this.m_endData - this.m_startData + 1 > this.m_maxFileSize))
throw new SecurityException("Size exceeded for this file : " + s4 + " (1105).");
l += this.m_endData - this.m_startData + 1;
if ((this.m_totalMaxFileSize > 0L) && (l > this.m_totalMaxFileSize))
throw new SecurityException("Total File Size exceeded (1110).");
}
if (flag3)
{
File file = new File();
file.setParent(this);
file.setFieldName(s3);
file.setFileName(s4);
file.setFileExt(s5);
file.setFilePathName(s6);
file.setIsMissing(s6.length() == 0);
file.setContentType(s7);
file.setContentDisp(s8);
file.setTypeMIME(s9);
file.setSubTypeMIME(s10);
if (s7.indexOf("application/x-macbinary") > 0)
this.m_startData += 128;
file.setSize(this.m_endData - this.m_startData + 1);
file.setStartData(this.m_startData);
file.setEndData(this.m_endData);
this.m_files.addFile(file);
}
else {
String s11 = new String(this.m_binArray, this.m_startData, this.m_endData - this.m_startData + 1);
this.m_formRequest.putParameter(s3, s11);
}
if ((char)this.m_binArray[(this.m_currentIndex + 1)] == '-')
break;
}
}
public int save(String s)
throws ServletException, IOException, SmartUploadException
{
return save(s, 0);
}
public int save(String s, int i)
throws ServletException, IOException, SmartUploadException
{
int j = 0;
if (s == null)
s = this.m_application.getRealPath("/");
if (s.indexOf("/") != -1)
{
if (s.charAt(s.length() - 1) != '/')
s = s + "/";
}
else if (s.charAt(s.length() - 1) != '\\')
s = s + "\\";
for (int k = 0; k < this.m_files.getCount(); k++) {
if (this.m_files.getFile(k).isMissing())
continue;
this.m_files.getFile(k).saveAs(s + this.m_files.getFile(k).getFileName(), i);
j++;
}
return j;
}
public int getSize()
{
return this.m_totalBytes;
}
public byte getBinaryData(int i)
{
byte byte0;
try
{
byte0 = this.m_binArray[i];
}
catch (Exception exception)
{
throw new ArrayIndexOutOfBoundsException("Index out of range (1005).");
}
return byte0;
}
public Files getFiles()
{
return this.m_files;
}
public Request getRequest()
{
return this.m_formRequest;
}
public void downloadFile(String s)
throws ServletException, IOException, SmartUploadException
{
downloadFile(s, null, null);
}
public void downloadFile(String s, St