From 41b21b0fe843d64c907aed041db687d5125ba18b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Tue, 5 Apr 2011 08:40:08 +0000
Subject: [PATCH] Adding support for using a idplist API to retreive a list of
 idps with successfull login at least once. used for weighting in discojuice

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2799 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/discojuice/lib/Feed.php       | 47 ++++++++++++++++++++++++---
 modules/discojuice/www/feed-debug.php |  2 +-
 2 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/modules/discojuice/lib/Feed.php b/modules/discojuice/lib/Feed.php
index 3038dd7fb..37d69fb53 100644
--- a/modules/discojuice/lib/Feed.php
+++ b/modules/discojuice/lib/Feed.php
@@ -5,9 +5,9 @@
  */
 class sspmod_discojuice_Feed {
 	
-	protected $config;
+	protected $config, $djconfig;
 	
-	protected $exlude, $override, $insert;
+	protected $excludes, $override, $insert, $idplist;
 	
 	protected $metadata;
 	protected $feed;
@@ -22,6 +22,10 @@ class sspmod_discojuice_Feed {
 		$metadatah = SimpleSAML_Metadata_MetaDataStorageHandler::getMetadataHandler();
 		$this->metadata = $metadatah->getList('saml20-idp-remote');
 		
+		$this->idplist = $this->getIdPList();
+		
+		SimpleSAML_Logger::info('IdP List contained : ' . count($this->idplist)  . ' entries.');
+		
 		$this->excludes = array_flip($this->djconfig->getValue('exclude'));
 		$this->insert = $this->djconfig->getValue('insert');
 		$this->overrides = $this->djconfig->getValue('overrides');
@@ -104,6 +108,21 @@ class sspmod_discojuice_Feed {
 		return (array_key_exists($e, $this->excludes));
 	}
 	
+	protected function getIdPList() {
+		$api = $this->djconfig->getValue('idplistapi', NULL);
+		if (empty($api)) return array();
+		
+		$result = array();
+		
+		$apiresult = json_decode(file_get_contents($api), TRUE);
+		if ($apiresult['status'] === 'ok') {
+			foreach($apiresult['data'] AS $idp) {
+				$result[$idp] = 1;
+			}
+		}
+		return $result;
+	}
+	
 	private function process() {
 		
 		$this->feed = array();
@@ -128,8 +147,11 @@ class sspmod_discojuice_Feed {
 		$this->getCountry($data, $m);
 		$this->getTitle($data, $m);
 		$this->getOverrides($data, $m);
-		
 		$this->getGeo($data, $m);
+		
+		if (!empty($this->idplist)) {
+			$this->islisted($data, $m);
+		}
 
 
 		return $data;
@@ -141,6 +163,19 @@ class sspmod_discojuice_Feed {
 	}
 	
 	
+	protected function islisted(&$data, $m) {
+		$weight = 0;
+		if (array_key_exists('weight', $data)) $weight = $data['weight'];
+		
+		if (!array_key_exists($m['entityid'], $this->idplist)) {
+			#echo 'Match for ' . $m['entityid'];
+			$weight += 2;
+		}
+		$data['weight'] = $weight;
+		
+#		echo '<pre>';
+#		print_r($this->idplist); exit;
+	}
 	
 	
 	protected function getGeo(&$data, $m) {
@@ -157,7 +192,11 @@ class sspmod_discojuice_Feed {
 
 			try {
 				$host = parse_url($endpoint['Location'], PHP_URL_HOST); if (empty($host)) return;
-				$ip = gethostbyname($host); if (empty($ip)) return;
+				$ip = gethostbyname($host); 
+				
+				if (empty($ip)) return;
+				if ($ip === $host) return;
+				
 				$capi = new sspmod_discojuice_Country($ip);
 				
 				if (empty($data['geo'])) {
diff --git a/modules/discojuice/www/feed-debug.php b/modules/discojuice/www/feed-debug.php
index 26651caf3..8495c354f 100644
--- a/modules/discojuice/www/feed-debug.php
+++ b/modules/discojuice/www/feed-debug.php
@@ -15,7 +15,7 @@ header('Content-Type: text/plain; charset=utf-8');
 
 foreach($data AS $key => $e) {
 	
-	if ($e['country'] == 'NO') {
+	if ($e['country'] == 'SE') {
 		print_r($e);
 	}
 	
-- 
GitLab