ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SimpleSAML_Metadata_MetaDataStorageHandlerPdo Class Reference
+ Inheritance diagram for SimpleSAML_Metadata_MetaDataStorageHandlerPdo:
+ Collaboration diagram for SimpleSAML_Metadata_MetaDataStorageHandlerPdo:

Public Member Functions

 __construct ($config)
 This constructor initializes the PDO metadata storage handler with the specified configuration. More...
 
 getMetadataSet ($set)
 Retrieve a list of all available metadata for a given set. More...
 
 addEntry ($index, $set, $entityData)
 Add metadata to the configured database. More...
 
 initDatabase ()
 Initialize the configured database. More...
 

Data Fields

 $supportedSets
 All the metadata sets supported by this MetaDataStorageHandler. More...
 

Private Member Functions

 load ($set)
 This function loads the given set of metadata from a file to a configured database. More...
 
 generateDynamicHostedEntityID ($set)
 
 getTableName ($table)
 Replace the -'s to an _ in table names for Metadata sets since SQL does not allow a - in a table name. More...
 

Private Attributes

 $db
 The PDO object. More...
 
 $tablePrefix
 Prefix to apply to the metadata table. More...
 
 $cachedMetadata = array()
 This is an associative array which stores the different metadata sets we have loaded. More...
 

Additional Inherited Members

Detailed Description

Definition at line 14 of file MetaDataStorageHandlerPdo.php.

Constructor & Destructor Documentation

◆ __construct()

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::__construct (   $config)

This constructor initializes the PDO metadata storage handler with the specified configuration.

The configuration is an associative array with the following possible elements (set in config.php):

  • 'usePersistentConnection': TRUE/FALSE if database connection should be persistent.
  • 'dsn': The database connection string.
  • 'username': Database user name
  • 'password': Password for the database user.
Parameters
array$configAn associative array with the configuration for this handler.

Definition at line 61 of file MetaDataStorageHandlerPdo.php.

References SimpleSAML\Database\getInstance().

62  {
63  assert('is_array($config)');
64 
66  }
static getInstance($altConfig=null)
Retrieves the current database instance.
Definition: Database.php:55
+ Here is the call graph for this function:

Member Function Documentation

◆ addEntry()

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::addEntry (   $index,
  $set,
  $entityData 
)

Add metadata to the configured database.

Parameters
string$indexEntity ID
string$setThe set to add the metadata to
array$entityDataMetadata
Returns
bool True/False if entry was successfully added

Definition at line 180 of file MetaDataStorageHandlerPdo.php.

References $index, $metadata, $params, $rows, array, and getTableName().

181  {
182  assert('is_string($index)');
183  assert('is_string($set)');
184  assert('is_array($entityData)');
185 
186  if (!in_array($set, $this->supportedSets, true)) {
187  return false;
188  }
189 
190  $tableName = $this->getTableName($set);
191 
192  $metadata = $this->db->read(
193  "SELECT entity_id, entity_data FROM $tableName WHERE entity_id = :entity_id",
194  array(
195  'entity_id' => $index,
196  )
197  );
198 
199  $retrivedEntityIDs = $metadata->fetch();
200 
201  $params = array(
202  'entity_id' => $index,
203  'entity_data' => json_encode($entityData),
204  );
205 
206  if ($retrivedEntityIDs !== false && count($retrivedEntityIDs) > 0) {
207  $rows = $this->db->write(
208  "UPDATE $tableName SET entity_data = :entity_data WHERE entity_id = :entity_id",
209  $params
210  );
211  } else {
212  $rows = $this->db->write(
213  "INSERT INTO $tableName (entity_id, entity_data) VALUES (:entity_id, :entity_data)",
214  $params
215  );
216  }
217 
218  return $rows === 1;
219  }
$params
Definition: disable.php:11
getTableName($table)
Replace the -'s to an _ in table names for Metadata sets since SQL does not allow a - in a table name...
$index
Definition: metadata.php:60
$metadata['__DYNAMIC:1__']
Create styles array
The data for the language used.
$rows
Definition: xhr_table.php:10
+ Here is the call graph for this function:

◆ generateDynamicHostedEntityID()

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::generateDynamicHostedEntityID (   $set)
private

Definition at line 146 of file MetaDataStorageHandlerPdo.php.

References $baseurl, and SimpleSAML\Utils\HTTP\getBaseURL().

Referenced by getMetadataSet().

147  {
148  assert('is_string($set)');
149 
150  // get the configuration
152 
153  if ($set === 'saml20-idp-hosted') {
154  return $baseurl.'saml2/idp/metadata.php';
155  } elseif ($set === 'saml20-sp-hosted') {
156  return $baseurl.'saml2/sp/metadata.php';
157  } elseif ($set === 'shib13-idp-hosted') {
158  return $baseurl.'shib13/idp/metadata.php';
159  } elseif ($set === 'shib13-sp-hosted') {
160  return $baseurl.'shib13/sp/metadata.php';
161  } elseif ($set === 'wsfed-sp-hosted') {
162  return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost();
163  } elseif ($set === 'adfs-idp-hosted') {
164  return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp';
165  } else {
166  throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']');
167  }
168  }
$baseurl
Definition: demo.php:25
static getBaseURL()
Retrieve the base URL of the SimpleSAMLphp installation.
Definition: HTTP.php:598
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMetadataSet()

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::getMetadataSet (   $set)

Retrieve a list of all available metadata for a given set.

Parameters
string$setThe set we are looking for metadata in.
Returns
array $metadata An associative array with all the metadata for the given set.

Definition at line 120 of file MetaDataStorageHandlerPdo.php.

References $entityId, array, generateDynamicHostedEntityID(), and load().

121  {
122  assert('is_string($set)');
123 
124  if (array_key_exists($set, $this->cachedMetadata)) {
125  return $this->cachedMetadata[$set];
126  }
127 
128  $metadataSet = $this->load($set);
129  if ($metadataSet === null) {
130  $metadataSet = array();
131  }
132 
133  foreach ($metadataSet as $entityId => &$entry) {
134  if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
135  $entry['entityid'] = $this->generateDynamicHostedEntityID($set);
136  } else {
137  $entry['entityid'] = $entityId;
138  }
139  }
140 
141  $this->cachedMetadata[$set] = $metadataSet;
142  return $metadataSet;
143  }
Create styles array
The data for the language used.
load($set)
This function loads the given set of metadata from a file to a configured database.
if($source===null) if(!($source instanceof sspmod_saml_Auth_Source_SP)) $entityId
Definition: metadata.php:22
+ Here is the call graph for this function:

◆ getTableName()

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::getTableName (   $table)
private

Replace the -'s to an _ in table names for Metadata sets since SQL does not allow a - in a table name.

Parameters
string$tableTable
Returns
string Replaced table name

Definition at line 230 of file MetaDataStorageHandlerPdo.php.

Referenced by addEntry(), initDatabase(), and load().

231  {
232  assert('is_string($table)');
233 
234  return $this->db->applyPrefix(str_replace("-", "_", $this->tablePrefix.$table));
235  }
+ Here is the caller graph for this function:

◆ initDatabase()

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::initDatabase ( )

Initialize the configured database.

Returns
int|false The number of SQL statements successfully executed, false if some error occurred.

Definition at line 243 of file MetaDataStorageHandlerPdo.php.

References $rows, and getTableName().

244  {
245  $stmt = 0;
246  $fine = true;
247  foreach ($this->supportedSets as $set) {
248  $tableName = $this->getTableName($set);
249  $rows = $this->db->write(
250  "CREATE TABLE IF NOT EXISTS $tableName (entity_id VARCHAR(255) PRIMARY KEY NOT NULL, entity_data ".
251  "TEXT NOT NULL)"
252  );
253  if ($rows === 0) {
254  $fine = false;
255  } else {
256  $stmt += $rows;
257  }
258  }
259  if (!$fine) {
260  return false;
261  }
262  return $stmt;
263  }
getTableName($table)
Replace the -'s to an _ in table names for Metadata sets since SQL does not allow a - in a table name...
$rows
Definition: xhr_table.php:10
+ Here is the call graph for this function:

◆ load()

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::load (   $set)
private

This function loads the given set of metadata from a file to a configured database.

This function returns NULL if it is unable to locate the given set in the metadata directory.

Parameters
string$setThe set of metadata we are loading.
Returns
array $metadata Associative array with the metadata, or NULL if we are unable to load metadata from the given file.
Exceptions
ExceptionIf a database error occurs.
SimpleSAML_Error_ExceptionIf the metadata can be retrieved from the database, but cannot be decoded.

Definition at line 81 of file MetaDataStorageHandlerPdo.php.

References $d, $data, $metadata, array, and getTableName().

Referenced by getMetadataSet().

82  {
83  assert('is_string($set)');
84 
85  $tableName = $this->getTableName($set);
86 
87  if (!in_array($set, $this->supportedSets, true)) {
88  return null;
89  }
90 
91  $stmt = $this->db->read("SELECT entity_id, entity_data FROM $tableName");
92  if ($stmt->execute()) {
93  $metadata = array();
94 
95  while ($d = $stmt->fetch()) {
96  $data = json_decode($d['entity_data'], true);
97  if ($data === null) {
98  throw new SimpleSAML_Error_Exception("Cannot decode metadata for entity '${d['entity_id']}'");
99  }
100  if (!array_key_exists('entityid', $data)) {
101  $data['entityid'] = $d['entity_id'];
102  }
103  $metadata[$d['entity_id']] = $data;
104  }
105 
106  return $metadata;
107  } else {
108  throw new Exception('PDO metadata handler: Database error: '.var_export($this->db->getLastError(), true));
109  }
110  }
getTableName($table)
Replace the -'s to an _ in table names for Metadata sets since SQL does not allow a - in a table name...
$metadata['__DYNAMIC:1__']
Create styles array
The data for the language used.
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $cachedMetadata

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::$cachedMetadata = array()
private

This is an associative array which stores the different metadata sets we have loaded.

Definition at line 30 of file MetaDataStorageHandlerPdo.php.

◆ $db

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::$db
private

The PDO object.

Definition at line 20 of file MetaDataStorageHandlerPdo.php.

◆ $supportedSets

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::$supportedSets
Initial value:
'adfs-idp-hosted',
'adfs-sp-remote',
'saml20-idp-hosted',
'saml20-idp-remote',
'saml20-sp-remote',
'shib13-idp-hosted',
'shib13-idp-remote',
'shib13-sp-hosted',
'shib13-sp-remote',
'wsfed-idp-remote',
'wsfed-sp-hosted'
)

All the metadata sets supported by this MetaDataStorageHandler.

Definition at line 35 of file MetaDataStorageHandlerPdo.php.

◆ $tablePrefix

SimpleSAML_Metadata_MetaDataStorageHandlerPdo::$tablePrefix
private

Prefix to apply to the metadata table.

Definition at line 25 of file MetaDataStorageHandlerPdo.php.


The documentation for this class was generated from the following file: