diff --git a/lib/SimpleSAML/Auth/LDAP.php b/lib/SimpleSAML/Auth/LDAP.php index 66f2788bba75738d8c7889d8e1d57373e8cb02df..f7dbebd3e3f23464a252834f6bc59c43aa5f4430 100644 --- a/lib/SimpleSAML/Auth/LDAP.php +++ b/lib/SimpleSAML/Auth/LDAP.php @@ -261,7 +261,7 @@ class SimpleSAML_Auth_LDAP { public function searchfordn($base, $attribute, $value, $allowZeroHits = FALSE) { // Traverse all search bases, returning DN if found. - $bases = SimpleSAML_Utilities::arrayize($base); + $bases = SimpleSAML_Utils_Arrays::arrayize($base); $result = NULL; foreach ($bases AS $current) { try { diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php index 40941d56bb39a4f2f8a3dbf20b64fb4e910744ac..a2108ee156e3d8636106f83642058ccb2536e138 100644 --- a/lib/SimpleSAML/Configuration.php +++ b/lib/SimpleSAML/Configuration.php @@ -111,7 +111,7 @@ class SimpleSAML_Configuration { $host = $_SERVER['HTTP_HOST']; if (array_key_exists($host, $config['override.host'])) { $ofs = $config['override.host'][$host]; - foreach (SimpleSAML_Utilities::arrayize($ofs) AS $of) { + foreach (SimpleSAML_Utils_Arrays::arrayize($ofs) AS $of) { $overrideFile = dirname($filename) . '/' . $of; if (!file_exists($overrideFile)) { throw new Exception('Config file [' . $filename . '] requests override for host ' . $host . ' but file does not exists [' . $of . ']'); diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php index 70f92f565e6fedbd1470d6221ee84e150d645598..52f71c2ae3d00dd2eeec043eb496243adffc6387 100644 --- a/lib/SimpleSAML/Metadata/SAMLBuilder.php +++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php @@ -277,9 +277,9 @@ class SimpleSAML_Metadata_SAMLBuilder { return; } - $orgName = SimpleSAML_Utilities::arrayize($metadata['OrganizationName'], 'en'); - $orgDisplayName = SimpleSAML_Utilities::arrayize($metadata['OrganizationDisplayName'], 'en'); - $orgURL = SimpleSAML_Utilities::arrayize($metadata['OrganizationURL'], 'en'); + $orgName = SimpleSAML_Utils_Arrays::arrayize($metadata['OrganizationName'], 'en'); + $orgDisplayName = SimpleSAML_Utils_Arrays::arrayize($metadata['OrganizationDisplayName'], 'en'); + $orgURL = SimpleSAML_Utils_Arrays::arrayize($metadata['OrganizationURL'], 'en'); $this->addOrganization($orgName, $orgDisplayName, $orgURL); } diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 052c07d010fa8d262664b029bdee1c19bd2658d2..5b99a9313779feae3181f3dacd404297dc217be5 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -1583,8 +1583,10 @@ class SimpleSAML_Utilities { return $doc->saveXML($root); } - /* + /** * Input is single value or array, returns an array. + * + * @deprecated This function will be removed in SSP 2.0. Please use SimpleSAML_Utils_Arrays::arrayize() instead. */ public static function arrayize($data, $index = 0) { if (is_array($data)) { diff --git a/lib/SimpleSAML/Utils/Arrays.php b/lib/SimpleSAML/Utils/Arrays.php index 7f5db6c48cfaa8249f06670649665bf538f75b09..db9582361d5660e12918e3b9e6a0500f5eb42677 100644 --- a/lib/SimpleSAML/Utils/Arrays.php +++ b/lib/SimpleSAML/Utils/Arrays.php @@ -9,6 +9,23 @@ class SimpleSAML_Utils_Arrays { + /** + * Put a non-array variable into an array. + * + * @param array $data The data to place into an array. + * @param mixed $index The index or key of the array where to place the data. Defaults to 0. + * + * @return array An array with one element containing $data, with key $index, or $data itself if it's already an + * array. + * + * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> + * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> + */ + public static function arrayize($data, $index = 0) + { + return (is_array($data)) ? $data : array($index => $data); + } + /** * Validate and normalize an array with attributes. * @@ -24,6 +41,7 @@ class SimpleSAML_Utils_Arrays * not strings. * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> + * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ public static function normalizeAttributesArray($attributes) { @@ -38,9 +56,7 @@ class SimpleSAML_Utils_Arrays throw new SimpleSAML_Error_Exception('Invalid attribute name: "'.print_r($name, true).'".'); } - if (!is_array($values)) { - $values = array($values); - } + $values = self::arrayize($values); foreach ($values as $value) { if (!is_string($value)) { diff --git a/modules/consent/templates/consentform.php b/modules/consent/templates/consentform.php index 59608f048eca5046b4971c63decf93526db8e6fd..65056c56f1a934563e6c5181953ce87adbe56eae 100644 --- a/modules/consent/templates/consentform.php +++ b/modules/consent/templates/consentform.php @@ -74,7 +74,7 @@ if (array_key_exists('descr_purpose', $this->data['dstMetadata'])) { array( 'SPNAME' => $dstName, 'SPDESC' => $this->getTranslation( - SimpleSAML_Utilities::arrayize( + SimpleSAML_Utils_Arrays::arrayize( $this->data['dstMetadata']['descr_purpose'], 'en' ) diff --git a/modules/core/templates/frontpage_federation.tpl.php b/modules/core/templates/frontpage_federation.tpl.php index 4891885676724fc3ae0c0f388669b47ed322ce75..eb8795ac6016ec00521d250578d38bb2b2523697 100644 --- a/modules/core/templates/frontpage_federation.tpl.php +++ b/modules/core/templates/frontpage_federation.tpl.php @@ -61,9 +61,9 @@ foreach ($this->data['metaentries']['hosted'] AS $hm) { if ($hm['entityid'] !== $hm['metadata-index']) echo '<br />Index: ' . $hm['metadata-index']; if (!empty($hm['name'])) - echo '<br /><strong>' . $this->getTranslation(SimpleSAML_Utilities::arrayize($hm['name'], 'en')) . '</strong>'; + echo '<br /><strong>' . $this->getTranslation(SimpleSAML_Utils_Arrays::arrayize($hm['name'], 'en')) . '</strong>'; if (!empty($hm['descr'])) - echo '<br /><strong>' . $this->getTranslation(SimpleSAML_Utilities::arrayize($hm['descr'], 'en')) . '</strong>'; + echo '<br /><strong>' . $this->getTranslation(SimpleSAML_Utils_Arrays::arrayize($hm['descr'], 'en')) . '</strong>'; echo '<br />[ <a href="' . $hm['metadata-url'] . '">' . $this->t('{core:frontpage:show_metadata}') . '</a> ]'; @@ -82,9 +82,9 @@ foreach($this->data['metaentries']['remote'] AS $setkey => $set) { htmlspecialchars(SimpleSAML_Module::getModuleURL('core/show_metadata.php', array('entityid' => $entry['entityid'], 'set' => $setkey ))) . '">'); if (!empty($entry['name'])) { - echo htmlspecialchars($this->getTranslation(SimpleSAML_Utilities::arrayize($entry['name'], 'en'))); + echo htmlspecialchars($this->getTranslation(SimpleSAML_Utils_Arrays::arrayize($entry['name'], 'en'))); } elseif (!empty($entry['OrganizationDisplayName'])) { - echo htmlspecialchars($this->getTranslation(SimpleSAML_Utilities::arrayize($entry['OrganizationDisplayName'], 'en'))); + echo htmlspecialchars($this->getTranslation(SimpleSAML_Utils_Arrays::arrayize($entry['OrganizationDisplayName'], 'en'))); } else { echo htmlspecialchars($entry['entityid']); } diff --git a/modules/saml/lib/Auth/Source/SP.php b/modules/saml/lib/Auth/Source/SP.php index 565ad663156eea11243914dacd15eec346f865dd..a1779a127fe43a4279218692e8f938e0dcd78d4b 100644 --- a/modules/saml/lib/Auth/Source/SP.php +++ b/modules/saml/lib/Auth/Source/SP.php @@ -193,7 +193,7 @@ class sspmod_saml_Auth_Source_SP extends SimpleSAML_Auth_Source { } if (isset($state['saml:AuthnContextClassRef'])) { - $accr = SimpleSAML_Utilities::arrayize($state['saml:AuthnContextClassRef']); + $accr = SimpleSAML_Utils_Arrays::arrayize($state['saml:AuthnContextClassRef']); $ar->setRequestedAuthnContext(array('AuthnContextClassRef' => $accr)); } diff --git a/modules/statistics/lib/StatDataset.php b/modules/statistics/lib/StatDataset.php index 1ea4f1a800c582aba4a24b8492db645a0cfc486b..0533a2211557057d0ee0692118fdd3f1ca5ee672 100644 --- a/modules/statistics/lib/StatDataset.php +++ b/modules/statistics/lib/StatDataset.php @@ -294,7 +294,7 @@ class sspmod_statistics_StatDataset { $statdir = $this->statconfig->getValue('statdir'); $resarray = array(); - $rules = SimpleSAML_Utilities::arrayize($this->ruleid); + $rules = SimpleSAML_Utils_Arrays::arrayize($this->ruleid); foreach($rules AS $rule) { // Get file and extract results. $resultFileName = $statdir . '/' . $rule . '-' . $this->timeres . '-'. $this->fileslot . '.stat'; diff --git a/tests/Utils/ArraysTest.php b/tests/Utils/ArraysTest.php index 1d4ed91d2beec77e266ee1f98e37eb3bae6e76a7..e43f5c28b95743c21c1106d34651fb4a0414d43d 100644 --- a/tests/Utils/ArraysTest.php +++ b/tests/Utils/ArraysTest.php @@ -7,6 +7,32 @@ class Utils_ArraysTest extends PHPUnit_Framework_TestCase { + /** + * Test the arrayize() function. + */ + public function testArrayize() + { + // check with empty array as input + $array = array(); + $this->assertEquals($array, SimpleSAML_Utils_Arrays::arrayize($array)); + + // check non-empty array as input + $array = array('key' => 'value'); + $this->assertEquals($array, SimpleSAML_Utils_Arrays::arrayize($array)); + + // check indexes are ignored when input is an array + $this->assertArrayNotHasKey('invalid', SimpleSAML_Utils_Arrays::arrayize($array, 'invalid')); + + // check default index + $expected = array('string'); + $this->assertEquals($expected, SimpleSAML_Utils_Arrays::arrayize($expected[0])); + + // check string index + $index = 'key'; + $expected = array($index => 'string'); + $this->assertEquals($expected, SimpleSAML_Utils_Arrays::arrayize($expected[$index], $index)); + } + /** * Test the normalizeAttributesArray() function with input not being an array *