<commit_before><?php
	/**
	* 
	*/
	class ProxyRequest
	{
		private $response;
		private $logger;

		function __construct($service, $urlTokens){
		    $this->logger = Logger::getLogger("proxy");
			$this->config = $this->getConfig();
			foreach($this->config as $config){
				if (in_array($service, $config["services"])){

				    $this->logger->debug("Found request service ".$service." in config services");
				    if (isset($urlTokens[$config["token"]])){
				        $this->logger->debug("Found request service token ".$config["token"]." in request");
				        if ($config["decodeToken"] == "true"){
						    $this->partnerRequestData = json_decode($urlTokens[$config["token"]]);
						} else {
						    $this->partnerRequestData = $urlTokens[$config["token"]];
						}
						$this->get($config["type"], $config["method"], $config["redirectTo"], $this->partnerRequestData);
						DataStore::getInstance()->setData("request", "", json_decode(json_encode($this->partnerRequestData), true));
						$this->setData($config["dataStores"]);
					}
				}	
			}
		}

		function getConfig(){
		    global $gProxyConfig;
			$data = file_get_contents($gProxyConfig, FILE_USE_INCLUDE_PATH);
			return json_decode($data, TRUE);
		}

		function getDtoConfig($dtoName) {
			$dataStoreName = 'DTO_'.$dtoName;//strtolower(get_class($this));
			//if (!apc_exists($dataStoreName)) {
				//echo "Load from file: ".$dataStoreName;
				$data = file_get_contents('./TVinci/'.$dtoName/*strtolower(get_class($this))*/.".json", FILE_USE_INCLUDE_PATH);
				//apc_store($dataStoreName, $data);
			//} else {
			//	echo "Load from cache: ".$dataStoreName;
			//	$data = apc_fetch($dataStoreName);
			//}

			return json_decode($data, TRUE);
		}
	
		function get($type, $method, $url, $params){
            $this->logger->info("Routing request to ".$url);

            $data = json_encode($this->objectToArray($params), true);

            $this->logger->debug("Routing type: ".$type.", method: ".$method);
            $this->logger->debug("Routing request with params=". $data);

            $timer = new Timer();
            $timer->start();
			switch($type){
			    case "file":
			        $result = $this->getFile($method, $url, $data);
                    break;
			    case "rest":
			        $result = $this->getRest($method, $url, $data);
                    break;
			}
            $this->response = json_decode($result, true);

            if (empty($this->response) ||
                (is_array($this->response) && count($this->response) == 0)){
                $this->logger->warn("Response is empty");
            }

            $this->logger->debug("Response=". $result);
            $timer->stop();
            $this->logger->info("Response time = ".$timer->getTimeMs(). " ms");
		}

		function setData($dataStores){
		    $this->logger->info("Set response data in containers");
			foreach ($dataStores as $dataStore => $container) {
			    $this->logger->debug("Set ".$dataStore." data in container ". $container);
				DataStore::getInstance()->setData($dataStore, $container, $this->response);
			}
		}

		function getRest($method, $url, $data = "")
        {
            $curl = curl_init();

            switch ($method)
            {
                case "POST":
                    curl_setopt($curl, CURLOPT_POST, 1);

                    if ($data){
                        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                    }

                    break;
                case "PUT":
                    curl_setopt($curl, CURLOPT_PUT, 1);
                    break;
                default:
                    if ($data)
                        $url = sprintf("%s?%s", $url, http_build_query($data));
            }

            // Optional Authentication:
            //curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            //curl_setopt($curl, CURLOPT_USERPWD, "<PASSWORD>:password");

            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_HEADER, true );
            curl_setopt($curl, CURLOPT_USERAGENT, isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '' );
            $ip = $this->getIp();
            if ($ip != ""){
                curl_setopt($curl, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "X_FORWARDED_FOR: $ip", "Expect:"));
            }

            $response = curl_exec( $curl );
            $response = preg_split( '/([\r\n][\r\n])\\1/', $response, 2 );

            list( $headers, $contents ) = $response;
            $this->logger->debug("Response headers=". $headers);
            $headers = preg_split( '/[\r\n]+/', $headers );

            foreach($headers as $header) {
                if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) {
                    header($header);
                }
            }

            return $contents;
		}

		function getIp(){
            $ip = '';
            $http_headers = getallheaders();
            if (isset($http_headers['X-KALTURA-REMOTE-ADDR'])){
                $tempList = explode(',', $http_headers['X-KALTURA-REMOTE-ADDR']);
                $ip = $tempList[0];
                $this->logger->debug("Request origin IP=". $ip);
            } elseif (isset($http_headers['X_KALTURA_REMOTE_ADDR'])){
                $tempList = explode(',', $http_headers['X_KALTURA_REMOTE_ADDR']);
                $ip = $tempList[0];
                $this->logger->debug("Request origin IP=". $ip);
            }else {
                $this->logger->warn('Could not retrieve origin IP');<commit_msg>Fix XFF header for ngix<commit_after><?php
	/**
	* 
	*/
	class ProxyRequest
	{
		private $response;
		private $logger;

		function __construct($service, $urlTokens){
		    $this->logger = Logger::getLogger("proxy");
			$this->config = $this->getConfig();
			foreach($this->config as $config){
				if (in_array($service, $config["services"])){

				    $this->logger->debug("Found request service ".$service." in config services");
				    if (isset($urlTokens[$config["token"]])){
				        $this->logger->debug("Found request service token ".$config["token"]." in request");
				        if ($config["decodeToken"] == "true"){
						    $this->partnerRequestData = json_decode($urlTokens[$config["token"]]);
						} else {
						    $this->partnerRequestData = $urlTokens[$config["token"]];
						}
						$this->get($config["type"], $config["method"], $config["redirectTo"], $this->partnerRequestData);
						DataStore::getInstance()->setData("request", "", json_decode(json_encode($this->partnerRequestData), true));
						$this->setData($config["dataStores"]);
					}
				}	
			}
		}

		function getConfig(){
		    global $gProxyConfig;
			$data = file_get_contents($gProxyConfig, FILE_USE_INCLUDE_PATH);
			return json_decode($data, TRUE);
		}

		function getDtoConfig($dtoName) {
			$dataStoreName = 'DTO_'.$dtoName;//strtolower(get_class($this));
			//if (!apc_exists($dataStoreName)) {
				//echo "Load from file: ".$dataStoreName;
				$data = file_get_contents('./TVinci/'.$dtoName/*strtolower(get_class($this))*/.".json", FILE_USE_INCLUDE_PATH);
				//apc_store($dataStoreName, $data);
			//} else {
			//	echo "Load from cache: ".$dataStoreName;
			//	$data = apc_fetch($dataStoreName);
			//}

			return json_decode($data, TRUE);
		}
	
		function get($type, $method, $url, $params){
            $this->logger->info("Routing request to ".$url);

            $data = json_encode($this->objectToArray($params), true);

            $this->logger->debug("Routing type: ".$type.", method: ".$method);
            $this->logger->debug("Routing request with params=". $data);

            $timer = new Timer();
            $timer->start();
			switch($type){
			    case "file":
			        $result = $this->getFile($method, $url, $data);
                    break;
			    case "rest":
			        $result = $this->getRest($method, $url, $data);
                    break;
			}
            $this->response = json_decode($result, true);

            if (empty($this->response) ||
                (is_array($this->response) && count($this->response) == 0)){
                $this->logger->warn("Response is empty");
            }

            $this->logger->debug("Response=". $result);
            $timer->stop();
            $this->logger->info("Response time = ".$timer->getTimeMs(). " ms");
		}

		function setData($dataStores){
		    $this->logger->info("Set response data in containers");
			foreach ($dataStores as $dataStore => $container) {
			    $this->logger->debug("Set ".$dataStore." data in container ". $container);
				DataStore::getInstance()->setData($dataStore, $container, $this->response);
			}
		}

		function getRest($method, $url, $data = "")
        {
            $curl = curl_init();

            switch ($method)
            {
                case "POST":
                    curl_setopt($curl, CURLOPT_POST, 1);

                    if ($data){
                        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                    }

                    break;
                case "PUT":
                    curl_setopt($curl, CURLOPT_PUT, 1);
                    break;
                default:
                    if ($data)
                        $url = sprintf("%s?%s", $url, http_build_query($data));
            }

            // Optional Authentication:
            //curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            //curl_setopt($curl, CURLOPT_USERPWD, "username:password");

            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_HEADER, true );
            curl_setopt($curl, CURLOPT_USERAGENT, isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '' );
            $ip = $this->getIp();
            if ($ip != ""){
                curl_setopt($curl, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "X_FORWARDED_FOR: $ip", "X-Forwarded-For: $ip", "Expect:"));
            }

            $response = curl_exec( $curl );
            $response = preg_split( '/([\r\n][\r\n])\\1/', $response, 2 );

            list( $headers, $contents ) = $response;
            $this->logger->debug("Response headers=". $headers);
            $headers = preg_split( '/[\r\n]+/', $headers );

            foreach($headers as $header) {
                if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) {
                    header($header);
                }
            }

            return $contents;
		}

		function getIp(){
            $ip = '';
            $http_headers = getallheaders();
            if (isset($http_headers['X-KALTURA-REMOTE-ADDR'])){
                $tempList = explode(',', $http_headers['X-KALTURA-REMOTE-ADDR']);
                $ip = $tempList[0];
                $this->logger->debug("Request origin IP=". $ip);
            } elseif (isset($http_headers['X_KALTURA_REMOTE_ADDR'])){
                $tempList = explode(',', $http_headers['X_KALTURA_REMOTE_ADDR']);
                $ip = $tempList[0];
                $this->logger->debug("Request origin IP=". $ip);
            }else {
                $this->logger->warn('Could not retrieve origin IP');
{'fraction_non_alphanumeric': 0.17584070796460177, 'fraction_numerical': 0.001415929203539823, 'mean_word_length': 2.191471335780853, 'pattern_counts': {'":': 8, '<': 6, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3569974', 'n_tokens_mistral': 3803, 'n_tokens_neox': 3314, 'n_words': 595}