ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PDO.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Sabre\DAV;
8 
20 class PDO extends AbstractBackend implements CreatePrincipalSupport {
21 
27  public $tableName = 'principals';
28 
34  public $groupMembersTableName = 'groupmembers';
35 
41  protected $pdo;
42 
48  protected $fieldMap = [
49 
53  '{DAV:}displayname' => [
54  'dbField' => 'displayname',
55  ],
56 
60  '{http://sabredav.org/ns}email-address' => [
61  'dbField' => 'email',
62  ],
63  ];
64 
70  function __construct(\PDO $pdo) {
71 
72  $this->pdo = $pdo;
73 
74  }
75 
92  function getPrincipalsByPrefix($prefixPath) {
93 
94  $fields = [
95  'uri',
96  ];
97 
98  foreach ($this->fieldMap as $key => $value) {
99  $fields[] = $value['dbField'];
100  }
101  $result = $this->pdo->query('SELECT ' . implode(',', $fields) . ' FROM ' . $this->tableName);
102 
103  $principals = [];
104 
105  while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
106 
107  // Checking if the principal is in the prefix
108  list($rowPrefix) = URLUtil::splitPath($row['uri']);
109  if ($rowPrefix !== $prefixPath) continue;
110 
111  $principal = [
112  'uri' => $row['uri'],
113  ];
114  foreach ($this->fieldMap as $key => $value) {
115  if ($row[$value['dbField']]) {
116  $principal[$key] = $row[$value['dbField']];
117  }
118  }
119  $principals[] = $principal;
120 
121  }
122 
123  return $principals;
124 
125  }
126 
136 
137  $fields = [
138  'id',
139  'uri',
140  ];
141 
142  foreach ($this->fieldMap as $key => $value) {
143  $fields[] = $value['dbField'];
144  }
145  $stmt = $this->pdo->prepare('SELECT ' . implode(',', $fields) . ' FROM ' . $this->tableName . ' WHERE uri = ?');
146  $stmt->execute([$path]);
147 
148  $row = $stmt->fetch(\PDO::FETCH_ASSOC);
149  if (!$row) return;
150 
151  $principal = [
152  'id' => $row['id'],
153  'uri' => $row['uri'],
154  ];
155  foreach ($this->fieldMap as $key => $value) {
156  if ($row[$value['dbField']]) {
157  $principal[$key] = $row[$value['dbField']];
158  }
159  }
160  return $principal;
161 
162  }
163 
179  function updatePrincipal($path, DAV\PropPatch $propPatch) {
180 
181  $propPatch->handle(array_keys($this->fieldMap), function($properties) use ($path) {
182 
183  $query = "UPDATE " . $this->tableName . " SET ";
184  $first = true;
185 
186  $values = [];
187 
188  foreach ($properties as $key => $value) {
189 
190  $dbField = $this->fieldMap[$key]['dbField'];
191 
192  if (!$first) {
193  $query .= ', ';
194  }
195  $first = false;
196  $query .= $dbField . ' = :' . $dbField;
197  $values[$dbField] = $value;
198 
199  }
200 
201  $query .= " WHERE uri = :uri";
202  $values['uri'] = $path;
203 
204  $stmt = $this->pdo->prepare($query);
205  $stmt->execute($values);
206 
207  return true;
208 
209  });
210 
211  }
212 
242  function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') {
243  if (count($searchProperties) == 0) return []; //No criteria
244 
245  $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE ';
246  $values = [];
247  foreach ($searchProperties as $property => $value) {
248  switch ($property) {
249  case '{DAV:}displayname' :
250  $column = "displayname";
251  break;
252  case '{http://sabredav.org/ns}email-address' :
253  $column = "email";
254  break;
255  default :
256  // Unsupported property
257  return [];
258  }
259  if (count($values) > 0) $query .= (strcmp($test, "anyof") == 0 ? " OR " : " AND ");
260  $query .= 'lower(' . $column . ') LIKE lower(?)';
261  $values[] = '%' . $value . '%';
262 
263  }
264  $stmt = $this->pdo->prepare($query);
265  $stmt->execute($values);
266 
267  $principals = [];
268  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
269 
270  // Checking if the principal is in the prefix
271  list($rowPrefix) = URLUtil::splitPath($row['uri']);
272  if ($rowPrefix !== $prefixPath) continue;
273 
274  $principals[] = $row['uri'];
275 
276  }
277 
278  return $principals;
279 
280  }
281 
299  function findByUri($uri, $principalPrefix) {
300  $value = null;
301  $scheme = null;
302  list($scheme, $value) = explode(":", $uri, 2);
303  if (empty($value)) return null;
304 
305  $uri = null;
306  switch ($scheme){
307  case "mailto":
308  $query = 'SELECT uri FROM ' . $this->tableName . ' WHERE lower(email)=lower(?)';
309  $stmt = $this->pdo->prepare($query);
310  $stmt->execute([$value]);
311 
312  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
313  // Checking if the principal is in the prefix
314  list($rowPrefix) = URLUtil::splitPath($row['uri']);
315  if ($rowPrefix !== $principalPrefix) continue;
316 
317  $uri = $row['uri'];
318  break; //Stop on first match
319  }
320  break;
321  default:
322  //unsupported uri scheme
323  return null;
324  }
325  return $uri;
326  }
327 
334  function getGroupMemberSet($principal) {
335 
336  $principal = $this->getPrincipalByPath($principal);
337  if (!$principal) throw new DAV\Exception('Principal not found');
338 
339  $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM ' . $this->groupMembersTableName . ' AS groupmembers LEFT JOIN ' . $this->tableName . ' AS principals ON groupmembers.member_id = principals.id WHERE groupmembers.principal_id = ?');
340  $stmt->execute([$principal['id']]);
341 
342  $result = [];
343  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
344  $result[] = $row['uri'];
345  }
346  return $result;
347 
348  }
349 
356  function getGroupMembership($principal) {
357 
358  $principal = $this->getPrincipalByPath($principal);
359  if (!$principal) throw new DAV\Exception('Principal not found');
360 
361  $stmt = $this->pdo->prepare('SELECT principals.uri as uri FROM ' . $this->groupMembersTableName . ' AS groupmembers LEFT JOIN ' . $this->tableName . ' AS principals ON groupmembers.principal_id = principals.id WHERE groupmembers.member_id = ?');
362  $stmt->execute([$principal['id']]);
363 
364  $result = [];
365  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
366  $result[] = $row['uri'];
367  }
368  return $result;
369 
370  }
371 
381  function setGroupMemberSet($principal, array $members) {
382 
383  // Grabbing the list of principal id's.
384  $stmt = $this->pdo->prepare('SELECT id, uri FROM ' . $this->tableName . ' WHERE uri IN (? ' . str_repeat(', ? ', count($members)) . ');');
385  $stmt->execute(array_merge([$principal], $members));
386 
387  $memberIds = [];
388  $principalId = null;
389 
390  while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
391  if ($row['uri'] == $principal) {
392  $principalId = $row['id'];
393  } else {
394  $memberIds[] = $row['id'];
395  }
396  }
397  if (!$principalId) throw new DAV\Exception('Principal not found');
398 
399  // Wiping out old members
400  $stmt = $this->pdo->prepare('DELETE FROM ' . $this->groupMembersTableName . ' WHERE principal_id = ?;');
401  $stmt->execute([$principalId]);
402 
403  foreach ($memberIds as $memberId) {
404 
405  $stmt = $this->pdo->prepare('INSERT INTO ' . $this->groupMembersTableName . ' (principal_id, member_id) VALUES (?, ?);');
406  $stmt->execute([$principalId, $memberId]);
407 
408  }
409 
410  }
411 
423  function createPrincipal($path, MkCol $mkCol) {
424 
425  $stmt = $this->pdo->prepare('INSERT INTO ' . $this->tableName . ' (uri) VALUES (?)');
426  $stmt->execute([$path]);
427  $this->updatePrincipal($path, $mkCol);
428 
429  }
430 
431 }
$path
Definition: aliased.php:25
findByUri($uri, $principalPrefix)
Finds a principal by its URI.
Definition: PDO.php:299
This class represents a set of properties that are going to be updated.
Definition: PropPatch.php:20
$result
$stmt
getPrincipalsByPrefix($prefixPath)
Returns a list of principals based on a prefix.
Definition: PDO.php:92
Implement this interface to add support for creating new principals to your principal backend...
createPrincipal($path, MkCol $mkCol)
Creates a new principal.
Definition: PDO.php:423
searchPrincipals($prefixPath, array $searchProperties, $test='allof')
This method is used to search for principals matching a set of properties.
Definition: PDO.php:242
Main Exception class.
Definition: Exception.php:18
$values
__construct(\PDO $pdo)
This is the users&#39; primary email-address.
Definition: PDO.php:70
$query
$row
updatePrincipal($path, DAV\PropPatch $propPatch)
Updates one ore more webdav properties on a principal.
Definition: PDO.php:179
This class represents a MKCOL operation.
Definition: MkCol.php:23
getGroupMemberSet($principal)
Returns the list of members for a group-principal.
Definition: PDO.php:334
getGroupMembership($principal)
Returns the list of groups a principal is a member of.
Definition: PDO.php:356
$key
Definition: croninfo.php:18
setGroupMemberSet($principal, array $members)
Updates the list of group members for a group principal.
Definition: PDO.php:381
static splitPath($path)
Returns the &#39;dirname&#39; and &#39;basename&#39; for a path.
Definition: URLUtil.php:83
getPrincipalByPath($path)
Returns a specific principal, specified by it&#39;s path.
Definition: PDO.php:135
$test
Definition: Utf8Test.php:84