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

CAS/PGTStorage/pgt-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 PGTStorageFile extends PGTStorage
00046 {
00058   var $_path;
00059 
00068   function getPath()
00069     {
00070       return $this->_path;
00071     }
00072 
00079   var $_format;
00080 
00088   function getFormat()
00089     {
00090       return $this->_format;
00091     }
00092 
00093   // ########################################################################
00094   //  DEBUGGING
00095   // ########################################################################
00096   
00104   function getStorageType()
00105     {
00106       return "file";
00107     }
00108 
00116   function getStorageInfo()
00117     {
00118       return 'path=`'.$this->getPath().'\', format=`'.$this->getFormat().'\'';
00119     }
00120 
00121   // ########################################################################
00122   //  CONSTRUCTOR
00123   // ########################################################################
00124   
00134   function PGTStorageFile($cas_parent,$format,$path)
00135     {
00136       phpCAS::traceBegin();
00137       // call the ancestor's constructor
00138       $this->PGTStorage($cas_parent);
00139 
00140       if (empty($format) ) $format = CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT;
00141       if (empty($path) ) $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
00142 
00143       // check that the path is an absolute path
00144       if (getenv("OS")=="Windows_NT"){
00145         
00146          if (!preg_match('`^[a-zA-Z]:`', $path)) {
00147                 phpCAS::error('an absolute path is needed for PGT storage to file');
00148         }
00149         
00150       }
00151       else
00152       {
00153       
00154         if ( $path[0] != '/' ) {
00155                         phpCAS::error('an absolute path is needed for PGT storage to file');
00156         }
00157 
00158         // store the path (with a leading and trailing '/')      
00159         $path = preg_replace('|[/]*$|','/',$path);
00160         $path = preg_replace('|^[/]*|','/',$path);
00161       }
00162       
00163       $this->_path = $path;
00164       // check the format and store it
00165       switch ($format) {
00166       case CAS_PGT_STORAGE_FILE_FORMAT_PLAIN:
00167       case CAS_PGT_STORAGE_FILE_FORMAT_XML:
00168         $this->_format = $format;
00169         break;
00170       default:
00171         phpCAS::error('unknown PGT file storage format (`'.CAS_PGT_STORAGE_FILE_FORMAT_PLAIN.'\' and `'.CAS_PGT_STORAGE_FILE_FORMAT_XML.'\' allowed)');
00172       }
00173       phpCAS::traceEnd();      
00174     }
00175 
00176   // ########################################################################
00177   //  INITIALIZATION
00178   // ########################################################################
00179   
00185   function init()
00186     {
00187       phpCAS::traceBegin();
00188       // if the storage has already been initialized, return immediatly
00189       if ( $this->isInitialized() )
00190         return;
00191       // call the ancestor's method (mark as initialized)
00192       parent::init();
00193       phpCAS::traceEnd();      
00194     }
00195 
00196   // ########################################################################
00197   //  PGT I/O
00198   // ########################################################################
00199 
00208   function getPGTIouFilename($pgt_iou)
00209     {
00210       phpCAS::traceBegin();
00211       $filename = $this->getPath().$pgt_iou.'.'.$this->getFormat();
00212       phpCAS::traceEnd($filename);
00213       return $filename;
00214     }
00215   
00225   function write($pgt,$pgt_iou)
00226           {
00227           phpCAS::traceBegin();
00228           $fname = $this->getPGTIouFilename($pgt_iou);
00229           if(!file_exists($fname)){
00230                   if ( $f=fopen($fname,"w") ) {
00231                           if ( fputs($f,$pgt) === FALSE ) {
00232                                   phpCAS::error('could not write PGT to `'.$fname.'\'');
00233                           }
00234                           fclose($f);
00235                   } else {
00236                           phpCAS::error('could not open `'.$fname.'\'');
00237                   }
00238           }else{
00239                   phpCAS::error('File exists: `'.$fname.'\'');
00240           }
00241           phpCAS::traceEnd();      
00242           }
00243 
00254   function read($pgt_iou)
00255           {
00256           phpCAS::traceBegin();
00257           $pgt = FALSE;
00258           $fname = $this->getPGTIouFilename($pgt_iou);
00259           if (file_exists($fname)){
00260                   if ( !($f=fopen($fname,"r")) ) {
00261                           phpCAS::trace('could not open `'.$fname.'\'');
00262                   } else {
00263                           if ( ($pgt=fgets($f)) === FALSE ) {
00264                                   phpCAS::trace('could not read PGT from `'.$fname.'\'');
00265                           } 
00266                           fclose($f);
00267                   }
00268                   
00269                   // delete the PGT file
00270                   @unlink($fname);
00271           }else{
00272                   phpCAS::trace('No such file `'.$fname.'\'');
00273           }
00274           phpCAS::traceEnd($pgt);
00275           return $pgt;
00276           }
00277   
00280 }
00281 
00282   
00283 ?>

Generated on Mon Oct 4 2010 08:11:25 for phpCAS by  doxygen 1.7.1