<%@ page language="java" %> <%@ page import="java.net.*,java.util.*,java.io.*,java.lang.*,idetix.util.*,revize.*" %> <%@ page import="javax.mail.*,javax.mail.internet.*" %> <%@ page import="net.tanesha.recaptcha.ReCaptchaImpl,net.tanesha.recaptcha.ReCaptchaResponse" %> <%! String mHost, mPortStr, mUser, mPwd, mFileName, mFolder, mLocation, mEmail, mSmtpServer, mFrom, mSubject, mCC; int mPort; Socket mFTPSocket; ServerSocket mDataSocket; BufferedReader mReader; Writer mWriter; String mResult; static final String EOL = "\r\n"; String message, returnStatus; String mailBody = ""; String mailFlag = ""; String keepMailBody = ""; String keepValueString = ""; String fileFlag = ""; String captcharequired = "no"; String mycaptcharequired = ""; boolean captchaValue = false; String toemail=""; void RZsplitLocationInFileFolder() { int iIndexSlash = mLocation.lastIndexOf('/'); if(iIndexSlash < 0 ) { mFolder = "/"; mFileName = mLocation; } else { mFolder = mLocation.substring(0, iIndexSlash+1); mFileName = mLocation.substring(iIndexSlash + 1); } } private void formMailBody(HttpServletRequest inRequest) { Vector vector = new Vector(); Vector names = new Vector(); String data=""; int counter=0; String challenge=""; String uresponse=""; Vector titles = new Vector(); String note=""; String img_url=""; String finalstring=""; String finalmessage=""; try{ BufferedReader r=inRequest.getReader(); String ssx; int t = 0; while ((ssx=r.readLine()) != null) { counter++; data=ssx; if(data.indexOf("=") == -1) { finalmessage=finalmessage+data+"
"; } else { if(data.substring(0,data.indexOf("=")).equalsIgnoreCase("imageurl")){ img_url = data.substring(data.indexOf("=")+1,data.length());} if(data.substring(0,data.indexOf("=")).equalsIgnoreCase("captcharequired")){ captcharequired = data.substring(data.indexOf("=")+1,data.length());} System.out.println("thatcaptcharequired="+captcharequired); if(data.substring(0,data.indexOf("=")).equalsIgnoreCase("recaptcha_challenge_field")){ challenge=data.substring(data.indexOf("=")+1,data.length());} if(data.substring(0,data.indexOf("=")).equalsIgnoreCase("recaptcha_response_field") ){ uresponse=data.substring(data.indexOf("=")+1,data.length());} if(!(data.substring(0,data.indexOf("="))).equalsIgnoreCase("recaptcha_challenge_field") && !(data.substring(0,data.indexOf("="))).equalsIgnoreCase("recaptcha_response_field") && !(data.substring(0,data.indexOf("="))).equalsIgnoreCase("submit") && !(data.substring(0,data.indexOf("="))).equalsIgnoreCase("reset") && !(data.substring(0,data.indexOf("="))).equalsIgnoreCase("captcharequired")){ note=(data.substring(data.indexOf("=")+1,data.length())); // mailBody += note + "\n"; titles.add(note); } //mailBody = titles.get(2) + "\n"; //mailBody = titles + "\n"; } } String html_msg = titles.get(3)+finalmessage; revize.log.LogManager.getPublishingLog().error("****************"+html_msg); //html_msg = StringUtils.replaceAll(html_msg,"+"," "); //html_msg=StringUtils.replaceAll(html_msg," ", " "); finalstring= ""+img_url+""; toemail=(String)titles.get(2); //mEmail="akshayak@yahoo.com"; mailBody = "Dear "+ titles.get(1) + "," + "
" + "You have received a postcard from your friend, " + titles.get(0) + "
" + finalstring + "

" + "Your friend also included this message:" + "
" + titles.get(3) + "
"+ finalmessage+ "
"; r.close(); } catch(IOException e){ } if(!keepMailBody.equals("")){ if(keepMailBody.equals(mailBody)){ mailBody = ""; mailFlag = "false"; return ;} } if(mailBody.equals("")){ mailFlag = "false"; return ; }else{ mailFlag = "true"; } String remoteAddr = inRequest.getRemoteAddr(); ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); reCaptcha.setPrivateKey("6LejZ8gSAAAAABtVp-mTyaWFLw8Gv0LeUgR2yzTk"); reCaptcha.setPublicKey("6LejZ8gSAAAAAOlWKEGIG6SG5WwfEbI9cQ8nZcpe"); ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse); if (reCaptchaResponse.isValid()) { captchaValue=true;}else {captchaValue=false; } if(mycaptcharequired.equals("null") || mycaptcharequired.equals("") || mycaptcharequired.equals("no")) { captchaValue=true; } } String formValueString( HttpServletRequest inRequest) { String sValue = new String(""); String value = new String(""); List fieldName = new LinkedList(); List paramValues = new LinkedList(); List paramValLst = new LinkedList(); HashMap formData = new HashMap(); if(inRequest.getParameterValues("fieldNames")== null){ Enumeration params=inRequest.getParameterNames(); while(params.hasMoreElements()){ String name = (String) params.nextElement(); if(name.equalsIgnoreCase("submit")||name.equalsIgnoreCase("reset")){ break; } else{ fieldName.add(name); } } String[] vals= inRequest.getParameterValues((String)fieldName.get(0)); for(int j=0; j < vals.length; j++){ if(!sValue.equals("")) sValue +="\r\n"; for(int k=0; k= 0) { out.write(chunk, 0, count); total += count; } toRet = new String(chunk, 0, total); out.close(); in.close(); //close FTP system CloseDataSocket(); CloseFTP(); } catch(IOException e) { CloseDataSocket(); CloseFTP(); message += "ERROR :: file download from FTP location \"" + mHost + "\" failed .\\n"; message += "Possible cause of error may be : \\n"; message += "your file has either been deleted, replaced or renamed \\n\\n"; throw new IOException(e.getMessage()); } return toRet; } void ValidateReply(String reply, String expectedReplyCode) throws IOException { // all reply codes are 3 chars long String replyCode = reply.substring(0, 3); // if unexpected reply, throw an exception if (!replyCode.equals(expectedReplyCode)) { throw new IOException(replyCode + " :: " + reply.substring(4)); } } void ValidateReply(String reply, String[] expectedReplyCodes) throws IOException { // all reply codes are 3 chars long String replyCode = reply.substring(0, 3); for (int i = 0; i < expectedReplyCodes.length; i++) if (replyCode.equals(expectedReplyCodes[i])) return; // got this far, not recognised throw new IOException(reply.substring(4) + " :: " + replyCode); } void SetDataPort(InetAddress host, short portNo) throws IOException { try { byte[] hostBytes = host.getAddress(); byte[] portBytes = toByteArray(portNo); // assemble the PORT command String cmd = new StringBuffer ("PORT ") .append (toUnsignedShort (hostBytes[0])) .append (",") .append (toUnsignedShort (hostBytes[1])) .append (",") .append (toUnsignedShort (hostBytes[2])) .append (",") .append (toUnsignedShort (hostBytes[3])) .append (",") .append (toUnsignedShort (portBytes[0])) .append (",") .append (toUnsignedShort (portBytes[1])) .toString (); // send command and check reply mWriter.write(cmd + EOL); mWriter.flush(); mResult = mReader.readLine(); if ( mResult != null ) ValidateReply(mResult, "200" ); } catch( IOException e ) { throw new IOException(e.getMessage()); } } short toUnsignedShort(byte value) { return ( value < 0 ) ? (short) (value + 256) : (short) value; } byte[] toByteArray (short value) { byte[] bytes = new byte[2]; bytes[0] = (byte) (value >> 8); // bits 1- 8 bytes[1] = (byte) (value & 0x00FF); // bits 9-16 return bytes; } String decodePassword(String pPwd) { String outStr = new String(""); int i = 1; int idx = 0; for ( int j = 0; j < pPwd.length(); j++ ) { if ( j == (idx + 1) ) { i++; idx += i + 1; } else { outStr += (char)(pPwd.charAt(j) - 1); } } return outStr; } private Vector getToList(String pToList){ Vector myToList = new Vector(); StringTokenizer stk = new StringTokenizer(pToList, ","); int emailCount = stk.countTokens(); myToList = new Vector(); try { for(int i = 0; i < emailCount; i++) { myToList.add(new InternetAddress(stk.nextToken().trim())); } }catch(AddressException ae){ ae.printStackTrace(); } return myToList; } private void setRecipients( Message msg, List recipients, Message.RecipientType recipientType) throws MessagingException { Iterator iterator = recipients.iterator(); if (recipients.size() == 0) return; Address[] addresses = new Address[recipients.size()]; addresses = (Address[]) recipients.toArray(addresses); msg.setRecipients(recipientType, addresses); } public synchronized void sendEmail(String pToList, String pSubject, String pBody, String pFrom) throws Exception { InternetAddress addressFrom = new InternetAddress(pFrom); if (addressFrom == null || addressFrom.getAddress() == null || addressFrom.getAddress().length() == 0){ throw new Exception("from address not found"); } if (mSmtpServer == null || mSmtpServer.length() == 0){ throw new Exception("Unable to send mail: Workflow configuration property SMTP Server is not set"); } Properties props = new Properties(); props.put("mail.smtp.host",mSmtpServer); if(!mUser.equals("null") && !mPwd.equals("null")) { props.put("mail.smtp.auth", "true"); } else { props.put("mail.smtp.auth", "false"); } Session session = Session.getDefaultInstance (props, null); Message msg = new MimeMessage(session); try{ msg.setFrom(addressFrom); setRecipients( msg, getToList( pToList), Message.RecipientType.TO); if(!mCC.equals("null") && !mCC.equals("")) { setRecipients( msg, getToList( mCC), Message.RecipientType.CC); } msg.setSubject(pSubject); if(!pBody.equals("")) { msg.setContent(pBody, "text/html"); pBody = StringUtils.replaceAll(pBody,"+"," "); pBody=""+pBody+""; msg.setContent(pBody, "text/html; charset=utf-8"); //msg.setText(pBody); } //Transport.send(msg); Transport tr = session.getTransport("smtp"); tr.connect(mSmtpServer, mUser, mPwd); msg.saveChanges(); // don't forget this tr.sendMessage(msg, msg.getAllRecipients()); tr.close(); } catch (SendFailedException sfe){ Address[] address = sfe.getInvalidAddresses(); sfe.printStackTrace(); throw new Exception(sfe.getMessage()); } catch (MessagingException me){ me.printStackTrace(); throw new Exception(me.getMessage()); } } %> <% mHost = "null"; mUser = System.getProperty(RevizeProperties.FORM_EMAIL_AUTH_USER); mPwd = System.getProperty(RevizeProperties.FORM_EMAIL_AUTH_PASSWORD); mLocation = "null"; mPortStr = "null"; //mEmail = request.getParameter("emailTo"); //if (mEmail == null || mEmail.length() == 0) mEmail = "ray@revize.com"; //mEmail = titles.get(2); mSmtpServer = System.getProperty(RevizeProperties.FORM_EMAIL_SMTP_SERVER); mFrom = "listserv@revize.com"; mSubject = "Post Card"; mCC = ""; mPort = 0; mFileName = ""; mFolder = ""; mFTPSocket = null; mReader = null; mWriter = null; mResult = null; mDataSocket = null; message = null; returnStatus = null; if(mEmail.equals("") || mEmail.equals("null")) { String savemHost = mHost; String savemUser = mUser; String savemPortStr = mPortStr; String savemPwd = mPwd; String endLoop = "false"; String firstIteration = "true"; String valueStr = ""; message = ""; do { if(savemHost.indexOf(",") != -1) { mHost = savemHost.substring(0,savemHost.indexOf(",")); mUser = savemUser.substring(0,savemUser.indexOf(",")); mPwd = savemPwd.substring(0,savemPwd.indexOf(",")); mPortStr = savemPortStr.substring(0,savemPortStr.indexOf(",")); savemHost = savemHost.substring(savemHost.indexOf(",")+1,savemHost.length()); savemUser = savemUser.substring(savemUser.indexOf(",")+1,savemUser.length()); savemPwd = savemPwd.substring(savemPwd.indexOf(",")+1,savemPwd.length()); savemPortStr = savemPortStr.substring(savemPortStr.indexOf(",")+1,savemPortStr.length()); } else { endLoop = "true"; mHost = savemHost; mUser = savemUser; mPwd = savemPwd; mPortStr = savemPortStr; } if( mPortStr != null && 0 < mPortStr.trim().length() ) mPort = new Integer(mPortStr).intValue(); RZsplitLocationInFileFolder(); mPwd = decodePassword(mPwd); try { /*----- File Processing 1. file download and store the content in memory. 2. append new values to current file content 3. upload new file with same name and new content created in step 2 */ //String newFileStr = DownloadFile("/" + mFileName); String newFileStr = DownloadFile(mFileName); if( newFileStr != null && newFileStr.length() > 0) { // "download success" now form string and upload file if(firstIteration == "true") { valueStr = formValueString(request); firstIteration = "false"; } // continue with appending the value line in existing file if("true" == fileFlag) { newFileStr += "\r\n" + valueStr; //upload the new file UploadFile( newFileStr.getBytes() ); //sendEmail(mEmail, "new data submitted", mailBody); } } else { //download fail returnStatus = "File Operation Failure"; //message = "Exception Occured During File Operation"; } } catch( IOException e ) { returnStatus = "File Operation Failure"; //message = "Error During Form Submission: Your file at the ftp location has either been deleted or replaced or renamed"; } //if(message.substring(message.indexOf(mHost)+mHost.length(),message.length()).indexOf("ERROR") == -1) { if(message == null || message.equals("")) message += "Successful Form Submission\\n\\n"; //} }while(endLoop != "true"); } else { formMailBody(request); if(!captchaValue) message = "Text entered in the verification box is not correct"; if(mailFlag.equals("true") && captchaValue) { try { sendEmail(toemail, mSubject, mailBody, mFrom); keepMailBody = mailBody; mailBody = ""; message = "Successful Form Submission"; } catch(Exception e) { returnStatus = "emai sending Failed"; mailBody = ""; message = "Error During Form Submission"; } } } String returnURL = request.getRequestURL().toString(); int index = returnURL.lastIndexOf("."); String extension = returnURL.substring(index); boolean match = returnURL.regionMatches(true, index + 1, "jsp", 0, 3); //if(match) //{ returnURL = returnURL.substring( 0, index-2); //} URL url = null; //try{ // url = new URL( returnURL); // Object obj = url.getContent(); //} //catch(IOException e) //{ // url = null; // e.printStackTrace(); //} %>