phpCAS version 1.3.6

Db.php
Go to the documentation of this file.
1 <?php
2
30 define('CAS_PGT_STORAGE_DB_DEFAULT_TABLE', 'cas_pgts');
31
46 class CAS_PGTStorage_Db extends CAS_PGTStorage_AbstractStorage
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 ) {
142 phpCAS::traceBegin();
143 // call the ancestor's constructor
144 parent::__construct($cas_parent);
145
146 // set default values
147 if ( empty($table) ) {
148 $table = CAS_PGT_STORAGE_DB_DEFAULT_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
167 phpCAS::traceEnd();
168 }
169
170 // ########################################################################
171 // INITIALIZATION
172 // ########################################################################
173
179 public function init()
180 {
181 phpCAS::traceBegin();
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
203 phpCAS::traceEnd();
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 {
305 phpCAS::traceBegin();
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
337 phpCAS::traceEnd();
338 }
339
349 public function write($pgt, $pgt_iou)
350 {
351 phpCAS::traceBegin();
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
381 phpCAS::traceEnd();
382 }
383
392 public function read($pgt_iou)
393 {
394 phpCAS::traceBegin();
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
432 phpCAS::traceEnd();
433 return $pgt;
434 }
435
438 }
439
440 ?>
CAS_PGTStorage_Db\_setErrorMode
_setErrorMode()
Definition: Db.php:221
CAS_PGTStorage_Db\createTableSql
createTableSql()
Definition: Db.php:253
CAS_PGTStorage_Db
Definition: Db.php:46
CAS_PGTStorage_Db\__construct
__construct( $cas_parent, $dsn_or_pdo, $username='', $password='', $table='', $driver_options=null)
Definition: Db.php:138
CAS_PGTStorage_Db\$_pdo
$_pdo
Definition: Db.php:56
CAS_PGTStorage_Db\_resetErrorMode
_resetErrorMode()
Definition: Db.php:234
CAS_PGTStorage_Db\retrievePgtSql
retrievePgtSql()
Definition: Db.php:278
phpCAS\error
static error($msg)
Definition: CAS.php:543
CAS_PGTStorage_Db\getStorageType
getStorageType()
Definition: Db.php:101
CAS_PGTStorage_Db\$_password
$_password
Definition: Db.php:73
CAS_PGTStorage_Db\deletePgtSql
deletePgtSql()
Definition: Db.php:289
CAS_PGTStorage_Db\$_dsn
$_dsn
Definition: Db.php:71
CAS_PGTStorage_Db\init
init()
Definition: Db.php:179
CAS_PGTStorage_AbstractStorage\isInitialized
isInitialized()
Definition: AbstractStorage.php:168
CAS_PGTStorage_Db\$_username
$_username
Definition: Db.php:72
phpCAS\traceEnd
static traceEnd($res='')
Definition: CAS.php:638
phpCAS\trace
static trace($str)
Definition: CAS.php:579
CAS_PGTStorage_Db\getStorageInfo
getStorageInfo()
Definition: Db.php:113
CAS_PGTStorage_Db\$_table
$_table
Definition: Db.php:79
CAS_PGTStorage_AbstractStorage
Definition: AbstractStorage.php:46
CAS_PGTStorage_Db\read
read($pgt_iou)
Definition: Db.php:392
CAS_PGTStorage_Db\storePgtSql
storePgtSql()
Definition: Db.php:265
CAS_PGTStorage_Db\_getTable
_getTable()
Definition: Db.php:86
CAS_PGTStorage_Db\$_table_options
$_table_options
Definition: Db.php:74
$driver_options
$driver_options
Definition: config.example.php:59
CAS_PGTStorage_Db\write
write($pgt, $pgt_iou)
Definition: Db.php:349
CAS_PGTStorage_Db\_getPdo
_getPdo()
Definition: Db.php:63
phpCAS\traceBegin
static traceBegin()
Definition: CAS.php:591
CAS_PGTStorage_Db\createTable
createTable()
Definition: Db.php:303
CAS_PGT_STORAGE_DB_DEFAULT_TABLE
const CAS_PGT_STORAGE_DB_DEFAULT_TABLE
Definition: Db.php:30
CAS_PGTStorage_Db\$_errMode
$_errMode
Definition: Db.php:214