Skip to content
Snippets Groups Projects
Commit 96d55973 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Reformat SimpleSAML_Metadata_MetaDataStorageHandlerSerialize.

parent 7513d23f
No related branches found
No related tags found
No related merge requests found
<?php <?php
/** /**
* Class for handling metadata files in serialized format. * Class for handling metadata files in serialized format.
* *
* @package simpleSAMLphp * @package SimpleSAMLphp
*/ */
class SimpleSAML_Metadata_MetaDataStorageHandlerSerialize extends SimpleSAML_Metadata_MetaDataStorageSource { class SimpleSAML_Metadata_MetaDataStorageHandlerSerialize extends SimpleSAML_Metadata_MetaDataStorageSource
{
/**
* The file extension we use for our metadata files. /**
*/ * The file extension we use for our metadata files.
const EXTENSION = '.serialized'; *
* @var string
*/
/** const EXTENSION = '.serialized';
* The base directory where metadata is stored.
*/
private $directory; /**
* The base directory where metadata is stored.
*
/** * @var string
* Constructor for this metadata handler. */
* private $directory;
* Parses configuration.
*
* @param array $config The configuration for this metadata handler. /**
*/ * Constructor for this metadata handler.
public function __construct($config) { *
assert('is_array($config)'); * Parses configuration.
*
$globalConfig = SimpleSAML_Configuration::getInstance(); * @param array $config The configuration for this metadata handler.
*/
$cfgHelp = SimpleSAML_Configuration::loadFromArray($config, 'serialize metadata source'); public function __construct($config)
{
$this->directory = $cfgHelp->getString('directory'); assert('is_array($config)');
/* Resolve this directory relative to the simpleSAMLphp directory (unless it is $globalConfig = SimpleSAML_Configuration::getInstance();
* an absolute path).
*/ $cfgHelp = SimpleSAML_Configuration::loadFromArray($config, 'serialize metadata source');
$this->directory = $globalConfig->resolvePath($this->directory);
} $this->directory = $cfgHelp->getString('directory');
/* Resolve this directory relative to the simpleSAMLphp directory (unless it is
/** * an absolute path).
* Helper function for retrieving the path of a metadata file. */
* $this->directory = $globalConfig->resolvePath($this->directory);
* @param string $entityId The entity ID. }
* @param string $set The metadata set.
* @return string The path to the metadata file.
*/ /**
private function getMetadataPath($entityId, $set) { * Helper function for retrieving the path of a metadata file.
assert('is_string($entityId)'); *
assert('is_string($set)'); * @param string $entityId The entity ID.
* @param string $set The metadata set.
return $this->directory . '/' . rawurlencode($set) . '/' . rawurlencode($entityId) . self::EXTENSION; *
} * @return string The path to the metadata file.
*/
private function getMetadataPath($entityId, $set)
/** {
* Retrieve a list of all available metadata sets. assert('is_string($entityId)');
* assert('is_string($set)');
* @return array An array with the available sets.
*/ return $this->directory.'/'.rawurlencode($set).'/'.rawurlencode($entityId).self::EXTENSION;
public function getMetadataSets() { }
$ret = array();
/**
$dh = @opendir($this->directory); * Retrieve a list of all available metadata sets.
if ($dh === FALSE) { *
SimpleSAML_Logger::warning('Serialize metadata handler: Unable to open directory: ' . var_export($this->directory, TRUE)); * @return array An array with the available sets.
return $ret; */
} public function getMetadataSets()
{
while ( ($entry = readdir($dh)) !== FALSE) { $ret = array();
if ($entry[0] === '.') { $dh = @opendir($this->directory);
/* Skip '..', '.' and hidden files. */ if ($dh === false) {
continue; SimpleSAML_Logger::warning(
} 'Serialize metadata handler: Unable to open directory: '.var_export($this->directory, true)
);
$path = $this->directory . '/' . $entry; return $ret;
}
if (!is_dir($path)) {
SimpleSAML_Logger::warning('Serialize metadata handler: Metadata directory contained a file where only directories should exist: ' . var_export($path, TRUE)); while (($entry = readdir($dh)) !== false) {
continue;
} if ($entry[0] === '.') {
// skip '..', '.' and hidden files
$ret[] = rawurldecode($entry); continue;
} }
closedir($dh); $path = $this->directory.'/'.$entry;
return $ret; if (!is_dir($path)) {
} SimpleSAML_Logger::warning(
'Serialize metadata handler: Metadata directory contained a file where only directories should '.
'exist: '.var_export($path, true)
/** );
* Retrieve a list of all available metadata for a given set. continue;
* }
* @param string $set The set we are looking for metadata in.
* @return array An associative array with all the metadata for the given set. $ret[] = rawurldecode($entry);
*/ }
public function getMetadataSet($set) {
assert('is_string($set)'); closedir($dh);
$ret = array(); return $ret;
}
$dir = $this->directory . '/' . rawurlencode($set);
if (!is_dir($dir)) {
/* Probably some code asked for a metadata set which wasn't available. */ /**
return $ret; * Retrieve a list of all available metadata for a given set.
} *
* @param string $set The set we are looking for metadata in.
$dh = @opendir($dir); *
if ($dh === FALSE) { * @return array An associative array with all the metadata for the given set.
SimpleSAML_Logger::warning('Serialize metadata handler: Unable to open directory: ' . var_export($dir, TRUE)); */
return $ret; public function getMetadataSet($set)
} {
assert('is_string($set)');
$extLen = strlen(self::EXTENSION);
$ret = array();
while ( ($file = readdir($dh)) !== FALSE) {
if (strlen($file) <= $extLen) { $dir = $this->directory.'/'.rawurlencode($set);
continue; if (!is_dir($dir)) {
} // probably some code asked for a metadata set which wasn't available
return $ret;
if (substr($file, -$extLen) !== self::EXTENSION) { }
continue;
} $dh = @opendir($dir);
if ($dh === false) {
$entityId = substr($file, 0, -$extLen); SimpleSAML_Logger::warning('Serialize metadata handler: Unable to open directory: '.var_export($dir, true));
$entityId = rawurldecode($entityId); return $ret;
}
$md = $this->getMetaData($entityId, $set);
if ($md !== NULL) { $extLen = strlen(self::EXTENSION);
$ret[$entityId] = $md;
} while (($file = readdir($dh)) !== false) {
} if (strlen($file) <= $extLen) {
continue;
closedir($dh); }
return $ret; if (substr($file, -$extLen) !== self::EXTENSION) {
} continue;
}
/** $entityId = substr($file, 0, -$extLen);
* Retrieve a metadata entry. $entityId = rawurldecode($entityId);
*
* @param string $entityId The entityId we are looking up. $md = $this->getMetaData($entityId, $set);
* @param string $set The set we are looking for metadata in. if ($md !== null) {
* @return array An associative array with metadata for the given entity, or NULL if we are unable to $ret[$entityId] = $md;
* locate the entity. }
*/ }
public function getMetaData($entityId, $set) {
assert('is_string($entityId)'); closedir($dh);
assert('is_string($set)');
return $ret;
$filePath = $this->getMetadataPath($entityId, $set); }
if (!file_exists($filePath)) {
return NULL; /**
} * Retrieve a metadata entry.
*
$data = @file_get_contents($filePath); * @param string $entityId The entityId we are looking up.
if ($data === FALSE) { * @param string $set The set we are looking for metadata in.
$error = error_get_last(); *
SimpleSAML_Logger::warning('Error reading file ' . $filePath . * @return array An associative array with metadata for the given entity, or NULL if we are unable to
': ' . $error['message']); * locate the entity.
return NULL; */
} public function getMetaData($entityId, $set)
{
$data = @unserialize($data); assert('is_string($entityId)');
if ($data === FALSE) { assert('is_string($set)');
SimpleSAML_Logger::warning('Error deserializing file: ' . $filePath);
return NULL; $filePath = $this->getMetadataPath($entityId, $set);
}
if (!file_exists($filePath)) {
return $data; return null;
} }
$data = @file_get_contents($filePath);
/** if ($data === false) {
* Save a metadata entry. $error = error_get_last();
* SimpleSAML_Logger::warning(
* @param string $entityId The entityId of the metadata entry. 'Error reading file '.$filePath.': '.$error['message']
* @param string $set The metadata set this metadata entry belongs to. );
* @param array $metadata The metadata. return null;
*/ }
public function saveMetadata($entityId, $set, $metadata) {
assert('is_string($entityId)'); $data = @unserialize($data);
assert('is_string($set)'); if ($data === false) {
assert('is_array($metadata)'); SimpleSAML_Logger::warning('Error unserializing file: '.$filePath);
return null;
$filePath = $this->getMetadataPath($entityId, $set); }
$newPath = $filePath . '.new';
return $data;
$dir = dirname($filePath); }
if (!is_dir($dir)) {
SimpleSAML_Logger::info('Creating directory: ' . $dir);
$res = @mkdir($dir, 0777, TRUE); /**
if ($res === FALSE) { * Save a metadata entry.
$error = error_get_last(); *
SimpleSAML_Logger::error('Failed to create directory ' . $dir . * @param string $entityId The entityId of the metadata entry.
': ' . $error['message']); * @param string $set The metadata set this metadata entry belongs to.
return FALSE; * @param array $metadata The metadata.
} *
} * @return boolean True if successfully saved, false otherwise.
*/
$data = serialize($metadata); public function saveMetadata($entityId, $set, $metadata)
{
SimpleSAML_Logger::debug('Writing: ' . $newPath); assert('is_string($entityId)');
assert('is_string($set)');
$res = file_put_contents($newPath, $data); assert('is_array($metadata)');
if ($res === FALSE) {
$error = error_get_last(); $filePath = $this->getMetadataPath($entityId, $set);
SimpleSAML_Logger::error('Error saving file ' . $newPath . $newPath = $filePath.'.new';
': ' . $error['message']);
return FALSE; $dir = dirname($filePath);
} if (!is_dir($dir)) {
SimpleSAML_Logger::info('Creating directory: '.$dir);
$res = rename($newPath, $filePath); $res = @mkdir($dir, 0777, true);
if ($res === FALSE) { if ($res === false) {
$error = error_get_last(); $error = error_get_last();
SimpleSAML_Logger::error('Error renaming ' . $newPath . ' to ' . $filePath . SimpleSAML_Logger::error('Failed to create directory '.$dir.': '.$error['message']);
': ' . $error['message']); return false;
return FALSE; }
} }
$data = serialize($metadata);
return TRUE;
} SimpleSAML_Logger::debug('Writing: '.$newPath);
$res = file_put_contents($newPath, $data);
/** if ($res === false) {
* Delete a metadata entry. $error = error_get_last();
* SimpleSAML_Logger::error('Error saving file '.$newPath.': '.$error['message']);
* @param string $entityId The entityId of the metadata entry. return false;
* @param string $set The metadata set this metadata entry belongs to. }
*/
public function deleteMetadata($entityId, $set) { $res = rename($newPath, $filePath);
assert('is_string($entityId)'); if ($res === false) {
assert('is_string($set)'); $error = error_get_last();
SimpleSAML_Logger::error('Error renaming '.$newPath.' to '.$filePath.': '.$error['message']);
$filePath = $this->getMetadataPath($entityId, $set); return false;
}
if (!file_exists($filePath)) {
SimpleSAML_Logger::warning('Attempted to erase non-existant metadata entry ' . return true;
var_export($entityId, TRUE) . ' in set ' . var_export($set, TRUE) . '.'); }
return;
}
/**
$res = unlink($filePath); * Delete a metadata entry.
if ($res === FALSE) { *
$error = error_get_last(); * @param string $entityId The entityId of the metadata entry.
SimpleSAML_Logger::error('Failed to delete file ' . $filePath . * @param string $set The metadata set this metadata entry belongs to.
': ' . $error['message']); */
} public function deleteMetadata($entityId, $set)
} {
assert('is_string($entityId)');
assert('is_string($set)');
$filePath = $this->getMetadataPath($entityId, $set);
if (!file_exists($filePath)) {
SimpleSAML_Logger::warning(
'Attempted to erase nonexistent metadata entry '.
var_export($entityId, true).' in set '.var_export($set, true).'.'
);
return;
}
$res = unlink($filePath);
if ($res === false) {
$error = error_get_last();
SimpleSAML_Logger::error(
'Failed to delete file '.$filePath.
': '.$error['message']
);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment