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

CAS/PGTStorage/File.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  */
00045 class CAS_PGTStorage_File extends CAS_PGTStorage_AbstractStorage
00046 {
00058         var $_path;
00059 
00068         function getPath()
00069         {
00070                 return $this->_path;
00071         }
00072 
00073         // ########################################################################
00074         //  DEBUGGING
00075         // ########################################################################
00076 
00084         function getStorageType()
00085         {
00086                 return "file";
00087         }
00088 
00096         function getStorageInfo()
00097         {
00098                 return 'path=`'.$this->getPath().'\'';
00099         }
00100 
00101         // ########################################################################
00102         //  CONSTRUCTOR
00103         // ########################################################################
00104 
00113         function __construct($cas_parent,$path)
00114         {
00115                 phpCAS::traceBegin();
00116                 // call the ancestor's constructor
00117                 parent::__construct($cas_parent);
00118                 
00119                 if (empty($path) ) $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
00120                 // check that the path is an absolute path
00121                 if (getenv("OS")=="Windows_NT"){
00122                          
00123                         if (!preg_match('`^[a-zA-Z]:`', $path)) {
00124                                 phpCAS::error('an absolute path is needed for PGT storage to file');
00125                         }
00126                          
00127                 }
00128                 else
00129                 {
00130 
00131                         if ( $path[0] != '/' ) {
00132                                 phpCAS::error('an absolute path is needed for PGT storage to file');
00133                         }
00134 
00135                         // store the path (with a leading and trailing '/')
00136                         $path = preg_replace('|[/]*$|','/',$path);
00137                         $path = preg_replace('|^[/]*|','/',$path);
00138                 }
00139 
00140                 $this->_path = $path;
00141                 phpCAS::traceEnd();
00142         }
00143 
00144         // ########################################################################
00145         //  INITIALIZATION
00146         // ########################################################################
00147 
00153         function init()
00154         {
00155                 phpCAS::traceBegin();
00156                 // if the storage has already been initialized, return immediatly
00157                 if ( $this->isInitialized() )
00158                 return;
00159                 // call the ancestor's method (mark as initialized)
00160                 parent::init();
00161                 phpCAS::traceEnd();
00162         }
00163 
00164         // ########################################################################
00165         //  PGT I/O
00166         // ########################################################################
00167 
00176         function getPGTIouFilename($pgt_iou)
00177         {
00178                 phpCAS::traceBegin();
00179                 $filename = $this->getPath().$pgt_iou.'.plain';
00180                 phpCAS::traceEnd($filename);
00181                 return $filename;
00182         }
00183 
00193         function write($pgt,$pgt_iou)
00194         {
00195                 phpCAS::traceBegin();
00196                 $fname = $this->getPGTIouFilename($pgt_iou);
00197                 if(!file_exists($fname)){
00198                         if ($f=fopen($fname,"w") ) {
00199                                 if ( fputs($f,$pgt) === FALSE ) {
00200                                         phpCAS::error('could not write PGT to `'.$fname.'\'');
00201                                 }
00202                                 fclose($f);
00203                         } else {
00204                                 phpCAS::error('could not open `'.$fname.'\'');
00205                         }
00206                 }else{
00207                         phpCAS::error('File exists: `'.$fname.'\'');
00208                 }
00209                 phpCAS::traceEnd();
00210         }
00211 
00222         function read($pgt_iou)
00223         {
00224                 phpCAS::traceBegin();
00225                 $pgt = FALSE;
00226                 $fname = $this->getPGTIouFilename($pgt_iou);
00227                 if (file_exists($fname)){
00228                         if ( !($f=fopen($fname,"r")) ) {
00229                                 phpCAS::trace('could not open `'.$fname.'\'');
00230                         } else {
00231                                 if ( ($pgt=fgets($f)) === FALSE ) {
00232                                         phpCAS::trace('could not read PGT from `'.$fname.'\'');
00233                                 }
00234                                 fclose($f);
00235                         }
00236                         
00237                         // delete the PGT file
00238                         @unlink($fname);
00239                 }else{
00240                         phpCAS::trace('No such file `'.$fname.'\'');
00241                 }
00242                 phpCAS::traceEnd($pgt);
00243                 return $pgt;
00244         }
00245 
00248 }
00249 
00250 
00251 ?>

Generated on Sun Jun 5 2011 19:05:26 for phpCAS by  doxygen 1.7.1