<commit_before><?php
/**
 * Controller name: Mozilla Open Badges Generator
 * Controller description: Generates Mozilla Open Badges compatible Assertions for the BadgeOS plugin
 */
class JSON_API_Badge_Controller {

	public function public_key() {
		print_r( BOSOBI_Settings::get( 'public_key' ) );
	}

	/* TODO: Implement server-side badge baking, instead of delegating our badge backing to backpack.openbadges.org/baker
	public function baked_badge() {
		$png = new PNG_MetaDataHandler('file.png');

		if ($png->check_chunks("tEXt", "openbadge")) {
			$newcontents = $png->add_chunks("tEXt", "openbadge", 'http://some.public.url/to.your.assertion.file');
		}
		
		file_put_contents('file.png', $newcontents);
	}
	*/

	public function assertion() {
		global $json_api;

		$uid_str = $json_api->query->uid;
		$uid = explode ( "-" , $uid_str );
		$post_id = $uid[0];
		$user_id = $uid[2];
		$assertion = array();

		if ( isset( $post_id ) ) {
			$for_baking = $json_api->query->bake;
			$use_signed_verification = BOSOBI_Settings::get( 'assertion_type' ) === 'signed' && ! $for_baking;

			$base_url = site_url() . '/' . get_option( 'json_api_base', 'api' );
			$submission = get_post( $post_id );
			$salt = "<PASSWORD>";
			$email = BOSOBI_Shortcodes::registered_email( $user_id );
			$post_type = get_post_type( $post_id );
			
			if ( $post_type === "submission" && BOSOBI_Settings::get( 'public_evidence' ) ) {
				$achievement_id = get_post_meta( $post_id, '_badgeos_submission_achievement_id', true );
				$assertion['evidence'] = get_permalink( $post_id );
			} else {
				$achievement_id = $post_id;
				$assertion['evidence'] = get_permalink( $achievement_id );
			}

			if ( $use_signed_verification ) {
				$verification = array(
					"type" => "signed",
					"url"  => $base_url .'/badge/public_key/',
				);
			} else {
				$verification = array(
					"type" => "hosted",
					"url"  => $base_url .'/badge/assertion/?uid=' . $uid_str,
				);
			}

			$image_url = wp_get_attachment_url( get_post_thumbnail_id( $achievement_id ) );

			$assertion = array_merge( array(
				"uid" => $uid_str,
				"recipient" => array(
					"type"     => "email",
					"hashed"   => true,
					"salt"     => $salt,
					"identity" => 'sha256$' . hash( 'sha256', $email . $salt )
				),
				"image"    => !$for_baking ? $image_url : 'http://backpack.openbadges.org/baker?assertion=' . $base_url . '/badge/assertion/?uid=' . $uid_str . '&bake=1',
				"issuedOn" => strtotime( $submission->post_date ),
				"badge"    => $base_url . '/badge/badge_class/?uid=' . $achievement_id,
				"verify"   => $verification,
			), $assertion );

			// For signed assertions, the payload must be encoded as a JSON Web Signature
			// See https://github.com/openbadges/openbadges-specification/blob/master/Assertion/latest.md
			if ( $verification['type'] === 'signed' ) {
				//require_once( sprintf( "%s/includes/jwt.php", BadgeOS_Open_Badges_Issuer_AddOn::$directory_path ) );
				//$assertion = JWT::encode( $assertion, BOSOBI_Settings::get( 'private_key' ) );
				
				// An alternate method for encoding, in case we question the integrity of the above method. Requires the phpseclib library.
				// See: https://github.com/mozilla/openbadges-validator/issues/23#issuecomment-26600324
				// require_once( sprintf( "%s/includes/phpseclib-0.3.9/Crypt/RSA.php", BadgeOS_Open_Badges_Issuer_AddOn::$directory_path ) );
				$old_include_path = set_include_path( sprintf( "%s/includes/phpseclib-0.3.9/", BadgeOS_Open_Badges_Issuer_AddOn::$directory_path ) );
				require_once( "Crypt/RSA.php" );

				$rsa = new Crypt_RSA();
				$rsa->setSignatureMode( CRYPT_RSA_SIGNATURE_PKCS1 );
				$rsa->setHash( 'sha256' );
				$rsa->setMGFHash( 'sha256' );
				$rsa->loadKey( BOSOBI_Settings::get( 'private_key' ), CRYPT_RSA_PRIVATE_FORMAT_PKCS1 );

				$header = array( 'alg' => "RS256" );
				$headerText = self::base64url_encode( json_encode( $header ) );
				$assertionText = self::base64url_encode( json_encode( $assertion ) );

				$input = $headerText . '.' . $assertionText;

				$signature = $rsa->sign( $input );<commit_msg>Minor fix<commit_after><?php
/**
 * Controller name: Mozilla Open Badges Generator
 * Controller description: Generates Mozilla Open Badges compatible Assertions for the BadgeOS plugin
 */
class JSON_API_Badge_Controller {

	public function public_key() {
		print_r( BOSOBI_Settings::get( 'public_key' ) );
	}

	/* TODO: Implement server-side badge baking, instead of delegating our badge backing to backpack.openbadges.org/baker
	public function baked_badge() {
		$png = new PNG_MetaDataHandler('file.png');

		if ($png->check_chunks("tEXt", "openbadge")) {
			$newcontents = $png->add_chunks("tEXt", "openbadge", 'http://some.public.url/to.your.assertion.file');
		}
		
		file_put_contents('file.png', $newcontents);
	}
	*/

	public function assertion() {
		global $json_api;

		$uid_str = $json_api->query->uid;
		$uid = explode ( "-" , $uid_str );
		$post_id = $uid[0];
		$user_id = $uid[2];
		$assertion = array();

		if ( isset( $post_id ) ) {
			$for_baking = $json_api->query->bake;
			$use_signed_verification = BOSOBI_Settings::get( 'assertion_type' ) === 'signed' && ! $for_baking;

			$base_url = site_url() . '/' . get_option( 'json_api_base', 'api' );
			$submission = get_post( $post_id );
			$salt = "<PASSWORD>";
			$email = BOSOBI_Shortcodes::registered_email( $user_id );
			$post_type = get_post_type( $post_id );
			
			if ( $post_type === "submission" && BOSOBI_Settings::get( 'public_evidence' ) ) {
				$achievement_id = get_post_meta( $post_id, '_badgeos_submission_achievement_id', true );
				$assertion['evidence'] = get_permalink( $post_id );
			} else {
				$achievement_id = $post_id;
				$assertion['evidence'] = get_permalink( $achievement_id );
			}

			if ( $use_signed_verification ) {
				$verification = array(
					"type" => "signed",
					"url"  => $base_url .'/badge/public_key/',
				);
			} else {
				$verification = array(
					"type" => "hosted",
					"url"  => $base_url .'/badge/assertion/?uid=' . $uid_str,
				);
			}

			$image_url = wp_get_attachment_url( get_post_thumbnail_id( $achievement_id ) );

			$assertion = array_merge( array(
				"uid" => $uid_str,
				"recipient" => array(
					"type"     => "email",
					"hashed"   => true,
					"salt"     => $salt,
					"identity" => 'sha256$' . hash( 'sha256', $email . $salt )
				),
				"image"    => $for_baking ? $image_url : 'http://backpack.openbadges.org/baker?assertion=' . $base_url . '/badge/assertion/?uid=' . $uid_str . '&bake=1',
				"issuedOn" => strtotime( $submission->post_date ),
				"badge"    => $base_url . '/badge/badge_class/?uid=' . $achievement_id,
				"verify"   => $verification,
			), $assertion );

			// For signed assertions, the payload must be encoded as a JSON Web Signature
			// See https://github.com/openbadges/openbadges-specification/blob/master/Assertion/latest.md
			if ( $verification['type'] === 'signed' ) {
				//require_once( sprintf( "%s/includes/jwt.php", BadgeOS_Open_Badges_Issuer_AddOn::$directory_path ) );
				//$assertion = JWT::encode( $assertion, BOSOBI_Settings::get( 'private_key' ) );
				
				// An alternate method for encoding, in case we question the integrity of the above method. Requires the phpseclib library.
				// See: https://github.com/mozilla/openbadges-validator/issues/23#issuecomment-26600324
				// require_once( sprintf( "%s/includes/phpseclib-0.3.9/Crypt/RSA.php", BadgeOS_Open_Badges_Issuer_AddOn::$directory_path ) );
				$old_include_path = set_include_path( sprintf( "%s/includes/phpseclib-0.3.9/", BadgeOS_Open_Badges_Issuer_AddOn::$directory_path ) );
				require_once( "Crypt/RSA.php" );

				$rsa = new Crypt_RSA();
				$rsa->setSignatureMode( CRYPT_RSA_SIGNATURE_PKCS1 );
				$rsa->setHash( 'sha256' );
				$rsa->setMGFHash( 'sha256' );
				$rsa->loadKey( BOSOBI_Settings::get( 'private_key' ), CRYPT_RSA_PRIVATE_FORMAT_PKCS1 );

				$header = array( 'alg' => "RS256" );
				$headerText = self::base64url_encode( json_encode( $header ) );
				$assertionText = self::base64url_encode( json_encode( $assertion ) );

				$input = $headerText . '.' . $assertionText;

				$signature = $rsa->sign( $input );
{'fraction_non_alphanumeric': 0.16611741730745758, 'fraction_numerical': 0.009764433052605883, 'mean_word_length': 3.854265402843602, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, 'https://': 4, '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': '2073120', 'n_tokens_mistral': 3470, 'n_tokens_neox': 2977, 'n_words': 531}