/*
 * Copyright 2008-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package itn.com.cmm.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

public class IpUtil {
	public static String getClientIP(HttpServletRequest request) {
        String userip = request.getHeader("X-Forwarded-For") == null ? request.getHeader("X-Forwarded-For") : request.getHeader("X-Forwarded-For").replaceAll("10.12.107.11", "").replaceAll(",", "").trim(); // 아이피 가져오기 아파치 아래에 웹로직이 있을경우
         
        if ( userip == null  || "".equals(userip) ) { // 아이피 가져오기 , 바로 웹로직이 있을경우
            userip = request.getRemoteAddr();
        }
        
        if ( userip == null  || "".equals(userip) ) {
            return "";
        }
        
        String[] userips = userip.split(",");
        return userips[0];
    }
	
	public static String getBrowser(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        
        System.out.println("header:"+header);
        
        if (header.indexOf("MSIE") > -1 || header.indexOf("rv:11.0") > -1) {
            return "MSIE";
        }else if (header.indexOf("Opera") > -1 || header.indexOf("OPR") > -1) {
            return "Opera";
        }else if (header.indexOf("Chrome") > -1) {
            return "Chrome";
        }else if (header.indexOf("Firefox") > -1) {
            return "Firefox";
        }
        return "Firefox";
	}
	
	public static String getStr(HttpServletRequest request,String str) throws UnsupportedEncodingException {
        String header = request.getHeader("User-Agent");
        
        String browser;
        
        if (header.indexOf("MSIE") > -1 || header.indexOf("rv:11.0") > -1) {
        	browser =  "MSIE";
        } else if (header.indexOf("Chrome") > -1) {
        	browser =  "Chrome";
        } else if (header.indexOf("Opera") > -1) {
        	browser =  "Opera";
        } else {
        	 browser =  "Firefox";
        }
       
        if (browser.equals("MSIE")) {
        	str = URLEncoder.encode(str, "UTF-8")
                    .replaceAll("\\+", "%20");
        } else if (browser.equals("Firefox")) {
        	str = new String(str.getBytes("UTF-8"), "8859_1");
        } else if (browser.equals("Opera")) {
        	str = new String(str.getBytes("UTF-8"), "8859_1");
        } else if (browser.equals("Chrome")) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (c > '~') {
                    sb.append(URLEncoder.encode("" + c, "UTF-8"));
                } else {
                    sb.append(c);
                }
            }
            str = sb.toString();
        } else {
        	str = "Not supported browser";
        }
        
        return str;
	}
}