<commit_before><?php
class MsnDistributionEngine extends DistributionEngine implements 
	IDistributionEngineUpdate,
	IDistributionEngineSubmit,
	IDistributionEngineReport,
	IDistributionEngineDelete,
	IDistributionEngineCloseUpdate,
	IDistributionEngineCloseSubmit,
	IDistributionEngineCloseDelete
{
	const USAGE_COUNTER_PLAYED = 1;
	const USAGE_COUNTER_EMAILED = 2;
	const USAGE_COUNTER_RATED = 3;
	const USAGE_COUNTER_BLOGGED = 4;
	const USAGE_COUNTER_REVIEWED = 5;
	const USAGE_COUNTER_BOOKMARKED = 6;
	const USAGE_COUNTER_PLAYBACKFAILED = 7;
	const USAGE_COUNTER_TIMESPENT = 8;
	const USAGE_COUNTER_RECOMMENDED = 9;

	
	private $defaultDomain = 'catalog.video.msn.com';
	private $submitPath = '/admin/services/storevideoandfiles.aspx';
	private $updatePath = '/admin/services/storevideoandfiles.aspx';
	private $deletePath = '/admin/services/storevideoandfiles.aspx'; // it's updating with end date to 5 days from now
	private $fetchReportPath = '/admin/services/videobyuuid.aspx';
	private $postFieldName = 'videoxml';
	
	/* (non-PHPdoc)
	 * @see DistributionEngine::configure()
	 */
	public function configure(KSchedularTaskConfig $taskConfig)
	{
		if($taskConfig->params->msnDefaultDomain)
			$this->defaultDomain = $taskConfig->params->msnDefaultDomain;
			
		if($taskConfig->params->msnSubmitPath)
			$this->submitPath = $taskConfig->params->msnSubmitPath;
			
		if($taskConfig->params->msnUpdatePath)
			$this->updatePath = $taskConfig->params->msnUpdatePath;
			
		if($taskConfig->params->msnDeletePath)
			$this->deletePath = $taskConfig->params->msnDeletePath;
			
		if($taskConfig->params->msnFetchReportPath)
			$this->fetchReportPath = $taskConfig->params->msnFetchReportPath;
			
		if($taskConfig->params->msnPostFieldName)
			$this->postFieldName = $taskConfig->params->msnPostFieldName;
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineSubmit::submit()
	 */
	public function submit(KalturaDistributionSubmitJobData $data)
	{
		if(!$data->distributionProfile || !($data->distributionProfile instanceof KalturaMsnDistributionProfile))
			KalturaLog::err("Distribution profile must be of type KalturaMsnDistributionProfile");
	
		if(!$data->providerData || !($data->providerData instanceof KalturaMsnDistributionJobProviderData))
			KalturaLog::err("Provider data must be of type KalturaMsnDistributionJobProviderData");
		
		$results = $this->handleSend($this->submitPath, $data, $data->distributionProfile, $data->providerData);
		$data->remoteId = trim($results);
		
		return false;
	}

	/**
	 * @param string $path
	 * @param KalturaDistributionJobData $data
	 * @param KalturaMsnDistributionProfile $distributionProfile
	 * @param KalturaMsnDistributionJobProviderData $providerData
	 * @throws Exception
	 */
	public function handleSend($path, KalturaDistributionJobData $data, KalturaMsnDistributionProfile $distributionProfile, KalturaMsnDistributionJobProviderData $providerData)
	{
		$domain = $this->defaultDomain;
		if(!is_null($distributionProfile->domain))
			$domain = $distributionProfile->domain;
			
		$username = $distributionProfile->username;
		$password = $distributionProfile->password;
		
		$url = "https://{$domain}{$path}";
		
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_USERAGENT, self::HTTP_USER_AGENT);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);
		
		$params = array($this->postFieldName => $providerData->xml);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		
		$results = curl_exec($ch);
		if(!$results)
		{
			$errNumber = curl_errno($ch);
			$errDescription = curl_error($ch);
			
			curl_close($ch);
		
			throw new Exception($errDescription, $errNumber);
		}
		curl_close($ch);
		KalturaLog::debug("MSN HTTP response:\n$results\n");
		$data->results = $results;
		return $results;
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineCloseSubmit::closeSubmit()
	 */
	public function closeSubmit(KalturaDistributionSubmitJobData $data)
	{
		$publishState = $this->fetchStatus($data);
		switch($publishState)
		{
			case 'Published':
				return true;
				
			case 'Error':
				$liveSiteErrorNodes = $xml->documentElement->getElementsByTagName('liveSiteError');
				if($liveSiteErrorNodes->length)
				{
					$errDescription = $liveSiteErrorNodes->item(0)->textContent;
					throw new Exception("MSN error: $errDescription");
				}
				throw new Exception('Unknows MSN error');
				
			// TODO - check with MSN what other statuses are available
			
			default:
				KalturaLog::err("Unknown publishState [$publishState]");
				return false;
		}
	}

	/**
	 * @param KalturaDistributionSubmitJobData $data
	 * @return string status
	 */
	public function fetchStatus(KalturaDistributionSubmitJobData $data)
	{
		$xml = $this->fetchXML($data);
			
		$publishStateAttr = $xml->documentElement->attributes->getNamedItem('publishState');
		if($publishStateAttr)
			return $publishStateAttr->value;
				
		return null;
	}

	/**
	 * @param KalturaDistributionSubmitJobData $data
	 * @throws Exception
	 * @return DOMDocument
	 */
	public function fetchXML(KalturaDistributionSubmitJobData $data)
	{
		$domain = $this->defaultDomain;
		if(!is_null($distributionProfile->domain))
			$domain = $distributionProfile->domain;
			
		$username = $distributionProfile->username;
		$password = $distribution<PASSWORD>->password;
		
		$url = "https://{$domain}{$this->fetchReportPath}?uuid={$data->remoteId}";
		
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_USERAGENT, self::HTTP_USER_AGENT);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_FORBID_REUSE, true); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");

		curl_setopt($ch, CURLOPT_URL, $url);<commit_msg>Removed user agent from http requests<commit_after><?php
class MsnDistributionEngine extends DistributionEngine implements 
	IDistributionEngineUpdate,
	IDistributionEngineSubmit,
	IDistributionEngineReport,
	IDistributionEngineDelete,
	IDistributionEngineCloseUpdate,
	IDistributionEngineCloseSubmit,
	IDistributionEngineCloseDelete
{
	const USAGE_COUNTER_PLAYED = 1;
	const USAGE_COUNTER_EMAILED = 2;
	const USAGE_COUNTER_RATED = 3;
	const USAGE_COUNTER_BLOGGED = 4;
	const USAGE_COUNTER_REVIEWED = 5;
	const USAGE_COUNTER_BOOKMARKED = 6;
	const USAGE_COUNTER_PLAYBACKFAILED = 7;
	const USAGE_COUNTER_TIMESPENT = 8;
	const USAGE_COUNTER_RECOMMENDED = 9;

	
	private $defaultDomain = 'catalog.video.msn.com';
	private $submitPath = '/admin/services/storevideoandfiles.aspx';
	private $updatePath = '/admin/services/storevideoandfiles.aspx';
	private $deletePath = '/admin/services/storevideoandfiles.aspx'; // it's updating with end date to 5 days from now
	private $fetchReportPath = '/admin/services/videobyuuid.aspx';
	private $postFieldName = 'videoxml';
	
	/* (non-PHPdoc)
	 * @see DistributionEngine::configure()
	 */
	public function configure(KSchedularTaskConfig $taskConfig)
	{
		if($taskConfig->params->msnDefaultDomain)
			$this->defaultDomain = $taskConfig->params->msnDefaultDomain;
			
		if($taskConfig->params->msnSubmitPath)
			$this->submitPath = $taskConfig->params->msnSubmitPath;
			
		if($taskConfig->params->msnUpdatePath)
			$this->updatePath = $taskConfig->params->msnUpdatePath;
			
		if($taskConfig->params->msnDeletePath)
			$this->deletePath = $taskConfig->params->msnDeletePath;
			
		if($taskConfig->params->msnFetchReportPath)
			$this->fetchReportPath = $taskConfig->params->msnFetchReportPath;
			
		if($taskConfig->params->msnPostFieldName)
			$this->postFieldName = $taskConfig->params->msnPostFieldName;
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineSubmit::submit()
	 */
	public function submit(KalturaDistributionSubmitJobData $data)
	{
		if(!$data->distributionProfile || !($data->distributionProfile instanceof KalturaMsnDistributionProfile))
			KalturaLog::err("Distribution profile must be of type KalturaMsnDistributionProfile");
	
		if(!$data->providerData || !($data->providerData instanceof KalturaMsnDistributionJobProviderData))
			KalturaLog::err("Provider data must be of type KalturaMsnDistributionJobProviderData");
		
		$results = $this->handleSend($this->submitPath, $data, $data->distributionProfile, $data->providerData);
		$data->remoteId = trim($results);
		
		return false;
	}

	/**
	 * @param string $path
	 * @param KalturaDistributionJobData $data
	 * @param KalturaMsnDistributionProfile $distributionProfile
	 * @param KalturaMsnDistributionJobProviderData $providerData
	 * @throws Exception
	 */
	public function handleSend($path, KalturaDistributionJobData $data, KalturaMsnDistributionProfile $distributionProfile, KalturaMsnDistributionJobProviderData $providerData)
	{
		$domain = $this->defaultDomain;
		if(!is_null($distributionProfile->domain))
			$domain = $distributionProfile->domain;
			
		$username = $distributionProfile->username;
		$password = $<PASSWORD>;
		
		$url = "https://{$domain}{$path}";
		
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);
		
		$params = array($this->postFieldName => $providerData->xml);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		
		$results = curl_exec($ch);
		if(!$results)
		{
			$errNumber = curl_errno($ch);
			$errDescription = curl_error($ch);
			
			curl_close($ch);
		
			throw new Exception($errDescription, $errNumber);
		}
		curl_close($ch);
		KalturaLog::debug("MSN HTTP response:\n$results\n");
		$data->results = $results;
		return $results;
	}

	/* (non-PHPdoc)
	 * @see IDistributionEngineCloseSubmit::closeSubmit()
	 */
	public function closeSubmit(KalturaDistributionSubmitJobData $data)
	{
		$publishState = $this->fetchStatus($data);
		switch($publishState)
		{
			case 'Published':
				return true;
				
			case 'Error':
				$liveSiteErrorNodes = $xml->documentElement->getElementsByTagName('liveSiteError');
				if($liveSiteErrorNodes->length)
				{
					$errDescription = $liveSiteErrorNodes->item(0)->textContent;
					throw new Exception("MSN error: $errDescription");
				}
				throw new Exception('Unknows MSN error');
				
			// TODO - check with MSN what other statuses are available
			
			default:
				KalturaLog::err("Unknown publishState [$publishState]");
				return false;
		}
	}

	/**
	 * @param KalturaDistributionSubmitJobData $data
	 * @return string status
	 */
	public function fetchStatus(KalturaDistributionSubmitJobData $data)
	{
		$xml = $this->fetchXML($data);
			
		$publishStateAttr = $xml->documentElement->attributes->getNamedItem('publishState');
		if($publishStateAttr)
			return $publishStateAttr->value;
				
		return null;
	}

	/**
	 * @param KalturaDistributionSubmitJobData $data
	 * @throws Exception
	 * @return DOMDocument
	 */
	public function fetchXML(KalturaDistributionSubmitJobData $data)
	{
		$domain = $this->defaultDomain;
		if(!is_null($distributionProfile->domain))
			$domain = $distributionProfile->domain;
			
		$username = $distributionProfile->username;
		$password = $distributionProfile->password;
		
		$url = "https://{$domain}{$this->fetchReportPath}?uuid={$data->remoteId}";
		
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_FORBID_REUSE, true); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");

		curl_setopt($ch, CURLOPT_URL, $url);
{'fraction_non_alphanumeric': 0.12901123961557256, 'fraction_numerical': 0.001791822772438508, 'mean_word_length': 5.486529318541997, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 136, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '6811933', 'n_tokens_mistral': 4572, 'n_tokens_neox': 3911, 'n_words': 670}