

package org.greenstone.gsdl3.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
//import java.io.IOException;
import java.io.InputStreamReader;
//import java.io.StringReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
// https://developer.android.com/reference/org/json/JSONObject.html
// https://developer.android.com/reference/org/json/JSONArray.html
//import org.json.JSONArray;
//import org.json.JSONException;
import org.json.JSONObject;

import org.greenstone.util.GlobalProperties;

public class RecaptchaUtil
{
    
    //  static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.RecaptchaUtil.class.getName());
    // error codes
    public static final int NO_ERROR = 0;
    public static final int ERROR_CAPTCHA_MISSING = 1;
    public static final int ERROR_CAPTCHA_FAILED = 2;
    public static final int ERROR_CONNECTION_FAILED = 3;
    
    private static String site_key = null;
    private static String secret_key = null;

    private static boolean loaded = false;
    private static boolean has_keys = false;
    
    public static void initKeys() {
	if (loaded == false) {
	    site_key = GlobalProperties.getProperty("recaptcha.site_key");
	    secret_key = GlobalProperties.getProperty("recaptcha.secret_key");
	    loaded = true;
	    if (site_key != null && site_key.length() >0 && secret_key !=null && secret_key.length() >0) {
		has_keys = true;
	    }
	}
    }

    public static boolean hasKeys() {
	if (loaded == false) {
	    initKeys();
	}
	return has_keys;
    }
    
    public static String getSiteKey() {
	return site_key;
    }

    public static int verifyRecaptcha(String user_response) {
	
	if (user_response == null || user_response.length() == 0) {
	    return ERROR_CAPTCHA_MISSING;
	}
	
	try{
	    
	    URL obj = new URL("https://www.google.com/recaptcha/api/siteverify");
	    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
	    
	    // add reuqest header
	    con.setRequestMethod("POST");
	    con.setRequestProperty("User-Agent", "Mozilla/5.0");
	    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
	    
	    String postParams = "secret=" + secret_key + "&response="
		+ user_response;
	    
	    // Send post request
	    con.setDoOutput(true);
	    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
	    wr.writeBytes(postParams);
	    wr.flush();
	    wr.close();
	    
	    int responseCode = con.getResponseCode();
	    //System.out.println("\nSending 'POST' request to URL : https://www.google.com/recaptcha/api/siteverify");// + url);
	    //System.out.println("Post parameters : " + postParams);
	    //System.out.println("Response Code : " + responseCode);
	    
	    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
	    String inputLine;
	    StringBuffer response = new StringBuffer();
	    
	    while ((inputLine = in.readLine()) != null) {
		response.append(inputLine);
	    }
	    in.close();
	    
	    // print result
	    //System.out.println(response.toString());
	    
	    JSONObject json_obj = new JSONObject(response.toString());
	    boolean res = json_obj.getBoolean("success");
	    if (res) {
		return NO_ERROR;
	    } else {
		return ERROR_CAPTCHA_FAILED;
	    }
	}catch(Exception e){
	    e.printStackTrace();
	    return ERROR_CONNECTION_FAILED;
	}
	
    }
    
}


