<commit_before>package app.sunstreak.yourpisd.net;


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import android.app.Application;
import android.graphics.Bitmap;


public class DataGrabber /*implements Parcelable*/ extends Application {

	Domain domain;
	String username;
	String password;
	String[] passthroughCredentials;
	String[] gradebookCredentials;
	String pageUniqueId;
	// 1= success. 0 = not tried; <0 = failure.
	int editureLogin = 0;
	int gradebookLogin = 0;

	ArrayList<String> cookies = new ArrayList<String>();
	JSONArray classList = null;
	int[][] gradeSummary = null;
	// Class -> Term
	//	Date[][] lastUpdated;
	int studentId = 0;
	int[] classIds;
	/**
	 * key is of the form {classIndex, termIndex}.
	 */
	Map<Integer[], JSONObject> classGrades = new HashMap<Integer[], JSONObject>();
	//	SparseArray<JSONObject> classGrades = new SparseArray<JSONObject>();
	String studentName = "";
	Bitmap studentPictureBitmap;

	int[] classMatch;

	public void clearData () {
		domain = null;
		username = null;
		password = null;
	}

	public void setData (Domain domain, String username, String password) {
		//	public DataGrabber (Domain domain, String username, String password) {
		this.domain = domain;
		this.username = username;
		this.password = password;

		/*
		 * to accept expired certificate of login1.mypisd.net
		 */
		//acceptAllCertificates();
	}

	/**
	 * Logs in to Editure/New Age portal. Retrieves passthroughCredentials.
	 * Precondition: username and password are defined.
	 * @throws MalformedURLException
	 * @throws IllegalUrlException
	 * @throws IOException
	 * @throws PISDException
	 * @throws InterruptedException
	 * @throws ExecutionException
	 * @return 1 if success, -1 if parent failure, -2 if student failure, 0 if domain value is not 0 or 1
	 */
	public int login(/*Domain dom, String username, String password*/)
			throws MalformedURLException, IllegalUrlException, IOException, PISDException, InterruptedException, ExecutionException {

		String response;
		int responseCode;

		switch (domain.index) {
		case 0:	// Parent
			Object[] cookieAuth = Request.sendPost(
					domain.loginAddress, 
					"password=" + URLEncoder.encode(password,"<PASSWORD>") + "&username=" + URLEncoder.encode(username,"UTF-8") + "&Submit=Login", 
					cookies);

			response = (String) cookieAuth[0];
			responseCode = (Integer) cookieAuth[1];
			cookies = (ArrayList<String>) cookieAuth[2];

			if (Parser.accessGrantedEditure(response)) {
				System.out.println("Editure access granted!");
				editureLogin = 1;


				passthroughCredentials = Parser.passthroughCredentials(response);

				return 1;
			}
			else {
				System.out.println("Bad username/password 1!");
				System.out.println(response);
				editureLogin = -1;
				return -1;
			}
		case 1: // Student
			Object[] portalDefaultPage = Request.sendGet(
					domain.loginAddress,
					cookies);

			response = (String) portalDefaultPage[0];
			responseCode = (Integer) portalDefaultPage[1];
			cookies = (ArrayList<String>) portalDefaultPage[2];

			String[][] requestProperties = new String[][] {
					{"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
					{"Accept-Encoding","gzip,deflate,sdch"},
					{"Accept-Language","en-US,en;q=0.8,es;q=0.6"},
					{"Cache-Control","max-age=0"},
					{"Connection","keep-alive"},
					//{"Content-Length","75"},
					{"Content-Type","application/x-www-form-urlencoded"},
					//{"Cookie","JSESSIONID=22DDAFA488B9D839F082FE26EFE8B38B"},
					{"Host","sso.portal.mypisd.net"},
					{"Origin","https://sso.portal.mypisd.net"},
					{"Referer","https://sso.portal.mypisd.net/cas/login?service=http%3A%2F%2Fportal.mypisd.net%2Fc%2Fportal%2Flogin"},
					{"User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36"}
			};

			ArrayList<String[]> rp = new ArrayList<String[]>(java.util.Arrays.asList(requestProperties));

			String lt = Parser.portalLt(response);

			String postParams = "username=" + URLEncoder.encode(username, "UTF-8") + 
					"&password=" + URLEncoder.encode(password, "<PASSWORD>") + 
					"&lt=" + lt +
					"&_eventId=submit";
			try {
				Object[] portalLogin = Request.sendPost(
						domain.loginAddress,
						cookies,
						rp,
						true,
						postParams);

				response = (String) portalLogin[0];
				responseCode = (Integer) portalLogin[1];
				cookies = (ArrayList<String>) portalLogin[2];


				// weird AF way of checking for bad password.
				if (Request.getRedirectLocation() == null)
					return -1;

				Object[] ticket = Request.sendGet(
						Request.getRedirectLocation(),<commit_msg>Removed User-Agent<commit_after>package app.sunstreak.yourpisd.net;


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

import android.app.Application;
import android.graphics.Bitmap;


public class DataGrabber /*implements Parcelable*/ extends Application {

	Domain domain;
	String username;
	String password;
	String[] passthroughCredentials;
	String[] gradebookCredentials;
	String pageUniqueId;
	// 1= success. 0 = not tried; <0 = failure.
	int editureLogin = 0;
	int gradebookLogin = 0;

	ArrayList<String> cookies = new ArrayList<String>();
	JSONArray classList = null;
	int[][] gradeSummary = null;
	// Class -> Term
	//	Date[][] lastUpdated;
	int studentId = 0;
	int[] classIds;
	/**
	 * key is of the form {classIndex, termIndex}.
	 */
	Map<Integer[], JSONObject> classGrades = new HashMap<Integer[], JSONObject>();
	//	SparseArray<JSONObject> classGrades = new SparseArray<JSONObject>();
	String studentName = "";
	Bitmap studentPictureBitmap;

	int[] classMatch;

	public void clearData () {
		domain = null;
		username = null;
		password = <PASSWORD>;
	}

	public void setData (Domain domain, String username, String password) {
		//	public DataGrabber (Domain domain, String username, String password) {
		this.domain = domain;
		this.username = username;
		this.password = password;

		/*
		 * to accept expired certificate of login1.mypisd.net
		 */
		//acceptAllCertificates();
	}

	/**
	 * Logs in to Editure/New Age portal. Retrieves passthroughCredentials.
	 * Precondition: username and password are defined.
	 * @throws MalformedURLException
	 * @throws IllegalUrlException
	 * @throws IOException
	 * @throws PISDException
	 * @throws InterruptedException
	 * @throws ExecutionException
	 * @return 1 if success, -1 if parent failure, -2 if student failure, 0 if domain value is not 0 or 1
	 */
	public int login(/*Domain dom, String username, String password*/)
			throws MalformedURLException, IllegalUrlException, IOException, PISDException, InterruptedException, ExecutionException {

		String response;
		int responseCode;

		switch (domain.index) {
		case 0:	// Parent
			Object[] cookieAuth = Request.sendPost(
					domain.loginAddress, 
					"password=" + URLEncoder.encode(password,"<PASSWORD>") + "&username=" + URLEncoder.encode(username,"UTF-8") + "&Submit=Login", 
					cookies);

			response = (String) cookieAuth[0];
			responseCode = (Integer) cookieAuth[1];
			cookies = (ArrayList<String>) cookieAuth[2];

			if (Parser.accessGrantedEditure(response)) {
				System.out.println("Editure access granted!");
				editureLogin = 1;


				passthroughCredentials = Parser.passthroughCredentials(response);

				return 1;
			}
			else {
				System.out.println("Bad username/password 1!");
				System.out.println(response);
				editureLogin = -1;
				return -1;
			}
		case 1: // Student
			Object[] portalDefaultPage = Request.sendGet(
					domain.loginAddress,
					cookies);

			response = (String) portalDefaultPage[0];
			responseCode = (Integer) portalDefaultPage[1];
			cookies = (ArrayList<String>) portalDefaultPage[2];

			String[][] requestProperties = new String[][] {
					{"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
					{"Accept-Encoding","gzip,deflate,sdch"},
					{"Accept-Language","en-US,en;q=0.8,es;q=0.6"},
					{"Cache-Control","max-age=0"},
					{"Connection","keep-alive"},
					//{"Content-Length","75"},
					{"Content-Type","application/x-www-form-urlencoded"},
					//{"Cookie","JSESSIONID=22DDAFA488B9D839F082FE26EFE8B38B"},
					{"Host","sso.portal.mypisd.net"},
					{"Origin","https://sso.portal.mypisd.net"},
					{"Referer","https://sso.portal.mypisd.net/cas/login?service=http%3A%2F%2Fportal.mypisd.net%2Fc%2Fportal%2Flogin"}
			};

			ArrayList<String[]> rp = new ArrayList<String[]>(java.util.Arrays.asList(requestProperties));

			String lt = Parser.portalLt(response);

			String postParams = "username=" + URLEncoder.encode(username, "UTF-8") + 
					"&password=" + URLEncoder.encode(password, "UTF-8") + 
					"&lt=" + lt +
					"&_eventId=submit";
			try {
				Object[] portalLogin = Request.sendPost(
						domain.loginAddress,
						cookies,
						rp,
						true,
						postParams);

				response = (String) portalLogin[0];
				responseCode = (Integer) portalLogin[1];
				cookies = (ArrayList<String>) portalLogin[2];


				// weird AF way of checking for bad password.
				if (Request.getRedirectLocation() == null)
					return -1;

				Object[] ticket = Request.sendGet(
						Request.getRedirectLocation(),
{'fraction_non_alphanumeric': 0.13841918993314983, 'fraction_numerical': 0.016122689736531654, 'mean_word_length': 4.127520161290323, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1683711', 'n_tokens_mistral': 3771, 'n_tokens_neox': 3210, 'n_words': 709}