ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
88  if ($entry[0] === '.') {
89  // skip '..', '.' and hidden files
90  continue;
91  }
92 
93  $path = $this->directory.'/'.$entry;
94 
95  if (!is_dir($path)) {
97  'Serialize metadata handler: Metadata directory contained a file where only directories should '.
98  'exist: '.var_export($path, true)
99  );
100  continue;
101  }
102 
103  $ret[] = rawurldecode($entry);
104  }
105 
106  closedir($dh);
107 
108  return $ret;
109  }
110 
111 
119  public function getMetadataSet($set)
120  {
121  assert('is_string($set)');
122 
123  $ret = array();
124 
125  $dir = $this->directory.'/'.rawurlencode($set);
126  if (!is_dir($dir)) {
127  // probably some code asked for a metadata set which wasn't available
128  return $ret;
129  }
130 
131  $dh = @opendir($dir);
132  if ($dh === false) {
133  SimpleSAML\Logger::warning('Serialize metadata handler: Unable to open directory: '.var_export($dir, true));
134  return $ret;
135  }
136 
137  $extLen = strlen(self::EXTENSION);
138 
139  while (($file = readdir($dh)) !== false) {
140  if (strlen($file) <= $extLen) {
141  continue;
142  }
143 
144  if (substr($file, -$extLen) !== self::EXTENSION) {
145  continue;
146  }
147 
148  $entityId = substr($file, 0, -$extLen);
149  $entityId = rawurldecode($entityId);
150 
151  $md = $this->getMetaData($entityId, $set);
152  if ($md !== null) {
153  $ret[$entityId] = $md;
154  }
155  }
156 
157  closedir($dh);
158 
159  return $ret;
160  }
161 
162 
172  public function getMetaData($entityId, $set)
173  {
174  assert('is_string($entityId)');
175  assert('is_string($set)');
176 
177  $filePath = $this->getMetadataPath($entityId, $set);
178 
179  if (!file_exists($filePath)) {
180  return null;
181  }
182 
183  $data = @file_get_contents($filePath);
184  if ($data === false) {
185  $error = error_get_last();
187  'Error reading file '.$filePath.': '.$error['message']
188  );
189  return null;
190  }
191 
192  $data = @unserialize($data);
193  if ($data === false) {
194  SimpleSAML\Logger::warning('Error unserializing file: '.$filePath);
195  return null;
196  }
197 
198  if (!array_key_exists('entityid', $data)) {
199  $data['entityid'] = $entityId;
200  }
201 
202  return $data;
203  }
204 
205 
215  public function saveMetadata($entityId, $set, $metadata)
216  {
217  assert('is_string($entityId)');
218  assert('is_string($set)');
219  assert('is_array($metadata)');
220 
221  $filePath = $this->getMetadataPath($entityId, $set);
222  $newPath = $filePath.'.new';
223 
224  $dir = dirname($filePath);
225  if (!is_dir($dir)) {
226  SimpleSAML\Logger::info('Creating directory: '.$dir);
227  $res = @mkdir($dir, 0777, true);
228  if ($res === false) {
229  $error = error_get_last();
230  SimpleSAML\Logger::error('Failed to create directory '.$dir.': '.$error['message']);
231  return false;
232  }
233  }
234 
235  $data = serialize($metadata);
236 
237  SimpleSAML\Logger::debug('Writing: '.$newPath);
238 
239  $res = file_put_contents($newPath, $data);
240  if ($res === false) {
241  $error = error_get_last();
242  SimpleSAML\Logger::error('Error saving file '.$newPath.': '.$error['message']);
243  return false;
244  }
245 
246  $res = rename($newPath, $filePath);
247  if ($res === false) {
248  $error = error_get_last();
249  SimpleSAML\Logger::error('Error renaming '.$newPath.' to '.$filePath.': '.$error['message']);
250  return false;
251  }
252 
253  return true;
254  }
255 
256 
263  public function deleteMetadata($entityId, $set)
264  {
265  assert('is_string($entityId)');
266  assert('is_string($set)');
267 
268  $filePath = $this->getMetadataPath($entityId, $set);
269 
270  if (!file_exists($filePath)) {
272  'Attempted to erase nonexistent metadata entry '.
273  var_export($entityId, true).' in set '.var_export($set, true).'.'
274  );
275  return;
276  }
277 
278  $res = unlink($filePath);
279  if ($res === false) {
280  $error = error_get_last();
282  'Failed to delete file '.$filePath.
283  ': '.$error['message']
284  );
285  }
286  }
287 }
static debug($string)
Definition: Logger.php:213
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__']
$error
Definition: Error.php:17
static info($string)
Definition: Logger.php:201
foreach($_POST as $key=> $value) $res
static warning($string)
Definition: Logger.php:179
static error($string)
Definition: Logger.php:168
Create styles array
The data for the language used.
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.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
__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.