Skip to content

Commit c754cda

Browse files
Implementing cron hook in metarefresh module...
git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1032 44740490-163a-0410-bde0-09ae8108e29a
1 parent 4114af1 commit c754cda

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Hook to run a cron job.
4+
*
5+
* @param array &$croninfo Output
6+
*/
7+
function metarefresh_hook_cron(&$croninfo) {
8+
assert('is_array($croninfo)');
9+
assert('array_key_exists("summary", $croninfo)');
10+
assert('array_key_exists("tag", $croninfo)');
11+
12+
SimpleSAML_Logger::info('cron [metarefresh]: Running cron in cron tag [' . $croninfo['tag'] . '] ');
13+
14+
try {
15+
$config = SimpleSAML_Configuration::getInstance();
16+
$mconfig = $config->copyFromBase('mconfig', 'config-metarefresh.php');
17+
18+
$sets = $mconfig->getValue('sets');
19+
if (count($sets) < 1) return;
20+
21+
foreach ($sets AS $setkey => $set) {
22+
// Only process sets where cron matches the current cron tag.
23+
if (!in_array($croninfo['tag'], $set['cron'])) continue;
24+
25+
SimpleSAML_Logger::info('cron [metarefresh]: Executing set [' . $setkey . ']');
26+
27+
$maxcache = NULL; if (array_key_exists('maxcache', $set)) $maxcache = $set['maxcache'];
28+
$maxduration = NULL; if (array_key_exists('maxduration', $set)) $maxcache = $set['maxduration'];
29+
$metaloader = new sspmod_metarefresh_MetaLoader($maxcache, $maxduration);
30+
31+
foreach($set['sources'] AS $source) {
32+
SimpleSAML_Logger::debug('cron [metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']');
33+
$metaloader->loadSource($source);
34+
}
35+
$metaloader->writeMetadataFiles($config->resolvePath($set['outputDir']));
36+
}
37+
38+
} catch (Exception $e) {
39+
$croninfo['summary'][] = 'Error during metarefresh: ' . $e->getMessage();
40+
}
41+
42+
}
43+
?>

modules/metarefresh/lib/MetaLoader.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ public function __construct($maxcache = NULL, $maxduration = NULL) {
3030
*
3131
* @param $src Filename of the metadata file.
3232
*/
33-
public function loadSource($src, $validateFingerprint = NULL, $template = NULL) {
33+
public function loadSource($source, $validateFingerprint = NULL, $template = NULL) {
3434

35-
$entities = SimpleSAML_Metadata_SAMLParser::parseDescriptorsFile($src);
35+
$entities = SimpleSAML_Metadata_SAMLParser::parseDescriptorsFile($source['src']);
3636

3737
foreach($entities as $entity) {
3838
if($validateFingerprint !== NULL) {
3939
if(!$entity->validateFingerprint($validateFingerprint)) {
40-
echo('Skipping "' . $entity->getEntityId() . '" - could not verify signature.' . "\n");
40+
SimpleSAML_Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify signature.' . "\n");
4141
continue;
4242
}
4343
}
4444

4545
if($ca !== NULL) {
4646
if(!$entity->validateCA($ca)) {
47-
echo('Skipping "' . $entity->getEntityId() . '" - could not verify certificate.' . "\n");
47+
SimpleSAML_Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify certificate.' . "\n");
4848
continue;
4949
}
5050
}
@@ -122,19 +122,19 @@ function writeMetadataFiles($outputDir) {
122122
}
123123

124124
if(!file_exists($outputDir)) {
125-
echo('Creating directory: ' . $outputDir . "\n");
125+
SimpleSAML_Logger::info('Creating directory: ' . $outputDir . "\n");
126126
mkdir($outputDir, 0777, TRUE);
127127
}
128128

129129
foreach($this->metadata as $category => $elements) {
130130

131131
$filename = $outputDir . '/' . $category . '.php';
132132

133-
echo('Writing: ' . $filename . "\n");
133+
SimpleSAML_Logger::debug('Writing: ' . $filename . "\n");
134134

135-
$fh = fopen($filename, 'w');
135+
$fh = @fopen($filename, 'w');
136136
if($fh === FALSE) {
137-
echo('Failed to open file for writing: ' . $filename . "\n");
137+
throw new Exception('Failed to open file for writing: ' . $filename . "\n");
138138
exit(1);
139139
}
140140

0 commit comments

Comments
 (0)