From c754cda5e971583ddd3c5f45abfec021d7f8bb08 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no>
Date: Sat, 29 Nov 2008 07:54:42 +0000
Subject: [PATCH] Implementing cron hook in metarefresh module...

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1032 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/metarefresh/hooks/hook_cron.php | 43 +++++++++++++++++++++++++
 modules/metarefresh/lib/MetaLoader.php  | 16 ++++-----
 2 files changed, 51 insertions(+), 8 deletions(-)
 create mode 100644 modules/metarefresh/hooks/hook_cron.php

diff --git a/modules/metarefresh/hooks/hook_cron.php b/modules/metarefresh/hooks/hook_cron.php
new file mode 100644
index 000000000..4065ff43c
--- /dev/null
+++ b/modules/metarefresh/hooks/hook_cron.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * 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 = $config->copyFromBase('mconfig', 'config-metarefresh.php');
+		
+		$sets = $mconfig->getValue('sets');
+		if (count($sets) < 1) return; 
+	
+		foreach ($sets AS $setkey => $set) {
+			// Only process sets where cron matches the current cron tag.
+			if (!in_array($croninfo['tag'], $set['cron'])) continue;
+	
+			SimpleSAML_Logger::info('cron [metarefresh]: Executing set [' . $setkey . ']');
+	
+			$maxcache = NULL; if (array_key_exists('maxcache', $set)) $maxcache = $set['maxcache'];
+			$maxduration = NULL; if (array_key_exists('maxduration', $set)) $maxcache = $set['maxduration'];
+			$metaloader = new sspmod_metarefresh_MetaLoader($maxcache, $maxduration);		
+			
+			foreach($set['sources'] AS $source) {
+				SimpleSAML_Logger::debug('cron [metarefresh]: In set [' . $setkey . '] loading source ['  . $source['src'] . ']');
+				$metaloader->loadSource($source);
+			}
+			$metaloader->writeMetadataFiles($config->resolvePath($set['outputDir']));
+		}
+	
+	} catch (Exception $e) {
+		$croninfo['summary'][] = 'Error during metarefresh: ' . $e->getMessage();
+	}
+	
+}
+?>
\ No newline at end of file
diff --git a/modules/metarefresh/lib/MetaLoader.php b/modules/metarefresh/lib/MetaLoader.php
index 8d7b67df7..4bc0947a8 100644
--- a/modules/metarefresh/lib/MetaLoader.php
+++ b/modules/metarefresh/lib/MetaLoader.php
@@ -30,21 +30,21 @@ class sspmod_metarefresh_MetaLoader {
 	 *
 	 * @param $src  Filename of the metadata file.
 	 */
-	public function loadSource($src, $validateFingerprint = NULL, $template = NULL) {
+	public function loadSource($source, $validateFingerprint = NULL, $template = NULL) {
 
-		$entities = SimpleSAML_Metadata_SAMLParser::parseDescriptorsFile($src);
+		$entities = SimpleSAML_Metadata_SAMLParser::parseDescriptorsFile($source['src']);
 	
 		foreach($entities as $entity) {
 			if($validateFingerprint !== NULL) {
 				if(!$entity->validateFingerprint($validateFingerprint)) {
-					echo('Skipping "' . $entity->getEntityId() . '" - could not verify signature.' . "\n");
+					SimpleSAML_Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify signature.' . "\n");
 					continue;
 				}
 			}
 	
 			if($ca !== NULL) {
 				if(!$entity->validateCA($ca)) {
-					echo('Skipping "' . $entity->getEntityId() . '" - could not verify certificate.' . "\n");
+					SimpleSAML_Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify certificate.' . "\n");
 					continue;
 				}
 			}
@@ -122,7 +122,7 @@ class sspmod_metarefresh_MetaLoader {
 		}
 	
 		if(!file_exists($outputDir)) {
-			echo('Creating directory: ' . $outputDir . "\n");
+			SimpleSAML_Logger::info('Creating directory: ' . $outputDir . "\n");
 			mkdir($outputDir, 0777, TRUE);
 		}
 	
@@ -130,11 +130,11 @@ class sspmod_metarefresh_MetaLoader {
 	
 			$filename = $outputDir . '/' . $category . '.php';
 	
-			echo('Writing: ' . $filename . "\n");
+			SimpleSAML_Logger::debug('Writing: ' . $filename . "\n");
 	
-			$fh = fopen($filename, 'w');
+			$fh = @fopen($filename, 'w');
 			if($fh === FALSE) {
-				echo('Failed to open file for writing: ' . $filename . "\n");
+				throw new Exception('Failed to open file for writing: ' . $filename . "\n");
 				exit(1);
 			}
 	
-- 
GitLab