<commit_before>#!/usr/bin/env php
<?php

define('SIMPLESAMLPATH', dirname(dirname(dirname(dirname(__FILE__)))));
define('MODULEPATH', (dirname(dirname(__FILE__))));
ini_set('error_level', E_ALL & E_STRICT);
ini_set('display_errors', true);

/*
 Merge metadata-generated/saml20-sp-remote.php with ../config/saml20-sp-mixin.php
 and drop superfluous fields.
 Templates may be specified by entityID or by category (http://macedir.org/entity-category).
 If specified by entity-category fields in the metadata dominate otherwise the template
 fields overwrite fields in the imported metadata.
 Merged metadata are written to metadata-generated/saml20-sp-merged.php.
 */

require(SIMPLESAMLPATH.'/metadata-generated/saml20-sp-remote.php');
require(MODULEPATH.'/config/saml20-sp-mixin.php');

foreach($template as $url => $mdata) {
	if(!isset($metadata[$url])) {
		passthru("logger -t IDP-METADATA-DIFF 'INFO: template url not found in metadata array: $url'");
	}
}

if(!($fh = @fopen(SIMPLESAMLPATH.'/metadata-generated/saml20-sp-merged.php', 'w'))) {
	echo("cannot open/create output file\n");
	exit(1);
}
@fwrite($fh, "<?php\n");

foreach($metadata as $url => $mdata) {
	foreach($fieldsToStrip as $field) unset($mdata[$field]);

	$templateWins = true;
	if(isset($template[$url])) $currentTemplate = $template[$url];
	else $currentTemplate = NULL;

	if($currentTemplate == NULL && isset($mdata['EntityAttributes']['http://macedir.org/entity-category'])) {
		foreach($mdata['EntityAttributes']['http://macedir.org/entity-category'] as $category) {
			if(isset($template[$category])) {
				$currentTemplate = $template[$category];
				$templateWins = false;
			}
		}
	}
	if($currentTemplate == NULL) $currentTemplate = $defaultTemplate;

	foreach($currentTemplate as $field => $value) {
		if($templateWins || !isset($mdata[$field])) $mdata[$field] = $value;
	}

	if(isset($mdata['attributes.required'])) {
		$checked = array_intersect($mdata['attributes.required'], $mdata['attributes']);
		if (empty($checked)) {
			unset($mdata['attributes.required']);
		} else {
			$mdata['attributes.required'] = $checked;
		}
	}

	// output
	if(!@fwrite($fh, "\n\$metadata['$url'] = ".var_export($mdata, true).";\n")) {
		echo("cannot write to output file\n");
		exit(1);
	}
}

@fwrite($fh, "\n?>");
if(!@fclose($fh)) {
	echo("cannot close output file\n");
	exit(1);
}

exit(0);

?>
<commit_msg>Restrict attribute release at compile time<commit_after>#!/usr/bin/env php
<?php

define('SIMPLESAMLPATH', dirname(dirname(dirname(dirname(__FILE__)))));
define('MODULEPATH', (dirname(dirname(__FILE__))));
ini_set('error_level', E_ALL & E_STRICT);
ini_set('display_errors', true);

/*
 Merge metadata-generated/saml20-sp-remote.php with ../config/saml20-sp-mixin.php
 and drop superfluous fields.
 Templates may be specified by entityID or by category (http://macedir.org/entity-category).
 If specified by entity-category fields in the metadata dominate otherwise the template
 fields overwrite fields in the imported metadata.
 Merged metadata are written to metadata-generated/saml20-sp-merged.php.
 */

require(SIMPLESAMLPATH.'/metadata-generated/saml20-sp-remote.php');
require(MODULEPATH.'/config/saml20-sp-mixin.php');

foreach($template as $url => $mdata) {
	if(!isset($metadata[$url])) {
		passthru("logger -t IDP-METADATA-DIFF 'INFO: template url not found in metadata array: $url'");
	}
}

if(!($fh = @fopen(SIMPLESAMLPATH.'/metadata-generated/saml20-sp-merged.php', 'w'))) {
	echo("cannot open/create output file\n");
	exit(1);
}
@fwrite($fh, "<?php\n");

foreach($metadata as $url => $mdata) {
	foreach($fieldsToStrip as $field) unset($mdata[$field]);

	$templateWins = true;
	if(isset($template[$url])) $currentTemplate = $template[$url];
	else $currentTemplate = NULL;

	if($currentTemplate == NULL && isset($mdata['EntityAttributes']['http://macedir.org/entity-category'])) {
		foreach($mdata['EntityAttributes']['http://macedir.org/entity-category'] as $category) {
			if(isset($template[$category])) {
				$currentTemplate = $template[$category];
				$templateWins = false;
			}
		}
	}
	if($currentTemplate == NULL) $currentTemplate = $defaultTemplate;

	foreach($currentTemplate as $field => $value) {
		if($templateWins || !isset($mdata[$field])) $mdata[$field] = $value;
	}

	if(isset($mdata['attributes.allowed'])) {
		$mdata['attributes'] = array_intersect($mdata['attributes'], $mdata['attributes.allowed']);
		unset($mdata['attributes.allowed']);
	}
	if(isset($mdata['attributes.allowed.ifRequired'])) {
		$allowed = array_intersect($mdata['attributes.required'], $mdata['attributes.allowed.ifRequired']);
		$mdata['attributes'] = $mdata['attributes'] + $allowed;
		unset($mdata['attributes.allowed.ifRequired']);
		unset($mdata['attributes.required']);
	}

	if(isset($mdata['attributes.required'])) {
		$checked = array_intersect($mdata['attributes.required'], $mdata['attributes']);
		if (empty($checked)) {
			unset($mdata['attributes.required']);
		} else {
			$mdata['attributes.required'] = $checked;
		}
	}

	// output
	if(!@fwrite($fh, "\n\$metadata['$url'] = ".var_export($mdata, true).";\n")) {
		echo("cannot write to output file\n");
		exit(1);
	}
}

@fwrite($fh, "\n?>");
if(!@fclose($fh)) {
	echo("cannot close output file\n");
	exit(1);
}

exit(0);

?>

{'fraction_non_alphanumeric': 0.19487666034155599, 'fraction_numerical': 0.0060721062618595825, 'mean_word_length': 5.706106870229007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '3287002', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1768, 'n_words': 325}