ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Db.php
Go to the documentation of this file.
1 <?php
2 
30 define('CAS_PGT_STORAGE_DB_DEFAULT_TABLE', 'cas_pgts');
31 
47 {
56  private $_pdo;
57 
63  private function _getPdo()
64  {
65  return $this->_pdo;
66  }
67 
71  private $_dsn;
72  private $_username;
73  private $_password;
74  private $_table_options;
75 
79  private $_table;
80 
86  private function _getTable()
87  {
88  return $this->_table;
89  }
90 
91  // ########################################################################
92  // DEBUGGING
93  // ########################################################################
94 
101  public function getStorageType()
102  {
103  return "db";
104  }
105 
113  public function getStorageInfo()
114  {
115  return 'table=`'.$this->_getTable().'\'';
116  }
117 
118  // ########################################################################
119  // CONSTRUCTOR
120  // ########################################################################
121 
138  public function __construct(
139  $cas_parent, $dsn_or_pdo, $username='', $password='', $table='',
140  $driver_options=null
141  ) {
143  // call the ancestor's constructor
144  parent::__construct($cas_parent);
145 
146  // set default values
147  if ( empty($table) ) {
149  }
150  if ( !is_array($driver_options) ) {
151  $driver_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
152  }
153 
154  // store the specified parameters
155  if ($dsn_or_pdo instanceof PDO) {
156  $this->_pdo = $dsn_or_pdo;
157  } else {
158  $this->_dsn = $dsn_or_pdo;
159  $this->_username = $username;
160  $this->_password = $password;
161  $this->_driver_options = $driver_options;
162  }
163 
164  // store the table name
165  $this->_table = $table;
166 
168  }
169 
170  // ########################################################################
171  // INITIALIZATION
172  // ########################################################################
173 
179  public function init()
180  {
182  // if the storage has already been initialized, return immediatly
183  if ($this->isInitialized()) {
184  return;
185  }
186 
187  // initialize the base object
188  parent::init();
189 
190  // create the PDO object if it doesn't exist already
191  if (!($this->_pdo instanceof PDO)) {
192  try {
193  $this->_pdo = new PDO(
194  $this->_dsn, $this->_username, $this->_password,
195  $this->_driver_options
196  );
197  }
198  catch(PDOException $e) {
199  phpCAS::error('Database connection error: ' . $e->getMessage());
200  }
201  }
202 
204  }
205 
206  // ########################################################################
207  // PDO database interaction
208  // ########################################################################
209 
214  private $_errMode;
215 
221  private function _setErrorMode()
222  {
223  // get PDO object and enable exception error mode
224  $pdo = $this->_getPdo();
225  $this->_errMode = $pdo->getAttribute(PDO::ATTR_ERRMODE);
226  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
227  }
228 
234  private function _resetErrorMode()
235  {
236  // get PDO object and reset the error mode to what it was originally
237  $pdo = $this->_getPdo();
238  $pdo->setAttribute(PDO::ATTR_ERRMODE, $this->_errMode);
239  }
240 
241  // ########################################################################
242  // database queries
243  // ########################################################################
244  // these queries are potentially unsafe because the person using this library
245  // can set the table to use, but there is no reliable way to escape SQL
246  // fieldnames in PDO yet
247 
253  protected function createTableSql()
254  {
255  return 'CREATE TABLE ' . $this->_getTable()
256  . ' (pgt_iou VARCHAR(255) NOT NULL PRIMARY KEY, pgt VARCHAR(255) NOT NULL)';
257  }
258 
265  protected function storePgtSql()
266  {
267  return 'INSERT INTO ' . $this->_getTable()
268  . ' (pgt_iou, pgt) VALUES (:pgt_iou, :pgt)';
269  }
270 
278  protected function retrievePgtSql()
279  {
280  return 'SELECT pgt FROM ' . $this->_getTable() . ' WHERE pgt_iou = :pgt_iou';
281  }
282 
289  protected function deletePgtSql()
290  {
291  return 'DELETE FROM ' . $this->_getTable() . ' WHERE pgt_iou = :pgt_iou';
292  }
293 
294  // ########################################################################
295  // PGT I/O
296  // ########################################################################
297 
303  public function createTable()
304  {
306 
307  // initialize this PGTStorage object if it hasn't been initialized yet
308  if ( !$this->isInitialized() ) {
309  $this->init();
310  }
311 
312  // initialize the PDO object for this method
313  $pdo = $this->_getPdo();
314  $this->_setErrorMode();
315 
316  try {
317  $pdo->beginTransaction();
318 
319  $query = $pdo->query($this->createTableSQL());
320  $query->closeCursor();
321 
322  $pdo->commit();
323  }
324  catch(PDOException $e) {
325  // attempt rolling back the transaction before throwing a phpCAS error
326  try {
327  $pdo->rollBack();
328  }
329  catch(PDOException $e) {
330  }
331  phpCAS::error('error creating PGT storage table: ' . $e->getMessage());
332  }
333 
334  // reset the PDO object
335  $this->_resetErrorMode();
336 
338  }
339 
349  public function write($pgt, $pgt_iou)
350  {
352 
353  // initialize the PDO object for this method
354  $pdo = $this->_getPdo();
355  $this->_setErrorMode();
356 
357  try {
358  $pdo->beginTransaction();
359 
360  $query = $pdo->prepare($this->storePgtSql());
361  $query->bindValue(':pgt', $pgt, PDO::PARAM_STR);
362  $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
363  $query->execute();
364  $query->closeCursor();
365 
366  $pdo->commit();
367  }
368  catch(PDOException $e) {
369  // attempt rolling back the transaction before throwing a phpCAS error
370  try {
371  $pdo->rollBack();
372  }
373  catch(PDOException $e) {
374  }
375  phpCAS::error('error writing PGT to database: ' . $e->getMessage());
376  }
377 
378  // reset the PDO object
379  $this->_resetErrorMode();
380 
382  }
383 
392  public function read($pgt_iou)
393  {
395  $pgt = false;
396 
397  // initialize the PDO object for this method
398  $pdo = $this->_getPdo();
399  $this->_setErrorMode();
400 
401  try {
402  $pdo->beginTransaction();
403 
404  // fetch the pgt for the specified pgt_iou
405  $query = $pdo->prepare($this->retrievePgtSql());
406  $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
407  $query->execute();
408  $pgt = $query->fetchColumn(0);
409  $query->closeCursor();
410 
411  // delete the specified pgt_iou from the database
412  $query = $pdo->prepare($this->deletePgtSql());
413  $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
414  $query->execute();
415  $query->closeCursor();
416 
417  $pdo->commit();
418  }
419  catch(PDOException $e) {
420  // attempt rolling back the transaction before throwing a phpCAS error
421  try {
422  $pdo->rollBack();
423  }
424  catch(PDOException $e) {
425  }
426  phpCAS::trace('error reading PGT from database: ' . $e->getMessage());
427  }
428 
429  // reset the PDO object
430  $this->_resetErrorMode();
431 
433  return $pgt;
434  }
435 
438 }
439 
440 ?>
_setErrorMode()
This method will enable the Exception error mode on the PDO object.
Definition: Db.php:221
createTableSql()
This method returns the query used to create a pgt storage table.
Definition: Db.php:253
Basic class for PGT database storage The CAS_PGTStorage_Db class is a class for PGT database storage...
Definition: Db.php:46
__construct( $cas_parent, $dsn_or_pdo, $username='', $password='', $table='', $driver_options=null)
The class constructor.
Definition: Db.php:138
$_pdo
the PDO object to use for database interactions
Definition: Db.php:56
_resetErrorMode()
this method will reset the error mode on the PDO object
Definition: Db.php:234
retrievePgtSql()
This method returns the query used to retrieve a pgt.
Definition: Db.php:278
static error($msg)
This method is used by interface methods to print an error and where the function was originally call...
Definition: CAS.php:543
getStorageType()
This method returns an informational string giving the type of storage used by the object (used for d...
Definition: Db.php:101
deletePgtSql()
This method returns the query used to delete a pgt.
Definition: Db.php:289
$_dsn
database connection options to use when creating a new PDO object
Definition: Db.php:71
init()
This method is used to initialize the storage.
Definition: Db.php:179
isInitialized()
This method tells if the storage has already been intialized.
static traceEnd($res='')
This method is used to indicate the end of the execution of a function in debug mode.
Definition: CAS.php:638
static trace($str)
This method is used to log something in debug mode.
Definition: CAS.php:579
getStorageInfo()
This method returns an informational string giving informations on the parameters of the storage...
Definition: Db.php:113
$_table
the table to use for storing/retrieving pgt&#39;s
Definition: Db.php:79
Basic class for PGT storage The CAS_PGTStorage_AbstractStorage class is a generic class for PGT stora...
read($pgt_iou)
This method reads a PGT corresponding to a PGT Iou and deletes the corresponding db entry...
Definition: Db.php:392
storePgtSql()
This method returns the query used to store a pgt.
Definition: Db.php:265
_getTable()
This method returns the table to use when storing/retrieving PGT&#39;s.
Definition: Db.php:86
write($pgt, $pgt_iou)
This method stores a PGT and its corresponding PGT Iou in the database.
Definition: Db.php:349
Create styles array
The data for the language used.
_getPdo()
This method returns the PDO object to use for database interactions.
Definition: Db.php:63
static traceBegin()
This method is used to indicate the start of the execution of a function in debug mode...
Definition: CAS.php:591
createTable()
This method creates the database table used to store pgt&#39;s and pgtiou&#39;s.
Definition: Db.php:303
const CAS_PGT_STORAGE_DB_DEFAULT_TABLE
Definition: Db.php:30
$_errMode
attribute that stores the previous error mode for the PDO handle while processing a transaction ...
Definition: Db.php:214