001    
002    package com.rain;
003    
004    import java.io.*;
005    import java.net.URL;
006    import java.util.*;
007    import java.lang.*;
008    import javax.imageio.ImageIO;
009    import java.awt.image.BufferedImage;
010    import java.awt.Graphics2D;
011    import javax.servlet.http.*;
012    import javax.servlet.ServletInputStream;
013    import com.oreilly.servlet.*;
014    import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
015    
016    /**  
017     * ÆÄÀÏ Ã·ºÎ °ü¸® ¹× ·¹ÀÎ ¿¡µðÅÍ Àü¼Û ³»¿ë °ü¸®
018     * @version 9.0
019     * @author http://cafen.net (outmind@cafen.net)
020     */
021    public class rainFile {
022    
023    /**  
024     * ±âº» ÀÛ¾÷ °ø°£
025     */
026            public String base_root = ".";
027    
028    /**  
029     * ±âº» ÀÛ¾÷ ÀÇ À¥URL
030     */
031            public String base_url = ".";
032    
033    /**  
034     * ÆÄÀÏ Àӽõî·Ï °ø°£
035     */
036            private String tmp_dir = "tmp";
037    
038    /**  
039     * µî·Ï±ÝÁö È®ÀåÀÚ¸í
040     */
041            private String forbidden_extension_pattern = "(html|htm|php|php3|cgi|phtml|shtml|jsp|asp|exe|com|dll)$";
042    
043    /**  
044     * ¸¶Áö¸· ¿À·ù ¸Þ¼¼Áö
045     */
046            public String error_msg = "";
047    
048    /**  
049     * MultipartRequest Àü¼Û¹ÞÀº µ¥ÀÌŸ
050     */
051            private MultipartRequest multi;
052    
053    /**  
054     * Enumeration Àü¼Û¹ÞÀº µ¥ÀÌŸ
055     */
056            private Enumeration formNames;
057    
058    /**  
059     * ÃÖ´ë Àü¼Û¹ÞÀ» ¼ö ÀÖ´Â µ¥ÀÌŸ ¿ë·®(50M)
060     */
061            private int max_uploadsize = 50 * 1024 * 1024;
062    
063    /**  
064     * »ý¼ºÀÚ
065     */
066            public rainFile(){}
067    
068    /**  
069     * »ý¼ºÀÚ
070     * @param  request  HttpServletRequest(multipart)
071     */
072            public rainFile(HttpServletRequest request){
073                    this.setMultiPart(request);
074            }
075            
076    /**  
077     * »ý¼ºÀÚ
078     * @param  base_root  ±âº»ÀÛ¾÷°ø°£
079     */
080            public rainFile(String base_root) {
081                    this.setBaseDir(base_root);
082            }
083    
084    /**  
085     * »ý¼ºÀÚ
086     * @param  base_root  ±âº»ÀÛ¾÷°ø°£
087     * @param  request  HttpServletRequest(multipart)
088     */
089            public rainFile(String base_root, HttpServletRequest request) {
090                    this.setBaseDir(base_root);
091                    this.setMultiPart(request);
092            }
093    
094    /**  
095     * ±âº»ÀÛ¾÷°ø°£ ¼³Á¤
096     * @param  base_root  ±âº»ÀÛ¾÷°ø°£
097     */
098            public void setBaseDir(String base_root) {
099                    this.base_root = base_root;
100                    this.tmp_dir = this.base_root + "junk/";
101            }
102                    
103    /**  
104     * ¸ÖƼ ÆÄÆ® ¼³Á¤
105     * @param  request  HttpServletRequest(multipart)
106     */
107            public void setMultiPart(HttpServletRequest request) {
108                    try {
109                            this.multi= new MultipartRequest(request, this.tmp_dir , this.max_uploadsize, new DefaultFileRenamePolicy());
110                            this.formNames= this.multi.getFileNames();
111                    } catch(Exception e) { this.error_msg += e.getMessage();}
112            }
113            
114    /**  
115     * ¸ÖƼ ÆÄÆ®¿¡¼­ º¯¼ö ÀÐ¾î ¿À±â
116     * @param  nm ÀÐ¾î ¿Ãº¯¼ö¸í
117     * @return º¯¼ö°ª
118     */
119            public String getParameter(String nm) {
120                    String ret_val = "";
121                    try {
122                            ret_val = this.multi.getParameter(nm);
123                    } catch(Exception e) { 
124                            this.error_msg += e.getMessage() ;
125                            return "";
126                    }
127                    if (ret_val == null) 
128                            ret_val = "";
129                    return ret_val;
130            }
131    
132    /**  
133     * ¸ÖƼ ÆÄÆ®¿¡¼­ ƯÁ¤ form¸íÀÇ ÆÄÀÏ¸í °¡Á®¿À±â
134     * @param  formName ÆûÀ̸§
135     * @return ÆÄÀϸí
136     */
137            public String getFileName(String formName) {
138                    String fileName = "";
139                    try {
140                            fileName=this.multi.getOriginalFileName(formName);
141                    } catch(Exception e) {}
142                    return fileName;
143            }
144            
145    /**  
146     * ¸ÖƼ ÆÄÆ®¿¡¼­ ƯÁ¤ form¸íÀÇ ÆÄÀÏÅ©±â °¡Á®¿À±â
147     * @param  formName ÆûÀ̸§
148     * @return ÆÄÀÏÅ©±â
149     */
150            public int getFileSize(String formName) {
151                    int fileSize = 0;
152                    try {
153                            fileSize= (int) (this.multi.getFile(formName)).length();
154                    } catch(Exception e) {}
155                    return fileSize;
156            }
157    
158    /**  
159     * ¼­¹ö¿¡ ÀúÀåµÈ ÆÄÀÏ¿¡¼­ ƯÁ¤ ÆÄÀÏÀÇ Å©±â °¡Á®¿À±â
160     * @param  fileName ÆÄÀϸí
161     * @return ÆÄÀÏÅ©±â
162     */
163            public int getSize(String fileName) {
164                    int file_size = 0;
165            try {
166                            file_size = (int) (new File(this.base_root + fileName)).length();
167                    } catch(Exception e) {
168                            file_size = 0;
169                    }
170                    return file_size;
171            }
172    
173    /**  
174     * ÆÄÀϰæ·Î ¸í¿¡¼­ ÆÄÀÏ¸í¸¸ °¡Á®¿À±â
175     * @param  fileName ÆÄÀϸí
176     * @return ÆÄÀϸí
177     */
178            public String getName(String fileName) {
179                    String file_name = "";
180            try {
181                            file_name = (new URL(fileName)).getPath();
182                            file_name = file_name.substring(file_name.lastIndexOf("/") +1, file_name.length());
183                    } catch(Exception e) {
184                            file_name = "";
185                    }
186                    return file_name;
187            }
188    
189    /**  
190     * Uniq ÇÑ ÆÄÀÏ¸í »ý¼º
191     * @param  prefix ¾Õ÷ÀÚ
192     * @return ÆÄÀϸí
193     */
194            public String nameUnique(String prefix) {
195                    Calendar nowDate = Calendar.getInstance();
196                    return prefix + nowDate.getTimeInMillis();
197            }
198            
199    /**  
200     * ƯÁ¤ÆÄÀϸíÀÌ ¾÷·Îµå °¡´ÉÇÑ ÆÄÀϸíÀÎÁö È®ÀÎ
201     * @param  fileName ÆÄÀϸí
202     * @return ¾÷·Îµå °¡´É¿©ºÎ
203     */
204            public Boolean isUploadable(String fileName) {
205                    return !(rainUtil.find(this.forbidden_extension_pattern, fileName));
206            }
207            
208    /**  
209     * À¥»ó¿¡ ÀÖ´Â ÆÄÀÏÀ» ·ÎÄü­¹ö¿¡ ÀúÀå
210     * @param  remoteName ¸®¸ðÆ® ¼­¹ö URL
211     * @param  tar_folderfile ÀúÀåÇÒ À§Ä¡(¸¶Áö¸·¿¡ / ÀÌ ÀÖ´Â °æ¿ì Æúµå·Î °£ÁÖ)
212     * @param  prefix ¾Õ÷ºÎ
213     * @return ¾÷·ÎµåµÈ ÆÄÀÏ °æ·Î¸í
214     */
215            public String remote_Copy(String remoteName, String tar_folderfile , String prefix) {
216                    String move_file = "";
217                    try {
218                            URL theURL = new URL(remoteName);
219                            InputStream input = theURL.openStream();
220                            String filename = this.getName(remoteName);
221                            if (filename == null ||filename.equals(""))
222                                    filename = "tmp.tmp";
223                            String fileext = (this.isUploadable(filename)) ? this.name2Ext(filename):"tmp";
224                            
225                            move_file = tar_folderfile + this.nameUnique(prefix) + "." + fileext;
226    
227                            if (! move_file.equals("") && rainUtil.checkDir(this.base_root,move_file)) {
228    //                              rainUtil.errorLog(this.base_root + move_file);
229                                    FileOutputStream output = new FileOutputStream(new File( this.base_root + move_file ));
230                                    int line = -1;
231                                    while((line = input.read()) != -1) {
232                                            output.write(line);
233                                    }
234                                    output.close();
235                                    input.close();
236                            } else 
237                                    move_file = "";
238                    } catch(Exception e) {
239                            move_file = "";
240    //                      rainUtil.errorLog(e.getMessage());
241                    }
242                    return move_file;
243            }
244    
245    /**  
246     * Àü¼Û ¹ÞÀº multi part ¿¡¼­ ƯÁ¤ µ¥ÀÌŸ¸¦ ƯÁ¤ ¿µ¿ªÀ¸·Î ÀÌÀü
247     * @param  formName ÆûÀ̸§
248     * @param  tar_folderfile ÀúÀåÇÒ À§Ä¡(¸¶Áö¸·¿¡ / ÀÌ ÀÖ´Â °æ¿ì Æúµå·Î °£ÁÖ)
249     * @return ¾÷·ÎµåµÈ ÆÄÀÏ °æ·Î¸í
250     */
251            public String file_Copy(String formName, String tar_folderfile) {
252                    return this.file_Copy( formName, tar_folderfile , "");
253            }
254    
255    /**  
256     * Àü¼Û ¹ÞÀº multi part ¿¡¼­ ƯÁ¤ µ¥ÀÌŸ¸¦ ƯÁ¤ ¿µ¿ªÀ¸·Î ÀÌÀü
257     * @param  formName ÆûÀ̸§
258     * @param  tar_folderfile ÀúÀåÇÒ À§Ä¡(¸¶Áö¸·¿¡ / ÀÌ ÀÖ´Â °æ¿ì Æúµå·Î °£ÁÖ)
259     * @param  prefix ¾Õ÷ÀÚ
260     * @return ¾÷·ÎµåµÈ ÆÄÀÏ °æ·Î¸í
261     */
262            public String file_Copy(String formName, String tar_folderfile , String prefix) {
263                    String fileName = "", fileServer = "", fileType = "", move_file = "", tar_file = "", tar_folder = "";
264                    int fileSize = 0;
265                    if (tar_folderfile.equals("") || tar_folderfile.equals(".")) 
266                            tar_folderfile = "/";
267                    try {
268                            fileServer = this.multi.getFilesystemName(formName);
269                            fileName=this.multi.getOriginalFileName(formName);
270                            fileType=this.multi.getContentType(formName);
271                            fileSize = (int) (this.multi.getFile(formName)).length() ;
272    
273                            if (!fileServer.equals("")) {
274                                    if (!tar_folderfile.equals("") && tar_folderfile.substring(tar_folderfile.length() -1,tar_folderfile.length()).equals("/")) {
275                                            tar_file = "";
276                                            tar_folder = tar_folderfile.substring(0,tar_folderfile.length() -1);
277                                    } else {
278                                            tar_file = (new File(tar_folderfile)).getName();
279                                            tar_folder = (new File(tar_folderfile + "/")).getParent();
280                                    }
281                                    if (tar_file.equals("")) {
282                                            Calendar nowDate = Calendar.getInstance();
283                                            String tar_file_name = prefix 
284                                                    + nowDate.getTimeInMillis();
285                                            String tar_file_extension = "." + ((this.isUploadable(fileName)) ? this.name2Ext(fileName):"tmp");
286                                            String strSeq = "";
287                                            while ((new File(this.base_root + tar_folder +"/" + tar_file + strSeq + tar_file_extension)).exists()) {
288                                                    strSeq = (strSeq.equals(""))?"0":strSeq;
289                                                    int intSeq = Integer.parseInt(strSeq)+1;
290                                                    strSeq = String.valueOf(intSeq);
291                                            }
292                                            move_file = tar_folder +"/" +tar_file_name + strSeq + tar_file_extension;
293                                    } else 
294                                            move_file = tar_folder +"/" +tar_file ;
295    
296                                    if (! move_file.equals("") && rainUtil.checkDir(this.base_root,move_file)) 
297                                            (new File(this.tmp_dir + fileServer)).renameTo((new File( this.base_root + move_file )));
298                                    else 
299                                            move_file = "";
300                            } else
301                                    move_file = "";
302                    } catch(Exception e) {
303    
304                    }
305                    return move_file ;      
306            }
307    
308    /**  
309     * ƯÁ¤ À̹ÌÁöÆÄÀÏÀÇ °¡·Î¼¼·Î Á¤º¸ °¡Á®¿À±â
310     * @param  fileName À̹ÌÁö ÆÄÀϰæ·Î
311     * @return [°¡·Î,¼¼·Î]
312     */
313            public int[] getImageInfo(String fileName) {
314                    int img_info[] = {0,0};
315            try {
316                    FileInputStream src = new FileInputStream(new File(this.base_root + fileName));
317                            BufferedImage srcImg = ImageIO.read(src);
318                            img_info[0] = srcImg.getWidth();
319                            img_info[1] = srcImg.getHeight();
320                            src.close();
321            } catch(Exception e) { error_msg += e.getMessage() ; }
322            return img_info;
323            }
324    
325    /**  
326     * ƯÁ¤ ÆÄÀÏ ´Ù¿î·Îµå
327     * @param  fileName ´Ù¿î·ÎµåÇÒ ÆÄÀÏ À§Ä¡
328     * @param  response HttpServletResponse
329     * @param  downName ´Ù¿î·Îµå¸í
330     */
331            public void downLoad(String fileName, HttpServletResponse response, String downName) {
332            try {
333                            byte[] buffer = new byte[8192];
334                            response.reset();
335                            response.setHeader("Content-Disposition", "attachment; filename=\"" + rainUtil.k2a(downName) + "\"");
336                            OutputStream w = response.getOutputStream();
337                            FileInputStream input = new FileInputStream(new File(this.base_root + fileName));
338                            while (input.available() > 0) {
339                                    int cnt = input.read(buffer);
340                                    w.write(buffer, 0, cnt);
341                            }
342                            w.close();
343                            input.close();
344            } catch(Exception e) { 
345                    error_msg += e.getMessage() ; 
346            }
347            }
348    
349    /**  
350     * ƯÁ¤ ÆÄÀÏ À̹ÌÁö ÇüÀ¸·Î ´Ù¿î·Îµå
351     * @param  fileName ´Ù¿î·ÎµåÇÒ ÆÄÀÏ À§Ä¡
352     * @param  response HttpServletResponse
353     */
354            public void sendImage(String fileName, HttpServletResponse response) {
355            try {
356                    FileInputStream src = new FileInputStream(new File(this.base_root + fileName));
357                            BufferedImage srcImg = ImageIO.read(src);
358                            String ext = this.name2Ext(fileName);
359                            ImageIO.write(srcImg, ext, response.getOutputStream());
360                            response.getOutputStream().flush();
361                            response.getOutputStream().close();
362                            src.close();
363            } catch(Exception e) { error_msg += e.getMessage() ;  }
364            }
365    
366    /**  
367     * ƯÁ¤ ÆÄÀÏ ³»¿ë Àбâ
368     * @param  fileName ÀÐÀ» ÆÄÀÏ À̸§
369     * @return  ÆÄÀÏ ³»¿ë
370     */
371            public String fileLoader(String fileName) {
372                    String ret_val = "";
373            try {
374                            FileReader fis = new FileReader (new File(this.base_root + fileName));
375                            BufferedReader in = new BufferedReader (fis);
376                            String nextLine = "";
377                            while ((nextLine =  in.readLine()) != null ) ret_val += nextLine + "\r\n";
378                            in.close();
379                    } catch(Exception e) { error_msg += e.getMessage() ;  }
380                    return  ret_val;
381            }
382    
383    /**  
384     * ÆÄÀÏ¸í¿¡¼­ È®ÀåÀÚ°¡Á®¿À±â
385     * @param  fileName ÀÐÀ» ÆÄÀÏ À̸§
386     * @return  ÆÄÀÏ È®ÀåÀÚ
387     */
388            public String name2Ext(String fileName) {
389                    String tar_file_extension = "";
390                    if (!fileName.equals("")) {
391                            tar_file_extension = fileName.substring(fileName.lastIndexOf(".") +1, fileName.length()).toLowerCase();
392                            if(tar_file_extension.equals("")) tar_file_extension = "tmp";
393                    }
394                    return tar_file_extension;
395            }
396    
397    /**  
398     * À̹ÌÁö ÆÄÀÏ¿©ºÎ È®ÀÎ
399     * @param  fileName ´ë»óÆÄÀÏ
400     * @return  À̹ÌÁöÆÄÀÏ¿©ºÎ
401     */
402            public Boolean isImage(String fileName) {
403                    return rainUtil.find("(gif|jpeg|jpg|png)$",fileName);
404            }
405    
406    /**  
407     * µ¿¿µ»ó ÆÄÀÏ¿©ºÎ È®ÀÎ
408     * @param  fileName ´ë»óÆÄÀÏ
409     * @return  µ¿¿µ»óÆÄÀÏ¿©ºÎ
410     */
411            public Boolean isMovie(String fileName) {
412                    return rainUtil.find("(asf|mp3|pmeg|pmg|avi|wma|wmv|flv)$", fileName);
413            }       
414    
415    /**  
416     * ÆÄÀÏÅ©±â¸¦ Àб⠽¬¿î ÇüÅ·Πº¯È¯
417     * @param  file_size ÆÄÀÏÅ©±â
418     * @return  ÆÄÀÏÅ©±â
419     */
420            public String file_Size(int file_size) {
421                    if (file_size <= 0 ) 
422                            return "";
423                    else if (file_size >= 1024*1024) 
424                            return Integer.valueOf(Math.round(file_size/(1024*1024))) + " M";
425                    else if (file_size >= 1024) 
426                            return Integer.valueOf(Math.round(file_size/1024)) + " K";
427                    else 
428                            return Integer.valueOf(file_size) + " B";
429            } 
430    
431    /**  
432     * ASC ¸¦ EUC-KR ·Î º¯È¯
433     * @param  str ´ë»ó ¹®ÀÚ
434     * @return  º¯È¯µÈ¹®ÀÚ
435     */
436            public static String a2k(String str) {
437                    String rtn = null;
438                    try {
439                            rtn = (str==null)?"":new String(str.getBytes("8859_1"),"euc-kr");
440                    } catch (java.io.UnsupportedEncodingException e) {}
441                    return rtn;
442            }
443    
444    /**  
445     * ¹®ÀåÀÌ text ÀÎÁö html ÇüÀÎÁö¸¦ ÀÚµ¿ ÆÇ´ÜÈÄ À¥¿¡¼­ Àб⠰¡´ÉÇÑ ÇüÅ·Πº¯È¯
446     * @param  s ´ë»ó ¹®ÀÚ
447     * @return  º¯È¯µÈ¹®ÀÚ
448     */
449            public String text2Html(String s) {
450                    s = s.trim().replace("\\\"","\"").replace("\\\'","\'");
451                    if (s.indexOf("<br") <= 0 && s.indexOf("<p") <= 0) 
452                            s = s.replace("\n","<br>");
453                    return s;
454            }
455    
456    /**  
457     * ÷ºÎµÈÆÄÀÏ Ã³¸®
458     * @param  mode Àû¿ë¸ðµå [DELETE : ÆÄÀÏ »èÁ¦, MOVE : ÆÄÀÏ ÀÌÀü]
459     * @param  fileName ´ë»ó ÆÄÀϸí
460     * @param  nfileName ÀÌÀüÇÒ À§Ä¡
461     */
462            public void treatUpload(String mode, String fileName, String nfileName) {
463                    if (mode.equals("DELETE")) 
464                            rainUtil.deleteFile(fileName);
465                    else if (mode.equals("MOVE"))
466                            rainUtil.moveFile(fileName, nfileName);
467            }
468    
469    /**  
470     * ÷ºÎµÈÆÄÀÏ Ã³¸®
471     * @param  mode Àû¿ë¸ðµå [DELETE : ÆÄÀÏ »èÁ¦, MOVE : ÆÄÀÏ ÀÌÀü]
472     * @param  fileName ´ë»ó ÆÄÀϸí
473     */
474            public void treatUpload(String mode, String fileName) {
475                    this.treatUpload(mode, fileName, "");
476            }
477    
478    /**  
479     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¸¶Áö¸· ¹®Àå
480     */
481            public String rainContents_html = "";
482    
483    /**  
484     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¸¶Áö¸· ¹®Àå(¿ø¼Ò½º)
485     */
486            public String rainContents_sorce = "";
487    
488    /**  
489     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ ¸ðµå(H: html, T: text, A: ÀÚµ¿¸ðµå)
490     */
491            public String rainContent_type = "";
492    
493    /**  
494     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ Ã·ºÎÆÄÀÏ Á¤º¸
495     */
496            public String[][] rainAttach = null ;
497    
498    /**  
499     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ ´ëÇ¥À̹ÌÁö °æ·Î
500     */
501            public String rainAttach_image = "";
502    
503    /**  
504     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­¿¡ µ¿¿µ»ó ÷ºÎ ¿©ºÎ[N: ÷ºÎ¾ÈµÊ, Y: ÷ºÎµÊ]
505     */
506            public String rainAttach_movie = "N";
507            
508    /**  
509     * ·¹Àο¡µðÅÍ ±¸ºÐÀÚ
510     */
511            private String boundary = "@4d4e81d3f9219886bcadb3dc9b503f82@";
512    
513    /**  
514     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ Àüü ÷ºÎ¿ë·®
515     */
516            public int totalattach_size = 0;
517    
518    /**  
519     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ º»¹®¾È¿¡ Æ÷ÇԵǾî ÀÖÁö ¾ÊÀº ÆÄÀϿ뷮
520     */
521            public int totalattach_unincludesize = 0;
522    
523    /**  
524     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ Á¤¸®
525     * @param  new_contents »õ·Î ÀÛ¼ºµÈ ³»¿ë
526     * @param  old_contents ÀÌÀü ÆÄÀÏ ³»¿ë
527     * @return  Á¤¸®µÈ ¹®¼­ ³»¿ë
528     */
529            public String rainEditAttachSaveDelete(String new_contents, String old_contents) {
530                    String[][] old_attach = null ;
531                    if (! old_contents.equals("")) {
532                            this.rainEditContents(old_contents);
533                            old_contents = this.rainContents_sorce; 
534                            old_attach = this.rainAttach;
535                    }
536                    this.rainEditContents(new_contents);
537                    new_contents = this.rainContents_sorce; 
538                    String[][] new_attach = this.rainAttach;
539                    String new_type = this.rainContent_type;
540    
541                    Boolean delete_file = false;
542                    if (old_attach != null) {
543                            for (int i = 0; i< old_attach.length; i++) {
544                                    try {
545                                            delete_file = true;
546                                            for (int j = 0; j< new_attach.length; j++) {
547                                                    if (old_attach[i][0].equals(new_attach[j][0])) {
548                                                            delete_file = false;
549                                                            break;
550                                                    }
551                                            }
552                                            if (delete_file) 
553                                                    this.treatUpload("DELETE", old_attach[i][0]);
554                                    } catch( Exception e) {}
555                            }
556                    }
557                    String file_str = "";
558                    if (new_attach != null) {
559                            Calendar nowDate = Calendar.getInstance();
560                            String tmp_dir = nowDate.get(Calendar.YEAR)+"/"+(nowDate.get(Calendar.MONTH) +1)+"/"+nowDate.get(Calendar.DATE) + "/";
561                            for(int i = 0; i < new_attach.length ; i++) {
562                                    if (new_attach[i] != null && new_attach[i][0] != null && new_attach[i][4] != null && !new_attach[i][4].equals("")) {
563                                            if (new_attach[i][0].indexOf("junk/") == 0) {
564                                                    String new_key = new_attach[i][0].replaceAll("^junk/", tmp_dir);
565                                                    this.treatUpload("MOVE", new_attach[i][0], new_key);
566                                                    new_contents = new_contents.replaceAll(new_attach[i][0], new_key);
567                                                    new_attach[i][0] = new_key;
568                                            }
569                                            file_str += new_attach[i][0] + "|"+ new_attach[i][1] + "|"+ new_attach[i][2] + "|"+ new_attach[i][3] + "#";
570                                    }
571                            }
572                    }
573                    String contents = new_contents + this.boundary + new_type + "*" + file_str + this.boundary;
574                    this.rainEditContents(contents);
575                    return rainUtil.addslashes(contents);
576            }
577    
578    /**  
579     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ Á¤¸®
580     * @param  new_contents »õ·Î ÀÛ¼ºµÈ ³»¿ë
581     * @return  Á¤¸®µÈ ¹®¼­ ³»¿ë
582     */
583            public String rainEditAttachSaveDelete(String new_contents) {
584                    return this.rainEditAttachSaveDelete(new_contents,"");
585            }
586    
587    /**  
588     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ ÆÄ½Ì
589     * @param  contents ¿¡µðÅÍ ³»¿ë
590     * @param  max_width Ç¥½Ã¿µ¿ª ÃÖ´ëÆø
591     * @return  Á¤¸®µÈ ¹®¼­ ³»¿ë
592     */
593            public String rainEditContents(String contents, int max_width) {
594                    String contents_type = "", contents_file = "";
595                    contents = contents.replace("\\\"","\"").replace("\\\'","\'");
596                    String[] contents_info = contents.split(this.boundary);
597                    if (contents_info.length == 2) {
598                            contents = contents_info[0];
599                            String contents_meta = contents_info[1];
600                            String[] arrcontents_meta = contents_meta.split("\\*");
601                            if (arrcontents_meta.length >= 1) {
602                                    contents_type = arrcontents_meta[0];
603                                    if (arrcontents_meta.length > 1)
604                                            contents_file = arrcontents_meta[1];
605                                    else
606                                            contents_file = "";
607                            } else {
608                                    contents_type = "A";
609                                    contents_file = contents_meta;
610                            }
611                    } else {
612                            contents_type = "A";
613                            contents_file = "";
614                    }
615                    this.rainContents_sorce = contents;
616                    if (contents_type.toLowerCase().equals("a")) 
617                            contents = this.text2Html(contents);
618                    else if (contents_type.toLowerCase().equals("h")) {
619                            contents = contents.replace("&amp;lt;", "&lt;");
620                            contents = contents.replace("&amp;gt;", "&gt;");
621                    } else {
622                            contents = contents.replace("\n","<br>");
623                    } 
624    
625                    String[][] file_list = new String[30][5];
626                    int img_size_max = 0;
627                    this.rainAttach_image = "";
628                    this.totalattach_size = 0;
629                    this.totalattach_unincludesize = 0;
630                    this.rainAttach_movie = "N";
631                    
632                    if (!contents_file.equals("")) {
633                            String[] contents_file_info = contents_file.split("#");
634                            for (int i = 0; i < contents_file_info.length; i++) {
635                                    if (! contents_file_info[i].equals("")) {
636                                            String[] file_info = contents_file_info[i].split("\\|");
637                                            file_list[i] =  new String[]{"","0","","",""};
638                                            if (file_info.length >= 4) {
639                                                    file_list[i] =  new String[]{file_info[0],file_info[1],file_info[2],file_info[3],""};
640                                                    if (this.isImage(file_info[0]) &&  img_size_max < Integer.parseInt(file_info[1])) {
641                                                            this.rainAttach_image = contents_file_info[i];
642                                                            img_size_max = Integer.parseInt(file_info[1]);
643                                                    }
644                                                    if (this.rainAttach_movie.equals("N") && this.isMovie(file_info[2])) 
645                                                            this.rainAttach_movie = "Y";
646                                                    
647                                                    if (contents.indexOf(file_info[0]) >= 0) file_list[i][4] = "true";
648                                                    else {
649                                                            file_list[i][4] = "false";
650                                                            this.totalattach_unincludesize += Integer.parseInt(file_list[i][1]);
651                                                    }
652                                                    this.totalattach_size += Integer.parseInt(file_list[i][1]);
653                                            }
654                                    }
655                            }
656                    }
657    
658    //              if (contents_type.toLowerCase().equals("h") || contents_type.toLowerCase().equals("a")) { 
659                            //contents = contents.replace("<IMG ","<IMG name=rainimg_resize style='cursor:hand' onError=RainImgError(this) onclick=imgView(this) ");
660    //              }
661                    
662                    this.rainContent_type = contents_type.toUpperCase();
663                    this.rainContents_html = contents;
664                    this.rainAttach = file_list;
665                    return contents;
666            }
667    
668    /**  
669     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ ÆÄ½Ì
670     * @param  contents ¿¡µðÅÍ ³»¿ë
671     * @return  Á¤¸®µÈ ¹®¼­ ³»¿ë
672     */
673            public String rainEditContents(String contents) {
674                    return rainEditContents(contents, 0);
675            }
676    
677    /**  
678     * ·¹Àο¡µðÅÍ¿¡¼­ ÀÛ¼ºµÈ ¹®¼­ÀÇ Ã·ºÎÆÄÀÏ ´Ù¿î·Îµå °æ·Î ÆÄ½Ì
679     * @param  show_include º»¹®¾È¿¡ Æ÷ÇÔµÈ ³»¿ëµµ °°ÀÌ ÆÄ½ÌÇÒ°ÍÀÎÁö ¿©ºÎ
680     * @return  ÷ºÎÆÄÀÏ ´Ù¿î·Îµå °æ·Î
681     */
682            public String rainAttachParse(Boolean show_include) {
683                    String return_str = "";
684    
685                    if (this.rainAttach == null) return "";
686                    for(int i = 0; i< rainAttach.length; i++) {
687                            if (rainAttach[i] == null || rainAttach[i][4] == null || rainAttach[i][4].equals("")) {
688                            } else if (show_include  || !rainAttach[i][4].equals("true")) {
689                                    String ext = rainAttach[i][2].toLowerCase();
690                                    String file_icon = "";
691                                    if (ext.equals("gif")) 
692                                            file_icon = "sgif.gif";
693                                    else if (ext.equals("jpg")) 
694                                            file_icon = "sjpg.gif";
695                                    else if (ext.equals("jpeg")) 
696                                            file_icon = "sjpg.gif";
697                                    else if (ext.equals("bmp")) 
698                                            file_icon = "sbmp.gif";
699                                    else if (ext.equals("alz")) 
700                                            file_icon = "salz.gif";
701                                    else if (ext.equals("avi")) 
702                                            file_icon = "savi.gif";
703                                    else if (ext.equals("doc")) 
704                                            file_icon = "sdoc.gif";
705                                    else if (ext.equals("exe")) 
706                                            file_icon = "sexe.gif";
707                                    else if (ext.equals("htm")) 
708                                            file_icon = "shtm.gif";
709                                    else if (ext.equals("html")) 
710                                            file_icon = "shtm.gif";
711                                    else if (ext.equals("hwp")) 
712                                            file_icon = "shwp.gif";
713                                    else if (ext.equals("mdb")) 
714                                            file_icon = "smdb.gif";
715                                    else if (ext.equals("mp3")) 
716                                            file_icon = "smp3.gif";
717                                    else if (ext.equals("pdf")) 
718                                            file_icon = "spdf.gif";
719                                    else if (ext.equals("gif")) 
720                                            file_icon = "sgif.gif";
721                                    else if (ext.equals("ppt")) 
722                                            file_icon = "sppt.gif";
723                                    else if (ext.equals("psd")) 
724                                            file_icon = "spsd.gif";
725                                    else if (ext.equals("tif")) 
726                                            file_icon = "stif.gif";
727                                    else if (ext.equals("txt")) 
728                                            file_icon = "stxt.gif";
729                                    else if (ext.equals("xls")) 
730                                            file_icon = "sxls.gif";
731                                    else if (ext.equals("zip")) 
732                                            file_icon = "szip.gif";
733                                    else if (ext.equals("tar")) 
734                                            file_icon = "star.gif";
735                                    else if (ext.equals("tgz")) 
736                                            file_icon = "stgz.gif";
737                                    else 
738                                            file_icon = "setc.gif";
739                                    return_str += "<img src='" 
740                                            + rainUtil.getConf("url_images") 
741                                            + "ext/" 
742                                            + file_icon 
743                                            + "' align=absmiddle border=0 hspace=1><a href='" 
744                                            + rainUtil.getConf("url_uploads")
745                                            + "download.jsp?fn=" 
746                                            + rainAttach[i][0] 
747                                            + "&dn=" 
748                                            + rainUtil.urlencode(rainAttach[i][3]) 
749                                            + "'>" + rainAttach[i][3] 
750                                            + "(" + this.file_Size(rainUtil.string2Int(rainAttach[i][1])) + ")</a>&nbsp; ";
751                            }
752                    }
753                    return rainUtil.addslashes(return_str);
754            }
755            
756    /**  
757     * ÅÂ±× Á¦°Å±â
758     * @param  text ´ë»ó ¹®ÀÚ
759     * @return  űװ¡ Á¦°ÅµÈ ¹®ÀÚ
760     */
761        public String stripTags(String text) {
762            String patternStr;
763             patternStr = "<[\\s\\S]+?>";
764            java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(patternStr);
765            java.util.regex.Matcher matcher = pattern.matcher(text);
766            return rainUtil.addslashes(matcher.replaceAll(""));
767        }
768    
769    }