<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->getWidgetId() . '_' . 
			   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 );
				}
			}
		}
		// set output from cache file flag: ( if no exception was thrown ) 
		$this->outputFromCache = true;
		
		$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( is_array( $rawResultObject ) && isset( $rawResultObject['code'] ) ) {
			$this->setError( $rawResultObject );
			return null;
		}
		
		if( isset( $rawResultObject->confFile ) ){
			return $this->cleanUiConf( $rawResultObject->confFile );
		}
		
	}
	
	/* 
	 * 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!
		$brokenFlashVarXMl =  'autoPlay=false&screensLayer.startScreenOverId=startScreen&screensLayer.startScreenId=startScreen';
		$uiConf = str_replace( $brokenFlashVarXMl, htmlentities( $brokenFlashVarXMl ), $uiConf );
		// handle any non-escapsed &
		$uiConf = preg_replace('/&[^; ]{0,6}.?/e', "((substr('\\0',-1) == ';') ? '\\0' : '&amp;'.substr('\\0',1))", $uiConf);

		return $uiConf;
	}
	/*
	 * Parse uiConf XML
	 */
	public function parseUiConfXML( $uiConf ){
		if( $uiConf == '' ) {
			// Empty uiConf ( don't try and parse, return an empty object)
			return new SimpleXMLElement('<xml />' );
		}
		/*
		libxml_use_internal_errors(true);
		$sxe = simplexml_load_string($uiConf);
		if (!$sxe) {
			echo "Failed loading XML\n";
			foreach(libxml_get_errors() as $error) {
				echo "\t", $error->message;
			}
		}
		*/
		$this->uiConfXml = new SimpleXMLElement( $uiConf );
	}
	public function getUiConf() {
		if( ! $this->uiConfFile ) {
			$this->loadUiConf();
		}
		return $this->uiConfFile;
	}
	public function getUiConfXML() {
		if( !$this->uiConfXml ){
			$this->loadUiConf();
		}
		return $this->uiConfXml;
	}	
	
	/* setupPlayerConfig()
	 * Creates an array of our player configuration.
	 * The array is build from: Flashvars, uiVars, uiConf
	 * The array include 2 arrays:
	 * plugins - contains all of our plugins configuration
	 * vars - contain flat player configuration
	 */
	function setupPlayerConfig() {

		$plugins = array();
		$vars = array();

		// Get all plugins elements
		if( $this->uiConfFile ) {
			$pluginsXml = $this->getUiConfXML()->xpath("*//*[@id]");
			for( $i=0; $i < count($pluginsXml); $i++ ) {
				$pluginId = (string) $pluginsXml[ $i ]->attributes()->id;
				// Enforce the lower case first letter of plugin convention: 
				if ( isset( $pluginId[0] ) ) {
					$pluginId = strtolower( $pluginId[0] ) . substr( $pluginId, 1 );
				}
				$plugins[ $pluginId ] = array(
					'plugin' => true
				);
				foreach( $pluginsXml[ $i ]->attributes() as $key => $value) {
					if( $key == "id" ) {
						continue;
					}
					$plugins[ $pluginId ][ $key ] = $this->formatString( (string) $value );
				}
			}
		}
		
		// Strings
		if( $this->uiConfFile ) {
			$uiStrings = $this->getUiConfXML()->xpath("*//string");
			for( $i=0; $i < count($uiStrings); $i++ ) {
				$key = ( string ) $uiStrings[ $i ]->attributes()->key;
				$value = ( string ) $uiStrings[ $i ]->attributes()->value;
				$locale = '';
				if( $uiStrings[ $i ]->attributes()->locale ){
					$locale = ( string ) $uiStrings[ $i ]->attributes()->locale;
				}
				
				
				// setup string s plugin: 
				if( !isset( $plugins[ 'strings' ] ) ){
					$plugins[ 'strings' ] = array ();
				}
				// add the current key value pair: 
				$plugins[ 'strings' ][ $locale . '_' . $key ] = $value;<commit_msg>append '_' to seperate locale from key in correct location<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->getWidgetId() . '_' . 
			   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 );
				}
			}
		}
		// set output from cache file flag: ( if no exception was thrown ) 
		$this->outputFromCache = true;
		
		$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( is_array( $rawResultObject ) && isset( $rawResultObject['code'] ) ) {
			$this->setError( $rawResultObject );
			return null;
		}
		
		if( isset( $rawResultObject->confFile ) ){
			return $this->cleanUiConf( $rawResultObject->confFile );
		}
		
	}
	
	/* 
	 * 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!
		$brokenFlashVarXMl =  'autoPlay=false&screensLayer.startScreenOverId=startScreen&screensLayer.startScreenId=startScreen';
		$uiConf = str_replace( $brokenFlashVarXMl, htmlentities( $brokenFlashVarXMl ), $uiConf );
		// handle any non-escapsed &
		$uiConf = preg_replace('/&[^; ]{0,6}.?/e', "((substr('\\0',-1) == ';') ? '\\0' : '&amp;'.substr('\\0',1))", $uiConf);

		return $uiConf;
	}
	/*
	 * Parse uiConf XML
	 */
	public function parseUiConfXML( $uiConf ){
		if( $uiConf == '' ) {
			// Empty uiConf ( don't try and parse, return an empty object)
			return new SimpleXMLElement('<xml />' );
		}
		/*
		libxml_use_internal_errors(true);
		$sxe = simplexml_load_string($uiConf);
		if (!$sxe) {
			echo "Failed loading XML\n";
			foreach(libxml_get_errors() as $error) {
				echo "\t", $error->message;
			}
		}
		*/
		$this->uiConfXml = new SimpleXMLElement( $uiConf );
	}
	public function getUiConf() {
		if( ! $this->uiConfFile ) {
			$this->loadUiConf();
		}
		return $this->uiConfFile;
	}
	public function getUiConfXML() {
		if( !$this->uiConfXml ){
			$this->loadUiConf();
		}
		return $this->uiConfXml;
	}	
	
	/* setupPlayerConfig()
	 * Creates an array of our player configuration.
	 * The array is build from: Flashvars, uiVars, uiConf
	 * The array include 2 arrays:
	 * plugins - contains all of our plugins configuration
	 * vars - contain flat player configuration
	 */
	function setupPlayerConfig() {

		$plugins = array();
		$vars = array();

		// Get all plugins elements
		if( $this->uiConfFile ) {
			$pluginsXml = $this->getUiConfXML()->xpath("*//*[@id]");
			for( $i=0; $i < count($pluginsXml); $i++ ) {
				$pluginId = (string) $pluginsXml[ $i ]->attributes()->id;
				// Enforce the lower case first letter of plugin convention: 
				if ( isset( $pluginId[0] ) ) {
					$pluginId = strtolower( $pluginId[0] ) . substr( $pluginId, 1 );
				}
				$plugins[ $pluginId ] = array(
					'plugin' => true
				);
				foreach( $pluginsXml[ $i ]->attributes() as $key => $value) {
					if( $key == "id" ) {
						continue;
					}
					$plugins[ $pluginId ][ $key ] = $this->formatString( (string) $value );
				}
			}
		}
		
		// Strings
		if( $this->uiConfFile ) {
			$uiStrings = $this->getUiConfXML()->xpath("*//string");
			for( $i=0; $i < count($uiStrings); $i++ ) {
				$key = ( string ) $uiStrings[ $i ]->attributes()->key;
				$value = ( string ) $uiStrings[ $i ]->attributes()->value;
				$locale = '';
				if( $uiStrings[ $i ]->attributes()->locale ){
					// append '_' to seperate locale from key 
					$locale = ( string ) $uiStrings[ $i ]->attributes()->locale . '_'; 
				}
				
				
				// setup string s plugin: 
				if( !isset( $plugins[ 'strings' ] ) ){
					$plugins[ 'strings' ] = array ();
				}
				// add the current key value pair: 
				$plugins[ 'strings' ][ $locale . $key ] = $value;
{'fraction_non_alphanumeric': 0.17707231040564375, 'fraction_numerical': 0.003527336860670194, 'mean_word_length': 3.2812382030955076, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 133, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '2642523', 'n_tokens_mistral': 4505, 'n_tokens_neox': 3885, 'n_words': 911}