added some stuff to make sure xml is encoded properly...
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
index b22ea33..56a4fdf 100755 (executable)
@@ -44,6 +44,8 @@ import freemarker.template.*;
 import com.oreilly.servlet.multipart.*;\r
 import com.oreilly.servlet.*;\r
 \r
+import org.apache.commons.net.smtp.*;\r
+\r
 import org.xml.sax.InputSource;\r
 import org.xml.sax.XMLReader;\r
 \r
@@ -67,6 +69,7 @@ import mir.entity.*;
 import mir.storage.*;\r
 import mir.media.*;\r
 import mir.log.*;\r
+import mir.util.StringRoutines;\r
 \r
 import mircoders.entity.*;\r
 import mircoders.storage.*;\r
@@ -84,7 +87,7 @@ import mircoders.search.*;
  *    open-postings to the newswire\r
  *\r
  * @author mir-coders group\r
- * @version $Id: ServletModuleOpenIndy.java,v 1.50 2002/12/14 17:36:17 zapata Exp $\r
+ * @version $Id: ServletModuleOpenIndy.java,v 1.53 2003/01/12 22:25:18 john Exp $\r
  *\r
  */\r
 \r
@@ -96,6 +99,7 @@ public class ServletModuleOpenIndy extends ServletModule
   private String        postingFormTemplate, postingFormDoneTemplate,\r
     postingFormDupeTemplate;\r
   private String        searchResultsTemplate;\r
+  private String        prepareMailTemplate,sentMailTemplate;\r
   private ModuleContent contentModule;\r
   private ModuleComment commentModule;\r
   private ModuleImages  imageModule;\r
@@ -117,6 +121,8 @@ public class ServletModuleOpenIndy extends ServletModule
       postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");\r
       postingFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDupeTemplate");\r
       searchResultsTemplate = MirConfig.getProp("ServletModule.OpenIndy.SearchResultsTemplate");\r
+      prepareMailTemplate = MirConfig.getProp("ServletModule.OpenIndy.PrepareMailTemplate");\r
+      sentMailTemplate = MirConfig.getProp("ServletModule.OpenIndy.SentMailTemplate");\r
       directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();\r
       passwdProtection = MirConfig.getProp("PasswdProtection").toLowerCase();\r
       mainModule = new ModuleComment(DatabaseComment.getInstance());\r
@@ -197,6 +203,7 @@ public class ServletModuleOpenIndy extends ServletModule
             withValues.put(k,StringUtil.removeHTMLTags(v));\r
           }\r
           withValues.put("is_published","1");\r
+          withValues.put("to_comment_status","1");\r
 \r
           //checking the onetimepasswd\r
           if(passwdProtection.equals("yes")){\r
@@ -261,10 +268,13 @@ public class ServletModuleOpenIndy extends ServletModule
     }\r
 \r
     String maxMedia = MirConfig.getProp("ServletModule.OpenIndy.MaxMediaUploadItems");\r
+    String defaultMedia = MirConfig.getProp("ServletModule.OpenIndy.DefaultMediaUploadItems");\r
     String numOfMedia = req.getParameter("medianum");\r
+\r
     if(numOfMedia==null||numOfMedia.equals("")){\r
-      numOfMedia="1";\r
-    } else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {\r
+      numOfMedia=defaultMedia;\r
+    }\r
+    else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {\r
       numOfMedia = maxMedia;\r
     }\r
 \r
@@ -352,7 +362,10 @@ public class ServletModuleOpenIndy extends ServletModule
         if (k.equals("content_data")){\r
           //this doesn't quite work yet, so for now, all html goes\r
           //withValues.put(k,StringUtil.approveHTMLTags(v));\r
-          //withValues.put(k,StringUtil.removeHTMLTags(v));\r
+          withValues.put(k,StringUtil.deleteForbiddenTags(v));\r
+        } else if (k.equals("description")) {\r
+          String tmp = StringUtil.deleteForbiddenTags(v);\r
+          withValues.put(k,StringUtil.deleteHTMLTableTags(tmp));\r
         } else {\r
           withValues.put(k,StringUtil.removeHTMLTags(v));\r
         }\r
@@ -417,7 +430,10 @@ public class ServletModuleOpenIndy extends ServletModule
         throw new ServletModuleException(t.getMessage());\r
       }\r
     }\r
-    catch (FileHandlerException e) { throw new ServletModuleException("MediaException: "+ e.getMessage());}\r
+    catch (FileHandlerException e) {\r
+      e.printStackTrace(System.out);\r
+      throw new ServletModuleException("MediaException: "+ e.getMessage());\r
+    }\r
     catch (IOException e) { throw new ServletModuleException("IOException: "+ e.getMessage());}\r
     catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.getMessage());}\r
     catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.getMessage());}\r
@@ -425,6 +441,134 @@ public class ServletModuleOpenIndy extends ServletModule
     deliver(req, res, mergeData, postingFormDoneTemplate);\r
   }\r
 \r
+    /*\r
+   * Method for preparing and sending a content as an email message\r
+   */\r
+  \r
+  public void mail(HttpServletRequest req, HttpServletResponse res)\r
+    throws ServletModuleException, ServletModuleUserException {\r
+    String aid = req.getParameter("mail_aid");\r
+    if (aid == null){\r
+      throw new ServletModuleUserException("An article id must be specified in requests to email an article.  Something therefore went badly wrong....");\r
+    }\r
+    \r
+    String to = req.getParameter("mail_to");\r
+    String from = req.getParameter("mail_from");\r
+    String from_name = req.getParameter("mail_from_name");\r
+    String comment = req.getParameter("mail_comment");\r
+    String mail_language = req.getParameter("mail_language");\r
+    \r
+    SimpleHash mergeData = new SimpleHash();\r
+    \r
+    if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){\r
+\r
+      for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) {\r
+       String pName=(String)theParams.nextElement();\r
+       if (pName.startsWith("mail_")){\r
+         mergeData.put(pName,new SimpleScalar(req.getParameter(pName)));\r
+       }\r
+      }\r
+      deliver(req,res,mergeData,prepareMailTemplate); \r
+    }\r
+    else {\r
+      //run checks on to and from and mail_language to make sure no monkey business occurring\r
+      if (mail_language.indexOf('.') != -1 || mail_language.indexOf('/') != -1 ){\r
+       throw new ServletModuleUserException("Sorry, you've entered an illegal character into the language field.  Go back and try again, asshole.");\r
+      }\r
+      if (to.indexOf('\n') != -1 \r
+         || to.indexOf('\r') != -1 \r
+         || to.indexOf(',') != -1 \r
+         || from.indexOf('\n') != -1 \r
+         || from.indexOf('\r') != -1 \r
+         || from.indexOf(',') != -1 ){\r
+       throw new ServletModuleUserException("Sorry, you've entered an illegal character into the from or to field.  Go back and try again.");\r
+      }\r
+      EntityContent contentEnt;\r
+      try{\r
+       contentEnt = (EntityContent)contentModule.getById(aid);\r
+      }\r
+      catch (ModuleException e){ \r
+       throw new ServletModuleUserException("Couldn't get content for article "+aid);\r
+      }\r
+      String producerStorageRoot=MirConfig.getProp("Producer.StorageRoot");\r
+      String producerDocRoot=MirConfig.getProp("Producer.DocRoot");\r
+      String publishPath = contentEnt.getValue("publish_path");\r
+      String txtFilePath = producerStorageRoot + producerDocRoot + "/" + mail_language + \r
+        publishPath + "/" + aid + ".txt";\r
+      \r
+\r
+      File inputFile = new File(txtFilePath);\r
+      String content;\r
+      \r
+      try{\r
+       FileReader in = new FileReader(inputFile);\r
+       StringWriter out = new StringWriter();\r
+       int c;\r
+       while ((c = in.read()) != -1)\r
+         out.write(c);\r
+       in.close();\r
+       content= out.toString();\r
+      }\r
+      catch (FileNotFoundException e){\r
+       throw new ServletModuleUserException("No text file found in " + txtFilePath);\r
+      }\r
+      catch (IOException e){\r
+       throw new ServletModuleUserException("Problem reading file in " + txtFilePath);\r
+      }\r
+      // add some headers\r
+      content = "To: " + to + "\nReply-To: "+ from + "\n" + content;\r
+      // put in the comment where it should go\r
+      if (comment != null) { \r
+       String commentTextToInsert = "\n\nAttached comment from " + from_name + ":\n" + comment;\r
+       try {\r
+         content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!",commentTextToInsert);\r
+       }\r
+       catch (Exception e){\r
+         throw new ServletModuleUserException("Problem doing regular expression replacement " + e.toString());\r
+       }\r
+      }\r
+      else{\r
+       try {\r
+         content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!","");\r
+       }\r
+       catch (Exception e){\r
+         throw new ServletModuleUserException("Problem doing regular expression replacement " + e.toString());\r
+       }\r
+      }\r
+      \r
+      SMTPClient client=new SMTPClient();\r
+      try {\r
+       int reply;\r
+       client.connect("localhost");\r
+       System.out.print(client.getReplyString());\r
+       \r
+       reply = client.getReplyCode();\r
+       \r
+       if(!SMTPReply.isPositiveCompletion(reply)) {\r
+         client.disconnect();\r
+         throw new ServletModuleUserException("SMTP server refused connection.");\r
+       }\r
+       \r
+       client.sendSimpleMessage(MirConfig.getProp("ServletModule.OpenIndy.EmailIsFrom"),to,content);\r
+       \r
+       client.disconnect();\r
+       //mission accomplished\r
+       deliver(req,res,mergeData,sentMailTemplate); \r
+\r
+      } catch(IOException e) {\r
+       if(client.isConnected()) {\r
+         try {\r
+           client.disconnect();\r
+         } catch(IOException f) {\r
+           // do nothing\r
+         }\r
+       }\r
+       throw new ServletModuleUserException(e.toString());\r
+      }\r
+    }\r
+  }\r
+\r
+\r
   /*\r
    * Method for querying a lucene index\r
    */\r