<?php

$session = SimpleSAML_Session::getInstance();
$janusConfig = SimpleSAML_Configuration::getConfig('module_janus.php');
$authSource = $janusConfig->getValue('auth', 'login-admin');
// Validate user
if (!$session->isValid($authSource)) {
    SimpleSAML_Utilities::redirect(SimpleSAML_Module::getModuleURL('janus/index.php', array('selectedtab' => "'federation'")));
}
$entities = array();
$util = new sspmod_janus_AdminUtil();
$userController = new sspmod_janus_UserController($janusConfig);
$entities = array_merge($userController->searchEntitiesByType('saml20-idp'), $userController->searchEntitiesByType('saml20-sp'));
foreach ($entities as $entity) {
    /**
     * @var sspmod_janus_Entity $entity
     */
    $entityId = $entity->getEid();
    $entityController = new sspmod_janus_EntityController($janusConfig);
    $entityController->setEntity($entityId);
    $entityController->loadEntity();
    $controllerEntity = $entityController->getEntity();
    $entityType = $controllerEntity->getType();
    if (!isset($entities[$entityType])) {
        $entities[$entityType] = array();
    }
    $entities_info[$entityType][] = array('Id' => $controllerEntity->getEntityid(), 'Name' => $controllerEntity->getPrettyname(), 'WorkflowStatus' => $controllerEntity->getWorkflow(), 'MetadataUrl' => $controllerEntity->getMetadataURL(), 'Eid' => $controllerEntity->getEid());
}
ksort($entities_info);
$template = new SimpleSAML_XHTML_Template(SimpleSAML_Configuration::getInstance(), 'janus:show-entities-validation.php', 'janus:show-entities-validation');
$template->data['header'] = "Service Registry JANUS entities validation";
 /** 
  * Retrieve all entity metadata for all entities of a certain type.
  * @param String $type Supported types: "saml20-idp" or "saml20-sp"
  * @param Array $keys optional list of metadata keys to retrieve. Retrieves all if blank
  * @param String $allowedEntityId if passed, returns only those entities that are 
  *                         whitelisted against the given entity
  * @return Array Associative array of all metadata. The key of the array is the identifier
  */
 protected static function _getEntities($type, $keys = array(), $allowedEntityId = NULL)
 {
     $econtroller = new sspmod_janus_EntityController(SimpleSAML_Configuration::getConfig('module_janus.php'));
     $ucontroller = new sspmod_janus_UserController(SimpleSAML_Configuration::getConfig('module_janus.php'));
     $entities = array();
     if (isset($allowedEntityId)) {
         $econtroller->setEntity($allowedEntityId);
         $econtroller->loadEntity();
         if ($econtroller->getEntity()->getAllowedAll() == "yes") {
             $entities = $ucontroller->searchEntitiesByType($type);
         } else {
             $allowedEntities = $econtroller->getAllowedEntities();
             // Check the whitelist
             if (count($allowedEntities)) {
                 foreach ($allowedEntities as $entityid => $data) {
                     $entities[] = $data["remoteentityid"];
                 }
             } else {
                 // Check the blacklist
                 $blockedEntities = $econtroller->getBlockedEntities();
                 if (count($blockedEntities)) {
                     $blockedEntityIds = array();
                     foreach ($blockedEntities as $entityid => $data) {
                         $blockedEntityIds[] = $data["remoteentityid"];
                     }
                     $all = $ucontroller->searchEntitiesByType($type);
                     $list = array();
                     foreach ($all as $entity) {
                         $list[] = $entity->getEntityId();
                     }
                     // Return all entities that are not in the blacklist
                     $entities = array_diff($list, $blockedEntityIds);
                 }
             }
         }
     } else {
         $entities = $ucontroller->searchEntitiesByType($type);
     }
     $result = array();
     foreach ($entities as $entity) {
         $data = self::_getMetadataForEntity($entity, NULL, $keys);
         // Add workflow state info for optional filtering at client side
         $data['workflowState'] = $entity->getWorkflow();
         if (is_object($entity)) {
             $entityId = $entity->getEntityId();
         } else {
             $entityId = $entity;
         }
         $result[$entityId] = $data;
     }
     return $result;
 }