|
| 1 | +<?php |
| 2 | + |
| 3 | +const CONFIG_FILE_NAME = 'module_perun_getMetadata.php'; |
| 4 | + |
| 5 | +const PERUN_PROXY_IDENTIFIER_ATTR_NAME = 'perunProxyIdentifierAttr'; |
| 6 | +const PERUN_PROXY_ENTITY_ID_ATTR_NAME = 'perunProxyEntityIDAttr'; |
| 7 | + |
| 8 | +const PROXY_IDENTIFIER = 'proxyIdentifier'; |
| 9 | +const ABSOLUTE_FILE_NAME = 'absoluteFileName'; |
| 10 | +const ATTRIBUTES_DEFINITIONS = 'attributesDefinitions'; |
| 11 | +const FACILITY_ATTRIBUTES = 'facilityAttributes'; |
| 12 | + |
| 13 | +const TYPE_INTEGER = 'java.lang.Integer'; |
| 14 | +const TYPE_BOOLEAN = 'java.lang.Boolean'; |
| 15 | +const TYPE_STRING = 'java.lang.String'; |
| 16 | +const TYPE_ARRAY = 'java.util.ArrayList'; |
| 17 | +const TYPE_MAP = 'java.util.LinkedHashMap'; |
| 18 | + |
| 19 | +$conf = SimpleSAML_Configuration::getConfig(CONFIG_FILE_NAME); |
| 20 | + |
| 21 | +$proxyIdentifier = $conf->getString(PROXY_IDENTIFIER); |
| 22 | +assert(is_null($proxyIdentifier) || empty($proxyIdentifier)); |
| 23 | + |
| 24 | +$attributesDefinitions = $conf->getArray(ATTRIBUTES_DEFINITIONS); |
| 25 | +assert(is_null($attributesDefinitions) || is_array($attributesDefinitions)); |
| 26 | + |
| 27 | +$perunProxyIdentifierAttr = $conf->getString(PERUN_PROXY_IDENTIFIER_ATTR_NAME); |
| 28 | +$perunProxyEntityIDAttr = $conf->getString(PERUN_PROXY_ENTITY_ID_ATTR_NAME); |
| 29 | +assert(is_null($perunProxyEntityIDAttr) || empty($perunProxyEntityIDAttr) || is_null($perunProxyIdentifierAttr) || empty($perunProxyIdentifierAttr)); |
| 30 | + |
| 31 | +$absoluteFileName = $conf->getString(ABSOLUTE_FILE_NAME); |
| 32 | +assert(is_null($absoluteFileName) || empty($absoluteFileName)); |
| 33 | + |
| 34 | + |
| 35 | +$rpcAdapter = new sspmod_perun_AdapterRpc(); |
| 36 | + |
| 37 | +// Get list of all attribute names |
| 38 | +$attrNames = array(); |
| 39 | +$allAttrNames = array(); |
| 40 | +array_push($allAttrNames, $perunProxyEntityIDAttr); |
| 41 | +foreach ($attributesDefinitions as $key => $value) { |
| 42 | + array_push($attrNames, $key); |
| 43 | + array_push($allAttrNames, $key); |
| 44 | +} |
| 45 | + |
| 46 | +// Get all facilities with proxyIdentifiers |
| 47 | +$attributeDefinition = array(); |
| 48 | +$attributeDefinition[$perunProxyIdentifierAttr] = $proxyIdentifier; |
| 49 | +$facilities = $rpcAdapter->searchFacilitiesByAttributeValue($attributeDefinition); |
| 50 | + |
| 51 | +// Get facilities with attributes |
| 52 | +$facilitiesWithAttributes = array(); |
| 53 | +foreach ($facilities as $facility) { |
| 54 | + $attributes = $rpcAdapter->getFacilityAttributes($facility, $allAttrNames); |
| 55 | + $facilityAttributes = array(); |
| 56 | + foreach ($attributes as $attribute) { |
| 57 | + $facilityAttributes[$attribute['name']] = $attribute; |
| 58 | + } |
| 59 | + $facilitiesWithAttributes[$facility->getId()] = array( |
| 60 | + 'facility' => $facility, |
| 61 | + FACILITY_ATTRIBUTES=> $facilityAttributes, |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +// Generate array with metadata |
| 66 | +$content = '<?php' . PHP_EOL; |
| 67 | +foreach ($facilitiesWithAttributes as $facilityWithAttributes) { |
| 68 | + $metadataContent = ''; |
| 69 | + if (isset($facilityWithAttributes[FACILITY_ATTRIBUTES][$perunProxyEntityIDAttr]) && !empty($facilityWithAttributes[FACILITY_ATTRIBUTES][$perunProxyEntityIDAttr]['value'])) { |
| 70 | + $metadataContent .= '$metadata[\'' . $facilityWithAttributes[FACILITY_ATTRIBUTES][$perunProxyEntityIDAttr]['value'] . '\'] = array(' . PHP_EOL; |
| 71 | + foreach ($attributesDefinitions as $perunAttrName => $metadataAttrName) { |
| 72 | + $attribute = $facilityWithAttributes[FACILITY_ATTRIBUTES][$perunAttrName]; |
| 73 | + if (($attribute['type'] === TYPE_INTEGER ) && is_numeric($attribute['value']) && !is_null($attribute['value'])) { |
| 74 | + $metadataContent .= "\t '" . $metadataAttrName . "' => " . $attribute['value'] . "," . PHP_EOL ; |
| 75 | + } elseif (($attribute['type'] === TYPE_STRING) && !is_null($attribute['value'])) { |
| 76 | + $metadataContent .= "\t '" . $metadataAttrName . "' => '" . $attribute['value'] . "'," . PHP_EOL ; |
| 77 | + } elseif ($attribute['type'] === TYPE_BOOLEAN) { |
| 78 | + $metadataContent .= "\t '" . $metadataAttrName . "' => " ; |
| 79 | + if (is_null($attribute['value']) || $attribute['value'] === 'false') { |
| 80 | + $metadataContent .= "false," . PHP_EOL; |
| 81 | + } else { |
| 82 | + $metadataContent .= "true," . PHP_EOL; |
| 83 | + } |
| 84 | + } elseif ($attribute['type'] === TYPE_ARRAY && !is_null($attribute['value'])) { |
| 85 | + $metadataContent .= "\t '" . $metadataAttrName . "' => array(" . PHP_EOL; |
| 86 | + foreach ($attribute['value'] as $value) { |
| 87 | + $metadataContent .= "\t\t'" . $value . "'," . PHP_EOL; |
| 88 | + } |
| 89 | + $metadataContent .= "\t)," . PHP_EOL; |
| 90 | + } elseif ($attribute['type'] === TYPE_MAP && !is_null($attribute['value'])) { |
| 91 | + $metadataContent .= "\t '" . $metadataAttrName . "' => array(" . PHP_EOL; |
| 92 | + foreach ($attribute['value'] as $key => $value) { |
| 93 | + $metadataContent .= "\t\t'" . $key . "' => '" . $value . "'," . PHP_EOL; |
| 94 | + } |
| 95 | + $metadataContent .= "\t)," . PHP_EOL; |
| 96 | + } |
| 97 | + } |
| 98 | + $metadataContent .= ");" . PHP_EOL . "\n"; |
| 99 | + } |
| 100 | + $content .= $metadataContent; |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +file_put_contents($absoluteFileName, $content,LOCK_EX); |
| 105 | + |
| 106 | +if (file_exists($absoluteFileName)) { |
| 107 | + header('Content-Description: File Transfer'); |
| 108 | + header('Content-Type: application/octet-stream'); |
| 109 | + header('Content-Disposition: attachment; filename="' . basename($absoluteFileName) . '"'); |
| 110 | + header('Expires: 0'); |
| 111 | + header('Cache-Control: must-revalidate'); |
| 112 | + header('Pragma: public'); |
| 113 | + header('Content-Length: ' . filesize($absoluteFileName)); |
| 114 | + readfile($absoluteFileName); |
| 115 | + exit; |
| 116 | +} |
0 commit comments