ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MetaDataStorageHandlerSerialize.php
Go to the documentation of this file.
1 <?php
2 
3 
10 {
11 
17  const EXTENSION = '.serialized';
18 
19 
25  private $directory;
26 
27 
35  public function __construct($config)
36  {
37  assert(is_array($config));
38 
40 
41  $cfgHelp = SimpleSAML_Configuration::loadFromArray($config, 'serialize metadata source');
42 
43  $this->directory = $cfgHelp->getString('directory');
44 
45  /* Resolve this directory relative to the SimpleSAMLphp directory (unless it is
46  * an absolute path).
47  */
48  $this->directory = $globalConfig->resolvePath($this->directory);
49  }
50 
51 
60  private function getMetadataPath($entityId, $set)
61  {
62  assert(is_string($entityId));
63  assert(is_string($set));
64 
65  return $this->directory.'/'.rawurlencode($set).'/'.rawurlencode($entityId).self::EXTENSION;
66  }
67 
68 
74  public function getMetadataSets()
75  {
76  $ret = array();
77 
78  $dh = @opendir($this->directory);
79  if ($dh === false) {
81  'Serialize metadata handler: Unable to open directory: '.var_export($this->directory, true)
82  );
83  return $ret;
84  }
85 
86  while (($entry = readdir($dh)) !== false) {
87  if ($entry[0] === '.') {
88  // skip '..', '.' and hidden files
89  continue;
90  }
91 
92  $path = $this->directory.'/'.$entry;
93 
94  if (!is_dir($path)) {
96  'Serialize metadata handler: Metadata directory contained a file where only directories should '.
97  'exist: '.var_export($path, true)
98  );
99  continue;
100  }
101 
102  $ret[] = rawurldecode($entry);
103  }
104 
105  closedir($dh);
106 
107  return $ret;
108  }
109 
110 
118  public function getMetadataSet($set)
119  {
120  assert(is_string($set));
121 
122  $ret = array();
123 
124  $dir = $this->directory.'/'.rawurlencode($set);
125  if (!is_dir($dir)) {
126  // probably some code asked for a metadata set which wasn't available
127  return $ret;
128  }
129 
130  $dh = @opendir($dir);
131  if ($dh === false) {
132  SimpleSAML\Logger::warning('Serialize metadata handler: Unable to open directory: '.var_export($dir, true));
133  return $ret;
134  }
135 
136  $extLen = strlen(self::EXTENSION);
137 
138  while (($file = readdir($dh)) !== false) {
139  if (strlen($file) <= $extLen) {
140  continue;
141  }
142 
143  if (substr($file, -$extLen) !== self::EXTENSION) {
144  continue;
145  }
146 
147  $entityId = substr($file, 0, -$extLen);
148  $entityId = rawurldecode($entityId);
149 
150  $md = $this->getMetaData($entityId, $set);
151  if ($md !== null) {
152  $ret[$entityId] = $md;
153  }
154  }
155 
156  closedir($dh);
157 
158  return $ret;
159  }
160 
161 
171  public function getMetaData($entityId, $set)
172  {
173  assert(is_string($entityId));
174  assert(is_string($set));
175 
176  $filePath = $this->getMetadataPath($entityId, $set);
177 
178  if (!file_exists($filePath)) {
179  return null;
180  }
181 
182  $data = @file_get_contents($filePath);
183  if ($data === false) {
184  $error = error_get_last();
186  'Error reading file '.$filePath.': '.$error['message']
187  );
188  return null;
189  }
190 
191  $data = @unserialize($data);
192  if ($data === false) {
193  SimpleSAML\Logger::warning('Error unserializing file: '.$filePath);
194  return null;
195  }
196 
197  if (!array_key_exists('entityid', $data)) {
198  $data['entityid'] = $entityId;
199  }
200 
201  return $data;
202  }
203 
204 
214  public function saveMetadata($entityId, $set, $metadata)
215  {
216  assert(is_string($entityId));
217  assert(is_string($set));
218  assert(is_array($metadata));
219 
220  $filePath = $this->getMetadataPath($entityId, $set);
221  $newPath = $filePath.'.new';
222 
223  $dir = dirname($filePath);
224  if (!is_dir($dir)) {
225  SimpleSAML\Logger::info('Creating directory: '.$dir);
226  $res = @mkdir($dir, 0777, true);
227  if ($res === false) {
228  $error = error_get_last();
229  SimpleSAML\Logger::error('Failed to create directory '.$dir.': '.$error['message']);
230  return false;
231  }
232  }
233 
234  $data = serialize($metadata);
235 
236  SimpleSAML\Logger::debug('Writing: '.$newPath);
237 
238  $res = file_put_contents($newPath, $data);
239  if ($res === false) {
240  $error = error_get_last();
241  SimpleSAML\Logger::error('Error saving file '.$newPath.': '.$error['message']);
242  return false;
243  }
244 
245  $res = rename($newPath, $filePath);
246  if ($res === false) {
247  $error = error_get_last();
248  SimpleSAML\Logger::error('Error renaming '.$newPath.' to '.$filePath.': '.$error['message']);
249  return false;
250  }
251 
252  return true;
253  }
254 
255 
262  public function deleteMetadata($entityId, $set)
263  {
264  assert(is_string($entityId));
265  assert(is_string($set));
266 
267  $filePath = $this->getMetadataPath($entityId, $set);
268 
269  if (!file_exists($filePath)) {
271  'Attempted to erase nonexistent metadata entry '.
272  var_export($entityId, true).' in set '.var_export($set, true).'.'
273  );
274  return;
275  }
276 
277  $res = unlink($filePath);
278  if ($res === false) {
279  $error = error_get_last();
281  'Failed to delete file '.$filePath.
282  ': '.$error['message']
283  );
284  }
285  }
286 }
$path
Definition: aliased.php:25
$config
Definition: bootstrap.php:15
static debug($string)
Definition: Logger.php:211
getMetadataPath($entityId, $set)
Helper function for retrieving the path of a metadata file.
getMetadataSets()
Retrieve a list of all available metadata sets.
$metadata['__DYNAMIC:1__']
static info($string)
Definition: Logger.php:199
foreach($_POST as $key=> $value) $res
static warning($string)
Definition: Logger.php:177
static error($string)
Definition: Logger.php:166
if($source===null) if(!($source instanceof sspmod_saml_Auth_Source_SP)) $entityId
Definition: metadata.php:22
$globalConfig
getMetadataSet($set)
Retrieve a list of all available metadata for a given set.
$ret
Definition: parser.php:6
saveMetadata($entityId, $set, $metadata)
Save a metadata entry.
__construct($config)
Constructor for this metadata handler.
static loadFromArray($config, $location='[ARRAY]', $instance=null)
Loads a configuration from the given array.
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
$data
Definition: bench.php:6