<commit_before><?php

// Include the kaltura client
require_once(  dirname( __FILE__ ) . '/kaltura_client_v3/KalturaClient.php' );


/**
 * Generates a kaltura result object based on url Parameters 
 */
class KalturaResultObject {
	 
	var $resultObj = null; // lazy init with getResultObject
	var $clientTag = null;
	var $uiConfXml = null; // lazy init
	var $noCache = false;
	
	// Local flag to store whether output was came from cache or was a fresh request
	private $outputFromCache = false;
	
	/**
	 * Variables set by the Frame request:
	 */
	private $urlParameters = array(
		'cache_st' => null,
		'p' => null,
		'wid' => null,
		'uiconf_id' => null,
		'entry_id' => null,
		'flashvars' => null,
		'playlist_id' => null,
		'urid' => null,
		// Custom service url properties ( only used when wgKalturaAllowIframeRemoteService is set to true ) 
		'ServiceUrl'=> null,
		'ServiceBase'=>null,
		'CdnUrl'=> null,
		'UseManifestUrls' => null
	
	);
	function __construct( $clientTag = 'php'){
		$this->clientTag = $clientTag;
		//parse input:
		$this->parseRequest();
		// load the request object:
		$this->getResultObject();
	}
	function getServiceConfig( $name ){
		global $wgKalturaAllowIframeRemoteService;
		// Check if we allow URL override: 
		if( $wgKalturaAllowIframeRemoteService ){
			// Check for urlParameters
			if( isset( $this->urlParameters[ $name ] ) ){
				return $this->urlParameters[ $name ];
			}
		}
		// Else use the global config: 
		switch( $name ){
			case 'ServiceUrl' : 
				global $wgKalturaServiceUrl;
				return $wgKalturaServiceUrl;
				break;
			case 'ServiceBase':
				global $wgKalturaServiceBase;
				return $wgKalturaServiceBase;
				break;
			case 'CdnUrl':
				global $wgKalturaCDNUrl;
				return $wgKalturaCDNUrl;
				break;
			case 'UseManifestUrls':
				global $wgKalturaUseManifestUrls;
				return $wgKalturaUseManifestUrls;
				break;
		}
		// else thorw an erro? 
	}
	/**
	 * Kaltura object provides sources, sometimes no sources are found or an error occurs in 
	 * a video delivery context we don't want ~nothing~ to happen instead we send a special error
	 * video. 
	 */
	public static function getErrorVideoSources(){
		// @@TODO pull this from config: 'Kaltura.BlackVideoSources' 
		return array(
		    'iphone' => array( 
		    	'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_g18we0u3/flavorId/1_ktavj42z/format/url/protocol/http/a.mp4',
		    	'type' =>'video/h264',
				'data-flavorid' => 'iPhone'
		    ),
		    'ogg' => array(  
		    	'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_g18we0u3/flavorId/1_gtm9gzz2/format/url/protocol/http/a.ogg',
		    	'type' => 'video/ogg',
		    	'data-flavorid' => 'ogg'
		    ),
		    'webm' => array(
		    	'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_g18we0u3/flavorId/1_bqsosjph/format/url/protocol/http/a.webm',
		    	'type' => 'video/webm',
		    	'data-flavorid' => 'webm'
		    ),
		    '3gp' => array( 
		    	'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_g18we0u3/flavorId/1_mfqemmyg/format/url/protocol/http/a.mp4',
		    	'type' => 'video/3gp',
		    	'data-flavorid' => '3gp'
		    )
		 );
	}
	public static function getBlackVideoSources(){
		// @@TODO merge with Kaltura.BlackVideoSources config!!
		return array(
		    'webm' => array(
		        'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_vp5cng42/flavorId/1_oiyfyphl/format/url/protocol/http/a.webm',
		        'type' => 'video/webm',
				'data-flavorid' => 'webm'
			),
			'ogg' => array(
				'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_vp5cng42/flavorId/1_6yqa4nmd/format/url/protocol/http/a.ogg',
				'type' => 'video/ogg',
				'data-flavorid' => 'ogg'
			),
			'iphone' => array(
				'src' =>'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_vp5cng42/flavorId/1_6wf0o9n7/format/url/protocol/http/a.mp4',
				'type' => 'video/h264',
				'data-flavorid' => 'iPhone'
			),
		);
	}
	
	// Check if the requested url is a playlist
	public function isPlaylist(){
		return ( $this->urlParameters['playlist_id'] !== null || $this->urlParameters['entry_id'] === null);
	}
	public function isCachedOutput(){
		return $this->outputFromCache;
	}
	
	private function getUserAgent() {
		return $_SERVER['HTTP_USER_AGENT'];
	}
	/**
	 * Checks if a user agent is restricted via the user restriction plugin present in the uiConf XML
	 * this check is run as part of resultObject handling so we must pass in the uiConf string
	 */ 
	public function isUserAgentRestrictedPlugin( $uiConf ) {
		// Get flashvars
		$flashVars = $this->urlParameters[ 'flashvars' ];
		$restrictedMessage = true;
		
		
		// Check for plugin definition in flashVars
		if( $flashVars && isset($flashVars['restrictUserAgent.plugin']) ) {
			$restrictedStrings = $flashVars['restrictUserAgent.restrictedUserAgents'];
			if( isset($flashVars['restrictUserAgent.restrictedUserAgentTitle']) && isset($flashVars['restrictUserAgent.restrictedUserAgentMessage']) ) {
				$restrictedMessage = $flashVars['restrictUserAgent.restrictedUserAgentTitle'] ."\n". $flashVars['restrictUserAgent.restrictedUserAgentMessage'];
			}
		} else {
			// Use the local uiConfXml object location to avoid re-parsing the uiConf
			// @@TODO clean up getUiConfXMl() method to handle unitialized resultObject state
			if( !$this->uiConfXml ){
				$this->uiConfXml = new SimpleXMLElement( $uiConf );
			}
			// Check for plug definition in uiConf
			$restrictUserAgentPlugin = $this->uiConfXml->xpath("*//Plugin[@id = 'restrictUserAgent']");
			if( $restrictUserAgentPlugin ) {
				$restrictUserAgentPlugin = $restrictUserAgentPlugin[0]->attributes();
				$restrictedStrings = $restrictUserAgentPlugin->restrictedUserAgents;
				if( isset($restrictUserAgentPlugin->restrictedUserAgentTitle) && isset($restrictUserAgentPlugin->restrictedUserAgentMessage) ) {
					$restrictedMessage = $restrictUserAgentPlugin->restrictedUserAgentTitle . "\n" . $restrictUserAgentPlugin->restrictedUserAgentMessage;
				}
			}else {
				return false;
			}
		}
		
		// If we don't have any string to search for, return true
		if( !isset($restrictedStrings) || empty($restrictedStrings) ) { return false; }

		// Lower case user agents string
		$userAgent = strtolower( $this->getUserAgent() );
		$restrictedStrings = strtolower( $restrictedStrings );
		$restrictedStrings = explode(",", $restrictedStrings);
		
		foreach( $restrictedStrings as $string ) {
			$string = str_replace(".*", "", $string); // Removes .*
			$string = trim($string);
			if( ! strpos( $userAgent, $string ) === false ) {
				return $restrictedMessage;
			}
		}
		return false;
	}

	public function getSourceForUserAgent( $sources = null, $userAgent = false ){
		// Get all sources
		if( !$sources ){
			$sources = $this->getSources();
		}
		// Get user agent
		if( !$userAgent ){
			$userAgent = $this->getUserAgent();
		}
	
		//@@TODO integrate a library for doing this user-agent -> source selection
		// what follows somewhat arbitrary
		
		$flavorUrl = false ;
		// First set the most compatible source ( iPhone h.264 )
		if( isset( $sources['iphone'] ) ) {
			$flavorUrl = $sources['iphone']['src'];
		}
		// h264 for iPad
		if( isset( $sources['ipad'] ) ) {
			$flavorUrl = $sources['ipad']['src'];
		}
		// rtsp3gp for BlackBerry
		if( strpos( $userAgent, 'BlackBerry' ) !== false && $sources['rtsp3gp'] ){
			return 	$sources['rtsp3gp']['src'];
		}
		
		// 3gp check 
		if( isset( $sources['3gp'] ) ) {
			// Blackberry ( newer blackberry's can play the iPhone src but better safe than broken )
			if( strpos( $userAgent, 'BlackBerry' ) !== false ){
				$flavorUrl = $sources['3gp']['src'];
			}
			// if we have no iphone source then do use 3gp:
			if( !$flavorUrl ){
				$flavorUrl = $sources['3gp']['src'];
			}
		}
		
		// Firefox > 3.5 and chrome support ogg
		if( isset( $sources['ogg'] ) ){
			// chrome supports ogg:
			if( strpos( $userAgent, 'Chrome' ) !== false ){
				$flavorUrl = $sources['ogg']['src'];
			}
			// firefox 3.5 and greater supported ogg:
			if( strpos( $userAgent, 'Firefox' ) !== false ){
				$flavorUrl = $sources['ogg']['src'];
			}
		}
		
		// Firefox 4x and chrome support webm ( use after ogg )
		if( isset( $sources['webm'] ) ){
			if( strpos( $userAgent, 'Chrome' ) !== false ){
				$flavorUrl = $sources['webm']['src'];
			}
			if( strpos( $userAgent, 'Firefox/4' ) !== false
				||
				strpos( $userAgent, 'Firefox/5' ) !== false 
			){
				$flavorUrl = $sources['webm']['src'];
			}
		}
		
		return $flavorUrl;
	}
	
	/**
	*  Access Control Handling
	*/
	public function isAccessControlAllowed( &$resultObject = null ) {
		// Kaltura only has entry level access control not playlist level access control atm: 
		// don't check playlist
		if( $this->isPlaylist() ){
			return true;
		}
		
		if( !$resultObject ){
			$resultObject =  $this->getResultObject();
		}
		
		$accessControl = $resultObject['accessControl'];
		
		// Check if we had no access control due to playlist
		if( is_array( $accessControl ) && isset( $accessControl['code'] )){
			// Error ? .. should do better error checking.
			// errors we have seen so far: 
				//$accessControl['code'] == 'MISSING_MANDATORY_PARAMETER'
				//$accessControl['code'] == 'INTERNAL_SERVERL_ERROR'  
			return true;
		}
		
		// Checks if admin
		if( $accessControl->isAdmin ) {
			return true;
		}

		/* Domain Name Restricted */
		if( $accessControl->isSiteRestricted ) {
			return "Un authorized domain\nWe're sorry, this content is only available on certain domains.";
		}

		/* Country Restricted */
		if( $accessControl->isCountryRestricted) {
			return "Un authorized country\nWe're sorry, this content is only available on certain countries.";
		}

		/* IP Address Restricted */
		if( $accessControl->isIpAddressRestricted) {
			return "Un authorized IP address\nWe're sorry, this content is only available for ceratin IP addresses.";
		}

		/* Session Restricted */
		if( $accessControl->isSessionRestricted && $accessControl->previewLength == -1 ) {
			return "No KS where KS is required\nWe're sorry, access to this content is restricted.";
		}

		if( $accessControl->isScheduledNow === 0) {
			return "Out of scheduling\nWe're sorry, this content is currently unavailable.";<commit_msg>fixed typo in geo restriction message <commit_after><?php

// Include the kaltura client
require_once(  dirname( __FILE__ ) . '/kaltura_client_v3/KalturaClient.php' );


/**
 * Generates a kaltura result object based on url Parameters 
 */
class KalturaResultObject {
	 
	var $resultObj = null; // lazy init with getResultObject
	var $clientTag = null;
	var $uiConfXml = null; // lazy init
	var $noCache = false;
	
	// Local flag to store whether output was came from cache or was a fresh request
	private $outputFromCache = false;
	
	/**
	 * Variables set by the Frame request:
	 */
	private $urlParameters = array(
		'cache_st' => null,
		'p' => null,
		'wid' => null,
		'uiconf_id' => null,
		'entry_id' => null,
		'flashvars' => null,
		'playlist_id' => null,
		'urid' => null,
		// Custom service url properties ( only used when wgKalturaAllowIframeRemoteService is set to true ) 
		'ServiceUrl'=> null,
		'ServiceBase'=>null,
		'CdnUrl'=> null,
		'UseManifestUrls' => null
	
	);
	function __construct( $clientTag = 'php'){
		$this->clientTag = $clientTag;
		//parse input:
		$this->parseRequest();
		// load the request object:
		$this->getResultObject();
	}
	function getServiceConfig( $name ){
		global $wgKalturaAllowIframeRemoteService;
		// Check if we allow URL override: 
		if( $wgKalturaAllowIframeRemoteService ){
			// Check for urlParameters
			if( isset( $this->urlParameters[ $name ] ) ){
				return $this->urlParameters[ $name ];
			}
		}
		// Else use the global config: 
		switch( $name ){
			case 'ServiceUrl' : 
				global $wgKalturaServiceUrl;
				return $wgKalturaServiceUrl;
				break;
			case 'ServiceBase':
				global $wgKalturaServiceBase;
				return $wgKalturaServiceBase;
				break;
			case 'CdnUrl':
				global $wgKalturaCDNUrl;
				return $wgKalturaCDNUrl;
				break;
			case 'UseManifestUrls':
				global $wgKalturaUseManifestUrls;
				return $wgKalturaUseManifestUrls;
				break;
		}
		// else thorw an erro? 
	}
	/**
	 * Kaltura object provides sources, sometimes no sources are found or an error occurs in 
	 * a video delivery context we don't want ~nothing~ to happen instead we send a special error
	 * video. 
	 */
	public static function getErrorVideoSources(){
		// @@TODO pull this from config: 'Kaltura.BlackVideoSources' 
		return array(
		    'iphone' => array( 
		    	'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_g18we0u3/flavorId/1_ktavj42z/format/url/protocol/http/a.mp4',
		    	'type' =>'video/h264',
				'data-flavorid' => 'iPhone'
		    ),
		    'ogg' => array(  
		    	'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_g18we0u3/flavorId/1_gtm9gzz2/format/url/protocol/http/a.ogg',
		    	'type' => 'video/ogg',
		    	'data-flavorid' => 'ogg'
		    ),
		    'webm' => array(
		    	'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_g18we0u3/flavorId/1_bqsosjph/format/url/protocol/http/a.webm',
		    	'type' => 'video/webm',
		    	'data-flavorid' => 'webm'
		    ),
		    '3gp' => array( 
		    	'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_g18we0u3/flavorId/1_mfqemmyg/format/url/protocol/http/a.mp4',
		    	'type' => 'video/3gp',
		    	'data-flavorid' => '3gp'
		    )
		 );
	}
	public static function getBlackVideoSources(){
		// @@TODO merge with Kaltura.BlackVideoSources config!!
		return array(
		    'webm' => array(
		        'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_vp5cng42/flavorId/1_oiyfyphl/format/url/protocol/http/a.webm',
		        'type' => 'video/webm',
				'data-flavorid' => 'webm'
			),
			'ogg' => array(
				'src' => 'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_vp5cng42/flavorId/1_6yqa4nmd/format/url/protocol/http/a.ogg',
				'type' => 'video/ogg',
				'data-flavorid' => 'ogg'
			),
			'iphone' => array(
				'src' =>'http://www.kaltura.com/p/243342/sp/24334200/playManifest/entryId/1_vp5cng42/flavorId/1_6wf0o9n7/format/url/protocol/http/a.mp4',
				'type' => 'video/h264',
				'data-flavorid' => 'iPhone'
			),
		);
	}
	
	// Check if the requested url is a playlist
	public function isPlaylist(){
		return ( $this->urlParameters['playlist_id'] !== null || $this->urlParameters['entry_id'] === null);
	}
	public function isCachedOutput(){
		return $this->outputFromCache;
	}
	
	private function getUserAgent() {
		return $_SERVER['HTTP_USER_AGENT'];
	}
	/**
	 * Checks if a user agent is restricted via the user restriction plugin present in the uiConf XML
	 * this check is run as part of resultObject handling so we must pass in the uiConf string
	 */ 
	public function isUserAgentRestrictedPlugin( $uiConf ) {
		// Get flashvars
		$flashVars = $this->urlParameters[ 'flashvars' ];
		$restrictedMessage = true;
		
		
		// Check for plugin definition in flashVars
		if( $flashVars && isset($flashVars['restrictUserAgent.plugin']) ) {
			$restrictedStrings = $flashVars['restrictUserAgent.restrictedUserAgents'];
			if( isset($flashVars['restrictUserAgent.restrictedUserAgentTitle']) && isset($flashVars['restrictUserAgent.restrictedUserAgentMessage']) ) {
				$restrictedMessage = $flashVars['restrictUserAgent.restrictedUserAgentTitle'] ."\n". $flashVars['restrictUserAgent.restrictedUserAgentMessage'];
			}
		} else {
			// Use the local uiConfXml object location to avoid re-parsing the uiConf
			// @@TODO clean up getUiConfXMl() method to handle unitialized resultObject state
			if( !$this->uiConfXml ){
				$this->uiConfXml = new SimpleXMLElement( $uiConf );
			}
			// Check for plug definition in uiConf
			$restrictUserAgentPlugin = $this->uiConfXml->xpath("*//Plugin[@id = 'restrictUserAgent']");
			if( $restrictUserAgentPlugin ) {
				$restrictUserAgentPlugin = $restrictUserAgentPlugin[0]->attributes();
				$restrictedStrings = $restrictUserAgentPlugin->restrictedUserAgents;
				if( isset($restrictUserAgentPlugin->restrictedUserAgentTitle) && isset($restrictUserAgentPlugin->restrictedUserAgentMessage) ) {
					$restrictedMessage = $restrictUserAgentPlugin->restrictedUserAgentTitle . "\n" . $restrictUserAgentPlugin->restrictedUserAgentMessage;
				}
			}else {
				return false;
			}
		}
		
		// If we don't have any string to search for, return true
		if( !isset($restrictedStrings) || empty($restrictedStrings) ) { return false; }

		// Lower case user agents string
		$userAgent = strtolower( $this->getUserAgent() );
		$restrictedStrings = strtolower( $restrictedStrings );
		$restrictedStrings = explode(",", $restrictedStrings);
		
		foreach( $restrictedStrings as $string ) {
			$string = str_replace(".*", "", $string); // Removes .*
			$string = trim($string);
			if( ! strpos( $userAgent, $string ) === false ) {
				return $restrictedMessage;
			}
		}
		return false;
	}

	public function getSourceForUserAgent( $sources = null, $userAgent = false ){
		// Get all sources
		if( !$sources ){
			$sources = $this->getSources();
		}
		// Get user agent
		if( !$userAgent ){
			$userAgent = $this->getUserAgent();
		}
	
		//@@TODO integrate a library for doing this user-agent -> source selection
		// what follows somewhat arbitrary
		
		$flavorUrl = false ;
		// First set the most compatible source ( iPhone h.264 )
		if( isset( $sources['iphone'] ) ) {
			$flavorUrl = $sources['iphone']['src'];
		}
		// h264 for iPad
		if( isset( $sources['ipad'] ) ) {
			$flavorUrl = $sources['ipad']['src'];
		}
		// rtsp3gp for BlackBerry
		if( strpos( $userAgent, 'BlackBerry' ) !== false && $sources['rtsp3gp'] ){
			return 	$sources['rtsp3gp']['src'];
		}
		
		// 3gp check 
		if( isset( $sources['3gp'] ) ) {
			// Blackberry ( newer blackberry's can play the iPhone src but better safe than broken )
			if( strpos( $userAgent, 'BlackBerry' ) !== false ){
				$flavorUrl = $sources['3gp']['src'];
			}
			// if we have no iphone source then do use 3gp:
			if( !$flavorUrl ){
				$flavorUrl = $sources['3gp']['src'];
			}
		}
		
		// Firefox > 3.5 and chrome support ogg
		if( isset( $sources['ogg'] ) ){
			// chrome supports ogg:
			if( strpos( $userAgent, 'Chrome' ) !== false ){
				$flavorUrl = $sources['ogg']['src'];
			}
			// firefox 3.5 and greater supported ogg:
			if( strpos( $userAgent, 'Firefox' ) !== false ){
				$flavorUrl = $sources['ogg']['src'];
			}
		}
		
		// Firefox 4x and chrome support webm ( use after ogg )
		if( isset( $sources['webm'] ) ){
			if( strpos( $userAgent, 'Chrome' ) !== false ){
				$flavorUrl = $sources['webm']['src'];
			}
			if( strpos( $userAgent, 'Firefox/4' ) !== false
				||
				strpos( $userAgent, 'Firefox/5' ) !== false 
			){
				$flavorUrl = $sources['webm']['src'];
			}
		}
		
		return $flavorUrl;
	}
	
	/**
	*  Access Control Handling
	*/
	public function isAccessControlAllowed( &$resultObject = null ) {
		// Kaltura only has entry level access control not playlist level access control atm: 
		// don't check playlist
		if( $this->isPlaylist() ){
			return true;
		}
		
		if( !$resultObject ){
			$resultObject =  $this->getResultObject();
		}
		
		$accessControl = $resultObject['accessControl'];
		
		// Check if we had no access control due to playlist
		if( is_array( $accessControl ) && isset( $accessControl['code'] )){
			// Error ? .. should do better error checking.
			// errors we have seen so far: 
				//$accessControl['code'] == 'MISSING_MANDATORY_PARAMETER'
				//$accessControl['code'] == 'INTERNAL_SERVERL_ERROR'  
			return true;
		}
		
		// Checks if admin
		if( $accessControl->isAdmin ) {
			return true;
		}

		/* Domain Name Restricted */
		if( $accessControl->isSiteRestricted ) {
			return "Un authorized domain\nWe're sorry, this content is only available on certain domains.";
		}

		/* Country Restricted */
		if( $accessControl->isCountryRestricted) {
			return "Un authorized country\nWe're sorry, this content is only available in certain countries.";
		}

		/* IP Address Restricted */
		if( $accessControl->isIpAddressRestricted) {
			return "Un authorized IP address\nWe're sorry, this content is only available for ceratin IP addresses.";
		}

		/* Session Restricted */
		if( $accessControl->isSessionRestricted && $accessControl->previewLength == -1 ) {
			return "No KS where KS is required\nWe're sorry, access to this content is restricted.";
		}

		if( $accessControl->isScheduledNow === 0) {
			return "Out of scheduling\nWe're sorry, this content is currently unavailable.";
{'fraction_non_alphanumeric': 0.14632498431542879, 'fraction_numerical': 0.017759760629313257, 'mean_word_length': 3.4862524355921196, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 149, 'https://': 0, 'lorem ipsum': 0, 'www.': 14, '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': '1879331', 'n_tokens_mistral': 8162, 'n_tokens_neox': 6928, 'n_words': 1627}