<commit_before><?php

/**
 * Description of KalturaUiConfResult
 *
 * @author ran
 */
require_once(  dirname( __FILE__ ) . '/KalturaResultObject.php');
class KalturaUiConfResult extends KalturaResultObject {

	var $uiConfFile = null;
	var $uiConfXml = null; 
	var $playerConfig = null;
	
	function __construct($clientTag = 'php') {
		parent::__construct($clientTag);
		$this->loadUiConf();
		
		$params = $this->getUrlParameters();
		if( ! $params['entry_id'] && ! isset( $params['flashvars']['referenceId'] ) ) {
			$this->error = parent::NO_ENTRY_ID_FOUND;
		}
	}
	function getCacheFilePath() {
		// Add entry id, cache_st and referrer
		$playerUnique = $this->getUiConfId() . $this->getCacheSt() . $this->getReferer();
		$cacheKey = substr( md5( $this->getServiceConfig( 'ServiceUrl' )  ), 0, 5 ) . '_' . $this->getPartnerId() . '_' . 
			   substr( md5( $playerUnique ), 0, 20 );
		
		return $this->getCacheDir() . '/' . $cacheKey . ".uiconf.txt";
	}	
	
	function loadUiConf() {
		// If no uiconf_id .. throw exception
		if( ! $this->getUiConfId() ) {
			throw new Exception( "Missing uiConf ID" );
		}
		// Check if we have a cached result object:
		if( !$this->uiConfFile ){
			$cacheFile = $this->getCacheFilePath();
			if( $this->canUseCacheFile( $cacheFile ) ){
				$this->uiConfFile = file_get_contents( $cacheFile );
			} else {
				$this->uiConfFile = $this->loadUiConfFromApi();
				if( $this->uiConfFile !== null ) {
					$this->putCacheFile( $cacheFile, $this->uiConfFile );
				} else {
					throw new Exception( $this->error );
				}
			}
		}
		$this->parseUiConfXML( $this->uiConfFile );
		$this->setupPlayerConfig();
	}

	function loadUiConfFromApi() {
		$client = $this->getClient();
		$kparams = array();
		try {
			if( $this->noCache ) {
				$client->addParam( $kparams, "nocache",  true );
			}
			$client->addParam( $kparams, "id",  $this->urlParameters['uiconf_id'] );
			$client->queueServiceActionCall( "uiconf", "get", $kparams );

			$rawResultObject = $client->doQueue();
		} catch( Exception $e ){
			// Update the Exception and pass it upward
			throw new Exception( KALTURA_GENERIC_SERVER_ERROR . "\n" . $e->getMessage() );
		}
		
		if( isset( $rawResultObject['code'] ) ) {
			$this->setError( $rawResultObject );
		}
		if( isset( $rawResultObject->confFile ) ){
			return $this->cleanUiConf( $rawResultObject->confFile );
		}
		return null;
	}
	
	/* 
	 * Cleans up uiConf XML from bad format
	 * Hopefully we could remove this function in the future
	 */
	public function cleanUiConf( $uiConf ) {
		// remove this hack as soon as possible
		$uiConf = str_replace( '[kClick="', 'kClick="', $uiConf);
		// remove this hack as soon as possible as well!<commit_msg>$rawResultObject is not an array<commit_after><?php

/**
 * Description of KalturaUiConfResult
 *
 * @author ran
 */
require_once(  dirname( __FILE__ ) . '/KalturaResultObject.php');
class KalturaUiConfResult extends KalturaResultObject {

	var $uiConfFile = null;
	var $uiConfXml = null; 
	var $playerConfig = null;
	
	function __construct($clientTag = 'php') {
		parent::__construct($clientTag);
		$this->loadUiConf();
		
		$params = $this->getUrlParameters();
		if( ! $params['entry_id'] && ! isset( $params['flashvars']['referenceId'] ) ) {
			$this->error = parent::NO_ENTRY_ID_FOUND;
		}
	}
	function getCacheFilePath() {
		// Add entry id, cache_st and referrer
		$playerUnique = $this->getUiConfId() . $this->getCacheSt() . $this->getReferer();
		$cacheKey = substr( md5( $this->getServiceConfig( 'ServiceUrl' )  ), 0, 5 ) . '_' . $this->getPartnerId() . '_' . 
			   substr( md5( $playerUnique ), 0, 20 );
		
		return $this->getCacheDir() . '/' . $cacheKey . ".uiconf.txt";
	}	
	
	function loadUiConf() {
		// If no uiconf_id .. throw exception
		if( ! $this->getUiConfId() ) {
			throw new Exception( "Missing uiConf ID" );
		}
		// Check if we have a cached result object:
		if( !$this->uiConfFile ){
			$cacheFile = $this->getCacheFilePath();
			if( $this->canUseCacheFile( $cacheFile ) ){
				$this->uiConfFile = file_get_contents( $cacheFile );
			} else {
				$this->uiConfFile = $this->loadUiConfFromApi();
				if( $this->uiConfFile !== null ) {
					$this->putCacheFile( $cacheFile, $this->uiConfFile );
				} else {
					throw new Exception( $this->error );
				}
			}
		}
		$this->parseUiConfXML( $this->uiConfFile );
		$this->setupPlayerConfig();
	}

	function loadUiConfFromApi() {
		$client = $this->getClient();
		$kparams = array();
		try {
			if( $this->noCache ) {
				$client->addParam( $kparams, "nocache",  true );
			}
			$client->addParam( $kparams, "id",  $this->urlParameters['uiconf_id'] );
			$client->queueServiceActionCall( "uiconf", "get", $kparams );

			$rawResultObject = $client->doQueue();
		} catch( Exception $e ){
			// Update the Exception and pass it upward
			throw new Exception( KALTURA_GENERIC_SERVER_ERROR . "\n" . $e->getMessage() );
		}
		
		if( isset( $rawResultObject->code ) ) {
			$this->setError( $rawResultObject );
		}
		if( isset( $rawResultObject->confFile ) ){
			return $this->cleanUiConf( $rawResultObject->confFile );
		}
		return null;
	}
	
	/* 
	 * Cleans up uiConf XML from bad format
	 * Hopefully we could remove this function in the future
	 */
	public function cleanUiConf( $uiConf ) {
		// remove this hack as soon as possible
		$uiConf = str_replace( '[kClick="', 'kClick="', $uiConf);
		// remove this hack as soon as possible as well!
{'fraction_non_alphanumeric': 0.16768349047883158, 'fraction_numerical': 0.00258827879460159, 'mean_word_length': 3.6003401360544216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 74, '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': '245279', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1845, 'n_words': 419}