<commit_before><?php
/* CLASS mite - last updated 23th December 2010
 * 
 * @description provides methods to communicate with the MITE API
 * @package mite.plugins
 * @author <NAME> <<EMAIL>>
 * @version of mite.php 1.2.1
 * @license MIT License
 * 
 * Example usage:
 * -------------
	$o_mite = mite::getInstance();
 	$o_mite->init(<YOUR_API_KEY>,<YOUR_ACCOUNT_SUBDOMAIN>,'<MY_APP_NAME>/v1.2.5');
	try {
 		$o_responseXML = $o_mite->sendRequest('get','/time_entries.xml');
	} catch (Exception $e) {
		echo "<p>".$e->getMessage()."</p>";
	}
 * -------------
 * 
 */

class mite {
	
############
# PROPERTIES
#######	
	private $s_header;
	private $i_port;
	private $s_miteAccountUrl;
	private $s_protocolPrefix;
	
	private static $instance = null;//necessary to act as singleton
	
############
# CONSTANTS
#######	
    const MITE_PHP_VERSION = '1.2';
	const MITE_DOMAIN = 'mite.yo.lk';
	const REQUEST_TIMEOUT = 5;
	const EXCEPTION_RSRC_NOT_FOUND = 100;
	const EXCEPTION_UNPARSABLE_XML = 101;
	const EXCEPTION_WRONG_REQUEST_TYPE = 102;
	const EXCEPTION_MISSING_ACCOUNT_DATA = 103;
	const EXCEPTION_NO_ACCESS = 104;
	const EXCEPTION_TIMED_OUT = 105;
	const EXCEPTION_NO_SERVER_RESPONSE = 106;
	const EXCEPTION_UNEXPECTED_RESPONSE = 107;
	const EXCEPTION_CONNECTION_REFUSED = 108;
	
############	
# METHODS
#######
	
# make silent to act as singleton
	private function __clone() {}
	private function __construct() {}
	
/*****************************************************
 * Returns always the same object instance
 * 
 * @return object miteRemote
 */
	public static function getInstance() {
 
		if (self::$instance === null) self::$instance = new self;
		
		return self::$instance;
	}//getInstance
	
	
/*****************************************************
 * Inits remote with mite account data and builds general request header
 *
 * @param string $s_apiKey
 * @param string $s_accountUrl
 * 
 * @throws EXCEPTION_MISSING_ACCOUNT_DATA
 */	
	public function init($s_apiKey, $s_accountUrl, $s_userAgent = false, $b_useSSLSocket = true) {
		
		if (!$s_apiKey || !$s_accountUrl) {
			throw new Exception('Error: Api key or account URL were missing!',
								self::EXCEPTION_MISSING_ACCOUNT_DATA);
			exit;
		}
		
		if (!$s_userAgent) {
		    $s_userAgent = "mite.php/v".self::MITE_PHP_VERSION;
		}
		
		$this->i_port = 80;
		$this->s_sslPrefix = '';
		$this->s_miteAccountUrl = urlencode($s_accountUrl).".".self::MITE_DOMAIN;
		
		if ($b_useSSLSocket) {
			$this->i_port = 443;
			$this->s_protocolPrefix = "ssl://";
		}
		
		$this->s_header = "Host: ".$this->s_miteAccountUrl."\r\n".
						  "X-MiteApiKey: ".$s_apiKey."\r\n".
                   		  "Content-type: application/xml\r\n".
                   		  "User-Agent: ".$s_userAgent."\r\n";
		
	}//init
	
	
/*****************************************************
 * Sends a request to mite and stores possible response data in $this->o_responseXml
 *
 * @param string $s_httpMethod 'post', 'get', 'delete', 'put'
 * @param string $s_rsrcName; e.g. '/time_entries.xml', '/projects.xml', '/time_entries/12345.xml'
 * @param string $s_requestData data for POST or PUT request
 * 
 * @throws 
 * - EXCEPTION_UNPARSABLE_XML
 * - EXCEPTION_WRONG_REQUEST_TYPE
 * - EXCEPTION_CONNECTION_REFUSED
 * - EXCEPTION_RSRC_NOT_FOUND
 * - EXCEPTION_NO_ACCESS
 * - EXCEPTION_TIMED_OUT
 * - EXCEPTION_NO_SERVER_RESPONSE
 * - EXCEPTION_UNEXPECTED_RESPONSE
 * 
 * @return object (simplexml) is empty when using method 'delete' or 'put' 
 * 		    
 */	
	public function sendRequest($s_httpMethod, $s_rsrcName, $s_requestData = '') {
		
	############	
	# VARS 
	#######
	/*
	 * @local objects and resources
	 */	
		$o_context = $o_responseXml = $r_fs = null;
	/*
	 * @local arrays
	 */	
		$a_lastPhpError = $a_rawResponse = $a_response = array();
	/*
	 * @local strings
	 */
		$s_fullUrl = $s_responsePart = $s_responseBody = $s_status = $s_request = $s_protocolPrefix = '';

	############	
	# ACTION 
	#######
		$s_httpMethod = strtoupper($s_httpMethod);
		$s_fullUrl = $this->s_miteAccountUrl.$s_rsrcName;
		
	# begin to form the request	
		$s_request = "$s_httpMethod $s_rsrcName HTTP/1.1\n".
					 $this->s_header;
	<commit_msg>mite.php: Changed HTTP protocol version for requests to 1.0 to avoid problems with chunked transfer encoding<commit_after><?php
/* CLASS mite - last updated 13th January 2011
 * 
 * @description provides methods to communicate with the MITE API
 * @package mite.plugins
 * @author <NAME> <<EMAIL>>
 * @license MIT License
 * 
 * Example usage:
 * -------------
	$o_mite = mite::getInstance();
 	$o_mite->init(<YOUR_API_KEY>,<YOUR_ACCOUNT_SUBDOMAIN>,'<MY_APP_NAME>/v1.2.5');
	try {
 		$o_responseXML = $o_mite->sendRequest('get','/time_entries.xml');
	} catch (Exception $e) {
		echo "<p>".$e->getMessage()."</p>";
	}
 * -------------
 * 
 */

class mite {
	
############
# PROPERTIES
#######	
	private $s_header;
	private $i_port;
	private $s_miteAccountUrl;
	private $s_protocolPrefix;
	
	private static $instance = null;//necessary to act as singleton
	
############
# CONSTANTS
#######	
    const MITE_PHP_VERSION = '1.2';
	const MITE_DOMAIN = 'mite.yo.lk';
	const REQUEST_TIMEOUT = 5;
	const EXCEPTION_RSRC_NOT_FOUND = 100;
	const EXCEPTION_UNPARSABLE_XML = 101;
	const EXCEPTION_WRONG_REQUEST_TYPE = 102;
	const EXCEPTION_MISSING_ACCOUNT_DATA = 103;
	const EXCEPTION_NO_ACCESS = 104;
	const EXCEPTION_TIMED_OUT = 105;
	const EXCEPTION_NO_SERVER_RESPONSE = 106;
	const EXCEPTION_UNEXPECTED_RESPONSE = 107;
	const EXCEPTION_CONNECTION_REFUSED = 108;
	
############	
# METHODS
#######
	
# make silent to act as singleton
	private function __clone() {}
	private function __construct() {}
	
/*****************************************************
 * Returns always the same object instance
 * 
 * @return object miteRemote
 */
	public static function getInstance() {
 
		if (self::$instance === null) self::$instance = new self;
		
		return self::$instance;
	}//getInstance
	
	
/*****************************************************
 * Inits remote with mite account data and builds general request header
 *
 * @param string $s_apiKey
 * @param string $s_accountUrl
 * 
 * @throws EXCEPTION_MISSING_ACCOUNT_DATA
 */	
	public function init($s_apiKey, $s_accountUrl, $s_userAgent = false, $b_useSSLSocket = true) {
		
		if (!$s_apiKey || !$s_accountUrl) {
			throw new Exception('Error: Api key or account URL were missing!',
								self::EXCEPTION_MISSING_ACCOUNT_DATA);
			exit;
		}
		
		if (!$s_userAgent) {
		    $s_userAgent = "mite.php/v".self::MITE_PHP_VERSION;
		}
		
		$this->i_port = 80;
		$this->s_sslPrefix = '';
		$this->s_miteAccountUrl = urlencode($s_accountUrl).".".self::MITE_DOMAIN;
		
		if ($b_useSSLSocket) {
			$this->i_port = 443;
			$this->s_protocolPrefix = "ssl://";
		}
		
		$this->s_header = "Host: ".$this->s_miteAccountUrl."\r\n".
						  "X-MiteApiKey: ".$s_apiKey."\r\n".
                   		  "Content-type: application/xml\r\n".
                   		  "User-Agent: ".$s_userAgent."\r\n";
		
	}//init
	
	
/*****************************************************
 * Sends a request to mite and stores possible response data in $this->o_responseXml
 *
 * @param string $s_httpMethod 'post', 'get', 'delete', 'put'
 * @param string $s_rsrcName; e.g. '/time_entries.xml', '/projects.xml', '/time_entries/12345.xml'
 * @param string $s_requestData data for POST or PUT request
 * 
 * @throws 
 * - EXCEPTION_UNPARSABLE_XML
 * - EXCEPTION_WRONG_REQUEST_TYPE
 * - EXCEPTION_CONNECTION_REFUSED
 * - EXCEPTION_RSRC_NOT_FOUND
 * - EXCEPTION_NO_ACCESS
 * - EXCEPTION_TIMED_OUT
 * - EXCEPTION_NO_SERVER_RESPONSE
 * - EXCEPTION_UNEXPECTED_RESPONSE
 * 
 * @return object (simplexml) is empty when using method 'delete' or 'put' 
 * 		    
 */	
	public function sendRequest($s_httpMethod, $s_rsrcName, $s_requestData = '') {
		
	############	
	# VARS 
	#######
	/*
	 * @local objects and resources
	 */	
		$o_context = $o_responseXml = $r_fs = null;
	/*
	 * @local arrays
	 */	
		$a_lastPhpError = $a_rawResponse = $a_response = array();
	/*
	 * @local strings
	 */
		$s_fullUrl = $s_responsePart = $s_responseBody = $s_status = $s_request = $s_protocolPrefix = '';

	############	
	# ACTION 
	#######
		$s_httpMethod = strtoupper($s_httpMethod);
		$s_fullUrl = $this->s_miteAccountUrl.$s_rsrcName;
		
	# begin to form the request	
		$s_request = "$s_httpMethod $s_rsrcName HTTP/1.0\n".
					 $this->s_header;
	
{'fraction_non_alphanumeric': 0.18979494515975204, 'fraction_numerical': 0.012756318550309967, 'mean_word_length': 3.78005698005698, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '787812', 'n_tokens_mistral': 3284, 'n_tokens_neox': 2958, 'n_words': 607}