<commit_before><?php

require_once (dirname(__FILE__)."/Configuration/DefaultSettings.php");
include_once (dirname(__FILE__).'/Utils/logger/Logger.php');
include_once ($gLoggerConfig);

//Ignore logger errors in production
$currErrorLevel = error_reporting();
error_reporting(0);
Logger::configure($loggerConfiguration);
error_reporting($currErrorLevel);

$logger = Logger::getLogger("main");
$logger->info("Start process");
$logger->info("Request received: ".$_SERVER["REQUEST_URI"]);

$qs       = $_SERVER['QUERY_STRING'];
$main     = new Main();
$timer = new Timer();
$timer->start();
$response = $main->resolveRequest($qs);
print_r($response);
$timer->stop();
$logger->info("Finish process in ".$timer->getTimeMs(). " ms");

class Main {

	function __construct() {
	    $requiredModules = array('/lib', '/lib/objects', '/lib/objects/custom', '/Utils', '/lib/kaltura_client_v3', '/lib/kaltura_client_v3/KalturaPlugins');
	    foreach ($requiredModules as $requiredModule) {
	        $this->loadModules($requiredModule);
	    }
	}

	function loadModules($folderName) {
		// load all plugins
		$pluginsFolder = realpath(dirname(__FILE__)).$folderName;
		if (is_dir($pluginsFolder)) {
			$dir = dir($pluginsFolder);
			while (false !== $fileName = $dir->read()) {
				$matches = null;
				if (preg_match('/^([^.]+).php$/', $fileName, $matches)) {
					require_once ("$pluginsFolder/$fileName");
				}
			}
		}
	}

	function resolveRequest($request) {
	    $logger = Logger::getLogger("main");
	    $method = $_SERVER['REQUEST_METHOD'];
	    if ($method == "POST"){
	        $rawInput = file_get_contents('php://input');
	        parse_str(urldecode($rawInput), $tokens);
	        $tokens["service"] = "multirequest";
	    } else {
	        parse_str(urldecode($request), $tokens);
	    }

		$service = isset($tokens["service"]) ? $tokens["service"] : "";
		if (!empty($service) && isset($service) && class_exists($service, false)) {
			$logger->info("Request service ".$service);
			$serviceHandler = call_user_func(array(ucfirst($service), 'getClass'));
			if ($serviceHandler->isValidService($tokens)){
			    $response = $serviceHandler->run($tokens);
			} else {
                $response = new stdClass;
            }

            if (isset($tokens["callback"])){
                return $tokens["callback"]."(".json_encode($response, true).");";
            } else {
                if ($serviceHandler->requireSerialization){
                    $response = @serialize($response);
                }
                return $response;
            }
		} else {
		    $logger->warn("Tries to request service ".$service." and service wasn't found!");
			return array("message" => "service not found!");
		}
	}
}
?><commit_msg>Support JSON response<commit_after><?php

require_once (dirname(__FILE__)."/Configuration/DefaultSettings.php");
include_once (dirname(__FILE__).'/Utils/logger/Logger.php');
include_once ($gLoggerConfig);

//Ignore logger errors in production
$currErrorLevel = error_reporting();
error_reporting(0);
Logger::configure($loggerConfiguration);
error_reporting($currErrorLevel);

$logger = Logger::getLogger("main");
$logger->info("Start process");
$logger->info("Request received: ".$_SERVER["REQUEST_URI"]);

$qs       = $_SERVER['QUERY_STRING'];
$main     = new Main();
$timer = new Timer();
$timer->start();
$response = $main->resolveRequest($qs);
print_r($response);
$timer->stop();
$logger->info("Finish process in ".$timer->getTimeMs(). " ms");

class Main {

	function __construct() {
	    $requiredModules = array('/lib', '/lib/objects', '/lib/objects/custom', '/Utils', '/lib/kaltura_client_v3', '/lib/kaltura_client_v3/KalturaPlugins');
	    foreach ($requiredModules as $requiredModule) {
	        $this->loadModules($requiredModule);
	    }
	}

	function loadModules($folderName) {
		// load all plugins
		$pluginsFolder = realpath(dirname(__FILE__)).$folderName;
		if (is_dir($pluginsFolder)) {
			$dir = dir($pluginsFolder);
			while (false !== $fileName = $dir->read()) {
				$matches = null;
				if (preg_match('/^([^.]+).php$/', $fileName, $matches)) {
					require_once ("$pluginsFolder/$fileName");
				}
			}
		}
	}

	function resolveRequest($request) {
	    $logger = Logger::getLogger("main");
	    $method = $_SERVER['REQUEST_METHOD'];
	    if ($method == "POST"){
	        $rawInput = file_get_contents('php://input');
	        parse_str(urldecode($rawInput), $tokens);
	        $tokens["service"] = "multirequest";
	    } else {
	        parse_str(urldecode($request), $tokens);
	    }

		$service = isset($tokens["service"]) ? $tokens["service"] : "";
		if (!empty($service) && isset($service) && class_exists($service, false)) {
			$logger->info("Request service ".$service);
			$serviceHandler = call_user_func(array(ucfirst($service), 'getClass'));
			if ($serviceHandler->isValidService($tokens)){

			    $response = $serviceHandler->run($tokens);
			} else {
                $response = new stdClass;
            }

            if (isset($tokens["callback"])){
                return $tokens["callback"]."(".json_encode($response, true).");";
            }
            else if(isset($tokens["format"]) && $tokens["format"] == 1){
                return json_encode($response, true);
            }
            else {
                if ($serviceHandler->requireSerialization){
                    $response = @serialize($response);
                }
                return $response;
            }
		} else {
		    $logger->warn("Tries to request service ".$service." and service wasn't found!");
			return array("message" => "service not found!");
		}
	}
}
?>
{'fraction_non_alphanumeric': 0.1846838824577026, 'fraction_numerical': 0.001246660730186999, 'mean_word_length': 3.4856230031948883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '2901797', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1640, 'n_words': 305}