ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
MetaDataStorageHandlerPdo.php
Go to the documentation of this file.
1 <?php
2 
3 
15 {
16 
20  private $db;
21 
25  private $tablePrefix;
26 
30  private $cachedMetadata = array();
31 
36  'adfs-idp-hosted',
37  'adfs-sp-remote',
38  'saml20-idp-hosted',
39  'saml20-idp-remote',
40  'saml20-sp-remote',
41  'shib13-idp-hosted',
42  'shib13-idp-remote',
43  'shib13-sp-hosted',
44  'shib13-sp-remote',
45  'wsfed-idp-remote',
46  'wsfed-sp-hosted'
47  );
48 
49 
61  public function __construct($config)
62  {
63  assert('is_array($config)');
64 
66  }
67 
68 
81  private function load($set)
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  }
111 
112 
120  public function getMetadataSet($set)
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  }
144 
145 
146  private function generateDynamicHostedEntityID($set)
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  }
169 
170 
180  public function addEntry($index, $set, $entityData)
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  }
220 
221 
230  private function getTableName($table)
231  {
232  assert('is_string($table)');
233 
234  return $this->db->applyPrefix(str_replace("-", "_", $this->tablePrefix.$table));
235  }
236 
237 
243  public function initDatabase()
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  }
264 
265 }
$params
Definition: disable.php:11
getTableName($table)
Replace the -&#39;s to an _ in table names for Metadata sets since SQL does not allow a - in a table name...
static getInstance($altConfig=null)
Retrieves the current database instance.
Definition: Database.php:55
$baseurl
Definition: demo.php:25
addEntry($index, $set, $entityData)
Add metadata to the configured database.
$index
Definition: metadata.php:60
__construct($config)
This constructor initializes the PDO metadata storage handler with the specified configuration.
$metadata['__DYNAMIC:1__']
getMetadataSet($set)
Retrieve a list of all available metadata for a given set.
$supportedSets
All the metadata sets supported by this MetaDataStorageHandler.
initDatabase()
Initialize the configured database.
$tablePrefix
Prefix to apply to the metadata table.
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
$rows
Definition: xhr_table.php:10
if(empty($password)) $table
Definition: pwgen.php:24
static getBaseURL()
Retrieve the base URL of the SimpleSAMLphp installation.
Definition: HTTP.php:598
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
$cachedMetadata
This is an associative array which stores the different metadata sets we have loaded.