<commit_before><?php

require_once __DIR__ . '/../PushHandler.class.php';

class WNSHandler extends PushHandler {

	/**
	 * @var array
	 */
	protected $_server = [
		'token'    => '',
		'url'      => '',
		'auth-url' => 'https://login.live.com/accesstoken.srf',
	];

	/**
	 * @param $deviceID
	 * @param $deviceSecret
	 */
	public function requestOAuthToken($clientID, $clientSecret) {
		// init http-headers
		$headers = [
			'Content-Type: application/x-www-form-urlencoded',
		];

		// open curl connection
		$curl = curl_init();

		// set url
		curl_setopt($curl, CURLOPT_URL, $this->_server['auth-url']);

		// apply headers and set type to POST
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

		// return response instead of status
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

		// check certificates
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

		// init payload
		$payload = 'grant_type=client_credentials&client_id=' . $clientID . '&client_secret=' . $clientSecret . '&scope=notify.windows.com';

		// append payload
		curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);

		// send request
		$result = curl_exec($curl);

		if ($result === false) {
			$error = curl_error($curl);
			curl_close($curl);
			throw new Exception('error processing WPN: ' . $error, 500);
		}

		$result = json_decode($result);

		// remeber to close the connection when finished
		curl_close($curl);

		// handle errors
		if (isset($output->error)) {
			throw new Exception($output->error_description, 500);
		} elseif (!isset($result->access_token)) {
			throw new Exception('access_token not found', 500);
		}

		return $result->access_token;
	}

	/**
	 * @param $message
	 * @param array $data
	 */
	protected static function _createPayload($message, array $data = null) {
		if (isset($data['title']) && !isset($data['image'])) {
			return '<?xml version="1.0" encoding="utf-8"?>' .
				'<toast>' .
				'<visual>' .
				'<binding template="ToastText02">' .
				'<text id="1">' . $messages . '</text>' .
				'<text id="2">' . $data['title'] . '</text>' .
				'</binding>' .
				'</visual>' .
				'</toast>';
		} elseif (isset($data['title'])) {
			return '<?xml version="1.0" encoding="utf-8"?>' .
				'<toast>' .
				'<visual>' .
				'<binding template="ToastImageAndText02">' .
				'<image id="1" src="' . $data['image'] . '" alt="' . $data['image'] . '"/>' .
				'<text id="1">' . $messages . '</text>' .
				'<text id="2">' . $data['title'] . '</text>' .
				'</binding>' .
				'</visual>' .
				'</toast>';
		} elseif (isset($data['image'])) {
			return '<?xml version="1.0" encoding="utf-8"?>' .
				'<toast>' .
				'<visual>' .
				'<binding template="ToastImageAndText01">' .
				'<image id="1" src="' . $data['image'] . '" alt="' . $data['image'] . '"/>' .<commit_msg>updated WNS handler to first check is_array for data-payload<commit_after><?php

require_once __DIR__ . '/../PushHandler.class.php';

class WNSHandler extends PushHandler {

	/**
	 * @var array
	 */
	protected $_server = [
		'token'    => '',
		'url'      => '',
		'auth-url' => 'https://login.live.com/accesstoken.srf',
	];

	/**
	 * @param $deviceID
	 * @param $deviceSecret
	 */
	public function requestOAuthToken($clientID, $clientSecret) {
		// init http-headers
		$headers = [
			'Content-Type: application/x-www-form-urlencoded',
		];

		// open curl connection
		$curl = curl_init();

		// set url
		curl_setopt($curl, CURLOPT_URL, $this->_server['auth-url']);

		// apply headers and set type to POST
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

		// return response instead of status
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

		// check certificates
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);

		// init payload
		$payload = 'grant_type=client_credentials&client_id=' . $clientID . '&client_secret=' . $clientSecret . '&scope=notify.windows.com';

		// append payload
		curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);

		// send request
		$result = curl_exec($curl);

		if ($result === false) {
			$error = curl_error($curl);
			curl_close($curl);
			throw new Exception('error processing WPN: ' . $error, 500);
		}

		$result = json_decode($result);

		// remeber to close the connection when finished
		curl_close($curl);

		// handle errors
		if (isset($output->error)) {
			throw new Exception($output->error_description, 500);
		} elseif (!isset($result->access_token)) {
			throw new Exception('access_token not found', 500);
		}

		return $result->access_token;
	}

	/**
	 * @param $message
	 * @param array $data
	 */
	protected static function _createPayload($message, array $data = null) {
		if (is_array($data) && isset($data['title']) && !isset($data['image'])) {
			return '<?xml version="1.0" encoding="utf-8"?>' .
				'<toast>' .
				'<visual>' .
				'<binding template="ToastText02">' .
				'<text id="1">' . $messages . '</text>' .
				'<text id="2">' . $data['title'] . '</text>' .
				'</binding>' .
				'</visual>' .
				'</toast>';
		} elseif (is_array($data) && isset($data['title'])) {
			return '<?xml version="1.0" encoding="utf-8"?>' .
				'<toast>' .
				'<visual>' .
				'<binding template="ToastImageAndText02">' .
				'<image id="1" src="' . $data['image'] . '" alt="' . $data['image'] . '"/>' .
				'<text id="1">' . $messages . '</text>' .
				'<text id="2">' . $data['title'] . '</text>' .
				'</binding>' .
				'</visual>' .
				'</toast>';
		} elseif (is_array($data) && isset($data['image'])) {
			return '<?xml version="1.0" encoding="utf-8"?>' .
				'<toast>' .
				'<visual>' .
				'<binding template="ToastImageAndText01">' .
				'<image id="1" src="' . $data['image'] . '" alt="' . $data['image'] . '"/>' .
{'fraction_non_alphanumeric': 0.20942944994875298, 'fraction_numerical': 0.01059104885548343, 'mean_word_length': 3.389055472263868, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 6, '>': 75, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '6859734', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2019, 'n_words': 417}