From a25f7374f83844f7125a90e696fbb8cb31ea192e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Mon, 19 Jan 2009 19:14:34 +0000
Subject: [PATCH] LDAP status module implements Ping in php. several other
 improvements...

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1167 44740490-163a-0410-bde0-09ae8108e29a
---
 .../templates/default/ldapstatus.php          | 11 ++-
 modules/ldapstatus/www/index.php              | 85 ++++++++++++++-----
 2 files changed, 75 insertions(+), 21 deletions(-)

diff --git a/modules/ldapstatus/templates/default/ldapstatus.php b/modules/ldapstatus/templates/default/ldapstatus.php
index a82b6cb4f..6ac7cca3e 100644
--- a/modules/ldapstatus/templates/default/ldapstatus.php
+++ b/modules/ldapstatus/templates/default/ldapstatus.php
@@ -11,6 +11,7 @@ $this->includeAtTemplateBase('includes/header.php');
 <table class="attributes" style="width: 100%; border: 1px solid #aaa">
 	<tr>
 	<th>Name of institusion</th>
+	<!-- th>Identifier</th -->
 	<th>Conf</th>
 	<th>Meta</th>
 	<th>Ping</th>
@@ -44,15 +45,21 @@ function showRes($key, $res, $template) {
 }
 
 
+
+
 $i = 0;
 $classes = array('odd', 'even');
-foreach($this->data['results'] as $orgkey => $res) {
+
+# $this->data['results']
+foreach($this->data['sortedOrgIndex'] as $orgkey) {
+	$res = $this->data['results'][$orgkey];
 	echo('<tr class="' . ($classes[($i++ % 2)]) . '">');
 	echo('<td>' . htmlspecialchars($this->getTranslation($this->data['orgconfig'][$orgkey]['description'])) . '</td>');
+#	echo('<td><tt>' . htmlspecialchars($orgkey) . '</tt></td>');
 	showRes('config',  $res, $this);
 	showRes('configMeta',  $res, $this);
 	showRes('ping',  $res, $this);
-	showRes('adminUser',  $res, $this);
+	showRes('adminBind',  $res, $this);
 	showRes('ldapSearchBogus',  $res, $this);
 	showRes('ldapSearchTestUser',  $res, $this);
 	showRes('ldapBindTestUser',  $res, $this);
diff --git a/modules/ldapstatus/www/index.php b/modules/ldapstatus/www/index.php
index d272f8dd2..f92a08cde 100644
--- a/modules/ldapstatus/www/index.php
+++ b/modules/ldapstatus/www/index.php
@@ -21,10 +21,29 @@ $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;
+		if (!array_key_exists($r, $conf)) {
+			$err[] = $r;
+		} elseif (empty($conf[$r])) {
+			$err[] = 'empty:' . $r;
+		}
 	}
 	if (count($err) > 0) {
 		return array(FALSE, 'Missing: ' . join(', ', $err));
@@ -39,28 +58,32 @@ foreach ($orgs AS $orgkey => $orgconfig) {
 	$results[$orgkey] = array();
 	
 
-	$results[$orgkey]['config'] = checkConfig($orgconfig, array('description', 'searchbase', 'hostname', 'attributes'));
+	$results[$orgkey]['config'] = checkConfig($orgconfig, array('description', 'searchbase', 'hostname'));
 	$results[$orgkey]['configMeta'] = checkConfig($orgconfig, array('enable_tls', 'testUser', 'testPassword', 'contactMail', 'contactURL'));
-	
-	$url = parse_url($orgconfig['hostname']);
 
-	$pingreturn = NULL;
-	$pingoutput = NULL;
-	
-#	echo 'Ping command: ' . $pingcommand; exit;
+	if (!$results[$orgkey]['config'][0]) continue;
+
+	$urldef = explode(' ', $orgconfig['hostname']);
+	$url = parse_url($urldef[0]);
+	$port = 389;
+	if (!empty($url['port'])) $port = $url['port'];
 	
-	exec($pingcommand . ' ' . escapeshellcmd($url['host']), $pingoutput, $pingreturn);
+	SimpleSAML_Logger::debug('ldapstatus Url parse [' . $orgconfig['hostname'] . '] => [' . $url['host'] . ']:[' . $port . ']' );
 
-#	echo $pingreturn; exit;
+// 	$pingreturn = NULL;
+// 	$pingoutput = NULL;
+// 	exec($pingcommand . ' ' . escapeshellcmd($url['host']), $pingoutput, $pingreturn);
+// 	if ($pingreturn == '0') {
+// 		$results[$orgkey]['ping'] = array(TRUE,join("\r\n", $pingoutput));
+// 	} else {
+// 		$results[$orgkey]['ping'] = array(FALSE,join("\r\n", $pingoutput));
+// 		continue;
+// 	}
+
+	$results[$orgkey]['ping'] = phpping($url['host'], $port);
+
+	if (!$results[$orgkey]['ping'][0]) continue;
 
-	if ($pingreturn == '0') {
-		$results[$orgkey]['ping'] = array(TRUE,join("\r\n", $pingoutput));
-	} else {
-		$results[$orgkey]['ping'] = array(FALSE,join("\r\n", $pingoutput));
-		continue;
-	}
-	
-	#continue;
 	
 	// LDAP Connect
 	try {
@@ -106,7 +129,7 @@ foreach ($orgs AS $orgkey => $orgconfig) {
 			continue;
 		}
 		
-		if ($ldap->bind($orgconfig['testUser'], $orgconfig['testPassword'])) {
+		if ($ldap->bind($dn, $orgconfig['testPassword'])) {
 			$results[$orgkey]['ldapBindTestUser'] = array(TRUE,NULL);
 			
 		} else {
@@ -124,9 +147,33 @@ foreach ($orgs AS $orgkey => $orgconfig) {
 }
 #echo '<pre>'; print_r($results); exit;
 
+function resultCode($res) {
+	$code = '';
+	$columns = array('config', 'configMeta', 'ping', 'adminUser', 'ldapSearchBogus', 'ldapSearchTestUser', 'ldapBindTestUser', 'ldapGetAttributesTestUser');
+	foreach ($columns AS $c) {
+		if (array_key_exists($c, $res)) {
+			$code .= ($res[$c][0] ? '0' : '2');
+		} else {
+			$code .= '1';
+		}
+	}
+	return $code;
+}
+
+	
+	
+$ressortable = array();
+foreach ($results AS $key => $res) {
+	$ressortable[$key] = resultCode($res);
+}
+asort($ressortable);
+#echo '<pre>'; print_r($ressortable); exit;
+
+
 $t = new SimpleSAML_XHTML_Template($config, 'ldapstatus:ldapstatus.php');
 $t->data['results'] = $results;
 $t->data['orgconfig'] = $orgs;
+$t->data['sortedOrgIndex'] = array_keys($ressortable);
 $t->show();
 exit;
 
-- 
GitLab