sspmod_metarefresh_MetaLoader::writeMetadataFiles PHP Method

writeMetadataFiles() public method

This function writes the metadata to to separate files in the output directory.
public writeMetadataFiles ( $outputDir )
    function writeMetadataFiles($outputDir)
    {
        while (strlen($outputDir) > 0 && $outputDir[strlen($outputDir) - 1] === '/') {
            $outputDir = substr($outputDir, 0, strlen($outputDir) - 1);
        }
        if (!file_exists($outputDir)) {
            SimpleSAML\Logger::info('Creating directory: ' . $outputDir . "\n");
            $res = @mkdir($outputDir, 0777, TRUE);
            if ($res === FALSE) {
                throw new Exception('Error creating directory: ' . $outputDir);
            }
        }
        foreach ($this->types as $type) {
            $filename = $outputDir . '/' . $type . '.php';
            if (array_key_exists($type, $this->metadata)) {
                $elements = $this->metadata[$type];
                SimpleSAML\Logger::debug('Writing: ' . $filename);
                $content = '<?php' . "\n" . '/* This file was generated by the metarefresh module at ' . $this->getTime() . "\n";
                $content .= ' Do not update it manually as it will get overwritten' . "\n" . '*/' . "\n";
                foreach ($elements as $m) {
                    $entityID = $m['metadata']['entityid'];
                    $content .= "\n";
                    $content .= '$metadata[\'' . addslashes($entityID) . '\'] = ' . var_export($m['metadata'], TRUE) . ';' . "\n";
                }
                $content .= "\n" . '?>';
                SimpleSAML\Utils\System::writeFile($filename, $content, 0644);
            } elseif (is_file($filename)) {
                if (unlink($filename)) {
                    SimpleSAML\Logger::debug('Deleting stale metadata file: ' . $filename);
                } else {
                    SimpleSAML\Logger::warning('Could not delete stale metadata file: ' . $filename);
                }
            }
        }
    }

Usage Example

示例#1
0
/**
 * Hook to run a cron job.
 *
 * @param array &$croninfo  Output
 */
function metarefresh_hook_cron(&$croninfo)
{
    assert('is_array($croninfo)');
    assert('array_key_exists("summary", $croninfo)');
    assert('array_key_exists("tag", $croninfo)');
    SimpleSAML_Logger::info('cron [metarefresh]: Running cron in cron tag [' . $croninfo['tag'] . '] ');
    try {
        $config = SimpleSAML_Configuration::getInstance();
        $mconfig = SimpleSAML_Configuration::getConfig('config-metarefresh.php');
        $sets = $mconfig->getConfigList('sets');
        foreach ($sets as $setkey => $set) {
            // Only process sets where cron matches the current cron tag.
            $cronTags = $set->getArray('cron');
            if (!in_array($croninfo['tag'], $cronTags)) {
                continue;
            }
            SimpleSAML_Logger::info('cron [metarefresh]: Executing set [' . $setkey . ']');
            $expireAfter = $set->getInteger('expireAfter', NULL);
            if ($expireAfter !== NULL) {
                $expire = time() + $expireAfter;
            } else {
                $expire = NULL;
            }
            $metaloader = new sspmod_metarefresh_MetaLoader($expire);
            foreach ($set->getArray('sources') as $source) {
                SimpleSAML_Logger::debug('cron [metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']');
                $metaloader->loadSource($source);
            }
            $outputDir = $set->getString('outputDir');
            $outputDir = $config->resolvePath($outputDir);
            $outputFormat = $set->getValueValidate('outputFormat', array('flatfile', 'serialize'), 'flatfile');
            switch ($outputFormat) {
                case 'flatfile':
                    $metaloader->writeMetadataFiles($outputDir);
                    break;
                case 'serialize':
                    $metaloader->writeMetadataSerialize($outputDir);
                    break;
            }
            if ($set->hasValue('arp')) {
                $arpconfig = SimpleSAML_Configuration::loadFromArray($set->getValue('arp'));
                $metaloader->writeARPfile($arpconfig);
            }
        }
    } catch (Exception $e) {
        $croninfo['summary'][] = 'Error during metarefresh: ' . $e->getMessage();
    }
}
All Usage Examples Of sspmod_metarefresh_MetaLoader::writeMetadataFiles