001    
002    package com.rain;
003    
004    import java.net.*;
005    import javax.servlet.http.*;
006    import java.lang.*;
007    import java.sql.*;
008    import java.util.Date;
009    import java.util.Calendar;
010    import java.text.*;
011    
012    /**  
013     * ·¹ÀÎ °Ô½ÃÆÇ - °´Ã¼ÁöÇâÀû°Ô½ÃÆÇ
014     * @version 9.0
015     * @author http://cafen.net (outmind@cafen.net)
016     */
017    public class rainBoard{
018    
019    /**  
020     * °Ô½ÃÆÇ ¾ÆÀ̵ð
021     */
022            public String board_id = "";
023    
024    /**  
025     * MYSQL CLASS
026     */
027            public rainMysql db_class = null;
028    
029    /**  
030     * ±âº» Äõ¸®
031     */
032            public String base_query = "";
033    
034    /**  
035     * ¸¶Áö¸· ÀÔ·Â °íÀ¯¹øÈ£
036     */
037            public int last_muid;
038    
039    /**  
040     * ¿À·ù ¸Þ¼¼Áö
041     */
042            public String error_msg = "";
043    
044    /**  
045     * °Ô½ÃÆÇ ȯ°æ Á¤º¸
046     */
047            public rainProperties board_conf = null;
048            
049    /**
050     * »ý¼ºÀÚ
051     * @param  board_id  °Ô½ÃÆÇ ¾ÆÀ̵ð
052     */
053            public rainBoard(String board_id) {
054                    this.board_conf = new rainProperties("board_" + board_id);
055                    this.db_class = new rainMysql("mysql");
056                    this.board_id = board_id;
057                    this.base_query = "select * from multi_board where board_id = '"+this.board_id+"' ";
058            }
059    
060    /**
061     *  °Ô½ÃÆÇ ȯ°æ Á¤º¸ Àбâ
062     * @param  name  ÀÐÀ» ۰ª
063     * @return  value
064     */
065            public String getConf(String name) {
066                    return this.board_conf.get(name);
067            }
068    
069    /**
070     *  ±ÇÇÑ È®ÀÎ
071     * @param  mode  »óű¸ºÐ [BOARD :°Ô½ÃÆÇÈ®ÀÎ, REPLY : ´äº¯±ÇÇÑ È®ÀÎ, WRITE : ÀÛ¼º±ÇÇÑ, TAIL : µ¡±Û »ç¿ë¿©ºÎ]
072     * @return  ±ÇÇÑ ¿©ºÎ
073     */
074            public Boolean checkAuth(String mode) {
075                    if (mode.equals("BOARD")) 
076                                    return (this.board_conf.file_loaded) ? true:false;
077                    else if (mode.equals("REPLY") || mode.equals("WRITE")) {
078                                    String mode_value = this.getConf((mode.equals("REPLY"))?"reply_mode":"write_mode");
079                                    if (mode_value.equals("A")) 
080                                            return rainUtil.chkUser("");
081                                    else if (mode_value.equals("M")) 
082                                            return rainUtil.isLogined();
083                                    else if (mode_value.equals("")) 
084                                            return true;
085                                    else
086                                            return rainUtil.chkUser(mode_value);
087                    } else if (mode.equals("TAIL")) 
088                            return (this.getConf("tail_mode").equals("ON")) ? true:false;
089                    else
090                            return false;
091            }
092    
093    /**
094     *  µ¡±Û Class °¡Á®¿À±â
095     * @param  muid ±Û °íÀ¯¹øÈ£
096     * @return  rainTail
097     */
098            public rainTail getTailClass(int muid) {
099                    return new rainTail(this.board_id, muid);
100            }
101    
102    /**
103     *  °Ô½ÃÆÇ ¸ñ·Ï °¡Á®¿À±â
104     * @param  cline ÇöÀç ¶óÀιøÈ£
105     * @param  req rainRequest
106     * @param  getnum °¡Á®¿Ã ±Û °¹¼ö
107     * @return  rainData
108     */
109            public rainData getList(int cline, rainRequest req , int getnum) {
110                    String qm = this.base_query ;
111                    if (!req.get("sk").equals("")) {
112                            String sk = rainUtil.addslashesQuery(req.get("sk"));
113                            String so = req.get("so");
114                            if (so.equals("T"))
115                                    qm += "  and title like '%"+ sk +"%' ";
116                            else if (so.equals("N"))
117                                    qm += "  and user_nm like '%"+ sk +"%' ";
118                            else if (so.equals("C"))
119                                    qm += "  and contents_text like '%"+ sk +"%' ";
120                            else
121                                    qm += "  and (title like '%"+ sk +"%' or user_nm like '%"+ sk +"%' or contents_text like '%"+ sk +"%') ";
122                    }
123                    qm += "order by board_uid desc ";
124                    return this.db_class.query2Array(qm, cline, getnum);
125            }
126            
127    /**
128     *  °Ô½ÃÆÇ ±Û ³»¿ë°¡Á®¿À±â
129     * @param  muid ±Û °íÀ¯¹øÈ£
130     * @param  addhits Á¶È½¼ö Ãß°¡ ¿©ºÎ
131     * @return  rainData
132     */
133            public rainData getView(int muid, Boolean addhits) {
134                    if (addhits) {
135                            rainBoardHit boardhit = new rainBoardHit(muid);
136                            boardhit.insertData();
137                    }
138                    String qm = this.base_query +" and muid = '"+muid+"' ";
139                    return this.db_class.queryFetch(qm);
140            }
141    
142    /**
143     *  °Ô½ÃÆÇ ±Û ³»¿ë°¡Á®¿À±â
144     * @param  muid ±Û °íÀ¯¹øÈ£
145     * @return  rainData
146     */
147            public rainData getView(int muid) {
148                    return this.getView(muid, false);
149            }
150    
151    /**
152     *  ÀÌÀü±Û °¡Á®¿À±â
153     * @param  board_uid ±âÁØ ±Û ¹øÈ£
154     * @return  rainData
155     */
156            public rainData getPrev(int board_uid) {
157                    String qm = this.base_query + " and board_uid > '"+board_uid+"' order by board_uid asc ";
158                    return this.db_class.queryFetch(qm);
159            }
160    
161    /**
162     *  ´ÙÀ½±Û °¡Á®¿À±â
163     * @param  board_uid ±âÁØ ±Û ¹øÈ£
164     * @return  rainData
165     */
166            public rainData getNext(int board_uid) {
167                    String qm = this.base_query + " and board_uid < '"+board_uid+"' order by board_uid desc ";
168                    return this.db_class.queryFetch(qm);
169            }
170    
171    /**
172     *  °Ô½ÃÆÇ °ü·Ã URL °¡Á®¿À±â
173     * @param  mode »óÅ [view : º¸±â, list : ¸ñ·Ï]
174     * @return  URL
175     */
176            public String getUrl(String mode) {
177                    if (mode.equals("view"))
178                            return this.getConf("url_view")+"?board_id="+this.board_id+"&muid="+this.last_muid;
179                    else
180                            return this.getConf("url_list")+"?board_id="+this.board_id;
181            }
182    
183    /**
184     *  °Ô½ÃÆÇ ÀÚ·á »èÁ¦
185     * @param  muid ±Û °íÀ¯¹øÈ£
186     * @param  passwd °ü¸® ¾ÏÈ£
187     * @return  »èÁ¦¼º°ø¿©ºÎ
188     */
189            public Boolean deleteData(int muid, String passwd) {
190                    if (this.checkPass(muid, passwd)) {
191                            rainData old_result = this.getView(muid);
192                            if (old_result.first()) {
193                                    rainFile rfile = new rainFile(rainUtil.getConf("path_uploads"));
194                                    String old_contents = old_result.getString("contents");
195                                    rfile.rainEditAttachSaveDelete("", old_contents);
196                            }
197                            rainTail tail_class = this.getTailClass(muid);
198                            tail_class.deleteAllData();
199                            rainBoardHit boardhit = new rainBoardHit(muid);
200                            boardhit.deleteAllData();
201                            
202                            this.db_class.change("delete from multi_board where muid = '"+muid+"' ");
203                            return true;
204                    } else {
205                            this.error_msg = "ÇØ´ç ±ÛÀÇ Á¢±Ù ±ÇÇÑÀÌ ¾ø½À´Ï´Ù";
206                            return false;
207                    }
208            }
209    
210    /**
211     *  °Ô½ÃÆÇ Á¢±Ù ±ÇÇÑ È®ÀÎ
212     * @param  muid ±Û °íÀ¯¹øÈ£
213     * @param  passwd °ü¸® ¾ÏÈ£
214     * @return  Á¢±Ù °¡´É ¿©ºÎ
215     */
216            public Boolean checkPass(int muid, String passwd) {
217                    rainData result = this.getView(muid);
218                    if (result.first()) {
219                            String old_passwd = result.getString("user_pw");
220                            String user_id = result.getString("user_id");
221                            if (rainUtil.chkUser(user_id) || rainUtil.chkPass(old_passwd, passwd))
222                                    return true;
223                            else
224                                    return false;
225                    } else
226                            return false;
227            }
228    
229    /**
230     *  °Ô½ÃÆÇ ±Û ÀÛ¼º
231     * @param  req ÀÔ·Â ¹ÞÀ» µ¥ÀÌŸ
232     * @return  ÀÔ·Â ¼º°ø ¿©ºÎ
233     */
234            public Boolean insertData(rainRequest req) {
235                    return this.insertData(req, 0);
236            }
237            
238    /**
239     *  °Ô½ÃÆÇ ±Û ÀÛ¼º
240     * @param  req ÀÔ·Â ¹ÞÀ» µ¥ÀÌŸ
241     * @param  reply_uid °ü·Ã ´ä±Û
242     * @return  ÀÔ·Â ¼º°ø ¿©ºÎ
243     */
244            public Boolean insertData(rainRequest req, int reply_uid) {
245                    if (!rainUtil.chkAuth(req.get("authcode"))) {
246                            this.error_msg = "ÀÎÁõÄڵ尡 ´Þ¶ó¼­ ó¸® ÇÒ¼ö ¾ø½À´Ï´Ù <br><br>¹Ù¸¥ÀÎÁõÄÚµå(<u>" + rainUtil.getPassCode() +"</u>) ";
247                            return false;
248                    } else if (! this.checkAuth((reply_uid == 0) ? "WRITE":"REPLY")) {
249                            this.error_msg = "ÇØ´ç °Ô½ÃÆÇÀÇ Á¢±Ù ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.<br>±ÇÇÑÀ» °ü¸®ÀÚ¿¡°Ô ¹®ÀÇ ¹Ù¶ø´Ï´Ù. ";
250                            return false;
251                    }
252                            
253                    int muid = this.db_class.getLastID("multi_board", "muid");
254                    rainFile rfile = new rainFile(rainUtil.getConf("path_uploads"));
255                    String title = rainUtil.htmlSpecialchars(req.get("title"));
256                    String contents= req.get("contents");
257                    contents = rfile.rainEditAttachSaveDelete(contents);
258                    String contents_view = rainUtil.addslashes(rfile.rainContents_html);
259                    String contents_text = rfile.stripTags(rfile.rainContents_html);
260                    String contents_attach = rfile.rainAttachParse(false);
261                    String contents_image = rfile.rainAttach_image;
262                    int attach_size = rfile.totalattach_size;
263                    String is_movie = rfile.rainAttach_movie;
264                    String user_id =  req.getID();
265                    String user_nm =  req.getName(req.get("user_nm"));
266                    String user_email = req.getEmail(req.get("user_email"));
267                    String user_pw = req.get("user_pw");
268                    String user_ip = req.getIP();
269                    String board_uid = "";
270                    String board_dep = "";
271                    String qm = "";
272                    if (reply_uid == 0) {
273                            board_uid = this.db_class.queryOne("select ifnull(max(board_uid)+1,1) as board_uid from multi_board where board_id = '"+this.board_id+"' ");
274                            board_dep = "0";
275                    } else{
276                            qm = "select board_uid, board_dep+1 as board_dep from multi_board where board_id = '"+this.board_id+"' and muid = '"+reply_uid+"' ";
277                            rainData check_result = this.db_class.queryFetch(qm);
278                            if (check_result.first()) {
279                                    board_uid = check_result.getString("board_uid");
280                                    board_dep = check_result.getString("board_dep");
281                                    qm = "update multi_board set board_uid = board_uid + 1 where board_id = '"+this.board_id+"' and board_uid >= '"+board_uid+"' ";
282                                    this.db_class.change(qm);
283                            } else {
284                                    board_uid = this.db_class.queryOne("select ifnull(max(board_uid)+1,1) as board_uid from multi_board where board_id = '"+this.board_id+"' ");
285                                    board_dep = "0";
286                            }                               
287                    }
288                    int regdate = req.getNow();
289                    qm = ""
290                            +"insert into multi_board ( "
291                            +" muid, "
292                            +"      board_id,"
293                            +"      board_uid,"
294                            +"      board_dep,"
295                            +"      user_nm,"
296                            +"      user_email,"
297                            +"      user_ip,"
298                            +"      user_id,"
299                            +"      user_pw,"
300                            +"      title,"
301                            +"      contents,"
302                            +"      contents_view,"
303                            +"      contents_text,"
304                            +"      contents_attach,"
305                            +"      contents_image,"
306                            +"      tail,"
307                            +"      regdate,"
308                            +"      attach_size,"
309                            +"      is_movie,"
310                            +"      hits"
311                            +")values("
312                            +"      '"+muid+"',"
313                            +"      '"+this.board_id+"',"
314                            +"      '"+board_uid+"',"
315                            +"      '"+board_dep+"',"
316                            +"      '"+user_nm+"',"
317                            +"      '"+user_email+"',"
318                            +"      '"+user_ip+"',"
319                            +"      '"+user_id+"',"
320                            +"      '"+user_pw+"',"
321                            +"      '"+title+"',"
322                            +"      '"+contents+"',"
323                            +"      '"+contents_view+"',"
324                            +"      '"+contents_text+"',"
325                            +"      '"+contents_attach+"',"
326                            +"      '"+contents_image+"',"
327                            +"      '0',"
328                            +"      '"+regdate+"',"
329                            +"      '"+attach_size+"',"
330                            +"      '"+is_movie+"',"
331                            +"      '0'"
332                            +")";
333                    this.last_muid = muid;
334                    this.db_class.change(qm);       
335                    return true;
336            }
337    
338    /**
339     *  °Ô½ÃÆÇ ±Û ¼öÁ¤
340     * @param  muid ¼öÁ¤ÇÒ ±Û °íÀ¯¹øÈ£
341     * @param  req ÀÔ·Â ¹ÞÀ» µ¥ÀÌŸ
342     * @return  ¼öÁ¤ ¼º°ø ¿©ºÎ
343     */
344            public Boolean modifyData(int muid,rainRequest req) {
345                    if (!rainUtil.chkAuth(req.get("authcode"))) {
346                            this.error_msg = "ÀÎÁõÄڵ尡 ´Þ¶ó¼­ ó¸® ÇÒ¼ö ¾ø½À´Ï´Ù <br><br>¹Ù¸¥ÀÎÁõÄÚµå(<u>" + rainUtil.getPassCode() + "</u>) ";
347                            return false;
348                    } else if (! this.checkAuth("WRITE")) {
349                            this.error_msg = "ÇØ´ç °Ô½ÃÆÇÀÇ Á¢±Ù ±ÇÇÑÀÌ ¾ø½À´Ï´Ù.<br>±ÇÇÑÀ» °ü¸®ÀÚ¿¡°Ô ¹®ÀÇ ¹Ù¶ø´Ï´Ù. ";
350                            return false;
351                    }
352    
353                    String old_passwd = req.get("user_pw");
354                    if (this.checkPass(muid, old_passwd)) {
355                            rainFile rfile = new rainFile(rainUtil.getConf("path_uploads"));
356                            String old_contents = "";
357                            rainData old_result = this.getView(muid);
358                            if (old_result.first())
359                                    old_contents = old_result.getString("contents");
360                                    
361                            String title = rainUtil.htmlSpecialchars(req.get("title"));
362                            String contents= req.get("contents");
363                            contents = rfile.rainEditAttachSaveDelete(contents, old_contents);
364                            String contents_view = rainUtil.addslashes(rfile.rainContents_html);
365                            String contents_text = rfile.stripTags(rfile.rainContents_html);
366                            String contents_attach = rfile.rainAttachParse(false);
367                            String contents_image = rfile.rainAttach_image;
368                            int attach_size = rfile.totalattach_size;
369                            String is_movie = rfile.rainAttach_movie;
370                            String user_id =  req.getID();
371                            String user_nm =  req.getName(req.get("user_nm"));
372                            String user_email = req.getEmail(req.get("user_email"));
373                            String user_ip = req.getIP();
374                            String qm = "update multi_board set "
375                                    + " user_id = '"+user_id+ "', "
376                                    + " user_nm = '"+user_nm+ "', "
377                                    + " user_email = '"+user_email+ "', "
378                                    + " user_ip = '"+user_ip+ "', "
379                                    + " title = '"+title+ "', "
380                                    + " contents = '"+contents+ "', "
381                                    + " contents_view = '"+contents_view+ "', "
382                                    + " contents_text = '"+contents_text+ "', "
383                                    + " contents_attach = '"+contents_attach+ "', "
384                                    + " contents_image = '"+contents_image+ "', "
385                                    + " attach_size = '"+attach_size+ "', "
386                                    + " is_movie = '"+is_movie+ "' "
387                                    + " where board_id = '"+this.board_id+"' and muid = '"+muid+"' ";
388                            this.db_class.change(qm);       
389                            this.last_muid = muid;
390                            return true;
391                    } else {
392                            this.error_msg = "±âÁ¸ ¾ÏÈ£¿Í ÀÏÄ¡ ÇÏÁö ¾Ê½À´Ï´Ù.";
393                            return false;
394                    }
395            }
396    
397    /**
398     *  Unix ŸÀÓÀ» ƯÁ¤ ÇüÅ·Πº¯°æ
399     * @param  date_str Unix ŸÀÓ
400     * @param  date_fmat ÀÏÀÚ Æ÷¸Ë
401     * @return  ƯÁ¤ Æ÷¸ËÀÇ ÀÏÀÚ
402     */
403            public String getDate(int date_str, String date_fmat) {
404                    Date today = new Date((long) date_str * 1000);
405                    return this.getDateFormat(today,date_fmat);
406            }
407    
408    /**
409     *  Date À» ƯÁ¤ ÇüÅ·Πº¯°æ
410     * @param  today Date °´Ã¼
411     * @param  date_fmat ÀÏÀÚ Æ÷¸Ë
412     * @return  ƯÁ¤ Æ÷¸ËÀÇ ÀÏÀÚ
413     */
414            public static String getDateFormat(Date today, String date_fmat) {
415                    SimpleDateFormat formatter = new SimpleDateFormat(date_fmat);
416                    return formatter.format(today);
417            }
418    }
419