<commit_before><?php


$config = SimpleSAML_Configuration::getInstance();
$session = SimpleSAML_Session::getInstance();

if (!$session->isValid('login-admin') ) {
	SimpleSAML_Utilities::redirect('/' . $config->getBaseURL() . 'auth/login-admin.php',
		array('RelayState' => SimpleSAML_Utilities::selfURL())
	);
}


$ldapconfig = $config->copyFromBase('loginfeide', 'config-login-feide.php');
$ldapStatusConfig = $config->copyFromBase('ldapstatus', 'module_ldapstatus.php');

$pingcommand = $ldapStatusConfig->getValue('ping');

$debug = $ldapconfig->getValue('ldapDebug', FALSE);

$orgs = $ldapconfig->getValue('orgldapconfig');

#echo '<pre>'; print_r($orgs); exit;


function phpping($host, $port) {

	SimpleSAML_Logger::debug('ldapstatus phpping(): ping [' . $host . ':' . $port . ']' );

	$timeout = 1.0;
	$socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
	@fclose($socket);
	if ($errno) {
		return array(FALSE, $errno . ':' . $errstr);
	} else {		
		return array(TRUE,NULL);
	}
}

function checkConfig($conf, $req) {
	$err = array();
	foreach($req AS $r) {
		if (!array_key_exists($r, $conf)) {
			$err[] = $r;
		} elseif (empty($conf[$r]) && $conf[$r] !== FALSE) {
			$err[] = 'empty:' . $r;
		}
	}
	if (count($err) > 0) {
		return array(FALSE, 'Missing: ' . join(', ', $err));
	}
	return array(TRUE, NULL);	
}

$results = array();

foreach ($orgs AS $orgkey => $orgconfig) {

	$results[$orgkey] = array();
	

	$results[$orgkey]['config'] = checkConfig($orgconfig, array('description', 'searchbase', 'hostname'));
	$results[$orgkey]['configMeta'] = checkConfig($orgconfig, array('enable_tls', 'contactMail', 'contactURL'));
	$results[$orgkey]['configTest'] = checkConfig($orgconfig, array('testUser', 'testPassword'));

	if (!$results[$orgkey]['config'][0]) continue;

	$urldef = explode(' ', $orgconfig['hostname']);
	$url = parse_url($urldef[0]);
	$port = 389;
	if (!empty($url['port'])) $port = $url['port'];
	
	SimpleSAML_Logger::debug('ldapstatus Url parse [' . $orgconfig['hostname'] . '] => [' . $url['host'] . ']:[' . $port . ']' );

// 	$pingreturn = NULL;<commit_msg>Use port 636 if ldaps as default<commit_after><?php


$config = SimpleSAML_Configuration::getInstance();
$session = SimpleSAML_Session::getInstance();

if (!$session->isValid('login-admin') ) {
	SimpleSAML_Utilities::redirect('/' . $config->getBaseURL() . 'auth/login-admin.php',
		array('RelayState' => SimpleSAML_Utilities::selfURL())
	);
}


$ldapconfig = $config->copyFromBase('loginfeide', 'config-login-feide.php');
$ldapStatusConfig = $config->copyFromBase('ldapstatus', 'module_ldapstatus.php');

$pingcommand = $ldapStatusConfig->getValue('ping');

$debug = $ldapconfig->getValue('ldapDebug', FALSE);

$orgs = $ldapconfig->getValue('orgldapconfig');

#echo '<pre>'; print_r($orgs); exit;


function phpping($host, $port) {

	SimpleSAML_Logger::debug('ldapstatus phpping(): ping [' . $host . ':' . $port . ']' );

	$timeout = 1.0;
	$socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
	@fclose($socket);
	if ($errno) {
		return array(FALSE, $errno . ':' . $errstr . ' [' . $host . ':' . $port . ']');
	} else {		
		return array(TRUE,NULL);
	}
}

function checkConfig($conf, $req) {
	$err = array();
	foreach($req AS $r) {
		if (!array_key_exists($r, $conf)) {
			$err[] = $r;
		} elseif (empty($conf[$r]) && $conf[$r] !== FALSE) {
			$err[] = 'empty:' . $r;
		}
	}
	if (count($err) > 0) {
		return array(FALSE, 'Missing: ' . join(', ', $err));
	}
	return array(TRUE, NULL);	
}

$results = array();

foreach ($orgs AS $orgkey => $orgconfig) {

	$results[$orgkey] = array();
	

	$results[$orgkey]['config'] = checkConfig($orgconfig, array('description', 'searchbase', 'hostname'));
	$results[$orgkey]['configMeta'] = checkConfig($orgconfig, array('enable_tls', 'contactMail', 'contactURL'));
	$results[$orgkey]['configTest'] = checkConfig($orgconfig, array('testUser', 'testPassword'));

	if (!$results[$orgkey]['config'][0]) continue;

	$urldef = explode(' ', $orgconfig['hostname']);
	$url = parse_url($urldef[0]);
	$port = 389;
	if (!empty($url['scheme']) && $url['scheme'] === 'ldaps') $port = 636;
	if (!empty($url['port'])) $port = $url['port'];
	
	SimpleSAML_Logger::debug('ldapstatus Url parse [' . $orgconfig['hostname'] . '] => [' . $url['host'] . ']:[' . $port . ']' );

// 	$pingreturn = NULL;
{'fraction_non_alphanumeric': 0.23702502316960147, 'fraction_numerical': 0.005097312326227989, 'mean_word_length': 5.433681073025335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '224348', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1553, 'n_words': 275}