• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • Examples
  • File List
  • File Members

CAS/PGTStorage/pgt-db.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  *     * Redistributions of source code must retain the above copyright notice,
00010  *       this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright notice,
00012  *       this list of conditions and the following disclaimer in the documentation
00013  *       and/or other materials provided with the distribution.
00014  *     * Neither the name of the ESUP-Portail consortium & the JA-SIG
00015  *       Collaborative nor the names of its contributors may be used to endorse or
00016  *       promote products derived from this software without specific prior
00017  *       written permission.
00018 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00023  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00026  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00044 define('CAS_PGT_STORAGE_DB_DEFAULT_TABLE', 'cas_pgts');
00045 
00046 class CAS_PGTStorageDb extends CAS_PGTStorage
00047 {
00056         private $_pdo;
00057 
00063         private function getPdo()
00064         {
00065                 return $this->_pdo;
00066         }
00067 
00071         private $_dsn;
00072         private $_username;
00073         private $_password;
00074         private $_table_options;
00075 
00079         private $_table;
00080 
00086         private function getTable()
00087         {
00088                 return $this->_table;
00089         }
00090 
00091         // ########################################################################
00092         //  DEBUGGING
00093         // ########################################################################
00094 
00101         public function getStorageType()
00102         {
00103                 return "db";
00104         }
00105 
00113         public function getStorageInfo()
00114         {
00115                 return 'table=`'.$this->getTable().'\'';
00116         }
00117 
00118         // ########################################################################
00119         //  CONSTRUCTOR
00120         // ########################################################################
00121 
00132         public function __construct($cas_parent, $dsn_or_pdo, $username='', $password='', $table='', $driver_options=null)
00133         {
00134                 phpCAS::traceBegin();
00135                 // call the ancestor's constructor
00136                 parent::__construct($cas_parent);
00137 
00138                 // set default values
00139                 if ( empty($table) ) $table = CAS_PGT_STORAGE_DB_DEFAULT_TABLE;
00140                 if ( !is_array($driver_options) ) $driver_options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
00141 
00142                 // store the specified parameters
00143                 if($dsn_or_pdo instanceof PDO) {
00144                         $this->_pdo = $dsn_or_pdo;
00145                 }
00146                 else {
00147                         $this->_dsn = $dsn_or_pdo;
00148                         $this->_username = $username;
00149                         $this->_password = $password;
00150                         $this->_driver_options = $driver_options;
00151                 }
00152 
00153                 // store the table name
00154                 $this->_table = $table;
00155 
00156                 phpCAS::traceEnd();
00157         }
00158 
00159         // ########################################################################
00160         //  INITIALIZATION
00161         // ########################################################################
00162 
00166         public function init()
00167         {
00168                 phpCAS::traceBegin();
00169                 // if the storage has already been initialized, return immediatly
00170                 if ( $this->isInitialized() )
00171                 return;
00172 
00173                 // initialize the base object
00174                 parent::init();
00175 
00176                 // create the PDO object if it doesn't exist already
00177                 if(!($this->_pdo instanceof PDO)) {
00178                         try {
00179                                 $this->_pdo = new PDO($this->_dsn, $this->_username, $this->_password, $this->_driver_options);
00180                         }
00181                         catch(PDOException $e) {
00182                                 phpCAS::error('Database connection error: ' . $e->getMessage());
00183                         }
00184                 }
00185 
00186                 phpCAS::traceEnd();
00187         }
00188 
00189         // ########################################################################
00190         //  PDO database interaction
00191         // ########################################################################
00192 
00196         private $_errMode;
00197 
00201         private function setErrorMode()
00202         {
00203                 // get PDO object and enable exception error mode
00204                 $pdo = $this->getPdo();
00205                 $this->_errMode = $pdo->getAttribute(PDO::ATTR_ERRMODE);
00206                 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
00207         }
00208 
00212         private function resetErrorMode()
00213         {
00214                 // get PDO object and reset the error mode to what it was originally
00215                 $pdo = $this->getPdo();
00216                 $pdo->setAttribute(PDO::ATTR_ERRMODE, $this->_errMode);
00217         }
00218 
00219         // ########################################################################
00220         //  database queries
00221         // ########################################################################
00222         // these queries are potentially unsafe because the person using this library
00223         // can set the table to use, but there is no reliable way to escape SQL
00224         // fieldnames in PDO yet
00225 
00231         protected function _createTableSql()
00232         {
00233                 return 'CREATE TABLE `' . $this->getTable() . '` (pgt_iou VARCHAR(255) NOT NULL PRIMARY KEY, pgt VARCHAR(255) NOT NULL)';
00234         }
00235 
00241         protected function _storePgtSql()
00242         {
00243                 return 'INSERT INTO `' . $this->getTable() . '` (pgt_iou, pgt) VALUES (:pgt_iou, :pgt)';
00244         }
00245 
00251         protected function _retrievePgtSql()
00252         {
00253                 return 'SELECT pgt FROM `' . $this->getTable() . '` WHERE pgt_iou = :pgt_iou';
00254         }
00255 
00261         protected function _deletePgtSql()
00262         {
00263                 return 'DELETE FROM `' . $this->getTable() . '` WHERE pgt_iou = :pgt_iou';
00264         }
00265 
00266         // ########################################################################
00267         //  PGT I/O
00268         // ########################################################################
00269 
00273         public function createTable()
00274         {
00275                 phpCAS::traceBegin();
00276 
00277                 // initialize the PDO object for this method
00278                 $pdo = $this->getPdo();
00279                 $this->setErrorMode();
00280 
00281                 try {
00282                         $pdo->beginTransaction();
00283 
00284                         $query = $pdo->query($this->_createTableSQL());
00285                         $query->closeCursor();
00286 
00287                         $pdo->commit();
00288                 }
00289                 catch(PDOException $e) {
00290                         // attempt rolling back the transaction before throwing a phpCAS error
00291                         try {
00292                                 $pdo->rollBack();
00293                         }
00294                         catch(PDOException $e) {}
00295                         phpCAS::error('error creating PGT storage table: ' . $e->getMessage());
00296                 }
00297 
00298                 // reset the PDO object
00299                 $this->resetErrorMode();
00300 
00301                 phpCAS::traceEnd();
00302         }
00303 
00311         public function write($pgt, $pgt_iou)
00312         {
00313                 phpCAS::traceBegin();
00314 
00315                 // initialize the PDO object for this method
00316                 $pdo = $this->getPdo();
00317                 $this->setErrorMode();
00318 
00319                 try {
00320                         $pdo->beginTransaction();
00321 
00322                         $query = $pdo->prepare($this->_storePgtSql());
00323                         $query->bindValue(':pgt', $pgt, PDO::PARAM_STR);
00324                         $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
00325                         $query->execute();
00326                         $query->closeCursor();
00327 
00328                         $pdo->commit();
00329                 }
00330                 catch(PDOException $e) {
00331                         // attempt rolling back the transaction before throwing a phpCAS error
00332                         try {
00333                                 $pdo->rollBack();
00334                         }
00335                         catch(PDOException $e) {}
00336                         phpCAS::error('error writing PGT to database: ' . $e->getMessage());
00337                 }
00338 
00339                 // reset the PDO object
00340                 $this->resetErrorMode();
00341 
00342                 phpCAS::traceEnd();
00343         }
00344 
00353         public function read($pgt_iou)
00354         {
00355                 phpCAS::traceBegin();
00356                 $pgt = FALSE;
00357 
00358                 // initialize the PDO object for this method
00359                 $pdo = $this->getPdo();
00360                 $this->setErrorMode();
00361 
00362                 try {
00363                         $pdo->beginTransaction();
00364 
00365                         // fetch the pgt for the specified pgt_iou
00366                         $query = $pdo->prepare($this->_retrievePgtSql());
00367                         $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
00368                         $query->execute();
00369                         $pgt = $query->fetchColumn(0);
00370                         $query->closeCursor();
00371 
00372                         // delete the specified pgt_iou from the database
00373                         $query = $pdo->prepare($this->_deletePgtSql());
00374                         $query->bindValue(':pgt_iou', $pgt_iou, PDO::PARAM_STR);
00375                         $query->execute();
00376                         $query->closeCursor();
00377 
00378                         $pdo->commit();
00379                 }
00380                 catch(PDOException $e) {
00381                         // attempt rolling back the transaction before throwing a phpCAS error
00382                         try {
00383                                 $pdo->rollBack();
00384                         }
00385                         catch(PDOException $e) {}
00386                         phpCAS::trace('error reading PGT from database: ' . $e->getMessage());
00387                 }
00388 
00389                 // reset the PDO object
00390                 $this->resetErrorMode();
00391 
00392                 phpCAS::traceEnd();
00393                 return $pgt;
00394         }
00395 
00398 }
00399 
00400 ?>

Generated on Sat Mar 26 2011 12:11:03 for phpCAS by  doxygen 1.7.1