001    
002    package com.rain;
003    
004    import java.io.*;
005    import java.net.*;
006    import javax.servlet.http.*;
007    import java.lang.*;
008    import java.sql.*;
009    import java.util.Date;
010    import java.util.Calendar;
011    import java.util.Vector;
012    import java.util.regex.Pattern;
013    import java.util.Random;
014    import java.text.*;
015    
016    /**  
017     * À¯Æ¿¸®Æ¼ - ÇØ´ç °´Ã¼´Â Á¤ÀûÀ̱â´Â Çϳª ÀϺΠ±â´ÉÀº rainRequest ¿¡¼­ »ó¼ÓÀ» ¹Þ°í ÀÖÀ½ 
018     * »ç¿ëÀü ÇÊÈ÷ rainRequest ¸¦ ¸ÕÀú ¼±¾ðÇØ¾ßÇÔ
019     * @version 1.0
020     * @author http://cafen.net (outmind@cafen.net)
021     */
022    public class rainUtil{
023    
024    /**  
025     * ±âº» À§Ä¡ - rainRequest ¿¡¼­ Àü´Þ ¹ÞÀ½
026     */
027            public static String base_pos;
028    
029    /**  
030     * »çÀÌÆ® ȯ°æ ÆÄÀÏ - rainRequest ¿¡¼­ Àü´Þ ¹ÞÀ½
031     */
032            public static rainProperties site_conf;
033    
034    /**  
035     * request °´Ã¼  - rainRequest ¿¡¼­ Àü´Þ ¹ÞÀ½
036     */
037            public static rainRequest request;
038    
039    /**  
040     * ¿À´ÃÀÇ unixtimestamp rainUtil.getNow() ÀÇ ÃÖÃÊ È£Ãâ ½ÃÁ¡¿¡¼­ ÃʱâÈ­µÊ
041     */
042            public static int today_unixtime = 0;
043    
044    /**
045     * urlencode 
046     * @param  str ´ë»ó ¹®ÀÚ
047     * @return  encode µÈ ¹®ÀÚ
048     */
049            public static String urlencode(String str) {
050                    String rtn = null;
051                    try {
052                            rtn = (str==null)?"":java.net.URLEncoder.encode(str, "KSC5601");
053                    } catch (java.io.UnsupportedEncodingException e) {}
054                    return rtn;
055            }
056    
057    /**
058     * base64_getMap1
059     * @return  map1
060     */
061            public static char[] base64_getMap1() {
062                    char[] map1 = new char[64];
063                    int i=0;
064                    for (char c='A'; c<='Z'; c++) map1[i++] = c;
065                    for (char c='a'; c<='z'; c++) map1[i++] = c;
066                    for (char c='0'; c<='9'; c++) map1[i++] = c;
067                    map1[i++] = '+'; map1[i++] = '/'; 
068                    return map1;            
069            }
070    
071    /**
072     * base64_getMap2
073     * @return  map2
074     */
075            public static byte[] base64_getMap2() {
076                    char[] map1 = rainUtil.base64_getMap1();
077                    byte[] map2 = new byte[128];
078                    for (int i=0; i<map2.length; i++) map2[i] = -1;
079                    for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; 
080                    return map2;
081            }
082    
083    /**
084     * base64_encode 
085     * @param  str ´ë»ó ¹®ÀÚ
086     * @return  encode µÈ ¹®ÀÚ
087     */
088            public static String base64_encode(String str) {
089                    byte[] b = str.getBytes();
090                    return rainUtil.base64_encode(b,b.length);
091            }
092    
093    /**
094     * base64_encode 
095     * @param  in ´ë»ó ¹®ÀÚ
096     * @param  iLen ±æÀÌ
097     * @return  encode µÈ ¹®ÀÚ
098     */
099            public static String base64_encode(byte[] in, int iLen) {
100                    char[] map1 = rainUtil.base64_getMap1();
101                    int oDataLen = (iLen*4+2)/3;
102                    int oLen = ((iLen+2)/3)*4;
103                    char[] out = new char[oLen];
104                    int ip = 0;
105                    int op = 0;
106                    while (ip < iLen) {
107                            int i0 = in[ip++] & 0xff;
108                            int i1 = ip < iLen ? in[ip++] & 0xff : 0;
109                            int i2 = ip < iLen ? in[ip++] & 0xff : 0;
110                            int o0 = i0 >>> 2;
111                            int o1 = ((i0 &   3) << 4) | (i1 >>> 4);
112                            int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
113                            int o3 = i2 & 0x3F;
114                            out[op++] = map1[o0];
115                            out[op++] = map1[o1];
116                            out[op] = op < oDataLen ? map1[o2] : '='; op++;
117                            out[op] = op < oDataLen ? map1[o3] : '='; op++;
118                    }
119                    return new String(out);
120       }
121    
122    /**
123     * base64_decode 
124     * @param  str ´ë»ó ¹®ÀÚ
125     * @return  encode µÈ ¹®ÀÚ
126     */
127            public static String base64_decode(String str) {
128               return rainUtil.base64_decode(str.toCharArray());
129            }
130    
131    /**
132     * base64_decode 
133     * @param  in ´ë»ó ¹®ÀÚ
134     * @return  encode µÈ ¹®ÀÚ
135     */
136            public static String base64_decode(char[] in) {
137                    byte[] map2 = rainUtil.base64_getMap2();
138                    int iLen = in.length;
139                    if (iLen%4 != 0) 
140                            return "";
141                    while (iLen > 0 && in[iLen-1] == '=') 
142                            iLen--;
143                    int oLen = (iLen*3) / 4;
144                    byte[] out = new byte[oLen];
145                    int ip = 0;
146                    int op = 0;
147                    while (ip < iLen) {
148                            int i0 = in[ip++];
149                            int i1 = in[ip++];
150                            int i2 = ip < iLen ? in[ip++] : 'A';
151                            int i3 = ip < iLen ? in[ip++] : 'A';
152                            if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
153                                    return "";
154                            int b0 = map2[i0];
155                            int b1 = map2[i1];
156                            int b2 = map2[i2];
157                            int b3 = map2[i3];
158                            if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
159                                    return "";
160                            int o0 = ( b0       <<2) | (b1>>>4);
161                            int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
162                            int o2 = ((b2 &   3)<<6) |  b3;
163                            out[op++] = (byte)o0;
164                            if (op<oLen) out[op++] = (byte)o1;
165                            if (op<oLen) out[op++] = (byte)o2; 
166                    }
167                    return new String(out); 
168            }
169    /**
170     * ÆäÀÌÁö Àüȯ
171     * @param  response response °´Ã¼
172     * @param  theURL ¿¬°á URL
173     */
174            public static void redirect(HttpServletResponse response, String theURL) {
175                    try {
176                            response.sendRedirect(theURL);
177                    } catch (Exception e) { }
178            }
179    
180    /**
181     * ÆäÀÌÁö Àüȯ
182     * @param  theURL ¿¬°á URL
183     * @param  msg ¸Þ¼¼Áö
184     * @return  ÀÚ¹Ù½ºÅ©¸³ÅÍ
185     */
186            public static String redirect(String theURL, String msg) {
187                    String html = "";
188                    html = "<script>";
189                    if (!msg.equals("")) html += "alert('"+msg+"');";
190                    if (!theURL.equals("")) html += "document.location.href='"+theURL+"';";
191                    else html += " history.back(); ";
192                    html += "</script>";
193                    return html;
194            }
195    
196    /**
197     * ÆäÀÌÁö Àüȯ
198     * @param  theURL ¿¬°á URL
199     * @return  ÀÚ¹Ù½ºÅ©¸³ÅÍ
200     */
201            public static String redirect(String theURL) {
202                    return rainUtil.redirect(theURL, "");
203            }
204    
205    /**
206     * Shell ¸í·É¾î ½ÇÇà
207     * @param  cmd ¸í·É¾î
208     * @return  ½ÇÇà°á°ú
209     */
210            public static String cellCmd(String cmd) {
211                    Runtime rt = Runtime.getRuntime();
212                    String cmd_result = "";
213                    try {
214                            rainUtil.errorLog(cmd);
215                            Process proc = rt.exec(cmd);
216                            InputStream stderr = proc.getErrorStream(); 
217                            InputStreamReader isr = new InputStreamReader(stderr); 
218                            BufferedReader br = new BufferedReader(isr); 
219                            String line = null; 
220                            while ( (line = br.readLine()) != null) 
221                                    cmd_result += line + " ";
222                            int exitVal = proc.waitFor(); 
223                            rainUtil.errorLog(cmd_result);
224                            stderr.close();
225                    } catch (Exception e) { 
226                            rainUtil.errorLog(e.getMessage());
227                    }
228                    return cmd_result;
229            }
230    
231    /**
232     * ÆÄÀÏ»èÁ¦
233     * @param  fileName »èÁ¦ÇÒ ÆÄÀϸí
234     */
235            public static void deleteFile(String fileName) {
236                    String domain_uploads = rainUtil.getConf("domain_uploads");
237                    if (domain_uploads.equals("")) {
238                            rainFileManager fm = new rainFileManager(fileName);
239                            fm.delete();
240                    } else {
241                            rainUtil.openURL(domain_uploads + "?mode=DELETE&ft="+fileName);
242                    }
243            }
244    
245    /**
246     * ÆÄÀÏÀÌÀü
247     * @param  fileName ÀÌÀüÇÒ ÆÄÀϸí 
248     * @param  nfileName »õ·Î¿î°æ·Î ÆÄÀϸí 
249     */
250            public static void moveFile(String fileName, String nfileName) {
251                    String domain_uploads = rainUtil.getConf("domain_uploads");
252                    if (domain_uploads.equals("")) {
253                            rainFileManager fm = new rainFileManager(fileName);
254                            fm.move(nfileName);
255                    } else {
256                            rainUtil.openURL(domain_uploads + "?mode=MOVE&ft="+fileName + "&fn=" + nfileName);
257                    }
258            }
259    
260    /**
261     * À¥°æ·Î ¿­±â
262     * @param  remoteName À¥°æ·Î¸í
263     * @return  ¹ÝȯµÈ ¹®Àå
264     */
265            public static String openURL(String remoteName) {
266                    String ret_val = "";
267                    try {
268                            URL theURL = new URL(remoteName);
269                            InputStream input = theURL.openStream();
270                            int line = -1;
271                            byte b[] = new byte[1024];
272                            while((line = input.read(b)) != -1) {
273                                    String newCommands = new String(b, 0, line);
274                            ret_val += newCommands;
275                            }
276                            input.close();
277                            
278                    } catch(Exception e) {}
279                    return ret_val;
280            }
281    
282    /**
283     * OS Unix ¿©ºÎ ÆÇ´Ü
284     * @return  Unix ¿©ºÎ
285     */
286            public static Boolean isUnix() {
287                    return (System.getProperty("os.name").indexOf("Windows") < 0)?true:false;
288            }
289    
290    /**
291     * ÇØ´ç ÆÄÀÏÀÌ Ä³½¬ÆÄÀÏÀÎÁö È®ÀÎ
292     * @return  ij½¬ÆÄÀÏ ¿©ºÎ
293     */
294            public static Boolean isCached(String fileName) {
295                    return rainUtil.find("^(thumb/|movie/)", fileName);
296            }
297    
298    /**
299     * µ¿¿µ»óÀ̳ª À½¿øÆÄÀÏÀÎÁö È®ÀÎ
300     * @return  µ¿¿µ»ó ¿©ºÎ
301     */
302            public static Boolean isMovie(String fileName) {
303                    return rainUtil.find("(asf|mp3|pmeg|pmg|avi|wma|wmv)$", fileName.toLowerCase());
304            }
305    
306    /**
307     * À̹ÌÁöÆÄÀÏÀÎÁö È®ÀÎ
308     * @return  À̹ÌÁö ¿©ºÎ
309     */
310            public static Boolean isImage(String fileName) {
311                    return rainUtil.find("(gif|jpeg|jpg|png)$", fileName.toLowerCase());
312            }
313    
314    /**
315     * ½æ³×ÀÏ À̹ÌÁö °¡Á®¿À±â
316     * @param  fileName ´ë»ó À̹ÌÁö ÆÄÀϸí
317     * @param  max_size ÀåÃà°ª
318     * @return  ½æ³×ÀÏ À̹ÌÁö °æ·Î
319     */
320            public static String getThumbNail(String fileName, int max_size) {
321                    String[] fileInfo = fileName.split("\\|");
322                    if (fileInfo.length > 0 && !fileInfo[0].equals("")) 
323                            return rainUtil.getConf("url_uploads") + "thumb/" + fileInfo[0] + "_M" + max_size + ".png";
324                    else
325                            return "";
326            }
327            
328    /**
329     * ¹®ÀÚ ÀÚ¸£±â
330     * @param  s ´ë»ó ¹®ÀÚ
331     * @param  l ÀÚ¸¦ ¹®ÀÚÅ©±â
332     * @return  ÀÚ¸¥¹®ÀÚ
333     */
334            public static String longCut(String s, int l) {
335                    if (s == null) 
336                            return "";
337                    else if (s.length() > l) 
338                            return s.substring(0, l) + "...";
339                    else 
340                            return s;
341            }
342    
343    /**
344     * Æúµå ÀÚµ¿ »ý¼º
345     * @param  base_root ±âº» °æ·Î
346     * @param  dirName ÇϺΠ°æ·Î
347     * @return  »ý¼º ¼º°ø¿©ºÎ
348     */
349            public static Boolean checkDir(String base_root, String dirName) {
350                    if (dirName.equals("")) return true;
351                    dirName = (new File(dirName + "/")).getParent();
352                    if (dirName.equals("")) return true;
353                    if ((new File(base_root + dirName)).exists()) return true;
354                    String[] folder_info = dirName.split(Pattern.quote(File.separator));
355                    String curr_dir = base_root;
356                    try {
357                            for (int i = 0; i < folder_info.length; i++) {
358                                    if (! folder_info[i].equals("")) {
359                                            curr_dir += folder_info[i] + "/";
360                                            File curr_file = new File(curr_dir);
361                                            if (! curr_file.exists()) curr_file.mkdirs();
362                                    }
363                            }
364                            return true;
365                    } catch(Exception e) {
366                            return false;
367                    }
368            }
369    
370    /**
371     * ÆÄÀÏÀ̸§ º¯°æ
372     * @param  sor_fileName ´ë»ó ÆÄÀϸí
373     * @param  tar_fileName º¯°æ ÆÄÀϸí
374     * @return  º¯°æ ¼º°ø¿©ºÎ
375    */
376            public static Boolean rename(String sor_fileName, String tar_fileName) {
377                    if (rainUtil.file_exists(sor_fileName) && !rainUtil.file_exists(tar_fileName)) {
378                            String base_dir = rainUtil.getConf("path_uploads");
379                            try {
380                                    if (rainUtil.checkDir(base_dir, tar_fileName)) {
381                                            (new File(base_dir + sor_fileName)).renameTo(new File(base_dir + tar_fileName));
382                                            return true;
383                                    } else
384                                            return false;
385                            } catch(Exception e) {
386                                    rainUtil.errorLog(e.getMessage() + "\r\n" + base_dir + sor_fileName + "\r\n" + base_dir + tar_fileName);
387                                    return false;
388                            }
389                    } else
390                            return false;
391            }
392    
393    /**
394     * ÆÄÀϺ¹»ç
395     * @param  sor_fileName ´ë»ó ÆÄÀϸí
396     * @param  tar_fileName º¹»ç ÆÄÀϸí
397     * @return  º¹»ç ¼º°ø¿©ºÎ
398    */
399            public static Boolean copy(String sor_fileName, String tar_fileName) {
400                    if (rainUtil.file_exists(sor_fileName) && !rainUtil.file_exists(tar_fileName)) {
401                            String base_dir = rainUtil.getConf("path_uploads");
402                            if (rainUtil.checkDir(base_dir, tar_fileName)) {
403                                    FileInputStream from = null;
404                                    FileOutputStream to = null;
405                                    try {
406                                            from = new FileInputStream(new File(base_dir + sor_fileName));
407                                            to = new FileOutputStream(new File(base_dir + tar_fileName));
408                                            byte[] buffer = new byte[4096];
409                                            int bytesRead;
410                                            while ((bytesRead = from.read(buffer)) != -1)
411                                                    to.write(buffer, 0, bytesRead);
412                                            from.close();
413                                            to.close();
414                                            return true;
415                                    } catch (Exception e) {
416                                            return false;
417                                    }
418                            } else
419                                    return false;
420                    } else
421                            return false;
422            }
423    
424    /**
425     * ÆÄÀÏ»èÁ¦
426     * @param  fileName ´ë»ó ÆÄÀϸí
427     * @return  »èÁ¦ ¼º°ø¿©ºÎ
428    */
429            public static Boolean unlink(String fileName) {
430                    if (rainUtil.file_exists(fileName)) {
431                            try {
432                                    (new File(rainUtil.getConf("path_uploads") + fileName)).delete();
433                                    return true;
434                            } catch(Exception e) {
435                                    return false;   
436                            }
437                    } else
438                            return false;
439            }
440    
441    /**
442     * ÆÄÀÏÁ¸Àç¿©ºÎ
443     * @param  fileName ´ë»ó ÆÄÀϸí
444     * @return  »èÁ¦ ¼º°ø¿©ºÎ
445    */
446            public static Boolean file_exists(String fileName) {
447                    return (new File(rainUtil.getConf("path_uploads")+fileName)).exists();
448            }
449    
450            /**
451             * Æú´õ ºñ¿ì±â
452             * @param  dirName ´ë»ó Æú´õ¸í
453            */
454            public static void dir_clear(String dirName , int gc_rate) {
455                    if ((new Random()).nextInt(gc_rate) == 0) {
456                            String realName = rainUtil.getConf("path_uploads")  + dirName;
457                            int base_time = rainUtil.getNow() - 60*60*2;
458                            File[] filelist =(new File(realName).listFiles());
459                            for(int i =0; i < filelist.length; i++) {
460                                    if (!filelist[i].getName().equals(".") && !filelist[i].getName().equals("..")) {
461                                            if (filelist[i].isFile() && Math.round(filelist[i].lastModified()/(long)1000) < base_time) {
462                                                    rainFileManager junkmanager = new rainFileManager(dirName +"/" + filelist[i].getName());
463                                                    junkmanager.delete(true);
464                                            } else if (filelist[i].isDirectory()) {
465                                                    rainUtil.dir_clear(dirName + "/" + filelist[i].getName(), gc_rate);     
466                                            }
467                                    }
468                            }
469                    }
470            }
471    
472    /**
473     * ½½·¡½¬ ³Ö±â
474     * @param  str ´ë»ó ¹®ÀÚ
475     * @return  ´ë»ó ¹®ÀÚ
476     */
477            public static String addslashes(String str) {
478                    return str.replace("\\", "\\\\").replace("'", "\\'");
479            }
480    
481    /**
482     * Äõ¸®¿ë ½½·¡½¬ ³Ö±â 
483     * @param  str ´ë»ó ¹®ÀÚ
484     * @return  ´ë»ó ¹®ÀÚ
485     */
486            public static String addslashesQuery(String str) {
487                    return rainUtil.addslashes(rainUtil.addslashes(str));
488            }
489    
490    
491    /**
492     * ½½·¡½¬ Á¦°Å
493     * @param  str ´ë»ó ¹®ÀÚ
494     * @return  ´ë»ó ¹®ÀÚ
495     */
496            public static String stripslashes(String str) {
497                    return str;
498            }
499    
500    /**
501     * HTML À§Çè ¹®ÀÚ º¯È¯
502     * @param  str ´ë»ó ¹®ÀÚ
503     * @return  ´ë»ó ¹®ÀÚ
504     */
505            public static String htmlSpecialchars(String str) {
506                    StringBuffer sb = new StringBuffer();
507                    for(int i=0; i<str.length(); i++) {
508                            char c = str.charAt(i);
509                            switch (c) {
510                                    case '<' : 
511                                            sb.append("&lt;");
512                                            break;
513                                    case '>' : 
514                                            sb.append("&gt;");
515                                            break;
516                                    case '&' :
517                                            sb.append("&amp;");
518                                            break;
519                                    case '"' :
520                                            sb.append("&quot;");
521                                            break;
522                                    case '\'' :
523                                            sb.append("&#039;");
524                                            break;
525                                    default:
526                                            sb.append(c);
527                                            break;
528                            }
529                    }
530                    return rainUtil.addslashes(sb.toString());
531            }
532    
533    /**
534     * NL À» BR ű׷Πº¯È¯
535     * @param  text ´ë»ó ¹®ÀÚ
536     * @return  ´ë»ó ¹®ÀÚ
537     */
538            public static String nl2br(String text) {
539                    return rainUtil.replace("\r\n", "<br/>",text);
540            }
541            
542    /**
543     * ASC ¹®ÀÚ¸¦ EUC-KR ·Î º¯È¯
544     * @param  str ´ë»ó ¹®ÀÚ
545     * @return  ´ë»ó ¹®ÀÚ
546     */
547            public static String a2k(String str) {
548                    String rtn = null;
549                    try {
550                            rtn = (str==null)?"":new String(str.getBytes("8859_1"),"euc-kr");
551                    } catch (java.io.UnsupportedEncodingException e) {}
552                    return rtn;
553            }
554    
555    /**
556     * EUC-KR ¹®ÀÚ¸¦ ASC ·Î º¯È¯
557     * @param  str ´ë»ó ¹®ÀÚ
558     * @return  ´ë»ó ¹®ÀÚ
559     */
560            public static String k2a(String str) {
561                    String rtn = null;
562                    try {
563                            rtn = (str==null)?"":new String(str.getBytes("euc-kr"),"8859_1");
564                    } catch (java.io.UnsupportedEncodingException e) {}
565                    return rtn;
566            }
567    
568    /**
569     * Date ¸¦ ƯÁ¤ Æ÷¸ËÀ¸·Î º¯È¯
570     * @param  today ´ë»ó ÀÏÀÚ
571     * @param  date_fmat ÀÏÀÚ Æ÷¸Ë
572     * @return  Æ÷¸Ë ¹®ÀÚ
573     */
574            public static String getDateFormat(Date today, String date_fmat) {
575                    SimpleDateFormat formatter = new SimpleDateFormat(date_fmat);
576                    return formatter.format(today);
577            }
578    
579    /**
580     * ¹®ÀÚ¸¦ Date ·Î º¯È¯
581     * @param  date_str ´ë»ó ÀÏÀÚ¹®ÀÚ
582     * @return  Calendar
583     */
584            public static Calendar getStr2Date(String date_str) {
585                    Calendar todaycal = Calendar.getInstance();
586                    int date_Y = 1970;      int date_M = 1;         int date_D = 1; int date_hh = 0; int date_mm = 0; int date_ss = 0;
587                    String date_ymd[] = {"","",""};
588                    String date_hms[] = {"","",""};
589                    String ret_date = "";
590                    try {
591                            String date_info[] = date_str.split(" ");
592                            if (date_info[0].indexOf("/") > 0) {
593                                    date_ymd = date_info[0].split("/");
594                            } else if (date_info[0].indexOf("-") > 0) {
595                                    date_ymd = date_info[0].split("-");
596                            } else {
597                                    if (date_info[0].length() >= 8) {
598                                            date_ymd[0] = date_info[0].substring(0,4);
599                                            date_ymd[1] = date_info[0].substring(4,6);
600                                            date_ymd[2] = date_info[0].substring(6,8);
601                                    } else if (date_info[0].length() >= 6) {
602                                            date_ymd[0] = date_info[0].substring(0,2);
603                                            date_ymd[1] = date_info[0].substring(2,4);
604                                            date_ymd[2] = date_info[0].substring(4,6);
605                                    } else if (date_info[0].length() >= 4) {
606                                            date_ymd[0] = date_info[0].substring(0,2);
607                                            date_ymd[1] = date_info[0].substring(2,4);
608                                            date_ymd[2] = "01";
609                                    } else {
610                                            date_ymd[0] = ""; date_ymd[1] = ""; date_ymd[2] = "";
611                                    }
612                            }
613                            if (date_ymd[0] == null || date_ymd[0].equals("")) date_Y = 1970;
614                            else if (Integer.parseInt(date_ymd[0]) > 100) date_Y = Integer.parseInt(date_ymd[0]);
615                            else if (Integer.parseInt(date_ymd[0]) > 70)  date_Y = Integer.parseInt(date_ymd[0]) + 1900;
616                            else date_Y = Integer.parseInt(date_ymd[0]) + 2000;
617                            date_M = (date_ymd.length < 2 || date_ymd[1].equals(""))?1:Integer.parseInt(date_ymd[1]);
618                            date_D = (date_ymd.length < 3 || date_ymd[2].equals(""))?1:Integer.parseInt(date_ymd[2]);
619                    
620                            if (date_info.length > 1 && !date_info[1].equals("")) {
621                                    date_hms = date_info[1].split(":");
622                                    date_hh = (date_hms.length < 1 || date_hms[0].equals(""))?1:Integer.parseInt(date_hms[0]);
623                                    date_mm = (date_hms.length < 2 || date_hms[1].equals(""))?1:Integer.parseInt(date_hms[1]);
624                                    date_ss = (date_hms.length < 3 ||  date_hms[2].equals(""))?1:Math.round(Float.parseFloat(date_hms[2]));
625                            }
626                            todaycal.set(date_Y,date_M - 1,date_D,date_hh,date_mm,date_ss);
627    
628                    }catch (Exception e){ }
629    
630                    return todaycal;
631    
632            }
633    
634    /**
635     * ¹®ÀÚÀÏÀÚ¸¦ ƯÁ¤Æ÷¸ËÀ¸·Î º¯È¯
636     * @param  date_str ´ë»ó ÀÏÀÚ¹®ÀÚ
637     * @param  date_fmat ´ë»ó ÀÏÀÚ Æ÷¸Ë
638     * @return  ÀÏÀÚ Æ÷¸Ë
639     */
640            public static String getDate(String date_str, String date_fmat) {
641                    return getDateFormat(getStr2Date(date_str).getTime() ,date_fmat);
642            }
643    
644    /**
645     * ¿À´Ã ÀÏÀÚ¸¦ ƯÁ¤ ÇöÅÂÀÇ Æ÷¸ËÀ¸·Î °¡Á®¿À±â
646     * @param  date_fmat ´ë»ó ÀÏÀÚ Æ÷¸Ë
647     * @return  ÀÏÀÚ Æ÷¸Ë
648     */
649            public static String getDate(String date_fmat) {
650                    Date today = new Date();
651                    return getDateFormat(today,date_fmat);
652            }
653    
654    /**
655     * Unix ŸÀÓÀÇ ¹®ÀÚ¸¦ ƯÁ¤ ÇüÅÂÀÇ Æ÷¸ËÀ¸·Î °¡Á®¿À±â
656     * @param  date_str ´ë»ó ÀÏÀÚ¹®ÀÚ
657     * @param  date_fmat ´ë»ó ÀÏÀÚ Æ÷¸Ë
658     * @return  ÀÏÀÚ Æ÷¸Ë
659     */
660            public static String getUDate(String date_str, String date_fmat) {
661                    return getUDate(Integer.parseInt(date_str),date_fmat);
662            }
663    
664    /**
665     * Unix ŸÀÓÀÇ ¼ýÀÚ¸¦ ƯÁ¤ ÇüÅÂÀÇ Æ÷¸ËÀ¸·Î °¡Á®¿À±â
666     * @param  date_str ´ë»ó ÀÏÀÚ¹®ÀÚ
667     * @param  date_fmat ´ë»ó ÀÏÀÚ Æ÷¸Ë
668     * @return  ÀÏÀÚ Æ÷¸Ë
669     */
670            public static String getUDate(int date_str, String date_fmat) {
671                    Date today = new Date((long) date_str * 1000);
672                    return getDateFormat(today,date_fmat);
673            }
674    
675    /**
676     * Date¸¦  ƯÁ¤ ÇüÅÂÀÇ Æ÷¸ËÀ¸·Î °¡Á®¿À±â
677     * @param  date_time ´ë»ó ÀÏÀÚ¹®ÀÚ
678     * @param  date_fmat ´ë»ó ÀÏÀÚ Æ÷¸Ë
679     * @return  ÀÏÀÚ Æ÷¸Ë
680     */
681            public static String getDate(Date date_time, String date_fmat) {
682                    return getDateFormat(date_time,date_fmat);
683            }
684            
685    /**
686     * ¼ýÀÚ¿¡ , ³Ö±â
687     * @param  mn ¼ýÀÚ
688     * @return  , ¼ýÀÚ
689     */
690            public static String getNumberFormat(Float mn) {
691                    if (mn <= 0) return "-";
692                    else return java.text.NumberFormat.getInstance().format(mn); 
693            }
694    
695    /**
696     * ¼ýÀÚ¿¡ , ³Ö±â
697     * @param  mn ¼ýÀÚ
698     * @return  , ¼ýÀÚ
699     */
700            public static String getNumberFormat(String mn) {
701                    Float f_nm = new Float(0.0);
702                    try {
703                            f_nm = Float.parseFloat(mn);
704                    }catch (Exception e){ }
705                    return getNumberFormat(f_nm);
706            }
707    
708    /**
709     * ¼ýÀÚ¿¡ , ³Ö±â
710     * @param  mn ¼ýÀÚ
711     * @return  , ¼ýÀÚ
712     */
713            public static String getNumberFormat(int mn) {
714                    return java.text.NumberFormat.getInstance().format(mn); 
715            }       
716    
717    /**
718     * ÇöÀç URL °¡Á®¿À±â
719     * @param  request HttpServletRequest
720     * @return  URL
721     */
722            public static String getSelfURL(HttpServletRequest request) {
723                    String selfUrl = "";
724                    selfUrl = request.getRequestURI();
725                    if (request.getQueryString() !=null && !request.getQueryString().equals("")) selfUrl += "?"+request.getQueryString();
726                    return selfUrl;
727            }
728            
729    /**
730     * ÄíŰ ¼³Á¤
731     * @param  response HttpServletResponse
732     * @param  name À̸§
733     * @param  value °ª
734     */
735            public static void setCookie(HttpServletResponse response, String name, String value) {
736                    value = java.net.URLEncoder.encode(value);
737                    Cookie cookie = new Cookie(name, value);
738                    cookie.setMaxAge(60*60*24*15);
739                    response.addCookie(cookie);
740            }
741            
742    /**
743     * ÄíŰ °¡Á®¿À±â
744     * @param  request HttpServletRequest
745     * @param  cookieName À̸§
746     * @return  ÄíŰ °ª
747     */
748            public static String getCookie(HttpServletRequest request, String cookieName) {
749                    Cookie [] cookies = request.getCookies();
750                    String value = "";
751                    for(int i=0;i<cookies.length;i++) {
752                            if(cookieName.equals(cookies[i].getName())) {
753                                    value = java.net.URLDecoder.decode(cookies[i].getValue());
754                                    break;
755                            }
756                    }
757                    return value;
758            }
759    
760    /**
761     * ÃÖ±Ù ¿©ºÎ È®ÀÎ
762     * @param  date_from unixtimestamp
763     * @param  new_date ÃÖ±Ù ÀÏ ±âÁØÀÏ (1 : 24½Ã°£)
764     * @return  ÃÖ±Ù ¿©ºÎ
765     */
766            public static Boolean isDateNew(long date_from, int new_date) {
767                    Date today1 = new Date();
768                    Date today2 = new Date(date_from * 1000);
769                    if ((today1.getTime() - today2.getTime()) <= new_date * 86400000 ) return true;
770                    else return false;
771            }
772    
773    /**
774     * ÃÖ±Ù ¾ÆÀÌÄÜ °¡Á®¿À±â
775     * @param  base_date unixtimestamp
776     * @return  ÃÖ±Ù±Û ¾ÆÀÌÄÜ html ¼Ò½º¸¦ ¹Ýȯ
777     */
778            public static String getNewIcon(int base_date) {
779                    if (base_date > rainUtil.getNow() - 60*60*24)
780                            return "<img src='"+rainUtil.getConf("url_images")+"ico_n.gif'  align=texttop hspace=3 border=0>";
781                    else
782                            return "";
783            }
784    
785    /**
786     * ÷ºÎÆÄÀÏ ¾ÆÀÌÄÜ ¹Ýȯ
787     * @param  attach_size ÷ºÎÆÄÀÏ Å©±â
788     * @param  attach_movie µ¿¿µ»ó ÷ºÎ¿©ºÎ(Y:÷ºÎ, N: ÷ºÎ¾ÈµÊ)
789     * @return  ÷ºÎ °ü·Ã html ¼Ò½º ¹Ýȯ
790     */
791            public static String attachIcon(int attach_size, String attach_movie) {
792                    String html = "";
793                    if (attach_size > 0)
794                            html += "<img src='"+rainUtil.getConf("url_images")+"ico_file.gif'  align=texttop hspace=3 border=0>";
795                    if (attach_movie.equals('Y'))
796                            html += "<img src='"+rainUtil.getConf("url_images")+"ico_movie.gif'  align=texttop hspace=3 border=0>";
797                    return html;
798            }
799            
800    /**
801     * ÇöÀç ½Ã°£ÀÇ unixtimestamp °¡Á®¿À±â
802     * @return  unixtimestamp
803     */
804            public static int getNow() {
805                    if (rainUtil.today_unixtime == 0) {
806                            Date today = new Date();
807                            return rainUtil.today_unixtime = (int) Math.floor(today.getTime()/1000);
808                    } else
809                            return rainUtil.today_unixtime;
810            }
811    
812    /**
813     * ¹®ÀÚ¿­ ¹Ýº¹
814     * @param  str ´ë»ó ¹®ÀÚ
815     * @param  cnt ¹Ýº¹È½¼ö
816     * @return  ¹Ýº¹µÈ ¹®ÀÚ
817     */
818            public static String repeat(String str, int cnt) {
819                    for(int i = 0; i < cnt; i++)
820                            str += str;
821                    return str;
822            }
823    
824    /**
825     * ¹®ÀÚ¿­ ġȯ
826     * @param  patternStr Àû¿ëÁ¤±ÔÇ¥Çö½Ä
827     * @param  replaceStr ġȯ
828     * @param  text Àû¿ë¹®ÀÚ
829     * @return  ġȯµÈ¹®ÀÚ
830     */
831            public static String replace(String patternStr, String replaceStr, String text) {
832            java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(patternStr);
833            java.util.regex.Matcher matcher = pattern.matcher(text);
834            return matcher.replaceAll(replaceStr);
835            }
836            
837    /**
838     * ¹®ÀÚ¿­ °Ë»ö
839     * @param  patternStr Àû¿ëÁ¤±ÔÇ¥Çö½Ä
840     * @param  text Àû¿ë¹®ÀÚ
841     * @param  find_str ¹ß°ßµÈ Á¤±ÔÇ¥Çö½ÄÀÇ group À» ÇØ´ç °ª¿¡ ³Ö¾îµÒ
842     * @return  ¹ß°ß¿©ºÎ
843     */
844            public static Boolean find(String patternStr, String text, Vector find_str) {
845            java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(patternStr);
846            java.util.regex.Matcher matcher = pattern.matcher(text);
847            if (matcher.find()) {
848                    for(int i = 0 ; i <= matcher.groupCount(); i++) 
849                            find_str.add(i,matcher.group(i));
850                    return true;
851                } else
852                    return false;
853            }
854            
855    /**
856     * ¹®ÀÚ¿­ °Ë»ö
857     * @param  patternStr Àû¿ëÁ¤±ÔÇ¥Çö½Ä
858     * @param  text Àû¿ë¹®ÀÚ
859     * @return  ¹ß°ß¿©ºÎ
860     */
861            public static Boolean find(String patternStr, String text) {
862                    Vector reg = new Vector();
863                    return rainUtil.find(patternStr, text, reg);
864            }
865    
866    /**
867     * ¹®ÀÚ¿­À» ¼ýÀÚ·Î °­Á¦ º¯È¯(º¯È¯¿À·ù½Ã 0 ÀÌ ¹ÝȯµÊ)
868     * @param  str ¹®ÀÚ¿­
869     * @return  º¯È¯µÈ ¼ýÀÚ
870     */
871            public static int string2Int(String str) {
872                    try {
873                            return Integer.parseInt(str);
874                    } catch (Exception e) {
875                            return 0;
876                    }
877            }
878    
879    /**
880     * ȸ¿ø Á¤º¸¸¦ ¼³Á¤ÇÔ(ÇØ´ç ÇÔ¼ö´Â rainRequest ¸¦ ¸ÕÀú È£ÃâÇØ¾ß¸¸ »ç¿ë°¡´ÉÇÔ)
881     * @param  user_id  ȸ¿ø¾ÆÀ̵ð
882     * @param  user_nm  ȸ¿øÀ̸§
883     * @param  user_email  ȸ¿ø¸ÞÀÏÁÖ¼Ò
884     */
885            public static void setUser(String user_id, String user_nm, String user_email) {
886                    rainUtil.request.setUser(user_id, user_nm, user_email);
887            }
888    
889    /**
890     * ·Î±×ÀÎ ¿©ºÎ°¡Á®¿À±â (ÇØ´ç ÇÔ¼ö´Â rainRequest ¸¦ ¸ÕÀú È£ÃâÇØ¾ß¸¸ »ç¿ë°¡´ÉÇÔ)
891     * @return  ·Î±×Àο©ºÎ
892     */
893            public static Boolean isLogined() {
894                    if (rainUtil.request.getID().equals(""))
895                            return false;
896                    else
897                            return true;
898            }
899            
900    /**
901     * ȯ°æÆÄÀÏÁ¤º¸°¡Á®¿À±â (ÇØ´ç ÇÔ¼ö´Â rainRequest ¸¦ ¸ÕÀú È£ÃâÇØ¾ß¸¸ »ç¿ë°¡´ÉÇÔ)
902     * @param  name  °¡Á®¿Ã Ű
903     * @return  ۰ª
904     */
905            public static String getConf(String name) {
906                    return rainUtil.site_conf.get(name);
907            }
908    
909    /**
910     * ±¤°í¹° Á¦ÇÑ Á¶Ä¡¸¦ À§ÇÑ ºñ·Î±×ÀÎȸ¿øÀÇ ÀÎÁõ¾ÏÈ£ »ý¼º ·ÎÁ÷ - (¿ù + ÀÏ)
911     * @return  ÀÎÁõ¾ÏÈ£
912     */
913            public static String getPassCode() {
914                    return rainUtil.getConf("spamCode");
915            }
916    
917    /**
918     * ±ÇÇÑ È®ÀÎ(ȸ¿ø·Î±×ÀÎÀÇ °æ¿ì true ¹Ýȯ, pass_key °¡ getPassCode ¿Í ÀÏÄ¡ÇÏ´Â °æ¿ì true ¹Ýȯ, ³ª¸ÓÁöÀÇ °æ¿ì´Â false ¹Ýȯ)
919     * @param  pass_key  ÀÎÁõ¾ÏÈ£
920     * @return  ±ÇÇÑ¿©ºÎ
921     */
922            public static Boolean chkAuth(String pass_key) {
923                    if (rainUtil.isLogined())
924                            return true;
925                    try {
926                            if (rainUtil.getPassCode().equals(pass_key))
927                                    return true;
928                            else
929                                    return false;
930                    } catch(Exception e) {
931                            return false;
932                    }
933            }
934            
935    /**
936     * ¾ÏÈ£ È®ÀÎ(·Î±×ÀÎµÈ È¸¿ø ¾ÆÀ̵𰡠¸¶½ºÆ® ¾ÆÀ̵ðÀÎ °æ¿ì true, old_pass ¿Í new_pass °¡ ÀÏÄ¡ÇÏ´Â °æ¿ì true , ±âŸÀÇ °æ¿ì´Â false ¸¦ ¹Ýȯ)
937     * @param  old_pass  ±âÁ¸ °ü¸® ¾ÏÈ£
938     * @param  new_pass  ÀÔ·Â ¹ÞÀº °ü¸® ¾ÏÈ£
939     * @return  ÀÏÄ¡ ¿©ºÎ
940     */
941            public static Boolean chkPass(String old_pass, String new_pass) {
942                    if (rainUtil.getConf("master_passwd").equals(new_pass))
943                            return true;
944                    else if (!new_pass.equals("") && old_pass.equals(new_pass))
945                            return true;
946                    else
947                            return false;
948            }
949    
950    /**
951     * ¿À·ù ·Î±× ±â·Ï - DOCUMENT ROOT / conf / error_log.txt ÆÄÀÏ¿¡ ±â·ÏÀÌ µÊ. ÇØ´çÆÄÀÏÀÌ ³Ê¹« Å©ÁöÁö ¾Êµµ·Ï °ü¸®°¡ ¿ä±¸µÊ - ¼ö½Ã »èÁ¦¿ä¸Á)
952     * @param  msg  ¿À·ù ¸Þ¼¼Áö
953     */
954            public static void errorLog(String msg) {
955                    if (!msg.equals("")) {
956                            try {
957                                    FileWriter fout = new FileWriter(new File(rainUtil.request.realPath("/") + "conf/error_log.txt"), true);
958                                    BufferedWriter bout = new BufferedWriter(fout);
959                                    bout.write(rainUtil.request.getSelf() + " "+ rainUtil.getUDate(rainUtil.request.getNow(),"yy³â MM¿ù ddÀÏ kk:mm")+"\r\n" + msg + "\r\n");
960                                    bout.close();
961                                    fout.close();
962                            } catch(Exception e) { }
963                    }
964            }
965    
966    /**
967     * ȸ¿ø ¾ÆÀ̵ð ÀÏÄ¡ ¿©ºÎ È®ÀÎ(·Î±×ÀÎµÈ È¸¿ø ¾ÆÀ̵𰡠¸¶½ºÆ® ¾ÆÀ̵ðÀÇ °æ¿ì true ¹Ýȯ)
968     * @param  user_id  ºñ±³ÇÒ È¸¿ø¾ÆÀ̵ð
969     * @return  ÀÏÄ¡¿©ºÎ
970     */
971            public static Boolean chkUser(String user_id) {
972                    if (!rainUtil.getConf("master_id").equals("") &&rainUtil.getConf("master_id").equals(rainUtil.request.getID()))
973                            return true;
974                    else if (!user_id.equals("") && user_id.equals(rainUtil.request.getID()))
975                            return true;
976                    else
977                            return false;
978            }
979    
980    /**
981     * ÀÛ¾÷°á°ú Àü¼ÛÀ» À§ÇÑ ½ºÅ©¸³ÅÍ »ý¼º
982     * @param  bl  ÀÛ¾÷¼º°ø ¿©ºÎ
983     * @param  go_url  À̵¿ÇÒ url
984     * @param  msg  À̵¿Àü¿¡ º¸¿©ÁÙ ¸Þ¼¼Áö
985     * @return  ½ºÅ©¸³ÅÍ
986     */
987            public static String sendResult(Boolean bl, String go_url, String msg) {
988                    String txt = ""
989                            + "<script>"
990                            + "try { "
991                            + "     parent.submitUnLock(); ";
992                    if (!msg.equals(""))
993                            txt += " parent.rainPopup.alert('"+msg+"'); ";
994                    if (!go_url.equals(""))
995                            txt += " rainCheck.openTop('"+go_url+"', parent); ";
996                    txt += "} catch(ex) {} </script> </body>";
997                    return txt;
998            }
999    
1000    /**
1001     * ÀÛ¾÷°á°ú Àü¼ÛÀ» À§ÇÑ XML ½ºÅ©¸³ÅÍ »ý¼º
1002     * @param  bl  ÀÛ¾÷¼º°ø ¿©ºÎ
1003     * @param  go_url  À̵¿ÇÒ url
1004     * @param  msg  À̵¿Àü¿¡ º¸¿©ÁÙ ¸Þ¼¼Áö
1005     * @return  XML½ºÅ©¸³ÅÍ
1006     */
1007            public static String sendXMLResult(Boolean bl, String go_url, String msg) {
1008                    String txt = ""
1009                    + "<?xml version=\"1.0\" encoding=\"EUC-KR\" ?>\r\n"
1010                    + "<rss version=\"2.0\""
1011                    + " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
1012                    + " xmlns:dc=\"http://purl.org/dc/elements/1.1/\""
1013                    + " xmlns:taxo=\"http://purl.org/rss/1.0/modules/taxonomy/\" >\r\n"
1014                    + "<channel>\r\n";
1015                    if (!msg.equals(""))
1016                            txt += "<msg><![CDATA["+msg+"]]></msg>\r\n";
1017                    if (!go_url.equals(""))
1018                            txt += "<link><![CDATA["+go_url+"]]></link>\r\n";
1019                    txt += "</channel>\r\n</rss>\r\n";
1020                    return txt;     
1021            }
1022    
1023    /**
1024     * ÀÛ¾÷°á°ú¸¦ XML ½ºÅ©¸³ÅÍ·Î Àü¼Û
1025     * @param  response  HttpServletResponse °´Ã¼
1026     * @param  bl  ÀÛ¾÷¼º°ø ¿©ºÎ
1027     * @param  go_url  À̵¿ÇÒ url
1028     * @param  msg  À̵¿Àü¿¡ º¸¿©ÁÙ ¸Þ¼¼Áö
1029     */
1030            public static void sendXMLResult(HttpServletResponse response, Boolean bl, String go_url, String msg) {
1031                    response.reset();
1032                    response.setHeader("Content-Type", "text/xml; charset=EUC-KR");
1033                    try {
1034                            PrintWriter out = response.getWriter();
1035                            out.print(rainUtil.sendXMLResult(bl,go_url, msg));
1036                            out.close();
1037                    } catch(Exception e) { }
1038            }
1039    }