added calls to StringUtil.removeHTMLTags for comments and postings. approveHTMLTags...
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
1 package mircoders.servlet;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.*;
6 import java.net.*;
7 import java.lang.reflect.*;
8 import javax.servlet.*;
9 import javax.servlet.http.*;
10
11 import freemarker.template.*;
12 import com.oreilly.servlet.multipart.*;
13 import com.oreilly.servlet.*;
14
15 import mir.servlet.*;
16 import mir.module.*;
17 import mir.misc.*;
18 import mir.entity.*;
19 import mir.storage.*;
20 import mir.media.*;
21
22 import mircoders.entity.*;
23 import mircoders.storage.*;
24 import mircoders.module.*;
25 import mircoders.producer.*;
26
27 /*
28  *  ServletModuleOpenIndy -
29  *   is the open-access-servlet, which is responsible for
30  *    adding comments to articles &
31  *    open-postings to the newswire
32  *
33  * @author RK
34  */
35
36 public class ServletModuleOpenIndy extends ServletModule
37 {
38
39   private String          commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;
40   private String          postingFormTemplate, postingFormDoneTemplate;
41   private ModuleContent   contentModule;
42   private ModuleImages    imageModule;
43   private ModuleTopics         themenModule;
44   private String          directOp ="yes";
45
46   // Singelton / Kontruktor
47   private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
48   public static ServletModule getInstance() { return instance; }
49
50   private ServletModuleOpenIndy() {
51     try {
52       theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile"));
53       commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate");
54       commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate");
55       commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate");
56       postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
57       postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
58       directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
59       mainModule = new ModuleComment(DatabaseComment.getInstance());
60       contentModule = new ModuleContent(DatabaseContent.getInstance());
61       themenModule = new ModuleTopics(DatabaseTopics.getInstance());
62       imageModule = new ModuleImages(DatabaseImages.getInstance());
63       defaultAction="addposting";
64     }
65     catch (StorageObjectException e) {
66         theLog.printError("servletmoduleopenindy could not be initialized");
67     }
68   }
69
70
71   /**
72    *  Method for making a comment
73    */
74
75   public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
76   {
77     String aid = req.getParameter("aid"); // the article id the comment will belong to
78     if (aid!=null && !aid.equals(""))
79     {
80       SimpleHash mergeData = new SimpleHash();
81       // ok, article
82       mergeData.put("aid", aid);
83       deliver(req, res, mergeData, commentFormTemplate);
84     }
85     else throw new ServletModuleException("aid not set!");
86   }
87
88   /**
89    *  Method for inserting a comment into the Database and delivering
90    *  the commentDone Page
91    */
92
93   public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
94   {
95     String aid = req.getParameter("to_media"); // the article id the comment will belong to
96     if (aid!=null && !aid.equals(""))
97     {
98       // ok, collecting data from form
99       try {
100           HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
101          
102           //no html in comments(for now)
103           for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
104               String k=(String)i.next();
105               String v=(String)withValues.get(k);
106                   
107               withValues.put(k,StringUtil.removeHTMLTags(v));
108           }
109           
110         withValues.put("is_published","1");
111
112                 // Dupe detection
113 //              EntityList com = mainModule.getByWhereClause("description='"+req.getParameter("description")+"' AND to_media='"+aid+"' AND creator='"+req.getParameter("creator")+"'", 0);
114 //              if (com.getCount() > 0) {
115 //              theLog.printDebugInfo("Comment dupe dropped for article "+aid);
116 //              deliver(req, res, (TemplateModelRoot) null, commentFormDupeTemplate);
117 //              } else {
118
119                 // inserting into database
120                 String id = mainModule.add(withValues);
121                 
122                 // producing new page
123                 new ProducerContent().handle(null, null, true, false, aid);
124
125                 // sync the server
126                 int exitValue = Helper.rsync();
127                 theLog.printDebugInfo("rsync:"+exitValue);
128
129                 // redirecting to url
130                 // should implement back to article
131                 SimpleHash mergeData = new SimpleHash();
132                 deliver(req, res, mergeData, commentFormDoneTemplate);
133 //              }
134       }
135       catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
136       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
137
138     }
139     else throw new ServletModuleException("aid not set!");
140
141   }
142
143   /**
144    *  Method for delivering the form-Page for open posting
145    */
146
147   public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
148   {
149     SimpleHash mergeData = new SimpleHash();
150     String numOfMedia = req.getParameter("medianum");
151     if(numOfMedia==null||numOfMedia.equals("")){
152       numOfMedia="1";
153     }
154     
155     int mediaNum = Integer.parseInt(numOfMedia);
156     SimpleList mediaFields = new SimpleList();
157     for(int i =0; i<mediaNum;i++){
158       Integer mNum = new Integer(i+1);
159       mediaFields.add(mNum.toString());
160     }
161     mergeData.put("medianum",numOfMedia);
162     mergeData.put("mediafields",mediaFields);
163     mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
164     
165     
166     /** @todo popups missing */
167     try{
168       mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
169     } catch (Exception e) {
170       theLog.printError("languagePopUpData failed");
171     }
172     deliver(req, res, mergeData, postingFormTemplate);
173   }
174
175   /**
176    *  Method for inserting an open posting into the Database and delivering
177    *  the postingDone Page
178    */
179
180   public void insposting(HttpServletRequest req, HttpServletResponse res)
181     throws ServletModuleException
182   {
183     SimpleHash mergeData = new SimpleHash();
184     boolean setMedia=false;
185
186     try {
187       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
188           
189       HashMap withValues = mp.getParameters();
190       
191       // call the routines that escape html
192
193       for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
194           String k=(String)i.next();
195           String v=(String)withValues.get(k);
196           
197           if (k.equals("content_data")){
198               //this doesn't quite work yet, so for now, all html goes  
199               //              withValues.put(k,StringUtil.approveHTMLTags(v));
200               withValues.put(k,StringUtil.removeHTMLTags(v));
201           }
202           else{
203             withValues.put(k,StringUtil.removeHTMLTags(v));
204           }
205           
206       }
207
208       withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
209       withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
210       withValues.put("is_produced", "0");
211       // op-articles are immediatly published
212       withValues.put("is_published","1");
213       // if op direct article-type == newswire
214       if (directOp.equals("yes")) withValues.put("to_article_type","1");
215       
216       // owner is openposting user
217       withValues.put("to_publisher","1");
218       if (withValues.get("creator").toString().equals(""))
219         withValues.put("creator","Anonym");
220
221       // inserting  content into database
222       String cid = contentModule.add(withValues);
223
224       String[] to_topicsArr = mp.getParameterValues("to_topic"); 
225       if (to_topicsArr != null && to_topicsArr.length > 0) {
226         try{
227           DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
228           theLog.printError("setting content_x_topic success");
229         } catch (Exception e) {
230           theLog.printError("setting content_x_topic failed");
231         } //end try
232       } //end if
233         
234       // if op contains uploaddata
235       String mediaId=null;
236       int i=1;
237       for(Iterator it = mp.requestList.iterator(); it.hasNext();){
238         MpRequest mpReq = (MpRequest)it.next();
239         String fileName = mpReq.getFilename();
240         String contentType = FileUtil.guessContentTypeFromName(fileName);
241         HashMap mediaValues = new HashMap();
242
243         theLog.printError("CONTENT TYPE IS: "+contentType);
244
245         if ((contentType==null) || (contentType=="application/octet-stream")) {
246           throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
247         }
248
249
250         String mediaTitle=(String)withValues.get("media_title"+i);
251         i++;
252
253         if (mediaTitle==null)
254             mediaTitle = (String)withValues.get("title");
255
256         mediaValues.put("title", mediaTitle);
257         mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
258         mediaValues.put("to_publisher", "1"); // op user
259         mediaValues.put("to_media_folder", "7"); // op media_folder
260         mediaValues.put("is_produced", "0");
261         mediaValues.put("is_published","1");
262
263         String wc = " mime_type='"+contentType+"'";
264
265         EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
266
267         String mediaTypeId = null;
268         String mediaStorageName = null;
269         String mediaHandlerName = null;
270   
271         if (mediaTypesList.size() > 0) {
272           mediaTypeId = mediaTypesList.elementAt(0).getId();
273           mediaStorageName = mediaTypesList.elementAt(0).getValue("tablename");
274           mediaHandlerName = mediaTypesList.elementAt(0).getValue("classname");
275           mediaValues.put("to_media_type",mediaTypeId);
276           
277           String MediaId;
278           try {
279                 Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
280                 Method m = mediaStorageClass.getMethod("getInstance", null);
281                 Database mediaStorage = (Database)m.invoke(null, null);
282                 Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
283                 mediaEnt.setStorage(mediaStorage);
284                 mediaEnt.setValues(mediaValues);
285                 mediaId = mediaEnt.insert();
286
287                 theLog.printError("done inserting!!! ");
288                 Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
289                 MirMedia mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
290                 mediaHandler.set(mpReq.getMedia(), mediaEnt,mediaTypesList.elementAt(0));
291                 theLog.printError("about to run ProduceMedia"+mediaId);
292               if(mediaId!=null){
293                 new ProducerMedia().handle(null, null, false, false, mediaId);
294               }
295           } catch (Exception e) {
296                 theLog.printError("setting uploaded_media failed: "+e.toString());
297           }
298               
299
300           try{
301               DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
302               theLog.printError("setting content_x_media success");
303           } catch (Exception e) {
304               theLog.printError("setting content_x_media failed");
305           }
306
307         } else {
308           theLog.printDebugInfo("Wrong file uploaded!: " + fileName);
309           throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
310         }
311           
312       }
313
314       // producing openpostinglist
315       new ProducerOpenPosting().handle(null,null,false,false);
316       // producing new page
317       new ProducerContent().handle(null, null, false, false,cid);
318       //if direct op producing startpage
319       if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
320       
321       // sync the server
322       //should be configureable
323       int exitValue = Helper.rsync();
324       theLog.printDebugInfo("rsync: "+exitValue);
325
326     }
327     catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
328     catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
329     catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());}
330
331     deliver(req, res, mergeData, postingFormDoneTemplate);
332   }
333
334 }
335